Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implement relaxed reserved word usage.

  • Loading branch information...
commit a79b6263024ea41df5fecafd90d1ddd8997f38cf 1 parent 55a4afc
Zach Carter authored July 19, 2010
51  lib/harmony/grammar.jiy
@@ -5,6 +5,39 @@
5 5
 
6 6
 %%
7 7
 
  8
+IdentifierName
  9
+    : IDENT
  10
+    | NULLTOKEN
  11
+    | TRUETOKEN
  12
+    | FALSETOKEN
  13
+    | BREAK
  14
+    | CASE
  15
+    | CATCH
  16
+    | CONTINUE
  17
+    | DEBUGGER
  18
+    | DEFAULT
  19
+    | DELETETOKEN
  20
+    | DO
  21
+    | ELSE
  22
+    | FINALLY
  23
+    | FOR
  24
+    | FUNCTION
  25
+    | IF
  26
+    | INTOKEN
  27
+    | INSTANCEOF
  28
+    | NEW
  29
+    | RETURN
  30
+    | SWITCH
  31
+    | THIS
  32
+    | THROW
  33
+    | TRY
  34
+    | TYPEOF
  35
+    | VAR
  36
+    | VOIDTOKEN
  37
+    | WHILE
  38
+    | WITH
  39
+    ;
  40
+
8 41
 Literal
9 42
     : NULLTOKEN
10 43
       {{ $$ = yy.Node('LiteralExpr', {type: 'null', value: null}); }}
@@ -33,17 +66,17 @@ RegularExpressionLiteralBegin
33 66
     ;
34 67
 
35 68
 Property
36  
-    : IDENT ':' AssignmentExpr
  69
+    : IdentifierName ':' AssignmentExpr
37 70
       {{ $$ = yy.Node('DataProp', {name:String($1)}, $3); }}
38 71
     | STRING ':' AssignmentExpr
39 72
       {{ $$ = yy.Node('DataProp', {name:String($1)}, $3); }}
40 73
     | NUMBER ':' AssignmentExpr
41 74
       {{ $$ = yy.Node('DataProp', {name:Number($1)}, $3); }}
42  
-    | IDENT IDENT '(' ')' OPENBRACE FunctionBody CLOSEBRACE
43  
-      {{ if ($1 !== 'get' && $1 !== 'set') throw 'Parse error, invalid set/get.'; // TODO: use jison ABORT when supported
  75
+    | IDENT IdentifierName '(' ')' OPENBRACE FunctionBody CLOSEBRACE
  76
+      {{ if ($1 !== 'get' && $1 !== 'set') throw new Error('Parse error, invalid set/get.'); // TODO: use jison ABORT when supported
44 77
       $$ = yy.Node('GetterSetterProp', {name:String($2), op:String($1)}, yy.Node('ParamDecl', {})).appendList($FunctionBody); }}
45  
-    | IDENT IDENT '(' FormalParameterList ')' OPENBRACE FunctionBody CLOSEBRACE
46  
-      {{ if ($1 !== 'get' && $1 !== 'set') throw 'Parse error, invalid set/get.'; // TODO: use jison ABORT when supported
  78
+    | IDENT IdentifierName '(' FormalParameterList ')' OPENBRACE FunctionBody CLOSEBRACE
  79
+      {{ if ($1 !== 'get' && $1 !== 'set') throw new Error('Parse error, invalid set/get.'); // TODO: use jison ABORT when supported
47 80
       $$ = yy.Node('GetterSetterProp', {name:String($2), op:String($1)}, yy.Node('ParamDecl', {}).appendList($FormalParameterList)).appendList($FunctionBody); }}
48 81
     ;
49 82
 
@@ -113,7 +146,7 @@ MemberExpr
113 146
     | FunctionExpr
114 147
     | MemberExpr '[' Expr ']'
115 148
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
116  
-    | MemberExpr '.' IDENT
  149
+    | MemberExpr '.' IdentifierName
117 150
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
118 151
     | NEW MemberExpr Arguments
119 152
       {{ $$ = yy.Node('NewExpr',{},$MemberExpr).appendList($Arguments); }}
@@ -123,7 +156,7 @@ MemberExprNoBF
123 156
     : PrimaryExprNoBrace
124 157
     | MemberExprNoBF '[' Expr ']'
125 158
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
126  
-    | MemberExprNoBF '.' IDENT
  159
+    | MemberExprNoBF '.' IdentifierName
127 160
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
128 161
     | NEW MemberExpr Arguments
129 162
       {{ $$ = yy.Node('NewExpr',{},$MemberExpr).appendList($Arguments); }}
@@ -152,7 +185,7 @@ CallExpr
152 185
       {{ $$ = yy.Node('CallExpr',{},$1).appendList($2); }}
153 186
     | CallExpr '[' Expr ']'
154 187
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
155  
-    | CallExpr '.' IDENT
  188
+    | CallExpr '.' IdentifierName
156 189
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
157 190
     ;
158 191
 
@@ -167,7 +200,7 @@ CallExprNoBF
167 200
       {{ $$ = yy.Node('CallExpr',{},$1).appendList($2); }}
168 201
     | CallExprNoBF '[' Expr ']'
169 202
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
170  
-    | CallExprNoBF '.' IDENT
  203
+    | CallExprNoBF '.' IdentifierName
171 204
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
172 205
     ;
173 206
 
662  lib/harmony/parser.js
331 additions, 331 deletions not shown
51  lib/js/grammar.jiy
@@ -5,6 +5,39 @@
5 5
 
6 6
 %%
7 7
 
  8
+IdentifierName
  9
+    : IDENT
  10
+    | NULLTOKEN
  11
+    | TRUETOKEN
  12
+    | FALSETOKEN
  13
+    | BREAK
  14
+    | CASE
  15
+    | CATCH
  16
+    | CONTINUE
  17
+    | DEBUGGER
  18
+    | DEFAULT
  19
+    | DELETETOKEN
  20
+    | DO
  21
+    | ELSE
  22
+    | FINALLY
  23
+    | FOR
  24
+    | FUNCTION
  25
+    | IF
  26
+    | INTOKEN
  27
+    | INSTANCEOF
  28
+    | NEW
  29
+    | RETURN
  30
+    | SWITCH
  31
+    | THIS
  32
+    | THROW
  33
+    | TRY
  34
+    | TYPEOF
  35
+    | VAR
  36
+    | VOIDTOKEN
  37
+    | WHILE
  38
+    | WITH
  39
+    ;
  40
+
8 41
 Literal
9 42
     : NULLTOKEN
10 43
       {{ $$ = yy.Node('LiteralExpr', {type: 'null', value: null}); }}
@@ -33,17 +66,17 @@ RegularExpressionLiteralBegin
33 66
     ;
34 67
 
35 68
 Property
36  
-    : IDENT ':' AssignmentExpr
  69
+    : IdentifierName ':' AssignmentExpr
37 70
       {{ $$ = yy.Node('DataProp', {name:String($1)}, $3); }}
38 71
     | STRING ':' AssignmentExpr
39 72
       {{ $$ = yy.Node('DataProp', {name:String($1)}, $3); }}
40 73
     | NUMBER ':' AssignmentExpr
41 74
       {{ $$ = yy.Node('DataProp', {name:Number($1)}, $3); }}
42  
-    | IDENT IDENT '(' ')' OPENBRACE FunctionBody CLOSEBRACE
43  
-      {{ if ($1 !== 'get' && $1 !== 'set') throw 'Parse error, invalid set/get.'; // TODO: use jison ABORT when supported
  75
+    | IDENT IdentifierName '(' ')' OPENBRACE FunctionBody CLOSEBRACE
  76
+      {{ if ($1 !== 'get' && $1 !== 'set') throw new Error('Parse error, invalid set/get.'); // TODO: use jison ABORT when supported
44 77
       $$ = yy.Node('GetterSetterProp', {name:String($2), op:String($1)}, yy.Node('ParamDecl', {})).appendList($FunctionBody); }}
45  
-    | IDENT IDENT '(' FormalParameterList ')' OPENBRACE FunctionBody CLOSEBRACE
46  
-      {{ if ($1 !== 'get' && $1 !== 'set') throw 'Parse error, invalid set/get.'; // TODO: use jison ABORT when supported
  78
+    | IDENT IdentifierName '(' FormalParameterList ')' OPENBRACE FunctionBody CLOSEBRACE
  79
+      {{ if ($1 !== 'get' && $1 !== 'set') throw new Error('Parse error, invalid set/get.'); // TODO: use jison ABORT when supported
47 80
       $$ = yy.Node('GetterSetterProp', {name:String($2), op:String($1)}, yy.Node('ParamDecl', {}).appendList($FormalParameterList)).appendList($FunctionBody); }}
48 81
     ;
49 82
 
@@ -113,7 +146,7 @@ MemberExpr
113 146
     | FunctionExpr
114 147
     | MemberExpr '[' Expr ']'
115 148
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
116  
-    | MemberExpr '.' IDENT
  149
+    | MemberExpr '.' IdentifierName
117 150
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
118 151
     | NEW MemberExpr Arguments
119 152
       {{ $$ = yy.Node('NewExpr',{},$MemberExpr).appendList($Arguments); }}
@@ -123,7 +156,7 @@ MemberExprNoBF
123 156
     : PrimaryExprNoBrace
124 157
     | MemberExprNoBF '[' Expr ']'
125 158
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
126  
-    | MemberExprNoBF '.' IDENT
  159
+    | MemberExprNoBF '.' IdentifierName
127 160
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
128 161
     | NEW MemberExpr Arguments
129 162
       {{ $$ = yy.Node('NewExpr',{},$MemberExpr).appendList($Arguments); }}
@@ -152,7 +185,7 @@ CallExpr
152 185
       {{ $$ = yy.Node('CallExpr',{},$1).appendList($2); }}
153 186
     | CallExpr '[' Expr ']'
154 187
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
155  
-    | CallExpr '.' IDENT
  188
+    | CallExpr '.' IdentifierName
156 189
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
157 190
     ;
158 191
 
@@ -167,7 +200,7 @@ CallExprNoBF
167 200
       {{ $$ = yy.Node('CallExpr',{},$1).appendList($2); }}
168 201
     | CallExprNoBF '[' Expr ']'
169 202
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
170  
-    | CallExprNoBF '.' IDENT
  203
+    | CallExprNoBF '.' IdentifierName
171 204
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
172 205
     ;
173 206
 
612  lib/js/parser.js
306 additions, 306 deletions not shown
46  lib/objj/grammar.jiy
@@ -7,6 +7,34 @@
7 7
 
8 8
 IdentifierName
9 9
     : IDENT
  10
+    | NULLTOKEN
  11
+    | TRUETOKEN
  12
+    | FALSETOKEN
  13
+    | BREAK
  14
+    | CASE
  15
+    | CATCH
  16
+    | CONTINUE
  17
+    | DEBUGGER
  18
+    | DEFAULT
  19
+    | DELETETOKEN
  20
+    | DO
  21
+    | ELSE
  22
+    | FINALLY
  23
+    | FOR
  24
+    | FUNCTION
  25
+    | IF
  26
+    | INTOKEN
  27
+    | INSTANCEOF
  28
+    | NEW
  29
+    | RETURN
  30
+    | SWITCH
  31
+    | THIS
  32
+    | THROW
  33
+    | TRY
  34
+    | TYPEOF
  35
+    | VAR
  36
+    | VOIDTOKEN
  37
+    | WHILE
10 38
     | WITH
11 39
     ;
12 40
 
@@ -38,17 +66,17 @@ RegularExpressionLiteralBegin
38 66
     ;
39 67
 
40 68
 Property
41  
-    : IDENT ':' AssignmentExpr
  69
+    : IdentifierName ':' AssignmentExpr
42 70
       {{ $$ = yy.Node('DataProp', {name:String($1)}, $3); }}
43 71
     | STRING ':' AssignmentExpr
44 72
       {{ $$ = yy.Node('DataProp', {name:String($1)}, $3); }}
45 73
     | NUMBER ':' AssignmentExpr
46 74
       {{ $$ = yy.Node('DataProp', {name:Number($1)}, $3); }}
47  
-    | IDENT IDENT '(' ')' OPENBRACE FunctionBody CLOSEBRACE
48  
-      {{ if ($1 !== 'get' && $1 !== 'set') throw 'Parse error, invalid set/get.'; // TODO: use jison ABORT when supported
  75
+    | IDENT IdentifierName '(' ')' OPENBRACE FunctionBody CLOSEBRACE
  76
+      {{ if ($1 !== 'get' && $1 !== 'set') throw new Error('Parse error, invalid set/get.'); // TODO: use jison ABORT when supported
49 77
       $$ = yy.Node('GetterSetterProp', {name:String($2), op:String($1)}, yy.Node('ParamDecl', {})).appendList($FunctionBody); }}
50  
-    | IDENT IDENT '(' FormalParameterList ')' OPENBRACE FunctionBody CLOSEBRACE
51  
-      {{ if ($1 !== 'get' && $1 !== 'set') throw 'Parse error, invalid set/get.'; // TODO: use jison ABORT when supported
  78
+    | IDENT IdentifierName '(' FormalParameterList ')' OPENBRACE FunctionBody CLOSEBRACE
  79
+      {{ if ($1 !== 'get' && $1 !== 'set') throw new Error('Parse error, invalid set/get.'); // TODO: use jison ABORT when supported
52 80
       $$ = yy.Node('GetterSetterProp', {name:String($2), op:String($1)}, yy.Node('ParamDecl', {}).appendList($FormalParameterList)).appendList($FunctionBody); }}
53 81
     ;
54 82
 
@@ -118,7 +146,7 @@ MemberExpr
118 146
     | FunctionExpr
119 147
     | MemberExpr '[' Expr ']'
120 148
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
121  
-    | MemberExpr '.' IDENT
  149
+    | MemberExpr '.' IdentifierName
122 150
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
123 151
     | NEW MemberExpr Arguments
124 152
       {{ $$ = yy.Node('NewExpr',{},$MemberExpr).appendList($Arguments); }}
@@ -128,7 +156,7 @@ MemberExprNoBF
128 156
     : PrimaryExprNoBrace
129 157
     | MemberExprNoBF '[' Expr ']'
130 158
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
131  
-    | MemberExprNoBF '.' IDENT
  159
+    | MemberExprNoBF '.' IdentifierName
132 160
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
133 161
     | NEW MemberExpr Arguments
134 162
       {{ $$ = yy.Node('NewExpr',{},$MemberExpr).appendList($Arguments); }}
@@ -157,7 +185,7 @@ CallExpr
157 185
       {{ $$ = yy.Node('CallExpr',{},$1).appendList($2); }}
158 186
     | CallExpr '[' Expr ']'
159 187
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
160  
-    | CallExpr '.' IDENT
  188
+    | CallExpr '.' IdentifierName
161 189
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
162 190
     ;
163 191
 
@@ -172,7 +200,7 @@ CallExprNoBF
172 200
       {{ $$ = yy.Node('CallExpr',{},$1).appendList($2); }}
173 201
     | CallExprNoBF '[' Expr ']'
174 202
       {{ $$ = yy.Node('MemberExpr',{brackets:true},$1,$3); }}
175  
-    | CallExprNoBF '.' IDENT
  203
+    | CallExprNoBF '.' IdentifierName
176 204
       {{ $$ = yy.Node('MemberExpr',{brackets:false},$1,yy.Node('LiteralExpr', {type: 'string', value: String($3)})); }}
177 205
     ;
178 206
 
692  lib/objj/parser.js
346 additions, 346 deletions not shown
33  tests/parser/expressions.js
@@ -393,6 +393,16 @@ exports["test member property"] = function () {
393 393
     assert.deepEqual(program, nodes);
394 394
 };
395 395
 
  396
+exports["test member reserved word property"] = function () {
  397
+    var source = 'foo.delete;',
  398
+        program = parse(source),
  399
+        nodes = Node('Program',{},
  400
+                  Node('MemberExpr',{brackets: false},
  401
+                    Node('IdExpr',{name:'foo'}),
  402
+                    Node('LiteralExpr',{type:'string',value:'delete'})));
  403
+    assert.deepEqual(program, nodes);
  404
+};
  405
+
396 406
 exports["test member property bracket access"] = function () {
397 407
     var source = 'foo[false];',
398 408
         program = parse(source),
@@ -449,6 +459,17 @@ exports["test call member"] = function () {
449 459
     assert.deepEqual(program, nodes);
450 460
 };
451 461
 
  462
+exports["test call reserved word member"] = function () {
  463
+    var source = 'foo().void;',
  464
+        program = parse(source),
  465
+        nodes = Node('Program',{},
  466
+                  Node('MemberExpr',{brackets: false},
  467
+                    Node('CallExpr',{},
  468
+                      Node('IdExpr',{name:'foo'})),
  469
+                    Node('LiteralExpr',{type:'string',value:'void'})));
  470
+    assert.deepEqual(program, nodes);
  471
+};
  472
+
452 473
 exports["test call member with brackets"] = function () {
453 474
     var source = 'foo()[true];',
454 475
         program = parse(source),
@@ -472,6 +493,18 @@ exports["test invoke"] = function () {
472 493
     assert.deepEqual(program, nodes);
473 494
 };
474 495
 
  496
+exports["test invoke reserved word"] = function () {
  497
+    var source = 'foo.with(b,c);',
  498
+        program = parse(source),
  499
+        nodes = Node('Program',{},
  500
+                  Node('InvokeExpr',{brackets:false},
  501
+                    Node('IdExpr',{name:'foo'}),
  502
+                    Node('LiteralExpr',{type:'string',value:'with'}),
  503
+                    Node('IdExpr',{name:'b'}),
  504
+                    Node('IdExpr',{name:'c'})));
  505
+    assert.deepEqual(program, nodes);
  506
+};
  507
+
475 508
 exports["test expression list"] = function () {
476 509
     var source = '1,2,3;',
477 510
         program = parse(source),
25  tests/parser/literals.js
@@ -117,6 +117,20 @@ exports["test object literal with properties"] = function () {
117 117
     assert.deepEqual(program, nodes);
118 118
 };
119 119
 
  120
+exports["test object literal with reserved word properties"] = function () {
  121
+    var source = '({delete:1, throw:2, if:3});',
  122
+        program = parse(source),
  123
+        nodes = Node('Program',{},
  124
+                    Node('ObjectExpr',{parens:true},
  125
+                      Node('DataProp', {name:'delete'},
  126
+                        Node('LiteralExpr',{type:'number',value:1})),
  127
+                      Node('DataProp', {name:'throw'},
  128
+                        Node('LiteralExpr',{type:'number',value:2})),
  129
+                      Node('DataProp', {name:'if'},
  130
+                        Node('LiteralExpr',{type:'number',value:3}))));
  131
+    assert.deepEqual(program, nodes);
  132
+};
  133
+
120 134
 exports["test object literal with getter"] = function () {
121 135
     var source = '({get foo(){1;}});',
122 136
         program = parse(source),
@@ -128,6 +142,17 @@ exports["test object literal with getter"] = function () {
128 142
     assert.deepEqual(program, nodes);
129 143
 };
130 144
 
  145
+exports["test object literal with reserved word getter"] = function () {
  146
+    var source = '({get throw(){1;}});',
  147
+        program = parse(source),
  148
+        nodes = Node('Program',{},
  149
+                    Node('ObjectExpr',{parens:true},
  150
+                      Node('GetterSetterProp', {name:'throw', op:'get'},
  151
+                        Node('ParamDecl', {}),
  152
+                        Node('LiteralExpr',{type:'number',value:1}))));
  153
+    assert.deepEqual(program, nodes);
  154
+};
  155
+
131 156
 exports["test object literal with getter and param"] = function () {
132 157
     var source = '({get foo(a){1;}});',
133 158
         program = parse(source),

0 notes on commit a79b626

Please sign in to comment.
Something went wrong with that request. Please try again.