| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package scanner |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"go/token" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"runtime" |
| 13 |
"slices" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
) |
| 17 |
|
| 18 |
var fset = token.NewFileSet() |
| 19 |
|
| 20 |
const /* class */ ( |
| 21 |
special = iota |
| 22 |
literal |
| 23 |
operator |
| 24 |
keyword |
| 25 |
) |
| 26 |
|
| 27 |
func tokenclass(tok token.Token) int { |
| 28 |
switch { |
| 29 |
case tok.IsLiteral(): |
| 30 |
return literal |
| 31 |
case tok.IsOperator(): |
| 32 |
return operator |
| 33 |
case tok.IsKeyword(): |
| 34 |
return keyword |
| 35 |
} |
| 36 |
return special |
| 37 |
} |
| 38 |
|
| 39 |
type elt struct { |
| 40 |
tok token.Token |
| 41 |
lit string |
| 42 |
class int |
| 43 |
} |
| 44 |
|
| 45 |
var tokens = []elt{ |
| 46 |
// Special tokens |
| 47 |
{token.COMMENT, "/* a comment */", special}, |
| 48 |
{token.COMMENT, "// a comment \n", special}, |
| 49 |
{token.COMMENT, "/*\r*/", special}, |
| 50 |
{token.COMMENT, "/**\r/*/", special}, // issue 11151 |
| 51 |
{token.COMMENT, "/**\r\r/*/", special}, |
| 52 |
{token.COMMENT, "//\r\n", special}, |
| 53 |
|
| 54 |
// Identifiers and basic type literals |
| 55 |
{token.IDENT, "foobar", literal}, |
| 56 |
{token.IDENT, "a۰۱۸", literal}, |
| 57 |
{token.IDENT, "foo६४", literal}, |
| 58 |
{token.IDENT, "bar9876", literal}, |
| 59 |
{token.IDENT, "ŝ", literal}, // was bug (issue 4000) |
| 60 |
{token.IDENT, "ŝfoo", literal}, // was bug (issue 4000) |
| 61 |
{token.INT, "0", literal}, |
| 62 |
{token.INT, "1", literal}, |
| 63 |
{token.INT, "123456789012345678890", literal}, |
| 64 |
{token.INT, "01234567", literal}, |
| 65 |
{token.INT, "0xcafebabe", literal}, |
| 66 |
{token.FLOAT, "0.", literal}, |
| 67 |
{token.FLOAT, ".0", literal}, |
| 68 |
{token.FLOAT, "3.14159265", literal}, |
| 69 |
{token.FLOAT, "1e0", literal}, |
| 70 |
{token.FLOAT, "1e+100", literal}, |
| 71 |
{token.FLOAT, "1e-100", literal}, |
| 72 |
{token.FLOAT, "2.71828e-1000", literal}, |
| 73 |
{token.IMAG, "0i", literal}, |
| 74 |
{token.IMAG, "1i", literal}, |
| 75 |
{token.IMAG, "012345678901234567889i", literal}, |
| 76 |
{token.IMAG, "123456789012345678890i", literal}, |
| 77 |
{token.IMAG, "0.i", literal}, |
| 78 |
{token.IMAG, ".0i", literal}, |
| 79 |
{token.IMAG, "3.14159265i", literal}, |
| 80 |
{token.IMAG, "1e0i", literal}, |
| 81 |
{token.IMAG, "1e+100i", literal}, |
| 82 |
{token.IMAG, "1e-100i", literal}, |
| 83 |
{token.IMAG, "2.71828e-1000i", literal}, |
| 84 |
{token.CHAR, "'a'", literal}, |
| 85 |
{token.CHAR, "'\\000'", literal}, |
| 86 |
{token.CHAR, "'\\xFF'", literal}, |
| 87 |
{token.CHAR, "'\\uff16'", literal}, |
| 88 |
{token.CHAR, "'\\U0000ff16'", literal}, |
| 89 |
{token.STRING, "`foobar`", literal}, |
| 90 |
{token.STRING, "`" + `foo |
| 91 |
bar` + |
| 92 |
"`", |
| 93 |
literal, |
| 94 |
}, |
| 95 |
{token.STRING, "`\r`", literal}, |
| 96 |
{token.STRING, "`foo\r\nbar`", literal}, |
| 97 |
|
| 98 |
// Operators and delimiters |
| 99 |
{token.ADD, "+", operator}, |
| 100 |
{token.SUB, "-", operator}, |
| 101 |
{token.MUL, "*", operator}, |
| 102 |
{token.QUO, "/", operator}, |
| 103 |
{token.REM, "%", operator}, |
| 104 |
|
| 105 |
{token.AND, "&", operator}, |
| 106 |
{token.OR, "|", operator}, |
| 107 |
{token.XOR, "^", operator}, |
| 108 |
{token.SHL, "<<", operator}, |
| 109 |
{token.SHR, ">>", operator}, |
| 110 |
{token.AND_NOT, "&^", operator}, |
| 111 |
|
| 112 |
{token.ADD_ASSIGN, "+=", operator}, |
| 113 |
{token.SUB_ASSIGN, "-=", operator}, |
| 114 |
{token.MUL_ASSIGN, "*=", operator}, |
| 115 |
{token.QUO_ASSIGN, "/=", operator}, |
| 116 |
{token.REM_ASSIGN, "%=", operator}, |
| 117 |
|
| 118 |
{token.AND_ASSIGN, "&=", operator}, |
| 119 |
{token.OR_ASSIGN, "|=", operator}, |
| 120 |
{token.XOR_ASSIGN, "^=", operator}, |
| 121 |
{token.SHL_ASSIGN, "<<=", operator}, |
| 122 |
{token.SHR_ASSIGN, ">>=", operator}, |
| 123 |
{token.AND_NOT_ASSIGN, "&^=", operator}, |
| 124 |
|
| 125 |
{token.LAND, "&&", operator}, |
| 126 |
{token.LOR, "||", operator}, |
| 127 |
{token.ARROW, "<-", operator}, |
| 128 |
{token.INC, "++", operator}, |
| 129 |
{token.DEC, "--", operator}, |
| 130 |
|
| 131 |
{token.EQL, "==", operator}, |
| 132 |
{token.LSS, "<", operator}, |
| 133 |
{token.GTR, ">", operator}, |
| 134 |
{token.ASSIGN, "=", operator}, |
| 135 |
{token.NOT, "!", operator}, |
| 136 |
|
| 137 |
{token.NEQ, "!=", operator}, |
| 138 |
{token.LEQ, "<=", operator}, |
| 139 |
{token.GEQ, ">=", operator}, |
| 140 |
{token.DEFINE, ":=", operator}, |
| 141 |
{token.ELLIPSIS, "...", operator}, |
| 142 |
|
| 143 |
{token.LPAREN, "(", operator}, |
| 144 |
{token.LBRACK, "[", operator}, |
| 145 |
{token.LBRACE, "{", operator}, |
| 146 |
{token.COMMA, ",", operator}, |
| 147 |
{token.PERIOD, ".", operator}, |
| 148 |
|
| 149 |
{token.RPAREN, ")", operator}, |
| 150 |
{token.RBRACK, "]", operator}, |
| 151 |
{token.RBRACE, "}", operator}, |
| 152 |
{token.SEMICOLON, ";", operator}, |
| 153 |
{token.COLON, ":", operator}, |
| 154 |
{token.TILDE, "~", operator}, |
| 155 |
|
| 156 |
// Keywords |
| 157 |
{token.BREAK, "break", keyword}, |
| 158 |
{token.CASE, "case", keyword}, |
| 159 |
{token.CHAN, "chan", keyword}, |
| 160 |
{token.CONST, "const", keyword}, |
| 161 |
{token.CONTINUE, "continue", keyword}, |
| 162 |
|
| 163 |
{token.DEFAULT, "default", keyword}, |
| 164 |
{token.DEFER, "defer", keyword}, |
| 165 |
{token.ELSE, "else", keyword}, |
| 166 |
{token.FALLTHROUGH, "fallthrough", keyword}, |
| 167 |
{token.FOR, "for", keyword}, |
| 168 |
|
| 169 |
{token.FUNC, "func", keyword}, |
| 170 |
{token.GO, "go", keyword}, |
| 171 |
{token.GOTO, "goto", keyword}, |
| 172 |
{token.IF, "if", keyword}, |
| 173 |
{token.IMPORT, "import", keyword}, |
| 174 |
|
| 175 |
{token.INTERFACE, "interface", keyword}, |
| 176 |
{token.MAP, "map", keyword}, |
| 177 |
{token.PACKAGE, "package", keyword}, |
| 178 |
{token.RANGE, "range", keyword}, |
| 179 |
{token.RETURN, "return", keyword}, |
| 180 |
|
| 181 |
{token.SELECT, "select", keyword}, |
| 182 |
{token.STRUCT, "struct", keyword}, |
| 183 |
{token.SWITCH, "switch", keyword}, |
| 184 |
{token.TYPE, "type", keyword}, |
| 185 |
{token.VAR, "var", keyword}, |
| 186 |
} |
| 187 |
|
| 188 |
const whitespace = " \t \n\n\n" // to separate tokens |
| 189 |
|
| 190 |
var source = func() []byte { |
| 191 |
var src []byte |
| 192 |
for _, t := range tokens { |
| 193 |
src = append(src, t.lit...) |
| 194 |
src = append(src, whitespace...) |
| 195 |
} |
| 196 |
return src |
| 197 |
}() |
| 198 |
|
| 199 |
func newlineCount(s string) int { |
| 200 |
n := 0 |
| 201 |
for i := 0; i < len(s); i++ { |
| 202 |
if s[i] == '\n' { |
| 203 |
n++ |
| 204 |
} |
| 205 |
} |
| 206 |
return n |
| 207 |
} |
| 208 |
|
| 209 |
func checkPos(t *testing.T, lit string, p token.Pos, expected token.Position) { |
| 210 |
pos := fset.Position(p) |
| 211 |
// Check cleaned filenames so that we don't have to worry about |
| 212 |
// different os.PathSeparator values. |
| 213 |
if pos.Filename != expected.Filename && filepath.Clean(pos.Filename) != filepath.Clean(expected.Filename) { |
| 214 |
t.Errorf("bad filename for %q: got %s, expected %s", lit, pos.Filename, expected.Filename) |
| 215 |
} |
| 216 |
if pos.Offset != expected.Offset { |
| 217 |
t.Errorf("bad position for %q: got %d, expected %d", lit, pos.Offset, expected.Offset) |
| 218 |
} |
| 219 |
if pos.Line != expected.Line { |
| 220 |
t.Errorf("bad line for %q: got %d, expected %d", lit, pos.Line, expected.Line) |
| 221 |
} |
| 222 |
if pos.Column != expected.Column { |
| 223 |
t.Errorf("bad column for %q: got %d, expected %d", lit, pos.Column, expected.Column) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
// Verify that calling Scan() provides the correct results. |
| 228 |
func TestScan(t *testing.T) { |
| 229 |
whitespace_linecount := newlineCount(whitespace) |
| 230 |
|
| 231 |
// error handler |
| 232 |
eh := func(_ token.Position, msg string) { |
| 233 |
t.Errorf("error handler called (msg = %s)", msg) |
| 234 |
} |
| 235 |
|
| 236 |
// verify scan |
| 237 |
var s Scanner |
| 238 |
s.Init(fset.AddFile("", fset.Base(), len(source)), source, eh, ScanComments|dontInsertSemis) |
| 239 |
|
| 240 |
// set up expected position |
| 241 |
epos := token.Position{ |
| 242 |
Filename: "", |
| 243 |
Offset: 0, |
| 244 |
Line: 1, |
| 245 |
Column: 1, |
| 246 |
} |
| 247 |
|
| 248 |
index := 0 |
| 249 |
for { |
| 250 |
pos, tok, lit := s.Scan() |
| 251 |
|
| 252 |
// check position |
| 253 |
if tok == token.EOF { |
| 254 |
// correction for EOF |
| 255 |
epos.Line = newlineCount(string(source)) |
| 256 |
epos.Column = 2 |
| 257 |
} |
| 258 |
checkPos(t, lit, pos, epos) |
| 259 |
|
| 260 |
// check token |
| 261 |
e := elt{token.EOF, "", special} |
| 262 |
if index < len(tokens) { |
| 263 |
e = tokens[index] |
| 264 |
index++ |
| 265 |
} |
| 266 |
if tok != e.tok { |
| 267 |
t.Errorf("bad token for %q: got %s, expected %s", lit, tok, e.tok) |
| 268 |
} |
| 269 |
|
| 270 |
// check token class |
| 271 |
if tokenclass(tok) != e.class { |
| 272 |
t.Errorf("bad class for %q: got %d, expected %d", lit, tokenclass(tok), e.class) |
| 273 |
} |
| 274 |
|
| 275 |
// check literal |
| 276 |
elit := "" |
| 277 |
switch e.tok { |
| 278 |
case token.COMMENT: |
| 279 |
// no CRs in comments |
| 280 |
elit = string(stripCR([]byte(e.lit), e.lit[1] == '*')) |
| 281 |
//-style comment literal doesn't contain newline |
| 282 |
if elit[1] == '/' { |
| 283 |
elit = elit[0 : len(elit)-1] |
| 284 |
} |
| 285 |
case token.IDENT: |
| 286 |
elit = e.lit |
| 287 |
case token.SEMICOLON: |
| 288 |
elit = ";" |
| 289 |
default: |
| 290 |
if e.tok.IsLiteral() { |
| 291 |
// no CRs in raw string literals |
| 292 |
elit = e.lit |
| 293 |
if elit[0] == '`' { |
| 294 |
elit = string(stripCR([]byte(elit), false)) |
| 295 |
} |
| 296 |
} else if e.tok.IsKeyword() { |
| 297 |
elit = e.lit |
| 298 |
} |
| 299 |
} |
| 300 |
if lit != elit { |
| 301 |
t.Errorf("bad literal for %q: got %q, expected %q", lit, lit, elit) |
| 302 |
} |
| 303 |
|
| 304 |
if tok == token.EOF { |
| 305 |
break |
| 306 |
} |
| 307 |
|
| 308 |
// update position |
| 309 |
epos.Offset += len(e.lit) + len(whitespace) |
| 310 |
epos.Line += newlineCount(e.lit) + whitespace_linecount |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
if s.ErrorCount != 0 { |
| 315 |
t.Errorf("found %d errors", s.ErrorCount) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
func TestStripCR(t *testing.T) { |
| 320 |
for _, test := range []struct{ have, want string }{ |
| 321 |
{"//\n", "//\n"}, |
| 322 |
{"//\r\n", "//\n"}, |
| 323 |
{"//\r\r\r\n", "//\n"}, |
| 324 |
{"//\r*\r/\r\n", "//*/\n"}, |
| 325 |
{"/**/", "/**/"}, |
| 326 |
{"/*\r/*/", "/*/*/"}, |
| 327 |
{"/*\r*/", "/**/"}, |
| 328 |
{"/**\r/*/", "/**\r/*/"}, |
| 329 |
{"/*\r/\r*\r/*/", "/*/*\r/*/"}, |
| 330 |
{"/*\r\r\r\r*/", "/**/"}, |
| 331 |
} { |
| 332 |
got := string(stripCR([]byte(test.have), len(test.have) >= 2 && test.have[1] == '*')) |
| 333 |
if got != test.want { |
| 334 |
t.Errorf("stripCR(%q) = %q; want %q", test.have, got, test.want) |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
func checkSemi(t *testing.T, input, want string, mode Mode) { |
| 340 |
if mode&ScanComments == 0 { |
| 341 |
want = strings.ReplaceAll(want, "COMMENT ", "") |
| 342 |
want = strings.ReplaceAll(want, " COMMENT", "") // if at end |
| 343 |
want = strings.ReplaceAll(want, "COMMENT", "") // if sole token |
| 344 |
} |
| 345 |
|
| 346 |
file := fset.AddFile("TestSemis", fset.Base(), len(input)) |
| 347 |
var scan Scanner |
| 348 |
scan.Init(file, []byte(input), nil, mode) |
| 349 |
var tokens []string |
| 350 |
for { |
| 351 |
pos, tok, lit := scan.Scan() |
| 352 |
if tok == token.EOF { |
| 353 |
break |
| 354 |
} |
| 355 |
if tok == token.SEMICOLON && lit != ";" { |
| 356 |
// Artificial semicolon: |
| 357 |
// assert that position is EOF or that of a newline. |
| 358 |
off := file.Offset(pos) |
| 359 |
if off != len(input) && input[off] != '\n' { |
| 360 |
t.Errorf("scanning <<%s>>, got SEMICOLON at offset %d, want newline or EOF", input, off) |
| 361 |
} |
| 362 |
} |
| 363 |
lit = tok.String() // "\n" => ";" |
| 364 |
tokens = append(tokens, lit) |
| 365 |
} |
| 366 |
if got := strings.Join(tokens, " "); got != want { |
| 367 |
t.Errorf("scanning <<%s>>, got [%s], want [%s]", input, got, want) |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
var semicolonTests = [...]struct{ input, want string }{ |
| 372 |
{"", ""}, |
| 373 |
{"\ufeff;", ";"}, // first BOM is ignored |
| 374 |
{";", ";"}, |
| 375 |
{"foo\n", "IDENT ;"}, |
| 376 |
{"123\n", "INT ;"}, |
| 377 |
{"1.2\n", "FLOAT ;"}, |
| 378 |
{"'x'\n", "CHAR ;"}, |
| 379 |
{`"x"` + "\n", "STRING ;"}, |
| 380 |
{"`x`\n", "STRING ;"}, |
| 381 |
|
| 382 |
{"+\n", "+"}, |
| 383 |
{"-\n", "-"}, |
| 384 |
{"*\n", "*"}, |
| 385 |
{"/\n", "/"}, |
| 386 |
{"%\n", "%"}, |
| 387 |
|
| 388 |
{"&\n", "&"}, |
| 389 |
{"|\n", "|"}, |
| 390 |
{"^\n", "^"}, |
| 391 |
{"<<\n", "<<"}, |
| 392 |
{">>\n", ">>"}, |
| 393 |
{"&^\n", "&^"}, |
| 394 |
|
| 395 |
{"+=\n", "+="}, |
| 396 |
{"-=\n", "-="}, |
| 397 |
{"*=\n", "*="}, |
| 398 |
{"/=\n", "/="}, |
| 399 |
{"%=\n", "%="}, |
| 400 |
|
| 401 |
{"&=\n", "&="}, |
| 402 |
{"|=\n", "|="}, |
| 403 |
{"^=\n", "^="}, |
| 404 |
{"<<=\n", "<<="}, |
| 405 |
{">>=\n", ">>="}, |
| 406 |
{"&^=\n", "&^="}, |
| 407 |
|
| 408 |
{"&&\n", "&&"}, |
| 409 |
{"||\n", "||"}, |
| 410 |
{"<-\n", "<-"}, |
| 411 |
{"++\n", "++ ;"}, |
| 412 |
{"--\n", "-- ;"}, |
| 413 |
|
| 414 |
{"==\n", "=="}, |
| 415 |
{"<\n", "<"}, |
| 416 |
{">\n", ">"}, |
| 417 |
{"=\n", "="}, |
| 418 |
{"!\n", "!"}, |
| 419 |
|
| 420 |
{"!=\n", "!="}, |
| 421 |
{"<=\n", "<="}, |
| 422 |
{">=\n", ">="}, |
| 423 |
{":=\n", ":="}, |
| 424 |
{"...\n", "..."}, |
| 425 |
|
| 426 |
{"(\n", "("}, |
| 427 |
{"[\n", "["}, |
| 428 |
{"{\n", "{"}, |
| 429 |
{",\n", ","}, |
| 430 |
{".\n", "."}, |
| 431 |
|
| 432 |
{")\n", ") ;"}, |
| 433 |
{"]\n", "] ;"}, |
| 434 |
{"}\n", "} ;"}, |
| 435 |
{";\n", ";"}, |
| 436 |
{":\n", ":"}, |
| 437 |
|
| 438 |
{"break\n", "break ;"}, |
| 439 |
{"case\n", "case"}, |
| 440 |
{"chan\n", "chan"}, |
| 441 |
{"const\n", "const"}, |
| 442 |
{"continue\n", "continue ;"}, |
| 443 |
|
| 444 |
{"default\n", "default"}, |
| 445 |
{"defer\n", "defer"}, |
| 446 |
{"else\n", "else"}, |
| 447 |
{"fallthrough\n", "fallthrough ;"}, |
| 448 |
{"for\n", "for"}, |
| 449 |
|
| 450 |
{"func\n", "func"}, |
| 451 |
{"go\n", "go"}, |
| 452 |
{"goto\n", "goto"}, |
| 453 |
{"if\n", "if"}, |
| 454 |
{"import\n", "import"}, |
| 455 |
|
| 456 |
{"interface\n", "interface"}, |
| 457 |
{"map\n", "map"}, |
| 458 |
{"package\n", "package"}, |
| 459 |
{"range\n", "range"}, |
| 460 |
{"return\n", "return ;"}, |
| 461 |
|
| 462 |
{"select\n", "select"}, |
| 463 |
{"struct\n", "struct"}, |
| 464 |
{"switch\n", "switch"}, |
| 465 |
{"type\n", "type"}, |
| 466 |
{"var\n", "var"}, |
| 467 |
|
| 468 |
{"foo//comment\n", "IDENT COMMENT ;"}, |
| 469 |
{"foo//comment", "IDENT COMMENT ;"}, |
| 470 |
{"foo/*comment*/\n", "IDENT COMMENT ;"}, |
| 471 |
{"foo/*\n*/", "IDENT COMMENT ;"}, |
| 472 |
{"foo/*comment*/ \n", "IDENT COMMENT ;"}, |
| 473 |
{"foo/*\n*/ ", "IDENT COMMENT ;"}, |
| 474 |
|
| 475 |
{"foo // comment\n", "IDENT COMMENT ;"}, |
| 476 |
{"foo // comment", "IDENT COMMENT ;"}, |
| 477 |
{"foo /*comment*/\n", "IDENT COMMENT ;"}, |
| 478 |
{"foo /*\n*/", "IDENT COMMENT ;"}, |
| 479 |
{"foo /* */ /* \n */ bar/**/\n", "IDENT COMMENT COMMENT ; IDENT COMMENT ;"}, |
| 480 |
{"foo /*0*/ /*1*/ /*2*/\n", "IDENT COMMENT COMMENT COMMENT ;"}, |
| 481 |
|
| 482 |
{"foo /*comment*/ \n", "IDENT COMMENT ;"}, |
| 483 |
{"foo /*0*/ /*1*/ /*2*/ \n", "IDENT COMMENT COMMENT COMMENT ;"}, |
| 484 |
{"foo /**/ /*-------------*/ /*----\n*/bar /* \n*/baa\n", "IDENT COMMENT COMMENT COMMENT ; IDENT COMMENT ; IDENT ;"}, |
| 485 |
{"foo /* an EOF terminates a line */", "IDENT COMMENT ;"}, |
| 486 |
{"foo /* an EOF terminates a line */ /*", "IDENT COMMENT COMMENT ;"}, |
| 487 |
{"foo /* an EOF terminates a line */ //", "IDENT COMMENT COMMENT ;"}, |
| 488 |
|
| 489 |
{"package main\n\nfunc main() {\n\tif {\n\t\treturn /* */ }\n}\n", "package IDENT ; func IDENT ( ) { if { return COMMENT } ; } ;"}, |
| 490 |
{"package main", "package IDENT ;"}, |
| 491 |
} |
| 492 |
|
| 493 |
func TestSemicolons(t *testing.T) { |
| 494 |
for _, test := range semicolonTests { |
| 495 |
input, want := test.input, test.want |
| 496 |
checkSemi(t, input, want, 0) |
| 497 |
checkSemi(t, input, want, ScanComments) |
| 498 |
|
| 499 |
// if the input ended in newlines, the input must tokenize the |
| 500 |
// same with or without those newlines |
| 501 |
for i := len(input) - 1; i >= 0 && input[i] == '\n'; i-- { |
| 502 |
checkSemi(t, input[0:i], want, 0) |
| 503 |
checkSemi(t, input[0:i], want, ScanComments) |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
type segment struct { |
| 509 |
srcline string // a line of source text |
| 510 |
filename string // filename for current token; error message for invalid line directives |
| 511 |
line, column int // line and column for current token; error position for invalid line directives |
| 512 |
} |
| 513 |
|
| 514 |
var segments = []segment{ |
| 515 |
// exactly one token per line since the test consumes one token per segment |
| 516 |
{" line1", "TestLineDirectives", 1, 3}, |
| 517 |
{"\nline2", "TestLineDirectives", 2, 1}, |
| 518 |
{"\nline3 //line File1.go:100", "TestLineDirectives", 3, 1}, // bad line comment, ignored |
| 519 |
{"\nline4", "TestLineDirectives", 4, 1}, |
| 520 |
{"\n//line File1.go:100\n line100", "File1.go", 100, 0}, |
| 521 |
{"\n//line \t :42\n line1", " \t ", 42, 0}, |
| 522 |
{"\n//line File2.go:200\n line200", "File2.go", 200, 0}, |
| 523 |
{"\n//line foo\t:42\n line42", "foo\t", 42, 0}, |
| 524 |
{"\n //line foo:42\n line43", "foo\t", 44, 0}, // bad line comment, ignored (use existing, prior filename) |
| 525 |
{"\n//line foo 42\n line44", "foo\t", 46, 0}, // bad line comment, ignored (use existing, prior filename) |
| 526 |
{"\n//line /bar:42\n line45", "/bar", 42, 0}, |
| 527 |
{"\n//line ./foo:42\n line46", "foo", 42, 0}, |
| 528 |
{"\n//line a/b/c/File1.go:100\n line100", "a/b/c/File1.go", 100, 0}, |
| 529 |
{"\n//line c:\\bar:42\n line200", "c:\\bar", 42, 0}, |
| 530 |
{"\n//line c:\\dir\\File1.go:100\n line201", "c:\\dir\\File1.go", 100, 0}, |
| 531 |
|
| 532 |
// tests for new line directive syntax |
| 533 |
{"\n//line :100\na1", "", 100, 0}, // missing filename means empty filename |
| 534 |
{"\n//line bar:100\nb1", "bar", 100, 0}, |
| 535 |
{"\n//line :100:10\nc1", "bar", 100, 10}, // missing filename means current filename |
| 536 |
{"\n//line foo:100:10\nd1", "foo", 100, 10}, |
| 537 |
|
| 538 |
{"\n/*line :100*/a2", "", 100, 0}, // missing filename means empty filename |
| 539 |
{"\n/*line bar:100*/b2", "bar", 100, 0}, |
| 540 |
{"\n/*line :100:10*/c2", "bar", 100, 10}, // missing filename means current filename |
| 541 |
{"\n/*line foo:100:10*/d2", "foo", 100, 10}, |
| 542 |
{"\n/*line foo:100:10*/ e2", "foo", 100, 14}, // line-directive relative column |
| 543 |
{"\n/*line foo:100:10*/\n\nf2", "foo", 102, 1}, // absolute column since on new line |
| 544 |
} |
| 545 |
|
| 546 |
var dirsegments = []segment{ |
| 547 |
// exactly one token per line since the test consumes one token per segment |
| 548 |
{" line1", "TestLineDir/TestLineDirectives", 1, 3}, |
| 549 |
{"\n//line File1.go:100\n line100", "TestLineDir/File1.go", 100, 0}, |
| 550 |
} |
| 551 |
|
| 552 |
var dirUnixSegments = []segment{ |
| 553 |
{"\n//line /bar:42\n line42", "/bar", 42, 0}, |
| 554 |
} |
| 555 |
|
| 556 |
var dirWindowsSegments = []segment{ |
| 557 |
{"\n//line c:\\bar:42\n line42", "c:\\bar", 42, 0}, |
| 558 |
} |
| 559 |
|
| 560 |
// Verify that line directives are interpreted correctly. |
| 561 |
func TestLineDirectives(t *testing.T) { |
| 562 |
testSegments(t, segments, "TestLineDirectives") |
| 563 |
testSegments(t, dirsegments, "TestLineDir/TestLineDirectives") |
| 564 |
if runtime.GOOS == "windows" { |
| 565 |
testSegments(t, dirWindowsSegments, "TestLineDir/TestLineDirectives") |
| 566 |
} else { |
| 567 |
testSegments(t, dirUnixSegments, "TestLineDir/TestLineDirectives") |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
func testSegments(t *testing.T, segments []segment, filename string) { |
| 572 |
var src string |
| 573 |
for _, e := range segments { |
| 574 |
src += e.srcline |
| 575 |
} |
| 576 |
|
| 577 |
// verify scan |
| 578 |
var S Scanner |
| 579 |
file := fset.AddFile(filename, fset.Base(), len(src)) |
| 580 |
S.Init(file, []byte(src), func(pos token.Position, msg string) { t.Error(Error{pos, msg}) }, dontInsertSemis) |
| 581 |
for _, s := range segments { |
| 582 |
p, _, lit := S.Scan() |
| 583 |
pos := file.Position(p) |
| 584 |
checkPos(t, lit, p, token.Position{ |
| 585 |
Filename: s.filename, |
| 586 |
Offset: pos.Offset, |
| 587 |
Line: s.line, |
| 588 |
Column: s.column, |
| 589 |
}) |
| 590 |
} |
| 591 |
|
| 592 |
if S.ErrorCount != 0 { |
| 593 |
t.Errorf("got %d errors", S.ErrorCount) |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
// The filename is used for the error message in these test cases. |
| 598 |
// The first line directive is valid and used to control the expected error line. |
| 599 |
var invalidSegments = []segment{ |
| 600 |
{"\n//line :1:1\n//line foo:42 extra text\ndummy", "invalid line number: 42 extra text", 1, 12}, |
| 601 |
{"\n//line :2:1\n//line foobar:\ndummy", "invalid line number: ", 2, 15}, |
| 602 |
{"\n//line :5:1\n//line :0\ndummy", "invalid line number: 0", 5, 9}, |
| 603 |
{"\n//line :10:1\n//line :1:0\ndummy", "invalid column number: 0", 10, 11}, |
| 604 |
{"\n//line :1:1\n//line :foo:0\ndummy", "invalid line number: 0", 1, 13}, // foo is considered part of the filename |
| 605 |
} |
| 606 |
|
| 607 |
// Verify that invalid line directives get the correct error message. |
| 608 |
func TestInvalidLineDirectives(t *testing.T) { |
| 609 |
// make source |
| 610 |
var src string |
| 611 |
for _, e := range invalidSegments { |
| 612 |
src += e.srcline |
| 613 |
} |
| 614 |
|
| 615 |
// verify scan |
| 616 |
var S Scanner |
| 617 |
var s segment // current segment |
| 618 |
file := fset.AddFile(filepath.Join("dir", "TestInvalidLineDirectives"), fset.Base(), len(src)) |
| 619 |
S.Init(file, []byte(src), func(pos token.Position, msg string) { |
| 620 |
if msg != s.filename { |
| 621 |
t.Errorf("got error %q; want %q", msg, s.filename) |
| 622 |
} |
| 623 |
if pos.Line != s.line || pos.Column != s.column { |
| 624 |
t.Errorf("got position %d:%d; want %d:%d", pos.Line, pos.Column, s.line, s.column) |
| 625 |
} |
| 626 |
}, dontInsertSemis) |
| 627 |
for _, s = range invalidSegments { |
| 628 |
S.Scan() |
| 629 |
} |
| 630 |
|
| 631 |
if S.ErrorCount != len(invalidSegments) { |
| 632 |
t.Errorf("got %d errors; want %d", S.ErrorCount, len(invalidSegments)) |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
// Verify that initializing the same scanner more than once works correctly. |
| 637 |
func TestInit(t *testing.T) { |
| 638 |
var s Scanner |
| 639 |
|
| 640 |
// 1st init |
| 641 |
src1 := "if true { }" |
| 642 |
f1 := fset.AddFile("src1", fset.Base(), len(src1)) |
| 643 |
s.Init(f1, []byte(src1), nil, dontInsertSemis) |
| 644 |
if f1.Size() != len(src1) { |
| 645 |
t.Errorf("bad file size: got %d, expected %d", f1.Size(), len(src1)) |
| 646 |
} |
| 647 |
s.Scan() // if |
| 648 |
s.Scan() // true |
| 649 |
_, tok, _ := s.Scan() // { |
| 650 |
if tok != token.LBRACE { |
| 651 |
t.Errorf("bad token: got %s, expected %s", tok, token.LBRACE) |
| 652 |
} |
| 653 |
|
| 654 |
// 2nd init |
| 655 |
src2 := "go true { ]" |
| 656 |
f2 := fset.AddFile("src2", fset.Base(), len(src2)) |
| 657 |
s.Init(f2, []byte(src2), nil, dontInsertSemis) |
| 658 |
if f2.Size() != len(src2) { |
| 659 |
t.Errorf("bad file size: got %d, expected %d", f2.Size(), len(src2)) |
| 660 |
} |
| 661 |
_, tok, _ = s.Scan() // go |
| 662 |
if tok != token.GO { |
| 663 |
t.Errorf("bad token: got %s, expected %s", tok, token.GO) |
| 664 |
} |
| 665 |
|
| 666 |
if s.ErrorCount != 0 { |
| 667 |
t.Errorf("found %d errors", s.ErrorCount) |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
func TestStdErrorHandler(t *testing.T) { |
| 672 |
const src = "@\n" + // illegal character, cause an error |
| 673 |
"@ @\n" + // two errors on the same line |
| 674 |
"//line File2:20\n" + |
| 675 |
"@\n" + // different file, but same line |
| 676 |
"//line File2:1\n" + |
| 677 |
"@ @\n" + // same file, decreasing line number |
| 678 |
"//line File1:1\n" + |
| 679 |
"@ @ @" // original file, line 1 again |
| 680 |
|
| 681 |
var list ErrorList |
| 682 |
eh := func(pos token.Position, msg string) { list.Add(pos, msg) } |
| 683 |
|
| 684 |
var s Scanner |
| 685 |
s.Init(fset.AddFile("File1", fset.Base(), len(src)), []byte(src), eh, dontInsertSemis) |
| 686 |
for { |
| 687 |
if _, tok, _ := s.Scan(); tok == token.EOF { |
| 688 |
break |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
if len(list) != s.ErrorCount { |
| 693 |
t.Errorf("found %d errors, expected %d", len(list), s.ErrorCount) |
| 694 |
} |
| 695 |
|
| 696 |
if len(list) != 9 { |
| 697 |
t.Errorf("found %d raw errors, expected 9", len(list)) |
| 698 |
PrintError(os.Stderr, list) |
| 699 |
} |
| 700 |
|
| 701 |
list.Sort() |
| 702 |
if len(list) != 9 { |
| 703 |
t.Errorf("found %d sorted errors, expected 9", len(list)) |
| 704 |
PrintError(os.Stderr, list) |
| 705 |
} |
| 706 |
|
| 707 |
list.RemoveMultiples() |
| 708 |
if len(list) != 4 { |
| 709 |
t.Errorf("found %d one-per-line errors, expected 4", len(list)) |
| 710 |
PrintError(os.Stderr, list) |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
type errorCollector struct { |
| 715 |
cnt int // number of errors encountered |
| 716 |
msg string // last error message encountered |
| 717 |
pos token.Position // last error position encountered |
| 718 |
} |
| 719 |
|
| 720 |
func checkError(t *testing.T, src string, tok token.Token, pos int, lit, err string) { |
| 721 |
var s Scanner |
| 722 |
var h errorCollector |
| 723 |
eh := func(pos token.Position, msg string) { |
| 724 |
h.cnt++ |
| 725 |
h.msg = msg |
| 726 |
h.pos = pos |
| 727 |
} |
| 728 |
s.Init(fset.AddFile("", fset.Base(), len(src)), []byte(src), eh, ScanComments|dontInsertSemis) |
| 729 |
_, tok0, lit0 := s.Scan() |
| 730 |
if tok0 != tok { |
| 731 |
t.Errorf("%q: got %s, expected %s", src, tok0, tok) |
| 732 |
} |
| 733 |
if tok0 != token.ILLEGAL && lit0 != lit { |
| 734 |
t.Errorf("%q: got literal %q, expected %q", src, lit0, lit) |
| 735 |
} |
| 736 |
cnt := 0 |
| 737 |
if err != "" { |
| 738 |
cnt = 1 |
| 739 |
} |
| 740 |
if h.cnt != cnt { |
| 741 |
t.Errorf("%q: got cnt %d, expected %d", src, h.cnt, cnt) |
| 742 |
} |
| 743 |
if h.msg != err { |
| 744 |
t.Errorf("%q: got msg %q, expected %q", src, h.msg, err) |
| 745 |
} |
| 746 |
if h.pos.Offset != pos { |
| 747 |
t.Errorf("%q: got offset %d, expected %d", src, h.pos.Offset, pos) |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
var errors = []struct { |
| 752 |
src string |
| 753 |
tok token.Token |
| 754 |
pos int |
| 755 |
lit string |
| 756 |
err string |
| 757 |
}{ |
| 758 |
{"\a", token.ILLEGAL, 0, "", "illegal character U+0007"}, |
| 759 |
{`#`, token.ILLEGAL, 0, "", "illegal character U+0023 '#'"}, |
| 760 |
{`…`, token.ILLEGAL, 0, "", "illegal character U+2026 '…'"}, |
| 761 |
{"..", token.PERIOD, 0, "", ""}, // two periods, not invalid token (issue #28112) |
| 762 |
{`' '`, token.CHAR, 0, `' '`, ""}, |
| 763 |
{`''`, token.CHAR, 0, `''`, "illegal rune literal"}, |
| 764 |
{`'12'`, token.CHAR, 0, `'12'`, "illegal rune literal"}, |
| 765 |
{`'123'`, token.CHAR, 0, `'123'`, "illegal rune literal"}, |
| 766 |
{`'\0'`, token.CHAR, 3, `'\0'`, "illegal character U+0027 ''' in escape sequence"}, |
| 767 |
{`'\07'`, token.CHAR, 4, `'\07'`, "illegal character U+0027 ''' in escape sequence"}, |
| 768 |
{`'\8'`, token.CHAR, 2, `'\8'`, "unknown escape sequence"}, |
| 769 |
{`'\08'`, token.CHAR, 3, `'\08'`, "illegal character U+0038 '8' in escape sequence"}, |
| 770 |
{`'\x'`, token.CHAR, 3, `'\x'`, "illegal character U+0027 ''' in escape sequence"}, |
| 771 |
{`'\x0'`, token.CHAR, 4, `'\x0'`, "illegal character U+0027 ''' in escape sequence"}, |
| 772 |
{`'\x0g'`, token.CHAR, 4, `'\x0g'`, "illegal character U+0067 'g' in escape sequence"}, |
| 773 |
{`'\u'`, token.CHAR, 3, `'\u'`, "illegal character U+0027 ''' in escape sequence"}, |
| 774 |
{`'\u0'`, token.CHAR, 4, `'\u0'`, "illegal character U+0027 ''' in escape sequence"}, |
| 775 |
{`'\u00'`, token.CHAR, 5, `'\u00'`, "illegal character U+0027 ''' in escape sequence"}, |
| 776 |
{`'\u000'`, token.CHAR, 6, `'\u000'`, "illegal character U+0027 ''' in escape sequence"}, |
| 777 |
{`'\u000`, token.CHAR, 6, `'\u000`, "escape sequence not terminated"}, |
| 778 |
{`'\u0000'`, token.CHAR, 0, `'\u0000'`, ""}, |
| 779 |
{`'\U'`, token.CHAR, 3, `'\U'`, "illegal character U+0027 ''' in escape sequence"}, |
| 780 |
{`'\U0'`, token.CHAR, 4, `'\U0'`, "illegal character U+0027 ''' in escape sequence"}, |
| 781 |
{`'\U00'`, token.CHAR, 5, `'\U00'`, "illegal character U+0027 ''' in escape sequence"}, |
| 782 |
{`'\U000'`, token.CHAR, 6, `'\U000'`, "illegal character U+0027 ''' in escape sequence"}, |
| 783 |
{`'\U0000'`, token.CHAR, 7, `'\U0000'`, "illegal character U+0027 ''' in escape sequence"}, |
| 784 |
{`'\U00000'`, token.CHAR, 8, `'\U00000'`, "illegal character U+0027 ''' in escape sequence"}, |
| 785 |
{`'\U000000'`, token.CHAR, 9, `'\U000000'`, "illegal character U+0027 ''' in escape sequence"}, |
| 786 |
{`'\U0000000'`, token.CHAR, 10, `'\U0000000'`, "illegal character U+0027 ''' in escape sequence"}, |
| 787 |
{`'\U0000000`, token.CHAR, 10, `'\U0000000`, "escape sequence not terminated"}, |
| 788 |
{`'\U00000000'`, token.CHAR, 0, `'\U00000000'`, ""}, |
| 789 |
{`'\Uffffffff'`, token.CHAR, 2, `'\Uffffffff'`, "escape sequence is invalid Unicode code point"}, |
| 790 |
{`'`, token.CHAR, 0, `'`, "rune literal not terminated"}, |
| 791 |
{`'\`, token.CHAR, 2, `'\`, "escape sequence not terminated"}, |
| 792 |
{"'\n", token.CHAR, 0, "'", "rune literal not terminated"}, |
| 793 |
{"'\n ", token.CHAR, 0, "'", "rune literal not terminated"}, |
| 794 |
{`""`, token.STRING, 0, `""`, ""}, |
| 795 |
{`"abc`, token.STRING, 0, `"abc`, "string literal not terminated"}, |
| 796 |
{"\"abc\n", token.STRING, 0, `"abc`, "string literal not terminated"}, |
| 797 |
{"\"abc\n ", token.STRING, 0, `"abc`, "string literal not terminated"}, |
| 798 |
{"``", token.STRING, 0, "``", ""}, |
| 799 |
{"`", token.STRING, 0, "`", "raw string literal not terminated"}, |
| 800 |
{"/**/", token.COMMENT, 0, "/**/", ""}, |
| 801 |
{"/*", token.COMMENT, 0, "/*", "comment not terminated"}, |
| 802 |
{"077", token.INT, 0, "077", ""}, |
| 803 |
{"078.", token.FLOAT, 0, "078.", ""}, |
| 804 |
{"07801234567.", token.FLOAT, 0, "07801234567.", ""}, |
| 805 |
{"078e0", token.FLOAT, 0, "078e0", ""}, |
| 806 |
{"0E", token.FLOAT, 2, "0E", "exponent has no digits"}, // issue 17621 |
| 807 |
{"078", token.INT, 2, "078", "invalid digit '8' in octal literal"}, |
| 808 |
{"07090000008", token.INT, 3, "07090000008", "invalid digit '9' in octal literal"}, |
| 809 |
{"0x", token.INT, 2, "0x", "hexadecimal literal has no digits"}, |
| 810 |
{"\"abc\x00def\"", token.STRING, 4, "\"abc\x00def\"", "illegal character NUL"}, |
| 811 |
{"\"abc\x80def\"", token.STRING, 4, "\"abc\x80def\"", "illegal UTF-8 encoding"}, |
| 812 |
{"\ufeff\ufeff", token.ILLEGAL, 3, "\ufeff\ufeff", "illegal byte order mark"}, // only first BOM is ignored |
| 813 |
{"//\ufeff", token.COMMENT, 2, "//\ufeff", "illegal byte order mark"}, // only first BOM is ignored |
| 814 |
{"'\ufeff" + `'`, token.CHAR, 1, "'\ufeff" + `'`, "illegal byte order mark"}, // only first BOM is ignored |
| 815 |
{`"` + "abc\ufeffdef" + `"`, token.STRING, 4, `"` + "abc\ufeffdef" + `"`, "illegal byte order mark"}, // only first BOM is ignored |
| 816 |
{"abc\x00def", token.IDENT, 3, "abc", "illegal character NUL"}, |
| 817 |
{"abc\x00", token.IDENT, 3, "abc", "illegal character NUL"}, |
| 818 |
{"“abc”", token.ILLEGAL, 0, "abc", `curly quotation mark '“' (use neutral '"')`}, |
| 819 |
} |
| 820 |
|
| 821 |
func TestScanErrors(t *testing.T) { |
| 822 |
for _, e := range errors { |
| 823 |
checkError(t, e.src, e.tok, e.pos, e.lit, e.err) |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
func TestUTF16(t *testing.T) { |
| 828 |
// This test doesn't fit within TestScanErrors because |
| 829 |
// the latter assumes that there was only one error. |
| 830 |
for _, src := range []string{ |
| 831 |
"\xfe\xff\x00p\x00a\x00c\x00k\x00a\x00g\x00e\x00 \x00p", // BOM + "package p" encoded as UTF-16 BE |
| 832 |
"\xff\xfep\x00a\x00c\x00k\x00a\x00g\x00e\x00 \x00p\x00", // BOM + "package p" encoded as UTF-16 LE |
| 833 |
} { |
| 834 |
var got []string |
| 835 |
eh := func(posn token.Position, msg string) { |
| 836 |
got = append(got, fmt.Sprintf("#%d: %s", posn.Offset, msg)) |
| 837 |
} |
| 838 |
var sc Scanner |
| 839 |
sc.Init(fset.AddFile("", fset.Base(), len(src)), []byte(src), eh, 0) |
| 840 |
sc.Scan() |
| 841 |
|
| 842 |
// We expect two errors: |
| 843 |
// one from the decoder, one from the scanner. |
| 844 |
want := []string{ |
| 845 |
"#0: illegal UTF-8 encoding (got UTF-16)", |
| 846 |
"#0: illegal character U+FFFD '�'", |
| 847 |
} |
| 848 |
if !slices.Equal(got, want) { |
| 849 |
t.Errorf("Scan(%q) returned errors %q, want %q", src, got, want) |
| 850 |
} |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
// Verify that no comments show up as literal values when skipping comments. |
| 855 |
func TestIssue10213(t *testing.T) { |
| 856 |
const src = ` |
| 857 |
var ( |
| 858 |
A = 1 // foo |
| 859 |
) |
| 860 |
|
| 861 |
var ( |
| 862 |
B = 2 |
| 863 |
// foo |
| 864 |
) |
| 865 |
|
| 866 |
var C = 3 // foo |
| 867 |
|
| 868 |
var D = 4 |
| 869 |
// foo |
| 870 |
|
| 871 |
func anycode() { |
| 872 |
// foo |
| 873 |
} |
| 874 |
` |
| 875 |
var s Scanner |
| 876 |
s.Init(fset.AddFile("", fset.Base(), len(src)), []byte(src), nil, 0) |
| 877 |
for { |
| 878 |
pos, tok, lit := s.Scan() |
| 879 |
class := tokenclass(tok) |
| 880 |
if lit != "" && class != keyword && class != literal && tok != token.SEMICOLON { |
| 881 |
t.Errorf("%s: tok = %s, lit = %q", fset.Position(pos), tok, lit) |
| 882 |
} |
| 883 |
if tok <= token.EOF { |
| 884 |
break |
| 885 |
} |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
func TestIssue28112(t *testing.T) { |
| 890 |
const src = "... .. 0.. .." // make sure to have stand-alone ".." immediately before EOF to test EOF behavior |
| 891 |
tokens := []token.Token{token.ELLIPSIS, token.PERIOD, token.PERIOD, token.FLOAT, token.PERIOD, token.PERIOD, token.PERIOD, token.EOF} |
| 892 |
var s Scanner |
| 893 |
s.Init(fset.AddFile("", fset.Base(), len(src)), []byte(src), nil, 0) |
| 894 |
for _, want := range tokens { |
| 895 |
pos, got, lit := s.Scan() |
| 896 |
if got != want { |
| 897 |
t.Errorf("%s: got %s, want %s", fset.Position(pos), got, want) |
| 898 |
} |
| 899 |
// literals expect to have a (non-empty) literal string and we don't care about other tokens for this test |
| 900 |
if tokenclass(got) == literal && lit == "" { |
| 901 |
t.Errorf("%s: for %s got empty literal string", fset.Position(pos), got) |
| 902 |
} |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
func BenchmarkScan(b *testing.B) { |
| 907 |
b.StopTimer() |
| 908 |
fset := token.NewFileSet() |
| 909 |
file := fset.AddFile("", fset.Base(), len(source)) |
| 910 |
var s Scanner |
| 911 |
b.StartTimer() |
| 912 |
for i := 0; i < b.N; i++ { |
| 913 |
s.Init(file, source, nil, ScanComments) |
| 914 |
for { |
| 915 |
_, tok, _ := s.Scan() |
| 916 |
if tok == token.EOF { |
| 917 |
break |
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
func BenchmarkScanFiles(b *testing.B) { |
| 924 |
// Scan a few arbitrary large files, and one small one, to provide some |
| 925 |
// variety in benchmarks. |
| 926 |
for _, p := range []string{ |
| 927 |
"go/types/expr.go", |
| 928 |
"go/parser/parser.go", |
| 929 |
"net/http/server.go", |
| 930 |
"go/scanner/errors.go", |
| 931 |
} { |
| 932 |
b.Run(p, func(b *testing.B) { |
| 933 |
b.StopTimer() |
| 934 |
filename := filepath.Join("..", "..", filepath.FromSlash(p)) |
| 935 |
src, err := os.ReadFile(filename) |
| 936 |
if err != nil { |
| 937 |
b.Fatal(err) |
| 938 |
} |
| 939 |
fset := token.NewFileSet() |
| 940 |
file := fset.AddFile(filename, fset.Base(), len(src)) |
| 941 |
b.SetBytes(int64(len(src))) |
| 942 |
var s Scanner |
| 943 |
b.StartTimer() |
| 944 |
for i := 0; i < b.N; i++ { |
| 945 |
s.Init(file, src, nil, ScanComments) |
| 946 |
for { |
| 947 |
_, tok, _ := s.Scan() |
| 948 |
if tok == token.EOF { |
| 949 |
break |
| 950 |
} |
| 951 |
} |
| 952 |
} |
| 953 |
}) |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
func TestNumbers(t *testing.T) { |
| 958 |
for _, test := range []struct { |
| 959 |
tok token.Token |
| 960 |
src, tokens, err string |
| 961 |
}{ |
| 962 |
// binaries |
| 963 |
{token.INT, "0b0", "0b0", ""}, |
| 964 |
{token.INT, "0b1010", "0b1010", ""}, |
| 965 |
{token.INT, "0B1110", "0B1110", ""}, |
| 966 |
|
| 967 |
{token.INT, "0b", "0b", "binary literal has no digits"}, |
| 968 |
{token.INT, "0b0190", "0b0190", "invalid digit '9' in binary literal"}, |
| 969 |
{token.INT, "0b01a0", "0b01 a0", ""}, // only accept 0-9 |
| 970 |
|
| 971 |
{token.FLOAT, "0b.", "0b.", "invalid radix point in binary literal"}, |
| 972 |
{token.FLOAT, "0b.1", "0b.1", "invalid radix point in binary literal"}, |
| 973 |
{token.FLOAT, "0b1.0", "0b1.0", "invalid radix point in binary literal"}, |
| 974 |
{token.FLOAT, "0b1e10", "0b1e10", "'e' exponent requires decimal mantissa"}, |
| 975 |
{token.FLOAT, "0b1P-1", "0b1P-1", "'P' exponent requires hexadecimal mantissa"}, |
| 976 |
|
| 977 |
{token.IMAG, "0b10i", "0b10i", ""}, |
| 978 |
{token.IMAG, "0b10.0i", "0b10.0i", "invalid radix point in binary literal"}, |
| 979 |
|
| 980 |
// octals |
| 981 |
{token.INT, "0o0", "0o0", ""}, |
| 982 |
{token.INT, "0o1234", "0o1234", ""}, |
| 983 |
{token.INT, "0O1234", "0O1234", ""}, |
| 984 |
|
| 985 |
{token.INT, "0o", "0o", "octal literal has no digits"}, |
| 986 |
{token.INT, "0o8123", "0o8123", "invalid digit '8' in octal literal"}, |
| 987 |
{token.INT, "0o1293", "0o1293", "invalid digit '9' in octal literal"}, |
| 988 |
{token.INT, "0o12a3", "0o12 a3", ""}, // only accept 0-9 |
| 989 |
|
| 990 |
{token.FLOAT, "0o.", "0o.", "invalid radix point in octal literal"}, |
| 991 |
{token.FLOAT, "0o.2", "0o.2", "invalid radix point in octal literal"}, |
| 992 |
{token.FLOAT, "0o1.2", "0o1.2", "invalid radix point in octal literal"}, |
| 993 |
{token.FLOAT, "0o1E+2", "0o1E+2", "'E' exponent requires decimal mantissa"}, |
| 994 |
{token.FLOAT, "0o1p10", "0o1p10", "'p' exponent requires hexadecimal mantissa"}, |
| 995 |
|
| 996 |
{token.IMAG, "0o10i", "0o10i", ""}, |
| 997 |
{token.IMAG, "0o10e0i", "0o10e0i", "'e' exponent requires decimal mantissa"}, |
| 998 |
|
| 999 |
// 0-octals |
| 1000 |
{token.INT, "0", "0", ""}, |
| 1001 |
{token.INT, "0123", "0123", ""}, |
| 1002 |
|
| 1003 |
{token.INT, "08123", "08123", "invalid digit '8' in octal literal"}, |
| 1004 |
{token.INT, "01293", "01293", "invalid digit '9' in octal literal"}, |
| 1005 |
{token.INT, "0F.", "0 F .", ""}, // only accept 0-9 |
| 1006 |
{token.INT, "0123F.", "0123 F .", ""}, |
| 1007 |
{token.INT, "0123456x", "0123456 x", ""}, |
| 1008 |
|
| 1009 |
// decimals |
| 1010 |
{token.INT, "1", "1", ""}, |
| 1011 |
{token.INT, "1234", "1234", ""}, |
| 1012 |
|
| 1013 |
{token.INT, "1f", "1 f", ""}, // only accept 0-9 |
| 1014 |
|
| 1015 |
{token.IMAG, "0i", "0i", ""}, |
| 1016 |
{token.IMAG, "0678i", "0678i", ""}, |
| 1017 |
|
| 1018 |
// decimal floats |
| 1019 |
{token.FLOAT, "0.", "0.", ""}, |
| 1020 |
{token.FLOAT, "123.", "123.", ""}, |
| 1021 |
{token.FLOAT, "0123.", "0123.", ""}, |
| 1022 |
|
| 1023 |
{token.FLOAT, ".0", ".0", ""}, |
| 1024 |
{token.FLOAT, ".123", ".123", ""}, |
| 1025 |
{token.FLOAT, ".0123", ".0123", ""}, |
| 1026 |
|
| 1027 |
{token.FLOAT, "0.0", "0.0", ""}, |
| 1028 |
{token.FLOAT, "123.123", "123.123", ""}, |
| 1029 |
{token.FLOAT, "0123.0123", "0123.0123", ""}, |
| 1030 |
|
| 1031 |
{token.FLOAT, "0e0", "0e0", ""}, |
| 1032 |
{token.FLOAT, "123e+0", "123e+0", ""}, |
| 1033 |
{token.FLOAT, "0123E-1", "0123E-1", ""}, |
| 1034 |
|
| 1035 |
{token.FLOAT, "0.e+1", "0.e+1", ""}, |
| 1036 |
{token.FLOAT, "123.E-10", "123.E-10", ""}, |
| 1037 |
{token.FLOAT, "0123.e123", "0123.e123", ""}, |
| 1038 |
|
| 1039 |
{token.FLOAT, ".0e-1", ".0e-1", ""}, |
| 1040 |
{token.FLOAT, ".123E+10", ".123E+10", ""}, |
| 1041 |
{token.FLOAT, ".0123E123", ".0123E123", ""}, |
| 1042 |
|
| 1043 |
{token.FLOAT, "0.0e1", "0.0e1", ""}, |
| 1044 |
{token.FLOAT, "123.123E-10", "123.123E-10", ""}, |
| 1045 |
{token.FLOAT, "0123.0123e+456", "0123.0123e+456", ""}, |
| 1046 |
|
| 1047 |
{token.FLOAT, "0e", "0e", "exponent has no digits"}, |
| 1048 |
{token.FLOAT, "0E+", "0E+", "exponent has no digits"}, |
| 1049 |
{token.FLOAT, "1e+f", "1e+ f", "exponent has no digits"}, |
| 1050 |
{token.FLOAT, "0p0", "0p0", "'p' exponent requires hexadecimal mantissa"}, |
| 1051 |
{token.FLOAT, "1.0P-1", "1.0P-1", "'P' exponent requires hexadecimal mantissa"}, |
| 1052 |
|
| 1053 |
{token.IMAG, "0.i", "0.i", ""}, |
| 1054 |
{token.IMAG, ".123i", ".123i", ""}, |
| 1055 |
{token.IMAG, "123.123i", "123.123i", ""}, |
| 1056 |
{token.IMAG, "123e+0i", "123e+0i", ""}, |
| 1057 |
{token.IMAG, "123.E-10i", "123.E-10i", ""}, |
| 1058 |
{token.IMAG, ".123E+10i", ".123E+10i", ""}, |
| 1059 |
|
| 1060 |
// hexadecimals |
| 1061 |
{token.INT, "0x0", "0x0", ""}, |
| 1062 |
{token.INT, "0x1234", "0x1234", ""}, |
| 1063 |
{token.INT, "0xcafef00d", "0xcafef00d", ""}, |
| 1064 |
{token.INT, "0XCAFEF00D", "0XCAFEF00D", ""}, |
| 1065 |
|
| 1066 |
{token.INT, "0x", "0x", "hexadecimal literal has no digits"}, |
| 1067 |
{token.INT, "0x1g", "0x1 g", ""}, |
| 1068 |
|
| 1069 |
{token.IMAG, "0xf00i", "0xf00i", ""}, |
| 1070 |
|
| 1071 |
// hexadecimal floats |
| 1072 |
{token.FLOAT, "0x0p0", "0x0p0", ""}, |
| 1073 |
{token.FLOAT, "0x12efp-123", "0x12efp-123", ""}, |
| 1074 |
{token.FLOAT, "0xABCD.p+0", "0xABCD.p+0", ""}, |
| 1075 |
{token.FLOAT, "0x.0189P-0", "0x.0189P-0", ""}, |
| 1076 |
{token.FLOAT, "0x1.ffffp+1023", "0x1.ffffp+1023", ""}, |
| 1077 |
|
| 1078 |
{token.FLOAT, "0x.", "0x.", "hexadecimal literal has no digits"}, |
| 1079 |
{token.FLOAT, "0x0.", "0x0.", "hexadecimal mantissa requires a 'p' exponent"}, |
| 1080 |
{token.FLOAT, "0x.0", "0x.0", "hexadecimal mantissa requires a 'p' exponent"}, |
| 1081 |
{token.FLOAT, "0x1.1", "0x1.1", "hexadecimal mantissa requires a 'p' exponent"}, |
| 1082 |
{token.FLOAT, "0x1.1e0", "0x1.1e0", "hexadecimal mantissa requires a 'p' exponent"}, |
| 1083 |
{token.FLOAT, "0x1.2gp1a", "0x1.2 gp1a", "hexadecimal mantissa requires a 'p' exponent"}, |
| 1084 |
{token.FLOAT, "0x0p", "0x0p", "exponent has no digits"}, |
| 1085 |
{token.FLOAT, "0xeP-", "0xeP-", "exponent has no digits"}, |
| 1086 |
{token.FLOAT, "0x1234PAB", "0x1234P AB", "exponent has no digits"}, |
| 1087 |
{token.FLOAT, "0x1.2p1a", "0x1.2p1 a", ""}, |
| 1088 |
|
| 1089 |
{token.IMAG, "0xf00.bap+12i", "0xf00.bap+12i", ""}, |
| 1090 |
|
| 1091 |
// separators |
| 1092 |
{token.INT, "0b_1000_0001", "0b_1000_0001", ""}, |
| 1093 |
{token.INT, "0o_600", "0o_600", ""}, |
| 1094 |
{token.INT, "0_466", "0_466", ""}, |
| 1095 |
{token.INT, "1_000", "1_000", ""}, |
| 1096 |
{token.FLOAT, "1_000.000_1", "1_000.000_1", ""}, |
| 1097 |
{token.IMAG, "10e+1_2_3i", "10e+1_2_3i", ""}, |
| 1098 |
{token.INT, "0x_f00d", "0x_f00d", ""}, |
| 1099 |
{token.FLOAT, "0x_f00d.0p1_2", "0x_f00d.0p1_2", ""}, |
| 1100 |
|
| 1101 |
{token.INT, "0b__1000", "0b__1000", "'_' must separate successive digits"}, |
| 1102 |
{token.INT, "0o60___0", "0o60___0", "'_' must separate successive digits"}, |
| 1103 |
{token.INT, "0466_", "0466_", "'_' must separate successive digits"}, |
| 1104 |
{token.FLOAT, "1_.", "1_.", "'_' must separate successive digits"}, |
| 1105 |
{token.FLOAT, "0._1", "0._1", "'_' must separate successive digits"}, |
| 1106 |
{token.FLOAT, "2.7_e0", "2.7_e0", "'_' must separate successive digits"}, |
| 1107 |
{token.IMAG, "10e+12_i", "10e+12_i", "'_' must separate successive digits"}, |
| 1108 |
{token.INT, "0x___0", "0x___0", "'_' must separate successive digits"}, |
| 1109 |
{token.FLOAT, "0x1.0_p0", "0x1.0_p0", "'_' must separate successive digits"}, |
| 1110 |
} { |
| 1111 |
var s Scanner |
| 1112 |
var err string |
| 1113 |
s.Init(fset.AddFile("", fset.Base(), len(test.src)), []byte(test.src), func(_ token.Position, msg string) { |
| 1114 |
if err == "" { |
| 1115 |
err = msg |
| 1116 |
} |
| 1117 |
}, 0) |
| 1118 |
for i, want := range strings.Split(test.tokens, " ") { |
| 1119 |
err = "" |
| 1120 |
_, tok, lit := s.Scan() |
| 1121 |
|
| 1122 |
// compute lit where for tokens where lit is not defined |
| 1123 |
switch tok { |
| 1124 |
case token.PERIOD: |
| 1125 |
lit = "." |
| 1126 |
case token.ADD: |
| 1127 |
lit = "+" |
| 1128 |
case token.SUB: |
| 1129 |
lit = "-" |
| 1130 |
} |
| 1131 |
|
| 1132 |
if i == 0 { |
| 1133 |
if tok != test.tok { |
| 1134 |
t.Errorf("%q: got token %s; want %s", test.src, tok, test.tok) |
| 1135 |
} |
| 1136 |
if err != test.err { |
| 1137 |
t.Errorf("%q: got error %q; want %q", test.src, err, test.err) |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
if lit != want { |
| 1142 |
t.Errorf("%q: got literal %q (%s); want %s", test.src, lit, tok, want) |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
// make sure we read all |
| 1147 |
_, tok, _ := s.Scan() |
| 1148 |
if tok == token.SEMICOLON { |
| 1149 |
_, tok, _ = s.Scan() |
| 1150 |
} |
| 1151 |
if tok != token.EOF { |
| 1152 |
t.Errorf("%q: got %s; want EOF", test.src, tok) |
| 1153 |
} |
| 1154 |
} |
| 1155 |
} |