antlr3 1.8.0 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/History.txt +35 -0
  2. data/Manifest.txt +73 -0
  3. data/README.txt +6 -13
  4. data/java/RubyTarget.java +43 -19
  5. data/java/antlr-full-3.2.1.jar +0 -0
  6. data/lib/antlr3/debug.rb +2 -0
  7. data/lib/antlr3/debug/event-hub.rb +55 -55
  8. data/lib/antlr3/debug/record-event-listener.rb +2 -2
  9. data/lib/antlr3/debug/rule-tracer.rb +14 -14
  10. data/lib/antlr3/debug/socket.rb +47 -47
  11. data/lib/antlr3/debug/trace-event-listener.rb +8 -8
  12. data/lib/antlr3/main.rb +29 -9
  13. data/lib/antlr3/modes/ast-builder.rb +7 -7
  14. data/lib/antlr3/modes/filter.rb +19 -17
  15. data/lib/antlr3/profile.rb +34 -6
  16. data/lib/antlr3/recognizers.rb +50 -1
  17. data/lib/antlr3/streams.rb +19 -15
  18. data/lib/antlr3/streams/rewrite.rb +241 -229
  19. data/lib/antlr3/template/group-file-lexer.rb +6 -8
  20. data/lib/antlr3/template/group-file-parser.rb +16 -16
  21. data/lib/antlr3/template/group-file.rb +1 -1
  22. data/lib/antlr3/test/call-stack.rb +13 -13
  23. data/lib/antlr3/test/core-extensions.rb +69 -69
  24. data/lib/antlr3/test/functional.rb +0 -4
  25. data/lib/antlr3/test/grammar.rb +70 -70
  26. data/lib/antlr3/token.rb +41 -17
  27. data/lib/antlr3/tree.rb +11 -14
  28. data/lib/antlr3/tree/debug.rb +53 -53
  29. data/lib/antlr3/tree/visitor.rb +11 -11
  30. data/lib/antlr3/tree/wizard.rb +35 -35
  31. data/lib/antlr3/util.rb +18 -0
  32. data/lib/antlr3/version.rb +1 -1
  33. data/rakefile +1 -0
  34. data/samples/ANTLRv3Grammar.g +3 -3
  35. data/samples/JavaScript.g +702 -0
  36. data/samples/standard/C/C.g +543 -0
  37. data/samples/standard/C/C.tokens +175 -0
  38. data/samples/standard/C/C__testrig.st +0 -0
  39. data/samples/standard/C/c.rb +12 -0
  40. data/samples/standard/C/input +3479 -0
  41. data/samples/standard/C/output +171 -0
  42. data/samples/standard/LL-star/LLStar.g +101 -0
  43. data/samples/standard/LL-star/input +12 -0
  44. data/samples/standard/LL-star/ll-star.rb +12 -0
  45. data/samples/standard/LL-star/output +2 -0
  46. data/samples/standard/calc/Calculator.g +47 -0
  47. data/samples/standard/calc/Calculator.py +16 -0
  48. data/samples/standard/calc/Calculator.rb +28 -0
  49. data/samples/standard/cminus/CMinus.g +141 -0
  50. data/samples/standard/cminus/bytecode.group +80 -0
  51. data/samples/standard/cminus/cminus.rb +16 -0
  52. data/samples/standard/cminus/input +9 -0
  53. data/samples/standard/cminus/java.group +91 -0
  54. data/samples/standard/cminus/output +11 -0
  55. data/samples/standard/cminus/python.group +48 -0
  56. data/samples/standard/dynamic-scope/DynamicScopes.g +50 -0
  57. data/samples/standard/dynamic-scope/dynamic-scopes.rb +12 -0
  58. data/samples/standard/dynamic-scope/input +7 -0
  59. data/samples/standard/dynamic-scope/output +4 -0
  60. data/samples/standard/fuzzy/FuzzyJava.g +89 -0
  61. data/samples/standard/fuzzy/fuzzy.py +11 -0
  62. data/samples/standard/fuzzy/fuzzy.rb +9 -0
  63. data/samples/standard/fuzzy/input +13 -0
  64. data/samples/standard/fuzzy/output +12 -0
  65. data/samples/standard/hoisted-predicates/HoistedPredicates.g +40 -0
  66. data/samples/standard/hoisted-predicates/hoisted-predicates.rb +13 -0
  67. data/samples/standard/hoisted-predicates/input +1 -0
  68. data/samples/standard/hoisted-predicates/output +1 -0
  69. data/samples/standard/island-grammar/Javadoc.g +46 -0
  70. data/samples/standard/island-grammar/Simple.g +104 -0
  71. data/samples/standard/island-grammar/input +11 -0
  72. data/samples/standard/island-grammar/island.rb +12 -0
  73. data/samples/standard/island-grammar/output +16 -0
  74. data/samples/standard/java/Java.g +827 -0
  75. data/samples/standard/java/input +80 -0
  76. data/samples/standard/java/java.rb +13 -0
  77. data/samples/standard/java/output +1 -0
  78. data/samples/standard/python/Python.g +718 -0
  79. data/samples/standard/python/PythonTokenSource.rb +107 -0
  80. data/samples/standard/python/input +210 -0
  81. data/samples/standard/python/output +24 -0
  82. data/samples/standard/python/python.rb +14 -0
  83. data/samples/standard/rakefile +18 -0
  84. data/samples/standard/scopes/SymbolTable.g +66 -0
  85. data/samples/standard/scopes/input +12 -0
  86. data/samples/standard/scopes/output +3 -0
  87. data/samples/standard/scopes/scopes.rb +12 -0
  88. data/samples/standard/simplecTreeParser/SimpleC.g +113 -0
  89. data/samples/standard/simplecTreeParser/SimpleCWalker.g +64 -0
  90. data/samples/standard/simplecTreeParser/input +12 -0
  91. data/samples/standard/simplecTreeParser/output +1 -0
  92. data/samples/standard/simplecTreeParser/simplec.rb +18 -0
  93. data/samples/standard/treeparser/Lang.g +24 -0
  94. data/samples/standard/treeparser/LangDumpDecl.g +17 -0
  95. data/samples/standard/treeparser/input +1 -0
  96. data/samples/standard/treeparser/output +2 -0
  97. data/samples/standard/treeparser/treeparser.rb +18 -0
  98. data/samples/standard/tweak/Tweak.g +68 -0
  99. data/samples/standard/tweak/input +9 -0
  100. data/samples/standard/tweak/output +16 -0
  101. data/samples/standard/tweak/tweak.rb +13 -0
  102. data/samples/standard/xml/README +16 -0
  103. data/samples/standard/xml/XML.g +123 -0
  104. data/samples/standard/xml/input +21 -0
  105. data/samples/standard/xml/output +39 -0
  106. data/samples/standard/xml/xml.rb +9 -0
  107. data/templates/Ruby.stg +4 -4
  108. data/test/functional/ast-output/auto-ast.rb +0 -5
  109. data/test/functional/ast-output/rewrites.rb +4 -4
  110. data/test/unit/test-scope.rb +45 -0
  111. metadata +96 -8
data/rakefile CHANGED
@@ -21,6 +21,7 @@ Hoe.spec "antlr3" do
21
21
  "test/unit/test-recognizers.rb",
22
22
  "test/unit/test-trees.rb",
23
23
  "test/unit/test-exceptions.rb",
24
+ "test/unit/test-scope.rb",
24
25
  "test/unit/test-scheme.rb",
25
26
  "test/unit/test-template.rb",
26
27
  "test/functional/delegation/import.rb",
@@ -92,7 +92,7 @@ grammar_def
92
92
  g='grammar' id ';' options_spec? tokens_spec? attr_scope* action*
93
93
  rule+
94
94
  EOF
95
- -> ^( {@adaptor.create!(@grammar_type, $g) }
95
+ -> ^( {@adaptor.create(@grammar_type, $g) }
96
96
  id DOC_COMMENT? options_spec? tokens_spec? attr_scope* action* rule+
97
97
  )
98
98
  ;
@@ -156,7 +156,7 @@ scope {
156
156
  throws_spec? options_spec? rule_scope_spec? rule_action*
157
157
  ':' alt_list ';'
158
158
  exception_group?
159
- -> ^( RULE id {$modifier ? @adaptor.create!($modifier) : nil} ^(ARG[$arg] $arg)? ^('returns' $rt)?
159
+ -> ^( RULE id {$modifier ? @adaptor.create($modifier) : nil} ^(ARG[$arg] $arg)? ^('returns' $rt)?
160
160
  throws_spec? options_spec? rule_scope_spec? rule_action*
161
161
  alt_list
162
162
  exception_group?
@@ -193,7 +193,7 @@ altpair : alternative rewrite ;
193
193
 
194
194
  alt_list
195
195
  @init {
196
- block_root = @adaptor.create!(BLOCK, @input.look(-1), "BLOCK");
196
+ block_root = @adaptor.create(BLOCK, @input.look(-1), "BLOCK");
197
197
  }
198
198
  : altpair ( '|' altpair )* -> ^( {block_root} altpair+ EOB["eob"] )
199
199
  ;
@@ -0,0 +1,702 @@
1
+ /**
2
+ [The "BSD licence"]
3
+ Copyright (c) 2010 Kyle Yetter
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions
8
+ are met:
9
+
10
+ 1. Redistributions of source code must retain the above copyright
11
+ notice, this list of conditions and the following disclaimer.
12
+ 2. Redistributions in binary form must reproduce the above copyright
13
+ notice, this list of conditions and the following disclaimer in the
14
+ documentation and/or other materials provided with the distribution.
15
+ 3. The name of the author may not be used to endorse or promote products
16
+ derived from this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ **/
30
+
31
+ grammar JavaScript;
32
+
33
+ options
34
+ {
35
+ language = Ruby;
36
+ output = AST;
37
+ }
38
+
39
+ tokens {
40
+ ABSTRACT='abstract'; GET='get'; POST_DECR;
41
+ AMP='&'; GOTO='goto'; POST_INCR;
42
+ AMP_ASGN='&='; GREATER='>'; PRIVATE='private';
43
+ AND='&&'; HAT='^'; PROGRAM;
44
+ AREF; HAT_ASGN='^='; PROTECTED='protected';
45
+ ARGUMENTS; IF='if'; PUBLIC='public';
46
+ ARRAY; IMPLEMENTS='implements'; QMARK='?';
47
+ AS='as'; IMPORT='import'; RBRACE='}';
48
+ ASGN='='; IN='in'; RBRACK=']';
49
+ BLOCK; INCR='++'; REGEX;
50
+ BOOLEAN='boolean'; INSTANCEOF='instanceof'; RETURN='return';
51
+ BREAK='break'; INT='int'; RPAREN=')';
52
+ BYTE='byte'; INTERFACE='interface'; RSHIFT3='>>>';
53
+ CALL; IS='is'; RSHIFT3_ASGN='>>>=';
54
+ CASE='case'; LABEL; RSHIFT='>>';
55
+ CATCH='catch'; LBRACE='{'; RSHIFT_ASGN='>>=';
56
+ CHAR='char'; LBRACK='['; SEMI=';'; //]
57
+ CLASS='class'; LEQ='<='; SET='set';
58
+ COLON=':'; LESS='<'; SHORT='short';
59
+ COMMA=','; LET='let'; SLASH;
60
+ COMMENT; LINE_COMMENT; SLASH_ASGN;
61
+ CONST='const'; LITERAL; STAR='*';
62
+ CONTINUE='continue'; LONG='long'; STAR_ASGN='*=';
63
+ DEBUGGER='debugger'; LPAREN='('; STATIC='static';
64
+ DECR='--'; LSHIFT='<<'; SUPER='super';
65
+ DEFAULT='default'; LSHIFT_ASGN='<<='; SWITCH='switch';
66
+ DELETE='delete'; MINUS='-'; SYNCHRONIZED='synchronized';
67
+ DO='do'; MINUS_ASGN='-='; THIS='this';
68
+ DOT='.'; MOD='%'; THROW='throw';
69
+ DOUBLE='double'; MOD_ASGN='%='; THROWS='throws';
70
+ EACH='each'; NAMESPACE='namespace'; TILDE='~';
71
+ ELSE='else'; NATIVE='native'; TRANSIENT='transient';
72
+ ENUM='enum'; NEQ='!='; TRUE='true';
73
+ EQ='=='; NEQQ='!=='; TRY='try';
74
+ EQQ='==='; NEW='new'; TYPEOF='typeof';
75
+ EXPORT='export'; NOT='!'; UNDEFINED='undefined';
76
+ EXTENDS='extends'; NULL='null'; USE='use';
77
+ FALSE='false'; OBJECT; VAR='var';
78
+ FINAL='final'; OR='||'; VOID='void';
79
+ FINALLY='finally'; PACKAGE='package'; VOLATILE='volatile';
80
+ FLOAT='float'; PARAMS; WHILE='while';
81
+ FOR='for'; PIPE='|'; WITH='with';
82
+ FOR_IN; PIPE_ASGN='|='; YIELD='yield';
83
+ FUNCTION='function'; PLUS='+';
84
+ GEQ='>='; PLUS_ASGN='+=';
85
+ }
86
+
87
+ scope InFor {
88
+ active;
89
+ }
90
+
91
+ @parser::members {
92
+
93
+ def auto_semicolon?( error )
94
+ if ANTLR3::Error::NoViableAlternative === error
95
+ ( input.position - 1 ).downto( 0 ) do | i |
96
+ token = input[ i ] or break
97
+ token.hidden? or break
98
+ token.type == NEWLINE and return true
99
+ end
100
+ end
101
+ return false
102
+ end
103
+
104
+ def recover( error = $! )
105
+ auto_semicolon?( error ) and return
106
+ super
107
+ end
108
+
109
+ def report_error( error = $! )
110
+ auto_semicolon?( error ) and return
111
+ super
112
+ end
113
+
114
+ }
115
+
116
+ @lexer::members {
117
+ attr_accessor :regex_possible
118
+
119
+ NO_REGEX_FOLLOWS = Set[
120
+ ID, REGEX, STRING, NUMBER, THIS,
121
+ TRUE, FALSE, NULL,
122
+ RPAREN, RBRACK, RBRACE, UNDEFINED
123
+ ]
124
+
125
+ def next_token
126
+ token = super
127
+ unless token.hidden?
128
+ @regex_possible =
129
+ NO_REGEX_FOLLOWS.include?( token.type ) ? false : true
130
+ @last_type = token.type
131
+ end
132
+ return( token )
133
+ end
134
+ }
135
+
136
+ @lexer::init {
137
+ @regex_possible = true
138
+ @last_type = nil
139
+ }
140
+
141
+ program
142
+ : source_elements
143
+ ;
144
+
145
+ source_elements
146
+ : statement ( statement )*
147
+ ;
148
+
149
+ block
150
+ : ( '{' )=> statement_block
151
+ | statement
152
+ ;
153
+
154
+ statement_block
155
+ : '{' ( statement_list )? '}' -> ^( BLOCK statement_list? )
156
+ ;
157
+
158
+ statement_list
159
+ : statement ( statement )* -> statement+
160
+ ;
161
+
162
+ statement
163
+ : variable_statement
164
+ | empty_statement
165
+ | ( ID ':' )=> labelled_statement
166
+ | ( 'let' )=> let_statement
167
+ | expression_statement
168
+ | if_statement
169
+ | iteration_statement
170
+ | continue_statement
171
+ | break_statement
172
+ | yield_statement
173
+ | return_statement
174
+ | with_statement
175
+ | switch_statement
176
+ | throw_statement
177
+ | try_statement
178
+ ;
179
+
180
+ variable_statement
181
+ : 'var' variable_declaration_list statement_end -> ^( 'var' variable_declaration_list )
182
+ ;
183
+
184
+ variable_declaration_list
185
+ : variable_declaration ( ',' variable_declaration )*
186
+ -> variable_declaration+
187
+ ;
188
+
189
+ variable_declaration
190
+ : declaration_target ( '='^ expression )?
191
+ ;
192
+
193
+ declaration_target
194
+ : '[' declaration_target ( ',' declaration_target )* ']' -> ^( ARRAY declaration_target+ )
195
+ | '{' declaration_key ( ',' declaration_key )* '}' -> ^( OBJECT declaration_key+ )
196
+ | variable_name -> variable_name
197
+ ;
198
+
199
+ declaration_key
200
+ : property_name ':'^ declaration_target
201
+ ;
202
+
203
+ empty_statement
204
+ : ';' ->
205
+ ;
206
+
207
+ expression_statement
208
+ : expression_list statement_end! ;
209
+
210
+ if_statement
211
+ : 'if' '(' expression_list ')'
212
+ when_true=block
213
+ ( 'else' when_false=block )?
214
+ -> ^( 'if' expression_list $when_true $when_false? )
215
+ ;
216
+
217
+ let_statement
218
+ : 'let' '(' variable_declaration_list ')' block
219
+ -> ^( 'let' variable_declaration_list block )
220
+ ;
221
+
222
+ iteration_statement
223
+ : do_while_statement
224
+ | while_statement
225
+ | ( 'for' 'each' '(' )=> for_each_in_statement
226
+ | ( 'for' '(' for_in_statement_initialiser_part 'in' )=> for_in_statement
227
+ | for_statement
228
+ ;
229
+
230
+ do_while_statement
231
+ : 'do' block 'while' '(' expression_list ')' statement_end -> ^( 'do' expression_list block )
232
+ ;
233
+
234
+ while_statement
235
+ : 'while' '(' expression_list ')' block
236
+ -> ^( 'while' expression_list block )
237
+ ;
238
+
239
+ for_statement
240
+ : 'for'
241
+ '('
242
+ ( for_statement_initialiser_part )? ';'
243
+ ( cond=expression_list )? ';'
244
+ ( step=expression_list )?
245
+ ')'
246
+ block
247
+ -> ^( 'for' for_statement_initialiser_part? $cond? $step? block )
248
+ ;
249
+
250
+ for_statement_initialiser_part
251
+ scope InFor;
252
+ @before {
253
+ $InFor::active = true
254
+ }
255
+ @after {
256
+ $InFor::active = false
257
+ }
258
+ : expression_list
259
+ | 'var'^ variable_declaration_list
260
+ ;
261
+
262
+ for_each_in_statement
263
+ : 'for' 'each' '(' for_in_statement_initialiser_part 'in' expression ')' block
264
+ -> ^( 'each' for_in_statement_initialiser_part expression block )
265
+ ;
266
+
267
+ for_in_statement
268
+ : f='for' '(' for_in_statement_initialiser_part 'in' expression ')' block
269
+ -> ^( FOR_IN[ $f ] for_in_statement_initialiser_part expression block )
270
+ ;
271
+
272
+ for_in_statement_initialiser_part
273
+ scope InFor;
274
+ @before {
275
+ $InFor::active = true
276
+ }
277
+ @after {
278
+ $InFor::active = false
279
+ }
280
+ : 'var'^ variable_declaration
281
+ | member
282
+ ;
283
+
284
+ continue_statement
285
+ : 'continue'^ ID? statement_end!
286
+ ;
287
+
288
+ break_statement
289
+ : 'break'^ ID? statement_end!
290
+ ;
291
+
292
+ return_statement
293
+ : 'return'^ expression_list? statement_end!
294
+ ;
295
+
296
+ yield_statement
297
+ : 'yield'^ expression_list? statement_end!
298
+ ;
299
+
300
+
301
+ with_statement
302
+ : 'with' '(' expression_list ')' block
303
+ -> ^( 'with' expression_list block )
304
+ ;
305
+
306
+ labelled_statement
307
+ : ID ':' block
308
+ -> ^( LABEL ID block )
309
+ ;
310
+
311
+ switch_statement
312
+ : 'switch' '(' expression_list ')'
313
+ '{'
314
+ ( case_clause )*
315
+ ( default_clause ( case_clause )* )?
316
+ '}'
317
+ -> ^( 'switch' expression_list case_clause* default_clause? )
318
+ ;
319
+
320
+ case_clause
321
+ : 'case'^ expression_list ':'! statement_list?
322
+ ;
323
+
324
+ default_clause
325
+ : 'default'^ ':'! statement_list?
326
+ ;
327
+
328
+ throw_statement
329
+ : 'throw'^ expression_list statement_end!
330
+ ;
331
+
332
+ try_statement
333
+ : 'try' ( statement_block -> statement_block )
334
+ ( f=finally_clause -> { $f.tree.unshift( $tree ) }
335
+ | ( catch_clause -> { $catch_clause.tree.unshift( $tree ) } )
336
+ ( f2=finally_clause -> { $f2.tree.unshift( $tree ) } )?
337
+ )
338
+ ;
339
+
340
+ catch_clause
341
+ : 'catch' '(' ID ')' statement_block
342
+ -> ^( 'catch' ID statement_block )
343
+ ;
344
+
345
+ finally_clause
346
+ : 'finally' statement_block
347
+ -> ^( 'finally' statement_block )
348
+ ;
349
+
350
+ /********************************************************************************
351
+ ******************************** Expression **********************************
352
+ ********************************************************************************/
353
+
354
+ expression_list
355
+ : expression ( ','^ expression )*
356
+ ;
357
+
358
+ expression
359
+ : ( member assignment_op )=> member assignment_op^ expression
360
+ | conditional
361
+ ;
362
+
363
+ assignment_op
364
+ : '='
365
+ | '*='
366
+ | SLASH_ASGN
367
+ | '%='
368
+ | '+='
369
+ | '-='
370
+ | '<<='
371
+ | '>>='
372
+ | '>>>='
373
+ | '&='
374
+ | '^='
375
+ | '|='
376
+ ;
377
+
378
+ conditional
379
+ : logical_or ( '?'^ t=expression ':'! f=expression )?
380
+ ;
381
+
382
+ logical_or
383
+ : logical_and ( '||'^ logical_and )*
384
+ ;
385
+
386
+ logical_and
387
+ : bit_or ( '&&'^ bit_or )*
388
+ ;
389
+
390
+ bit_or
391
+ : bit_xor ( '|'^ bit_xor )*
392
+ ;
393
+
394
+ bit_xor
395
+ : bit_and ( '^'^ bit_and )*
396
+ ;
397
+
398
+ bit_and
399
+ : equality ( '&'^ equality )*
400
+ ;
401
+
402
+ equality
403
+ : relation
404
+ (
405
+ ( '=='^ | '!='^ | '==='^ | '!=='^ )
406
+ relation
407
+ )*
408
+ ;
409
+
410
+ relation
411
+ : shift ( relation_op^ shift )*
412
+ ;
413
+
414
+ relation_op
415
+ : '<'
416
+ | '>'
417
+ | '<='
418
+ | '>='
419
+ | 'instanceof'
420
+ | { !( $InFor.last && $InFor::active ) }?=> 'in'
421
+ ;
422
+
423
+ shift
424
+ : add ( ( '<<'^ | '>>'^ | '>>>'^ ) add )*
425
+ ;
426
+
427
+ add
428
+ : mult ( ( '+'^ | '-'^ ) mult )*
429
+ ;
430
+
431
+ mult
432
+ : unary ( ( '*'^ | SLASH^ | '%'^ ) unary )*
433
+ ;
434
+
435
+ unary
436
+ : 'delete'^ unary
437
+ | 'void'^ unary
438
+ | 'typeof'^ unary
439
+ | '++'^ unary
440
+ | '--'^ unary
441
+ | '+'^ unary
442
+ | '-'^ unary
443
+ | '~'^ unary
444
+ | '!'^ unary
445
+ | postfix
446
+ ;
447
+
448
+ postfix
449
+ : member
450
+ ( '++' -> ^( POST_INCR[ '++' ] member )
451
+ | '--' -> ^( POST_DECR[ '--' ] member )
452
+ | -> member
453
+ )
454
+ ;
455
+
456
+ member
457
+ : ( receiver -> receiver )
458
+ ( accessor -> { $accessor.tree.unshift( $tree ) }
459
+ | arguments -> ^( CALL $member arguments )
460
+ )*
461
+ ;
462
+
463
+ accessor
464
+ : '[' expression ']' -> ^( AREF[ '[' ] expression ) //]
465
+ | '.' property_name -> ^( '.' property_name )
466
+ ;
467
+
468
+ receiver
469
+ : primary
470
+ | function
471
+ | ( 'new' new_target arguments? )=>
472
+ 'new' new_target arguments? -> ^( 'new' new_target arguments? )
473
+ ;
474
+
475
+ new_target
476
+ : ( receiver -> receiver )
477
+ ( accessor -> { $accessor.tree.unshift( $tree ) } )*
478
+ ;
479
+
480
+ arguments
481
+ : '(' ')' -> ^( ARGUMENTS )
482
+ | '(' expression ( ',' expression )* ')'
483
+ -> ^( ARGUMENTS expression+ )
484
+ ;
485
+
486
+ primary
487
+ : 'this'^
488
+ | variable_name^
489
+ | 'null'^
490
+ | 'true'^
491
+ | 'false'^
492
+ | 'undefined'^
493
+ | NUMBER^
494
+ | STRING^
495
+ | REGEX^
496
+ | array_literal
497
+ | object_literal
498
+ | '('! expression_list ')'!
499
+ ;
500
+
501
+ function
502
+ : 'function'^ ( ID )? formal_parameter_list block
503
+ ;
504
+
505
+ formal_parameter_list
506
+ : '(' ( ID ( ',' ID )* )? ')'
507
+ -> ^( PARAMS ID* )
508
+ ;
509
+
510
+ literal
511
+ : 'null'^ | 'true'^ | 'false'^ | NUMBER^ | STRING^ | REGEX^
512
+ ;
513
+
514
+
515
+ array_literal
516
+ : '[' ']' -> ^( ARRAY )
517
+ | '[' expression ( ',' expression? )* ']' -> ^( ARRAY expression+ )
518
+ ;
519
+
520
+ list_element
521
+ : ( ',' )=> -> ^( UNDEFINED )
522
+ | expression -> expression
523
+ ;
524
+
525
+
526
+ object_literal
527
+ : '{' '}' -> ^( OBJECT )
528
+ | '{' property_definition ( ',' property_definition )* '}'
529
+ -> ^( OBJECT property_definition+ )
530
+ ;
531
+
532
+ property_definition
533
+ : 'get'^ ID formal_parameter_list block
534
+ | 'set'^ ID formal_parameter_list block
535
+ | property_name ':'^ expression
536
+ ;
537
+
538
+ property_name
539
+ : ID
540
+ | reserved { $reserved.tree.token.type = ID }
541
+ | STRING
542
+ | NUMBER
543
+ ;
544
+
545
+ statement_end
546
+ : ';'
547
+ | ( '}' )=>
548
+ | EOF
549
+ ;
550
+
551
+ variable_name
552
+ : ID
553
+ | t=pseudokeyword { $t.tree.token.type = ID }
554
+ ;
555
+
556
+ pseudokeyword
557
+ : 'abstract'
558
+ | 'as'
559
+ | 'boolean'
560
+ | 'byte'
561
+ | 'char'
562
+ | 'class'
563
+ | 'debugger'
564
+ | 'double'
565
+ | 'each'
566
+ | 'enum'
567
+ | 'export'
568
+ | 'extends'
569
+ | 'final'
570
+ | 'float'
571
+ | 'get'
572
+ | 'set'
573
+ | 'goto'
574
+ | 'implements'
575
+ | 'import'
576
+ | 'int'
577
+ | 'interface'
578
+ | 'is'
579
+ | 'long'
580
+ | 'namespace'
581
+ | 'native'
582
+ | 'package'
583
+ | 'private'
584
+ | 'protected'
585
+ | 'public'
586
+ | 'short'
587
+ | 'static'
588
+ | 'super'
589
+ | 'synchronized'
590
+ | 'throws'
591
+ | 'transient'
592
+ | 'use'
593
+ | 'volatile'
594
+ ;
595
+
596
+ reserved
597
+ : 'abstract'
598
+ | 'as'
599
+ | 'boolean'
600
+ | 'break'
601
+ | 'byte'
602
+ | 'case'
603
+ | 'catch'
604
+ | 'char'
605
+ | 'class'
606
+ | 'const'
607
+ | 'continue'
608
+ | 'debugger'
609
+ | 'default'
610
+ | 'delete'
611
+ | 'do'
612
+ | 'double'
613
+ | 'each'
614
+ | 'else'
615
+ | 'enum'
616
+ | 'export'
617
+ | 'extends'
618
+ | 'false'
619
+ | 'final'
620
+ | 'finally'
621
+ | 'float'
622
+ | 'for'
623
+ | 'function'
624
+ | 'get'
625
+ | 'goto'
626
+ | 'if'
627
+ | 'implements'
628
+ | 'import'
629
+ | 'in'
630
+ | 'instanceof'
631
+ | 'int'
632
+ | 'interface'
633
+ | 'is'
634
+ | 'let'
635
+ | 'long'
636
+ | 'namespace'
637
+ | 'native'
638
+ | 'new'
639
+ | 'null'
640
+ | 'package'
641
+ | 'private'
642
+ | 'protected'
643
+ | 'public'
644
+ | 'return'
645
+ | 'set'
646
+ | 'short'
647
+ | 'static'
648
+ | 'super'
649
+ | 'switch'
650
+ | 'synchronized'
651
+ | 'this'
652
+ | 'throw'
653
+ | 'throws'
654
+ | 'transient'
655
+ | 'true'
656
+ | 'try'
657
+ | 'typeof'
658
+ | 'undefined'
659
+ | 'use'
660
+ | 'var'
661
+ | 'void'
662
+ | 'volatile'
663
+ | 'while'
664
+ | 'with'
665
+ | 'yield'
666
+ ;
667
+
668
+ /********************************************************************************
669
+ *********************************** Lexer ************************************
670
+ ********************************************************************************/
671
+
672
+ SLASH
673
+ : '//' ~( '\n' | '\r' | '\u2028' | '\u2029' )* { skip }
674
+ | '/*' .* '*/' { skip }
675
+ | { @regex_possible }?=> '/' ( ~( '/' | '*' | '\\' | '\r' | '\n' ) | '\\' . ) ( ~( '/' | '\\' | '\n' | '\r' ) | '\\' . )* '/' ( 'a'..'z' )* { $type = REGEX }
676
+ | { !@regex_possible }?=> '/' ( '=' { $type = SLASH_ASGN } | { $type = SLASH } )
677
+ ;
678
+
679
+ STRING
680
+ : '"' ( ~( '"' | '\\' | '\n' | '\r' ) | '\\' . )* '"'
681
+ | '\'' ( ~( '\'' | '\\' | '\n' | '\r' ) | '\\' . )* '\''
682
+ ;
683
+
684
+ NUMBER
685
+ : ('0'..'9')+ '.' ('0'..'9')* ( ('e' | 'E') ('+' | '-')? ('0'..'9')+ )?
686
+ | '.'? ('0'..'9')+ ( ('e' | 'E') ('+' | '-')? ('0'..'9')+ )?
687
+ | '0' ('x' | 'X') ( '0'..'9' | 'a'..'f' | 'A'..'F' )+
688
+ ;
689
+
690
+ NEWLINE
691
+ : ( '\n' | '\r' )+
692
+ { $channel = HIDDEN }
693
+ ;
694
+
695
+ ID
696
+ : ( '$' | '_' | 'a'..'z' | 'A'..'Z' )
697
+ ( 'a'..'z' | 'A'..'Z' | '0'..'9' | '_' | '$' )*
698
+ ;
699
+
700
+ WS // Tab, vertical tab, form feed, space, non-breaking space and any other unicode "space separator".
701
+ : ( '\t' | '\f' | ' ' | '\u00A0' )+ { $channel = HIDDEN }
702
+ ;