summaryrefslogtreecommitdiff
path: root/src/pkg/go/scanner/scanner_test.go
blob: 0cb200b48fae175ceddfbb3de2e84cb3d3f029e2 (plain)
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
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package scanner

import (
	"go/scanner";
	"go/token";
	"os";
	"strings";
	"testing";
)


const /* class */ (
	special = iota;
	literal;
	operator;
	keyword;
)


func tokenclass(tok token.Token) int {
	switch {
	case tok.IsLiteral(): return literal;
	case tok.IsOperator(): return operator;
	case tok.IsKeyword(): return keyword;
	}
	return special;
}


type elt struct {
	tok token.Token;
	lit string;
	class int;
}


var tokens = [...]elt{
	// Special tokens
	elt{ token.COMMENT, "/* a comment */", special },
	elt{ token.COMMENT, "// a comment \n", special },

	// Identifiers and basic type literals
	elt{ token.IDENT, "foobar", literal },
	elt{ token.IDENT, "a۰۱۸", literal },
	elt{ token.IDENT, "foo६४", literal },
	elt{ token.IDENT, "bar9876", literal },
	elt{ token.INT, "0", literal },
	elt{ token.INT, "01234567", literal },
	elt{ token.INT, "0xcafebabe", literal },
	elt{ token.FLOAT, "0.", literal },
	elt{ token.FLOAT, ".0", literal },
	elt{ token.FLOAT, "3.14159265", literal },
	elt{ token.FLOAT, "1e0", literal },
	elt{ token.FLOAT, "1e+100", literal },
	elt{ token.FLOAT, "1e-100", literal },
	elt{ token.FLOAT, "2.71828e-1000", literal },
	elt{ token.CHAR, "'a'", literal },
	elt{ token.CHAR, "'\\000'", literal },
	elt{ token.CHAR, "'\\xFF'", literal },
	elt{ token.CHAR, "'\\uff16'", literal },
	elt{ token.CHAR, "'\\U0000ff16'", literal },
	elt{ token.STRING, "`foobar`", literal },
	elt{ token.STRING, "`" `foo
	                        bar` "`", literal },

	// Operators and delimitors
	elt{ token.ADD, "+", operator },
	elt{ token.SUB, "-", operator },
	elt{ token.MUL, "*", operator },
	elt{ token.QUO, "/", operator },
	elt{ token.REM, "%", operator },

	elt{ token.AND, "&", operator },
	elt{ token.OR, "|", operator },
	elt{ token.XOR, "^", operator },
	elt{ token.SHL, "<<", operator },
	elt{ token.SHR, ">>", operator },
	elt{ token.AND_NOT, "&^", operator },

	elt{ token.ADD_ASSIGN, "+=", operator },
	elt{ token.SUB_ASSIGN, "-=", operator },
	elt{ token.MUL_ASSIGN, "*=", operator },
	elt{ token.QUO_ASSIGN, "/=", operator },
	elt{ token.REM_ASSIGN, "%=", operator },

	elt{ token.AND_ASSIGN, "&=", operator },
	elt{ token.OR_ASSIGN, "|=", operator },
	elt{ token.XOR_ASSIGN, "^=", operator },
	elt{ token.SHL_ASSIGN, "<<=", operator },
	elt{ token.SHR_ASSIGN, ">>=", operator },
	elt{ token.AND_NOT_ASSIGN, "&^=", operator },

	elt{ token.LAND, "&&", operator },
	elt{ token.LOR, "||", operator },
	elt{ token.ARROW, "<-", operator },
	elt{ token.INC, "++", operator },
	elt{ token.DEC, "--", operator },

	elt{ token.EQL, "==", operator },
	elt{ token.LSS, "<", operator },
	elt{ token.GTR, ">", operator },
	elt{ token.ASSIGN, "=", operator },
	elt{ token.NOT, "!", operator },

	elt{ token.NEQ, "!=", operator },
	elt{ token.LEQ, "<=", operator },
	elt{ token.GEQ, ">=", operator },
	elt{ token.DEFINE, ":=", operator },
	elt{ token.ELLIPSIS, "...", operator },

	elt{ token.LPAREN, "(", operator },
	elt{ token.LBRACK, "[", operator },
	elt{ token.LBRACE, "{", operator },
	elt{ token.COMMA, ",", operator },
	elt{ token.PERIOD, ".", operator },

	elt{ token.RPAREN, ")", operator },
	elt{ token.RBRACK, "]", operator },
	elt{ token.RBRACE, "}", operator },
	elt{ token.SEMICOLON, ";", operator },
	elt{ token.COLON, ":", operator },

	// Keywords
	elt{ token.BREAK, "break", keyword },
	elt{ token.CASE, "case", keyword },
	elt{ token.CHAN, "chan", keyword },
	elt{ token.CONST, "const", keyword },
	elt{ token.CONTINUE, "continue", keyword },

	elt{ token.DEFAULT, "default", keyword },
	elt{ token.DEFER, "defer", keyword },
	elt{ token.ELSE, "else", keyword },
	elt{ token.FALLTHROUGH, "fallthrough", keyword },
	elt{ token.FOR, "for", keyword },

	elt{ token.FUNC, "func", keyword },
	elt{ token.GO, "go", keyword },
	elt{ token.GOTO, "goto", keyword },
	elt{ token.IF, "if", keyword },
	elt{ token.IMPORT, "import", keyword },

	elt{ token.INTERFACE, "interface", keyword },
	elt{ token.MAP, "map", keyword },
	elt{ token.PACKAGE, "package", keyword },
	elt{ token.RANGE, "range", keyword },
	elt{ token.RETURN, "return", keyword },

	elt{ token.SELECT, "select", keyword },
	elt{ token.STRUCT, "struct", keyword },
	elt{ token.SWITCH, "switch", keyword },
	elt{ token.TYPE, "type", keyword },
	elt{ token.VAR, "var", keyword },
}


const whitespace = "  \t  \n\n\n";  // to separate tokens

type TestErrorHandler struct {
	t *testing.T
}

func (h *TestErrorHandler) Error(pos token.Position, msg string) {
	h.t.Errorf("Error() called (msg = %s)", msg);
}


func NewlineCount(s string) int {
	n := 0;
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' {
			n++;
		}
	}
	return n;
}


func checkPos(t *testing.T, lit string, pos, expected token.Position) {
	if pos.Filename != expected.Filename {
		t.Errorf("bad filename for %s: got %s, expected %s", lit, pos.Filename, expected.Filename);
	}
	if pos.Offset != expected.Offset {
		t.Errorf("bad position for %s: got %d, expected %d", lit, pos.Offset, expected.Offset);
	}
	if pos.Line != expected.Line {
		t.Errorf("bad line for %s: got %d, expected %d", lit, pos.Line, expected.Line);
	}
	if pos.Column!= expected.Column {
		t.Errorf("bad column for %s: got %d, expected %d", lit, pos.Column, expected.Column);
	}
}


// Verify that calling Scan() provides the correct results.
func TestScan(t *testing.T) {
	// make source
	var src string;
	for _, e := range tokens {
		src += e.lit + whitespace;
	}
	whitespace_linecount := NewlineCount(whitespace);

	// verify scan
	index := 0;
	epos := token.Position{"", 0, 1, 1};
	nerrors := scanner.Tokenize("", strings.Bytes(src), &TestErrorHandler{t}, scanner.ScanComments,
		func (pos token.Position, tok token.Token, litb []byte) bool {
			e := elt{token.EOF, "", special};
			if index < len(tokens) {
				e = tokens[index];
			}
			lit := string(litb);
			if tok == token.EOF {
				lit = "<EOF>";
				epos.Column = 0;
			}
			checkPos(t, lit, pos, epos);
			if tok != e.tok {
				t.Errorf("bad token for %s: got %s, expected %s", lit, tok.String(), e.tok.String());
			}
			if e.tok.IsLiteral() && lit != e.lit {
				t.Errorf("bad literal for %s: got %s, expected %s", lit, lit, e.lit);
			}
			if tokenclass(tok) != e.class {
				t.Errorf("bad class for %s: got %d, expected %d", lit, tokenclass(tok), e.class);
			}
			epos.Offset += len(lit) + len(whitespace);
			epos.Line += NewlineCount(lit) + whitespace_linecount;
			if tok == token.COMMENT && litb[1] == '/' {
				// correct for unaccounted '/n' in //-style comment
				epos.Offset++;
				epos.Line++;
			}
			index++;
			return tok != token.EOF;
		}
	);
	if nerrors != 0 {
		t.Errorf("found %d errors", nerrors);
	}
}


type seg struct {
	srcline string;  // a line of source text
	filename string;  // filename for current token
	line int;  // line number for current token
}


var segments = []seg{
	// exactly one token per line since the test consumes one token per segment
	seg{ "  line1", "TestLineComments", 1 },
	seg{ "\nline2", "TestLineComments", 2 },
	seg{ "\nline3  //line File1.go:100", "TestLineComments", 3 },  // bad line comment, ignored
	seg{ "\nline4", "TestLineComments", 4 },
	seg{ "\n//line File1.go:100\n  line100", "File1.go", 100 },
	seg{ "\n//line File2.go:200\n  line200", "File2.go", 200 },
	seg{ "\n//line :1\n  line1", "", 1 },
	seg{ "\n//line foo:42\n  line42", "foo", 42 },
	seg{ "\n //line foo:42\n  line44", "foo", 44 },  // bad line comment, ignored
	seg{ "\n//line foo 42\n  line46", "foo", 46 },  // bad line comment, ignored
	seg{ "\n//line foo:42 extra text\n  line48", "foo", 48 },  // bad line comment, ignored
	seg{ "\n//line foo:42\n  line42", "foo", 42 },
	seg{ "\n//line foo:42\n  line42", "foo", 42 },
	seg{ "\n//line File1.go:100\n  line100", "File1.go", 100 },
}


// Verify that comments of the form "//line filename:line" are interpreted correctly.
func TestLineComments(t *testing.T) {
	// make source
	var src string;
	for _, e := range segments {
		src += e.srcline;
	}

	// verify scan
	var S scanner.Scanner;
	S.Init("TestLineComments", strings.Bytes(src), nil, 0);
	for _, s := range segments {
		pos, tok, lit := S.Scan();
		checkPos(t, string(lit), pos, token.Position{s.filename, pos.Offset, s.line, pos.Column});
	}

	if S.ErrorCount != 0 {
		t.Errorf("found %d errors", S.ErrorCount);
	}
}


// Verify that initializing the same scanner more then once works correctly.
func TestInit(t *testing.T) {
	var s scanner.Scanner;

	// 1st init
	s.Init("", strings.Bytes("if true { }"), nil, 0);
	s.Scan();  // if
	s.Scan();  // true
	pos, tok, lit := s.Scan();  // {
	if tok != token.LBRACE {
		t.Errorf("bad token: got %s, expected %s", tok.String(), token.LBRACE);
	}

	// 2nd init
	s.Init("", strings.Bytes("go true { ]"), nil, 0);
	pos, tok, lit = s.Scan();  // go
	if tok != token.GO {
		t.Errorf("bad token: got %s, expected %s", tok.String(), token.GO);
	}

	if s.ErrorCount != 0 {
		t.Errorf("found %d errors", s.ErrorCount);
	}
}


func TestIllegalChars(t *testing.T) {
	var s scanner.Scanner;

	const src = "*?*$*@*";
	s.Init("", strings.Bytes(src), &TestErrorHandler{t}, scanner.AllowIllegalChars);
	for offs, ch := range src {
		pos, tok, lit := s.Scan();
		if pos.Offset != offs {
			t.Errorf("bad position for %s: got %d, expected %d", string(lit), pos.Offset, offs);
		}
		if tok == token.ILLEGAL && string(lit) != string(ch) {
			t.Errorf("bad token: got %s, expected %s", string(lit), string(ch));
		}
	}

	if s.ErrorCount != 0 {
		t.Errorf("found %d errors", s.ErrorCount);
	}
}


func TestStdErrorHander(t *testing.T) {
	const src =
		"@\n"  // illegal character, cause an error
		"@ @\n"  // two errors on the same line
		"//line File2:20\n"
		"@\n"  // different file, but same line
		"//line File2:1\n"
		"@ @\n"  // same file, decreasing line number
		"//line File1:1\n"
		"@ @ @"  // original file, line 1 again
	;

	var s scanner.Scanner;
	v := NewErrorVector();
	nerrors := scanner.Tokenize("File1", strings.Bytes(src), v, 0,
		func (pos token.Position, tok token.Token, litb []byte) bool {
			return tok != token.EOF;
		}
	);

	list := v.GetErrorList(Raw);
	if len(list) != 9 {
		t.Errorf("found %d raw errors, expected 9", len(list));
		PrintError(os.Stderr, list);
	}

	list = v.GetErrorList(Sorted);
	if len(list) != 9 {
		t.Errorf("found %d sorted errors, expected 9", len(list));
		PrintError(os.Stderr, list);
	}

	list = v.GetErrorList(NoMultiples);
	if len(list) != 4 {
		t.Errorf("found %d one-per-line errors, expected 4", len(list));
		PrintError(os.Stderr, list);
	}

	if v.ErrorCount() != nerrors {
		t.Errorf("found %d errors, expected %d", v.ErrorCount(), nerrors);
	}
}