src: remove handling of `is` (AEeExpr)
[nit.git] / src / parser / nit.sablecc3xx
1
2 /* This file is part of NIT ( http://www.nitlanguage.org ).
3  *
4  * Copyright 2008-2009 Jean Privat <jean@pryen.org>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 /* This grammar defines the NIT language. */
20 Package org.nitlanguage.gen;
21
22 /*****************************************************************************/
23 Helpers
24 /*****************************************************************************/
25
26 all = [0 .. 0xFF];
27 lowercase = ['a' .. 'z'];
28 uppercase = ['A' .. 'Z'];
29 digit = ['0' .. '9'];
30 letter = lowercase | uppercase | digit | '_';
31
32 tab = 9;
33 cr = 13;
34 lf = 10;
35 any = [all - [cr + lf]];
36 eol_helper = cr lf | cr | lf; // This takes care of different platforms
37
38 // characers inside strings and super-strings (atomaton powaa)
39 str_char
40         = [any - [['"' + '{'] + '\']] 
41         | '\' any 
42         ;
43 str_body = str_char*;
44
45 sstr_char
46         = [any - [''' + '\']]
47         | '\' any
48         ;
49
50 sstr_body = sstr_char*;
51
52 long_str_char = str_char | cr | lf;
53
54 long_str_part
55         = long_str_char
56         | '"' long_str_char
57         | '"' '"' long_str_char
58         | '{' long_str_char
59         | '{' '{' long_str_char
60         ;
61
62 long_str_body = long_str_part*;
63
64 long_sstr_char = sstr_char | cr | lf;
65 long_sstr_part
66         = long_sstr_char
67         | ''' long_sstr_char
68         | ''' ''' long_sstr_char
69         ;
70
71 long_sstr_body = long_sstr_part*;
72
73
74 extern_code_char
75         = [all - ['`' + '\']]
76         | '\' all
77         ;
78 extern_code_body = extern_code_char*;
79
80 /*****************************************************************************/
81 States
82 /*****************************************************************************/
83 initial;
84
85
86 /*****************************************************************************/
87 Tokens
88 /*****************************************************************************/
89
90 blank = (' ' | tab)+;
91
92 eol = eol_helper;
93 comment = '#' any* eol_helper?;
94
95 kwpackage = 'package';
96 kwmodule = 'module';
97 kwimport = 'import';
98 kwclass = 'class';
99 kwabstract = 'abstract';
100 kwinterface = 'interface';
101 kwenum = 'universal'|'enum';
102 kwend = 'end';
103 kwmeth = 'fun';
104 kwtype = 'type';
105 kwinit = 'init';
106 kwredef = 'redef';
107 kwis = 'is';
108 kwdo = 'do';
109 kwreadable = 'readable';
110 kwwritable = 'writable';
111 kwvar = 'var';
112 kwintern = 'intern';
113 kwextern = 'extern';
114 kwprotected = 'protected';
115 kwprivate = 'private';
116 kwintrude = 'intrude';
117 kwif = 'if';
118 kwthen = 'then';
119 kwelse = 'else';
120 kwwhile = 'while';
121 kwloop = 'loop';
122 kwfor = 'for';
123 kwin = 'in';
124 kwand = 'and';
125 kwor = 'or';
126 kwnot = 'not';
127 kwimplies = 'implies';
128 kwreturn = 'return';
129 kwcontinue = 'continue';
130 kwbreak = 'break';
131 kwabort = 'abort';
132 kwassert = 'assert';
133 kwnew = 'new';
134 kwisa = 'isa';
135 kwonce = 'once';
136 kwsuper = 'super';
137 kwself = 'self';
138 kwtrue = 'true';
139 kwfalse = 'false';
140 kwnull = 'null';
141 kwas = 'as';
142 kwnullable = 'nullable';
143 kwisset = 'isset';
144 kwlabel = 'label';
145 kwdebug = '__debug__';
146
147 opar = '(';
148 cpar = ')';
149 obra = '[';
150 cbra = ']';
151 comma = ',';
152 column = ':';
153 quad = '::';
154 assign = '=';
155 pluseq = '+=';
156 minuseq = '-=';
157 dotdotdot = '...';
158 dotdot = '..';
159 dot = '.';
160 plus = '+';
161 minus = '-';
162 star = '*';
163 slash = '/';
164 percent = '%';
165 eq = '==';
166 ne = '!=';
167 lt = '<';
168 le = '<=';
169 ll = '<<';
170 gt = '>';
171 ge = '>=';
172 gg = '>>';
173 starship = '<=>';
174 bang='!';
175 at='@';
176
177 classid = uppercase letter*;
178 id = lowercase letter*;
179 attrid = '_' lowercase letter*;
180
181 number = digit+;
182 float = digit* '.' digit+;
183 string = '"' str_body '"' | '"' '"' '"' long_str_body '"' '"' '"' | ''' ''' ''' long_sstr_body ''' ''' ''';
184 start_string = '"' str_body '{' | '"' '"' '"' long_str_body '{' '{' '{';
185 mid_string = '}' str_body '{' | '}' '}' '}' long_str_body '{' '{' '{';
186 end_string = '}' str_body '"' | '}' '}' '}' long_str_body '"' '"' '"';
187 char = (''' [[any - '''] - '\'] ''') | (''' '\' any ''');
188 bad_string = ('"'|'}') str_body | '"' '"' '"' long_str_body | ''' ''' ''' long_sstr_body;
189 bad_char = ''' '\'? any;
190
191 extern_code_segment = '`' '{' extern_code_body '`' '}';
192
193 /*****************************************************************************/
194 Ignored Tokens
195 /*****************************************************************************/
196
197 blank;
198
199 /*****************************************************************************/
200 Productions
201 /*****************************************************************************/
202
203 /* MODULES *******************************************************************/
204 module
205         = moduledecl? [imports]:import* [extern_bodies]:extern_code_body* [classdefs]:classdef* implicit_top_class? implicit_main_class? {-> New module(moduledecl, [imports.import], [extern_bodies.extern_code_block], [classdefs.classdef,implicit_top_class.classdef,implicit_main_class.classdef])};
206
207 moduledecl
208         = [doc]:no kwmodule no module_name annotation_withend? [n2]:n1 {-> New moduledecl(doc.doc, kwmodule, module_name, annotation_withend.annotations)};
209
210 import
211         = {std} [doc]:no visibility kwimport no module_name annotation_withend? [n2]:n1 {-> New import.std(visibility, kwimport, module_name, annotation_withend.annotations)}
212         | {no} [doc]:no visibility kwimport no kwend [n2]:n1 {-> New import.no(visibility, kwimport, kwend)}
213         ;
214
215 implicit_top_class {-> classdef}
216         = propdefs_toplevel+ {-> New classdef.top([propdefs_toplevel.propdef])};
217
218 implicit_main_class {-> classdef?}
219         = implicit_main_meth {-> New classdef.main([implicit_main_meth.propdef])}
220         | {null} n {-> Null}
221         ;
222 implicit_main_meth {-> propdef}
223         = [doc]:no stmts {-> New propdef.main_meth(Null, stmts.expr)}
224         | {n} [doc]:no stmtsn {-> New propdef.main_meth(Null, stmtsn.expr)}
225         ;
226
227 /* CLASSES *******************************************************************/
228 classdef
229         = [doc]:no redef visibility classkind no qualified? classid formaldefs? line_annotations_forclass? extern_code_block? [superclasses]:superclass* propdefs* [n2]:no kwend {-> New classdef.std(doc.doc, redef.kwredef, visibility, classkind, classid, [formaldefs.formaldef], line_annotations_forclass.annotations, extern_code_block, [superclasses.superclass], [propdefs.propdef], kwend)};
230 redef {-> kwredef?}
231         = kwredef? {-> kwredef};
232 classkind
233         = {concrete} kwclass
234         | {abstract} kwabstract kwclass
235         | {interface} kwinterface
236         | {enum} kwenum
237         | {extern} kwextern kwclass?
238         ;
239
240 formaldefs {-> formaldef*}
241         = obra no formaldef formaldefs_tail* [n2]:no cbra {-> [formaldef, formaldefs_tail.formaldef]};
242 formaldefs_tail {-> formaldef}
243         = comma no formaldef {-> formaldef};
244 formaldef
245         = classid annotations? typing? {-> New formaldef(classid, typing.type, annotations)};
246
247 superclass {-> superclass}
248         = {super} no kwsuper [n2]:no type annotation_withend? {-> New superclass(kwsuper, type, annotation_withend.annotations)}
249         ;
250
251 propdefs~toplevel {-> propdef}
252         = propdef~toplevel n1 {-> propdef~toplevel.propdef}
253         ;
254 propdef~toplevel {-> propdef}
255         = {meth} [doc]:no redef visibility kwmeth methid signature annotation_noend? kwdo stmtso kwend? {-> New propdef.concrete_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature, annotation_noend.annotations, stmtso.expr)}
256         | {assign_return} [doc]:no redef visibility kwmeth methid signature_withret assign no assign_return {-> New propdef.concrete_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature_withret.signature, Null, assign_return.expr)}
257         | {nobody} [doc]:no redef visibility kwmeth methid signature annotation_withend {-> New propdef.deferred_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature.signature, annotation_withend.annotations)}
258 !toplevel| {deferred} [doc]:no redef visibility kwmeth methid signature kwis kwabstract {-> New propdef.deferred_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature.signature, Null)}
259 !toplevel| {intern} [doc]:no redef visibility kwmeth methid signature kwis kwintern {-> New propdef.intern_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature.signature)}
260         | {extern} [doc]:no redef visibility kwmeth methid signature kwis kwextern string? extern_calls? extern_code_block? {-> New propdef.extern_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature.signature, string, extern_calls, extern_code_block)}
261         | {extern_implicit} [doc]:no redef visibility kwmeth methid signature extern_calls? extern_code_block {-> New propdef.extern_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature.signature, Null, extern_calls, extern_code_block)}
262 !toplevel| {var} [doc]:no readable? writable? redef visibility kwvar attrid typing? {-> New propdef.attr(doc.doc, readable.able, writable.able, redef.kwredef, visibility, kwvar, attrid, Null, typing.type, Null, Null)}
263 !toplevel| {var2} [doc]:no readable? writable? redef visibility kwvar attrid typing? assign [n2]:no expr {-> New propdef.attr(doc.doc, readable.able, writable.able, redef.kwredef, visibility, kwvar, attrid, Null, typing.type, Null, expr)}
264 !toplevel| {var3} [doc]:no redef visibility kwvar id typing? writable2? annotation_withend? {-> New propdef.attr(doc.doc, Null, writable2.able, redef.kwredef, visibility, kwvar, Null, id, typing.type, annotation_withend.annotations, Null)}
265 !toplevel| {var4} [doc]:no redef visibility kwvar id typing? writable2? assign [n2]:no expr_nois annotation_withend? {-> New propdef.attr(doc.doc, Null, writable2.able, redef.kwredef, visibility, kwvar, Null, id, typing.type, annotation_withend.annotations, expr_nois.expr)}
266 !toplevel| {init} [doc]:no redef visibility kwinit methid? signature annotation_noend? kwdo stmtso kwend? {-> New propdef.concrete_init(doc.doc, redef.kwredef, visibility, kwinit, methid, signature, annotation_noend.annotations, stmtso.expr)}
267 !toplevel| {type} [doc]:no redef visibility kwtype classid typing annotation_withend? {-> New propdef.type(doc.doc, redef.kwredef, visibility, kwtype, classid, typing.type, annotation_withend.annotations)}
268 !toplevel| {extern_init} [doc]:no redef visibility kwnew methid? signature kwis kwextern string? extern_calls? extern_code_block? {-> New propdef.extern_init(doc.doc, redef.kwredef, visibility, kwnew, methid, signature, string, extern_calls, extern_code_block)}
269 !toplevel| {extern_init_implicit} [doc]:no redef visibility kwnew methid? signature string? extern_calls? extern_code_block {-> New propdef.extern_init(doc.doc, redef.kwredef, visibility, kwnew, methid, signature, string, extern_calls, extern_code_block)}
270         ;
271 annotation_withend {-> annotations}
272         = {oneliner} kwis many_annotations {-> many_annotations.annotations}
273         | {more} kwis n1 line_annotations kwend {-> line_annotations.annotations}
274         ;
275 annotation_noend {-> annotations}
276         = {oneliner} kwis many_annotations {-> many_annotations.annotations}
277         | {more} kwis n1 line_annotations {-> line_annotations.annotations}
278         ;
279 readable {-> able}
280         = redef kwreadable {-> New able.read(redef.kwredef, kwreadable)}
281         ;
282 writable {-> able}
283         = redef kwwritable {-> New able.write(redef.kwredef, Null, kwwritable)}
284         ;
285 writable2 {-> able}
286         = redef visibility kwwritable {-> New able.write(redef.kwredef, visibility, kwwritable)}
287         ;
288
289 visibility
290         = {public} {-> New visibility.public()}
291         | {private} kwprivate no {-> New visibility.private(kwprivate)}
292         | {protected} kwprotected no {-> New visibility.protected(kwprotected)}
293         | {intrude} kwintrude no {-> New visibility.intrude(kwintrude)}
294         ;
295
296 methid {-> methid}
297         = {id} id {-> New methid.id(id)}
298         | {plus} plus {-> New methid.plus(plus)}
299         | {minus} minus {-> New methid.minus(minus)}
300         | {star} star {-> New methid.star(star)}
301         | {slash} slash {-> New methid.slash(slash)}
302         | {percent} percent {-> New methid.percent(percent)}
303         | {eq} eq {-> New methid.eq(eq)}
304         | {ne} ne {-> New methid.ne(ne)}
305         | {le} le {-> New methid.le(le)}
306         | {ge} ge {-> New methid.ge(ge)}
307         | {lt} lt {-> New methid.lt(lt)}
308         | {gt} gt {-> New methid.gt(gt)}
309         | {ll} ll {-> New methid.ll(ll)}
310         | {gg} gg {-> New methid.gg(gg)}
311         | {bra} obra cbra {-> New methid.bra(obra, cbra)}
312         | {starship} starship {-> New methid.starship(starship)}
313         | {assign} id assign {-> New methid.assign(id, assign)}
314         | {braassign} obra cbra assign {-> New methid.braassign(obra, cbra, assign)}
315         ;
316
317 signature~withret {-> signature}
318         = opar no params? cpar typing [no2]:no {-> New signature(opar, [params.param], cpar, typing.type)}
319 !withret| {noret} opar no params? cpar [no2]:no {-> New signature(opar, [params.param], cpar, Null)}
320         | {nopar} typing no {-> New signature(Null, [], Null, typing.type)}
321 !withret| {noparnoret} no {-> New signature(Null, [], Null, Null)}
322         ;
323
324 params {-> param*} 
325         = param params_tail* [n2]:no {-> [param, params_tail.param] }
326         ;
327 params_tail {-> param}
328         = comma no param {-> param};
329 param
330         = {untyped} id annotations? {-> New param(id, Null, Null, annotations)}
331         | id annotations? typing dotdotdot? {-> New param(id, typing.type, dotdotdot, annotations)}
332         ;
333
334 assign_return{-> expr}
335         = expr_final {-> New expr.return(Null, expr_final.expr)}
336         ;
337
338 extern_calls {-> extern_calls}
339         = kwimport no extern_call extern_call_tail* {-> New extern_calls( kwimport, [extern_call, extern_call_tail.extern_call] )}
340         ;
341 extern_call_tail {-> extern_call}
342         = comma no extern_call {-> extern_call};
343 extern_call {-> extern_call}
344     = {prop} extern_call_prop {-> extern_call_prop.extern_call}
345     | {cast} extern_call_cast {-> extern_call_cast.extern_call}
346         | {super} kwsuper {-> New extern_call.super( kwsuper )}
347     ;
348 extern_call_prop {-> extern_call}
349     = {local} methid {-> New extern_call.local_prop( methid )}
350         | {full} classid quad methid {-> New extern_call.full_prop( classid, quad, methid )}
351         | {init} classid {-> New extern_call.init_prop( classid )}
352         ;
353 extern_call_cast {-> extern_call}
354     = {as_cast} [from_type]:type kwas [n2]:no opar [n3]:no [to_type]:type [n4]:no cpar {-> New extern_call.cast_as(from_type, kwas, to_type)}
355         | {as_nullable} type kwas [n2]:no kwnullable {-> New extern_call.as_nullable( type, kwas, kwnullable)}
356         | {as_not_nullable} type kwas [n2]:no kwnot [n3]:no kwnullable {-> New extern_call.as_not_nullable( type, kwas, kwnot, kwnullable)}
357         ;
358
359 in_language = kwin string;
360 extern_code_block = in_language? extern_code_segment;
361 extern_code_body {-> extern_code_block} = no extern_code_block {-> extern_code_block};
362
363 /* TYPES *********************************************************************/
364 type~nobra~nopar {-> type}
365         = {simple} kwnullable? classid annotations~nopar? {-> New type(kwnullable, classid, [], annotations~nopar.annotations)}
366 !nobra  | {generic} kwnullable? classid obra no types [n2]:no cbra annotations~nopar? {-> New type(kwnullable, classid, [types.type], annotations~nopar.annotations)}
367         ;
368 types {-> type*} 
369         = type types_tail* {-> [type, types_tail.type]};
370 types_tail {-> type}
371         = comma no type {-> type};
372 typing {-> type}
373         = column no type {-> type};
374
375 /* STATMENTS *****************************************************************/
376 stmtso~withelse~withend {-> expr?}
377         = {block} n stmtsnend {-> stmtsnend.expr}
378         | {emptyblock} n kwend {-> New expr.block([], kwend)}
379         | {emptyoneline} kwend {-> New expr.block([], kwend)}
380 !withend| {oneline} stmt~withelse {-> stmt~withelse.expr}
381         ;
382 stmts {-> expr}
383         = stmt stmts_tail* {-> New expr.block([stmt.expr, stmts_tail.expr], Null)};
384 stmtsn {-> expr}
385         = stmt stmts_tail* n {-> New expr.block([stmt.expr, stmts_tail.expr], Null)};
386 stmtsnend {-> expr}
387         = stmt stmts_tail* n kwend {-> New expr.block([stmt.expr, stmts_tail.expr], kwend)};
388 stmts_tail {-> expr}
389         = n stmt {-> stmt.expr};
390 stmt~withelse~noexpr~nopar {-> expr}
391         = {vardecl} vardecl~withelse {-> vardecl~withelse.expr}
392         | {assign} assignment~withelse~nopar {-> assignment~withelse~nopar.expr}
393         | {return} kwreturn expr_final~withelse? {-> New expr.return(kwreturn, expr_final~withelse.expr)}
394         | {break} kwbreak label? expr_final~withelse? {-> New expr.break(kwbreak, label, expr_final~withelse.expr)}
395         | {abort} kwabort {-> New expr.abort(kwabort)}
396         | {continue} kwcontinue label? expr_final~withelse? {-> New expr.continue(kwcontinue, label, expr_final~withelse.expr)}
397         | {do} do~withelse {-> do~withelse.expr}
398 !noexpr | {if} if~withelse {-> if~withelse.expr}
399         | {while} while~withelse {-> while~withelse.expr}
400         | {loop} loop~withelse {-> loop~withelse.expr}
401         | {for} for~withelse {-> for~withelse.expr}
402         | {assert} assert~withelse {-> assert~withelse.expr}
403 !noexpr | {call} recv qualified? id args_nopar {-> New expr.call(recv.expr, id, args_nopar.exprs)}
404 !noexpr | {super} qualified? kwsuper args_nopar {-> New expr.super(qualified, kwsuper, args_nopar.exprs)}
405 !noexpr | {init} recv qualified? kwinit args_nopar {-> New expr.init(recv.expr, kwinit, args_nopar.exprs)}
406         | {debug_type_is} kwdebug kwtype type column expr_final~withelse {-> New expr.debug_type(kwdebug, kwtype, expr_final~withelse.expr, type) }
407         ;
408
409 label= kwlabel id;
410
411 assign_continue~withelse{-> expr}
412         = expr_final~withelse {-> New expr.continue(Null, Null, expr_final~withelse.expr)}
413         ;
414
415 vardecl~withelse{-> expr}
416         = kwvar id annotations? typing? {-> New expr.vardecl(kwvar, id, typing.type, Null, Null, annotations)}
417         | {assign} kwvar id annotations? typing? assign no expr_final~withelse {-> New expr.vardecl(kwvar, id, typing.type, assign, expr_final~withelse.expr, annotations)}
418         ;
419
420 assignment~withelse~nopar {-> expr}
421         = {attr} recv~nopar qualified? attrid assign expr_final~withelse {-> New expr.attr_assign(recv~nopar.expr, attrid, assign, expr_final~withelse.expr)}
422         | {call} recv~nopar qualified? id args assign expr_final~withelse {-> New expr.call_assign(recv~nopar.expr, id, args.exprs, assign,  expr_final~withelse.expr)}
423         | {bra} expr_atom~nopar braargs assign expr_final~withelse {-> New expr.bra_assign(expr_atom~nopar.expr, braargs.exprs, assign,  expr_final~withelse.expr)}
424         | {attr_re} recv~nopar qualified? attrid assign_op expr_final~withelse {-> New expr.attr_reassign(recv~nopar.expr, attrid, assign_op,  expr_final~withelse.expr)}
425         | {call_re} recv~nopar qualified? id args assign_op expr_final~withelse {-> New expr.call_reassign(recv~nopar.expr, id, args.exprs, assign_op,  expr_final~withelse.expr)}
426         | {bra_re} expr_atom~nopar braargs assign_op expr_final~withelse {-> New expr.bra_reassign(expr_atom~nopar.expr, braargs.exprs, assign_op,  expr_final~withelse.expr)}
427         ;
428 assign_op
429         = {plus} pluseq
430         | {minus} minuseq
431         ;
432
433 do~withelse {-> expr}
434         = kwdo stmtso_withend label {-> New expr.do(kwdo, stmtso_withend.expr, label)}
435         | {nolabel} kwdo stmtso~withelse {-> New expr.do(kwdo, stmtso~withelse.expr, Null)}
436         ;
437
438 if~withelse {-> expr}
439         = {onelineelse} kwif no expr [n2]:no kwthen stmt_withelse kwelse stmtso~withelse {-> New expr.if(kwif, expr, stmt_withelse.expr, stmtso~withelse.expr)}
440 !withelse       | {oneline} kwif no expr [n2]:no kwthen stmt {-> New expr.if(kwif, expr, stmt.expr, Null)}
441 !withelse       | {block} kwif no expr [n2]:no kwthen [n3]:n stmtsn elsepartblock {-> New expr.if(kwif, expr, stmtsn.expr, elsepartblock.expr)}
442 !withelse       | {emptyblock} kwif no expr [n2]:no kwthen [n3]:n? elsepartblock {-> New expr.if(kwif, expr, Null, elsepartblock.expr)}
443         ;
444 elsepartblock {-> expr?}
445         = {else} kwelse stmtso {-> stmtso.expr}
446         | {empty} kwend {-> New expr.block([], kwend)}
447         ;
448
449 loop~withelse {-> expr}
450         = kwloop stmtso_withend label {-> New expr.loop(kwloop, stmtso_withend.expr, label)}
451         | {nolabel} kwloop stmtso~withelse {-> New expr.loop(kwloop, stmtso~withelse.expr, Null)}
452         ;
453
454 while~withelse {-> expr}
455         = kwwhile no expr [n2]:no kwdo stmtso_withend label {-> New expr.while(kwwhile, expr, kwdo, stmtso_withend.expr, label)}
456         | {nolabel} kwwhile no expr [n2]:no kwdo stmtso~withelse {-> New expr.while(kwwhile, expr, kwdo, stmtso~withelse.expr, Null)}
457         ;
458
459 for~withelse {-> expr}
460         = kwfor no [ids]:idlist [n2]:no kwin [n3]:no expr [n4]:no kwdo stmtso_withend label {-> New expr.for(kwfor, [ids.id], expr, kwdo, stmtso_withend.expr, label)}
461         | {nolabel} kwfor no [ids]:idlist [n2]:no kwin [n3]:no expr [n4]:no kwdo stmtso~withelse {-> New expr.for(kwfor, [ids.id], expr, kwdo, stmtso~withelse.expr, Null)}
462         ;
463
464 assert~withelse {-> expr}
465         = {else} kwassert assertid? expr_final_withelse kwelse stmtso~withelse {-> New expr.assert(kwassert, assertid.id, expr_final_withelse.expr, stmtso~withelse.expr)}
466 !withelse| {noelse} kwassert assertid? expr_final {-> New expr.assert(kwassert, assertid.id, expr_final.expr, Null)}
467         ;
468 assertid {-> id}
469         = id column {-> id};
470
471 /* EXPRESSIONS ***************************************************************/
472 expr_final~nopar~withelse~nobra {-> expr}
473         = expr~nopar~nobra {-> expr~nopar~nobra.expr}
474         ;
475
476 expr~nopar~nobra~nois {-> expr}
477         = expr_and~nopar~nobra~nois {-> expr_and~nopar~nobra~nois.expr}
478         | {ifexpr} kwif [n1]:no expr [n2]:no kwthen [n3]:no [then]:expr [n4]:no kwelse [n5]:no [else]:expr~nopar~nobra~nois {-> New expr.ifexpr(kwif, expr, kwthen, then, kwelse, else.expr)}
479         ;
480
481 expr_and~nopar~nobra~nois {-> expr}
482         = expr_not~nopar~nobra~nois {-> expr_not~nopar~nobra~nois.expr}
483         | {:or} expr_and~nopar~nobra~nois :kwor :no expr_not~nopar~nobra~nois
484         | {:and} expr_and~nopar~nobra~nois :kwand :no expr_not~nopar~nobra~nois
485         | {:or_else} expr_and~nopar~nobra~nois :kwor :kwelse :no expr_not~nopar~nobra~nois
486         | {:implies} expr_and~nopar~nobra~nois :kwimplies :no expr_not~nopar~nobra~nois
487         ;
488
489 expr_not~nopar~nobra~nois {-> expr}
490         = expr_eq~nopar~nobra~nois {-> expr_eq~nopar~nobra~nois.expr}
491         | {not} kwnot no expr_not~nopar~nobra~nois {-> New expr.not(kwnot, expr_not~nopar~nobra~nois.expr)}
492         ;
493
494 expr_eq~nopar~nobra~nois {-> expr}
495         = expr_add~nopar~nobra {-> expr_add~nopar~nobra.expr}
496         | {:eq} expr_add~nopar~nobra :eq :no [expr2]:expr_add~nopar~nobra
497 !nois   | {ee} [expr]:expr_add~nopar~nobra kwis no [expr2]:expr_add~nopar~nobra {-> New expr.ee(expr.expr, expr2.expr)}
498         | {:ne} expr_add~nopar~nobra :ne :no [expr2]:expr_add~nopar~nobra
499         | {:lt} expr_add~nopar~nobra :lt :no [expr2]:expr_add~nopar~nobra
500         | {:le} expr_add~nopar~nobra :le :no [expr2]:expr_add~nopar~nobra
501         | {:ll} expr_eq~nopar~nobra~nois :ll :no [expr2]:expr_add~nopar~nobra
502         | {:gt} expr_add~nopar~nobra :gt :no [expr2]:expr_add~nopar~nobra
503         | {:ge} expr_add~nopar~nobra :ge :no [expr2]:expr_add~nopar~nobra
504         | {:gg} expr_eq~nopar~nobra~nois :gg :no [expr2]:expr_add~nopar~nobra
505         | {:starship} expr_add~nopar~nobra :starship :no [expr2]:expr_add~nopar~nobra
506         | {:isa} expr_add~nopar~nobra :kwisa :no type~nobra
507         ;
508
509 expr_add~nopar~nobra {-> expr}
510         =  expr_mul~nopar~nobra {-> expr_mul~nopar~nobra.expr}
511         | {:plus} expr_add~nopar~nobra :plus :no [expr2]:expr_mul~nopar~nobra
512         | {:minus} expr_add~nopar~nobra :minus :no [expr2]:expr_mul~nopar~nobra
513         ;
514
515 expr_mul~nopar~nobra {-> expr}
516         = expr_minus~nopar~nobra {-> expr_minus~nopar~nobra.expr}
517         | {:star} expr_mul~nopar~nobra :star :no [expr2]:expr_minus~nopar~nobra
518         | {:slash} expr_mul~nopar~nobra :slash :no [expr2]:expr_minus~nopar~nobra
519         | {:percent} expr_mul~nopar~nobra :percent :no [expr2]:expr_minus~nopar~nobra
520         ;
521
522 expr_minus~nopar~nobra {-> expr}
523         = expr_new~nopar~nobra {-> expr_new~nopar~nobra.expr}
524         | {:uminus} minus :no expr_minus~nopar~nobra
525         | {:once} kwonce :no expr_minus~nopar~nobra
526         ;
527
528 expr_new~nopar~nobra {-> expr}
529         = expr_atom~nopar~nobra {-> expr_atom~nopar~nobra.expr}
530         | {new} kwnew no type~nobra_nopar args {-> New expr.new(kwnew, type~nobra_nopar.type, Null, args.exprs)}
531         | {isset_attr} kwisset recv~nopar~nobra qualified? attrid {-> New expr.isset_attr(kwisset, recv~nopar~nobra.expr, attrid)}
532         ;
533
534 expr_atom~nopar~nobra {-> expr}
535         = {attr} recv~nopar~nobra qualified? attrid {-> New expr.attr(recv~nopar~nobra.expr, attrid)}
536         | {call} recv~nopar~nobra qualified? id args {-> New expr.call(recv~nopar~nobra.expr, id, args.exprs)}
537         | {super} qualified? kwsuper args {-> New expr.super(qualified, kwsuper, args.exprs)}
538         | {init} recv~nopar~nobra kwinit args {-> New expr.init(recv~nopar~nobra.expr, kwinit, args.exprs)}
539 !nobra  | {bra} expr_atom~nopar braargs {-> New expr.bra(expr_atom~nopar.expr, braargs.exprs)}
540         | {new} kwnew no type~nobra_nopar dot [n2]:no qualified? id args {-> New expr.new(kwnew, type~nobra_nopar.type, id, args.exprs)}
541 // !nopar to unambiguise 'foo[5].bar' between '(foo[5]).bar' and 'foo([5].bar),
542 !nobra!nopar    | {range} obra no expr [n2]:no dotdot [n3]:no [expr2]:expr_nobra [n4]:no cbra annotations? {-> New expr.crange(obra, expr, expr2.expr, cbra, annotations)}
543 !nobra!nopar    | {orange} obra no expr [n2]:no dotdot [n3]:no [expr2]:expr_nobra [n4]:no [cbra]:obra annotations? {-> New expr.orange(obra, expr, expr2.expr, cbra, annotations)}
544 !nobra!nopar    | {array} braargs annotations? {-> New expr.array(braargs.exprs, annotations)}
545         | {self} kwself annotations? {-> New expr.self(kwself, annotations)}
546         | {true} kwtrue annotations? {-> New expr.true(kwtrue, annotations)}
547         | {false} kwfalse annotations? {-> New expr.false(kwfalse, annotations)}
548         | {null} kwnull annotations? {-> New expr.null(kwnull, annotations)}
549         | {int} number annotations? {-> New expr.int(number, annotations)}
550         | {float} float annotations? {-> New expr.float(float, annotations)}
551         | {char} char annotations? {-> New expr.char(char, annotations)}
552         | {string} string annotations? {-> New expr.string(string, annotations)}
553         | {superstring} superstring  {-> superstring.expr}
554 !nopar  | {par} opar no expr [n2]:no cpar annotations? {-> New expr.par(opar, expr, cpar, annotations)}
555         | {as_cast} expr_atom~nopar~nobra dot no kwas [n2]:no opar [n3]:no type [n4]:no cpar {-> New expr.as_cast(expr_atom~nopar~nobra.expr, kwas, opar, type, cpar)}
556         | {as_notnull} expr_atom~nopar~nobra dot no kwas [n2]:no opar [n3]:no kwnot [n4]:no kwnull [n5]:no cpar {-> New expr.as_notnull(expr_atom~nopar~nobra.expr, kwas, opar, kwnot, kwnull, cpar)}
557         ;
558
559 superstring {-> expr} 
560         = superstring_start superstring_middle* superstring_end annotations? {-> New expr.superstring([superstring_start.expr, superstring_middle.expr, superstring_end.expr], annotations)};
561 superstring_start {-> expr*}
562         = start_string_p no expr [n2]:no {-> [start_string_p.expr, expr]};
563 start_string_p {-> expr}
564         = start_string {-> New expr.start_string(start_string)};
565 superstring_middle {-> expr*}
566         = mid_string_p no expr [n2]:no {-> [mid_string_p.expr, expr]};
567 mid_string_p {-> expr}
568         = mid_string {-> New expr.mid_string(mid_string)};
569 superstring_end {-> expr}
570         = end_string {-> New expr.end_string(end_string)};
571
572 /* ANNOTATIONS *******************************************************************/
573
574 annotations~nopar {-> annotations}
575         = {one} at one_annotation~nopar {-> New annotations(at, Null, [one_annotation~nopar.annotation], Null)}
576         | {many} at opar no annotation_list [n2]:no cpar {-> New annotations(at, opar, [annotation_list.annotation], cpar)}
577         ;
578
579 one_annotation~nopar {-> annotation}
580         = {alone} atid annotations~nopar? {-> New annotation(atid, Null, [], Null, annotations~nopar.annotations)}
581 // !nopar to unambiguise 'new T@foo(bar)' between 'new T@(foo(bar))' and 'new (T@foo)(bar)'
582 !nopar  | {args} atid opar no at_args [n2]:no cpar annotations~nopar? {-> New annotation(atid, opar, [at_args.at_arg], cpar, annotations~nopar.annotations)}
583         ;
584
585 many_annotations {-> annotations}
586         = {many} annotation_list {-> New annotations(Null, Null, [annotation_list.annotation], Null)}
587         ;
588
589 annotation_list {-> annotation*}
590         = {many} one_annotation annotations_tail* {-> [one_annotation.annotation, annotations_tail.annotation] }
591         ;
592
593 line_annotations~forclass {-> annotations}
594         = line_annotation~forclass+ {-> New annotations(Null, Null, [line_annotation~forclass.annotation], Null) }
595         ;
596 line_annotation~forclass {-> annotation}
597         = [doc]:no atid~forclass annotations? n1 {-> New annotation(atid~forclass.atid, Null, [], Null, annotations)}
598         | {args} [doc]:no atid~forclass opar no at_args cpar annotations? n1 {-> New annotation(atid~forclass.atid, opar, [at_args.at_arg], cpar, annotations)}
599         | {nopar} [doc]:no atid~forclass at_args_nopar n1 {-> New annotation(atid~forclass.atid, Null, [at_args_nopar.at_arg], Null, Null)}
600         ;
601
602 annotations_tail {-> annotation}
603         = comma no one_annotation {-> one_annotation.annotation}
604         ;
605
606 at_args~nopar {-> at_arg* }
607         = {many} at_arg~nopar at_args_tail* {-> [at_arg~nopar.at_arg, at_args_tail.at_arg]}
608         ;
609
610 at_args_tail {-> at_arg}
611         = comma no at_arg {-> at_arg}
612         ;
613
614 at_arg~nopar {-> at_arg}
615         = {type} type {-> New at_arg.type(type)}
616         | {expr} expr~nopar {-> New at_arg.expr(expr~nopar.expr)}
617         | {stmt} stmt_noexpr~nopar {-> New at_arg.expr(stmt_noexpr~nopar.expr)}
618 !nopar  | {at} annotations {-> New at_arg.at(annotations.annotations)}
619         ;
620
621 atid~forclass {-> atid}
622         = {id} qualified? id {-> New atid.id(id)}
623 //!forclass     | {kwextern} qualified? kwextern {-> New atid.kwextern(kwextern)}
624 //!forclass     | {kwintern} qualified? kwintern {-> New atid.kwintern(kwintern)}
625 !forclass       | {kwreadable} qualified? kwreadable {-> New atid.kwreadable(kwreadable)}
626 !forclass       | {kwwritable} qualified? kwwritable {-> New atid.kwwritable(kwwritable)}
627         | {kwimport} qualified? kwimport {-> New atid.kwimport(kwimport)}
628         ;
629
630 /* MISC **********************************************************************/
631
632 recv~nopar~nobra {-> expr}
633         = expr_atom~nopar~nobra dot no {-> expr_atom~nopar~nobra.expr}
634         | {implicit} {-> New expr.implicit_self()}
635         ;
636
637 args {-> exprs}
638         = opar no expr_list cpar {-> New exprs.par(opar, [expr_list.expr], cpar) }
639         | {emptypar} opar no cpar {-> New exprs.par(opar, [], cpar) }
640         | {empty} {-> New exprs.list([])}
641         ;
642 args_nopar {-> exprs}
643         = opar no expr_list cpar {-> New exprs.par(opar, [expr_list.expr], cpar) }
644         | {onearg} expr_nopar {-> New exprs.list([expr_nopar.expr])}
645         | {emptypar} opar no cpar {-> New exprs.par(opar, [], cpar) }
646         | {empty} {-> New exprs.list([])}
647         ;
648 braargs {-> exprs}
649         = obra no expr_list cbra {-> New exprs.bra(obra, [expr_list.expr], cbra)};
650 expr_list {-> expr*}
651         = expr [n2]:no expr_tail* {-> [expr, expr_tail.expr]};
652 expr_tail {-> expr} 
653         = comma no expr [n2]:no {-> expr};
654 idlist {-> id*}
655         = opar no idlist_nopar [n2]:no cpar {-> [idlist_nopar.id]}
656         | {nopar} idlist_nopar {-> [idlist_nopar.id]}
657         ;
658 idlist_nopar {-> id*}
659         = {single} id {-> [id]}
660         | {more} idlist_nopar no comma [n2]:no id {-> [idlist_nopar.id, id]}
661         ;
662
663 module_name {-> module_name}
664         = {mod} modquad* id {-> New module_name(Null, [modquad.id], id)}
665         | {root} quad no modquad* id {-> New module_name(quad, [modquad.id], id)}
666         ;
667
668 qualified 
669         = {cla} modquad* classquad {-> New qualified([modquad.id], classquad.classid)}
670         | {mod} modquad+ {-> New qualified([modquad.id], Null)}
671         ; 
672 modquad {-> id}
673         = id quad no {-> id};
674 classquad {-> classid} 
675         = classid quad no {-> classid};
676
677 n1      = {a} comment | {b} eol;
678 n {-> doc?}
679         = {a} n2? comment+ {-> New doc([comment])}
680         | {b} n2 {-> Null}
681         ;
682 no {-> doc?}
683         = {empty} {-> Null}
684         | n {-> n.doc}
685         ;
686
687 n2
688         = {a} n2? comment+ eol+
689         | {b} eol+
690         ;
691
692 /*****************************************************************************/
693 Abstract Syntax Tree
694 /*****************************************************************************/
695
696 module  = moduledecl? [imports]:import* [extern_code_blocks]:extern_code_block* [classdefs]:classdef*;
697
698 moduledecl
699         = doc? kwmodule [name]:module_name annotations?;
700
701 import  = {std} visibility kwimport [name]:module_name annotations?
702         | {no} visibility kwimport kwend
703         ;
704
705 visibility
706         = {public}
707         | {private} kwprivate
708         | {protected} kwprotected
709         | {intrude} kwintrude
710         ;
711
712 classdef= {std} doc? kwredef? visibility classkind [id]:classid? [formaldefs]:formaldef* annotations? extern_code_block? [superclasses]:superclass* [propdefs]:propdef* kwend
713         | {top} [propdefs]:propdef*
714         | {main} [propdefs]:propdef*
715         ;
716 classkind
717         = {concrete} kwclass
718         | {abstract} kwabstract kwclass
719         | {interface} kwinterface
720         | {enum} kwenum
721         | {extern} kwextern kwclass?
722         ;
723 formaldef = [id]:classid type? annotations?;
724 superclass = kwsuper type annotations?;
725
726
727 propdef = {attr} doc? [readable]:able? [writable]:able? kwredef? visibility kwvar [id]:attrid? [id2]:id? type? annotations? expr?
728         | {meth} doc? kwredef? visibility methid signature
729         | {deferred_meth} doc? kwredef? visibility kwmeth methid signature annotations?
730         | {intern_meth} doc? kwredef? visibility kwmeth methid signature
731         | {extern_meth} doc? kwredef? visibility kwmeth methid signature [extern]:string? extern_calls? extern_code_block?
732         | {concrete_meth} doc? kwredef? visibility kwmeth methid signature annotations? [block]:expr?
733         | {concrete_init} doc? kwredef? visibility kwinit methid? signature annotations? [block]:expr?
734         //| {concrete_new} doc? kwredef? visibility kwnew methid? signature [block]:expr?
735         | {extern_init} doc? kwredef? visibility kwnew methid? signature [extern]:string? extern_calls? extern_code_block?
736         | {main_meth} kwredef? [block]:expr?
737         | {type} doc? kwredef? visibility kwtype [id]:classid type annotations?
738         ;
739
740 able    = {read} kwredef? kwreadable
741         | {write} kwredef? visibility? kwwritable
742         ;
743
744 methid = {id} id | {plus} plus | {minus} minus | {star} star | {slash} slash | {percent} percent | {eq} eq | {ne} ne | {le} le | {ge} ge | {lt} lt | {gt} gt |  {ll} ll | {gg} gg | {bra} obra cbra | {starship} starship | {assign} id assign | {braassign} obra cbra assign;
745
746 signature = opar? [params]:param* cpar? type?;
747
748 param   = id type? dotdotdot? annotations?
749         ;
750
751 type    = kwnullable? [id]:classid [types]:type* annotations?;
752
753 label = kwlabel id;
754
755 expr    = {block} expr* kwend? 
756         | {vardecl} kwvar id type? assign? expr? annotations?
757         | {return} kwreturn? expr?
758         | {break} kwbreak label? expr?
759         | {abort} kwabort
760         | {continue} kwcontinue? label? expr?
761         | {do} kwdo [block]:expr? label?
762         | {if} kwif expr [then]:expr? [else]:expr? 
763         | {ifexpr} kwif expr kwthen [then]:expr kwelse [else]:expr
764         | {while} kwwhile expr kwdo [block]:expr? label?
765         | {loop} kwloop [block]:expr? label?
766         | {for} kwfor [ids]:id* expr kwdo [block]:expr? label?
767         | {assert} kwassert id? expr [else]:expr?
768         | {once} kwonce expr 
769         | {send} expr 
770         | {binop} expr [expr2]:expr 
771         | {or} expr [expr2]:expr 
772         | {and} expr [expr2]:expr 
773         | {or_else} expr [expr2]:expr
774         | {implies} expr [expr2]:expr
775         | {not} kwnot expr 
776         | {eq} expr [expr2]:expr 
777         | {ee} expr [expr2]:expr 
778         | {ne} expr [expr2]:expr 
779         | {lt} expr [expr2]:expr 
780         | {le} expr [expr2]:expr 
781         | {ll} expr [expr2]:expr
782         | {gt} expr [expr2]:expr 
783         | {ge} expr [expr2]:expr 
784         | {gg} expr [expr2]:expr
785         | {isa} expr type 
786         | {plus} expr [expr2]:expr 
787         | {minus} expr [expr2]:expr 
788         | {starship} expr [expr2]:expr 
789         | {star} expr [expr2]:expr 
790         | {slash} expr [expr2]:expr 
791         | {percent} expr [expr2]:expr 
792         | {uminus} minus expr 
793         | {new} kwnew type id? [args]:exprs
794         | {attr} expr [id]:attrid 
795         | {attr_assign} expr [id]:attrid assign [value]:expr 
796         | {attr_reassign} expr [id]:attrid assign_op [value]:expr 
797         | {call} expr id [args]:exprs
798         | {call_assign} expr id [args]:exprs assign [value]:expr 
799         | {call_reassign} expr id [args]:exprs assign_op [value]:expr 
800         | {super} qualified? kwsuper [args]:exprs
801         | {init} expr kwinit [args]:exprs 
802         | {bra} expr [args]:exprs
803         | {bra_assign} expr [args]:exprs assign [value]:expr 
804         | {bra_reassign} expr [args]:exprs assign_op [value]:expr 
805         | {var} id
806         | {var_assign} id assign [value]:expr 
807         | {var_reassign} id assign_op [value]:expr 
808         | {range} expr [expr2]:expr annotations?
809         | {crange} obra expr [expr2]:expr cbra annotations?
810         | {orange} obra expr [expr2]:expr [cbra]:obra annotations?
811         | {array} [exprs]:exprs annotations?
812         | {self} kwself annotations?
813         | {implicit_self} 
814         | {true} kwtrue annotations?
815         | {false} kwfalse annotations?
816         | {null} kwnull annotations?
817         | {int} number annotations?
818         | {float} float annotations?
819         | {char} char annotations?
820         | {string} string annotations?
821         | {start_string} [string]:start_string 
822         | {mid_string} [string]:mid_string 
823         | {end_string} [string]:end_string 
824         | {superstring} [exprs]:expr* annotations?
825         | {par} opar expr cpar annotations?
826         | {as_cast} expr kwas opar type cpar
827         | {as_notnull} expr kwas opar kwnot kwnull cpar
828         | {isset_attr} kwisset expr [id]:attrid
829         | {debug_type} kwdebug kwtype expr type
830         ;
831 exprs
832         = {list} [exprs]:expr*
833         | {par} opar [exprs]:expr* cpar
834         | {bra} obra [exprs]:expr* cbra
835         ;
836 assign_op
837         = {plus} pluseq
838         | {minus} minuseq
839         ;
840
841 module_name = quad? [path]:id* id;
842 extern_calls = kwimport [extern_calls]:extern_call*
843         ;
844 extern_call =
845         | {super} kwsuper
846         | {local_prop} methid
847         | {full_prop} classid quad? methid
848         | {init_prop} classid
849         | {cast_as} [from_type]:type kwas [to_type]:type
850         | {as_nullable} type kwas kwnullable
851         | {as_not_nullable} type kwas kwnot kwnullable
852         ;
853 in_language = kwin string;
854 extern_code_block = in_language? extern_code_segment;
855
856 qualified = id* classid? ;
857
858 doc = comment+;
859
860 annotations = at? opar? [items]:annotation* cpar?;
861
862 annotation = atid opar? [args]:at_arg* cpar? annotations?;
863
864 at_arg
865         = {type} type
866         | {expr} expr
867         | {at} annotations
868         ;
869 atid = {id} id | {kwextern} [id]:kwextern | {kwintern} [id]:kwintern | {kwreadable} [id]:kwreadable | {kwwritable} [id]:kwwritable | {kwimport} [id]:kwimport;
870
871 /*****************************************************************************/
872