-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.go
536 lines (436 loc) · 12.3 KB
/
tokenizer.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package corn
import (
"fmt"
"strconv"
"strings"
)
type tokenId = int32
type stateId = int8
const (
tokenBraceOpen = iota
tokenBraceClose
tokenBracketOpen
tokenBracketClose
tokenEquals
tokenDoubleQuote
tokenSpread
tokenPathSeparator
tokenLet
tokenIn
tokenTrue
tokenFalse
tokenNull
tokenPathSegment
tokenFloat
tokenInteger
tokenCharEscape
tokenCharSequence
tokenInput
)
const (
charsWhitespace = " \t\r\n"
charsInvalidPath = charsWhitespace + "=."
charsHexInteger = "0123456789abcdefABCDEF"
charsInteger = "-0123456789_"
charsFloat = charsInteger + "+e"
charsInputFirst = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
charsInput = charsInputFirst + "1234567890_"
charsInvalidCharSequence = "\"\\$"
)
const (
stateTopLevel = iota
stateAssignBlock
stateValue
stateObject
stateArray
stateString
)
type matcher = func(input []rune, tokens []Token[any]) ([]rune, []Token[any], bool)
type dynamicTokenMatcher = func(input []rune) ([]rune, any)
type stateChanger = func(state []stateId) []stateId
type MatchRule struct {
matcher matcher
stateChange stateChanger
}
func matchRule(matcher matcher) MatchRule {
return MatchRule{matcher: matcher}
}
func matchRuleStateChange(matcher matcher, stateChange stateChanger) MatchRule {
return MatchRule{
matcher: matcher,
stateChange: stateChange,
}
}
type Token[T comparable] struct {
Id tokenId
Data *T
}
func (t Token[Stringer]) String() string {
var identifier string
switch t.Id {
case tokenBraceOpen:
identifier = "{"
case tokenBraceClose:
identifier = "}"
case tokenBracketOpen:
identifier = "["
case tokenBracketClose:
identifier = "]"
case tokenEquals:
identifier = "="
case tokenDoubleQuote:
identifier = "\""
case tokenSpread:
identifier = ".."
case tokenPathSeparator:
identifier = "."
case tokenLet:
identifier = "let"
case tokenIn:
identifier = "in"
case tokenTrue:
identifier = "true"
case tokenFalse:
identifier = "false"
case tokenNull:
identifier = "null"
case tokenPathSegment:
identifier = "path_seg"
case tokenFloat:
identifier = "float"
case tokenInteger:
identifier = "int"
case tokenInput:
identifier = "input"
case tokenCharSequence:
identifier = "char_seq"
case tokenCharEscape:
identifier = "char_escape"
default:
identifier = "?"
}
if t.Data != nil {
return fmt.Sprintf("Token(%s(%v))", identifier, *t.Data)
} else {
return fmt.Sprintf("Token(%s)", identifier)
}
}
func simpleToken(id tokenId) Token[any] {
return Token[any]{Id: id}
}
func dataToken[T comparable](id tokenId, data T) Token[T] {
return Token[T]{Id: id, Data: &data}
}
func takeWhile(input []rune, check func(rune) bool) ([]rune, []rune) {
var slice []rune
if len(input) == 0 {
return slice, input
}
var char = input[0]
for check(char) && len(input) > 1 {
slice = append(slice, char)
input = input[1:]
char = input[0]
}
return slice, input
}
func runesEqual(a []rune, b []rune) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
func matchToken(tokenId tokenId, tokenChar rune) matcher {
return func(input []rune, tokens []Token[any]) ([]rune, []Token[any], bool) {
var match = false
if len(input) > 0 && input[0] == tokenChar {
input = input[1:]
tokens = append(tokens, simpleToken(tokenId))
match = true
}
return input, tokens, match
}
}
func matchCompoundToken(tokenId tokenId, tokenChars []rune) matcher {
return func(input []rune, tokens []Token[any]) ([]rune, []Token[any], bool) {
if len(input) < len(tokenChars) {
return input, tokens, false
}
if runesEqual(input[:len(tokenChars)], tokenChars) {
input = input[len(tokenChars):]
tokens = append(tokens, simpleToken(tokenId))
return input, tokens, true
}
return input, tokens, false
}
}
func matchDynamicToken(tokenId tokenId, matcher dynamicTokenMatcher) matcher {
return func(input []rune, tokens []Token[any]) ([]rune, []Token[any], bool) {
var data any
input, data = matcher(input)
if data != nil {
var token = dataToken(tokenId, data)
tokens = append(tokens, token)
return input, tokens, true
}
return input, tokens, false
}
}
func matchInput(input []rune) ([]rune, any) {
if len(input) < 2 || input[0] != '$' {
return input, nil
}
var name = []rune{input[0]}
if strings.ContainsRune(charsInputFirst, input[1]) {
name = append(name, input[1])
input = input[2:]
rest, input := takeWhile(input, func(char rune) bool {
return strings.ContainsRune(charsInput, char)
})
name = append(name, rest...)
return input, string(name)
}
return input, nil
}
func matchQuotedPathSegment(input []rune) ([]rune, any) {
if len(input) < 3 || input[0] != '\'' {
return input, nil
}
escaping := false
path, input := takeWhile(input[1:], func(r rune) bool {
if r == '\\' {
escaping = true
}
if r == '\'' && escaping {
escaping = false
return true
} else {
return r != '\''
}
})
if input[0] != '\'' {
return input, nil
}
input = input[1:]
// TODO: this is a hack to handle escaped quotes including their backslash
// would be better to just exclude them in the first place
pathS := strings.ReplaceAll(string(path), "\\'", "'")
return input, pathS
}
func matchPathSegment(input []rune) ([]rune, any) {
path, input := takeWhile(input, func(r rune) bool {
return !strings.ContainsRune(charsInvalidPath, r)
})
if len(path) > 0 {
return input, string(path)
}
return input, nil
}
func matchFloat(input []rune) ([]rune, any) {
numS, _ := takeWhile(input, func(char rune) bool {
return strings.ContainsRune(charsInteger, char)
})
if len(input) < len(numS)+2 || input[len(numS)] != '.' {
return input, nil
}
input = input[len(numS)+1:]
decimals, input := takeWhile(input, func(char rune) bool {
return strings.ContainsRune(charsFloat, char)
})
numS = append(numS, '.')
numS = append(numS, decimals...)
var num, err = strconv.ParseFloat(string(numS), 64)
if err != nil {
return input, nil
}
return input, num
}
func matchHexInteger(input []rune) ([]rune, any) {
if len(input) < 3 {
return input, nil
}
if input[0] != '0' || input[1] != 'x' {
return input, nil
}
numS, input := takeWhile(input[2:], func(char rune) bool {
return strings.ContainsRune(charsHexInteger, char)
})
var num, err = strconv.ParseInt(string(numS), 16, 64)
if err != nil {
return input, nil
}
return input, num
}
func matchInteger(input []rune) ([]rune, any) {
numS, input := takeWhile(input, func(char rune) bool {
return strings.ContainsRune(charsInteger, char)
})
// TODO: This is not spec compliant - needs to only allow one consecutive _
numString := strings.ReplaceAll(string(numS), "_", "")
var num, err = strconv.ParseInt(string(numString), 10, 64)
if err != nil {
return input, nil
}
return input, num
}
func matchCharEscape(input []rune) ([]rune, any) {
if len(input) < 2 || input[0] != '\\' {
return input, nil
}
const LEN_UNICODE = 2 + 4
if len(input) >= LEN_UNICODE && input[1] == 'u' {
var codeS = input[2:6]
var code, err = strconv.ParseInt(string(codeS), 16, 32)
if err != nil {
return input, nil
}
input = input[6:]
return input, rune(code)
}
var char rune
switch input[1] {
case '\\':
char = '\\'
case '"':
char = '"'
case 'n':
char = '\n'
case 'r':
char = '\r'
case 't':
char = '\t'
case '$':
char = '$'
default:
return input, nil
}
input = input[2:]
return input, char
}
func matchCharSequence(input []rune) ([]rune, any) {
seq, input := takeWhile(input, func(char rune) bool {
return !strings.ContainsRune(charsInvalidCharSequence, char)
})
return input, string(seq)
}
// TODO: Make more efficient - allocating on every token currently :(
func getMatchers(state stateId) []MatchRule {
switch state {
case stateTopLevel:
return []MatchRule{
matchRuleStateChange(matchCompoundToken(tokenLet, []rune("let")), statePusher(stateAssignBlock)),
matchRuleStateChange(matchToken(tokenBraceOpen, '{'), statePusher(stateObject)),
}
case stateAssignBlock:
return []MatchRule{
matchRuleStateChange(matchCompoundToken(tokenIn, []rune("in")), popState),
matchRule(matchToken(tokenBraceOpen, '{')),
matchRule(matchToken(tokenBraceClose, '}')),
matchRule(matchDynamicToken(tokenInput, matchInput)),
matchRuleStateChange(matchToken(tokenEquals, '='), statePusher(stateValue)),
}
case stateObject:
return []MatchRule{
matchRuleStateChange(matchToken(tokenBraceClose, '}'), popState),
matchRuleStateChange(matchToken(tokenEquals, '='), statePusher(stateValue)),
matchRule(matchCompoundToken(tokenSpread, []rune(".."))),
matchRule(matchToken(tokenPathSeparator, '.')),
matchRule(matchDynamicToken(tokenInput, matchInput)),
matchRule(matchDynamicToken(tokenPathSegment, matchQuotedPathSegment)),
matchRule(matchDynamicToken(tokenPathSegment, matchPathSegment)),
}
case stateArray:
return []MatchRule{
matchRuleStateChange(matchToken(tokenBracketClose, ']'), popState),
matchRuleStateChange(matchToken(tokenBraceOpen, '{'), statePusher(stateObject)),
matchRuleStateChange(matchToken(tokenBracketOpen, '['), statePusher(stateArray)),
matchRule(matchCompoundToken(tokenSpread, []rune(".."))),
matchRule(matchCompoundToken(tokenTrue, []rune("true"))),
matchRule(matchCompoundToken(tokenFalse, []rune("false"))),
matchRule(matchCompoundToken(tokenNull, []rune("null"))),
matchRuleStateChange(matchToken(tokenDoubleQuote, '"'), statePusher(stateString)),
matchRule(matchDynamicToken(tokenInput, matchInput)),
matchRule(matchDynamicToken(tokenFloat, matchFloat)),
matchRule(matchDynamicToken(tokenInteger, matchInteger)),
}
case stateValue:
return []MatchRule{
matchRuleStateChange(matchToken(tokenBraceOpen, '{'), stateReplacer(stateObject)),
matchRuleStateChange(matchToken(tokenBracketOpen, '['), stateReplacer(stateArray)),
matchRuleStateChange(matchCompoundToken(tokenTrue, []rune("true")), popState),
matchRuleStateChange(matchCompoundToken(tokenFalse, []rune("false")), popState),
matchRuleStateChange(matchCompoundToken(tokenNull, []rune("null")), popState),
matchRuleStateChange(matchToken(tokenDoubleQuote, '"'), stateReplacer(stateString)),
matchRuleStateChange(matchDynamicToken(tokenInput, matchInput), popState),
matchRuleStateChange(matchDynamicToken(tokenFloat, matchFloat), popState),
matchRuleStateChange(matchDynamicToken(tokenInteger, matchHexInteger), popState),
matchRuleStateChange(matchDynamicToken(tokenInteger, matchInteger), popState),
matchRuleStateChange(matchToken(tokenBracketClose, ']'), popState),
}
case stateString:
return []MatchRule{
matchRuleStateChange(matchToken(tokenDoubleQuote, '"'), popState),
matchRule(matchDynamicToken(tokenInput, matchInput)),
matchRule(matchDynamicToken(tokenCharEscape, matchCharEscape)),
matchRule(matchDynamicToken(tokenCharSequence, matchCharSequence)),
}
default:
panic("invalid state id " + strconv.Itoa(int(state)))
}
}
func statePusher(newState stateId) stateChanger {
return func(state []stateId) []stateId {
return append(state, newState)
}
}
func stateReplacer(newState stateId) stateChanger {
return func(state []stateId) []stateId {
return append(state[:len(state)-1], newState)
}
}
func popState(state []stateId) []stateId {
return state[:len(state)-1]
}
func tokenize(inputString string) []Token[any] {
var input = []rune(strings.TrimSpace(inputString))
var tokens []Token[any]
var state = []stateId{stateTopLevel}
for len(input) > 0 {
var length = len(input)
var currentState = state[len(state)-1]
if currentState != stateString {
// handle whitespace
if strings.ContainsRune(charsWhitespace, input[0]) {
_, input = takeWhile(input, func(r rune) bool {
return strings.ContainsRune(charsWhitespace, r)
})
}
// handle comments
if len(input) > 1 && input[0] == '/' && input[1] == '/' {
_, input = takeWhile(input, func(r rune) bool {
return r != '\n'
})
}
}
var matchers = getMatchers(currentState)
for _, matcher := range matchers {
var match bool
input, tokens, match = matcher.matcher(input, tokens)
if match {
if matcher.stateChange != nil {
state = matcher.stateChange(state)
}
break
}
}
if len(input) == length {
panic("length unchanged")
}
}
return tokens
}