• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1grammar t042ast;
2options {
3    language = JavaScript;
4    output = AST;
5}
6
7tokens {
8    VARDEF;
9    FLOAT;
10    EXPR;
11    BLOCK;
12    VARIABLE;
13    FIELD;
14    CALL;
15    INDEX;
16    FIELDACCESS;
17}
18
19@header {
20org.antlr.lang.map = function(a, fn) {
21    var i, len, r=[];
22    for (i=0, len=a.length; i<len; i++) {
23        r.push(fn(a[i], i));
24    }
25    return r;
26};
27}
28
29r1
30    : INT ('+'^ INT)*
31    ;
32
33r2
34    : 'assert'^ x=expression (':'! y=expression)? ';'!
35    ;
36
37r3
38    : 'if'^ expression s1=statement ('else'! s2=statement)?
39    ;
40
41r4
42    : 'while'^ expression statement
43    ;
44
45r5
46    : 'return'^ expression? ';'!
47    ;
48
49r6
50    : (INT|ID)+
51    ;
52
53r7
54    : INT ->
55    ;
56
57r8
58    : 'var' ID ':' type -> ^('var' type ID)
59    ;
60
61r9
62    : type ID ';' -> ^(VARDEF type ID)
63    ;
64
65r10
66    : INT -> {new org.antlr.runtime.tree.CommonTree(new org.antlr.runtime.CommonToken(FLOAT, $INT.text + ".0"))}
67    ;
68
69r11
70    : expression -> ^(EXPR expression)
71    | -> EXPR
72    ;
73
74r12
75    : ID (',' ID)* -> ID+
76    ;
77
78r13
79    : type ID (',' ID)* ';' -> ^(type ID+)
80    ;
81
82r14
83    :   expression? statement* type+
84        -> ^(EXPR expression? statement* type+)
85    ;
86
87r15
88    : INT -> INT INT
89    ;
90
91r16
92    : 'int' ID (',' ID)* -> ^('int' ID)+
93    ;
94
95r17
96    : 'for' '(' start=statement ';' expression ';' next=statement ')' statement
97        -> ^('for' $start expression $next statement)
98    ;
99
100r18
101    : t='for' -> ^(BLOCK)
102    ;
103
104r19
105    : t='for' -> ^(BLOCK[$t])
106    ;
107
108r20
109    : t='for' -> ^(BLOCK[$t,"FOR"])
110    ;
111
112r21
113    : t='for' -> BLOCK
114    ;
115
116r22
117    : t='for' -> BLOCK[$t]
118    ;
119
120r23
121    : t='for' -> BLOCK[$t,"FOR"]
122    ;
123
124r24
125    : r=statement expression -> ^($r expression)
126    ;
127
128r25
129    : r+=statement (',' r+=statement)+ expression -> ^($r expression)
130    ;
131
132r26
133    : r+=statement (',' r+=statement)+ -> ^(BLOCK $r+)
134    ;
135
136r27
137    : r=statement expression -> ^($r ^($r expression))
138    ;
139
140r28
141    : ('foo28a'|'foo28b') ->
142    ;
143
144r29
145    : (r+=statement)* -> ^(BLOCK $r+)
146    ;
147
148r30
149    : statement* -> ^(BLOCK statement?)
150    ;
151
152r31
153    : modifier type ID ('=' expression)? ';'
154        -> {this.flag === 0}? ^(VARDEF ID modifier* type expression?)
155        -> {this.flag === 1}? ^(VARIABLE ID modifier* type expression?)
156        ->                   ^(FIELD ID modifier* type expression?)
157    ;
158
159r32[which]
160  : ID INT -> {which==1}? ID
161           -> {which==2}? INT
162           -> // yield nothing as else-clause
163  ;
164
165r33
166    :   modifiers! statement
167    ;
168
169r34
170    :   modifiers! r34a[$modifiers.tree]
171    //|   modifiers! r33b[$modifiers.tree]
172    ;
173
174r34a[mod]
175    :   'class' ID ('extends' sup=type)?
176        ( 'implements' i+=type (',' i+=type)*)?
177        '{' statement* '}'
178        -> ^('class' ID {$mod} ^('extends' $sup)? ^('implements' $i+)? statement* )
179    ;
180
181r35
182    : '{' 'extends' (sup=type)? '}'
183        ->  ^('extends' $sup)?
184    ;
185
186r36
187    : 'if' '(' expression ')' s1=statement
188        ( 'else' s2=statement -> ^('if' ^(EXPR expression) $s1 $s2)
189        |                     -> ^('if' ^(EXPR expression) $s1)
190        )
191    ;
192
193r37
194    : (INT -> INT) ('+' i=INT -> ^('+' $r37 $i) )*
195    ;
196
197r38
198    : INT ('+'^ INT)*
199    ;
200
201r39
202    : (primary->primary) // set return tree to just primary
203        ( '(' arg=expression ')'
204            -> ^(CALL $r39 $arg)
205        | '[' ie=expression ']'
206            -> ^(INDEX $r39 $ie)
207        | '.' p=primary
208            -> ^(FIELDACCESS $r39 $p)
209        )*
210    ;
211
212r40
213    : (INT -> INT) ( ('+' i+=INT)* -> ^('+' $r40 $i*) ) ';'
214    ;
215
216r41
217    : (INT -> INT) ( ('+' i=INT) -> ^($i $r41) )* ';'
218    ;
219
220r42
221    : ids+=ID (','! ids+=ID)*
222    ;
223
224r43 returns [res]
225    : ids+=ID! (','! ids+=ID!)* {$res = org.antlr.lang.map($ids, function(id) { return id.getText(); });}
226    ;
227
228r44
229    : ids+=ID^ (','! ids+=ID^)*
230    ;
231
232r45
233    : primary^
234    ;
235
236r46 returns [res]
237    : ids+=primary! (','! ids+=primary!)* {$res = org.antlr.lang.map($ids, function(id) { return id.getText(); });}
238    ;
239
240r47
241    : ids+=primary (','! ids+=primary)*
242    ;
243
244r48
245    : ids+=. (','! ids+=.)*
246    ;
247
248r49
249    : .^ ID
250    ;
251
252r50
253    : ID
254        -> ^({new org.antlr.runtime.tree.CommonTree(new org.antlr.runtime.CommonToken(FLOAT, "1.0"))} ID)
255    ;
256
257/** templates tested:
258    tokenLabelPropertyRef_tree
259*/
260r51 returns [res]
261    : ID t=ID ID
262        { $res = $t.tree; }
263    ;
264
265/** templates tested:
266    rulePropertyRef_tree
267*/
268r52 returns [res]
269@after {
270    $res = $tree;
271}
272    : ID
273    ;
274
275/** templates tested:
276    ruleLabelPropertyRef_tree
277*/
278r53 returns [res]
279    : t=primary
280        { $res = $t.tree; }
281    ;
282
283/** templates tested:
284    ruleSetPropertyRef_tree
285*/
286r54 returns [res]
287@after {
288    $tree = $t.tree;;
289}
290    : ID t=expression ID
291    ;
292
293/** backtracking */
294r55
295options { backtrack=true; k=1; }
296    : (modifier+ INT)=> modifier+ expression
297    | modifier+ statement
298    ;
299
300
301/** templates tested:
302    rewriteTokenRef with len(args)>0
303*/
304r56
305    : t=ID* -> ID[$t,'foo']
306    ;
307
308/** templates tested:
309    rewriteTokenRefRoot with len(args)>0
310*/
311r57
312    : t=ID* -> ^(ID[$t,'foo'])
313    ;
314
315/** templates tested:
316    ???
317*/
318r58
319    : ({new org.antlr.runtime.tree.CommonTree(new org.antlr.runtime.CommonToken(FLOAT, "2.0"))})^
320    ;
321
322/** templates tested:
323    rewriteTokenListLabelRefRoot
324*/
325r59
326    : (t+=ID)+ statement -> ^($t statement)+
327    ;
328
329primary
330    : ID
331    ;
332
333expression
334    : r1
335    ;
336
337statement
338    : 'fooze'
339    | 'fooze2'
340    ;
341
342modifiers
343    : modifier+
344    ;
345
346modifier
347    : 'public'
348    | 'private'
349    ;
350
351type
352    : 'int'
353    | 'bool'
354    ;
355
356ID : 'a'..'z' + ;
357INT : '0'..'9' +;
358WS: (' ' | '\n' | '\t')+ {$channel = HIDDEN;};
359
360