-
Notifications
You must be signed in to change notification settings - Fork 19
/
lexer_test.go
274 lines (262 loc) · 10.5 KB
/
lexer_test.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
package memefish
import (
"fmt"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
. "github.com/cloudspannerecosystem/memefish/token"
)
// Keep same order https://github.com/google/zetasql/blob/master/zetasql/parser/flex_tokenizer.l
var symbols = []string{
"(",
"[",
"{",
")",
"]",
"}",
"*",
",",
"=",
"+=",
"-=",
"!=",
"<=",
"<<",
"=>",
"->",
"<",
">",
">=",
"||",
"|",
"^",
"&",
"+",
"-",
"/",
"~",
"?",
"!",
"%",
"|>",
"@",
"@@",
".",
":",
"\\",
";",
"$",
"<>", // <> is not a valid token in ZetaSQL, but it is a token in memefish
">>", // >> is not a valid token in ZetaSQL, but it is a token in memefish.
}
var lexerTestCases = []struct {
source string
tokens []*Token
}{
// Spaces
{" 0", []*Token{{Kind: "<int>", Space: " ", Raw: "0", Base: 10}}},
// Comment
{"# foo", nil},
{"# foo\n0", []*Token{{Kind: "<int>", Space: "", Raw: "0", Base: 10, Comments: []TokenComment{{Space: "", Raw: "# foo\n", Pos: 0, End: 6}}}}},
{"-- foo\n0", []*Token{{Kind: "<int>", Space: "", Raw: "0", Base: 10, Comments: []TokenComment{{Space: "", Raw: "-- foo\n", Pos: 0, End: 7}}}}},
{"// foo\n0", []*Token{{Kind: "<int>", Space: "", Raw: "0", Base: 10, Comments: []TokenComment{{Space: "", Raw: "// foo\n", Pos: 0, End: 7}}}}},
{"/* foo */ 0", []*Token{{Kind: "<int>", Space: " ", Raw: "0", Base: 10, Comments: []TokenComment{{Space: "", Raw: "/* foo */", Pos: 0, End: 9}}}}},
{"// aaa\n// bbb\n/* foo */ 0", []*Token{{Kind: "<int>", Space: " ", Raw: "0", Base: 10, Comments: []TokenComment{
{Space: "", Raw: "// aaa\n", Pos: 0, End: 7},
{Space: "", Raw: "// bbb\n", Pos: 7, End: 14},
{Space: "", Raw: "/* foo */", Pos: 14, End: 23},
}}}},
// TokenInt
{"0", []*Token{{Kind: TokenInt, Raw: "0", Base: 10}}},
{"1", []*Token{{Kind: TokenInt, Raw: "1", Base: 10}}},
{"123", []*Token{{Kind: TokenInt, Raw: "123", Base: 10}}},
{"+123", []*Token{{Kind: "+", Raw: "+"}, {Kind: TokenInt, Raw: "123", Base: 10}}},
{"-123", []*Token{{Kind: "-", Raw: "-"}, {Kind: TokenInt, Raw: "123", Base: 10}}},
{"9223372036854775807", []*Token{{Kind: TokenInt, Raw: "9223372036854775807", Base: 10}}},
{"-9223372036854775808", []*Token{{Kind: "-", Raw: "-"}, {Kind: TokenInt, Raw: "9223372036854775808", Base: 10}}},
{"0123", []*Token{{Kind: TokenInt, Raw: "0123", Base: 10}}}, // TODO: fix base
{"0xbeaf", []*Token{{Kind: TokenInt, Raw: "0xbeaf", Base: 16}}},
{"0XBEAF", []*Token{{Kind: TokenInt, Raw: "0XBEAF", Base: 16}}},
// TokenFloat
{"1.2", []*Token{{Kind: TokenFloat, Raw: "1.2"}}},
{"+1.2", []*Token{{Kind: "+", Raw: "+"}, {Kind: TokenFloat, Raw: "1.2"}}},
{"-1.2", []*Token{{Kind: "-", Raw: "-"}, {Kind: TokenFloat, Raw: "1.2"}}},
{".1", []*Token{{Kind: TokenFloat, Raw: ".1"}}},
{"00.1", []*Token{{Kind: TokenFloat, Raw: "00.1"}}},
{"1.", []*Token{{Kind: TokenFloat, Raw: "1."}}},
{"1e1", []*Token{{Kind: TokenFloat, Raw: "1e1"}}},
{"1E1", []*Token{{Kind: TokenFloat, Raw: "1E1"}}},
{"1e+1", []*Token{{Kind: TokenFloat, Raw: "1e+1"}}},
{"1e-1", []*Token{{Kind: TokenFloat, Raw: "1e-1"}}},
// TokenParam
{"@foo", []*Token{{Kind: TokenParam, Raw: "@foo", AsString: "foo"}}},
{"@foo.1", []*Token{{Kind: TokenParam, Raw: "@foo", AsString: "foo"}, {Kind: ".", Raw: "."}, {Kind: TokenIdent, Raw: "1", AsString: "1"}}},
// TokenIdent
{"foo", []*Token{{Kind: TokenIdent, Raw: "foo", AsString: "foo"}}},
{"foo_bar", []*Token{{Kind: TokenIdent, Raw: "foo_bar", AsString: "foo_bar"}}},
{"foo.1", []*Token{{Kind: TokenIdent, Raw: "foo", AsString: "foo"}, {Kind: ".", Raw: "."}, {Kind: TokenIdent, Raw: "1", AsString: "1"}}},
{"foo.*", []*Token{{Kind: TokenIdent, Raw: "foo", AsString: "foo"}, {Kind: ".", Raw: "."}, {Kind: "*", Raw: "*"}}},
{"`select`", []*Token{{Kind: TokenIdent, Raw: "`select`", AsString: "select"}}},
{"`select`.1", []*Token{{Kind: TokenIdent, Raw: "`select`", AsString: "select"}, {Kind: ".", Raw: "."}, {Kind: TokenIdent, Raw: "1", AsString: "1"}}},
{"].1", []*Token{{Kind: "]", Raw: "]"}, {Kind: ".", Raw: "."}, {Kind: TokenIdent, Raw: "1", AsString: "1"}}},
{").1", []*Token{{Kind: ")", Raw: ")"}, {Kind: ".", Raw: "."}, {Kind: TokenIdent, Raw: "1", AsString: "1"}}},
{"`foo\\u0031`", []*Token{{Kind: TokenIdent, Raw: "`foo\\u0031`", AsString: "foo1"}}},
{"BR", []*Token{{Kind: TokenIdent, Raw: "BR", AsString: "BR"}}},
{`R "foo"`, []*Token{{Kind: TokenIdent, Raw: "R", AsString: "R"}, {Kind: TokenString, Space: " ", Raw: `"foo"`, AsString: "foo"}}},
// TokenString
{`""`, []*Token{{Kind: TokenString, Raw: `""`, AsString: ""}}},
{`''`, []*Token{{Kind: TokenString, Raw: `''`, AsString: ""}}},
{`"foo"`, []*Token{{Kind: TokenString, Raw: `"foo"`, AsString: "foo"}}},
{`'foo'`, []*Token{{Kind: TokenString, Raw: `'foo'`, AsString: "foo"}}},
{`"""foo\nbar"""`, []*Token{{Kind: TokenString, Raw: `"""foo\nbar"""`, AsString: "foo\nbar"}}},
{"'''foo\nbar'''", []*Token{{Kind: TokenString, Raw: "'''foo\nbar'''", AsString: "foo\nbar"}}},
{`"\a\b\f\n\r\t\v\\\?\"\'"`, []*Token{{Kind: TokenString, Raw: `"\a\b\f\n\r\t\v\\\?\"\'"`, AsString: "\a\b\f\n\r\t\v\\?\"'"}}},
{"'\\`'", []*Token{{Kind: TokenString, Raw: "'\\`'", AsString: "`"}}},
{`"\061\x31\X31\u0031\U00000031"`, []*Token{{Kind: TokenString, Raw: `"\061\x31\X31\u0031\U00000031"`, AsString: "11111"}}},
{`"\xff"`, []*Token{{Kind: TokenString, Raw: `"\xff"`, AsString: "\xff"}}},
{`R"\\"`, []*Token{{Kind: TokenString, Raw: `R"\\"`, AsString: "\\\\"}}},
{`R'\\'`, []*Token{{Kind: TokenString, Raw: `R'\\'`, AsString: "\\\\"}}},
{`r"\\"`, []*Token{{Kind: TokenString, Raw: `r"\\"`, AsString: "\\\\"}}},
{`r'\\'`, []*Token{{Kind: TokenString, Raw: `r'\\'`, AsString: "\\\\"}}},
{`R"""\\"""`, []*Token{{Kind: TokenString, Raw: `R"""\\"""`, AsString: "\\\\"}}},
{`R'''\\'''`, []*Token{{Kind: TokenString, Raw: `R'''\\'''`, AsString: "\\\\"}}},
// ByteString
{`B"foo"`, []*Token{{Kind: TokenBytes, Raw: `B"foo"`, AsString: "foo"}}},
{`B'foo'`, []*Token{{Kind: TokenBytes, Raw: `B'foo'`, AsString: "foo"}}},
{`b"foo"`, []*Token{{Kind: TokenBytes, Raw: `b"foo"`, AsString: "foo"}}},
{`b'foo'`, []*Token{{Kind: TokenBytes, Raw: `b'foo'`, AsString: "foo"}}},
{`B"""foo"""`, []*Token{{Kind: TokenBytes, Raw: `B"""foo"""`, AsString: "foo"}}},
{`B'''foo'''`, []*Token{{Kind: TokenBytes, Raw: `B'''foo'''`, AsString: "foo"}}},
{`B"\a\b\f\n\r\t\v\\\?\"\'"`, []*Token{{Kind: TokenBytes, Raw: `B"\a\b\f\n\r\t\v\\\?\"\'"`, AsString: "\a\b\f\n\r\t\v\\?\"'"}}},
{`RB"foo"`, []*Token{{Kind: TokenBytes, Raw: `RB"foo"`, AsString: "foo"}}},
{"RB'''foo\nbar'''", []*Token{{Kind: TokenBytes, Raw: "RB'''foo\nbar'''", AsString: "foo\nbar"}}},
{`rb"foo"`, []*Token{{Kind: TokenBytes, Raw: `rb"foo"`, AsString: "foo"}}},
{`BR"foo"`, []*Token{{Kind: TokenBytes, Raw: `BR"foo"`, AsString: "foo"}}},
}
var lexerWrongTestCase = []struct {
source string
pos Pos
end Pos
message string
}{
{"\b", 0, 0, "illegal input character: '\\b'"},
{`"foo`, 0, 4, "unclosed string literal"},
{`R"foo`, 1, 5, "unclosed raw string literal"},
{"'foo\n", 0, 4, "unclosed string literal: newline appears in non triple-quoted"},
{"R'foo\n", 1, 5, "unclosed raw string literal: newline appears in non triple-quoted"},
{"R'foo\\", 5, 6, "invalid escape sequence: \\<eof>"},
{`"\400"`, 1, 3, "invalid escape sequence: \\4"},
{`"\3xx"`, 1, 4, "invalid escape sequence: octal escape sequence must be follwed by 3 octal digits"},
{`"\xZZ"`, 1, 4, "invalid escape sequence: hex escape sequence must be follwed by 2 hex digits"},
{`"\XZZ"`, 1, 4, "invalid escape sequence: hex escape sequence must be follwed by 2 hex digits"},
{`B"\u0031"`, 2, 4, "invalid escape sequence: \\u is not allowed in bytes literal"},
{`B"\U00000031"`, 2, 4, "invalid escape sequence: \\U is not allowed in bytes literal"},
{`B"\U00000031"`, 2, 4, "invalid escape sequence: \\U is not allowed in bytes literal"},
{`"\UFFFFFFFF"`, 1, 11, "invalid escape sequence: invalid code point: U+FFFFFFFF"},
{"``", 0, 2, "invalid empty identifier"},
{"1from", 1, 1, "number literal cannot follow identifier without any spaces"},
{`'''0`, 0, 4, "unclosed triple-quoted string literal"},
{`/*`, 0, 2, "unclosed comment"},
}
func testLexer(t *testing.T, source string, tokens []*Token) {
t.Helper()
l := &Lexer{
File: &File{FilePath: "[test]", Buffer: source},
}
for _, t2 := range tokens {
err := l.NextToken()
if err != nil {
t.Errorf("error on lexer: %v", err)
return
}
opts := []cmp.Option{
cmpopts.IgnoreFields(Token{}, "Pos", "End"),
}
if diff := cmp.Diff(&l.Token, t2, opts...); diff != "" {
t.Errorf("(-got, +want)\n%s", diff)
}
}
err := l.NextToken()
if err != nil {
t.Errorf("error on lexer: %v", err)
return
}
if l.Token.Kind != TokenEOF {
t.Errorf("expected EOF, but: %#v", &l.Token)
return
}
}
func TestLexer(t *testing.T) {
for _, s := range Keywords {
t.Run(fmt.Sprintf("keyword/%q", string(s)), func(t *testing.T) {
testLexer(t, string(s), []*Token{{Kind: s, Raw: string(s)}})
})
l := strings.ToLower(string(s))
t.Run(fmt.Sprintf("keyword/%q", l), func(t *testing.T) {
testLexer(t, l, []*Token{{Kind: s, Raw: l}})
})
}
for _, s := range symbols {
t.Run(fmt.Sprintf("symbol/%q", s), func(t *testing.T) {
testLexer(t, s, []*Token{{Kind: TokenKind(s), Raw: s}})
})
}
for _, tc := range lexerTestCases {
t.Run(fmt.Sprintf("testcase/%q", tc.source), func(t *testing.T) {
testLexer(t, tc.source, tc.tokens)
})
}
}
func TestLexerWrong(t *testing.T) {
for _, tc := range lexerWrongTestCase {
t.Run(fmt.Sprintf("testcase/%q", tc.source), func(t *testing.T) {
l := &Lexer{
File: &File{FilePath: "[test]", Buffer: tc.source},
}
var err error
for l.Token.Kind != TokenEOF {
err = l.NextToken()
if err != nil {
break
}
}
if err == nil {
t.Errorf("unexpected EOF")
return
}
if e, ok := err.(*Error); ok {
if e.Message != tc.message {
t.Errorf("expected error message: %q, but: %q", tc.message, e.Message)
}
if e.Position.Pos != tc.pos {
t.Errorf("expected error position (pos): %v, but: %v", tc.pos, e.Position.Pos)
}
if e.Position.End != tc.end {
t.Errorf("expected error position (end): %v, but: %v", tc.end, e.Position.End)
}
} else {
t.Errorf("unexpected error: %v", err)
}
})
}
}
func TestLexerWrongNoError(t *testing.T) {
for _, tc := range lexerWrongTestCase {
t.Run(fmt.Sprintf("testcase/%q", tc.source), func(t *testing.T) {
l := &Lexer{
File: &File{FilePath: "[test]", Buffer: tc.source},
}
hasBad := false
for l.Token.Kind != TokenEOF {
l.nextToken(true)
if l.Token.Kind == TokenBad {
hasBad = true
}
}
if !hasBad {
t.Errorf("expected <bad>")
}
})
}
}