592cc429d27e9bf1bae02a8fae4cfdbf8ac1d675
[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 hexdigit = ['0'..'9'] | ['a'..'f'] | ['A'..'F'] | '_';
31 bindigit = '0' | '1' | '_';
32 octdigit = ['0' .. '7'] | '_';
33 letter = lowercase | uppercase | digit | '_';
34
35 tab = 9;
36 cr = 13;
37 lf = 10;
38 any = [all - [cr + lf]];
39 eol_helper = cr lf | cr | lf; // This takes care of different platforms
40
41 // characers inside strings and super-strings (atomaton powaa)
42 str_char
43         = [any - [['"' + '{'] + '\']] 
44         | '\' any 
45         ;
46 str_body = str_char*;
47
48 sstr_char
49         = [any - [''' + '\']]
50         | '\' any
51         ;
52
53 sstr_body = sstr_char*;
54
55 long_str_char = str_char | cr | lf;
56
57 // because no substraction in sablecc3, complex long strings are difficult to express
58 ls1 = '"' '"'? ;
59 ls2 = '{' '{'? ;
60 ls12 = ls1? (ls2 ls1)* ls2?;
61
62 long_str_part
63         = ls12 long_str_char
64         ;
65
66 long_str_body = long_str_part*;
67 lsend1 = ls2? (ls1 ls2)* '"""' '"'*;
68 lsend2 = ls1? (ls2 ls1)* '{{{' '{'*;
69
70 long_sstr_char = sstr_char | cr | lf;
71 long_sstr_part
72         = long_sstr_char
73         | ''' long_sstr_char
74         | ''' ''' long_sstr_char
75         ;
76
77 long_sstr_body = long_sstr_part*;
78
79
80 extern_code_char
81         = [all - ['`' + '\']]
82         | '\' all
83         | '`' [all - '}']
84         ;
85 extern_code_body = extern_code_char*;
86
87 /*****************************************************************************/
88 States
89 /*****************************************************************************/
90 initial;
91
92
93 /*****************************************************************************/
94 Tokens
95 /*****************************************************************************/
96
97 blank = (' ' | tab)+;
98
99 eol = eol_helper;
100 comment = '#' any* eol_helper?;
101
102 kwpackage = 'package';
103 kwmodule = 'module';
104 kwimport = 'import';
105 kwclass = 'class';
106 kwabstract = 'abstract';
107 kwinterface = 'interface';
108 kwenum = 'universal'|'enum';
109 kwend = 'end';
110 kwmeth = 'fun';
111 kwtype = 'type';
112 kwinit = 'init';
113 kwredef = 'redef';
114 kwis = 'is';
115 kwdo = 'do';
116 kwvar = 'var';
117 kwextern = 'extern';
118 kwpublic = 'public';
119 kwprotected = 'protected';
120 kwprivate = 'private';
121 kwintrude = 'intrude';
122 kwif = 'if';
123 kwthen = 'then';
124 kwelse = 'else';
125 kwwhile = 'while';
126 kwloop = 'loop';
127 kwfor = 'for';
128 kwin = 'in';
129 kwand = 'and';
130 kwor = 'or';
131 kwnot = 'not';
132 kwimplies = 'implies';
133 kwreturn = 'return';
134 kwcontinue = 'continue';
135 kwbreak = 'break';
136 kwabort = 'abort';
137 kwassert = 'assert';
138 kwnew = 'new';
139 kwisa = 'isa';
140 kwonce = 'once';
141 kwsuper = 'super';
142 kwself = 'self';
143 kwtrue = 'true';
144 kwfalse = 'false';
145 kwnull = 'null';
146 kwas = 'as';
147 kwnullable = 'nullable';
148 kwisset = 'isset';
149 kwlabel = 'label';
150 kwwith = 'with';
151 kwdebug = '__debug__';
152
153 opar = '(';
154 cpar = ')';
155 obra = '[';
156 cbra = ']';
157 comma = ',';
158 column = ':';
159 quad = '::';
160 assign = '=';
161 pluseq = '+=';
162 minuseq = '-=';
163 stareq = '*=';
164 slasheq = '/=';
165 percenteq = '%=';
166 starstareq = '**=';
167 pipeeq = '|=';
168 careteq = '^=';
169 ampeq = '&=';
170 lleq = '<<=';
171 ggeq = '>>=';
172 dotdotdot = '...';
173 dotdot = '..';
174 dot = '.';
175 plus = '+';
176 minus = '-';
177 star = '*';
178 starstar = '**';
179 slash = '/';
180 percent = '%';
181 pipe = '|';
182 caret = '^';
183 amp = '&';
184 tilde = '~';
185 eq = '==';
186 ne = '!=';
187 lt = '<';
188 le = '<=';
189 ll = '<<';
190 gt = '>';
191 ge = '>=';
192 gg = '>>';
193 starship = '<=>';
194 bang='!';
195 at='@';
196 semi=';';
197
198 classid = uppercase letter*;
199 id = lowercase letter*;
200 attrid = '_' lowercase letter*;
201
202 number = digit+;
203 hex_number = ('0x' | '0X') hexdigit+;
204 bin_number = ('0b' | '0B') bindigit+;
205 oct_number = ('0o' | '0O') octdigit+;
206 bytenum = digit+ 'u8';
207 hex_bytenum = ('0x' | '0X') hexdigit+ 'u8';
208 bin_bytenum = ('0b' | '0B') bindigit+ 'u8';
209 oct_bytenum = ('0o' | '0O') octdigit+ 'u8';
210 float = digit* '.' digit+ | (digit+ | digit* '.' digit+) ('E'|'e') ('+'|'-'|) digit+;
211 string = '"' str_body '"' | '"' '"' '"' long_str_body lsend1 | ''' ''' ''' long_sstr_body ''' ''' ''';
212 start_string = '"' str_body '{' | '"' '"' '"' long_str_body lsend2;
213 mid_string = '}' str_body '{' | '}' '}' '}' long_str_body lsend2;
214 end_string = '}' str_body '"' | '}' '}' '}' long_str_body lsend1;
215 char = (''' [[any - '''] - '\'] ''') | (''' '\' any ''');
216 bad_string = ('"'|'}') str_body | '"' '"' '"' long_str_body | ''' ''' ''' long_sstr_body;
217 bad_char = ''' '\'? any;
218
219 extern_code_segment = '`' '{' extern_code_body '`' '}';
220
221 /*****************************************************************************/
222 Ignored Tokens
223 /*****************************************************************************/
224
225 blank;
226
227 /*****************************************************************************/
228 Productions
229 /*****************************************************************************/
230
231 /* MODULES *******************************************************************/
232 module
233         = moduledecl? [imports]:import* [extern_bodies]:extern_code_body* [classdefs]:topdef* implicit_main_class {-> New module(moduledecl, [imports.import], [extern_bodies.extern_code_block], [classdefs.classdef,implicit_main_class.classdef])};
234
235 moduledecl
236         = [doc]:nd redef visibility kwmodule no module_name annotation_withend [n2]:n1 {-> New moduledecl(doc.doc, redef.kwredef, visibility, kwmodule, module_name, annotation_withend.annotations)};
237
238 import
239         = {std} [doc]:nd redef visibility kwimport no module_name annotation_withend [n2]:n1 {-> New import.std(visibility, kwimport, module_name, annotation_withend.annotations)}
240         | {no} [doc]:nd redef visibility kwimport no kwend [n2]:n1 {-> New import.no(visibility, kwimport, kwend)}
241         ;
242
243 topdef {-> classdef}
244         = {classdef} classdef {-> classdef}
245         | propdef_toplevel n1 {-> New classdef.top([propdef_toplevel.propdef])}
246         ;
247
248 implicit_main_class {-> classdef?}
249         = implicit_main_meth {-> New classdef.main([implicit_main_meth.propdef])}
250         | {null} n? {-> Null}
251         ;
252 implicit_main_meth {-> propdef}
253         = [doc]:nd stmts {-> New propdef.main_meth(Null, stmts.expr)}
254         | {n} [doc]:nd stmtsn {-> New propdef.main_meth(Null, stmtsn.expr)}
255         ;
256
257 /* CLASSES *******************************************************************/
258 classdef
259         = [doc]:nd redef visibility classkind no qclassid extern_code_block? propdefs kwend {-> New classdef.std(doc.doc, redef.kwredef, visibility, classkind, qclassid.classid, Null, [], Null, extern_code_block, [propdefs.propdef], kwend)}
260         | {for} [doc]:nd redef visibility classkind no qclassid obra [n2]:no formaldefs cbra extern_code_block? propdefs kwend {-> New classdef.std(doc.doc, redef.kwredef, visibility, classkind, qclassid.classid, obra, [formaldefs.formaldef], cbra, extern_code_block, [propdefs.propdef], kwend)}
261         ;
262
263 redef {-> kwredef?}
264         = kwredef? {-> kwredef};
265 classkind
266         = {concrete} kwclass
267         | {abstract} kwabstract kwclass
268         | {interface} kwinterface
269         | {enum} kwenum
270         | {extern} kwextern kwclass
271         ;
272
273 formaldefs {-> formaldef*}
274         = formaldef formaldefs_tail* {-> [formaldef, formaldefs_tail.formaldef]}
275         | {empty} {->[]}
276         ;
277 formaldefs_tail {-> formaldef}
278         = comma no formaldef {-> formaldef};
279 formaldef
280         = classid annotations? typing_o no {-> New formaldef(classid, typing_o.type, annotations)};
281
282 superclass {-> propdef}
283         = {super} [doc]:nd redef visibility kwsuper [n2]:no type annotation_withend {-> New propdef.super(doc.doc, redef.kwredef, visibility, kwsuper, type, annotation_withend.annotations)}
284         ;
285
286 propdefs {-> propdef*}
287         = propdefn+ no {-> [propdefn.propdef]}
288         | {super} superclass {-> [superclass.propdef]}
289         | {empty} nd {-> []}
290         ;
291 propdefn {-> propdef}
292         = propdef n1 {-> propdef.propdef}
293         ;
294 propdef~toplevel {-> propdef}
295         = {meth} [doc]:nd redef visibility kwmeth qmethid signature annotation_noend? kwdo stmtso kwend_o {-> New propdef.meth(doc.doc, redef.kwredef, visibility, kwmeth, Null, Null, qmethid.methid, signature, annotation_noend.annotations, Null, Null, kwdo, stmtso.expr, kwend_o.kwend)}
296         | {nobody} [doc]:nd redef visibility kwmeth qmethid signature annotation_withend_nonull {-> New propdef.meth(doc.doc, redef.kwredef, visibility, kwmeth, Null, Null, qmethid.methid, signature.signature, annotation_withend_nonull.annotations, Null, Null, Null, Null, Null)}
297 !toplevel| {intern_new} [doc]:nd redef visibility kwnew qmethid? signature annotation_withend_nonull {-> New propdef.meth(doc.doc, redef.kwredef, visibility, Null, Null, kwnew, qmethid.methid, signature, annotation_withend_nonull.annotations, Null, Null, Null, Null, Null)}
298 !toplevel| {new} [doc]:nd redef visibility kwnew qmethid? signature annotation_noend? kwdo stmtso kwend_o {-> New propdef.meth(doc.doc, redef.kwredef, visibility, Null, Null, kwnew, qmethid.methid, signature, annotation_noend.annotations, Null, Null, kwdo, stmtso.expr, kwend_o.kwend)}
299         | {extern_implicit} [doc]:nd redef visibility kwmeth qmethid signature annotation_noend? extern_calls extern_code_block {-> New propdef.meth(doc.doc, redef.kwredef, visibility, kwmeth, Null, Null, qmethid.methid, signature.signature, annotation_noend.annotations, extern_calls, extern_code_block, Null, Null, Null)}
300 !toplevel| {var3} [doc]:nd redef visibility kwvar id typing_o annotation_withend {-> New propdef.attr(doc.doc, redef.kwredef, visibility, kwvar, id, typing_o.type, Null, Null, annotation_withend.annotations, Null, Null, Null)}
301 !toplevel| {var4} [doc]:nd redef visibility kwvar id typing_o assign [n2]:no expr annotation_withend {-> New propdef.attr(doc.doc, redef.kwredef, visibility, kwvar, id, typing_o.type, assign, expr.expr, annotation_withend.annotations, Null, Null, Null)}
302 !toplevel| {var5} [doc]:nd redef visibility kwvar id typing_o annotation_noend? kwdo stmtso kwend? {-> New propdef.attr(doc.doc, redef.kwredef, visibility, kwvar, id, typing_o.type, Null, Null, annotation_noend.annotations, kwdo, stmtso.expr, kwend)}
303 !toplevel| {init} [doc]:nd redef visibility kwinit qmethid? signature annotation_noend? kwdo stmtso kwend_o {-> New propdef.meth(doc.doc, redef.kwredef, visibility, Null, kwinit, Null, qmethid.methid, signature, annotation_noend.annotations, Null, Null, kwdo, stmtso.expr, kwend_o.kwend)}
304 !toplevel| {type} [doc]:nd redef visibility kwtype classid typing annotation_withend {-> New propdef.type(doc.doc, redef.kwredef, visibility, kwtype, classid, typing.type, annotation_withend.annotations)}
305 !toplevel| {extern_init_implicit} [doc]:nd redef visibility kwnew qmethid? signature annotation_noend? extern_calls extern_code_block {-> New propdef.meth(doc.doc, redef.kwredef, visibility, Null, Null, kwnew, qmethid.methid, signature, annotation_noend.annotations, extern_calls, extern_code_block, Null, Null, Null)}
306 !toplevel| {annot} line_annotation_forclass {-> line_annotation_forclass.propdef}
307 !toplevel| {super} superclass {-> superclass.propdef}
308         ;
309 annotation_withend~nonull {-> annotations?}
310         = {oneliner} kwis annotation_list {-> New annotations(kwis, Null, Null, [annotation_list.annotation], Null, Null)}
311         | {more} kwis n1 line_annotation+ kwend {-> New annotations(kwis, Null, Null, [line_annotation.annotation], Null, kwend) }
312 !nonull | {null} {-> Null}
313         ;
314 annotation_noend {-> annotations}
315         = {oneliner} kwis annotation_list {-> New annotations(kwis, Null, Null, [annotation_list.annotation], Null, Null)}
316         | {more} kwis n1 line_annotation+ {-> New annotations(kwis, Null, Null, [line_annotation.annotation], Null, Null) }
317         ;
318
319 visibility
320         = {public} {-> New visibility.public(Null)}
321         | {public2} kwpublic no {-> New visibility.public(kwpublic)}
322         | {private} kwprivate no {-> New visibility.private(kwprivate)}
323         | {protected} kwprotected no {-> New visibility.protected(kwprotected)}
324         | {intrude} kwintrude no {-> New visibility.intrude(kwintrude)}
325         ;
326
327 methid~noid {-> methid}
328         = {plus} plus {-> New methid.plus(plus)}
329         | {minus} minus {-> New methid.minus(minus)}
330         | {star} star {-> New methid.star(star)}
331         | {starstar} starstar {-> New methid.starstar(starstar)}
332         | {slash} slash {-> New methid.slash(slash)}
333         | {percent} percent {-> New methid.percent(percent)}
334         | {pipe} pipe {-> New methid.pipe(pipe)}
335         | {caret} caret {-> New methid.caret(caret)}
336         | {amp} amp {-> New methid.amp(amp)}
337         | {tilde} tilde {-> New methid.tilde(tilde)}
338         | {eq} eq {-> New methid.eq(eq)}
339         | {ne} ne {-> New methid.ne(ne)}
340         | {le} le {-> New methid.le(le)}
341         | {ge} ge {-> New methid.ge(ge)}
342         | {lt} lt {-> New methid.lt(lt)}
343         | {gt} gt {-> New methid.gt(gt)}
344         | {ll} ll {-> New methid.ll(ll)}
345         | {gg} gg {-> New methid.gg(gg)}
346         | {bra} obra cbra {-> New methid.bra(obra, cbra)}
347         | {starship} starship {-> New methid.starship(starship)}
348         | {assign} id assign {-> New methid.assign(id, assign)}
349         | {braassign} obra cbra assign {-> New methid.braassign(obra, cbra, assign)}
350 !noid   | {id} id {-> New methid.id(id)}
351         ;
352
353 signature {-> signature}
354         = opar no params cpar typing [no2]:no {-> New signature(opar, [params.param], cpar, typing.type)}
355         | {noret} opar no params cpar [no2]:no {-> New signature(opar, [params.param], cpar, Null)}
356         | {nopar} typing no {-> New signature(Null, [], Null, typing.type)}
357         | {noparnoret} no {-> New signature(Null, [], Null, Null)}
358         ;
359
360 params {-> param*} 
361         = param params_tail* [n2]:no {-> [param, params_tail.param] }
362         | {null} {-> []}
363         ;
364 params_tail {-> param}
365         = comma no param {-> param};
366 param
367         = {untyped} id annotations_o {-> New param(id, Null, Null, annotations_o.annotations)}
368         | id annotations? typing dotdotdot? {-> New param(id, typing.type, dotdotdot, annotations)}
369         ;
370
371 extern_calls {-> extern_calls?}
372         = kwimport no extern_call extern_call_tail* {-> New extern_calls( kwimport, [extern_call, extern_call_tail.extern_call] )}
373         | {null} {-> Null}
374         ;
375 extern_call_tail {-> extern_call}
376         = comma no extern_call {-> extern_call};
377 extern_call {-> extern_call}
378         = {prop} extern_call_prop {-> extern_call_prop.extern_call}
379         | {cast} extern_call_cast {-> extern_call_cast.extern_call}
380         | {super} kwsuper {-> New extern_call.super( kwsuper )}
381         ;
382 extern_call_prop {-> extern_call}
383         = {local} qmethid {-> New extern_call.local_prop( qmethid.methid )}
384         | {full} type dot qmethid {-> New extern_call.full_prop( type, dot, qmethid.methid )}
385         | {init} type {-> New extern_call.init_prop( type )}
386         ;
387 extern_call_cast {-> extern_call}
388         = {as_cast} [from_type]:type dot kwas [n2]:no opar [n3]:no [to_type]:type [n4]:no cpar {-> New extern_call.cast_as(from_type, dot, kwas, to_type)}
389         | {as_cast2}[from_type]:type dot kwas [n2]:no [to_type]:type {-> New extern_call.cast_as(from_type, dot, kwas, to_type)}
390         | {as_nullable} type dot kwas [n2]:no opar [n3]:no kwnullable [n4]:no cpar {-> New extern_call.as_nullable( type, kwas, kwnullable)}
391         | {as_nullable2}type dot kwas [n2]:no kwnullable {-> New extern_call.as_nullable( type, kwas, kwnullable)}
392         | {as_not_nullable} type dot kwas [n2]:no opar [n3]:no kwnot [n4]:no kwnullable [n5]:no cpar {-> New extern_call.as_not_nullable( type, kwas, kwnot, kwnullable)}
393         | {as_not_nullable2}type dot kwas [n2]:no kwnot [n3]:no kwnullable {-> New extern_call.as_not_nullable( type, kwas, kwnot, kwnullable)}
394         ;
395
396 string_o {->string?} = string? {-> string};
397
398 in_language = kwin no string [n1]:no {-> New in_language(kwin, string)};
399 extern_code_block = in_language? extern_code_segment;
400 extern_code_block_o {-> extern_code_block?}
401         = extern_code_block {-> extern_code_block}
402         | {null} {-> Null}
403         ;
404 extern_code_body {-> extern_code_block} = no extern_code_block {-> extern_code_block};
405
406 /* TYPES *********************************************************************/
407 type~nobra~nopar {-> type}
408         = {simple} kwnullable? classid annotations_o~nopar {-> New type(kwnullable, classid, Null, [], Null, annotations_o~nopar.annotations)}
409 !nobra  | {generic} kwnullable? classid obra no types [n2]:no cbra annotations_o~nopar {-> New type(kwnullable, classid, obra, [types.type], cbra, annotations_o~nopar.annotations)}
410         ;
411 types {-> type*} 
412         = type types_tail* {-> [type, types_tail.type]};
413 types_tail {-> type}
414         = comma no type {-> type};
415 typing {-> type}
416         = column no type {-> type};
417 typing_o {-> type?}
418         = column no type {-> type}
419         | {null} {-> Null}
420         ;
421
422 /* STATMENTS *****************************************************************/
423 stmtso~withelse~withend {-> expr?}
424         = {block} n stmtsnend {-> stmtsnend.expr}
425         | {emptyblock} n kwend {-> New expr.block([], kwend)}
426         | {emptyoneline} kwend {-> New expr.block([], kwend)}
427 !withend| {oneline} stmt~withelse {-> stmt~withelse.expr}
428         ;
429 stmts {-> expr}
430         = stmt stmts_tail* {-> New expr.block([stmt.expr, stmts_tail.expr], Null)};
431 stmtsn {-> expr}
432         = stmt stmts_tail* n {-> New expr.block([stmt.expr, stmts_tail.expr], Null)};
433 stmtsnend {-> expr}
434         = stmt stmts_tail* n kwend {-> New expr.block([stmt.expr, stmts_tail.expr], kwend)};
435 stmts_tail {-> expr}
436         = n stmt {-> stmt.expr};
437 stmt~withelse~noexpr~nopar {-> expr}
438         = {vardecl} vardecl {-> vardecl.expr}
439         | {assign} assignment~nopar {-> assignment~nopar.expr}
440         | {return} kwreturn expr? {-> New expr.return(kwreturn, expr)}
441         | {break} kwbreak label? {-> New expr.break(kwbreak, label)}
442         | {abort} kwabort {-> New expr.abort(kwabort)}
443         | {continue} kwcontinue label? {-> New expr.continue(kwcontinue, label)}
444         | {do} do~withelse {-> do~withelse.expr}
445 !noexpr | {if} if~withelse {-> if~withelse.expr}
446         | {while} while~withelse {-> while~withelse.expr}
447         | {loop} loop~withelse {-> loop~withelse.expr}
448         | {for} for~withelse {-> for~withelse.expr}
449         | {with} with~withelse {-> with~withelse.expr}
450         | {assert} assert~withelse {-> assert~withelse.expr}
451 !noexpr | {call} recv qid args_nopar {-> New expr.call(recv.expr, qid.id, args_nopar.exprs)}
452 !noexpr | {super} qualified_o kwsuper args_nopar {-> New expr.super(qualified_o.qualified, kwsuper, args_nopar.exprs)}
453 !noexpr | {init} recv qualified? kwinit args_nopar {-> New expr.init(recv.expr, kwinit, args_nopar.exprs)}
454         | {debug_type_is} kwdebug kwtype type column expr {-> New expr.debug_type(kwdebug, kwtype, expr.expr, type) }
455         ;
456
457 label= kwlabel id?;
458
459 vardecl{-> expr}
460         = kwvar id annotations? typing_o {-> New expr.vardecl(kwvar, id, typing_o.type, Null, Null, annotations)}
461         | {assign} kwvar id annotations? typing_o assign no expr {-> New expr.vardecl(kwvar, id, typing_o.type, assign, expr.expr, annotations)}
462         ;
463
464 assignment~nopar {-> expr}
465         = {attr} recv~nopar qualified_o attrid assign expr {-> New expr.attr_assign(recv~nopar.expr, attrid, assign, expr)}
466         | {call} recv~nopar qid args assign expr {-> New expr.call_assign(recv~nopar.expr, qid.id, args.exprs, assign,  expr)}
467         | {bra} expr_atom~nopar braargs assign expr {-> New expr.bra_assign(expr_atom~nopar.expr, braargs.exprs, assign,  expr)}
468         | {attr_re} recv~nopar qualified_o attrid assign_op expr {-> New expr.attr_reassign(recv~nopar.expr, attrid, assign_op,  expr)}
469         | {call_re} recv~nopar qid args assign_op expr {-> New expr.call_reassign(recv~nopar.expr, qid.id, args.exprs, assign_op,  expr)}
470         | {bra_re} expr_atom~nopar braargs assign_op expr {-> New expr.bra_reassign(expr_atom~nopar.expr, braargs.exprs, assign_op,  expr)}
471         ;
472 assign_op
473         = {plus} pluseq
474         | {minus} minuseq
475         | {star} stareq
476         | {slash} slasheq
477         | {percent} percenteq
478         | {starstar} starstareq
479         | {pipe} pipeeq
480         | {caret} careteq
481         | {amp} ampeq
482         | {ll} lleq
483         | {gg} ggeq
484         ;
485
486 do~withelse {-> expr}
487         = kwdo stmtso_withend label {-> New expr.do(kwdo, stmtso_withend.expr, label)}
488         | {nolabel} kwdo stmtso~withelse {-> New expr.do(kwdo, stmtso~withelse.expr, Null)}
489         ;
490
491 if~withelse {-> expr}
492         = {onelineelse} kwif no expr [n2]:no kwthen stmt_withelse kwelse stmtso~withelse {-> New expr.if(kwif, expr, kwthen, stmt_withelse.expr, kwelse, stmtso~withelse.expr)}
493 !withelse       | {oneline} kwif no expr [n2]:no kwthen stmt {-> New expr.if(kwif, expr, kwthen, stmt.expr, Null, Null)}
494 !withelse       | {block} kwif no expr [n2]:no kwthen [n3]:n stmtsn kwelse stmtso {-> New expr.if(kwif, expr, kwthen, stmtsn.expr, kwelse, stmtso.expr)}
495 !withelse       | {emptyblock} kwif no expr [n2]:no kwthen [n3]:n? kwelse stmtso {-> New expr.if(kwif, expr, kwthen, Null, kwelse, stmtso.expr)}
496 !withelse       | {blocknoelse} kwif no expr [n2]:no kwthen [n3]:n stmtsn endblock {-> New expr.if(kwif, expr, kwthen, stmtsn.expr, Null, endblock.expr)}
497 !withelse       | {emptyblocknoelse} kwif no expr [n2]:no kwthen [n3]:n? endblock {-> New expr.if(kwif, expr, kwthen, Null, Null, endblock.expr)}
498         ;
499 endblock {-> expr}
500         = {empty} kwend {-> New expr.block([], kwend)}
501         ;
502
503 loop~withelse {-> expr}
504         = kwloop stmtso_withend label {-> New expr.loop(kwloop, stmtso_withend.expr, label)}
505         | {nolabel} kwloop stmtso~withelse {-> New expr.loop(kwloop, stmtso~withelse.expr, Null)}
506         ;
507
508 while~withelse {-> expr}
509         = kwwhile no expr [n2]:no kwdo stmtso_withend label {-> New expr.while(kwwhile, expr, kwdo, stmtso_withend.expr, label)}
510         | {nolabel} kwwhile no expr [n2]:no kwdo stmtso~withelse {-> New expr.while(kwwhile, expr, kwdo, stmtso~withelse.expr, Null)}
511         ;
512
513 for~withelse {-> expr}
514         = kwfor no [ids]:idlist [n2]:no kwin [n3]:no expr [n4]:no kwdo stmtso_withend label {-> New expr.for(kwfor, [ids.id], kwin, expr, kwdo, stmtso_withend.expr, label)}
515         | {nolabel} kwfor no [ids]:idlist [n2]:no kwin [n3]:no expr [n4]:no kwdo stmtso~withelse {-> New expr.for(kwfor, [ids.id], kwin, expr, kwdo, stmtso~withelse.expr, Null)}
516         ;
517
518 with~withelse {-> expr}
519         = kwwith no withexpr [n4]:no kwdo stmtso_withend label {-> New expr.with(kwwith, withexpr.expr, kwdo, stmtso_withend.expr, label)}
520         | {nolabel} kwwith no withexpr [n4]:no kwdo stmtso~withelse {-> New expr.with(kwwith, withexpr.expr, kwdo, stmtso~withelse.expr, Null)}
521         ;
522
523 withexpr {-> expr}
524         = {assign} id annotations? typing_o assign no expr {-> New expr.vardecl(Null, id, typing_o.type, assign, expr.expr, annotations)}
525         | expr {-> expr}
526         ;
527
528 assert~withelse {-> expr}
529         = {else} kwassert assertid? expr kwelse stmtso~withelse {-> New expr.assert(kwassert, assertid.id, expr, kwelse, stmtso~withelse.expr)}
530 !withelse| {noelse} kwassert assertid? expr {-> New expr.assert(kwassert, assertid.id, expr, Null, Null)}
531         ;
532 assertid {-> id}
533         = id column {-> id};
534
535 /* EXPRESSIONS ***************************************************************/
536 expr~nopar~nobra {-> expr}
537         = expr_and~nopar~nobra {-> expr_and~nopar~nobra.expr}
538         | {ifexpr} kwif [n1]:no expr [n2]:no kwthen [n3]:no [then]:expr [n4]:no kwelse [n5]:no [else]:expr~nopar~nobra {-> New expr.ifexpr(kwif, expr, kwthen, then, kwelse, else.expr)}
539         ;
540
541 expr_and~nopar~nobra {-> expr}
542         = expr_not~nopar~nobra {-> expr_not~nopar~nobra.expr}
543         | {:or} expr_and~nopar~nobra kwor :no expr_not~nopar~nobra
544         | {:and} expr_and~nopar~nobra kwand :no expr_not~nopar~nobra
545         | {:or_else} expr_and~nopar~nobra kwor kwelse :no expr_not~nopar~nobra
546         | {:implies} expr_and~nopar~nobra kwimplies :no expr_not~nopar~nobra
547         ;
548
549 expr_not~nopar~nobra {-> expr}
550         = expr_eq~nopar~nobra {-> expr_eq~nopar~nobra.expr}
551         | {not} kwnot no expr_not~nopar~nobra {-> New expr.not(kwnot, expr_not~nopar~nobra.expr)}
552         ;
553
554 expr_eq~nopar~nobra {-> expr}
555         = expr_bitor~nopar~nobra {-> expr_bitor~nopar~nobra.expr}
556         | {:eq} expr_bitor~nopar~nobra eq :no [expr2]:expr_bitor~nopar~nobra
557         | {:ne} expr_bitor~nopar~nobra ne :no [expr2]:expr_bitor~nopar~nobra
558         | {:lt} expr_bitor~nopar~nobra lt :no [expr2]:expr_bitor~nopar~nobra
559         | {:le} expr_bitor~nopar~nobra le :no [expr2]:expr_bitor~nopar~nobra
560         | {:gt} expr_bitor~nopar~nobra gt :no [expr2]:expr_bitor~nopar~nobra
561         | {:ge} expr_bitor~nopar~nobra ge :no [expr2]:expr_bitor~nopar~nobra
562         | {:starship} expr_bitor~nopar~nobra starship :no [expr2]:expr_bitor~nopar~nobra
563         | {:isa} expr_bitor~nopar~nobra kwisa :no type~nobra
564         ;
565
566 expr_bitor~nopar~nobra {-> expr}
567         = [expr]:expr_bitxor~nopar~nobra {-> expr.expr}
568         | {:pipe} expr_bitor~nopar~nobra pipe :no [expr2]:expr_bitxor~nopar~nobra
569         ;
570
571 expr_bitxor~nopar~nobra {-> expr}
572         = [expr]:expr_bitand~nopar~nobra {-> expr.expr}
573         | {:caret} expr_bitxor~nopar~nobra caret :no [expr2]:expr_bitand~nopar~nobra
574         ;
575
576 expr_bitand~nopar~nobra {-> expr}
577         = [expr]:expr_shift~nopar~nobra {-> expr.expr}
578         | {:amp} expr_bitand~nopar~nobra amp :no [expr2]:expr_shift~nopar~nobra
579         ;
580
581 expr_shift~nopar~nobra {-> expr}
582         = [expr]:expr_add~nopar~nobra {-> expr.expr}
583         | {:ll} expr_shift~nopar~nobra ll :no [expr2]:expr_add~nopar~nobra
584         | {:gg} expr_shift~nopar~nobra gg :no [expr2]:expr_add~nopar~nobra
585         ;
586
587 expr_add~nopar~nobra {-> expr}
588         =  expr_mul~nopar~nobra {-> expr_mul~nopar~nobra.expr}
589         | {:plus} expr_add~nopar~nobra plus :no [expr2]:expr_mul~nopar~nobra
590         | {:minus} expr_add~nopar~nobra minus :no [expr2]:expr_mul~nopar~nobra
591         ;
592
593 expr_mul~nopar~nobra {-> expr}
594         = expr_pow~nopar~nobra {-> expr_pow~nopar~nobra.expr}
595         | {:star} expr_mul~nopar~nobra star :no [expr2]:expr_pow~nopar~nobra
596         | {:slash} expr_mul~nopar~nobra slash :no [expr2]:expr_pow~nopar~nobra
597         | {:percent} expr_mul~nopar~nobra percent :no [expr2]:expr_pow~nopar~nobra
598         ;
599
600 expr_pow~nopar~nobra {-> expr}
601         = expr_minus~nopar~nobra {-> expr_minus~nopar~nobra.expr}
602         | {:starstar} expr_minus~nopar~nobra starstar :no [expr2]:expr_pow~nopar~nobra
603         ;
604
605 expr_minus~nopar~nobra {-> expr}
606         = expr_new~nopar~nobra {-> expr_new~nopar~nobra.expr}
607         | {:uminus} minus expr_minus~nobra
608         | {:uplus} plus expr_minus~nobra
609         | {:utilde} tilde expr_minus~nobra
610         | {:once} kwonce :no expr_minus~nobra
611         ;
612
613 expr_new~nopar~nobra {-> expr}
614         = expr_atom~nopar~nobra {-> expr_atom~nopar~nobra.expr}
615         | {new} kwnew no type~nobra_nopar args {-> New expr.new(kwnew, type~nobra_nopar.type, Null, args.exprs)}
616         | {isset_attr} kwisset recv~nopar~nobra qualified_o attrid {-> New expr.isset_attr(kwisset, recv~nopar~nobra.expr, attrid)}
617         ;
618
619 expr_atom~nopar~nobra {-> expr}
620         = expr_single~nopar~nobra {-> expr_single~nopar~nobra.expr}
621         | {attr} recv~nopar~nobra qualified_o attrid {-> New expr.attr(recv~nopar~nobra.expr, attrid)}
622         | {call} recv~nopar~nobra qid args {-> New expr.call(recv~nopar~nobra.expr, qid.id, args.exprs)}
623         | {super} qualified_o kwsuper args {-> New expr.super(qualified_o.qualified, kwsuper, args.exprs)}
624         | {init} recv~nopar~nobra kwinit args {-> New expr.init(recv~nopar~nobra.expr, kwinit, args.exprs)}
625 !nobra  | {bra} expr_atom~nopar braargs {-> New expr.bra(expr_atom~nopar.expr, braargs.exprs)}
626         | {new} kwnew no type~nobra_nopar dot [n2]:no qid args {-> New expr.new(kwnew, type~nobra_nopar.type, qid.id, args.exprs)}
627         | {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)}
628         | {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)}
629         | {as_notnull2}expr_atom~nopar~nobra dot no kwas [n2]:no kwnot [n4]:no kwnull {-> New expr.as_notnull(expr_atom~nopar~nobra.expr, kwas, Null, kwnot, kwnull, Null)}
630         ;
631
632 arg~nopar~nobra {-> expr}
633         = [expr]:expr~nopar~nobra {-> expr.expr}
634         | {vararg} [expr]:expr~nopar~nobra dotdotdot {-> New expr.vararg(expr.expr, dotdotdot)}
635         | {namedarg} id assign [expr]:expr~nopar~nobra  {-> New expr.namedarg(id, assign, expr.expr) }
636         ;
637
638 expr_single~nopar~nobra {-> expr}
639         = {self} kwself annotations_o {-> New expr.self(kwself, annotations_o.annotations)}
640         | {true} kwtrue annotations_o {-> New expr.true(kwtrue, annotations_o.annotations)}
641         | {false} kwfalse annotations_o {-> New expr.false(kwfalse, annotations_o.annotations)}
642         | {null} kwnull annotations_o {-> New expr.null(kwnull, annotations_o.annotations)}
643         | {int} number annotations_o {-> New expr.dec_int(number, annotations_o.annotations)}
644         | {hex_int} hex_number annotations_o {-> New expr.hex_int(hex_number, annotations_o.annotations)}
645         | {bin_int} bin_number annotations_o {-> New expr.bin_int(bin_number, annotations_o.annotations)}
646         | {oct_int} oct_number annotations_o {-> New expr.oct_int(oct_number, annotations_o.annotations)}
647         | {byte} bytenum annotations_o {-> New expr.dec_byte(bytenum, annotations_o.annotations)}
648         | {hex_byte} hex_bytenum annotations_o {-> New expr.hex_byte(hex_bytenum, annotations_o.annotations)}
649         | {bin_byte} bin_bytenum annotations_o {-> New expr.bin_byte(bin_bytenum, annotations_o.annotations)}
650         | {oct_byte} oct_bytenum annotations_o {-> New expr.oct_byte(oct_bytenum, annotations_o.annotations)}
651         | {float} float annotations_o {-> New expr.float(float, annotations_o.annotations)}
652         | {char} char annotations_o {-> New expr.char(char, annotations_o.annotations)}
653         | {string} string annotations_o {-> New expr.string(string, annotations_o.annotations)}
654         | {superstring} superstring  {-> superstring.expr}
655 !nopar  | {par} expr_par {-> expr_par.expr}
656 // !nopar to unambiguise 'foo[5].bar' between '(foo[5]).bar' and 'foo([5].bar),
657 !nobra!nopar    | {range} obra no expr [n2]:no dotdot [n3]:no [expr2]:expr_nobra [n4]:no cbra annotations_o {-> New expr.crange(obra, expr, dotdot, expr2.expr, cbra, annotations_o.annotations)}
658 !nobra!nopar    | {orange} obra no expr [n2]:no dotdot [n3]:no [expr2]:expr_nobra [n4]:no [cbra]:obra annotations_o {-> New expr.orange(obra, expr, dotdot, expr2.expr, cbra, annotations_o.annotations)}
659 !nobra!nopar    | {array} obra no array_items typing_o cbra annotations_o {-> New expr.array(obra, [array_items.expr], typing_o.type, cbra, annotations_o.annotations)}
660         ;
661
662 expr_par {-> expr}
663         = {par} opar no any_expr [n2]:no cpar annotations_o {-> New expr.par(opar, any_expr.expr, cpar, annotations_o.annotations)}
664         | {tuple} opar no many_expr [n2]:no cpar annotations_o {-> New expr.par(opar, many_expr.expr, cpar, annotations_o.annotations)}
665         ;
666
667 many_expr {->expr}
668         = any_expr many_expr_tail+ {-> New expr.many([any_expr.expr, many_expr_tail.expr])}
669         ;
670
671 many_expr_tail {->expr}
672         = comma no any_expr {-> any_expr.expr}
673         ;
674
675 array_items {-> expr*}
676         = array_item array_items_tail* {-> [array_item.expr, array_items_tail.expr]}
677         ;
678 array_items_tail {-> expr}
679         = comma no array_item {-> array_item.expr}
680         ;
681 array_item {-> expr}
682         = expr no {-> expr}
683         | {for} kwfor no [ids]:idlist [n2]:no kwin [n3]:no expr [n4]:no kwdo [block]:array_item {-> New expr.for(kwfor, [ids.id], kwin, expr, kwdo, block.expr, Null)}
684         | {if} kwif [n1]:no expr [n2]:no kwthen [n3]:no [then]:array_item {-> New expr.if(kwif, expr, kwthen, then.expr, Null, Null)}
685         ;
686
687 superstring {-> expr} 
688         = superstring_start superstring_middle* superstring_end annotations_o {-> New expr.superstring([superstring_start.expr, superstring_middle.expr, superstring_end.expr], annotations_o.annotations)};
689 superstring_start {-> expr*}
690         = start_string_p no expr [n2]:no {-> [start_string_p.expr, expr]}
691         | {noexpr} start_string_p no {-> [start_string_p.expr]}
692         ;
693 start_string_p {-> expr}
694         = start_string {-> New expr.start_string(start_string)};
695 superstring_middle {-> expr*}
696         = mid_string_p no expr [n2]:no {-> [mid_string_p.expr, expr]}
697         | {noexpr} mid_string_p no {-> [mid_string_p.expr]}
698         ;
699 mid_string_p {-> expr}
700         = mid_string {-> New expr.mid_string(mid_string)};
701 superstring_end {-> expr}
702         = end_string {-> New expr.end_string(end_string)};
703
704 /* ANNOTATIONS *******************************************************************/
705
706 annotations~nopar {-> annotations}
707         = {one} at one_annotation~nopar {-> New annotations(Null, at, Null, [one_annotation~nopar.annotation], Null, Null)}
708         | {many} at opar no annotation_list [n2]:no cpar {-> New annotations(Null, at, opar, [annotation_list.annotation], cpar, Null)}
709         ;
710 annotations_o~nopar {-> annotations?}
711         = annotations~nopar {-> annotations~nopar.annotations}
712         | {null} {-> Null}
713         ;
714
715 one_annotation~nopar {-> annotation}
716         = {alone} redef visibility atid annotations_o~nopar {-> New annotation(Null, redef.kwredef, visibility, atid, Null, [], Null, annotations_o~nopar.annotations)}
717 // !nopar to unambiguise 'new T@foo(bar)' between 'new T@(foo(bar))' and 'new (T@foo)(bar)'
718 !nopar  | {args} redef visibility atid opar no at_args [n2]:no cpar annotations_o~nopar {-> New annotation(Null, redef.kwredef, visibility, atid, opar, [at_args.expr], cpar, annotations_o~nopar.annotations)}
719         ;
720
721 annotation_list {-> annotation*}
722         = {many} one_annotation_list annotations_tail* {-> [one_annotation_list.annotation, annotations_tail.annotation] }
723         ;
724
725 one_annotation_list~nopar {-> annotation}
726         = {alone} redef visibility atid annotations_o~nopar {-> New annotation(Null, redef.kwredef, visibility, atid, Null, [], Null, annotations_o~nopar.annotations)}
727 // !nopar to unambiguise 'new T@foo(bar)' between 'new T@(foo(bar))' and 'new (T@foo)(bar)'
728 !nopar  | {args} redef visibility atid opar no at_args [n2]:no cpar annotations_o~nopar {-> New annotation(Null, redef.kwredef, visibility, atid, opar, [at_args.expr], cpar, annotations_o~nopar.annotations)}
729 !nopar  | {nopar} redef visibility atid at_arg_single {-> New annotation(Null, redef.kwredef, visibility, atid, Null, [at_arg_single.expr], Null, Null)}
730         ;
731 at_arg_single {-> expr}
732 // FIXME: why expr_single but not expr_atom is not clear :(
733         = {expr} [expr]:expr_single_nopar {-> expr.expr}
734         ;
735
736 annotations_tail {-> annotation}
737         = comma no one_annotation_list {-> one_annotation_list.annotation}
738         ;
739
740 line_annotation {-> annotation}
741         = [doc]:nd redef visibility atid annotations? n1 {-> New annotation(doc.doc, redef.kwredef, visibility, atid.atid, Null, [], Null, annotations)}
742         | {args} [doc]:nd redef visibility atid opar no at_args cpar annotations? n1 {-> New annotation(doc.doc, redef.kwredef, visibility, atid.atid, opar, [at_args.expr], cpar, annotations)}
743         | {nopar} [doc]:nd redef visibility atid at_args_nopar n1 {-> New annotation(doc.doc, redef.kwredef, visibility, atid.atid, Null, [at_args_nopar.expr], Null, Null)}
744         ;
745 line_annotation_forclass {-> propdef}
746         = [doc]:nd atid_forclass annotations? {-> New propdef.annot(doc.doc, Null, Null, atid_forclass.atid, Null, [], Null, annotations)}
747         | {args} [doc]:nd atid_forclass opar no at_args cpar annotations? {-> New propdef.annot(doc.doc, Null, Null, atid_forclass.atid, opar, [at_args.expr], cpar, annotations)}
748         | {nopar} [doc]:nd atid_forclass at_args_nopar {-> New propdef.annot(doc.doc, Null, Null, atid_forclass.atid, Null, [at_args_nopar.expr], Null, Null)}
749         ;
750
751 at_args~nopar {-> expr* }
752         = {many} any_expr~nopar at_args_tail* {-> [any_expr~nopar.expr, at_args_tail.expr]}
753         ;
754
755 at_args_tail {-> expr}
756         = comma no any_expr {-> any_expr.expr}
757         ;
758
759 any_expr~nopar {-> expr}
760         = {type} type {-> New expr.type(type)}
761         | {expr} expr~nopar {-> expr~nopar.expr}
762         | {stmt} stmt_noexpr~nopar {-> stmt_noexpr~nopar.expr}
763         | {methid} recv~nopar qmethid_noid {-> New expr.methid(recv~nopar.expr, qmethid_noid.methid)}
764 !nopar  | {at} annotations {-> New expr.at(annotations.annotations)}
765         ;
766
767 atid~forclass {-> atid}
768         = {id}  id {-> New atid.id(id)}
769 !forclass       | {kwextern} kwextern {-> New atid.kwextern(kwextern)}
770 //      | {kwimport} kwimport {-> New atid.kwimport(kwimport)}
771         | {kwabstract} kwabstract {-> New atid.kwabstract(kwabstract)}
772         ;
773
774 /* MISC **********************************************************************/
775
776 recv~nopar~nobra {-> expr}
777         = expr_atom~nopar~nobra dot no {-> expr_atom~nopar~nobra.expr}
778         | {implicit} {-> New expr.implicit_self()}
779         ;
780
781 args {-> exprs}
782         = args_n {-> args_n.exprs}
783         | {empty} {-> New exprs.list([])}
784         ;
785 args_n {-> exprs}
786         = opar no expr_list cpar {-> New exprs.par(opar, [expr_list.expr], cpar) }
787         | {emptypar} opar no cpar {-> New exprs.par(opar, [], cpar) }
788         ;
789 args_nopar {-> exprs}
790         = opar no expr_list cpar {-> New exprs.par(opar, [expr_list.expr], cpar) }
791         | {onearg} expr_nopar {-> New exprs.list([expr_nopar.expr])}
792         | {emptypar} opar no cpar {-> New exprs.par(opar, [], cpar) }
793         | {empty} {-> New exprs.list([])}
794         ;
795 braargs {-> exprs}
796         = obra no expr_list cbra {-> New exprs.bra(obra, [expr_list.expr], cbra)};
797 expr_list {-> expr*}
798         = arg [n2]:no expr_tail* {-> [arg.expr, expr_tail.expr]};
799 expr_tail {-> expr} 
800         = comma no arg [n2]:no {-> arg.expr};
801 idlist {-> id*}
802         = opar no idlist_nopar [n2]:no cpar {-> [idlist_nopar.id]}
803         | {nopar} idlist_nopar {-> [idlist_nopar.id]}
804         ;
805 idlist_nopar {-> id*}
806         = {single} id {-> [id]}
807         | {more} idlist_nopar comma [n2]:no id {-> [idlist_nopar.id, id]}
808         ;
809
810 module_name {-> module_name}
811         = {mod} modquad* id {-> New module_name(Null, [modquad.id], id)}
812         | {root} quad no modquad* id {-> New module_name(quad, [modquad.id], id)}
813         ;
814
815 qualified 
816         = {cla} modquad* classquad {-> New qualified([modquad.id], classquad.classid)}
817         | {mod} modquad+ {-> New qualified([modquad.id], Null)}
818         ; 
819 qualified_o {-> qualified?}
820         = qualified {-> qualified}
821         | {null} {-> Null}
822         ;
823 qid {-> id}
824         = qualified? id {-> id}
825         ;
826 qclassid {-> classid}
827         = qualified? classid {-> classid}
828         ;
829 qmethid~noid {-> methid}
830         = qualified? methid~noid {-> methid~noid.methid}
831         ;
832 modquad {-> id}
833         = id quad no {-> id};
834 classquad {-> classid} 
835         = classid quad no {-> classid};
836
837 kwend_o {-> kwend?}
838         = kwend? {-> kwend}
839         ;
840
841 /* A single hard break */
842 n1      = {a} comment | {b} eol | {c} semi;
843
844 /* A mandatory hard break, returns the last comment */
845 n~nosemi {-> doc?}
846         = {a} n2* comment+ {-> New doc([comment])}
847         | {b} n2+ {-> Null}
848 !nosemi | {c} n2* comment* semi n? {-> n.doc }
849         ;
850
851 /* An optional hard break, returns the last comment.
852  * Used mainly to introduce optional documentation */
853 nd {-> doc?}
854         = {empty} {-> Null}
855         | n {-> n.doc}
856         ;
857
858 /* An optional soft break.
859  * Used when a unambiguous line break or comment could be inserted/ */
860 no
861         = {empty}
862         | n_nosemi
863         ;
864
865 n2
866         = {a} comment* eol
867         ;
868
869 /*****************************************************************************/
870 Abstract Syntax Tree
871 /*****************************************************************************/
872
873 module  = moduledecl? [imports]:import* [extern_code_blocks]:extern_code_block* [classdefs]:classdef*;
874
875 moduledecl
876         = doc? kwredef? visibility kwmodule [name]:module_name annotations?;
877
878 import  = {std} visibility kwimport [name]:module_name annotations?
879         | {no} visibility kwimport kwend
880         ;
881
882 visibility
883         = {public} kwpublic?
884         | {private} kwprivate
885         | {protected} kwprotected
886         | {intrude} kwintrude
887         ;
888
889 classdef= {std} doc? kwredef? visibility classkind [id]:classid? obra? [formaldefs]:formaldef* cbra? extern_code_block? [propdefs]:propdef* kwend
890         | {top} [propdefs]:propdef*
891         | {main} [propdefs]:propdef*
892         ;
893 classkind
894         = {concrete} kwclass
895         | {abstract} kwabstract kwclass
896         | {interface} kwinterface
897         | {enum} kwenum
898         | {extern} kwextern kwclass?
899         ;
900 formaldef = [id]:classid type? annotations?;
901
902
903 propdef = {attr} doc? kwredef? visibility kwvar [id2]:id type? assign? expr? annotations? kwdo? [block]:expr? kwend?
904         | {main_meth} kwredef? [block]:expr?
905         | {type} doc? kwredef? visibility kwtype [id]:classid type annotations?
906         | {meth} doc? kwredef? visibility kwmeth? kwinit? kwnew? methid? signature annotations? extern_calls? extern_code_block? kwdo? [block]:expr? kwend?
907         | {super} doc? kwredef? visibility kwsuper type annotations?
908         | {annot} doc? kwredef? visibility? atid opar? [args]:expr* cpar? annotations?
909         ;
910
911 methid
912         = {id} id
913         | {plus} [op]:plus
914         | {minus} [op]:minus
915         | {star} [op]:star
916         | {starstar} [op]:starstar
917         | {slash} [op]:slash
918         | {percent} [op]:percent
919         | {eq} [op]:eq
920         | {ne} [op]:ne
921         | {le} [op]:le
922         | {ge} [op]:ge
923         | {lt} [op]:lt
924         | {gt} [op]:gt
925         | {ll} [op]:ll
926         | {gg} [op]:gg
927         | {starship} [op]:starship
928         | {pipe} [op]:pipe
929         | {caret} [op]:caret
930         | {amp} [op]:amp
931         | {tilde} [op]:tilde
932         | {bra} obra cbra
933         | {assign} id assign
934         | {braassign} obra cbra assign
935         ;
936
937 signature = opar? [params]:param* cpar? type?;
938
939 param   = id type? dotdotdot? annotations?
940         ;
941
942 type    = kwnullable? [id]:classid obra? [types]:type* cbra? annotations?;
943
944 label = kwlabel id?;
945
946 expr    = {block} expr* kwend? 
947         | {vardecl} kwvar? id type? assign? expr? annotations?
948         | {return} kwreturn? expr?
949         | {break} kwbreak label?
950         | {abort} kwabort
951         | {continue} kwcontinue? label?
952         | {do} kwdo [block]:expr? label?
953         | {if} kwif expr kwthen [then]:expr? kwelse? [else]:expr?
954         | {ifexpr} kwif expr kwthen [then]:expr kwelse [else]:expr
955         | {while} kwwhile expr kwdo [block]:expr? label?
956         | {loop} kwloop [block]:expr? label?
957         | {for} kwfor [ids]:id* kwin expr kwdo [block]:expr? label?
958         | {with} kwwith expr kwdo [block]:expr? label?
959         | {assert} kwassert id? expr kwelse? [else]:expr?
960         | {once} kwonce expr 
961         | {send} expr 
962         | {binop} expr [expr2]:expr 
963         | {or} expr [op]:kwor [expr2]:expr
964         | {and} expr [op]:kwand [expr2]:expr
965         | {or_else} expr [op]:kwor kwelse [expr2]:expr
966         | {implies} expr [op]:kwimplies [expr2]:expr
967         | {not} kwnot expr
968         | {eq} expr [op]:eq [expr2]:expr
969         | {ne} expr [op]:ne [expr2]:expr
970         | {lt} expr [op]:lt [expr2]:expr
971         | {le} expr [op]:le [expr2]:expr
972         | {ll} expr [op]:ll [expr2]:expr
973         | {gt} expr [op]:gt [expr2]:expr
974         | {ge} expr [op]:ge [expr2]:expr
975         | {gg} expr [op]:gg [expr2]:expr
976         | {isa} expr kwisa type
977         | {plus} expr [op]:plus [expr2]:expr
978         | {minus} expr [op]:minus [expr2]:expr
979         | {starship} expr [op]:starship [expr2]:expr
980         | {star} expr [op]:star [expr2]:expr
981         | {starstar} expr [op]:starstar [expr2]:expr
982         | {slash} expr [op]:slash [expr2]:expr
983         | {percent} expr [op]:percent [expr2]:expr
984         | {pipe} expr [op]:pipe [expr2]:expr
985         | {caret} expr [op]:caret [expr2]:expr
986         | {amp} expr [op]:amp [expr2]:expr
987         | {uminus} [op]:minus expr
988         | {uplus} [op]:plus expr
989         | {utilde} [op]:tilde expr
990         | {new} kwnew type id? [args]:exprs
991         | {attr} expr [id]:attrid 
992         | {attr_assign} expr [id]:attrid assign [value]:expr 
993         | {attr_reassign} expr [id]:attrid assign_op [value]:expr 
994         | {call} expr id [args]:exprs
995         | {call_assign} expr id [args]:exprs assign [value]:expr
996         | {call_reassign} expr id [args]:exprs assign_op [value]:expr 
997         | {super} qualified? kwsuper [args]:exprs
998         | {init} expr kwinit [args]:exprs 
999         | {bra} expr [args]:exprs
1000         | {bra_assign} expr [args]:exprs assign [value]:expr 
1001         | {bra_reassign} expr [args]:exprs assign_op [value]:expr 
1002         | {var} id
1003         | {var_assign} id assign [value]:expr 
1004         | {var_reassign} id assign_op [value]:expr 
1005         | {range} expr [expr2]:expr annotations?
1006         | {crange} obra expr dotdot [expr2]:expr cbra annotations?
1007         | {orange} obra expr dotdot [expr2]:expr [cbra]:obra annotations?
1008         | {array} obra [exprs]:expr* type? cbra annotations?
1009         | {self} kwself annotations?
1010         | {implicit_self} 
1011         | {true} kwtrue annotations?
1012         | {false} kwfalse annotations?
1013         | {null} kwnull annotations?
1014         | {dec_int} number annotations?
1015         | {hex_int} hex_number annotations?
1016         | {bin_int} bin_number annotations?
1017         | {oct_int} oct_number annotations?
1018         | {dec_byte} bytenum annotations?
1019         | {hex_byte} hex_bytenum annotations?
1020         | {bin_byte} bin_bytenum annotations?
1021         | {oct_byte} oct_bytenum annotations?
1022         | {float} float annotations?
1023         | {char} char annotations?
1024         | {string} string annotations?
1025         | {start_string} [string]:start_string 
1026         | {mid_string} [string]:mid_string 
1027         | {end_string} [string]:end_string 
1028         | {superstring} [exprs]:expr* annotations?
1029         | {par} opar expr cpar annotations?
1030         | {as_cast} expr kwas opar? type cpar?
1031         | {as_notnull} expr kwas opar? kwnot kwnull cpar?
1032         | {isset_attr} kwisset expr [id]:attrid
1033         | {debug_type} kwdebug kwtype expr type
1034         | {vararg} expr dotdotdot
1035         | {namedarg} id assign expr
1036         | {type} type
1037         | {methid} expr [id]:methid
1038         | {at} annotations
1039         | {many} [exprs]:expr*
1040         ;
1041 exprs
1042         = {list} [exprs]:expr*
1043         | {par} opar [exprs]:expr* cpar
1044         | {bra} obra [exprs]:expr* cbra
1045         ;
1046 assign_op
1047         = {plus} [op]:pluseq
1048         | {minus}[op]:minuseq
1049         | {star} [op]:stareq
1050         | {slash} [op]:slasheq
1051         | {percent} [op]:percenteq
1052         | {starstar} [op]:starstareq
1053         | {pipe} [op]:pipeeq
1054         | {caret} [op]:careteq
1055         | {amp} [op]:ampeq
1056         | {ll} [op]:lleq
1057         | {gg} [op]:ggeq
1058         ;
1059
1060 module_name = quad? [path]:id* id;
1061 extern_calls = kwimport [extern_calls]:extern_call*
1062         ;
1063 extern_call =
1064         | {super} kwsuper
1065         | {local_prop} methid
1066         | {full_prop} type dot? methid
1067         | {init_prop} type
1068         | {cast_as} [from_type]:type dot? kwas [to_type]:type
1069         | {as_nullable} type kwas kwnullable
1070         | {as_not_nullable} type kwas kwnot kwnullable
1071         ;
1072 in_language = kwin string;
1073 extern_code_block = in_language? extern_code_segment;
1074
1075 qualified = id* classid? ;
1076
1077 doc = comment+;
1078
1079 annotations = kwis? at? opar? [items]:annotation* cpar? kwend?;
1080
1081 annotation = doc? kwredef? visibility? atid opar? [args]:expr* cpar? annotations?;
1082
1083 atid = {id} id | {kwextern} [id]:kwextern | {kwabstract} [id]:kwabstract | {kwimport} [id]:kwimport;
1084
1085 /*****************************************************************************/
1086