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