-
Notifications
You must be signed in to change notification settings - Fork 485
/
scanner.go
699 lines (615 loc) · 17 KB
/
scanner.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
// Package scanner implements a lexical scanner for River source files.
package scanner
import (
"fmt"
"unicode"
"unicode/utf8"
"github.com/grafana/agent/pkg/river/token"
)
// EBNF for the scanner:
//
// letter = /* any unicode letter class character */ | "_"
// number = /* any unicode number class character */
// digit = /* ASCII characters 0 through 9 */
// digits = digit { digit }
// string_character = /* any unicode character that isn't '"' */
//
// COMMENT = line_comment | block_comment
// line_comment = "//" { character }
// block_comment = "/*" { character | newline } "*/"
//
// IDENT = letter { letter | number }
// NULL = "null"
// BOOL = "true" | "false"
// NUMBER = digits
// FLOAT = ( digits | "." digits ) [ "e" [ "+" | "-" ] digits ]
// STRING = '"' { string_character | escape_sequence } '"'
// OR = "||"
// AND = "&&"
// NOT = "!"
// NEQ = "!="
// ASSIGN = "="
// EQ = "=="
// LT = "<"
// LTE = "<="
// GT = ">"
// GTE = ">="
// ADD = "+"
// SUB = "-"
// MUL = "*"
// DIV = "/"
// MOD = "%"
// POW = "^"
// LCURLY = "{"
// RCURLY = "}"
// LPAREN = "("
// RPAREN = ")"
// LBRACK = "["
// RBRACK = "]"
// COMMA = ","
// DOT = "."
//
// The EBNF for escape_sequence is currently undocumented; see scanEscape for
// details. The escape sequences supported by River are the same as the escape
// sequences supported by Go, except that it is always valid to use \' in
// strings (which in Go, is only valid to use in character literals).
// ErrorHandler is invoked whenever there is an error.
type ErrorHandler func(pos token.Pos, msg string)
// Mode is a set of bitwise flags which control scanner behavior.
type Mode uint
const (
// IncludeComments will cause comments to be returned as comment tokens.
// Otherwise, comments are ignored.
IncludeComments Mode = 1 << iota
// Avoids automatic insertion of terminators (for testing only).
dontInsertTerms
)
const (
bom = 0xFEFF // byte order mark, permitted as very first character
eof = -1 // end of file
)
// Scanner holds the internal state for the tokenizer while processing configs.
type Scanner struct {
file *token.File // Config file handle for tracking line offsets
input []byte // Input config
err ErrorHandler // Error reporting (may be nil)
mode Mode
// scanning state variables:
ch rune // Current character
offset int // Byte offset of ch
readOffset int // Byte offset of first character *after* ch
insertTerm bool // Insert a newline before the next newline
numErrors int // Number of errors encountered during scanning
}
// New creates a new scanner to tokenize the provided input config. The scanner
// uses the provided file for adding line information for each token. The mode
// parameter customizes scanner behavior.
//
// Calls to Scan will invoke the error handler eh when a lexical error is found
// if eh is not nil.
func New(file *token.File, input []byte, eh ErrorHandler, mode Mode) *Scanner {
s := &Scanner{
file: file,
input: input,
err: eh,
mode: mode,
}
// Preload first character.
s.next()
if s.ch == bom {
s.next() // Ignore BOM if it's the first character.
}
return s
}
// peek gets the next byte after the current character without advancing the
// scanner. Returns 0 if the scanner is at EOF.
func (s *Scanner) peek() byte {
if s.readOffset < len(s.input) {
return s.input[s.readOffset]
}
return 0
}
// next advances the scanner and reads the next Unicode character into s.ch.
// s.ch == eof indicates end of file.
func (s *Scanner) next() {
if s.readOffset >= len(s.input) {
s.offset = len(s.input)
if s.ch == '\n' {
// Make sure we track final newlines at the end of the file
s.file.AddLine(s.offset)
}
s.ch = eof
return
}
s.offset = s.readOffset
if s.ch == '\n' {
s.file.AddLine(s.offset)
}
r, width := rune(s.input[s.readOffset]), 1
switch {
case r == 0:
s.onError(s.offset, "illegal character NUL")
case r >= utf8.RuneSelf:
r, width = utf8.DecodeRune(s.input[s.readOffset:])
if r == utf8.RuneError && width == 1 {
s.onError(s.offset, "illegal UTF-8 encoding")
} else if r == bom && s.offset > 0 {
s.onError(s.offset, "illegal byte order mark")
}
}
s.readOffset += width
s.ch = r
}
func (s *Scanner) onError(offset int, msg string) {
if s.err != nil {
s.err(s.file.Pos(offset), msg)
}
s.numErrors++
}
// NumErrors returns the current number of errors encountered during scanning.
// This is useful as a fallback to detect errors when no ErrorHandler was
// provided to the scanner.
func (s *Scanner) NumErrors() int { return s.numErrors }
// Scan scans the next token and returns the token's position, the token
// itself, and the token's literal string (when applicable). The end of the
// input is indicated by token.EOF.
//
// If the returned token is a literal (such as token.STRING), then lit contains
// the corresponding literal text (including surrounding quotes).
//
// If the returned token is a keyword, lit is the keyword text that was
// scanned.
//
// If the returned token is token.TERMINATOR, lit will contain "\n".
//
// If the returned token is token.ILLEGAL, lit contains the offending
// character.
//
// In all other cases, lit will be an empty string.
//
// For more tolerant parsing, Scan returns a valid token character whenever
// possible when a syntax error was encountered. Callers must check NumErrors
// or the number of times the provided ErrorHandler was invoked to ensure there
// were no errors found during scanning.
//
// Scan will inject line information to the file provided by NewScanner.
// Returned token positions are relative to that file.
func (s *Scanner) Scan() (pos token.Pos, tok token.Token, lit string) {
scanAgain:
s.skipWhitespace()
// Start of current token.
pos = s.file.Pos(s.offset)
var insertTerm bool
// Determine token value
switch ch := s.ch; {
case isLetter(ch):
lit = s.scanIdentifier()
if len(lit) > 1 { // Keywords are always > 1 char
tok = token.Lookup(lit)
switch tok {
case token.IDENT, token.NULL, token.BOOL:
insertTerm = true
}
} else {
insertTerm = true
tok = token.IDENT
}
case isDecimal(ch) || (ch == '.' && isDecimal(rune(s.peek()))):
insertTerm = true
tok, lit = s.scanNumber()
default:
s.next() // Make progress
// ch is now the first character in a sequence and s.ch is the second
// character.
switch ch {
case eof:
if s.insertTerm {
s.insertTerm = false // Consumed EOF
return pos, token.TERMINATOR, "\n"
}
tok = token.EOF
case '\n':
// This case is only reachable when s.insertTerm is true, since otherwise
// skipWhitespace consumes all other newlines.
s.insertTerm = false // Consumed newline
return pos, token.TERMINATOR, "\n"
case '\'':
s.onError(pos.Offset(), "illegal single-quoted string; use double quotes")
insertTerm = true
tok = token.ILLEGAL
lit = s.scanString('\'')
case '"':
insertTerm = true
tok = token.STRING
lit = s.scanString('"')
case '|':
if s.ch != '|' {
s.onError(s.offset, "missing second | in ||")
} else {
s.next() // consume second '|'
}
tok = token.OR
case '&':
if s.ch != '&' {
s.onError(s.offset, "missing second & in &&")
} else {
s.next() // consume second '&'
}
tok = token.AND
case '!': // !, !=
tok = s.switch2(token.NOT, token.NEQ, '=')
case '=': // =, ==
tok = s.switch2(token.ASSIGN, token.EQ, '=')
case '<': // <, <=
tok = s.switch2(token.LT, token.LTE, '=')
case '>': // >, >=
tok = s.switch2(token.GT, token.GTE, '=')
case '+':
tok = token.ADD
case '-':
tok = token.SUB
case '*':
tok = token.MUL
case '/':
if s.ch == '/' || s.ch == '*' {
// //- or /*-style comment.
//
// If we're expected to inject a terminator, we can only do so if our
// comment goes to the end of the line. Otherwise, the terminator will
// have to be injected after the comment token.
if s.insertTerm && s.findLineEnd() {
// Reset position to the beginning of the comment.
s.ch = '/'
s.offset = pos.Offset()
s.readOffset = s.offset + 1
s.insertTerm = false // Consumed newline
return pos, token.TERMINATOR, "\n"
}
comment := s.scanComment()
if s.mode&IncludeComments == 0 {
// Skip over comment
s.insertTerm = false // Consumed newline
goto scanAgain
}
tok = token.COMMENT
lit = comment
} else {
tok = token.DIV
}
case '%':
tok = token.MOD
case '^':
tok = token.POW
case '{':
tok = token.LCURLY
case '}':
insertTerm = true
tok = token.RCURLY
case '(':
tok = token.LPAREN
case ')':
insertTerm = true
tok = token.RPAREN
case '[':
tok = token.LBRACK
case ']':
insertTerm = true
tok = token.RBRACK
case ',':
tok = token.COMMA
case '.':
// NOTE: Fractions starting with '.' are handled by outer switch
tok = token.DOT
default:
// s.next() reports invalid BOMs so we don't need to repeat the error.
if ch != bom {
s.onError(pos.Offset(), fmt.Sprintf("illegal character %#U", ch))
}
insertTerm = s.insertTerm // Preserve previous s.insertTerm state
tok = token.ILLEGAL
lit = string(ch)
}
}
if s.mode&dontInsertTerms == 0 {
s.insertTerm = insertTerm
}
return
}
func (s *Scanner) skipWhitespace() {
for s.ch == ' ' || s.ch == '\t' || s.ch == '\r' || (s.ch == '\n' && !s.insertTerm) {
s.next()
}
}
func isLetter(ch rune) bool {
// We check for ASCII first as an optimization, and leave checking unicode
// (the slowest) to the very end.
return (lower(ch) >= 'a' && lower(ch) <= 'z') ||
ch == '_' ||
(ch >= utf8.RuneSelf && unicode.IsLetter(ch))
}
func lower(ch rune) rune { return ('a' - 'A') | ch }
func isDecimal(ch rune) bool { return '0' <= ch && ch <= '9' }
func isDigit(ch rune) bool {
return isDecimal(ch) || (ch >= utf8.RuneSelf && unicode.IsDigit(ch))
}
// scanIdentifier reads the string of valid identifier characters starting at
// s.offet. It must only be called when s.ch is a valid character which starts
// an identifier.
//
// scanIdentifier is highly optimized for identifiers are modifications must be
// made carefully.
func (s *Scanner) scanIdentifier() string {
off := s.offset
// Optimize for common case of ASCII identifiers.
//
// Ranging over s.input[s.readOffset:] avoids bounds checks and avoids
// conversions to runes.
//
// We'll fall back to the slower path if we find a non-ASCII character.
for readOffset, b := range s.input[s.readOffset:] {
if (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9') {
// Common case: ASCII character; don't assign a rune.
continue
}
s.readOffset += readOffset
if b > 0 && b < utf8.RuneSelf {
// Optimization: ASCII character that isn't a letter or number; we've
// reached the end of the identifier sequence and can terminate. We avoid
// the call to s.next() and the corresponding setup.
//
// This optimization only works because we know that s.ch (the current
// character when scanIdentifier was called) is never '\n' since '\n'
// cannot start an identifier.
s.ch = rune(b)
s.offset = s.readOffset
s.readOffset++
goto exit
}
// The preceding character is valid for an identifier because
// scanIdentifier is only called when s.ch is a letter; calling s.next() at
// s.readOffset will reset the scanner state.
s.next()
for isLetter(s.ch) || isDigit(s.ch) {
s.next()
}
// No more valid characters for the identifier; terminate.
goto exit
}
s.offset = len(s.input)
s.readOffset = len(s.input)
s.ch = eof
exit:
return string(s.input[off:s.offset])
}
func (s *Scanner) scanNumber() (tok token.Token, lit string) {
tok = token.NUMBER
off := s.offset
// Integer part of number
if s.ch != '.' {
s.digits()
}
// Fractional part of number
if s.ch == '.' {
tok = token.FLOAT
s.next()
s.digits()
}
// Exponent
if lower(s.ch) == 'e' {
tok = token.FLOAT
s.next()
if s.ch == '+' || s.ch == '-' {
s.next()
}
if s.digits() == 0 {
s.onError(off, "exponent has no digits")
}
}
return tok, string(s.input[off:s.offset])
}
// digits scans a sequence of digits.
func (s *Scanner) digits() (count int) {
for isDecimal(s.ch) {
s.next()
count++
}
return
}
func (s *Scanner) scanString(until rune) string {
// subtract 1 to account for the opening '"' which was already consumed by
// the scanner forcing progress.
off := s.offset - 1
for {
ch := s.ch
if ch == '\n' || ch == eof {
s.onError(off, "string literal not terminated")
break
}
s.next()
if ch == until {
break
}
if ch == '\\' {
s.scanEscape()
}
}
return string(s.input[off:s.offset])
}
// scanEscape parses an escape sequence. In case of a syntax error, scanEscape
// stops at the offending character without consuming it.
func (s *Scanner) scanEscape() {
off := s.offset
var (
n int
base, max uint32
)
switch s.ch {
case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', '"':
s.next()
return
case '0', '1', '2', '3', '4', '5', '6', '7':
n, base, max = 3, 8, 255
case 'x':
s.next()
n, base, max = 2, 16, 255
case 'u':
s.next()
n, base, max = 4, 16, unicode.MaxRune
case 'U':
s.next()
n, base, max = 8, 16, unicode.MaxRune
default:
msg := "unknown escape sequence"
if s.ch == eof {
msg = "escape sequence not terminated"
}
s.onError(off, msg)
return
}
var x uint32
for n > 0 {
d := uint32(digitVal(s.ch))
if d >= base {
msg := fmt.Sprintf("illegal character %#U in escape sequence", s.ch)
if s.ch == eof {
msg = "escape sequence not terminated"
}
s.onError(off, msg)
return
}
x = x*base + d
s.next()
n--
}
if x > max || x >= 0xD800 && x < 0xE000 {
s.onError(off, "escape sequence is invalid Unicode code point")
}
}
func digitVal(ch rune) int {
switch {
case ch >= '0' && ch <= '9':
return int(ch - '0')
case lower(ch) >= 'a' && lower(ch) <= 'f':
return int(lower(ch) - 'a' + 10)
}
return 16 // Larger than any legal digit val
}
func (s *Scanner) scanComment() string {
// The initial character in the comment was already consumed from the scanner
// forcing progress.
//
// slashComment will be true when the comment is a //- or /*-style comment.
var (
off = s.offset - 1 // Offset of initial character
numCR = 0
blockComment = false
)
if s.ch == '/' { // NOTE: s.ch is second character in comment sequence
// //-style comment.
//
// The final '\n' is not considered to be part of the comment.
if s.ch == '/' {
s.next() // Consume second '/'
}
for s.ch != '\n' && s.ch != eof {
if s.ch == '\r' {
numCR++
}
s.next()
}
goto exit
}
// /*-style comment.
blockComment = true
s.next()
for s.ch != eof {
ch := s.ch
if ch == '\r' {
numCR++
}
s.next()
if ch == '*' && s.ch == '/' {
s.next()
goto exit
}
}
s.onError(off, "block comment not terminated")
exit:
lit := s.input[off:s.offset]
// On Windows, a single comment line may end in "\r\n". We want to remove the
// final \r.
if numCR > 0 && len(lit) >= 1 && lit[len(lit)-1] == '\r' {
lit = lit[:len(lit)-1]
numCR--
}
if numCR > 0 {
lit = stripCR(lit, blockComment)
}
return string(lit)
}
func stripCR(b []byte, blockComment bool) []byte {
c := make([]byte, len(b))
i := 0
for j, ch := range b {
if ch != '\r' || blockComment && i > len("/*") && c[i-1] == '*' && j+1 < len(b) && b[j+1] == '/' {
c[i] = ch
i++
}
}
return c[:i]
}
// findLineEnd checks to see if a comment runs to the end of the line.
func (s *Scanner) findLineEnd() bool {
// NOTE: initial '/' is already consumed by forcing the scanner to progress.
defer func(off int) {
// Reset scanner state to where it was upon calling findLineEnd.
s.ch = '/'
s.offset = off
s.readOffset = off + 1
s.next() // Consume initial starting '/' again
}(s.offset - 1)
// Read ahead until a newline, EOF, or non-comment token is found.
// We loop to consume multiple sequences of comment tokens.
for s.ch == '/' || s.ch == '*' {
if s.ch == '/' {
// //-style comments always contain newlines.
return true
}
// We're looking at a /*-style comment; look for its newline.
s.next()
for s.ch != eof {
ch := s.ch
if ch == '\n' {
return true
}
s.next()
if ch == '*' && s.ch == '/' { // End of block comment
s.next()
break
}
}
// Check to see if there's a newline after the block comment.
s.skipWhitespace() // s.insertTerm is set
if s.ch == eof || s.ch == '\n' {
return true
}
if s.ch != '/' {
// Non-comment token
return false
}
s.next() // Consume '/' at the end of the /* style-comment
}
return false
}
// switch2 returns a if s.ch is next, b otherwise. The scanner will be advanced
// if b is returned.
//
// This is used for tokens which can either be a single character but also are
// the starting character for a 2-length token (i.e., = and ==).
func (s *Scanner) switch2(a, b token.Token, next rune) token.Token { //nolint:unparam
if s.ch == next {
s.next()
return b
}
return a
}