2 "This test checks that the following expressions or statements are valid ECMASCRIPT code or should throw parse error"
5 function runTest(_a, throws)
8 if (typeof _a != "string")
9 testFailed("runTest expects string argument: " + _a);
14 success = !(e instanceof SyntaxError);
16 if (throws == !success) {
18 testPassed('Invalid: "' + _a + '"');
20 testPassed('Valid: "' + _a + '"');
23 testFailed('Invalid: "' + _a + '" should throw SyntaxError: Parse error');
25 testFailed('Valid: "' + _a + '" should NOT throw SyntaxError: Parse error');
31 // Test both the grammar and the syntax checker
33 runTest("function f() { " + _a + " }", false);
38 // Test both the grammar and the syntax checker
40 runTest("function f() { " + _a + " }", true);
44 // some statements requires statement as argument, and
45 // it seems the End-Of-File terminator is converted to semicolon
46 // "a:[EOF]" is not parse error, while "{ a: }" is parse error
47 // "if (a)[EOF]" is not parse error, while "{ if (a) }" is parse error
48 // known issues of bison parser:
49 // accepts: 'function f() { return 6 + }' (only inside a function declaration)
50 // some comma expressions: see reparsing-semicolon-insertion.js
52 debug ("Unary operators and member access");
61 valid ("a: +~!new a");
64 valid ("a: b: c: new f(x++)++")
70 valid ("++ new new a ++");
71 valid ("delete void 0");
72 invalid("delete the void");
86 debug ("Binary and conditional operators");
88 valid ("a + + typeof this");
94 valid ("a = b ? b = c : d = e");
95 valid ("s: a[1].l ? b.l['s'] ? c++ : d : true");
96 valid ("a ? b + 1 ? c + 3 * d.l : d[5][6] : e");
97 valid ("a in b instanceof delete -c");
98 invalid("a in instanceof b.l");
99 valid ("- - true % 5");
100 invalid("- false = 3");
101 valid ("a: b: c: (1 + null) = 3");
102 valid ("a[2] = b.l += c /= 4 * 7 ^ !6");
103 invalid("a + typeof b += c in d");
104 invalid("typeof a &= typeof b");
105 valid ("a: ((typeof (a))) >>>= a || b.l && c");
106 valid ("a: b: c[a /= f[a %= b]].l[c[x] = 7] -= a ? b <<= f : g");
107 valid ("-void+x['y'].l == x.l != 5 - f[7]");
109 debug ("Function calls (and new with arguments)");
112 valid ("s: l: a[2](4 == 6, 5 = 6)(f[4], 6)");
113 valid ("s: eval(a.apply(), b.call(c[5] - f[7]))");
119 valid ("a(b[7], c <d> e.l, new a() > b)");
122 valid ("~new new a(1)(i++)(c[l])");
124 valid ("((((a))((b)()).l))()");
125 valid ("(a)[b + (c) / (d())].l--");
128 valid ("new (f + 5)(6, (g)() - 'l'() - true(false))");
129 invalid("a(.length)");
131 debug ("function declaration and expression");
133 valid ("function f() {}");
134 valid ("function f(a,b) {}");
135 invalid("function () {}");
136 invalid("function f(a b) {}");
137 invalid("function f(a,) {}");
138 invalid("function f(a,");
139 invalid("function f(a, 1) {}");
140 valid ("function g(arguments, eval) {}");
141 valid ("function f() {} + function g() {}");
142 invalid("(function a{})");
143 invalid("(function this(){})");
144 valid ("(delete new function f(){} + function(a,b){}(5)(6))");
145 valid ("6 - function (m) { function g() {} }");
146 invalid("function l() {");
147 invalid("function l++(){}");
149 debug ("Array and object literal, comma operator");
151 // Note these are tested elsewhere, no need to repeat those tests here
152 valid ("[] in [5,6] * [,5,] / [,,5,,] || [a,] && new [,b] % [,,]");
156 valid ("1 + {get get(){}, set set(a){}, get1:4, set1:get-set, }");
159 invalid("1 + {get l(");
161 valid ("(4,(5,a(3,4))),f[4,a-6]");
164 invalid("a ? b, c : d");
166 debug ("simple statements");
173 valid ("{} f; { 6 + f() }");
174 valid ("{ a[5],6; {} ++b-new (-5)() } c().l++");
175 valid ("{ l1: l2: l3: { this } a = 32 ; { i++ ; { { { } } ++i } } }");
177 invalid("{ if (a) }");
180 invalid("if (a { }");
181 valid ("x: s: if (a) ; else b");
183 valid ("if (a) if (b) y; else {} else ;");
184 invalid("if (a) {} else x; else");
185 invalid("if (a) { else }");
186 valid ("if (a.l + new b()) 4 + 5 - f()");
187 valid ("if (a) with (x) ; else with (y) ;");
188 invalid("with a.b { }");
189 valid ("while (a() - new b) ;");
190 invalid("while a {}");
191 valid ("do ; while(0) i++"); // Is this REALLY valid? (Firefox also accepts this)
192 valid ("do if (a) x; else y; while(z)");
193 invalid("do g; while 4");
194 invalid("do g; while ((4)");
195 valid ("{ { do do do ; while(0) while(0) while(0) } }");
196 valid ("do while (0) if (a) {} else y; while(0)");
197 valid ("if (a) while (b) if (c) with(d) {} else e; else f");
198 invalid("break ; break your_limits ; continue ; continue living ; debugger");
199 invalid("debugger X");
200 invalid("break 0.2");
201 invalid("continue a++");
202 invalid("continue (my_friend)");
203 valid ("while (1) break");
204 valid ("do if (a) with (b) continue; else debugger; while (false)");
205 invalid("do if (a) while (false) else debugger");
206 invalid("while if (a) ;");
207 valid ("if (a) function f() {} else function g() {}");
208 valid ("if (a()) while(0) function f() {} else function g() {}");
209 invalid("if (a()) function f() { else function g() }");
210 invalid("if (a) if (b) ; else function f {}");
211 invalid("if (a) if (b) ; else function (){}");
213 valid ("throw a + b in void c");
216 debug ("var and const statements");
218 valid ("var a, b = null");
219 valid ("const a = 5, b, c");
222 invalid("var c (6)");
223 valid ("if (a) var a,b; else const b, c");
224 invalid("var 5 = 6");
225 valid ("while (0) var a, b, c=6, d, e, f=5*6, g=f*h, h");
226 invalid("var a = if (b) { c }");
227 invalid("var a = var b");
228 valid ("const a = b += c, a, a, a = (b - f())");
229 invalid("var a %= b | 5");
230 invalid("var (a) = 5");
231 invalid("var a = (4, b = 6");
232 invalid("const 'l' = 3");
233 invalid("var var = 3");
234 valid ("var varr = 3 in 1");
235 valid ("const a, a, a = void 7 - typeof 8, a = 8");
236 valid ("const x_x = 6 /= 7 ? e : f");
237 invalid("var a = ?");
238 invalid("const a = *7");
239 invalid("var a = :)");
240 valid ("var a = a in b in c instanceof d");
241 invalid("var a = b ? c, b");
242 invalid("const a = b : c");
244 debug ("for statement");
246 valid ("for ( ; ; ) { break }");
247 valid ("for ( a ; ; ) { break }");
248 valid ("for ( ; a ; ) { break }");
249 valid ("for ( ; ; a ) { break }");
250 valid ("for ( a ; a ; ) break");
251 valid ("for ( a ; ; a ) break");
252 valid ("for ( ; a ; a ) break");
253 invalid("for () { }");
254 invalid("for ( a ) { }");
255 invalid("for ( ; ) ;");
256 invalid("for a ; b ; c { }");
257 invalid("for (a ; { }");
258 invalid("for ( a ; ) ;");
259 invalid("for ( ; a ) break");
260 valid ("for (var a, b ; ; ) { break } ");
261 valid ("for (var a = b, b = a ; ; ) break");
262 valid ("for (var a = b, c, d, b = a ; x in b ; ) { break }");
263 valid ("for (var a = b, c, d ; ; 1 in a()) break");
264 invalid("for ( ; var a ; ) break");
265 invalid("for (const a; ; ) break");
266 invalid("for ( %a ; ; ) { }");
267 valid ("for (a in b) break");
268 valid ("for (a() in b) break");
269 valid ("for (a().l[4] in b) break");
270 valid ("for (new a in b in c in d) break");
271 valid ("for (new new new a in b) break");
272 invalid("for (delete new a() in b) break");
273 invalid("for (a * a in b) break");
274 valid ("for ((a * a) in b) break");
275 invalid("for (a++ in b) break");
276 valid ("for ((a++) in b) break");
277 invalid("for (++a in b) break");
278 valid ("for ((++a) in b) break");
279 invalid("for (a, b in c) break");
280 invalid("for (a,b in c ;;) break");
281 valid ("for (a,(b in c) ;;) break");
282 valid ("for ((a, b) in c) break");
283 invalid("for (a ? b : c in c) break");
284 valid ("for ((a ? b : c) in c) break");
285 valid ("for (var a in b in c) break");
286 valid ("for (var a = 5 += 6 in b) break");
287 invalid("for (var a += 5 in b) break");
288 invalid("for (var a = in b) break");
289 invalid("for (var a, b in b) break");
290 invalid("for (var a = -6, b in b) break");
291 invalid("for (var a, b = 8 in b) break");
292 valid ("for (var a = (b in c) in d) break");
293 invalid("for (var a = (b in c in d) break");
294 invalid("for (var (a) in b) { }");
295 valid ("for (var a = 7, b = c < d >= d ; f()[6]++ ; --i()[1]++ ) {}");
297 debug ("try statement");
299 invalid("try { break } catch(e) {}");
300 valid ("try {} finally { c++ }");
301 valid ("try { with (x) { } } catch(e) {} finally { if (a) ; }");
303 invalid("catch(e) {}");
304 invalid("finally {}");
305 invalid("try a; catch(e) {}");
306 invalid("try {} catch(e) a()");
307 invalid("try {} finally a()");
308 invalid("try {} catch(e)");
309 invalid("try {} finally");
310 invalid("try {} finally {} catch(e) {}");
311 invalid("try {} catch (...) {}");
312 invalid("try {} catch {}");
313 valid ("if (a) try {} finally {} else b;");
314 valid ("if (--a()) do with(1) try {} catch(ke) { f() ; g() } while (a in b) else {}");
315 invalid("if (a) try {} else b; catch (e) { }");
316 invalid("try { finally {}");
318 debug ("switch statement");
320 valid ("switch (a) {}");
321 invalid("switch () {}");
324 invalid("switch (a) b;");
325 invalid("switch (a) case 3: b;");
326 valid ("switch (f()) { case 5 * f(): default: case '6' - 9: ++i }");
327 invalid("switch (true) { default: case 6: default: }");
328 invalid("switch (l) { f(); }");
329 invalid("switch (l) { case 1: ; a: case 5: }");
330 valid ("switch (g() - h[5].l) { case 1 + 6: a: b: c: ++f }");
331 invalid("switch (g) { case 1: a: }");
332 invalid("switch (g) { case 1: a: default: }");
333 invalid("switch g { case 1: l() }");
334 invalid("switch (g) { case 1:");
335 valid ("switch (l) { case a = b ? c : d : }");
336 valid ("switch (sw) { case a ? b - 7[1] ? [c,,] : d = 6 : { } : }");
337 invalid("switch (l) { case b ? c : }");
338 valid ("switch (l) { case 1: a: with(g) switch (g) { case 2: default: } default: }");
339 invalid("switch (4 - ) { }");
340 invalid("switch (l) { default case: 5; }");
343 invalid("L: L1: L: ;");
344 invalid("L: L1: L2: L3: L4: L: ;");
346 invalid("for(var a,b 'this shouldn\'t be allowed' false ; ) ;");
347 invalid("for(var a,b '");
349 invalid("function __proto__(){}")
350 invalid("(function __proto__(){})")
351 invalid("'use strict'; function __proto__(){}")
352 invalid("'use strict'; (function __proto__(){})")
354 valid("if (0) $foo; ")
355 valid("if (0) _foo; ")
356 valid("if (0) foo$; ")
357 valid("if (0) foo_; ")
358 valid("if (0) obj.$foo; ")
359 valid("if (0) obj._foo; ")
360 valid("if (0) obj.foo$; ")
361 valid("if (0) obj.foo_; ")
362 valid("if (0) obj.foo\\u03bb; ")
364 try { eval("a.b.c = {};"); } catch(e1) { e=e1; shouldBe("e.line", "1") }
368 eval("foo = 'PASS'; a.b.c = {}; bar = 'FAIL';");
370 shouldBe("foo", "'PASS'");
371 shouldBe("bar", "'PASS'");