-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.go
executable file
·422 lines (393 loc) · 7.25 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package openscad
import (
"bytes"
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
var moduleKeyword = []byte(`module`)
var functionKeyword = []byte(`function`)
var forKeyword = []byte(`for`)
var letKeyword = []byte(`let`)
var includeKeyword = []byte(`include`)
var useKeyword = []byte(`use`)
var ifKeyword = []byte(`if`)
var elseKeyword = []byte(`else`)
const (
plus = '+'
minus = '-'
asterisk = '*'
slash = '/'
equal = '='
sharp = '#'
dquote = '"'
colon = ':'
semicolon = ';'
comma = ','
openParen = '('
closeParen = ')'
openBrace = '{'
closeBrace = '}'
openBracket = '['
closeBracket = ']'
lessThan = '<'
greaterThan = '>'
question = '?'
percent = '%'
ampersand = '&'
exclamation = '!'
)
const (
EOF = iota
Keyword
Literal
Numeric
Ident
Comma
Equal
Semicolon
Colon
Question
DoubleQuote
Asterisk
Plus
Minus
Slash
LessThan
LessThanEqual
GreaterThan
GreaterThanEqual
Equality // ==
OpenParen // (
CloseParen // )
OpenBracket // [
CloseBracket // ]
OpenBrace // {
CloseBrace // }
Percent
Sharp
And
BitwiseAnd
Exclamation
)
type Token struct {
Type int
Value string
}
type lexer struct {
src []byte
ch chan *Token
pos int
peekPos []int
}
func (l *lexer) skipWhiteSpaces() {
for len(l.src) > 0 {
r := l.peek()
if !unicode.IsSpace(r) {
l.unread()
return
}
l.advance()
}
}
func (l *lexer) emit(typ int, value string) {
l.ch <- &Token{Type: typ, Value: value}
}
func (l *lexer) peek() rune {
r, s := utf8.DecodeRune(l.src[l.pos:])
l.pos += s
l.peekPos = append(l.peekPos, s)
return r
}
func (l *lexer) unread() {
if lp := len(l.peekPos); lp > 0 {
l.pos -= l.peekPos[lp-1]
l.peekPos = l.peekPos[:lp-1]
}
}
func (l *lexer) advance() {
l.peekPos = nil
l.src = l.src[l.pos:]
l.pos = 0
}
func (l *lexer) emitBuffer(typ int) {
l.emit(typ, string(l.src[:l.pos]))
l.advance()
}
func Lex(ch chan *Token, src []byte) {
l := lexer{
src: src,
ch: ch,
}
defer close(ch)
var inInclude bool
for len(l.src) > 0 {
l.skipWhiteSpaces()
found := true
peeked := l.peek()
switch peeked {
case ampersand:
if l.peek() == ampersand {
l.emitBuffer(And)
} else {
l.emitBuffer(BitwiseAnd)
}
case comma:
l.emitBuffer(Comma)
case equal:
next := l.peek()
if next == equal {
l.emitBuffer(Equality)
} else {
l.unread()
l.emitBuffer(Equal)
}
case semicolon:
l.emitBuffer(Semicolon)
case colon:
l.emitBuffer(Colon)
case openBracket:
l.emitBuffer(OpenBracket)
case closeBracket:
l.emitBuffer(CloseBracket)
case openParen:
l.emitBuffer(OpenParen)
case closeParen:
l.emitBuffer(CloseParen)
case openBrace:
l.emitBuffer(OpenBrace)
case closeBrace:
l.emitBuffer(CloseBrace)
case question:
l.emitBuffer(Question)
case asterisk:
l.emitBuffer(Asterisk)
case plus:
l.emitBuffer(Plus)
case minus:
l.emitBuffer(Minus)
case exclamation:
l.emitBuffer(Exclamation)
case slash:
next := l.peek()
switch next {
case slash:
// inline comment, ignore until end of line
for {
r := l.peek()
if r == '\n' || r == utf8.RuneError {
break
}
}
l.advance()
case asterisk:
// block comment, ignore until */
OUTER:
for {
r := l.peek()
switch r {
case utf8.RuneError:
break OUTER
case asterisk:
next := l.peek()
if next == slash {
break OUTER
}
}
}
l.advance()
default:
l.unread()
l.emitBuffer(Slash)
}
case lessThan:
if inInclude {
l.unread()
if err := l.captureLiteral(lessThan, greaterThan); err != nil {
return
}
inInclude = false
continue
}
next := l.peek()
if next != equal {
l.unread()
l.emitBuffer(LessThan)
} else {
l.advance()
l.emitBuffer(LessThanEqual)
}
case greaterThan:
next := l.peek()
if next != equal {
l.unread()
l.emitBuffer(GreaterThan)
} else {
l.advance()
l.emitBuffer(GreaterThanEqual)
}
case percent:
l.emitBuffer(Percent)
case sharp:
l.emitBuffer(Sharp)
default:
found = false
}
if found {
continue
}
// maybe it's one of the keywords...
found = true
switch peeked {
case 'i':
l.unread()
if err := l.expect(Keyword, ifKeyword); err != nil {
if err := l.expect(Keyword, includeKeyword); err == nil {
inInclude = true
} else {
found = false
}
}
case 'e':
l.unread()
if err := l.expect(Keyword, elseKeyword); err != nil {
found = false
}
case 'u':
l.unread()
if err := l.expect(Keyword, useKeyword); err == nil {
inInclude = true
} else {
found = false
}
case 'l':
l.unread()
if err := l.expect(Keyword, letKeyword); err != nil {
found = false
}
case 'm':
l.unread()
if err := l.expect(Keyword, moduleKeyword); err != nil {
found = false
}
case 'f':
l.unread()
if err := l.expect(Keyword, forKeyword); err != nil {
if err := l.expect(Keyword, functionKeyword); err != nil {
found = false
}
}
case dquote:
l.unread()
if err := l.captureLiteral(dquote, dquote); err != nil {
found = false
}
case '-':
l.unread()
if err := l.captureNumeric(); err != nil {
found = false
}
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
l.unread()
if err := l.captureNumeric(); err != nil {
found = false
}
default:
found = false
}
if found {
continue
}
l.unread()
// it must be an identifier, then
_ = l.captureIdent() // TODO: error handling
}
l.emit(EOF, "")
}
func (l *lexer) expect(typ int, v []byte) error {
l.skipWhiteSpaces()
if !bytes.Equal(v, l.src[l.pos:len(v)]) {
return fmt.Errorf("expected %q, but was not foud", v)
}
l.src = l.src[l.pos+len(v):]
l.pos = 0
l.peekPos = nil
l.emit(typ, string(v))
return nil
}
func (l *lexer) captureNumericLike(sb *strings.Builder) {
for {
// first character
r := l.peek()
if !unicode.IsNumber(r) {
l.unread()
break
}
sb.WriteRune(r)
}
}
func (l *lexer) captureNumeric() error {
l.skipWhiteSpaces()
var sb strings.Builder
r := l.peek()
if r == '-' {
sb.WriteRune(r)
} else {
l.unread()
}
l.captureNumericLike(&sb)
r = l.peek()
if r == '.' {
sb.WriteRune(r)
l.captureNumericLike(&sb)
} else {
l.unread()
}
l.emit(Numeric, sb.String())
l.advance()
return nil
}
func (l *lexer) captureIdent() error {
l.skipWhiteSpaces()
var sb strings.Builder
for len(l.src) > 0 {
r := l.peek()
// We need to allow $ in the first character because it's used in the
// special variables like $fn
if sb.Len() > 0 {
if r != '_' && !unicode.IsLetter(r) && !unicode.IsNumber(r) {
l.unread()
break
}
} else {
if r != '$' && r != '_' && !unicode.IsLetter(r) && !unicode.IsNumber(r) {
l.unread()
break
}
}
l.advance()
sb.WriteRune(r)
}
if sb.Len() > 0 {
l.emit(Ident, sb.String())
}
return nil
}
func (l *lexer) captureLiteral(begin, end rune) error {
l.skipWhiteSpaces()
var sb strings.Builder
if l.peek() != begin {
return fmt.Errorf("expected %q, but was not found", begin)
}
for len(l.src) > 0 {
r := l.peek()
if r == end {
break
}
sb.WriteRune(r)
}
l.advance()
l.emit(Literal, sb.String())
return nil
}