testparser.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #! /usr/bin/env node
  2. var parseJS = require("../lib/parse-js");
  3. var sys = require("sys");
  4. // write debug in a very straightforward manner
  5. var debug = function(){
  6. sys.log(Array.prototype.slice.call(arguments).join(', '));
  7. };
  8. ParserTestSuite(function(i, input, desc){
  9. try {
  10. parseJS.parse(input);
  11. debug("ok " + i + ": " + desc);
  12. } catch(e){
  13. debug("FAIL " + i + " " + desc + " (" + e + ")");
  14. }
  15. });
  16. function ParserTestSuite(callback){
  17. var inps = [
  18. ["var abc;", "Regular variable statement w/o assignment"],
  19. ["var abc = 5;", "Regular variable statement with assignment"],
  20. ["/* */;", "Multiline comment"],
  21. ['/** **/;', 'Double star multiline comment'],
  22. ["var f = function(){;};", "Function expression in var assignment"],
  23. ['hi; // moo\n;', 'single line comment'],
  24. ['var varwithfunction;', 'Dont match keywords as substrings'], // difference between `var withsomevar` and `"str"` (local search and lits)
  25. ['a + b;', 'addition'],
  26. ["'a';", 'single string literal'],
  27. ["'a\\n';", 'single string literal with escaped return'],
  28. ['"a";', 'double string literal'],
  29. ['"a\\n";', 'double string literal with escaped return'],
  30. ['"var";', 'string is a keyword'],
  31. ['"variable";', 'string starts with a keyword'],
  32. ['"somevariable";', 'string contains a keyword'],
  33. ['"somevar";', 'string ends with a keyword'],
  34. ['500;', 'int literal'],
  35. ['500.;', 'float literal w/o decimals'],
  36. ['500.432;', 'float literal with decimals'],
  37. ['.432432;', 'float literal w/o int'],
  38. ['(a,b,c);', 'parens and comma'],
  39. ['[1,2,abc];', 'array literal'],
  40. ['var o = {a:1};', 'object literal unquoted key'],
  41. ['var o = {"b":2};', 'object literal quoted key'], // opening curly may not be at the start of a statement...
  42. ['var o = {c:c};', 'object literal keyname is identifier'],
  43. ['var o = {a:1,"b":2,c:c};', 'object literal combinations'],
  44. ['var x;\nvar y;', 'two lines'],
  45. ['var x;\nfunction n(){; }', 'function def'],
  46. ['var x;\nfunction n(abc){; }', 'function def with arg'],
  47. ['var x;\nfunction n(abc, def){ ;}', 'function def with args'],
  48. ['function n(){ "hello"; }', 'function def with body'],
  49. ['/a/;', 'regex literal'],
  50. ['/a/b;', 'regex literal with flag'],
  51. ['/a/ / /b/;', 'regex div regex'],
  52. ['a/b/c;', 'triple division looks like regex'],
  53. ['+function(){/regex/;};', 'regex at start of function body'],
  54. // http://code.google.com/p/es-lab/source/browse/trunk/tests/parser/parsertests.js?r=86
  55. // http://code.google.com/p/es-lab/source/browse/trunk/tests/parser/parsertests.js?r=430
  56. // first tests for the lexer, should also parse as program (when you append a semi)
  57. // comments
  58. ['//foo!@#^&$1234\nbar;', 'single line comment'],
  59. ['/* abcd!@#@$* { } && null*/;', 'single line multi line comment'],
  60. ['/*foo\nbar*/;','multi line comment'],
  61. ['/*x*x*/;','multi line comment with *'],
  62. ['/**/;','empty comment'],
  63. // identifiers
  64. ["x;",'1 identifier'],
  65. ["_x;",'2 identifier'],
  66. ["xyz;",'3 identifier'],
  67. ["$x;",'4 identifier'],
  68. ["x$;",'5 identifier'],
  69. ["_;",'6 identifier'],
  70. ["x5;",'7 identifier'],
  71. ["x_y;",'8 identifier'],
  72. ["x+5;",'9 identifier'],
  73. ["xyz123;",'10 identifier'],
  74. ["x1y1z1;",'11 identifier'],
  75. ["foo\\u00D8bar;",'12 identifier unicode escape'],
  76. //["foo�bar;",'13 identifier unicode embedded (might fail)'],
  77. // numbers
  78. ["5;", '1 number'],
  79. ["5.5;", '2 number'],
  80. ["0;", '3 number'],
  81. ["0.0;", '4 number'],
  82. ["0.001;", '5 number'],
  83. ["1.e2;", '6 number'],
  84. ["1.e-2;", '7 number'],
  85. ["1.E2;", '8 number'],
  86. ["1.E-2;", '9 number'],
  87. [".5;", '10 number'],
  88. [".5e3;", '11 number'],
  89. [".5e-3;", '12 number'],
  90. ["0.5e3;", '13 number'],
  91. ["55;", '14 number'],
  92. ["123;", '15 number'],
  93. ["55.55;", '16 number'],
  94. ["55.55e10;", '17 number'],
  95. ["123.456;", '18 number'],
  96. ["1+e;", '20 number'],
  97. ["0x01;", '22 number'],
  98. ["0XCAFE;", '23 number'],
  99. ["0x12345678;", '24 number'],
  100. ["0x1234ABCD;", '25 number'],
  101. ["0x0001;", '26 number'],
  102. // strings
  103. ["\"foo\";", '1 string'],
  104. ["\'foo\';", '2 string'],
  105. ["\"x\";", '3 string'],
  106. ["\'\';", '4 string'],
  107. ["\"foo\\tbar\";", '5 string'],
  108. ["\"!@#$%^&*()_+{}[]\";", '6 string'],
  109. ["\"/*test*/\";", '7 string'],
  110. ["\"//test\";", '8 string'],
  111. ["\"\\\\\";", '9 string'],
  112. ["\"\\u0001\";", '10 string'],
  113. ["\"\\uFEFF\";", '11 string'],
  114. ["\"\\u10002\";", '12 string'],
  115. ["\"\\x55\";", '13 string'],
  116. ["\"\\x55a\";", '14 string'],
  117. ["\"a\\\\nb\";", '15 string'],
  118. ['";"', '16 string: semi in a string'],
  119. ['"a\\\nb";', '17 string: line terminator escape'],
  120. // literals
  121. ["null;", "null"],
  122. ["true;", "true"],
  123. ["false;", "false"],
  124. // regex
  125. ["/a/;", "1 regex"],
  126. ["/abc/;", "2 regex"],
  127. ["/abc[a-z]*def/g;", "3 regex"],
  128. ["/\\b/;", "4 regex"],
  129. ["/[a-zA-Z]/;", "5 regex"],
  130. // program tests (for as far as they havent been covered above)
  131. // regexp
  132. ["/foo(.*)/g;", "another regexp"],
  133. // arrays
  134. ["[];", "1 array"],
  135. ["[ ];", "2 array"],
  136. ["[1];", "3 array"],
  137. ["[1,2];", "4 array"],
  138. ["[1,2,,];", "5 array"],
  139. ["[1,2,3];", "6 array"],
  140. ["[1,2,3,,,];", "7 array"],
  141. // objects
  142. ["{};", "1 object"],
  143. ["({x:5});", "2 object"],
  144. ["({x:5,y:6});", "3 object"],
  145. ["({x:5,});", "4 object"],
  146. ["({if:5});", "5 object"],
  147. ["({ get x() {42;} });", "6 object"],
  148. ["({ set y(a) {1;} });", "7 object"],
  149. // member expression
  150. ["o.m;", "1 member expression"],
  151. ["o['m'];", "2 member expression"],
  152. ["o['n']['m'];", "3 member expression"],
  153. ["o.n.m;", "4 member expression"],
  154. ["o.if;", "5 member expression"],
  155. // call and invoke expressions
  156. ["f();", "1 call/invoke expression"],
  157. ["f(x);", "2 call/invoke expression"],
  158. ["f(x,y);", "3 call/invoke expression"],
  159. ["o.m();", "4 call/invoke expression"],
  160. ["o['m'];", "5 call/invoke expression"],
  161. ["o.m(x);", "6 call/invoke expression"],
  162. ["o['m'](x);", "7 call/invoke expression"],
  163. ["o.m(x,y);", "8 call/invoke expression"],
  164. ["o['m'](x,y);", "9 call/invoke expression"],
  165. ["f(x)(y);", "10 call/invoke expression"],
  166. ["f().x;", "11 call/invoke expression"],
  167. // eval
  168. ["eval('x');", "1 eval"],
  169. ["(eval)('x');", "2 eval"],
  170. ["(1,eval)('x');", "3 eval"],
  171. ["eval(x,y);", "4 eval"],
  172. // new expression
  173. ["new f();", "1 new expression"],
  174. ["new o;", "2 new expression"],
  175. ["new o.m;", "3 new expression"],
  176. ["new o.m(x);", "4 new expression"],
  177. ["new o.m(x,y);", "5 new expression"],
  178. // prefix/postfix
  179. ["++x;", "1 pre/postfix"],
  180. ["x++;", "2 pre/postfix"],
  181. ["--x;", "3 pre/postfix"],
  182. ["x--;", "4 pre/postfix"],
  183. ["x ++;", "5 pre/postfix"],
  184. ["x /* comment */ ++;", "6 pre/postfix"],
  185. ["++ /* comment */ x;", "7 pre/postfix"],
  186. // unary operators
  187. ["delete x;", "1 unary operator"],
  188. ["void x;", "2 unary operator"],
  189. ["+ x;", "3 unary operator"],
  190. ["-x;", "4 unary operator"],
  191. ["~x;", "5 unary operator"],
  192. ["!x;", "6 unary operator"],
  193. // meh
  194. ["new Date++;", "new date ++"],
  195. ["+x++;", " + x ++"],
  196. // expression expressions
  197. ["1 * 2;", "1 expression expressions"],
  198. ["1 / 2;", "2 expression expressions"],
  199. ["1 % 2;", "3 expression expressions"],
  200. ["1 + 2;", "4 expression expressions"],
  201. ["1 - 2;", "5 expression expressions"],
  202. ["1 << 2;", "6 expression expressions"],
  203. ["1 >>> 2;", "7 expression expressions"],
  204. ["1 >> 2;", "8 expression expressions"],
  205. ["1 * 2 + 3;", "9 expression expressions"],
  206. ["(1+2)*3;", "10 expression expressions"],
  207. ["1*(2+3);", "11 expression expressions"],
  208. ["x<y;", "12 expression expressions"],
  209. ["x>y;", "13 expression expressions"],
  210. ["x<=y;", "14 expression expressions"],
  211. ["x>=y;", "15 expression expressions"],
  212. ["x instanceof y;", "16 expression expressions"],
  213. ["x in y;", "17 expression expressions"],
  214. ["x&y;", "18 expression expressions"],
  215. ["x^y;", "19 expression expressions"],
  216. ["x|y;", "20 expression expressions"],
  217. ["x+y<z;", "21 expression expressions"],
  218. ["x<y+z;", "22 expression expressions"],
  219. ["x+y+z;", "23 expression expressions"],
  220. ["x+y<z;", "24 expression expressions"],
  221. ["x<y+z;", "25 expression expressions"],
  222. ["x&y|z;", "26 expression expressions"],
  223. ["x&&y;", "27 expression expressions"],
  224. ["x||y;", "28 expression expressions"],
  225. ["x&&y||z;", "29 expression expressions"],
  226. ["x||y&&z;", "30 expression expressions"],
  227. ["x<y?z:w;", "31 expression expressions"],
  228. // assignment
  229. ["x >>>= y;", "1 assignment"],
  230. ["x <<= y;", "2 assignment"],
  231. ["x = y;", "3 assignment"],
  232. ["x += y;", "4 assignment"],
  233. ["x /= y;", "5 assignment"],
  234. // comma
  235. ["x, y;", "comma"],
  236. // block
  237. ["{};", "1 block"],
  238. ["{x;};", "2 block"],
  239. ["{x;y;};", "3 block"],
  240. // vars
  241. ["var x;", "1 var"],
  242. ["var x,y;", "2 var"],
  243. ["var x=1,y=2;", "3 var"],
  244. ["var x,y=2;", "4 var"],
  245. // empty
  246. [";", "1 empty"],
  247. ["\n;", "2 empty"],
  248. // expression statement
  249. ["x;", "1 expression statement"],
  250. ["5;", "2 expression statement"],
  251. ["1+2;", "3 expression statement"],
  252. // if
  253. ["if (c) x; else y;", "1 if statement"],
  254. ["if (c) x;", "2 if statement"],
  255. ["if (c) {} else {};", "3 if statement"],
  256. ["if (c1) if (c2) s1; else s2;", "4 if statement"],
  257. // while
  258. ["do s; while (e);", "1 while statement"],
  259. ["do { s; } while (e);", "2 while statement"],
  260. ["while (e) s;", "3 while statement"],
  261. ["while (e) { s; };", "4 while statement"],
  262. // for
  263. ["for (;;) ;", "1 for statement"],
  264. ["for (;c;x++) x;", "2 for statement"],
  265. ["for (i;i<len;++i){};", "3 for statement"],
  266. ["for (var i=0;i<len;++i) {};", "4 for statement"],
  267. ["for (var i=0,j=0;;){};", "5 for statement"],
  268. //["for (x in b; c; u) {};", "6 for statement"],
  269. ["for ((x in b); c; u) {};", "7 for statement"],
  270. ["for (x in a);", "8 for statement"],
  271. ["for (var x in a){};", "9 for statement"],
  272. ["for (var x=5 in a) {};", "10 for statement"],
  273. ["for (var x = a in b in c) {};", "11 for statement"],
  274. ["for (var x=function(){a+b;}; a<b; ++i) some;", "11 for statement, testing for parsingForHeader reset with the function"],
  275. ["for (var x=function(){for (x=0; x<15; ++x) alert(foo); }; a<b; ++i) some;", "11 for statement, testing for parsingForHeader reset with the function"],
  276. // flow statements
  277. ["while(1){ continue; }", "1 flow statement"],
  278. ["label: while(1){ continue label; }", "2 flow statement"],
  279. ["while(1){ break; }", "3 flow statement"],
  280. ["somewhere: while(1){ break somewhere; }", "4 flow statement"],
  281. ["while(1){ continue /* comment */ ; }", "5 flow statement"],
  282. ["while(1){ continue \n; }", "6 flow statement"],
  283. ["(function(){ return; })()", "7 flow statement"],
  284. ["(function(){ return 0; })()", "8 flow statement"],
  285. ["(function(){ return 0 + \n 1; })()", "9 flow statement"],
  286. // with
  287. ["with (e) s;", "with statement"],
  288. // switch
  289. ["switch (e) { case x: s; };", "1 switch statement"],
  290. ["switch (e) { case x: s1;s2; default: s3; case y: s4; };", "2 switch statement"],
  291. ["switch (e) { default: s1; case x: s2; case y: s3; };", "3 switch statement"],
  292. ["switch (e) { default: s; };", "4 switch statement"],
  293. ["switch (e) { case x: s1; case y: s2; };", "5 switch statement"],
  294. // labels
  295. ["foo : x;", " flow statement"],
  296. // throw
  297. ["throw x;", "1 throw statement"],
  298. ["throw x\n;", "2 throw statement"],
  299. // try catch finally
  300. ["try { s1; } catch (e) { s2; };", "1 trycatchfinally statement"],
  301. ["try { s1; } finally { s2; };", "2 trycatchfinally statement"],
  302. ["try { s1; } catch (e) { s2; } finally { s3; };", "3 trycatchfinally statement"],
  303. // debugger
  304. ["debugger;", "debuger statement"],
  305. // function decl
  306. ["function f(x) { e; return x; };", "1 function declaration"],
  307. ["function f() { x; y; };", "2 function declaration"],
  308. ["function f(x,y) { var z; return x; };", "3 function declaration"],
  309. // function exp
  310. ["(function f(x) { return x; });", "1 function expression"],
  311. ["(function empty() {;});", "2 function expression"],
  312. ["(function empty() {;});", "3 function expression"],
  313. ["(function (x) {; });", "4 function expression"],
  314. // program
  315. ["var x; function f(){;}; null;", "1 program"],
  316. [";;", "2 program"],
  317. ["{ x; y; z; }", "3 program"],
  318. ["function f(){ function g(){;}};", "4 program"],
  319. ["x;\n/*foo*/\n ;", "5 program"],
  320. // asi
  321. ["foo: while(1){ continue \n foo; }", "1 asi"],
  322. ["foo: while(1){ break \n foo; }", "2 asi"],
  323. ["(function(){ return\nfoo; })()", "3 asi"],
  324. ["var x; { 1 \n 2 } 3", "4 asi"],
  325. ["ab /* hi */\ncd", "5 asi"],
  326. ["ab/*\n*/cd", "6 asi (multi line multilinecomment counts as eol)"],
  327. ["foo: while(1){ continue /* wtf \n busta */ foo; }", "7 asi illegal with multi line comment"],
  328. ["function f() { s }", "8 asi"],
  329. ["function f() { return }", "9 asi"],
  330. // use strict
  331. // XXX: some of these should actually fail?
  332. // no support for "use strict" yet...
  333. ['"use strict"; \'bla\'\n; foo;', "1 directive"],
  334. ['(function() { "use strict"; \'bla\';\n foo; });', "2 directive"],
  335. ['"use\\n strict";', "3 directive"],
  336. ['foo; "use strict";', "4 directive"],
  337. // tests from http://es5conform.codeplex.com/
  338. ['"use strict"; var o = { eval: 42};', "8.7.2-3-1-s: the use of eval as property name is allowed"],
  339. ['({foo:0,foo:1});', 'Duplicate property name allowed in not strict mode'],
  340. ['function foo(a,a){}', 'Duplicate parameter name allowed in not strict mode'],
  341. ['(function foo(eval){})', 'Eval allowed as parameter name in non strict mode'],
  342. ['(function foo(arguments){})', 'Arguments allowed as parameter name in non strict mode'],
  343. // empty programs
  344. ['', '1 Empty program'],
  345. ['// test', '2 Empty program'],
  346. ['//test\n', '3 Empty program'],
  347. ['\n// test', '4 Empty program'],
  348. ['\n// test\n', '5 Empty program'],
  349. ['/* */', '6 Empty program'],
  350. ['/*\ns,fd\n*/', '7 Empty program'],
  351. ['/*\ns,fd\n*/\n', '8 Empty program'],
  352. [' ', '9 Empty program'],
  353. [' /*\nsmeh*/ \n ', '10 Empty program'],
  354. // trailing whitespace
  355. ['a ', '1 Trailing whitespace'],
  356. ['a /* something */', '2 Trailing whitespace'],
  357. ['a\n // hah', '3 Trailing whitespace'],
  358. ['/abc/de//f', '4 Trailing whitespace'],
  359. ['/abc/de/*f*/\n ', '5 Trailing whitespace'],
  360. // things the parser tripped over at one point or the other (prevents regression bugs)
  361. ['for (x;function(){ a\nb };z) x;', 'for header with function body forcing ASI'],
  362. ['c=function(){return;return};', 'resetting noAsi after literal'],
  363. ['d\nd()', 'asi exception causing token overflow'],
  364. ['for(;;){x=function(){}}', 'function expression in a for header'],
  365. ['for(var k;;){}', 'parser failing due to ASI accepting the incorrect "for" rule'],
  366. ['({get foo(){ }})', 'getter with empty function body'],
  367. ['\nreturnr', 'eol causes return statement to ignore local search requirement'],
  368. [' / /', '1 whitespace before regex causes regex to fail?'],
  369. ['/ // / /', '2 whitespace before regex causes regex to fail?'],
  370. ['/ / / / /', '3 whitespace before regex causes regex to fail?'],
  371. ['\n\t// Used for trimming whitespace\n\ttrimLeft = /^\\s+/;\n\ttrimRight = /\\s+$/;\t\n','turned out this didnt crash (the test below did), but whatever.'],
  372. ['/[\\/]/;', 'escaped forward slash inside class group (would choke on fwd slash)'],
  373. ['/[/]/;', 'also broke but is valid in es5 (not es3)'],
  374. ['({get:5});','get property name thats not a getter'],
  375. ['({set:5});','set property name thats not a setter'],
  376. ['l !== "px" && (d.style(h, c, (k || 1) + l), j = (k || 1) / f.cur() * j, d.style(h, c, j + l)), i[1] && (k = (i[1] === "-=" ? -1 : 1) * k + j), f.custom(j, k, l)', 'this choked regex/div at some point'],
  377. ['(/\'/g, \'\\\\\\\'\') + "\'";', 'the sequence of escaped characters confused the tokenizer'],
  378. ['if (true) /=a/.test("a");', 'regexp starting with "=" in not obvious context (not implied by preceding token)']
  379. ];
  380. for (var i=0; i<inps.length; ++i) {
  381. callback(i, inps[i][0], inps[i][1]);
  382. };
  383. };