-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.go
157 lines (135 loc) · 2.58 KB
/
lexer.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"fmt"
"os"
"strings"
"unicode/utf8"
)
type Lexer struct {
emitter TokenEmitter
input string
start int
pos int
width int
}
func (l *Lexer) Current() string {
return l.input[l.start:l.pos]
}
func (l *Lexer) Emit(typ TokenType) {
token := &Token{
Type: typ,
Value: l.input[l.start:l.pos],
Start: l.start,
End: l.pos,
}
l.emitter.EmitToken(token)
l.start = l.pos
}
func (l *Lexer) Next() rune {
if l.pos >= len(l.input) {
l.width = 0
return 0
}
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = w
l.pos += l.width
return r
}
func (l *Lexer) Peek() rune {
r := l.Next()
l.Backup()
return r
}
func (l *Lexer) PeekN(n int) string {
end := l.pos + n
if end > len(l.input) {
end = len(l.input)
}
return l.input[l.pos:end]
}
func (l *Lexer) NextN(n int) string {
end := l.pos + n
if end > len(l.input) {
end = len(l.input)
}
l.pos = end
return l.input[l.start:end]
}
func (l *Lexer) Backup() {
l.pos -= l.width
l.width = 0
}
func (l *Lexer) Ignore() {
l.start = l.pos
}
func (l *Lexer) Accept(valid string) bool {
if strings.ContainsRune(valid, l.Next()) {
return true
}
l.Backup()
return false
}
func (l *Lexer) AcceptRun(valid string) bool {
result := false
for strings.ContainsRune(valid, l.Next()) {
result = true
}
l.Backup()
return result
}
func (l *Lexer) AcceptRunUntil(invalid string) {
for {
next := l.Next()
if next == 0 || strings.ContainsRune(invalid, next) {
break
}
}
l.Backup()
}
func (l *Lexer) Errorf(format string, args ...interface{}) {
l.emitter.EmitToken(&Token{
Type: TokError,
Value: fmt.Sprintf(format, args...),
Start: l.start,
End: l.pos,
})
}
func (l *Lexer) Run(state State) {
for state != nil {
state = state(l)
}
}
type State func(*Lexer) State
func Start(emitter TokenEmitter, inital State, input string) {
lexer := &Lexer{
emitter: emitter,
input: input,
}
for state := inital; state != nil; {
state = state(lexer)
}
}
func StartWithFilenames(emitter TokenEmitter, inital State, filenames ...string) {
for i, filename := range filenames {
b, err := os.ReadFile(filename)
if err != nil {
emitter.EmitToken(&Token{
Type: TokError,
Value: err.Error(),
Filename: filename,
Start: -1,
End: -1,
})
return
}
Start(TokenEmitterFunc(func(tok *Token) {
if tok.Type == TokEOF && i != len(filenames)-1 {
// because we havn't process all the files,
// we simply ignore EOF of previous procceed files
return
}
tok.Filename = filename
emitter.EmitToken(tok)
}), inital, string(b))
}
}