parser_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. package parser
  2. import (
  3. "errors"
  4. "regexp"
  5. "strings"
  6. "testing"
  7. "github.com/dop251/goja/ast"
  8. "github.com/dop251/goja/file"
  9. )
  10. func firstErr(err error) error {
  11. switch err := err.(type) {
  12. case ErrorList:
  13. return err[0]
  14. }
  15. return err
  16. }
  17. var matchBeforeAfterSeparator = regexp.MustCompile(`(?m)^[ \t]*---$`)
  18. func testParse(src string) (parser *_parser, program *ast.Program, err error) {
  19. defer func() {
  20. if tmp := recover(); tmp != nil {
  21. switch tmp := tmp.(type) {
  22. case string:
  23. if strings.HasPrefix(tmp, "SyntaxError:") {
  24. parser = nil
  25. program = nil
  26. err = errors.New(tmp)
  27. return
  28. }
  29. }
  30. panic(tmp)
  31. }
  32. }()
  33. parser = newParser("", src)
  34. program, err = parser.parse()
  35. return
  36. }
  37. func TestParseFile(t *testing.T) {
  38. tt(t, func() {
  39. _, err := ParseFile(nil, "", `/abc/`, 0)
  40. is(err, nil)
  41. _, err = ParseFile(nil, "", `/(?!def)abc/`, IgnoreRegExpErrors)
  42. is(err, nil)
  43. _, err = ParseFile(nil, "", `/(?!def)abc/; return`, IgnoreRegExpErrors)
  44. is(err, "(anonymous): Line 1:15 Illegal return statement")
  45. })
  46. }
  47. func TestParseFunction(t *testing.T) {
  48. tt(t, func() {
  49. test := func(prm, bdy string, expect interface{}) *ast.FunctionLiteral {
  50. function, err := ParseFunction(prm, bdy)
  51. is(firstErr(err), expect)
  52. return function
  53. }
  54. test("a, b,c,d", "", nil)
  55. test("a, b;,c,d", "", "(anonymous): Line 1:15 Unexpected token ;")
  56. test("this", "", "(anonymous): Line 1:11 Unexpected token this")
  57. test("a, b, c, null", "", "(anonymous): Line 1:20 Unexpected token null")
  58. test("a, b,c,d", "return;", nil)
  59. test("a, b,c,d", "break;", "(anonymous): Line 2:1 Illegal break statement")
  60. test("a, b,c,d", "{}", nil)
  61. })
  62. }
  63. func TestParserErr(t *testing.T) {
  64. tt(t, func() {
  65. test := func(input string, expect interface{}) (*ast.Program, *_parser) {
  66. parser := newParser("", input)
  67. program, err := parser.parse()
  68. is(firstErr(err), expect)
  69. return program, parser
  70. }
  71. program, parser := test("", nil)
  72. program, parser = test(`
  73. var abc;
  74. break; do {
  75. } while(true);
  76. `, "(anonymous): Line 3:9 Illegal break statement")
  77. {
  78. stmt := program.Body[1].(*ast.BadStatement)
  79. is(parser.position(stmt.From).Column, 9)
  80. is(parser.position(stmt.To).Column, 16)
  81. is(parser.slice(stmt.From, stmt.To), "break; ")
  82. }
  83. test("{", "(anonymous): Line 1:2 Unexpected end of input")
  84. test("}", "(anonymous): Line 1:1 Unexpected token }")
  85. test("3ea", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  86. test("3in", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  87. test("3in []", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  88. test("3e", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  89. test("3e+", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  90. test("3e-", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  91. test("3x", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  92. test("3x0", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  93. test("0x", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  94. test("09", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  95. test("018", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  96. test("01.0", "(anonymous): Line 1:3 Unexpected number")
  97. test("01a", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  98. test("0x3in[]", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  99. test("\"Hello\nWorld\"", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  100. test("\u203f = 10", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  101. test("x\\", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  102. test("x\\\\", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  103. test("x\\u005c", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  104. test("x\\u002a", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  105. test("x\\\\u002a", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  106. test("/\n", "(anonymous): Line 1:1 Invalid regular expression: missing /")
  107. test("0 = 1", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  108. test("func() = 1", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  109. test("(1 + 1) = 2", "(anonymous): Line 1:2 Invalid left-hand side in assignment")
  110. test("1++", "(anonymous): Line 1:2 Invalid left-hand side in assignment")
  111. test("1--", "(anonymous): Line 1:2 Invalid left-hand side in assignment")
  112. test("--1", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  113. test("for((1 + 1) in abc) def();", "(anonymous): Line 1:1 Invalid left-hand side in for-in")
  114. test("[", "(anonymous): Line 1:2 Unexpected end of input")
  115. test("[,", "(anonymous): Line 1:3 Unexpected end of input")
  116. test("1 + {", "(anonymous): Line 1:6 Unexpected end of input")
  117. test("1 + { abc:abc", "(anonymous): Line 1:14 Unexpected end of input")
  118. test("1 + { abc:abc,", "(anonymous): Line 1:15 Unexpected end of input")
  119. test("var abc = /\n/", "(anonymous): Line 1:11 Invalid regular expression: missing /")
  120. test("var abc = \"\n", "(anonymous): Line 1:11 Unexpected token ILLEGAL")
  121. test("var if = 0", "(anonymous): Line 1:5 Unexpected token if")
  122. test("abc + 0 = 1", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  123. test("+abc = 1", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  124. test("1 + (", "(anonymous): Line 1:6 Unexpected end of input")
  125. test("\n\n\n{", "(anonymous): Line 4:2 Unexpected end of input")
  126. test("\n/* Some multiline\ncomment */\n)", "(anonymous): Line 4:1 Unexpected token )")
  127. // TODO
  128. //{ set 1 }
  129. //{ get 2 }
  130. //({ set: s(if) { } })
  131. //({ set s(.) { } })
  132. //({ set: s() { } })
  133. //({ set: s(a, b) { } })
  134. //({ get: g(d) { } })
  135. //({ get i() { }, i: 42 })
  136. //({ i: 42, get i() { } })
  137. //({ set i(x) { }, i: 42 })
  138. //({ i: 42, set i(x) { } })
  139. //({ get i() { }, get i() { } })
  140. //({ set i(x) { }, set i(x) { } })
  141. test("function abc(if) {}", "(anonymous): Line 1:14 Unexpected token if")
  142. test("function abc(true) {}", "(anonymous): Line 1:14 Unexpected token true")
  143. test("function abc(false) {}", "(anonymous): Line 1:14 Unexpected token false")
  144. test("function abc(null) {}", "(anonymous): Line 1:14 Unexpected token null")
  145. test("function null() {}", "(anonymous): Line 1:10 Unexpected token null")
  146. test("function true() {}", "(anonymous): Line 1:10 Unexpected token true")
  147. test("function false() {}", "(anonymous): Line 1:10 Unexpected token false")
  148. test("function if() {}", "(anonymous): Line 1:10 Unexpected token if")
  149. test("a b;", "(anonymous): Line 1:3 Unexpected identifier")
  150. test("if.a", "(anonymous): Line 1:3 Unexpected token .")
  151. test("a if", "(anonymous): Line 1:3 Unexpected token if")
  152. test("a class", "(anonymous): Line 1:3 Unexpected reserved word")
  153. test("break\n", "(anonymous): Line 1:1 Illegal break statement")
  154. test("break 1;", "(anonymous): Line 1:7 Unexpected number")
  155. test("for (;;) { break 1; }", "(anonymous): Line 1:18 Unexpected number")
  156. test("continue\n", "(anonymous): Line 1:1 Illegal continue statement")
  157. test("continue 1;", "(anonymous): Line 1:10 Unexpected number")
  158. test("for (;;) { continue 1; }", "(anonymous): Line 1:21 Unexpected number")
  159. test("throw", "(anonymous): Line 1:1 Unexpected end of input")
  160. test("throw;", "(anonymous): Line 1:6 Unexpected token ;")
  161. test("throw \n", "(anonymous): Line 1:1 Unexpected end of input")
  162. test("for (var abc, def in {});", "(anonymous): Line 1:19 Unexpected token in")
  163. test("for ((abc in {});;);", nil)
  164. test("for ((abc in {}));", "(anonymous): Line 1:17 Unexpected token )")
  165. test("for (+abc in {});", "(anonymous): Line 1:1 Invalid left-hand side in for-in")
  166. test("if (false)", "(anonymous): Line 1:11 Unexpected end of input")
  167. test("if (false) abc(); else", "(anonymous): Line 1:23 Unexpected end of input")
  168. test("do", "(anonymous): Line 1:3 Unexpected end of input")
  169. test("while (false)", "(anonymous): Line 1:14 Unexpected end of input")
  170. test("for (;;)", "(anonymous): Line 1:9 Unexpected end of input")
  171. test("with (abc)", "(anonymous): Line 1:11 Unexpected end of input")
  172. test("try {}", "(anonymous): Line 1:1 Missing catch or finally after try")
  173. test("try {} catch {}", "(anonymous): Line 1:14 Unexpected token {")
  174. test("try {} catch () {}", "(anonymous): Line 1:15 Unexpected token )")
  175. test("\u203f = 1", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  176. // TODO
  177. // const x = 12, y;
  178. // const x, y = 12;
  179. // const x;
  180. // if(true) let a = 1;
  181. // if(true) const a = 1;
  182. test(`new abc()."def"`, "(anonymous): Line 1:11 Unexpected string")
  183. test("/*", "(anonymous): Line 1:3 Unexpected end of input")
  184. test("/**", "(anonymous): Line 1:4 Unexpected end of input")
  185. test("/*\n\n\n", "(anonymous): Line 4:1 Unexpected end of input")
  186. test("/*\n\n\n*", "(anonymous): Line 4:2 Unexpected end of input")
  187. test("/*abc", "(anonymous): Line 1:6 Unexpected end of input")
  188. test("/*abc *", "(anonymous): Line 1:9 Unexpected end of input")
  189. test("\n]", "(anonymous): Line 2:1 Unexpected token ]")
  190. test("\r\n]", "(anonymous): Line 2:1 Unexpected token ]")
  191. test("\n\r]", "(anonymous): Line 3:1 Unexpected token ]")
  192. test("//\r\n]", "(anonymous): Line 2:1 Unexpected token ]")
  193. test("//\n\r]", "(anonymous): Line 3:1 Unexpected token ]")
  194. test("/abc\\\n/", "(anonymous): Line 1:1 Invalid regular expression: missing /")
  195. test("//\r \n]", "(anonymous): Line 3:1 Unexpected token ]")
  196. test("/*\r\n*/]", "(anonymous): Line 2:3 Unexpected token ]")
  197. test("/*\r \n*/]", "(anonymous): Line 3:3 Unexpected token ]")
  198. test("\\\\", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  199. test("\\u005c", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  200. test("\\abc", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  201. test("\\u0000", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  202. test("\\u200c = []", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  203. test("\\u200D = []", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  204. test(`"\`, "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  205. test(`"\u`, "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  206. test("return", "(anonymous): Line 1:1 Illegal return statement")
  207. test("continue", "(anonymous): Line 1:1 Illegal continue statement")
  208. test("break", "(anonymous): Line 1:1 Illegal break statement")
  209. test("switch (abc) { default: continue; }", "(anonymous): Line 1:25 Illegal continue statement")
  210. test("do { abc } *", "(anonymous): Line 1:12 Unexpected token *")
  211. test("while (true) { break abc; }", "(anonymous): Line 1:16 Undefined label 'abc'")
  212. test("while (true) { continue abc; }", "(anonymous): Line 1:16 Undefined label 'abc'")
  213. test("abc: while (true) { (function(){ break abc; }); }", "(anonymous): Line 1:34 Undefined label 'abc'")
  214. test("abc: while (true) { (function(){ abc: break abc; }); }", nil)
  215. test("abc: while (true) { (function(){ continue abc; }); }", "(anonymous): Line 1:34 Undefined label 'abc'")
  216. test(`abc: if (0) break abc; else {}`, nil)
  217. test(`abc: if (0) { break abc; } else {}`, nil)
  218. test(`abc: if (0) { break abc } else {}`, nil)
  219. test("abc: while (true) { abc: while (true) {} }", "(anonymous): Line 1:21 Label 'abc' already exists")
  220. test(`if(0) { do { } while(0) } else { do { } while(0) }`, nil)
  221. test(`if(0) do { } while(0); else do { } while(0)`, nil)
  222. if false {
  223. // TODO When strict mode is implemented
  224. test("(function () { 'use strict'; delete abc; }())", "")
  225. }
  226. test("_: _: while (true) {]", "(anonymous): Line 1:4 Label '_' already exists")
  227. test("_:\n_:\nwhile (true) {]", "(anonymous): Line 2:1 Label '_' already exists")
  228. test("_:\n _:\nwhile (true) {]", "(anonymous): Line 2:4 Label '_' already exists")
  229. test("function(){}", "(anonymous): Line 1:9 Unexpected token (")
  230. test("\n/*/", "(anonymous): Line 2:4 Unexpected end of input")
  231. test("/*/.source", "(anonymous): Line 1:11 Unexpected end of input")
  232. test("var class", "(anonymous): Line 1:5 Unexpected reserved word")
  233. test("var if", "(anonymous): Line 1:5 Unexpected token if")
  234. test("object Object", "(anonymous): Line 1:8 Unexpected identifier")
  235. test("[object Object]", "(anonymous): Line 1:9 Unexpected identifier")
  236. test("\\u0xyz", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  237. test(`for (var abc, def in {}) {}`, "(anonymous): Line 1:19 Unexpected token in")
  238. test(`for (abc, def in {}) {}`, "(anonymous): Line 1:1 Invalid left-hand side in for-in")
  239. test(`for (var abc=def, ghi=("abc" in {}); true;) {}`, nil)
  240. {
  241. // Semicolon insertion
  242. test("this\nif (1);", nil)
  243. test("while (1) { break\nif (1); }", nil)
  244. test("throw\nif (1);", "(anonymous): Line 1:1 Illegal newline after throw")
  245. test("(function(){ return\nif (1); })", nil)
  246. test("while (1) { continue\nif (1); }", nil)
  247. test("debugger\nif (1);", nil)
  248. }
  249. { // Reserved words
  250. test("class", "(anonymous): Line 1:1 Unexpected reserved word")
  251. test("abc.class = 1", nil)
  252. test("var class;", "(anonymous): Line 1:5 Unexpected reserved word")
  253. test("const", "(anonymous): Line 1:1 Unexpected reserved word")
  254. test("abc.const = 1", nil)
  255. test("var const;", "(anonymous): Line 1:5 Unexpected reserved word")
  256. test("enum", "(anonymous): Line 1:1 Unexpected reserved word")
  257. test("abc.enum = 1", nil)
  258. test("var enum;", "(anonymous): Line 1:5 Unexpected reserved word")
  259. test("export", "(anonymous): Line 1:1 Unexpected reserved word")
  260. test("abc.export = 1", nil)
  261. test("var export;", "(anonymous): Line 1:5 Unexpected reserved word")
  262. test("extends", "(anonymous): Line 1:1 Unexpected reserved word")
  263. test("abc.extends = 1", nil)
  264. test("var extends;", "(anonymous): Line 1:5 Unexpected reserved word")
  265. test("import", "(anonymous): Line 1:1 Unexpected reserved word")
  266. test("abc.import = 1", nil)
  267. test("var import;", "(anonymous): Line 1:5 Unexpected reserved word")
  268. test("super", "(anonymous): Line 1:1 Unexpected reserved word")
  269. test("abc.super = 1", nil)
  270. test("var super;", "(anonymous): Line 1:5 Unexpected reserved word")
  271. }
  272. { // Reserved words (strict)
  273. test(`implements`, nil)
  274. test(`abc.implements = 1`, nil)
  275. test(`var implements;`, nil)
  276. test(`interface`, nil)
  277. test(`abc.interface = 1`, nil)
  278. test(`var interface;`, nil)
  279. test(`let`, nil)
  280. test(`abc.let = 1`, nil)
  281. test(`var let;`, nil)
  282. test(`package`, nil)
  283. test(`abc.package = 1`, nil)
  284. test(`var package;`, nil)
  285. test(`private`, nil)
  286. test(`abc.private = 1`, nil)
  287. test(`var private;`, nil)
  288. test(`protected`, nil)
  289. test(`abc.protected = 1`, nil)
  290. test(`var protected;`, nil)
  291. test(`public`, nil)
  292. test(`abc.public = 1`, nil)
  293. test(`var public;`, nil)
  294. test(`static`, nil)
  295. test(`abc.static = 1`, nil)
  296. test(`var static;`, nil)
  297. test(`yield`, nil)
  298. test(`abc.yield = 1`, nil)
  299. test(`var yield;`, nil)
  300. }
  301. })
  302. }
  303. func TestParser(t *testing.T) {
  304. tt(t, func() {
  305. test := func(source string, chk interface{}) *ast.Program {
  306. _, program, err := testParse(source)
  307. is(firstErr(err), chk)
  308. return program
  309. }
  310. test(`
  311. abc
  312. --
  313. []
  314. `, "(anonymous): Line 3:13 Invalid left-hand side in assignment")
  315. test(`
  316. abc--
  317. []
  318. `, nil)
  319. test("1\n[]\n", "(anonymous): Line 2:2 Unexpected token ]")
  320. test(`
  321. function abc() {
  322. }
  323. abc()
  324. `, nil)
  325. program := test("", nil)
  326. test("//", nil)
  327. test("/* */", nil)
  328. test("/** **/", nil)
  329. test("/*****/", nil)
  330. test("/*", "(anonymous): Line 1:3 Unexpected end of input")
  331. test("#", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  332. test("/**/#", "(anonymous): Line 1:5 Unexpected token ILLEGAL")
  333. test("new +", "(anonymous): Line 1:5 Unexpected token +")
  334. program = test(";", nil)
  335. is(len(program.Body), 1)
  336. is(program.Body[0].(*ast.EmptyStatement).Semicolon, file.Idx(1))
  337. program = test(";;", nil)
  338. is(len(program.Body), 2)
  339. is(program.Body[0].(*ast.EmptyStatement).Semicolon, file.Idx(1))
  340. is(program.Body[1].(*ast.EmptyStatement).Semicolon, file.Idx(2))
  341. program = test("1.2", nil)
  342. is(len(program.Body), 1)
  343. is(program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.NumberLiteral).Literal, "1.2")
  344. program = test("/* */1.2", nil)
  345. is(len(program.Body), 1)
  346. is(program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.NumberLiteral).Literal, "1.2")
  347. program = test("\n", nil)
  348. is(len(program.Body), 0)
  349. test(`
  350. if (0) {
  351. abc = 0
  352. }
  353. else abc = 0
  354. `, nil)
  355. test("if (0) abc = 0 else abc = 0", "(anonymous): Line 1:16 Unexpected token else")
  356. test(`
  357. if (0) {
  358. abc = 0
  359. } else abc = 0
  360. `, nil)
  361. test(`
  362. if (0) {
  363. abc = 1
  364. } else {
  365. }
  366. `, nil)
  367. test(`
  368. do {
  369. } while (true)
  370. `, nil)
  371. test(`
  372. try {
  373. } finally {
  374. }
  375. `, nil)
  376. test(`
  377. try {
  378. } catch (abc) {
  379. } finally {
  380. }
  381. `, nil)
  382. test(`
  383. try {
  384. }
  385. catch (abc) {
  386. }
  387. finally {
  388. }
  389. `, nil)
  390. test(`try {} catch (abc) {} finally {}`, nil)
  391. test(`
  392. do {
  393. do {
  394. } while (0)
  395. } while (0)
  396. `, nil)
  397. test(`
  398. (function(){
  399. try {
  400. if (
  401. 1
  402. ) {
  403. return 1
  404. }
  405. return 0
  406. } finally {
  407. }
  408. })()
  409. `, nil)
  410. test("abc = ''\ndef", nil)
  411. test("abc = 1\ndef", nil)
  412. test("abc = Math\ndef", nil)
  413. test(`"\'"`, nil)
  414. test(`
  415. abc = function(){
  416. }
  417. abc = 0
  418. `, nil)
  419. test("abc.null = 0", nil)
  420. test("0x41", nil)
  421. test(`"\d"`, nil)
  422. test(`(function(){return this})`, nil)
  423. test(`
  424. Object.defineProperty(Array.prototype, "0", {
  425. value: 100,
  426. writable: false,
  427. configurable: true
  428. });
  429. abc = [101];
  430. abc.hasOwnProperty("0") && abc[0] === 101;
  431. `, nil)
  432. test(`new abc()`, nil)
  433. test(`new {}`, nil)
  434. test(`
  435. limit = 4
  436. result = 0
  437. while (limit) {
  438. limit = limit - 1
  439. if (limit) {
  440. }
  441. else {
  442. break
  443. }
  444. result = result + 1
  445. }
  446. `, nil)
  447. test(`
  448. while (0) {
  449. if (0) {
  450. continue
  451. }
  452. }
  453. `, nil)
  454. test("var \u0061\u0062\u0063 = 0", nil)
  455. // 7_3_1
  456. test("var test7_3_1\nabc = 66;", nil)
  457. test("var test7_3_1\u2028abc = 66;", nil)
  458. // 7_3_3
  459. test("//\u2028 =;", "(anonymous): Line 2:2 Unexpected token =")
  460. // 7_3_10
  461. test("var abc = \u2029;", "(anonymous): Line 2:1 Unexpected token ;")
  462. test("var abc = \\u2029;", "(anonymous): Line 1:11 Unexpected token ILLEGAL")
  463. test("var \\u0061\\u0062\\u0063 = 0;", nil)
  464. test("'", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  465. test("'\nstr\ning\n'", "(anonymous): Line 1:1 Unexpected token ILLEGAL")
  466. // S7.6_A4.3_T1
  467. test(`var $\u0030 = 0;`, nil)
  468. // S7.6.1.1_A1.1
  469. test(`switch = 1`, "(anonymous): Line 1:8 Unexpected token =")
  470. // S7.8.3_A2.1_T1
  471. test(`.0 === 0.0`, nil)
  472. // 7.8.5-1
  473. test("var regExp = /\\\rn/;", "(anonymous): Line 1:14 Invalid regular expression: missing /")
  474. // S7.8.5_A1.1_T2
  475. test("var regExp = /=/;", nil)
  476. // S7.8.5_A1.2_T1
  477. test("/*/", "(anonymous): Line 1:4 Unexpected end of input")
  478. // Sbp_7.9_A9_T3
  479. test(`
  480. do {
  481. ;
  482. } while (false) true
  483. `, nil)
  484. // S7.9_A10_T10
  485. test(`
  486. {a:1
  487. } 3
  488. `, nil)
  489. test(`
  490. abc
  491. ++def
  492. `, nil)
  493. // S7.9_A5.2_T1
  494. test(`
  495. for(false;false
  496. ) {
  497. break;
  498. }
  499. `, "(anonymous): Line 3:13 Unexpected token )")
  500. // S7.9_A9_T8
  501. test(`
  502. do {};
  503. while (false)
  504. `, "(anonymous): Line 2:18 Unexpected token ;")
  505. // S8.4_A5
  506. test(`
  507. "x\0y"
  508. `, nil)
  509. // S9.3.1_A6_T1
  510. test(`
  511. 10e10000
  512. `, nil)
  513. // 10.4.2-1-5
  514. test(`
  515. "abc\
  516. def"
  517. `, nil)
  518. test("'\\\n'", nil)
  519. test("'\\\r\n'", nil)
  520. //// 11.13.1-1-1
  521. test("42 = 42;", "(anonymous): Line 1:1 Invalid left-hand side in assignment")
  522. // S11.13.2_A4.2_T1.3
  523. test(`
  524. abc /= "1"
  525. `, nil)
  526. // 12.1-1
  527. test(`
  528. try{};catch(){}
  529. `, "(anonymous): Line 2:13 Missing catch or finally after try")
  530. // 12.1-3
  531. test(`
  532. try{};finally{}
  533. `, "(anonymous): Line 2:13 Missing catch or finally after try")
  534. // S12.6.3_A11.1_T3
  535. test(`
  536. while (true) {
  537. break abc;
  538. }
  539. `, "(anonymous): Line 3:17 Undefined label 'abc'")
  540. // S15.3_A2_T1
  541. test(`var x / = 1;`, "(anonymous): Line 1:7 Unexpected token /")
  542. test(`
  543. function abc() {
  544. if (0)
  545. return;
  546. else {
  547. }
  548. }
  549. `, nil)
  550. test("//\u2028 var =;", "(anonymous): Line 2:6 Unexpected token =")
  551. test(`
  552. throw
  553. {}
  554. `, "(anonymous): Line 2:13 Illegal newline after throw")
  555. // S7.6.1.1_A1.11
  556. test(`
  557. function = 1
  558. `, "(anonymous): Line 2:22 Unexpected token =")
  559. // S7.8.3_A1.2_T1
  560. test(`0e1`, nil)
  561. test("abc = 1; abc\n++", "(anonymous): Line 2:3 Unexpected end of input")
  562. // ---
  563. test("({ get abc() {} })", nil)
  564. test(`for (abc.def in {}) {}`, nil)
  565. test(`while (true) { break }`, nil)
  566. test(`while (true) { continue }`, nil)
  567. test(`abc=/^(?:(\w+:)\/{2}(\w+(?:\.\w+)*\/?)|(.{0,2}\/{1}))?([/.]*?(?:[^?]+)?\/)?((?:[^/?]+)\.(\w+))(?:\?(\S+)?)?$/,def=/^(?:(\w+:)\/{2})|(.{0,2}\/{1})?([/.]*?(?:[^?]+)?\/?)?$/`, nil)
  568. test(`(function() { try {} catch (err) {} finally {} return })`, nil)
  569. test(`0xde0b6b3a7640080.toFixed(0)`, nil)
  570. test(`/[^-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u37f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/`, nil)
  571. test(`/[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]/`, nil)
  572. test("var abc = 1;\ufeff", nil)
  573. test("\ufeff/* var abc = 1; */", nil)
  574. test(`if (-0x8000000000000000<=abc&&abc<=0x8000000000000000) {}`, nil)
  575. test(`(function(){debugger;return this;})`, nil)
  576. test(`
  577. `, nil)
  578. test(`
  579. var abc = ""
  580. debugger
  581. `, nil)
  582. test(`
  583. var abc = /\[\]$/
  584. debugger
  585. `, nil)
  586. test(`
  587. var abc = 1 /
  588. 2
  589. debugger
  590. `, nil)
  591. })
  592. }
  593. func Test_parseStringLiteral(t *testing.T) {
  594. tt(t, func() {
  595. test := func(have, want string) {
  596. have, err := parseStringLiteral(have)
  597. is(err, nil)
  598. is(have, want)
  599. }
  600. test("", "")
  601. test("1(\\\\d+)", "1(\\d+)")
  602. test("\\u2029", "\u2029")
  603. test("abc\\uFFFFabc", "abc\uFFFFabc")
  604. test("[First line \\\nSecond line \\\n Third line\\\n. ]",
  605. "[First line Second line Third line. ]")
  606. test("\\u007a\\x79\\u000a\\x78", "zy\nx")
  607. // S7.8.4_A4.2_T3
  608. test("\\a", "a")
  609. test("\u0410", "\u0410")
  610. // S7.8.4_A5.1_T1
  611. test("\\0", "\u0000")
  612. // S8.4_A5
  613. test("\u0000", "\u0000")
  614. // 15.5.4.20
  615. test("'abc'\\\n'def'", "'abc''def'")
  616. // 15.5.4.20-4-1
  617. test("'abc'\\\r\n'def'", "'abc''def'")
  618. // Octal
  619. test("\\0", "\000")
  620. test("\\00", "\000")
  621. test("\\000", "\000")
  622. test("\\09", "\0009")
  623. test("\\009", "\0009")
  624. test("\\0009", "\0009")
  625. test("\\1", "\001")
  626. test("\\01", "\001")
  627. test("\\001", "\001")
  628. test("\\0011", "\0011")
  629. test("\\1abc", "\001abc")
  630. test("\\\u4e16", "\u4e16")
  631. // err
  632. test = func(have, want string) {
  633. have, err := parseStringLiteral(have)
  634. is(err.Error(), want)
  635. is(have, "")
  636. }
  637. test(`\u`, `invalid escape: \u: len("") != 4`)
  638. test(`\u0`, `invalid escape: \u: len("0") != 4`)
  639. test(`\u00`, `invalid escape: \u: len("00") != 4`)
  640. test(`\u000`, `invalid escape: \u: len("000") != 4`)
  641. test(`\x`, `invalid escape: \x: len("") != 2`)
  642. test(`\x0`, `invalid escape: \x: len("0") != 2`)
  643. test(`\x0`, `invalid escape: \x: len("0") != 2`)
  644. })
  645. }
  646. func Test_parseNumberLiteral(t *testing.T) {
  647. tt(t, func() {
  648. test := func(input string, expect interface{}) {
  649. result, err := parseNumberLiteral(input)
  650. is(err, nil)
  651. is(result, expect)
  652. }
  653. test("0", 0)
  654. test("0x8000000000000000", float64(9.223372036854776e+18))
  655. })
  656. }
  657. func TestPosition(t *testing.T) {
  658. tt(t, func() {
  659. parser := newParser("", "// Lorem ipsum")
  660. // Out of range, idx0 (error condition)
  661. is(parser.slice(0, 1), "")
  662. is(parser.slice(0, 10), "")
  663. // Out of range, idx1 (error condition)
  664. is(parser.slice(1, 128), "")
  665. is(parser.str[0:0], "")
  666. is(parser.slice(1, 1), "")
  667. is(parser.str[0:1], "/")
  668. is(parser.slice(1, 2), "/")
  669. is(parser.str[0:14], "// Lorem ipsum")
  670. is(parser.slice(1, 15), "// Lorem ipsum")
  671. parser = newParser("", "(function(){ return 0; })")
  672. program, err := parser.parse()
  673. is(err, nil)
  674. var node ast.Node
  675. node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral)
  676. is(node.Idx0(), file.Idx(2))
  677. is(node.Idx1(), file.Idx(25))
  678. is(parser.slice(node.Idx0(), node.Idx1()), "function(){ return 0; }")
  679. is(parser.slice(node.Idx0(), node.Idx1()+1), "function(){ return 0; })")
  680. is(parser.slice(node.Idx0(), node.Idx1()+2), "")
  681. is(node.(*ast.FunctionLiteral).Source, "function(){ return 0; }")
  682. node = program
  683. is(node.Idx0(), file.Idx(2))
  684. is(node.Idx1(), file.Idx(25))
  685. is(parser.slice(node.Idx0(), node.Idx1()), "function(){ return 0; }")
  686. parser = newParser("", "(function(){ return abc; })")
  687. program, err = parser.parse()
  688. is(err, nil)
  689. node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral)
  690. is(node.(*ast.FunctionLiteral).Source, "function(){ return abc; }")
  691. })
  692. }