ref: 3a53f16966e476d19265aa66feacfb229b58ee02 fastpeg/internal/bootstrap/scanner/scanner_test.go -rw-r--r-- 8.0 KiB View raw
                                                                                
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
package scanner

import (
	"fmt"
	"os"
	"strings"
	"testing"
	"unicode/utf8"

	"git.sr.ht/~mna/fastpeg/internal/bootstrap/token"
	"github.com/stretchr/testify/require"
)

type tuple struct {
	tok token.Token
	lit string
}

func TestScanner_Tokens(t *testing.T) {
	input := `
abc "" 'def'
[] <- ←⟵=
| /     &
&{a} ${ _1 } @{ _-_ }
. : ; ! ? * + ()
"'x'" '"y"' [z]
`

	output := []tuple{
		{token.Identifier, "abc"},
		{token.Literal, `""`},
		{token.Literal, `'def'`},
		{token.CharClass, `[]`},
		{token.Arrow, `<-`},
		{token.Arrow, `←`},
		{token.Arrow, `⟵`},
		{token.Arrow, `=`},
		{token.Separator, `|`},
		{token.Separator, `/`},
		{token.Ampersand, ``},
		{token.PredCoderef, `a`},
		{token.StateCoderef, `_1`},
		{token.ActionCoderef, `_-_`},
		{token.Dot, ``},
		{token.Colon, ``},
		{token.Semicolon, ``},
		{token.Exclamation, ``},
		{token.Question, ``},
		{token.Star, ``},
		{token.Plus, ``},
		{token.Lparen, ``},
		{token.Rparen, ``},
		{token.Literal, `"'x'"`},
		{token.Literal, `'"y"'`},
		{token.CharClass, `[z]`},
	}

	var s Scanner
	result := scanAll(&s, input)
	require.Equal(t, output, result)
	require.NoError(t, s.Err())
}

func TestScanner_Escapes(t *testing.T) {
	input := `
"\a\b\f\n\r\t\v\\\""
'\a\b\f\n\r\t\v\\\''
"\x00\x10\xa1\xAF\xff"
'\x00\x10\xA1\xaf\xff'
[\x00\x10\xA1\xaf\xff]
"\u0123\uabcd\uEFef\U00045678"
'\u0123\uabcd\uEFef\U00045678'
[\u0123\uabcd\uEFef\U00045678]
[\a\b\f\n\r\t\v\\\]\[\-\^]
[\pC\PL\p{X}\P{Latin}]
`

	output := []tuple{
		{token.Literal, `"\a\b\f\n\r\t\v\\\""`},
		{token.Literal, `'\a\b\f\n\r\t\v\\\''`},
		{token.Literal, `"\x00\x10\xa1\xAF\xff"`},
		{token.Literal, `'\x00\x10\xA1\xaf\xff'`},
		{token.CharClass, `[\x00\x10\xA1\xaf\xff]`},
		{token.Literal, `"\u0123\uabcd\uEFef\U00045678"`},
		{token.Literal, `'\u0123\uabcd\uEFef\U00045678'`},
		{token.CharClass, `[\u0123\uabcd\uEFef\U00045678]`},
		{token.CharClass, `[\a\b\f\n\r\t\v\\\]\[\-\^]`},
		{token.CharClass, `[\pC\PL\p{X}\P{Latin}]`},
	}

	var s Scanner
	result := scanAll(&s, input)
	require.Equal(t, output, result)
	require.NoError(t, s.Err())
}

func TestScanner_Errors(t *testing.T) {
	cases := []struct {
		input  string
		output []tuple
		errMsg string
	}{
		{"", nil, ""},
		{
			string(rune(bom)) + "a",
			[]tuple{{token.Identifier, "a"}},
			"",
		},
		{
			"a" + string(rune(bom)),
			[]tuple{
				{token.Identifier, "a"},
				{token.Illegal, "\ufeff"},
			},
			"illegal byte-order mark",
		},
		{
			"\x00",
			[]tuple{{token.Illegal, "\x00"}},
			"illegal character NUL",
		},
		{
			"\xff",
			[]tuple{{token.Illegal, string(utf8.RuneError)}},
			"illegal UTF-8 encoding",
		},
		{
			"<Z",
			[]tuple{{token.Illegal, ""}, {token.Identifier, "Z"}},
			"incomplete arrow symbol",
		},
		{
			"<",
			[]tuple{{token.Illegal, ""}},
			"arrow symbol not terminated",
		},
		{
			"$Z",
			[]tuple{{token.Illegal, ""}, {token.Identifier, "Z"}},
			"invalid state coderef",
		},
		{
			"$",
			[]tuple{{token.Illegal, ""}},
			"state coderef not terminated",
		},
		{
			"${",
			[]tuple{{token.StateCoderef, ""}},
			"coderef not terminated",
		},
		{
			"@Z",
			[]tuple{{token.Illegal, ""}, {token.Identifier, "Z"}},
			"invalid action coderef",
		},
		{
			"@",
			[]tuple{{token.Illegal, ""}},
			"action coderef not terminated",
		},
		{
			"@{",
			[]tuple{{token.ActionCoderef, ""}},
			"coderef not terminated",
		},
		{
			"&Z",
			[]tuple{{token.Ampersand, ""}, {token.Identifier, "Z"}},
			"",
		},
		{
			"&",
			[]tuple{{token.Ampersand, ""}},
			"",
		},
		{
			"&{",
			[]tuple{{token.PredCoderef, ""}},
			"coderef not terminated",
		},
		{
			`"`,
			[]tuple{{token.Literal, `"`}},
			"literal not terminated",
		},
		{
			`'`,
			[]tuple{{token.Literal, `'`}},
			"literal not terminated",
		},
		{
			`"a
      b`,
			[]tuple{{token.Literal, `"a`}, {token.Identifier, "b"}},
			"literal not terminated",
		},
		{
			`'a
      b`,
			[]tuple{{token.Literal, `'a`}, {token.Identifier, "b"}},
			"literal not terminated",
		},
		{
			`[`,
			[]tuple{{token.CharClass, `[`}},
			"character class not terminated",
		},
		{
			`[a
      b`,
			[]tuple{{token.CharClass, `[a`}, {token.Identifier, "b"}},
			"character class not terminated",
		},
		{
			`[\z]`,
			[]tuple{{token.CharClass, `[\z]`}},
			"unknown escape sequence",
		},
		{
			`[\`,
			[]tuple{{token.CharClass, `[\`}},
			"escape sequence not terminated",
		},
		{
			`[\x`,
			[]tuple{{token.CharClass, `[\x`}},
			"escape sequence not terminated",
		},
		{
			`[\x0`,
			[]tuple{{token.CharClass, `[\x0`}},
			"escape sequence not terminated",
		},
		{
			`[\u000`,
			[]tuple{{token.CharClass, `[\u000`}},
			"escape sequence not terminated",
		},
		{
			`[\U0000001`,
			[]tuple{{token.CharClass, `[\U0000001`}},
			"escape sequence not terminated",
		},
		{
			`[\x0G]`,
			[]tuple{{token.CharClass, `[\x0G]`}},
			"illegal character U+0047",
		},
		{
			`[\u000G]`,
			[]tuple{{token.CharClass, `[\u000G]`}},
			"illegal character U+0047",
		},
		{
			`[\U0000000G]`,
			[]tuple{{token.CharClass, `[\U0000000G]`}},
			"illegal character U+0047",
		},
		{
			`[\uD800]`,
			[]tuple{{token.CharClass, `[\uD800]`}},
			"invalid Unicode code point",
		},
		{
			`[\U99999999]`,
			[]tuple{{token.CharClass, `[\U99999999]`}},
			"invalid Unicode code point",
		},
		{
			`[\p`,
			[]tuple{{token.CharClass, `[\p`}},
			"escape sequence not terminated",
		},
		{
			`[\p{`,
			[]tuple{{token.CharClass, `[\p{`}},
			"escape sequence not terminated",
		},
		{
			`[\P`,
			[]tuple{{token.CharClass, `[\P`}},
			"escape sequence not terminated",
		},
		{
			`[\P{`,
			[]tuple{{token.CharClass, `[\P{`}},
			"escape sequence not terminated",
		},
		{
			`[\PA]`,
			[]tuple{{token.CharClass, `[\PA]`}},
			"invalid Unicode class",
		},
		{
			`[\p{abc`,
			[]tuple{{token.CharClass, `[\p{abc`}},
			"escape sequence not terminated",
		},
		{
			`[\p{abc
      d`,
			[]tuple{{token.CharClass, `[\p{abc`}, {token.Identifier, "d"}},
			"escape sequence not terminated",
		},
	}
	for _, c := range cases {
		t.Run(c.input, func(t *testing.T) {
			var s Scanner
			result := scanAll(&s, c.input)
			require.Equal(t, c.output, result)

			if c.errMsg == "" {
				require.NoError(t, s.Err())
				return
			}

			err := s.Err()
			require.Error(t, err)
			PrintError(os.Stderr, err)
			require.Contains(t, err.Error(), c.errMsg)
		})
	}
}

func TestScanner_Pos(t *testing.T) {
	// NOTE: ⟵ is 3 bytes, ← too, BOM too (all in utf-8)

	// list of files sent to the scanner in the same FileSet
	inputs := []string{
		` start ⟵ expression*
expression <- factor (_ factor)?
`,
		`factor = "lit"
            | 'lat';`,
		"\ufeff\n\tchars←[\\a-\\r]+",
	}

	expect := `
test-0:1:2:1: Identifier[start]
test-0:1:8:7: Arrow[⟵]
test-0:1:12:11: Identifier[expression]
test-0:1:22:21: *[]
test-0:2:1:23: Identifier[expression]
test-0:2:12:34: Arrow[<-]
test-0:2:15:37: Identifier[factor]
test-0:2:22:44: ([]
test-0:2:23:45: Identifier[_]
test-0:2:25:47: Identifier[factor]
test-0:2:31:53: )[]
test-0:2:32:54: ?[]
test-0:2:34:56: EOF[]
test-1:1:1:0: Identifier[factor]
test-1:1:8:7: Arrow[=]
test-1:1:10:9: Literal["lit"]
test-1:2:13:27: Separator[|]
test-1:2:15:29: Literal['lat']
test-1:2:20:34: ;[]
test-1:2:21:35: EOF[]
test-2:2:2:5: Identifier[chars]
test-2:2:7:10: Arrow[←]
test-2:2:10:13: CharClass[[\a-\r]]
test-2:2:17:20: +[]
test-2:2:18:21: EOF[]
`

	var s Scanner
	var result strings.Builder

	fs := token.NewFileSet()
	for i, input := range inputs {
		f := fs.AddFile(fmt.Sprintf("test-%d", i), -1, len(input))
		s.Init(f, []byte(input))

		for {
			pos, tok, lit := s.Scan()
			lpos := fs.Position(pos)
			fmt.Fprintf(&result, "%s:%d:%d:%d: %s[%s]\n", lpos.Filename, lpos.Line, lpos.Column, lpos.Offset, tok, lit)
			if tok == token.EOF {
				break
			}
		}
	}

	require.Equal(t, strings.TrimSpace(expect), strings.TrimSpace(result.String()))
}

func scanAll(s *Scanner, input string) []tuple {
	var result []tuple

	fs := token.NewFileSet()
	f := fs.AddFile("test", -1, len(input))
	s.Init(f, []byte(input))
	for {
		_, tok, lit := s.Scan()
		if tok == token.EOF {
			break
		}
		result = append(result, tuple{tok, lit})
	}
	return result
}