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