-
Notifications
You must be signed in to change notification settings - Fork 1
/
lex.go
305 lines (269 loc) · 6.12 KB
/
lex.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package gmars
import (
"bufio"
"fmt"
"io"
"unicode"
)
// tokenReader defines an interface shared between the stream based lexer
// and a bufTokenReader to cache tokens in memory.
type tokenReader interface {
NextToken() (token, error)
Tokens() ([]token, error)
}
type lexer struct {
reader *bufio.Reader
nextRune rune
atEOF bool
closed bool
tokens chan token
}
type lexStateFn func(l *lexer) lexStateFn
func newLexer(r io.Reader) *lexer {
lex := &lexer{
reader: bufio.NewReader(r),
tokens: make(chan token),
}
lex.next()
go lex.run()
return lex
}
func LexInput(r io.Reader) ([]token, error) {
lexer := newLexer(r)
return lexer.Tokens()
}
func (l *lexer) next() (rune, bool) {
if l.atEOF {
return '\x00', true
}
r, _, err := l.reader.ReadRune()
if err != nil {
l.atEOF = true
return l.nextRune, true
}
lastRune := l.nextRune
l.nextRune = r
return lastRune, false
}
func (l *lexer) run() {
for state := lexInput; state != nil; {
state = state(l)
}
close(l.tokens)
l.closed = true
}
func (l *lexer) NextToken() (token, error) {
if l.closed {
return token{}, fmt.Errorf("no more tokens")
}
return <-l.tokens, nil
}
func (l *lexer) Tokens() ([]token, error) {
tokens := make([]token, 0)
for {
token, err := l.NextToken()
if err != nil {
return nil, err
}
tokens = append(tokens, token)
if token.typ == tokEOF || token.typ == tokError {
break
}
}
return tokens, nil
}
func (l *lexer) consume(nextState lexStateFn) lexStateFn {
_, eof := l.next()
if eof {
l.tokens <- token{tokEOF, ""}
return nil
}
return nextState
}
func (l *lexer) emitConsume(tok token, nextState lexStateFn) lexStateFn {
l.tokens <- tok
_, eof := l.next()
if eof {
l.tokens <- token{tokEOF, ""}
return nil
}
return nextState
}
func lexInput(l *lexer) lexStateFn {
// consume any space until non-space characters, emitting tokNewlines
if unicode.IsSpace(l.nextRune) {
for unicode.IsSpace(l.nextRune) {
if l.nextRune == '\n' {
l.tokens <- token{typ: tokNewline}
}
_, eof := l.next()
if eof {
l.tokens <- token{typ: tokEOF}
return nil
}
}
return lexInput
}
// handle alphanumeric input
if unicode.IsLetter(l.nextRune) || l.nextRune == '_' {
return lexText
}
if unicode.IsDigit(l.nextRune) {
return lexNumber
}
// dispatch based on next rune, or error
switch l.nextRune {
case '\x00':
l.tokens <- token{tokEOF, ""}
case ';':
return lexComment
case ',':
return l.emitConsume(token{tokComma, ","}, lexInput)
case '(':
return l.emitConsume(token{tokParenL, "("}, lexInput)
case ')':
return l.emitConsume(token{tokParenR, ")"}, lexInput)
case '+':
fallthrough
case '-':
fallthrough
case '*':
fallthrough
case '/':
fallthrough
case '%':
return l.emitConsume(token{tokSymbol, string(l.nextRune)}, lexInput)
case '$':
fallthrough
case '#':
fallthrough
case '@':
fallthrough
case '{':
fallthrough
case '}':
return l.emitConsume(token{tokSymbol, string(l.nextRune)}, lexInput)
case '<':
return l.consume(lexLt)
case '>':
return l.consume(lexGt)
case ':':
return l.emitConsume(token{tokColon, ":"}, lexInput)
case '=':
return l.consume(lexEquals)
case '|':
return l.consume(lexPipe)
case '&':
return l.consume(lexAnd)
case '\x1a':
return l.consume(lexInput)
default:
// we will put this in the stream. if a file is formatted
// properly, and invalid input should be after an 'end'
// pseudo-op which will cause the parser to stop before
// processing this token, otherwise it is an error
l.tokens <- token{tokInvalid, string(l.nextRune)}
l.tokens <- token{typ: tokEOF}
return nil
}
return nil
}
func lexText(l *lexer) lexStateFn {
runeBuf := make([]rune, 0, 10)
for unicode.IsLetter(l.nextRune) || unicode.IsDigit(l.nextRune) || l.nextRune == '.' || l.nextRune == '_' {
r, eof := l.next()
runeBuf = append(runeBuf, r)
if eof {
l.tokens <- token{typ: tokText, val: string(runeBuf)}
l.tokens <- token{typ: tokEOF}
return nil
}
}
if len(runeBuf) > 0 {
l.tokens <- token{typ: tokText, val: string(runeBuf)}
}
return lexInput
}
func lexNumber(l *lexer) lexStateFn {
numberBuf := make([]rune, 0, 10)
// // remove leading zeros
for l.nextRune == '0' {
_, eof := l.next()
if eof {
l.tokens <- token{tokNumber, "0"}
l.tokens <- token{typ: tokEOF}
return nil
}
}
for unicode.IsDigit(l.nextRune) {
r, eof := l.next()
numberBuf = append(numberBuf, r)
if eof {
l.tokens <- token{tokNumber, string(numberBuf)}
l.tokens <- token{typ: tokEOF}
return nil
}
}
if len(numberBuf) == 0 {
l.tokens <- token{tokNumber, "0"}
return lexInput
}
if len(numberBuf) > 0 {
l.tokens <- token{tokNumber, string(numberBuf)}
}
return lexInput
}
func lexComment(l *lexer) lexStateFn {
commentBuf := make([]rune, 0, 32)
for l.nextRune != '\n' {
commentBuf = append(commentBuf, l.nextRune)
_, eof := l.next()
if eof {
l.tokens <- token{tokComment, string(commentBuf)}
l.tokens <- token{tokEOF, ""}
return nil
}
}
l.tokens <- token{typ: tokComment, val: string(commentBuf)}
return lexInput
}
func lexEquals(l *lexer) lexStateFn {
if l.nextRune == '=' {
return l.emitConsume(token{tokSymbol, "=="}, lexInput)
} else {
l.tokens <- token{tokError, fmt.Sprintf("expected '=' after '=', got '%s'", string(l.nextRune))}
return nil
}
}
func lexPipe(l *lexer) lexStateFn {
if l.nextRune == '|' {
return l.emitConsume(token{tokSymbol, "||"}, lexInput)
} else {
l.tokens <- token{tokError, fmt.Sprintf("expected '|' after '|', got '%s'", string(l.nextRune))}
return nil
}
}
func lexAnd(l *lexer) lexStateFn {
if l.nextRune == '&' {
return l.emitConsume(token{tokSymbol, "&&"}, lexInput)
} else {
l.tokens <- token{tokError, fmt.Sprintf("expected '&' after '&', got '%s'", string(l.nextRune))}
return nil
}
}
func lexGt(l *lexer) lexStateFn {
if l.nextRune == '=' {
return l.emitConsume(token{tokSymbol, ">="}, lexInput)
} else {
l.tokens <- token{tokSymbol, ">"}
return lexInput
}
}
func lexLt(l *lexer) lexStateFn {
if l.nextRune == '=' {
return l.emitConsume(token{tokSymbol, "<="}, lexInput)
} else {
l.tokens <- token{tokSymbol, "<"}
return lexInput
}
}