forked from blues/jsonata-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lexer.go
556 lines (484 loc) · 10.2 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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Copyright 2018 Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
package jparse
import (
"fmt"
"unicode/utf8"
)
const eof = -1
type tokenType uint8
const (
typeEOF tokenType = iota
typeError
typeString // string literal, e.g. "hello"
typeNumber // number literal, e.g. 3.14159
typeBoolean // true or false
typeNull // null
typeName // field name, e.g. Price
typeNameEsc // escaped field name, e.g. `Product Name`
typeVariable // variable, e.g. $x
typeRegex // regular expression, e.g. /ab+/
// Symbol operators
typeBracketOpen
typeBracketClose
typeBraceOpen
typeBraceClose
typeParenOpen
typeParenClose
typeDot
typeComma
typeColon
typeSemicolon
typeCondition
typePlus
typeMinus
typeMult
typeDiv
typeMod
typePipe
typeEqual
typeNotEqual
typeLess
typeLessEqual
typeGreater
typeGreaterEqual
typeApply
typeSort
typeConcat
typeRange
typeAssign
typeDescendent
// Keyword operators
typeAnd
typeOr
typeIn
// Join operator
typeJoin
)
func (tt tokenType) String() string {
switch tt {
case typeEOF:
return "(eof)"
case typeError:
return "(error)"
case typeString:
return "(string)"
case typeNumber:
return "(number)"
case typeBoolean:
return "(boolean)"
case typeName, typeNameEsc:
return "(name)"
case typeVariable:
return "(variable)"
case typeRegex:
return "(regex)"
case typeJoin:
return "(join)"
default:
if s := symbolsAndKeywords[tt]; s != "" {
return s
}
return "(unknown)"
}
}
// symbols1 maps 1-character symbols to the corresponding
// token types.
var symbols1 = [...]tokenType{
'[': typeBracketOpen,
']': typeBracketClose,
'{': typeBraceOpen,
'}': typeBraceClose,
'(': typeParenOpen,
')': typeParenClose,
'.': typeDot,
',': typeComma,
';': typeSemicolon,
':': typeColon,
'?': typeCondition,
'+': typePlus,
'-': typeMinus,
'*': typeMult,
'/': typeDiv,
'%': typeMod,
'|': typePipe,
'=': typeEqual,
'<': typeLess,
'>': typeGreater,
'^': typeSort,
'&': typeConcat,
'@': typeJoin,
}
type runeTokenType struct {
r rune
tt tokenType
}
// symbols2 maps 2-character symbols to the corresponding
// token types.
var symbols2 = [...][]runeTokenType{
'!': {{'=', typeNotEqual}},
'<': {{'=', typeLessEqual}},
'>': {{'=', typeGreaterEqual}},
'.': {{'.', typeRange}},
'~': {{'>', typeApply}},
':': {{'=', typeAssign}},
'*': {{'*', typeDescendent}},
}
const (
symbol1Count = rune(len(symbols1))
symbol2Count = rune(len(symbols2))
)
func lookupSymbol1(r rune) tokenType {
if r < 0 || r >= symbol1Count {
return 0
}
return symbols1[r]
}
func lookupSymbol2(r rune) []runeTokenType {
if r < 0 || r >= symbol2Count {
return nil
}
return symbols2[r]
}
func lookupKeyword(s string) tokenType {
switch s {
case "and":
return typeAnd
case "or":
return typeOr
case "in":
return typeIn
case "true", "false":
return typeBoolean
case "null":
return typeNull
default:
return 0
}
}
// A token represents a discrete part of a JSONata expression
// such as a string, a number, a field name, or an operator.
type token struct {
Type tokenType
Value string
Position int
}
// lexer converts a JSONata expression into a sequence of tokens.
// The implmentation is based on the technique described in Rob
// Pike's 'Lexical Scanning in Go' talk.
type lexer struct {
input string
length int
start int
current int
width int
err error
}
// newLexer creates a new lexer from the provided input. The
// input is tokenized by successive calls to the next method.
func newLexer(input string) lexer {
return lexer{
input: input,
length: len(input),
}
}
// next returns the next token from the provided input. When
// the end of the input is reached, next returns EOF for all
// subsequent calls.
//
// The allowRegex argument determines how the lexer interprets
// a forward slash character. Forward slashes in JSONata can
// either be the start of a regular expression or the division
// operator depending on their position. If allowRegex is true,
// the lexer will treat a forward slash like a regular
// expression.
func (l *lexer) next(allowRegex bool) token {
l.skipWhitespace()
ch := l.nextRune()
if ch == eof {
return l.eof()
}
if allowRegex && ch == '/' {
l.ignore()
return l.scanRegex(ch)
}
if rts := lookupSymbol2(ch); rts != nil {
for _, rt := range rts {
if l.acceptRune(rt.r) {
return l.newToken(rt.tt)
}
}
}
if tt := lookupSymbol1(ch); tt > 0 {
return l.newToken(tt)
}
if ch == '"' || ch == '\'' {
l.ignore()
return l.scanString(ch)
}
if ch >= '0' && ch <= '9' {
l.backup()
return l.scanNumber()
}
if ch == '`' {
l.ignore()
return l.scanEscapedName(ch)
}
l.backup()
return l.scanName()
}
// scanRegex reads a regular expression from the current position
// and returns a regex token. The opening delimiter has already
// been consumed.
func (l *lexer) scanRegex(delim rune) token {
var depth int
Loop:
for {
switch l.nextRune() {
case delim:
if depth == 0 {
break Loop
}
case '(', '[', '{':
depth++
case ')', ']', '}':
depth--
case '\\':
if r := l.nextRune(); r != eof && r != '\n' {
break
}
fallthrough
case eof, '\n':
return l.error(ErrUnterminatedRegex, string(delim))
}
}
l.backup()
t := l.newToken(typeRegex)
l.acceptRune(delim)
l.ignore()
// Convert JavaScript-style regex flags to Go format,
// e.g. /ab+/i becomes /(?i)ab+/.
if l.acceptAll(isRegexFlag) {
flags := l.newToken(0)
t.Value = fmt.Sprintf("(?%s)%s", flags.Value, t.Value)
}
return t
}
// scanString reads a string literal from the current position
// and returns a string token. The opening quote has already been
// consumed.
func (l *lexer) scanString(quote rune) token {
Loop:
for {
switch l.nextRune() {
case quote:
break Loop
case '\\':
if r := l.nextRune(); r != eof {
break
}
fallthrough
case eof:
return l.error(ErrUnterminatedString, fmt.Sprintf("%s, starting from character position %d", string(quote), l.start))
}
}
l.backup()
t := l.newToken(typeString)
l.acceptRune(quote)
l.ignore()
return t
}
// scanNumber reads a number literal from the current position
// and returns a number token.
func (l *lexer) scanNumber() token {
// JSON does not support leading zeroes. The integer part of
// a number will either be a single zero, or a non-zero digit
// followed by zero or more digits.
if !l.acceptRune('0') {
l.accept(isNonZeroDigit)
l.acceptAll(isDigit)
}
if l.acceptRune('.') {
if !l.acceptAll(isDigit) {
// If there are no digits after the decimal point,
// don't treat the dot as part of the number. It
// could be part of the range operator, e.g. "1..5".
l.backup()
return l.newToken(typeNumber)
}
}
if l.acceptRunes2('e', 'E') {
l.acceptRunes2('+', '-')
l.acceptAll(isDigit)
}
return l.newToken(typeNumber)
}
// scanEscapedName reads a field name from the current position
// and returns a name token. The opening quote has already been
// consumed.
func (l *lexer) scanEscapedName(quote rune) token {
Loop:
for {
switch l.nextRune() {
case quote:
break Loop
case eof, '\n':
return l.error(ErrUnterminatedName, string(quote))
}
}
l.backup()
t := l.newToken(typeNameEsc)
l.acceptRune(quote)
l.ignore()
return t
}
// scanName reads from the current position and returns a name,
// variable, or keyword token.
func (l *lexer) scanName() token {
isVar := l.acceptRune('$')
if isVar {
l.ignore()
}
for {
ch := l.nextRune()
if ch == eof {
break
}
// Stop reading if we hit whitespace...
if isWhitespace(ch) {
l.backup()
break
}
// ...or anything that looks like an operator.
if lookupSymbol1(ch) > 0 || lookupSymbol2(ch) != nil {
l.backup()
break
}
}
t := l.newToken(typeName)
if isVar {
t.Type = typeVariable
} else if tt := lookupKeyword(t.Value); tt > 0 {
t.Type = tt
}
return t
}
func (l *lexer) eof() token {
return token{
Type: typeEOF,
Position: l.current,
}
}
func (l *lexer) error(typ ErrType, hint string) token {
t := l.newToken(typeError)
l.err = newErrorHint(typ, t, hint)
return t
}
func (l *lexer) newToken(tt tokenType) token {
t := token{
Type: tt,
Value: l.input[l.start:l.current],
Position: l.start,
}
l.width = 0
l.start = l.current
return t
}
func (l *lexer) nextRune() rune {
if l.err != nil || l.current >= l.length {
l.width = 0
return eof
}
r, w := utf8.DecodeRuneInString(l.input[l.current:])
l.width = w
l.current += w
/*
if r == '\n' {
l.line++
}
*/
return r
}
func (l *lexer) backup() {
// TODO: Support more than one backup operation.
// TODO: Store current rune so that when nextRune
// is called again, we don't need to repeat the call
// to DecodeRuneInString.
l.current -= l.width
}
func (l *lexer) ignore() {
l.start = l.current
}
func (l *lexer) acceptRune(r rune) bool {
return l.accept(func(c rune) bool {
return c == r
})
}
func (l *lexer) acceptRunes2(r1, r2 rune) bool {
return l.accept(func(c rune) bool {
return c == r1 || c == r2
})
}
func (l *lexer) accept(isValid func(rune) bool) bool {
if isValid(l.nextRune()) {
return true
}
l.backup()
return false
}
func (l *lexer) acceptAll(isValid func(rune) bool) bool {
var b bool
for l.accept(isValid) {
b = true
}
return b
}
func (l *lexer) skipWhitespace() {
l.acceptAll(isWhitespace)
l.ignore()
}
func isWhitespace(r rune) bool {
switch r {
case ' ', '\t', '\n', '\r', '\v':
return true
default:
return false
}
}
func isRegexFlag(r rune) bool {
switch r {
case 'i', 'm', 's':
return true
default:
return false
}
}
func isDigit(r rune) bool {
return r >= '0' && r <= '9'
}
func isNonZeroDigit(r rune) bool {
return r >= '1' && r <= '9'
}
// symbolsAndKeywords maps operator token types back to their
// string representations. It's only used by tokenType.String
// (and one test).
var symbolsAndKeywords = func() map[tokenType]string {
m := map[tokenType]string{
typeAnd: "and",
typeOr: "or",
typeIn: "in",
typeNull: "null",
}
for r, tt := range symbols1 {
if tt > 0 {
m[tt] = fmt.Sprintf("%c", r)
}
}
for r, rts := range symbols2 {
for _, rt := range rts {
m[rt.tt] = fmt.Sprintf("%c", r) + fmt.Sprintf("%c", rt.r)
}
}
return m
}()