parser_test.go 26 KB

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