-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
54 lines (43 loc) · 1.12 KB
/
state.go
File metadata and controls
54 lines (43 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
)
// lexer holds the state of the scanner.
type lexer struct {
name string // used only for error reports.
input string // the string being scanned.
start int // start position of this item.
pos int // current position in the input.
width int // width of last rune read from input.
// items chan item // channel of scanned items.
}
// stateFn represents the state of the scanner
// as a function that returns the next state.
type stateFn func(*lexer) stateFn
func lexText(l *lexer) stateFn {
fmt.Println("lexText")
return lexA // Next state.
}
func lexA(l *lexer) stateFn {
fmt.Println("lexA")
return lexB // Next state.
}
func lexB(l *lexer) stateFn {
fmt.Println("lexB")
return nil // Next state.
}
// run lexes the input by executing state functions until
// the state is nil.
func (l *lexer) run() {
for state := lexText; state != nil; {
state = state(l)
}
// close(l.items) // No more tokens will be delivered.
}
func main() {
l := &lexer{
name: "test",
input: "abcd",
}
l.run()
}