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
|
package spec
import (
"strings"
"testing"
verr "github.com/nihei9/vartan/error"
)
func TestLexer_Run(t *testing.T) {
idTok := func(text string) *token {
return newIDToken(text, newPosition(1, 0))
}
termPatTok := func(text string) *token {
return newTerminalPatternToken(text, newPosition(1, 0))
}
strTok := func(text string) *token {
return newStringLiteralToken(text, newPosition(1, 0))
}
symTok := func(kind tokenKind) *token {
return newSymbolToken(kind, newPosition(1, 0))
}
invalidTok := func(text string) *token {
return newInvalidToken(text, newPosition(1, 0))
}
tests := []struct {
caption string
src string
tokens []*token
err error
}{
{
caption: "the lexer can recognize all kinds of tokens",
src: `id"terminal"'string':|;@...#$()`,
tokens: []*token{
idTok("id"),
termPatTok("terminal"),
strTok(`string`),
symTok(tokenKindColon),
symTok(tokenKindOr),
symTok(tokenKindSemicolon),
symTok(tokenKindLabelMarker),
symTok(tokenKindExpantion),
symTok(tokenKindDirectiveMarker),
symTok(tokenKindOrderedSymbolMarker),
symTok(tokenKindLParen),
symTok(tokenKindRParen),
newEOFToken(),
},
},
{
caption: "the lexer can recognize keywords",
src: `fragment`,
tokens: []*token{
symTok(tokenKindKWFragment),
newEOFToken(),
},
},
{
caption: "the lexer can recognize character sequences and escape sequences in a terminal",
src: `"abc\"\\"`,
tokens: []*token{
termPatTok(`abc"\\`),
newEOFToken(),
},
},
{
caption: "the lexer can recognize character sequences and escape sequences in a string literal",
src: `'.*+?|()[\'\\'`,
tokens: []*token{
strTok(`.*+?|()['\`),
newEOFToken(),
},
},
{
caption: "a pattern must include at least one character",
src: `""`,
err: synErrEmptyPattern,
},
{
caption: "a string must include at least one character",
src: `''`,
err: synErrEmptyString,
},
{
caption: "the lexer can recognize newlines and combine consecutive newlines into one",
src: "\u000A | \u000D | \u000D\u000A | \u000A\u000A \u000D\u000D \u000D\u000A\u000D\u000A",
tokens: []*token{
symTok(tokenKindNewline),
symTok(tokenKindOr),
symTok(tokenKindNewline),
symTok(tokenKindOr),
symTok(tokenKindNewline),
symTok(tokenKindOr),
symTok(tokenKindNewline),
newEOFToken(),
},
},
{
caption: "the lexer ignores line comments",
src: `
// This is the first comment.
foo
// This is the second comment.
// This is the third comment.
bar // This is the fourth comment.
`,
tokens: []*token{
symTok(tokenKindNewline),
idTok("foo"),
symTok(tokenKindNewline),
idTok("bar"),
symTok(tokenKindNewline),
newEOFToken(),
},
},
{
caption: "an identifier cannot contain the capital-case letters",
src: `Abc`,
err: synErrIDInvalidChar,
},
{
caption: "an identifier cannot contain the capital-case letters",
src: `Zyx`,
err: synErrIDInvalidChar,
},
{
caption: "the underscore cannot be placed at the beginning of an identifier",
src: `_abc`,
err: synErrIDInvalidUnderscorePos,
},
{
caption: "the underscore cannot be placed at the end of an identifier",
src: `abc_`,
err: synErrIDInvalidUnderscorePos,
},
{
caption: "the underscore cannot be placed consecutively",
src: `a__b`,
err: synErrIDConsecutiveUnderscores,
},
{
caption: "the digits cannot be placed at the biginning of an identifier",
src: `0abc`,
err: synErrIDInvalidDigitsPos,
},
{
caption: "the digits cannot be placed at the biginning of an identifier",
src: `9abc`,
err: synErrIDInvalidDigitsPos,
},
{
caption: "an unclosed terminal is not a valid token",
src: `"abc`,
err: synErrUnclosedTerminal,
},
{
caption: "an incompleted terminal in a pattern is not a valid token",
src: `"\`,
err: synErrIncompletedEscSeq,
},
{
caption: "an unclosed string is not a valid token",
src: `'abc`,
err: synErrUnclosedString,
},
{
caption: "an incompleted terminal in a string is not a valid token",
src: `'\`,
err: synErrIncompletedEscSeq,
},
{
caption: "the lexer can recognize valid tokens following an invalid token",
src: `abc!!!def`,
tokens: []*token{
idTok("abc"),
invalidTok("!!!"),
idTok("def"),
newEOFToken(),
},
},
{
caption: "the lexer skips white spaces",
// \u0009: HT
// \u0020: SP
src: "a\u0009b\u0020c",
tokens: []*token{
idTok("a"),
idTok("b"),
idTok("c"),
newEOFToken(),
},
},
}
for _, tt := range tests {
t.Run(tt.caption, func(t *testing.T) {
l, err := newLexer(strings.NewReader(tt.src))
if err != nil {
t.Fatal(err)
}
n := 0
for {
var tok *token
tok, err = l.next()
if err != nil {
break
}
testToken(t, tok, tt.tokens[n])
n++
if tok.kind == tokenKindEOF {
break
}
}
if tt.err != nil {
synErr, ok := err.(*verr.SpecError)
if !ok {
t.Fatalf("unexpected error; want: %v, got: %v", tt.err, err)
}
if tt.err != synErr.Cause {
t.Fatalf("unexpected error; want: %v, got: %v", tt.err, synErr.Cause)
}
} else {
if err != nil {
t.Fatalf("unexpected error; want: %v, got: %v", tt.err, err)
}
}
})
}
}
func testToken(t *testing.T, tok, expected *token) {
t.Helper()
if tok.kind != expected.kind || tok.text != expected.text {
t.Fatalf("unexpected token; want: %+v, got: %+v", expected, tok)
}
}
|