| 1 |
package parser |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"regexp" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/robertkrimen/otto/ast" |
| 10 |
"github.com/robertkrimen/otto/file" |
| 11 |
"github.com/robertkrimen/otto/underscore" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
func firstErr(err error) error { |
| 16 |
var lerr *ErrorList |
| 17 |
if errors.As(err, &lerr) { |
| 18 |
return (*lerr)[0] |
| 19 |
} |
| 20 |
return err |
| 21 |
} |
| 22 |
|
| 23 |
var matchBeforeAfterSeparator = regexp.MustCompile(`(?m)^[ \t]*---$`) |
| 24 |
|
| 25 |
func testParse(src string) (*parser, *ast.Program, error) { |
| 26 |
return testParseWithMode(src, 0) |
| 27 |
} |
| 28 |
|
| 29 |
func testParseWithMode(src string, mode Mode) (parser *parser, program *ast.Program, err error) { |
| 30 |
defer func() { |
| 31 |
if tmp := recover(); tmp != nil { |
| 32 |
if tmp, ok := tmp.(string); ok { |
| 33 |
if strings.HasPrefix(tmp, "SyntaxError:") { |
| 34 |
parser = nil |
| 35 |
program = nil |
| 36 |
err = errors.New(tmp) |
| 37 |
return |
| 38 |
} |
| 39 |
} |
| 40 |
panic(tmp) |
| 41 |
} |
| 42 |
}() |
| 43 |
parser = newParser("", src, 1, nil) |
| 44 |
parser.mode = mode |
| 45 |
program, err = parser.parse() |
| 46 |
return parser, program, err |
| 47 |
} |
| 48 |
|
| 49 |
func TestParseFile(t *testing.T) { |
| 50 |
tt(t, func() { |
| 51 |
_, err := ParseFile(nil, "", `/abc/`, 0) |
| 52 |
is(err, nil) |
| 53 |
|
| 54 |
_, err = ParseFile(nil, "", `/(?!def)abc/`, IgnoreRegExpErrors) |
| 55 |
is(err, nil) |
| 56 |
|
| 57 |
_, err = ParseFile(nil, "", `/(?!def)abc/`, 0) |
| 58 |
is(err, "(anonymous): Line 1:1 Invalid regular expression: re2: Invalid (?!) <lookahead>") |
| 59 |
|
| 60 |
_, err = ParseFile(nil, "", `/(?!def)abc/; return`, IgnoreRegExpErrors) |
| 61 |
is(err, "(anonymous): Line 1:15 Illegal return statement") |
| 62 |
|
| 63 |
_, err = ParseFile(nil, "/make-sure-file-path-is-returned-not-anonymous", `a..`, 0) |
| 64 |
is(err, "/make-sure-file-path-is-returned-not-anonymous: Line 1:3 Unexpected token .") |
| 65 |
}) |
| 66 |
} |
| 67 |
|
| 68 |
func TestParseFunction(t *testing.T) { |
| 69 |
tt(t, func() { |
| 70 |
test := func(prm, bdy string, expect interface{}) *ast.FunctionLiteral { |
| 71 |
function, err := ParseFunction(prm, bdy) |
| 72 |
is(firstErr(err), expect) |
| 73 |
return function |
| 74 |
} |
| 75 |
|
| 76 |
test("a, b,c,d", "", nil) |
| 77 |
|
| 78 |
test("a, b;,c,d", "", "(anonymous): Line 1:15 Unexpected token ;") |
| 79 |
|
| 80 |
test("this", "", "(anonymous): Line 1:11 Unexpected token this") |
| 81 |
|
| 82 |
test("a, b, c, null", "", "(anonymous): Line 1:20 Unexpected token null") |
| 83 |
|
| 84 |
test("a, b,c,d", "return;", nil) |
| 85 |
|
| 86 |
test("a, b,c,d", "break;", "(anonymous): Line 2:1 Illegal break statement") |
| 87 |
|
| 88 |
test("a, b,c,d", "{}", nil) |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
func TestParserErr(t *testing.T) { |
| 93 |
tt(t, func() { |
| 94 |
test := func(input string, expect interface{}) (*ast.Program, *parser) { |
| 95 |
parser := newParser("", input, 1, nil) |
| 96 |
program, err := parser.parse() |
| 97 |
is(firstErr(err), expect) |
| 98 |
return program, parser |
| 99 |
} |
| 100 |
|
| 101 |
test("", nil) |
| 102 |
|
| 103 |
program, parser := test(` |
| 104 |
var abc; |
| 105 |
break; do { |
| 106 |
} while(true); |
| 107 |
`, "(anonymous): Line 3:9 Illegal break statement") |
| 108 |
{ |
| 109 |
stmt := program.Body[1].(*ast.BadStatement) |
| 110 |
is(parser.position(stmt.From).Column, 9) |
| 111 |
is(parser.position(stmt.To).Column, 16) |
| 112 |
is(parser.slice(stmt.From, stmt.To), "break; ") |
| 113 |
} |
| 114 |
|
| 115 |
test("{", "(anonymous): Line 1:2 Unexpected end of input") |
| 116 |
|
| 117 |
test("}", "(anonymous): Line 1:1 Unexpected token }") |
| 118 |
|
| 119 |
test("3ea", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 120 |
|
| 121 |
test("3in", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 122 |
|
| 123 |
test("3in []", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 124 |
|
| 125 |
test("3e", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 126 |
|
| 127 |
test("3e+", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 128 |
|
| 129 |
test("3e-", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 130 |
|
| 131 |
test("3x", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 132 |
|
| 133 |
test("3x0", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 134 |
|
| 135 |
test("0x", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 136 |
|
| 137 |
test("09", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 138 |
|
| 139 |
test("018", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 140 |
|
| 141 |
test("01.0", "(anonymous): Line 1:3 Unexpected number") |
| 142 |
|
| 143 |
test("01a", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 144 |
|
| 145 |
test("0x3in[]", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 146 |
|
| 147 |
test("\"Hello\nWorld\"", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 148 |
|
| 149 |
test("\u203f = 10", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 150 |
|
| 151 |
test("x\\", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 152 |
|
| 153 |
test("x\\\\", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 154 |
|
| 155 |
test("x\\u005c", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 156 |
|
| 157 |
test("x\\u002a", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 158 |
|
| 159 |
test("x\\\\u002a", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 160 |
|
| 161 |
test("/\n", "(anonymous): Line 1:1 Invalid regular expression: missing /") |
| 162 |
|
| 163 |
test("var x = /(s/g", "(anonymous): Line 1:9 Invalid regular expression: Unterminated group") |
| 164 |
|
| 165 |
test("0 = 1", "(anonymous): Line 1:1 invalid left-hand side in assignment") |
| 166 |
|
| 167 |
test("func() = 1", "(anonymous): Line 1:1 invalid left-hand side in assignment") |
| 168 |
|
| 169 |
test("(1 + 1) = 2", "(anonymous): Line 1:2 invalid left-hand side in assignment") |
| 170 |
|
| 171 |
test("1++", "(anonymous): Line 1:2 invalid left-hand side in assignment") |
| 172 |
|
| 173 |
test("1--", "(anonymous): Line 1:2 invalid left-hand side in assignment") |
| 174 |
|
| 175 |
test("--1", "(anonymous): Line 1:1 invalid left-hand side in assignment") |
| 176 |
|
| 177 |
test("for((1 + 1) in abc) def();", "(anonymous): Line 1:1 Invalid left-hand side in for-in") |
| 178 |
|
| 179 |
test("[", "(anonymous): Line 1:2 Unexpected end of input") |
| 180 |
|
| 181 |
test("[,", "(anonymous): Line 1:3 Unexpected end of input") |
| 182 |
|
| 183 |
test("1 + {", "(anonymous): Line 1:6 Unexpected end of input") |
| 184 |
|
| 185 |
test("1 + { abc:abc", "(anonymous): Line 1:14 Unexpected end of input") |
| 186 |
|
| 187 |
test("1 + { abc:abc,", "(anonymous): Line 1:15 Unexpected end of input") |
| 188 |
|
| 189 |
test("var abc = /\n/", "(anonymous): Line 1:11 Invalid regular expression: missing /") |
| 190 |
|
| 191 |
test("var abc = \"\n", "(anonymous): Line 1:11 Unexpected token ILLEGAL") |
| 192 |
|
| 193 |
test("var if = 0", "(anonymous): Line 1:5 Unexpected token if") |
| 194 |
|
| 195 |
test("abc + 0 = 1", "(anonymous): Line 1:1 invalid left-hand side in assignment") |
| 196 |
|
| 197 |
test("+abc = 1", "(anonymous): Line 1:1 invalid left-hand side in assignment") |
| 198 |
|
| 199 |
test("1 + (", "(anonymous): Line 1:6 Unexpected end of input") |
| 200 |
|
| 201 |
test("\n\n\n{", "(anonymous): Line 4:2 Unexpected end of input") |
| 202 |
|
| 203 |
test("\n/* Some multiline\ncomment */\n)", "(anonymous): Line 4:1 Unexpected token )") |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
test("function abc(if) {}", "(anonymous): Line 1:14 Unexpected token if") |
| 221 |
|
| 222 |
test("function abc(true) {}", "(anonymous): Line 1:14 Unexpected token true") |
| 223 |
|
| 224 |
test("function abc(false) {}", "(anonymous): Line 1:14 Unexpected token false") |
| 225 |
|
| 226 |
test("function abc(null) {}", "(anonymous): Line 1:14 Unexpected token null") |
| 227 |
|
| 228 |
test("function null() {}", "(anonymous): Line 1:10 Unexpected token null") |
| 229 |
|
| 230 |
test("function true() {}", "(anonymous): Line 1:10 Unexpected token true") |
| 231 |
|
| 232 |
test("function false() {}", "(anonymous): Line 1:10 Unexpected token false") |
| 233 |
|
| 234 |
test("function if() {}", "(anonymous): Line 1:10 Unexpected token if") |
| 235 |
|
| 236 |
test("a b;", "(anonymous): Line 1:3 Unexpected identifier") |
| 237 |
|
| 238 |
test("if.a", "(anonymous): Line 1:3 Unexpected token .") |
| 239 |
|
| 240 |
test("a if", "(anonymous): Line 1:3 Unexpected token if") |
| 241 |
|
| 242 |
test("a class", "(anonymous): Line 1:3 Unexpected reserved word") |
| 243 |
|
| 244 |
test("break\n", "(anonymous): Line 1:1 Illegal break statement") |
| 245 |
|
| 246 |
test("break 1;", "(anonymous): Line 1:7 Unexpected number") |
| 247 |
|
| 248 |
test("for (;;) { break 1; }", "(anonymous): Line 1:18 Unexpected number") |
| 249 |
|
| 250 |
test("continue\n", "(anonymous): Line 1:1 Illegal continue statement") |
| 251 |
|
| 252 |
test("continue 1;", "(anonymous): Line 1:10 Unexpected number") |
| 253 |
|
| 254 |
test("for (;;) { continue 1; }", "(anonymous): Line 1:21 Unexpected number") |
| 255 |
|
| 256 |
test("throw", "(anonymous): Line 1:1 Unexpected end of input") |
| 257 |
|
| 258 |
test("throw;", "(anonymous): Line 1:6 Unexpected token ;") |
| 259 |
|
| 260 |
test("throw \n", "(anonymous): Line 1:1 Unexpected end of input") |
| 261 |
|
| 262 |
test("for (var abc, def in {});", "(anonymous): Line 1:19 Unexpected token in") |
| 263 |
|
| 264 |
test("for ((abc in {});;);", nil) |
| 265 |
|
| 266 |
test("for ((abc in {}));", "(anonymous): Line 1:17 Unexpected token )") |
| 267 |
|
| 268 |
test("for (+abc in {});", "(anonymous): Line 1:1 Invalid left-hand side in for-in") |
| 269 |
|
| 270 |
test("if (false)", "(anonymous): Line 1:11 Unexpected end of input") |
| 271 |
|
| 272 |
test("if (false) abc(); else", "(anonymous): Line 1:23 Unexpected end of input") |
| 273 |
|
| 274 |
test("do", "(anonymous): Line 1:3 Unexpected end of input") |
| 275 |
|
| 276 |
test("while (false)", "(anonymous): Line 1:14 Unexpected end of input") |
| 277 |
|
| 278 |
test("for (;;)", "(anonymous): Line 1:9 Unexpected end of input") |
| 279 |
|
| 280 |
test("with (abc)", "(anonymous): Line 1:11 Unexpected end of input") |
| 281 |
|
| 282 |
test("try {}", "(anonymous): Line 1:1 Missing catch or finally after try") |
| 283 |
|
| 284 |
test("try {} catch {}", "(anonymous): Line 1:14 Unexpected token {") |
| 285 |
|
| 286 |
test("try {} catch () {}", "(anonymous): Line 1:15 Unexpected token )") |
| 287 |
|
| 288 |
test("\u203f = 1", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
test(`new abc()."def"`, "(anonymous): Line 1:11 Unexpected string") |
| 298 |
|
| 299 |
test("/*", "(anonymous): Line 1:3 Unexpected end of input") |
| 300 |
|
| 301 |
test("/**", "(anonymous): Line 1:4 Unexpected end of input") |
| 302 |
|
| 303 |
test("/*\n\n\n", "(anonymous): Line 4:1 Unexpected end of input") |
| 304 |
|
| 305 |
test("/*\n\n\n*", "(anonymous): Line 4:2 Unexpected end of input") |
| 306 |
|
| 307 |
test("/*abc", "(anonymous): Line 1:6 Unexpected end of input") |
| 308 |
|
| 309 |
test("/*abc *", "(anonymous): Line 1:9 Unexpected end of input") |
| 310 |
|
| 311 |
test("\n]", "(anonymous): Line 2:1 Unexpected token ]") |
| 312 |
|
| 313 |
test("\r\n]", "(anonymous): Line 2:1 Unexpected token ]") |
| 314 |
|
| 315 |
test("\n\r]", "(anonymous): Line 3:1 Unexpected token ]") |
| 316 |
|
| 317 |
test("//\r\n]", "(anonymous): Line 2:1 Unexpected token ]") |
| 318 |
|
| 319 |
test("//\n\r]", "(anonymous): Line 3:1 Unexpected token ]") |
| 320 |
|
| 321 |
test("/abc\\\n/", "(anonymous): Line 1:1 Invalid regular expression: missing /") |
| 322 |
|
| 323 |
test("//\r \n]", "(anonymous): Line 3:1 Unexpected token ]") |
| 324 |
|
| 325 |
test("/*\r\n*/]", "(anonymous): Line 2:3 Unexpected token ]") |
| 326 |
|
| 327 |
test("/*\r \n*/]", "(anonymous): Line 3:3 Unexpected token ]") |
| 328 |
|
| 329 |
test("\\\\", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 330 |
|
| 331 |
test("\\u005c", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 332 |
|
| 333 |
test("\\abc", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 334 |
|
| 335 |
test("\\u0000", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 336 |
|
| 337 |
test("\\u200c = []", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 338 |
|
| 339 |
test("\\u200D = []", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 340 |
|
| 341 |
test(`"\`, "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 342 |
|
| 343 |
test(`"\u`, "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 344 |
|
| 345 |
test("return", "(anonymous): Line 1:1 Illegal return statement") |
| 346 |
|
| 347 |
test("continue", "(anonymous): Line 1:1 Illegal continue statement") |
| 348 |
|
| 349 |
test("break", "(anonymous): Line 1:1 Illegal break statement") |
| 350 |
|
| 351 |
test("switch (abc) { default: continue; }", "(anonymous): Line 1:25 Illegal continue statement") |
| 352 |
|
| 353 |
test("do { abc } *", "(anonymous): Line 1:12 Unexpected token *") |
| 354 |
|
| 355 |
test("while (true) { break abc; }", "(anonymous): Line 1:16 Undefined label 'abc'") |
| 356 |
|
| 357 |
test("while (true) { continue abc; }", "(anonymous): Line 1:16 Undefined label 'abc'") |
| 358 |
|
| 359 |
test("abc: while (true) { (function(){ break abc; }); }", "(anonymous): Line 1:34 Undefined label 'abc'") |
| 360 |
|
| 361 |
test("abc: while (true) { (function(){ abc: break abc; }); }", nil) |
| 362 |
|
| 363 |
test("abc: while (true) { (function(){ continue abc; }); }", "(anonymous): Line 1:34 Undefined label 'abc'") |
| 364 |
|
| 365 |
test(`abc: if (0) break abc; else {}`, nil) |
| 366 |
|
| 367 |
test(`abc: if (0) { break abc; } else {}`, nil) |
| 368 |
|
| 369 |
test(`abc: if (0) { break abc } else {}`, nil) |
| 370 |
|
| 371 |
test("abc: while (true) { abc: while (true) {} }", "(anonymous): Line 1:21 Label 'abc' already exists") |
| 372 |
|
| 373 |
if false { |
| 374 |
|
| 375 |
test("(function () { 'use strict'; delete abc; }())", "") |
| 376 |
} |
| 377 |
|
| 378 |
test("_: _: while (true) {]", "(anonymous): Line 1:4 Label '_' already exists") |
| 379 |
|
| 380 |
test("_:\n_:\nwhile (true) {]", "(anonymous): Line 2:1 Label '_' already exists") |
| 381 |
|
| 382 |
test("_:\n _:\nwhile (true) {]", "(anonymous): Line 2:4 Label '_' already exists") |
| 383 |
|
| 384 |
test("/Xyzzy(?!Nothing happens)/", |
| 385 |
"(anonymous): Line 1:1 Invalid regular expression: re2: Invalid (?!) <lookahead>") |
| 386 |
|
| 387 |
test("function(){}", "(anonymous): Line 1:9 Unexpected token (") |
| 388 |
|
| 389 |
test("\n/*/", "(anonymous): Line 2:4 Unexpected end of input") |
| 390 |
|
| 391 |
test("/*/.source", "(anonymous): Line 1:11 Unexpected end of input") |
| 392 |
|
| 393 |
test("/\\1/.source", "(anonymous): Line 1:1 Invalid regular expression: re2: Invalid \\1 <backreference>") |
| 394 |
|
| 395 |
test("var class", "(anonymous): Line 1:5 Unexpected reserved word") |
| 396 |
|
| 397 |
test("var if", "(anonymous): Line 1:5 Unexpected token if") |
| 398 |
|
| 399 |
test("object Object", "(anonymous): Line 1:8 Unexpected identifier") |
| 400 |
|
| 401 |
test("[object Object]", "(anonymous): Line 1:9 Unexpected identifier") |
| 402 |
|
| 403 |
test("\\u0xyz", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 404 |
|
| 405 |
test(`for (var abc, def in {}) {}`, "(anonymous): Line 1:19 Unexpected token in") |
| 406 |
|
| 407 |
test(`for (abc, def in {}) {}`, "(anonymous): Line 1:1 Invalid left-hand side in for-in") |
| 408 |
|
| 409 |
test(`for (var abc=def, ghi=("abc" in {}); true;) {}`, nil) |
| 410 |
|
| 411 |
{ |
| 412 |
|
| 413 |
|
| 414 |
test("this\nif (1);", nil) |
| 415 |
|
| 416 |
test("while (1) { break\nif (1); }", nil) |
| 417 |
|
| 418 |
test("throw\nif (1);", "(anonymous): Line 1:1 Illegal newline after throw") |
| 419 |
|
| 420 |
test("(function(){ return\nif (1); })", nil) |
| 421 |
|
| 422 |
test("while (1) { continue\nif (1); }", nil) |
| 423 |
|
| 424 |
test("debugger\nif (1);", nil) |
| 425 |
} |
| 426 |
|
| 427 |
{ |
| 428 |
test("class", "(anonymous): Line 1:1 Unexpected reserved word") |
| 429 |
test("abc.class = 1", nil) |
| 430 |
test("var class;", "(anonymous): Line 1:5 Unexpected reserved word") |
| 431 |
|
| 432 |
test("const", "(anonymous): Line 1:1 Unexpected reserved word") |
| 433 |
test("abc.const = 1", nil) |
| 434 |
test("var const;", "(anonymous): Line 1:5 Unexpected reserved word") |
| 435 |
|
| 436 |
test("enum", "(anonymous): Line 1:1 Unexpected reserved word") |
| 437 |
test("abc.enum = 1", nil) |
| 438 |
test("var enum;", "(anonymous): Line 1:5 Unexpected reserved word") |
| 439 |
|
| 440 |
test("export", "(anonymous): Line 1:1 Unexpected reserved word") |
| 441 |
test("abc.export = 1", nil) |
| 442 |
test("var export;", "(anonymous): Line 1:5 Unexpected reserved word") |
| 443 |
|
| 444 |
test("extends", "(anonymous): Line 1:1 Unexpected reserved word") |
| 445 |
test("abc.extends = 1", nil) |
| 446 |
test("var extends;", "(anonymous): Line 1:5 Unexpected reserved word") |
| 447 |
|
| 448 |
test("import", "(anonymous): Line 1:1 Unexpected reserved word") |
| 449 |
test("abc.import = 1", nil) |
| 450 |
test("var import;", "(anonymous): Line 1:5 Unexpected reserved word") |
| 451 |
|
| 452 |
test("super", "(anonymous): Line 1:1 Unexpected reserved word") |
| 453 |
test("abc.super = 1", nil) |
| 454 |
test("var super;", "(anonymous): Line 1:5 Unexpected reserved word") |
| 455 |
} |
| 456 |
|
| 457 |
{ |
| 458 |
test(`implements`, nil) |
| 459 |
test(`abc.implements = 1`, nil) |
| 460 |
test(`var implements;`, nil) |
| 461 |
|
| 462 |
test(`interface`, nil) |
| 463 |
test(`abc.interface = 1`, nil) |
| 464 |
test(`var interface;`, nil) |
| 465 |
|
| 466 |
test(`let`, nil) |
| 467 |
test(`abc.let = 1`, nil) |
| 468 |
test(`var let;`, nil) |
| 469 |
|
| 470 |
test(`package`, nil) |
| 471 |
test(`abc.package = 1`, nil) |
| 472 |
test(`var package;`, nil) |
| 473 |
|
| 474 |
test(`private`, nil) |
| 475 |
test(`abc.private = 1`, nil) |
| 476 |
test(`var private;`, nil) |
| 477 |
|
| 478 |
test(`protected`, nil) |
| 479 |
test(`abc.protected = 1`, nil) |
| 480 |
test(`var protected;`, nil) |
| 481 |
|
| 482 |
test(`public`, nil) |
| 483 |
test(`abc.public = 1`, nil) |
| 484 |
test(`var public;`, nil) |
| 485 |
|
| 486 |
test(`static`, nil) |
| 487 |
test(`abc.static = 1`, nil) |
| 488 |
test(`var static;`, nil) |
| 489 |
|
| 490 |
test(`yield`, nil) |
| 491 |
test(`abc.yield = 1`, nil) |
| 492 |
test(`var yield;`, nil) |
| 493 |
} |
| 494 |
}) |
| 495 |
} |
| 496 |
|
| 497 |
func TestParser(t *testing.T) { |
| 498 |
tt(t, func() { |
| 499 |
test := func(source string, chk interface{}) *ast.Program { |
| 500 |
_, program, err := testParse(source) |
| 501 |
is(firstErr(err), chk) |
| 502 |
return program |
| 503 |
} |
| 504 |
|
| 505 |
test(` |
| 506 |
abc |
| 507 |
-- |
| 508 |
[] |
| 509 |
`, "(anonymous): Line 3:13 invalid left-hand side in assignment") |
| 510 |
|
| 511 |
test(` |
| 512 |
abc-- |
| 513 |
[] |
| 514 |
`, nil) |
| 515 |
|
| 516 |
test("1\n[]\n", "(anonymous): Line 2:2 Unexpected token ]") |
| 517 |
|
| 518 |
test(` |
| 519 |
function abc() { |
| 520 |
} |
| 521 |
abc() |
| 522 |
`, nil) |
| 523 |
|
| 524 |
test("", nil) |
| 525 |
|
| 526 |
test("//", nil) |
| 527 |
|
| 528 |
test("/* */", nil) |
| 529 |
|
| 530 |
test("/** **/", nil) |
| 531 |
|
| 532 |
test("/*****/", nil) |
| 533 |
|
| 534 |
test("/*", "(anonymous): Line 1:3 Unexpected end of input") |
| 535 |
|
| 536 |
test("#", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 537 |
|
| 538 |
test("/**/#", "(anonymous): Line 1:5 Unexpected token ILLEGAL") |
| 539 |
|
| 540 |
test("new +", "(anonymous): Line 1:5 Unexpected token +") |
| 541 |
|
| 542 |
program := test(";", nil) |
| 543 |
is(len(program.Body), 1) |
| 544 |
is(program.Body[0].(*ast.EmptyStatement).Semicolon, file.Idx(1)) |
| 545 |
|
| 546 |
program = test(";;", nil) |
| 547 |
is(len(program.Body), 2) |
| 548 |
is(program.Body[0].(*ast.EmptyStatement).Semicolon, file.Idx(1)) |
| 549 |
is(program.Body[1].(*ast.EmptyStatement).Semicolon, file.Idx(2)) |
| 550 |
|
| 551 |
program = test("1.2", nil) |
| 552 |
is(len(program.Body), 1) |
| 553 |
is(program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.NumberLiteral).Literal, "1.2") |
| 554 |
|
| 555 |
program = test("/* */1.2", nil) |
| 556 |
is(len(program.Body), 1) |
| 557 |
is(program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.NumberLiteral).Literal, "1.2") |
| 558 |
|
| 559 |
program = test("\n", nil) |
| 560 |
is(len(program.Body), 0) |
| 561 |
|
| 562 |
test(` |
| 563 |
if (0) { |
| 564 |
abc = 0 |
| 565 |
} |
| 566 |
else abc = 0 |
| 567 |
`, nil) |
| 568 |
|
| 569 |
test("if (0) abc = 0 else abc = 0", "(anonymous): Line 1:16 Unexpected token else") |
| 570 |
|
| 571 |
test(` |
| 572 |
if (0) { |
| 573 |
abc = 0 |
| 574 |
} else abc = 0 |
| 575 |
`, nil) |
| 576 |
|
| 577 |
test(` |
| 578 |
if (0) { |
| 579 |
abc = 1 |
| 580 |
} else { |
| 581 |
} |
| 582 |
`, nil) |
| 583 |
|
| 584 |
test(` |
| 585 |
do { |
| 586 |
} while (true) |
| 587 |
`, nil) |
| 588 |
|
| 589 |
test(` |
| 590 |
try { |
| 591 |
} finally { |
| 592 |
} |
| 593 |
`, nil) |
| 594 |
|
| 595 |
test(` |
| 596 |
try { |
| 597 |
} catch (abc) { |
| 598 |
} finally { |
| 599 |
} |
| 600 |
`, nil) |
| 601 |
|
| 602 |
test(` |
| 603 |
try { |
| 604 |
} |
| 605 |
catch (abc) { |
| 606 |
} |
| 607 |
finally { |
| 608 |
} |
| 609 |
`, nil) |
| 610 |
|
| 611 |
test(`try {} catch (abc) {} finally {}`, nil) |
| 612 |
|
| 613 |
test(` |
| 614 |
do { |
| 615 |
do { |
| 616 |
} while (0) |
| 617 |
} while (0) |
| 618 |
`, nil) |
| 619 |
|
| 620 |
test(`do do; while(0); while(0);`, nil) |
| 621 |
|
| 622 |
test(` |
| 623 |
(function(){ |
| 624 |
try { |
| 625 |
if ( |
| 626 |
1 |
| 627 |
) { |
| 628 |
return 1 |
| 629 |
} |
| 630 |
return 0 |
| 631 |
} finally { |
| 632 |
} |
| 633 |
})() |
| 634 |
`, nil) |
| 635 |
|
| 636 |
test("abc = ''\ndef", nil) |
| 637 |
|
| 638 |
test("abc = 1\ndef", nil) |
| 639 |
|
| 640 |
test("abc = Math\ndef", nil) |
| 641 |
|
| 642 |
test(`"\'"`, nil) |
| 643 |
|
| 644 |
test(` |
| 645 |
abc = function(){ |
| 646 |
} |
| 647 |
abc = 0 |
| 648 |
`, nil) |
| 649 |
|
| 650 |
test("abc.null = 0", nil) |
| 651 |
|
| 652 |
test("0x41", nil) |
| 653 |
|
| 654 |
test(`"\d"`, nil) |
| 655 |
|
| 656 |
test(`(function(){return this})`, nil) |
| 657 |
|
| 658 |
test(` |
| 659 |
Object.defineProperty(Array.prototype, "0", { |
| 660 |
value: 100, |
| 661 |
writable: false, |
| 662 |
configurable: true |
| 663 |
}); |
| 664 |
abc = [101]; |
| 665 |
abc.hasOwnProperty("0") && abc[0] === 101; |
| 666 |
`, nil) |
| 667 |
|
| 668 |
test(`new abc()`, nil) |
| 669 |
test(`new {}`, nil) |
| 670 |
|
| 671 |
test(` |
| 672 |
limit = 4 |
| 673 |
result = 0 |
| 674 |
while (limit) { |
| 675 |
limit = limit - 1 |
| 676 |
if (limit) { |
| 677 |
} |
| 678 |
else { |
| 679 |
break |
| 680 |
} |
| 681 |
result = result + 1 |
| 682 |
} |
| 683 |
`, nil) |
| 684 |
|
| 685 |
test(` |
| 686 |
while (0) { |
| 687 |
if (0) { |
| 688 |
continue |
| 689 |
} |
| 690 |
} |
| 691 |
`, nil) |
| 692 |
|
| 693 |
test("var \u0061\u0062\u0063 = 0", nil) |
| 694 |
|
| 695 |
|
| 696 |
test("var test7_3_1\nabc = 66;", nil) |
| 697 |
test("var test7_3_1\u2028abc = 66;", nil) |
| 698 |
|
| 699 |
|
| 700 |
test("//\u2028 =;", "(anonymous): Line 2:2 Unexpected token =") |
| 701 |
|
| 702 |
|
| 703 |
test("var abc = \u2029;", "(anonymous): Line 2:1 Unexpected token ;") |
| 704 |
test("var abc = \\u2029;", "(anonymous): Line 1:11 Unexpected token ILLEGAL") |
| 705 |
test("var \\u0061\\u0062\\u0063 = 0;", nil) |
| 706 |
|
| 707 |
test("'", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 708 |
|
| 709 |
test("'\nstr\ning\n'", "(anonymous): Line 1:1 Unexpected token ILLEGAL") |
| 710 |
|
| 711 |
|
| 712 |
test(`var $\u0030 = 0;`, nil) |
| 713 |
|
| 714 |
|
| 715 |
test(`switch = 1`, "(anonymous): Line 1:8 Unexpected token =") |
| 716 |
|
| 717 |
|
| 718 |
test(`.0 === 0.0`, nil) |
| 719 |
|
| 720 |
|
| 721 |
test("var regExp = /\\\rn/;", "(anonymous): Line 1:14 Invalid regular expression: missing /") |
| 722 |
|
| 723 |
|
| 724 |
test("var regExp = /=/;", nil) |
| 725 |
|
| 726 |
|
| 727 |
test("/*/", "(anonymous): Line 1:4 Unexpected end of input") |
| 728 |
|
| 729 |
|
| 730 |
test(` |
| 731 |
do { |
| 732 |
; |
| 733 |
} while (false) true |
| 734 |
`, nil) |
| 735 |
|
| 736 |
|
| 737 |
test(` |
| 738 |
{a:1 |
| 739 |
} 3 |
| 740 |
`, nil) |
| 741 |
|
| 742 |
test(` |
| 743 |
abc |
| 744 |
++def |
| 745 |
`, nil) |
| 746 |
|
| 747 |
|
| 748 |
test(` |
| 749 |
for(false;false |
| 750 |
) { |
| 751 |
break; |
| 752 |
} |
| 753 |
`, "(anonymous): Line 3:13 Unexpected token )") |
| 754 |
|
| 755 |
|
| 756 |
test(` |
| 757 |
do {}; |
| 758 |
while (false) |
| 759 |
`, "(anonymous): Line 2:18 Unexpected token ;") |
| 760 |
|
| 761 |
|
| 762 |
test(` |
| 763 |
"x\0y" |
| 764 |
`, nil) |
| 765 |
|
| 766 |
|
| 767 |
test(` |
| 768 |
10e10000 |
| 769 |
`, nil) |
| 770 |
|
| 771 |
|
| 772 |
test(` |
| 773 |
"abc\ |
| 774 |
def" |
| 775 |
`, nil) |
| 776 |
|
| 777 |
test("'\\\n'", nil) |
| 778 |
|
| 779 |
test("'\\\r\n'", nil) |
| 780 |
|
| 781 |
|
| 782 |
test("42 = 42;", "(anonymous): Line 1:1 invalid left-hand side in assignment") |
| 783 |
|
| 784 |
|
| 785 |
test(` |
| 786 |
abc /= "1" |
| 787 |
`, nil) |
| 788 |
|
| 789 |
|
| 790 |
test(` |
| 791 |
try{};catch(){} |
| 792 |
`, "(anonymous): Line 2:13 Missing catch or finally after try") |
| 793 |
|
| 794 |
|
| 795 |
test(` |
| 796 |
try{};finally{} |
| 797 |
`, "(anonymous): Line 2:13 Missing catch or finally after try") |
| 798 |
|
| 799 |
|
| 800 |
test(` |
| 801 |
while (true) { |
| 802 |
break abc; |
| 803 |
} |
| 804 |
`, "(anonymous): Line 3:17 Undefined label 'abc'") |
| 805 |
|
| 806 |
|
| 807 |
test(`var x / = 1;`, "(anonymous): Line 1:7 Unexpected token /") |
| 808 |
|
| 809 |
test(` |
| 810 |
function abc() { |
| 811 |
if (0) |
| 812 |
return; |
| 813 |
else { |
| 814 |
} |
| 815 |
} |
| 816 |
`, nil) |
| 817 |
|
| 818 |
test("//\u2028 var =;", "(anonymous): Line 2:6 Unexpected token =") |
| 819 |
|
| 820 |
test(` |
| 821 |
throw |
| 822 |
{} |
| 823 |
`, "(anonymous): Line 2:13 Illegal newline after throw") |
| 824 |
|
| 825 |
|
| 826 |
test(` |
| 827 |
function = 1 |
| 828 |
`, "(anonymous): Line 2:22 Unexpected token =") |
| 829 |
|
| 830 |
|
| 831 |
test(`0e1`, nil) |
| 832 |
|
| 833 |
test("abc = 1; abc\n++", "(anonymous): Line 2:3 Unexpected end of input") |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
test("({ get abc() {} })", nil) |
| 838 |
|
| 839 |
test(`for (abc.def in {}) {}`, nil) |
| 840 |
|
| 841 |
test(`while (true) { break }`, nil) |
| 842 |
|
| 843 |
test(`while (true) { continue }`, nil) |
| 844 |
|
| 845 |
test(`abc=/^(?:(\w+:)\/{2}(\w+(?:\.\w+)*\/?)|(.{0,2}\/{1}))?([/.]*?(?:[^?]+)?\/)?((?:[^/?]+)\.(\w+))(?:\?(\S+)?)?$/,def=/^(?:(\w+:)\/{2})|(.{0,2}\/{1})?([/.]*?(?:[^?]+)?\/?)?$/`, nil) |
| 846 |
|
| 847 |
test(`(function() { try {} catch (err) {} finally {} return })`, nil) |
| 848 |
|
| 849 |
test(`0xde0b6b3a7640080.toFixed(0)`, nil) |
| 850 |
|
| 851 |
test(`/[^-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u37f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/`, nil) |
| 852 |
|
| 853 |
test(`/[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]/`, nil) |
| 854 |
|
| 855 |
test("var abc = 1;\ufeff", nil) |
| 856 |
|
| 857 |
test("\ufeff/* var abc = 1; */", nil) |
| 858 |
|
| 859 |
test(`if (-0x8000000000000000<=abc&&abc<=0x8000000000000000) {}`, nil) |
| 860 |
|
| 861 |
test(`(function(){debugger;return this;})`, nil) |
| 862 |
|
| 863 |
test(` |
| 864 |
|
| 865 |
`, nil) |
| 866 |
|
| 867 |
test(` |
| 868 |
var abc = "" |
| 869 |
debugger |
| 870 |
`, nil) |
| 871 |
|
| 872 |
test(` |
| 873 |
var abc = /\[\]$/ |
| 874 |
debugger |
| 875 |
`, nil) |
| 876 |
|
| 877 |
test(` |
| 878 |
var abc = 1 / |
| 879 |
2 |
| 880 |
debugger |
| 881 |
`, nil) |
| 882 |
|
| 883 |
test(` |
| 884 |
function mergeObjects(x, y) { /* dummy body */} |
| 885 |
|
| 886 |
mergeObjects( |
| 887 |
{ "duck": "quack" }, |
| 888 |
{ "dog": "bark" }, // Allow trailing comma after the last argument. |
| 889 |
); |
| 890 |
`, nil) |
| 891 |
}) |
| 892 |
} |
| 893 |
|
| 894 |
func Test_parseStringLiteral(t *testing.T) { |
| 895 |
tt(t, func() { |
| 896 |
test := func(have, want string) { |
| 897 |
have, err := parseStringLiteral(have) |
| 898 |
is(err, nil) |
| 899 |
is(have, want) |
| 900 |
} |
| 901 |
|
| 902 |
test("", "") |
| 903 |
|
| 904 |
test("1(\\\\d+)", "1(\\d+)") |
| 905 |
|
| 906 |
test("\\u2029", "\u2029") |
| 907 |
|
| 908 |
test("abc\\uFFFFabc", "abc\uFFFFabc") |
| 909 |
|
| 910 |
test("[First line \\\nSecond line \\\n Third line\\\n. ]", |
| 911 |
"[First line Second line Third line. ]") |
| 912 |
|
| 913 |
test("\\u007a\\x79\\u000a\\x78", "zy\nx") |
| 914 |
|
| 915 |
|
| 916 |
test("\\a", "a") |
| 917 |
test("\u0410", "\u0410") |
| 918 |
|
| 919 |
|
| 920 |
test("\\0", "\u0000") |
| 921 |
|
| 922 |
|
| 923 |
test("\u0000", "\u0000") |
| 924 |
|
| 925 |
|
| 926 |
test("'abc'\\\n'def'", "'abc''def'") |
| 927 |
|
| 928 |
|
| 929 |
test("'abc'\\\r\n'def'", "'abc''def'") |
| 930 |
|
| 931 |
|
| 932 |
test("\\0", "\000") |
| 933 |
test("\\00", "\000") |
| 934 |
test("\\000", "\000") |
| 935 |
test("\\09", "\0009") |
| 936 |
test("\\009", "\0009") |
| 937 |
test("\\0009", "\0009") |
| 938 |
test("\\1", "\001") |
| 939 |
test("\\01", "\001") |
| 940 |
test("\\001", "\001") |
| 941 |
test("\\0011", "\0011") |
| 942 |
test("\\1abc", "\001abc") |
| 943 |
|
| 944 |
test("\\\u4e16", "\u4e16") |
| 945 |
|
| 946 |
|
| 947 |
test = func(have, want string) { |
| 948 |
have, err := parseStringLiteral(have) |
| 949 |
is(err.Error(), want) |
| 950 |
is(have, "") |
| 951 |
} |
| 952 |
|
| 953 |
test(`\u`, `invalid escape: \u: len("") != 4`) |
| 954 |
test(`\u0`, `invalid escape: \u: len("0") != 4`) |
| 955 |
test(`\u00`, `invalid escape: \u: len("00") != 4`) |
| 956 |
test(`\u000`, `invalid escape: \u: len("000") != 4`) |
| 957 |
|
| 958 |
test(`\x`, `invalid escape: \x: len("") != 2`) |
| 959 |
test(`\x0`, `invalid escape: \x: len("0") != 2`) |
| 960 |
test(`\x0`, `invalid escape: \x: len("0") != 2`) |
| 961 |
}) |
| 962 |
} |
| 963 |
|
| 964 |
func Test_parseNumberLiteral(t *testing.T) { |
| 965 |
tt(t, func() { |
| 966 |
test := func(input string, expect interface{}) { |
| 967 |
result, err := parseNumberLiteral(input) |
| 968 |
is(err, nil) |
| 969 |
is(result, expect) |
| 970 |
} |
| 971 |
|
| 972 |
test("0", 0) |
| 973 |
|
| 974 |
test("0x8000000000000000", float64(9.223372036854776e+18)) |
| 975 |
}) |
| 976 |
} |
| 977 |
|
| 978 |
func Test_praseRegExpLiteral(t *testing.T) { |
| 979 |
tt(t, func() { |
| 980 |
test := func(input, literal, pattern, flags string) { |
| 981 |
parser := newParser("", input, 1, nil) |
| 982 |
program, err := parser.parse() |
| 983 |
is(err, nil) |
| 984 |
|
| 985 |
regex := program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.RegExpLiteral) |
| 986 |
is(regex.Literal, literal) |
| 987 |
is(regex.Pattern, pattern) |
| 988 |
is(regex.Flags, flags) |
| 989 |
} |
| 990 |
|
| 991 |
test("/abc/", "/abc/", "abc", "") |
| 992 |
test("/abc/gim", "/abc/gim", "abc", "gim") |
| 993 |
test("/abc/ ", "/abc/", "abc", "") |
| 994 |
test("/abc/gim ", "/abc/gim", "abc", "gim") |
| 995 |
test("/abc/;", "/abc/", "abc", "") |
| 996 |
test("/abc/gim;", "/abc/gim", "abc", "gim") |
| 997 |
test("/abc/\n", "/abc/", "abc", "") |
| 998 |
test("/abc/gim\n", "/abc/gim", "abc", "gim") |
| 999 |
test("/abc/;\n", "/abc/", "abc", "") |
| 1000 |
test("/abc/gim;\n", "/abc/gim", "abc", "gim") |
| 1001 |
}) |
| 1002 |
} |
| 1003 |
|
| 1004 |
func TestPosition(t *testing.T) { |
| 1005 |
tt(t, func() { |
| 1006 |
parser := newParser("", "// Lorem ipsum", 1, nil) |
| 1007 |
|
| 1008 |
|
| 1009 |
is(parser.slice(0, 1), "") |
| 1010 |
is(parser.slice(0, 10), "") |
| 1011 |
|
| 1012 |
|
| 1013 |
is(parser.slice(1, 128), "") |
| 1014 |
|
| 1015 |
is(parser.str[0:0], "") |
| 1016 |
is(parser.slice(1, 1), "") |
| 1017 |
|
| 1018 |
is(parser.str[0:1], "/") |
| 1019 |
is(parser.slice(1, 2), "/") |
| 1020 |
|
| 1021 |
is(parser.str[0:14], "// Lorem ipsum") |
| 1022 |
is(parser.slice(1, 15), "// Lorem ipsum") |
| 1023 |
|
| 1024 |
parser = newParser("", "(function(){ return 0; })", 1, nil) |
| 1025 |
program, err := parser.parse() |
| 1026 |
is(err, nil) |
| 1027 |
|
| 1028 |
var node ast.Node |
| 1029 |
node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral) |
| 1030 |
is(node.Idx0(), file.Idx(2)) |
| 1031 |
is(node.Idx1(), file.Idx(25)) |
| 1032 |
is(parser.slice(node.Idx0(), node.Idx1()), "function(){ return 0; }") |
| 1033 |
is(parser.slice(node.Idx0(), node.Idx1()+1), "function(){ return 0; })") |
| 1034 |
is(parser.slice(node.Idx0(), node.Idx1()+2), "") |
| 1035 |
is(node.(*ast.FunctionLiteral).Source, "function(){ return 0; }") |
| 1036 |
|
| 1037 |
node = program |
| 1038 |
is(node.Idx0(), file.Idx(2)) |
| 1039 |
is(node.Idx1(), file.Idx(25)) |
| 1040 |
is(parser.slice(node.Idx0(), node.Idx1()), "function(){ return 0; }") |
| 1041 |
|
| 1042 |
parser = newParser("", "(function(){ return abc; })", 1, nil) |
| 1043 |
program, err = parser.parse() |
| 1044 |
is(err, nil) |
| 1045 |
node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral) |
| 1046 |
is(node.(*ast.FunctionLiteral).Source, "function(){ return abc; }") |
| 1047 |
|
| 1048 |
parser = newParser("", "this.style", 1, nil) |
| 1049 |
program, err = parser.parse() |
| 1050 |
is(err, nil) |
| 1051 |
node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.DotExpression).Left.(*ast.ThisExpression) |
| 1052 |
is(node.Idx0(), file.Idx(1)) |
| 1053 |
is(node.Idx1(), file.Idx(5)) |
| 1054 |
|
| 1055 |
parser = newParser("", "(function(){ if (abc) { throw 'failed'; } })", 1, nil) |
| 1056 |
program, err = parser.parse() |
| 1057 |
is(err, nil) |
| 1058 |
block := program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1059 |
node = block.List[0].(*ast.IfStatement) |
| 1060 |
is(node.Idx0(), 14) |
| 1061 |
is(parser.slice(node.Idx0(), node.Idx1()), "if (abc) { throw 'failed'; }") |
| 1062 |
node = node.(*ast.IfStatement).Consequent.(*ast.BlockStatement).List[0].(*ast.ThrowStatement) |
| 1063 |
is(node.Idx0(), 25) |
| 1064 |
is(parser.slice(node.Idx0(), node.Idx1()), "throw 'failed'") |
| 1065 |
|
| 1066 |
parser = newParser("", "(function(){ for (x=1; x<=4; x++) { console.log(x); } })", 1, nil) |
| 1067 |
program, err = parser.parse() |
| 1068 |
is(err, nil) |
| 1069 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1070 |
node = block.List[0].(*ast.ForStatement) |
| 1071 |
is(node.Idx0(), 14) |
| 1072 |
is(parser.slice(node.Idx0(), node.Idx1()), "for (x=1; x<=4; x++) { console.log(x); }") |
| 1073 |
|
| 1074 |
parser = newParser("", "(function(){ for (p in o) { console.log(p); } })", 1, nil) |
| 1075 |
program, err = parser.parse() |
| 1076 |
is(err, nil) |
| 1077 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1078 |
node = block.List[0].(*ast.ForInStatement) |
| 1079 |
is(node.Idx0(), 14) |
| 1080 |
is(parser.slice(node.Idx0(), node.Idx1()), "for (p in o) { console.log(p); }") |
| 1081 |
|
| 1082 |
parser = newParser("", "x = {x: 1}", 1, nil) |
| 1083 |
program, err = parser.parse() |
| 1084 |
is(err, nil) |
| 1085 |
node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ObjectLiteral) |
| 1086 |
is(node.Idx0(), 5) |
| 1087 |
is(parser.slice(node.Idx0(), node.Idx1()), "{x: 1}") |
| 1088 |
|
| 1089 |
parser = newParser("", "x = [1, 2]", 1, nil) |
| 1090 |
program, err = parser.parse() |
| 1091 |
is(err, nil) |
| 1092 |
node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ArrayLiteral) |
| 1093 |
is(node.Idx0(), 5) |
| 1094 |
is(parser.slice(node.Idx0(), node.Idx1()), "[1, 2]") |
| 1095 |
|
| 1096 |
parser = newParser("", "x = true ? 1 : 2", 1, nil) |
| 1097 |
program, err = parser.parse() |
| 1098 |
is(err, nil) |
| 1099 |
node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ConditionalExpression) |
| 1100 |
is(node.Idx0(), 5) |
| 1101 |
is(parser.slice(node.Idx0(), node.Idx1()), "true ? 1 : 2") |
| 1102 |
|
| 1103 |
parser = newParser("", "(function(){ x = 1, y = 2; })", 1, nil) |
| 1104 |
program, err = parser.parse() |
| 1105 |
is(err, nil) |
| 1106 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1107 |
node = block.List[0].(*ast.ExpressionStatement).Expression.(*ast.SequenceExpression) |
| 1108 |
is(node.Idx0(), 14) |
| 1109 |
is(parser.slice(node.Idx0(), node.Idx1()), "x = 1, y = 2") |
| 1110 |
|
| 1111 |
parser = newParser("", "x = ~x", 1, nil) |
| 1112 |
program, err = parser.parse() |
| 1113 |
is(err, nil) |
| 1114 |
node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.UnaryExpression) |
| 1115 |
is(node.Idx0(), 5) |
| 1116 |
is(parser.slice(node.Idx0(), node.Idx1()), "~x") |
| 1117 |
|
| 1118 |
parser = newParser("", "(function(){ xyz++; })", 1, nil) |
| 1119 |
program, err = parser.parse() |
| 1120 |
is(err, nil) |
| 1121 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1122 |
node = block.List[0].(*ast.ExpressionStatement).Expression.(*ast.UnaryExpression) |
| 1123 |
is(node.Idx0(), 14) |
| 1124 |
is(parser.slice(node.Idx0(), node.Idx1()), "xyz++") |
| 1125 |
|
| 1126 |
parser = newParser("", "(function(){ var abc, xyz = 1; })", 1, nil) |
| 1127 |
program, err = parser.parse() |
| 1128 |
is(err, nil) |
| 1129 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1130 |
node = block.List[0].(*ast.VariableStatement) |
| 1131 |
is(node.Idx0(), 14) |
| 1132 |
is(parser.slice(node.Idx0(), node.Idx1()), "var abc, xyz = 1") |
| 1133 |
node = block.List[0].(*ast.VariableStatement).List[0].(*ast.VariableExpression) |
| 1134 |
is(node.Idx0(), 18) |
| 1135 |
is(parser.slice(node.Idx0(), node.Idx1()), "abc") |
| 1136 |
node = block.List[0].(*ast.VariableStatement).List[1].(*ast.VariableExpression) |
| 1137 |
is(node.Idx0(), 23) |
| 1138 |
is(parser.slice(node.Idx0(), node.Idx1()), "xyz = 1") |
| 1139 |
|
| 1140 |
parser = newParser("", "for (i = 0; i < 10; i++) { if (i == 5) break; }", 1, nil) |
| 1141 |
program, err = parser.parse() |
| 1142 |
is(err, nil) |
| 1143 |
block = program.Body[0].(*ast.ForStatement).Body.(*ast.BlockStatement) |
| 1144 |
node = block.List[0].(*ast.IfStatement).Consequent.(*ast.BranchStatement) |
| 1145 |
is(node.Idx0(), 40) |
| 1146 |
is(parser.slice(node.Idx0(), node.Idx1()), "break") |
| 1147 |
|
| 1148 |
parser = newParser("", "(function(){ xyz: for (i = 0; i < 10; i++) { if (i == 5) continue xyz; } })", 1, nil) |
| 1149 |
program, err = parser.parse() |
| 1150 |
is(err, nil) |
| 1151 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1152 |
node = block.List[0].(*ast.LabelledStatement) |
| 1153 |
is(node.Idx0(), 14) |
| 1154 |
is(parser.slice(node.Idx0(), node.Idx1()), "xyz: for (i = 0; i < 10; i++) { if (i == 5) continue xyz; }") |
| 1155 |
block = node.(*ast.LabelledStatement).Statement.(*ast.ForStatement).Body.(*ast.BlockStatement) |
| 1156 |
node = block.List[0].(*ast.IfStatement).Consequent.(*ast.BranchStatement) |
| 1157 |
is(node.Idx0(), 58) |
| 1158 |
is(parser.slice(node.Idx0(), node.Idx1()), "continue xyz") |
| 1159 |
|
| 1160 |
parser = newParser("", "(function(){ return; })", 1, nil) |
| 1161 |
program, err = parser.parse() |
| 1162 |
is(err, nil) |
| 1163 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1164 |
node = block.List[0].(*ast.ReturnStatement) |
| 1165 |
is(node.Idx0(), 14) |
| 1166 |
is(parser.slice(node.Idx0(), node.Idx1()), "return") |
| 1167 |
|
| 1168 |
parser = newParser("", "(function(){ return 10; })", 1, nil) |
| 1169 |
program, err = parser.parse() |
| 1170 |
is(err, nil) |
| 1171 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1172 |
node = block.List[0].(*ast.ReturnStatement) |
| 1173 |
is(node.Idx0(), 14) |
| 1174 |
is(parser.slice(node.Idx0(), node.Idx1()), "return 10") |
| 1175 |
|
| 1176 |
parser = newParser("", "(function(){ switch (a) { default: return; }})", 1, nil) |
| 1177 |
program, err = parser.parse() |
| 1178 |
is(err, nil) |
| 1179 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1180 |
node = block.List[0].(*ast.SwitchStatement) |
| 1181 |
is(node.Idx0(), 14) |
| 1182 |
is(parser.slice(node.Idx0(), node.Idx1()), "switch (a) { default: return; }") |
| 1183 |
|
| 1184 |
parser = newParser("", "(function(){ try { a(); } catch (error) { b(); } })", 1, nil) |
| 1185 |
program, err = parser.parse() |
| 1186 |
is(err, nil) |
| 1187 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1188 |
node = block.List[0].(*ast.TryStatement) |
| 1189 |
is(node.Idx0(), 14) |
| 1190 |
is(parser.slice(node.Idx0(), node.Idx1()), "try { a(); } catch (error) { b(); }") |
| 1191 |
node = block.List[0].(*ast.TryStatement).Catch |
| 1192 |
is(node.Idx0(), 27) |
| 1193 |
is(parser.slice(node.Idx0(), node.Idx1()), "catch (error) { b(); }") |
| 1194 |
|
| 1195 |
parser = newParser("", "(function(){ try { a(); } catch (error) { b(); } finally { c(); } })", 1, nil) |
| 1196 |
program, err = parser.parse() |
| 1197 |
is(err, nil) |
| 1198 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1199 |
node = block.List[0].(*ast.TryStatement) |
| 1200 |
is(node.Idx0(), 14) |
| 1201 |
is(parser.slice(node.Idx0(), node.Idx1()), "try { a(); } catch (error) { b(); } finally { c(); }") |
| 1202 |
|
| 1203 |
parser = newParser("", "(function(){ with (1) {} })", 1, nil) |
| 1204 |
program, err = parser.parse() |
| 1205 |
is(err, nil) |
| 1206 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1207 |
node = block.List[0].(*ast.WithStatement) |
| 1208 |
is(node.Idx0(), 14) |
| 1209 |
is(parser.slice(node.Idx0(), node.Idx1()), "with (1) {}") |
| 1210 |
|
| 1211 |
parser = newParser("", "while (i < 10) { i++; }", 1, nil) |
| 1212 |
program, err = parser.parse() |
| 1213 |
is(err, nil) |
| 1214 |
node = program.Body[0].(*ast.WhileStatement) |
| 1215 |
is(node.Idx0(), 1) |
| 1216 |
is(parser.slice(node.Idx0(), node.Idx1()), "while (i < 10) { i++; }") |
| 1217 |
|
| 1218 |
parser = newParser("", "do { i++; } while (i < 10 )", 1, nil) |
| 1219 |
program, err = parser.parse() |
| 1220 |
is(err, nil) |
| 1221 |
node = program.Body[0].(*ast.DoWhileStatement) |
| 1222 |
is(node.Idx0(), 1) |
| 1223 |
is(parser.slice(node.Idx0(), node.Idx1()), "do { i++; } while (i < 10 )") |
| 1224 |
|
| 1225 |
parser = newParser("", "(function() { // single-line comment\n })", 1, nil) |
| 1226 |
parser.mode |= StoreComments |
| 1227 |
program, err = parser.parse() |
| 1228 |
is(err, nil) |
| 1229 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1230 |
comment := parser.comments.CommentMap[block][0] |
| 1231 |
is(comment.Begin, 15) |
| 1232 |
is(parser.slice(comment.Begin, file.Idx(int(comment.Begin)+len(comment.Text)+2)), "// single-line comment") |
| 1233 |
|
| 1234 |
parser = newParser("", "(function() { /* multi-line comment */ })", 1, nil) |
| 1235 |
parser.mode |= StoreComments |
| 1236 |
program, err = parser.parse() |
| 1237 |
is(err, nil) |
| 1238 |
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement) |
| 1239 |
comment = parser.comments.CommentMap[block][0] |
| 1240 |
is(comment.Begin, 15) |
| 1241 |
is(parser.slice(comment.Begin, file.Idx(int(comment.Begin)+len(comment.Text)+4)), "/* multi-line comment */") |
| 1242 |
}) |
| 1243 |
} |
| 1244 |
|
| 1245 |
func BenchmarkParser(b *testing.B) { |
| 1246 |
src := underscore.Source() |
| 1247 |
b.ResetTimer() |
| 1248 |
|
| 1249 |
for i := 0; i < b.N; i++ { |
| 1250 |
parser := newParser("", src, 1, nil) |
| 1251 |
_, err := parser.parse() |
| 1252 |
require.NoError(b, err) |
| 1253 |
} |
| 1254 |
} |