forked from googleapis/google-cloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
3765 lines (3349 loc) · 82 KB
/
parser.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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Package spansql contains types and a parser for the Cloud Spanner SQL dialect.
To parse, use one of the Parse functions (ParseDDL, ParseDDLStmt, ParseQuery, etc.).
Sources:
https://cloud.google.com/spanner/docs/lexical
https://cloud.google.com/spanner/docs/query-syntax
https://cloud.google.com/spanner/docs/data-definition-language
*/
package spansql
/*
This file is structured as follows:
- There are several exported ParseFoo functions that accept an input string
and return a type defined in types.go. This is the principal API of this package.
These functions are implemented as wrappers around the lower-level functions,
with additional checks to ensure things such as input exhaustion.
- The token and parser types are defined. These constitute the lexical token
and parser machinery. parser.next is the main way that other functions get
the next token, with parser.back providing a single token rewind, and
parser.sniff, parser.eat and parser.expect providing lookahead helpers.
- The parseFoo methods are defined, matching the SQL grammar. Each consumes its
namesake production from the parser. There are also some fooParser helper vars
defined that abbreviate the parsing of some of the regular productions.
*/
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"unicode/utf8"
"cloud.google.com/go/civil"
)
const debug = false
func debugf(format string, args ...interface{}) {
if !debug {
return
}
fmt.Fprintf(os.Stderr, "spansql debug: "+format+"\n", args...)
}
// ParseDDL parses a DDL file.
//
// The provided filename is used for error reporting and will
// appear in the returned structure.
func ParseDDL(filename, s string) (*DDL, error) {
ddl := &DDL{}
if err := parseStatements(ddl, filename, s); err != nil {
return nil, err
}
return ddl, nil
}
// ParseDML parses a DML file.
//
// The provided filename is used for error reporting and will
// appear in the returned structure.
func ParseDML(filename, s string) (*DML, error) {
dml := &DML{}
if err := parseStatements(dml, filename, s); err != nil {
return nil, err
}
return dml, nil
}
func parseStatements(stmts statements, filename string, s string) error {
p := newParser(filename, s)
stmts.setFilename(filename)
for {
p.skipSpace()
if p.done {
break
}
switch v := stmts.(type) {
case *DDL:
stmt, err := p.parseDDLStmt()
if err != nil {
return err
}
v.List = append(v.List, stmt)
case *DML:
stmt, err := p.parseDMLStmt()
if err != nil {
return err
}
v.List = append(v.List, stmt)
}
tok := p.next()
if tok.err == eof {
break
} else if tok.err != nil {
return tok.err
}
if tok.value == ";" {
continue
} else {
return p.errorf("unexpected token %q", tok.value)
}
}
if p.Rem() != "" {
return fmt.Errorf("unexpected trailing contents %q", p.Rem())
}
// Handle comments.
for _, com := range p.comments {
c := &Comment{
Marker: com.marker,
Isolated: com.isolated,
Start: com.start,
End: com.end,
Text: com.text,
}
// Strip common whitespace prefix and any whitespace suffix.
// TODO: This is a bodgy implementation of Longest Common Prefix,
// and also doesn't do tabs vs. spaces well.
var prefix string
for i, line := range c.Text {
line = strings.TrimRight(line, " \b\t")
c.Text[i] = line
trim := len(line) - len(strings.TrimLeft(line, " \b\t"))
if i == 0 {
prefix = line[:trim]
} else {
// Check how much of prefix is in common.
for !strings.HasPrefix(line, prefix) {
prefix = prefix[:len(prefix)-1]
}
}
if prefix == "" {
break
}
}
if prefix != "" {
for i, line := range c.Text {
c.Text[i] = strings.TrimPrefix(line, prefix)
}
}
stmts.addComment(c)
}
return nil
}
// ParseDDLStmt parses a single DDL statement.
func ParseDDLStmt(s string) (DDLStmt, error) {
p := newParser("-", s)
stmt, err := p.parseDDLStmt()
if err != nil {
return nil, err
}
if p.Rem() != "" {
return nil, fmt.Errorf("unexpected trailing contents %q", p.Rem())
}
return stmt, nil
}
// ParseDMLStmt parses a single DML statement.
func ParseDMLStmt(s string) (DMLStmt, error) {
p := newParser("-", s)
stmt, err := p.parseDMLStmt()
if err != nil {
return nil, err
}
if p.Rem() != "" {
return nil, fmt.Errorf("unexpected trailing contents %q", p.Rem())
}
return stmt, nil
}
// ParseQuery parses a query string.
func ParseQuery(s string) (Query, error) {
p := newParser("-", s)
q, err := p.parseQuery()
if err != nil {
return Query{}, err
}
if p.Rem() != "" {
return Query{}, fmt.Errorf("unexpected trailing query contents %q", p.Rem())
}
return q, nil
}
type token struct {
value string
err *parseError
line, offset int
typ tokenType
float64 float64
string string // unquoted form for stringToken/bytesToken/quotedID
// int64Token is parsed as a number only when it is known to be a literal.
// This permits correct handling of operators preceding such a token,
// which cannot be identified as part of the int64 until later.
int64Base int
}
type tokenType int
const (
unknownToken tokenType = iota
int64Token
float64Token
stringToken
bytesToken
unquotedID
quotedID
)
func (t *token) String() string {
if t.err != nil {
return fmt.Sprintf("parse error: %v", t.err)
}
return strconv.Quote(t.value)
}
type parseError struct {
message string
filename string
line int // 1-based line number
offset int // 0-based byte offset from start of input
}
func (pe *parseError) Error() string {
if pe == nil {
return "<nil>"
}
if pe.line == 1 {
return fmt.Sprintf("%s:1.%d: %v", pe.filename, pe.offset, pe.message)
}
return fmt.Sprintf("%s:%d: %v", pe.filename, pe.line, pe.message)
}
var eof = &parseError{message: "EOF"}
type parser struct {
s string // Remaining input.
done bool // Whether the parsing is finished (success or error).
backed bool // Whether back() was called.
cur token
filename string
line, offset int // updated by places that shrink s
comments []comment // accumulated during parse
}
type comment struct {
marker string // "#" or "--" or "/*"
isolated bool // if it starts on its own line
start, end Position
text []string
}
// Pos reports the position of the current token.
func (p *parser) Pos() Position { return Position{Line: p.cur.line, Offset: p.cur.offset} }
func newParser(filename, s string) *parser {
return &parser{
s: s,
cur: token{line: 1},
filename: filename,
line: 1,
}
}
// Rem returns the unparsed remainder, ignoring space.
func (p *parser) Rem() string {
rem := p.s
if p.backed {
rem = p.cur.value + rem
}
i := 0
for ; i < len(rem); i++ {
if !isSpace(rem[i]) {
break
}
}
return rem[i:]
}
func (p *parser) String() string {
if p.backed {
return fmt.Sprintf("next tok: %s (rem: %q)", &p.cur, p.s)
}
return fmt.Sprintf("rem: %q", p.s)
}
func (p *parser) errorf(format string, args ...interface{}) *parseError {
pe := &parseError{
message: fmt.Sprintf(format, args...),
filename: p.filename,
line: p.cur.line,
offset: p.cur.offset,
}
p.cur.err = pe
p.done = true
return pe
}
func isInitialIdentifierChar(c byte) bool {
// https://cloud.google.com/spanner/docs/lexical#identifiers
switch {
case 'A' <= c && c <= 'Z':
return true
case 'a' <= c && c <= 'z':
return true
case c == '_':
return true
}
return false
}
func isIdentifierChar(c byte) bool {
// https://cloud.google.com/spanner/docs/lexical#identifiers
// This doesn't apply the restriction that an identifier cannot start with [0-9],
// nor does it check against reserved keywords.
switch {
case 'A' <= c && c <= 'Z':
return true
case 'a' <= c && c <= 'z':
return true
case '0' <= c && c <= '9':
return true
case c == '_':
return true
}
return false
}
func isHexDigit(c byte) bool {
return '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F'
}
func isOctalDigit(c byte) bool {
return '0' <= c && c <= '7'
}
func (p *parser) consumeNumber() {
/*
int64_value:
{ decimal_value | hex_value }
decimal_value:
[-]0—9+
hex_value:
[-]0[xX]{0—9|a—f|A—F}+
(float64_value is not formally specified)
float64_value :=
[+-]DIGITS.[DIGITS][e[+-]DIGITS]
| [DIGITS].DIGITS[e[+-]DIGITS]
| DIGITSe[+-]DIGITS
*/
i, neg, base := 0, false, 10
float, e, dot := false, false, false
if p.s[i] == '-' {
neg = true
i++
} else if p.s[i] == '+' {
// This isn't in the formal grammar, but is mentioned informally.
// https://cloud.google.com/spanner/docs/lexical#integer-literals
i++
}
if strings.HasPrefix(p.s[i:], "0x") || strings.HasPrefix(p.s[i:], "0X") {
base = 16
i += 2
}
d0 := i
digitLoop:
for i < len(p.s) {
switch c := p.s[i]; {
case '0' <= c && c <= '9':
i++
case base == 16 && 'A' <= c && c <= 'F':
i++
case base == 16 && 'a' <= c && c <= 'f':
i++
case base == 10 && (c == 'e' || c == 'E'):
if e {
p.errorf("bad token %q", p.s[:i])
return
}
// Switch to consuming float.
float, e = true, true
i++
if i < len(p.s) && (p.s[i] == '+' || p.s[i] == '-') {
i++
}
case base == 10 && c == '.':
if dot || e { // any dot must come before E
p.errorf("bad token %q", p.s[:i])
return
}
// Switch to consuming float.
float, dot = true, true
i++
default:
break digitLoop
}
}
if d0 == i {
p.errorf("no digits in numeric literal")
return
}
sign := ""
if neg {
sign = "-"
}
p.cur.value, p.s = p.s[:i], p.s[i:]
p.offset += i
var err error
if float {
p.cur.typ = float64Token
p.cur.float64, err = strconv.ParseFloat(sign+p.cur.value[d0:], 64)
} else {
p.cur.typ = int64Token
p.cur.value = sign + p.cur.value[d0:]
p.cur.int64Base = base
// This is parsed on demand.
}
if err != nil {
p.errorf("bad numeric literal %q: %v", p.cur.value, err)
}
}
func (p *parser) consumeString() {
// https://cloud.google.com/spanner/docs/lexical#string-and-bytes-literals
delim := p.stringDelimiter()
if p.cur.err != nil {
return
}
p.cur.string, p.cur.err = p.consumeStringContent(delim, false, true, "string literal")
p.cur.typ = stringToken
}
func (p *parser) consumeRawString() {
// https://cloud.google.com/spanner/docs/lexical#string-and-bytes-literals
p.s = p.s[1:] // consume 'R'
delim := p.stringDelimiter()
if p.cur.err != nil {
return
}
p.cur.string, p.cur.err = p.consumeStringContent(delim, true, true, "raw string literal")
p.cur.typ = stringToken
}
func (p *parser) consumeBytes() {
// https://cloud.google.com/spanner/docs/lexical#string-and-bytes-literals
p.s = p.s[1:] // consume 'B'
delim := p.stringDelimiter()
if p.cur.err != nil {
return
}
p.cur.string, p.cur.err = p.consumeStringContent(delim, false, false, "bytes literal")
p.cur.typ = bytesToken
}
func (p *parser) consumeRawBytes() {
// https://cloud.google.com/spanner/docs/lexical#string-and-bytes-literals
p.s = p.s[2:] // consume 'RB'
delim := p.stringDelimiter()
if p.cur.err != nil {
return
}
p.cur.string, p.cur.err = p.consumeStringContent(delim, true, false, "raw bytes literal")
p.cur.typ = bytesToken
}
// stringDelimiter returns the opening string delimiter.
func (p *parser) stringDelimiter() string {
c := p.s[0]
if c != '"' && c != '\'' {
p.errorf("invalid string literal")
return ""
}
// Look for triple.
if len(p.s) >= 3 && p.s[1] == c && p.s[2] == c {
return p.s[:3]
}
return p.s[:1]
}
// consumeStringContent consumes a string-like literal, including its delimiters.
//
// - delim is the opening/closing delimiter.
// - raw is true if consuming a raw string.
// - unicode is true if unicode escape sequence (\uXXXX or \UXXXXXXXX) are permitted.
// - name identifies the name of the consuming token.
//
// It is designed for consuming string, bytes literals, and also backquoted identifiers.
func (p *parser) consumeStringContent(delim string, raw, unicode bool, name string) (string, *parseError) {
// https://cloud.google.com/spanner/docs/lexical#string-and-bytes-literals
if len(delim) == 3 {
name = "triple-quoted " + name
}
i := len(delim)
var content []byte
for i < len(p.s) {
if strings.HasPrefix(p.s[i:], delim) {
i += len(delim)
p.s = p.s[i:]
p.offset += i
return string(content), nil
}
if p.s[i] == '\\' {
i++
if i >= len(p.s) {
return "", p.errorf("unclosed %s", name)
}
if raw {
content = append(content, '\\', p.s[i])
i++
continue
}
switch p.s[i] {
case 'a':
i++
content = append(content, '\a')
case 'b':
i++
content = append(content, '\b')
case 'f':
i++
content = append(content, '\f')
case 'n':
i++
content = append(content, '\n')
case 'r':
i++
content = append(content, '\r')
case 't':
i++
content = append(content, '\t')
case 'v':
i++
content = append(content, '\v')
case '\\':
i++
content = append(content, '\\')
case '?':
i++
content = append(content, '?')
case '"':
i++
content = append(content, '"')
case '\'':
i++
content = append(content, '\'')
case '`':
i++
content = append(content, '`')
case 'x', 'X':
i++
if !(i+1 < len(p.s) && isHexDigit(p.s[i]) && isHexDigit(p.s[i+1])) {
return "", p.errorf("illegal escape sequence: hex escape sequence must be followed by 2 hex digits")
}
c, err := strconv.ParseUint(p.s[i:i+2], 16, 64)
if err != nil {
return "", p.errorf("illegal escape sequence: invalid hex digits: %q: %v", p.s[i:i+2], err)
}
content = append(content, byte(c))
i += 2
case 'u', 'U':
t := p.s[i]
if !unicode {
return "", p.errorf("illegal escape sequence: \\%c", t)
}
i++
size := 4
if t == 'U' {
size = 8
}
if i+size-1 >= len(p.s) {
return "", p.errorf("illegal escape sequence: \\%c escape sequence must be followed by %d hex digits", t, size)
}
for j := 0; j < size; j++ {
if !isHexDigit(p.s[i+j]) {
return "", p.errorf("illegal escape sequence: \\%c escape sequence must be followed by %d hex digits", t, size)
}
}
c, err := strconv.ParseUint(p.s[i:i+size], 16, 64)
if err != nil {
return "", p.errorf("illegal escape sequence: invalid \\%c digits: %q: %v", t, p.s[i:i+size], err)
}
if 0xD800 <= c && c <= 0xDFFF || 0x10FFFF < c {
return "", p.errorf("illegal escape sequence: invalid codepoint: %x", c)
}
var buf [utf8.UTFMax]byte
n := utf8.EncodeRune(buf[:], rune(c))
content = append(content, buf[:n]...)
i += size
case '0', '1', '2', '3', '4', '5', '6', '7':
if !(i+2 < len(p.s) && isOctalDigit(p.s[i+1]) && isOctalDigit(p.s[i+2])) {
return "", p.errorf("illegal escape sequence: octal escape sequence must be followed by 3 octal digits")
}
c, err := strconv.ParseUint(p.s[i:i+3], 8, 64)
if err != nil {
return "", p.errorf("illegal escape sequence: invalid octal digits: %q: %v", p.s[i:i+3], err)
}
if c >= 256 {
return "", p.errorf("illegal escape sequence: octal digits overflow: %q (%d)", p.s[i:i+3], c)
}
content = append(content, byte(c))
i += 3
default:
return "", p.errorf("illegal escape sequence: \\%c", p.s[i])
}
continue
}
if p.s[i] == '\n' {
if len(delim) != 3 { // newline is only allowed inside triple-quoted.
return "", p.errorf("newline forbidden in %s", name)
}
p.line++
}
content = append(content, p.s[i])
i++
}
return "", p.errorf("unclosed %s", name)
}
var operators = map[string]bool{
// Arithmetic operators.
"-": true, // both unary and binary
"~": true,
"*": true,
"/": true,
"||": true,
"+": true,
"<<": true,
">>": true,
"&": true,
"^": true,
"|": true,
// Comparison operators.
"<": true,
"<=": true,
">": true,
">=": true,
"=": true,
"!=": true,
"<>": true,
}
func isSpace(c byte) bool {
// Per https://cloud.google.com/spanner/docs/lexical, informally,
// whitespace is defined as "space, backspace, tab, newline".
switch c {
case ' ', '\b', '\t', '\n':
return true
}
return false
}
// skipSpace skips past any space or comments.
func (p *parser) skipSpace() bool {
initLine := p.line
// If we start capturing a comment in this method,
// this is set to its comment value. Multi-line comments
// are only joined during a single skipSpace invocation.
var com *comment
i := 0
for i < len(p.s) {
if isSpace(p.s[i]) {
if p.s[i] == '\n' {
p.line++
}
i++
continue
}
// Comments.
marker, term := "", ""
if p.s[i] == '#' {
marker, term = "#", "\n"
} else if i+1 < len(p.s) && p.s[i] == '-' && p.s[i+1] == '-' {
marker, term = "--", "\n"
} else if i+1 < len(p.s) && p.s[i] == '/' && p.s[i+1] == '*' {
marker, term = "/*", "*/"
}
if term == "" {
break
}
// Search for the terminator, starting after the marker.
ti := strings.Index(p.s[i+len(marker):], term)
if ti < 0 {
p.errorf("unterminated comment")
return false
}
ti += len(marker) // make ti relative to p.s[i:]
if com != nil && (com.end.Line+1 < p.line || com.marker != marker) {
// There's a previous comment, but there's an
// intervening blank line, or the marker changed.
// Terminate the previous comment.
com = nil
}
if com == nil {
// New comment.
p.comments = append(p.comments, comment{
marker: marker,
isolated: (p.line != initLine) || p.line == 1,
start: Position{
Line: p.line,
Offset: p.offset + i,
},
})
com = &p.comments[len(p.comments)-1]
}
textLines := strings.Split(p.s[i+len(marker):i+ti], "\n")
com.text = append(com.text, textLines...)
com.end = Position{
Line: p.line + len(textLines) - 1,
Offset: p.offset + i + ti,
}
p.line = com.end.Line
if term == "\n" {
p.line++
}
i += ti + len(term)
// A non-isolated comment is always complete and doesn't get
// combined with any future comment.
if !com.isolated {
com = nil
}
}
p.s = p.s[i:]
p.offset += i
if p.s == "" {
p.done = true
}
return i > 0
}
// advance moves the parser to the next token, which will be available in p.cur.
func (p *parser) advance() {
prevID := p.cur.typ == quotedID || p.cur.typ == unquotedID
p.skipSpace()
if p.done {
return
}
// If the previous token was an identifier (quoted or unquoted),
// the next token being a dot means this is a path expression (not a number).
if prevID && p.s[0] == '.' {
p.cur.err = nil
p.cur.line, p.cur.offset = p.line, p.offset
p.cur.typ = unknownToken
p.cur.value, p.s = p.s[:1], p.s[1:]
p.offset++
return
}
p.cur.err = nil
p.cur.line, p.cur.offset = p.line, p.offset
p.cur.typ = unknownToken
// TODO: struct literals
switch p.s[0] {
case ',', ';', '(', ')', '{', '}', '[', ']', '*', '+', '-':
// Single character symbol.
p.cur.value, p.s = p.s[:1], p.s[1:]
p.offset++
return
// String literal prefix.
case 'B', 'b', 'R', 'r', '"', '\'':
// "B", "b", "BR", "Rb" etc are valid string literal prefix, however "BB", "rR" etc are not.
raw, bytes := false, false
for i := 0; i < 4 && i < len(p.s); i++ {
switch {
case !raw && (p.s[i] == 'R' || p.s[i] == 'r'):
raw = true
continue
case !bytes && (p.s[i] == 'B' || p.s[i] == 'b'):
bytes = true
continue
case p.s[i] == '"' || p.s[i] == '\'':
switch {
case raw && bytes:
p.consumeRawBytes()
case raw:
p.consumeRawString()
case bytes:
p.consumeBytes()
default:
p.consumeString()
}
return
}
break
}
case '`':
// Quoted identifier.
p.cur.string, p.cur.err = p.consumeStringContent("`", false, true, "quoted identifier")
p.cur.typ = quotedID
return
}
if p.s[0] == '@' || isInitialIdentifierChar(p.s[0]) {
// Start consuming identifier.
i := 1
for i < len(p.s) && isIdentifierChar(p.s[i]) {
i++
}
p.cur.value, p.s = p.s[:i], p.s[i:]
p.cur.typ = unquotedID
p.offset += i
return
}
if len(p.s) >= 2 && p.s[0] == '.' && ('0' <= p.s[1] && p.s[1] <= '9') {
// dot followed by a digit.
p.consumeNumber()
return
}
if '0' <= p.s[0] && p.s[0] <= '9' {
p.consumeNumber()
return
}
// Look for operator (two or one bytes).
for i := 2; i >= 1; i-- {
if i <= len(p.s) && operators[p.s[:i]] {
p.cur.value, p.s = p.s[:i], p.s[i:]
p.offset += i
return
}
}
p.errorf("unexpected byte %#x", p.s[0])
}
// back steps the parser back one token. It cannot be called twice in succession.
func (p *parser) back() {
if p.backed {
panic("parser backed up twice")
}
p.done = false
p.backed = true
// If an error was being recovered, we wish to ignore the error.
// Don't do that for eof since that'll be returned next.
if p.cur.err != eof {
p.cur.err = nil
}
}
// next returns the next token.
func (p *parser) next() *token {
if p.backed || p.done {
p.backed = false
return &p.cur
}
p.advance()
if p.done && p.cur.err == nil {
p.cur.value = ""
p.cur.err = eof
}
debugf("parser·next(): returning [%v] [err: %v] @l%d,o%d", p.cur.value, p.cur.err, p.cur.line, p.cur.offset)
return &p.cur
}
// caseEqual reports whether the token is valid, not a quoted identifier, and
// equal to the provided string under a case insensitive comparison.
// Use this (or sniff/eat/expect) instead of comparing a string directly for keywords, etc.
func (t *token) caseEqual(x string) bool {
return t.err == nil && t.typ != quotedID && strings.EqualFold(t.value, x)
}
// sniff reports whether the next N tokens are as specified.
func (p *parser) sniff(want ...string) bool {
// Store current parser state and restore on the way out.
orig := *p
defer func() { *p = orig }()
for _, w := range want {
if !p.next().caseEqual(w) {
return false
}
}
return true
}
// sniffTokenType reports whether the next token type is as specified.
func (p *parser) sniffTokenType(want tokenType) bool {
orig := *p
defer func() { *p = orig }()
if p.next().typ == want {
return true
}
return false
}
// eat reports whether the next N tokens are as specified,
// then consumes them.
func (p *parser) eat(want ...string) bool {
// Store current parser state so we can restore if we get a failure.
orig := *p
for _, w := range want {
if !p.next().caseEqual(w) {
// Mismatch.
*p = orig
return false
}
}
return true
}
func (p *parser) expect(want ...string) *parseError {
for _, w := range want {
tok := p.next()
if tok.err != nil {
return tok.err
}
if !tok.caseEqual(w) {
return p.errorf("got %q while expecting %q", tok.value, w)
}
}
return nil
}
func (p *parser) parseDDLStmt() (DDLStmt, *parseError) {
debugf("parseDDLStmt: %v", p)
/*
statement:
{ create_database | create_table | create_index | alter_table | drop_table | drop_index | create_change_stream | alter_change_stream | drop_change_stream }
*/
// TODO: support create_database
if p.sniff("CREATE", "TABLE") {
ct, err := p.parseCreateTable()
return ct, err
} else if p.sniff("CREATE", "INDEX") || p.sniff("CREATE", "UNIQUE", "INDEX") || p.sniff("CREATE", "NULL_FILTERED", "INDEX") || p.sniff("CREATE", "UNIQUE", "NULL_FILTERED", "INDEX") {
ci, err := p.parseCreateIndex()
return ci, err
} else if p.sniff("CREATE", "VIEW") || p.sniff("CREATE", "OR", "REPLACE", "VIEW") {
cv, err := p.parseCreateView()
return cv, err
} else if p.sniff("ALTER", "TABLE") {
a, err := p.parseAlterTable()