本文整理汇总了Golang中io.Reader.ReadRune方法的典型用法代码示例。如果您正苦于以下问题:Golang Reader.ReadRune方法的具体用法?Golang Reader.ReadRune怎么用?Golang Reader.ReadRune使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.Reader
的用法示例。
在下文中一共展示了Reader.ReadRune方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: newLexerWithInit
// newLexerWithInit creates a new lexer object, runs the given callback on it,
// then returns it.
func newLexerWithInit(in io.Reader, initFun func(*lexer)) *lexer {
type dfa struct {
acc []bool // Accepting states.
f []func(rune) int // Transitions.
startf, endf []int // Transitions at start and end of input.
nest []dfa
}
yylex := new(lexer)
if initFun != nil {
initFun(yylex)
}
yylex.ch = make(chan frame)
var scan func(in *bufio.Reader, ch chan frame, family []dfa, line, column int)
scan = func(in *bufio.Reader, ch chan frame, family []dfa, line, column int) {
// Index of DFA and length of highest-precedence match so far.
matchi, matchn := 0, -1
var buf []rune
n := 0
checkAccept := func(i int, st int) bool {
// Higher precedence match? DFAs are run in parallel, so matchn is at most len(buf), hence we may omit the length equality check.
if family[i].acc[st] && (matchn < n || matchi > i) {
matchi, matchn = i, n
return true
}
return false
}
var state [][2]int
for i := 0; i < len(family); i++ {
mark := make([]bool, len(family[i].startf))
// Every DFA starts at state 0.
st := 0
for {
state = append(state, [2]int{i, st})
mark[st] = true
// As we're at the start of input, follow all ^ transitions and append to our list of start states.
st = family[i].startf[st]
if -1 == st || mark[st] {
break
}
// We only check for a match after at least one transition.
checkAccept(i, st)
}
}
atEOF := false
for {
if n == len(buf) && !atEOF {
r, _, err := in.ReadRune()
switch err {
case io.EOF:
atEOF = true
case nil:
buf = append(buf, r)
default:
panic(err)
}
}
if !atEOF {
r := buf[n]
n++
var nextState [][2]int
for _, x := range state {
x[1] = family[x[0]].f[x[1]](r)
if -1 == x[1] {
continue
}
nextState = append(nextState, x)
checkAccept(x[0], x[1])
}
state = nextState
} else {
dollar: // Handle $.
for _, x := range state {
mark := make([]bool, len(family[x[0]].endf))
for {
mark[x[1]] = true
x[1] = family[x[0]].endf[x[1]]
if -1 == x[1] || mark[x[1]] {
break
}
if checkAccept(x[0], x[1]) {
// Unlike before, we can break off the search. Now that we're at the end, there's no need to maintain the state of each DFA.
break dollar
}
}
}
state = nil
}
if state == nil {
lcUpdate := func(r rune) {
if r == '\n' {
line++
column = 0
} else {
column++
}
}
// All DFAs stuck. Return last match if it exists, otherwise advance by one rune and restart all DFAs.
//.........这里部分代码省略.........
示例2: NewLexerWithInit
// NewLexerWithInit creates a new Lexer object, runs the given callback on it,
// then returns it.
func NewLexerWithInit(in io.Reader, initFun func(*Lexer)) *Lexer {
type dfa struct {
acc []bool // Accepting states.
f []func(rune) int // Transitions.
startf, endf []int // Transitions at start and end of input.
nest []dfa
}
yylex := new(Lexer)
if initFun != nil {
initFun(yylex)
}
yylex.ch = make(chan intstring)
var scan func(in *bufio.Reader, ch chan intstring, family []dfa)
scan = func(in *bufio.Reader, ch chan intstring, family []dfa) {
// Index of DFA and length of highest-precedence match so far.
matchi, matchn := 0, -1
var buf []rune
n := 0
checkAccept := func(i int, st int) bool {
// Higher precedence match? DFAs are run in parallel, so matchn is at most len(buf), hence we may omit the length equality check.
if family[i].acc[st] && (matchn < n || matchi > i) {
matchi, matchn = i, n
return true
}
return false
}
var state [][2]int
for i := 0; i < len(family); i++ {
mark := make([]bool, len(family[i].startf))
// Every DFA starts at state 0.
st := 0
for {
state = append(state, [2]int{i, st})
mark[st] = true
// As we're at the start of input, follow all ^ transitions and append to our list of start states.
st = family[i].startf[st]
if -1 == st || mark[st] {
break
}
// We only check for a match after at least one transition.
checkAccept(i, st)
}
}
atEOF := false
for {
if n == len(buf) && !atEOF {
r, _, err := in.ReadRune()
switch err {
case io.EOF:
atEOF = true
case nil:
buf = append(buf, r)
default:
panic(err)
}
}
if !atEOF {
r := buf[n]
n++
var nextState [][2]int
for _, x := range state {
x[1] = family[x[0]].f[x[1]](r)
if -1 == x[1] {
continue
}
nextState = append(nextState, x)
checkAccept(x[0], x[1])
}
state = nextState
} else {
dollar: // Handle $.
for _, x := range state {
mark := make([]bool, len(family[x[0]].endf))
for {
mark[x[1]] = true
x[1] = family[x[0]].endf[x[1]]
if -1 == x[1] || mark[x[1]] {
break
}
if checkAccept(x[0], x[1]) {
// Unlike before, we can break off the search. Now that we're at the end, there's no need to maintain the state of each DFA.
break dollar
}
}
}
state = nil
}
if state == nil {
// All DFAs stuck. Return last match if it exists, otherwise advance by one rune and restart all DFAs.
if matchn == -1 {
if len(buf) == 0 { // This can only happen at the end of input.
break
}
buf = buf[1:]
} else {
text := string(buf[:matchn])
buf = buf[matchn:]
//.........这里部分代码省略.........
示例3: NewLexerWithInit
// NewLexerWithInit creates a new Lexer object, runs the given callback on it,
// then returns it.
func NewLexerWithInit(in io.Reader, initFun func(*Lexer)) *Lexer {
type dfa struct {
acc []bool // Accepting states.
f []func(rune) int // Transitions.
startf, endf []int // Transitions at start and end of input.
nest []dfa
}
yylex := new(Lexer)
if initFun != nil {
initFun(yylex)
}
yylex.ch = make(chan frame)
var scan func(in *bufio.Reader, ch chan frame, family []dfa, line, column int)
scan = func(in *bufio.Reader, ch chan frame, family []dfa, line, column int) {
// Index of DFA and length of highest-precedence match so far.
matchi, matchn := 0, -1
var buf []rune
n := 0
checkAccept := func(i int, st int) bool {
// Higher precedence match? DFAs are run in parallel, so matchn is at most len(buf), hence we may omit the length equality check.
if family[i].acc[st] && (matchn < n || matchi > i) {
matchi, matchn = i, n
return true
}
return false
}
var state [][2]int
for i := 0; i < len(family); i++ {
mark := make([]bool, len(family[i].startf))
// Every DFA starts at state 0.
st := 0
for {
state = append(state, [2]int{i, st})
mark[st] = true
// As we're at the start of input, follow all ^ transitions and append to our list of start states.
st = family[i].startf[st]
if -1 == st || mark[st] {
break
}
// We only check for a match after at least one transition.
checkAccept(i, st)
}
}
atEOF := false
for {
if n == len(buf) && !atEOF {
r, _, err := in.ReadRune()
switch err {
case io.EOF:
atEOF = true
case nil:
buf = append(buf, r)
default:
panic(err)
}
}
if !atEOF {
r := buf[n]
n++
var nextState [][2]int
for _, x := range state {
x[1] = family[x[0]].f[x[1]](r)
if -1 == x[1] {
continue
}
nextState = append(nextState, x)
checkAccept(x[0], x[1])
}
state = nextState
} else {
dollar: // Handle $.
for _, x := range state {
mark := make([]bool, len(family[x[0]].endf))
for {
mark[x[1]] = true
x[1] = family[x[0]].endf[x[1]]
if -1 == x[1] || mark[x[1]] {
break
}
if checkAccept(x[0], x[1]) {
// Unlike before, we can break off the search. Now that we're at the end, there's no need to maintain the state of each DFA.
break dollar
}
}
}
state = nil
}
if state == nil {
lcUpdate := func(r rune) {
if r == '\n' {
line++
column = 0
} else {
column++
}
}
// All DFAs stuck. Return last match if it exists, otherwise advance by one rune and restart all DFAs.
//.........这里部分代码省略.........