parser_test.go 26 KB

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