-
Notifications
You must be signed in to change notification settings - Fork 5
/
ada.g2
1947 lines (1705 loc) · 52.7 KB
/
ada.g2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Ada95 Recognizer for ANTLR
*
* Oliver M. Kellogg <okellogg@users.sourceforge.net>
*
* Adapted from lexer9x.l/grammar9x.y,
*
******* A YACC grammar for Ada 9X *********************************
* Copyright (C) Intermetrics, Inc. 1994 Cambridge, MA USA *
* Copying permitted if accompanied by this statement. *
* Derivative works are permitted if accompanied by this statement.*
* This grammar is thought to be correct as of May 1, 1994 *
* but as usual there is *no warranty* to that effect. *
*******************************************************************
*
* $Id: ada.g,v 1.2 2003/08/02 20:00:00 okellogg Exp $
*
* Not all rules from the Ada95 Reference Manual (RM) Annex P,
* Syntax Summary, are mirrored as rules here.
* The tree nodes follow the RM grammar as closely as sensible.
* This applies in particular to the terminals. OTOH, trivially
* reconstructable non-terminal rules are not reflected in the tree.
* FIXME: Document the exact rationale of the tree design.
*
*/
header "pre_include_hpp" {
#include <antlr/SemanticException.hpp> // antlr wants this
#include "AdaAST.hpp"
#include "preambles.h"
}
options {
language="Cpp";
}
//-----------------------------------------------------------------------------
// Define a Parser, calling it AdaParser
//-----------------------------------------------------------------------------
class AdaParser extends Parser;
options {
k = 2; // token lookahead
exportVocab=Ada; // Call its vocabulary "Ada"
// codeGenMakeSwitchThreshold = 2; // Some optimizations
// codeGenBitsetTestThreshold = 3;
defaultErrorHandler = true; // Generate parser error handlers
buildAST = true;
ASTLabelType = "RefAdaAST";
}
{
ANTLR_PARSER_PREAMBLE
public:
// Ada support stuff
void push_def_id (const RefAdaAST& defid);
const RefAdaAST& pop_def_id ();
bool end_id_matches_def_id (const RefAdaAST& endid);
bool definable_operator (const char *string); // operator_symbol sans "/="
bool is_operator_symbol (const char *string);
}
// Compilation Unit: This is the start rule for this parser.
// The rules in this grammar are listed in the order in which
// compilation_unit introduces them, depth first, with the
// exception of the expression related rules which are listed
// towards the end.
compilation_unit
: context_items_opt ( library_item | subunit ) ( pragma )*
;
// The pragma related rules are pulled up here to get them out of the way.
pragma : PRAGMA^ IDENTIFIER pragma_args_opt SEMI!
;
pragma_args_opt : ( LPAREN! pragma_arg ( COMMA! pragma_arg )* RPAREN! )?
;
pragma_arg : ( IDENTIFIER RIGHT_SHAFT^ )? expression
;
context_items_opt : ( pragma )* ( with_clause ( use_clause | pragma )* )*
{ #context_items_opt =
#(#[CONTEXT_CLAUSE, "CONTEXT_CLAUSE"], #context_items_opt); }
// RM Annex P neglects pragmas; we include them.
// The node should really be named CONTEXT_ITEMS_OPT, but we
// stick with the RM wording.
;
with_clause : w:WITH^ c_name_list SEMI!
{ Set(#w, WITH_CLAUSE); }
;
c_name_list : compound_name ( COMMA! compound_name )*
;
compound_name : IDENTIFIER ( DOT^ IDENTIFIER )*
// Strangely, the RM never defines this rule, which however is
// required for tightening up the syntax of certain names
// (library unit names etc.)
;
use_clause : u:USE^
( TYPE! subtype_mark ( COMMA! subtype_mark )*
{ Set(#u, USE_TYPE_CLAUSE); }
| c_name_list { Set(#u, USE_CLAUSE); }
)
SEMI!
;
subtype_mark : compound_name ( TIC^ attribute_id )?
// { #subtype_mark = #(#[SUBTYPE_MARK, "SUBTYPE_MARK"], #subtype_mark); }
;
attribute_id : RANGE
| DIGITS
| DELTA
| ACCESS
| IDENTIFIER
;
library_item : private_opt
/* Slightly loose; PRIVATE can only precede
{generic|package|subprog}_decl.
Semantic check required to ensure it.*/
( lib_pkg_spec_or_body
| subprog_decl_or_rename_or_inst_or_body[true]
| generic_decl[true]
)
{ #library_item = #(#[LIBRARY_ITEM, "LIBRARY_ITEM"], #library_item); }
;
private_opt : ( PRIVATE )?
{ #private_opt = #(#[MODIFIERS, "MODIFIERS"], #private_opt); }
;
lib_pkg_spec_or_body
: pkg:PACKAGE^
( BODY! def_id[true] IS! pkg_body_part end_id_opt! SEMI!
{ Set(#pkg, PACKAGE_BODY); }
| def_id[true] spec_decl_part[#pkg]
)
;
subprog_decl [bool lib_level]
{ RefAdaAST t; }
: p:PROCEDURE^ def_id[lib_level]
( generic_subp_inst
{ Set(#p, GENERIC_PROCEDURE_INSTANTIATION); }
| formal_part_opt
( renames { Set(#p, PROCEDURE_RENAMING_DECLARATION); }
| is_separate_or_abstract_or_decl[#p]
)
SEMI!
)
| f:FUNCTION^ def_designator[lib_level]
( generic_subp_inst
{ Set(#f, GENERIC_FUNCTION_INSTANTIATION); }
| function_tail
( renames { Set(#f, FUNCTION_RENAMING_DECLARATION); }
| is_separate_or_abstract_or_decl[#f]
)
SEMI!
)
;
def_id [bool lib_level]
: { lib_level }? cn:compound_name { push_def_id(#cn); }
| { !lib_level }? n:IDENTIFIER { push_def_id(#n); }
;
generic_subp_inst : IS! generic_inst SEMI!
;
generic_inst : NEW! compound_name ( LPAREN! value_s RPAREN! )?
{ pop_def_id(); }
;
parenth_values : LPAREN! value ( COMMA! value )* RPAREN!
;
value : ( OTHERS^ RIGHT_SHAFT! expression
| ranged_expr_s ( RIGHT_SHAFT^ expression )?
)
// { #value = #(#[VALUE, "VALUE"], #value); }
;
ranged_expr_s : ranged_expr ( PIPE^ ranged_expr )*
// { #ranged_expr_s =
// #(#[RANGED_EXPRS, "RANGED_EXPRS"], #ranged_expr_s); }
;
ranged_expr : expression
( DOT_DOT^ simple_expression
| RANGE^ range
)?
;
range_constraint : RANGE! range
;
range : ( (range_dots) => range_dots
| range_attrib_ref
)
// Current assumption is we don't need an extra node for range,
// otherwise uncomment the following line:
// { #range = #(#[RANGE_EXPR, "RANGE_EXPR"], #range); }
;
range_dots : simple_expression DOT_DOT^ simple_expression
;
range_attrib_ref : // "name TIC RANGE" is ambiguous; instead:
prefix TIC! r:RANGE^ ( LPAREN! expression RPAREN! )?
{ Set(#r, RANGE_ATTRIBUTE_REFERENCE); }
;
// Here, the definition of `prefix' deviates from the RM.
// This gives us some more strictness than `name' (which the RM uses to
// define `prefix'.)
prefix : IDENTIFIER
( DOT^ ( ALL | IDENTIFIER )
| p:LPAREN^ value_s RPAREN!
{ Set(#p, INDEXED_COMPONENT); }
)*
;
formal_part_opt : ( LPAREN! parameter_specification
( SEMI! parameter_specification )*
RPAREN! )?
{ #formal_part_opt = #([FORMAL_PART_OPT, "FORMAL_PART_OPT"],
#formal_part_opt); }
;
parameter_specification : def_ids_colon mode_opt subtype_mark init_opt
{ #parameter_specification =
#(#[PARAMETER_SPECIFICATION,
"PARAMETER_SPECIFICATION"], #parameter_specification); }
;
def_ids_colon : defining_identifier_list COLON!
;
defining_identifier_list : IDENTIFIER ( COMMA! IDENTIFIER )*
{ #defining_identifier_list =
#(#[DEFINING_IDENTIFIER_LIST,
"DEFINING_IDENTIFIER_LIST"], #defining_identifier_list); }
;
mode_opt : ( IN ( OUT )? | OUT | ACCESS )?
{ #mode_opt = #(#[MODIFIERS, "MODIFIERS"], #mode_opt); }
;
renames { RefAdaAST dummy; }
: RENAMES! ( name
| dummy=definable_operator_symbol
)
{ pop_def_id(); }
;
name { RefAdaAST dummy; }
: IDENTIFIER
( DOT^ ( ALL
| IDENTIFIER
| CHARACTER_LITERAL
| dummy=is_operator
)
| p:LPAREN^ value_s RPAREN!
{ Set(#p, INDEXED_COMPONENT); }
| TIC^ attribute_id // must be in here because of e.g.
// Character'Pos (x)
)*
// { #name = #(#[NAME, "NAME"], #name); }
;
is_operator returns [RefAdaAST d]
: { is_operator_symbol(LT(1)->getText().c_str()) }?
op:CHAR_STRING { #op->setType(OPERATOR_SYMBOL); d=#op; }
;
definable_operator_symbol returns [RefAdaAST d]
: { definable_operator(LT(1)->getText().c_str()) }?
op:CHAR_STRING { #op->setType(OPERATOR_SYMBOL); d=#op; }
;
parenthesized_primary : pp:LPAREN^
( NuLL RECORD!
| value_s extension_opt
)
RPAREN!
{ Set(#pp, PARENTHESIZED_PRIMARY); }
;
extension_opt : ( WITH! ( NuLL RECORD! | value_s ) )?
{ #extension_opt =
#(#[EXTENSION_OPT, "EXTENSION_OPT"], #extension_opt); }
;
is_separate_or_abstract_or_decl! [RefAdaAST t]
: IS! separate_or_abstract[t]
| { pop_def_id();
if (t->getType() == AdaTokenTypes::PROCEDURE)
Set(t, PROCEDURE_DECLARATION);
else
Set(t, FUNCTION_DECLARATION);
}
;
separate_or_abstract! [RefAdaAST t]
: SEPARATE!
{ pop_def_id();
if (t->getType() == AdaTokenTypes::PROCEDURE)
Set(t, PROCEDURE_BODY_STUB);
else
Set(t, FUNCTION_BODY_STUB);
}
| ABSTRACT!
{ pop_def_id();
if (t->getType() == AdaTokenTypes::PROCEDURE)
Set(t, ABSTRACT_PROCEDURE_DECLARATION);
else
Set(t, ABSTRACT_FUNCTION_DECLARATION);
}
;
def_designator [bool lib_level]
{ RefAdaAST d; }
: { lib_level }? n:compound_name { push_def_id(#n); }
| { !lib_level }? d=designator { push_def_id(d); }
;
designator returns [RefAdaAST d]
{ RefAdaAST op; }
: op=definable_operator_symbol { d = op; }
| n:IDENTIFIER { d = #n; }
;
function_tail : func_formal_part_opt RETURN! subtype_mark
;
// formal_part_opt is not strict enough for functions, i.e. it permits
// "in out" and "out" as modes, thus we make an extra rule:
func_formal_part_opt : ( LPAREN! func_param ( SEMI! func_param )* RPAREN! )?
{ #func_formal_part_opt =
#([FORMAL_PART_OPT,
"FORMAL_PART_OPT"], #func_formal_part_opt); }
;
func_param : def_ids_colon in_access_opt subtype_mark init_opt
{ #func_param =
#(#[PARAMETER_SPECIFICATION,
"PARAMETER_SPECIFICATION"], #func_param); }
;
in_access_opt : ( IN! | ACCESS )?
{ #in_access_opt = #(#[MODIFIERS, "MODIFIERS"], #in_access_opt); }
;
spec_decl_part [RefAdaAST pkg]
: ( IS! ( generic_inst { Set(pkg, GENERIC_PACKAGE_INSTANTIATION); }
| pkg_spec_part { Set(pkg, PACKAGE_SPECIFICATION); }
)
| renames { Set(pkg, PACKAGE_RENAMING_DECLARATION); }
)
SEMI!
;
pkg_spec_part : basic_declarative_items_opt
( PRIVATE! basic_declarative_items_opt )?
end_id_opt!
;
basic_declarative_items_opt : ( basic_decl_item | pragma )*
{ #basic_declarative_items_opt =
#(#[BASIC_DECLARATIVE_ITEMS_OPT,
"BASIC_DECLARATIVE_ITEMS_OPT"],
#basic_declarative_items_opt); }
;
basic_declarative_items : ( basic_decl_item | pragma )+
{ #basic_declarative_items =
#(#[BASIC_DECLARATIVE_ITEMS_OPT,
"BASIC_DECLARATIVE_ITEMS_OPT"],
#basic_declarative_items); }
;
basic_decl_item
: pkg:PACKAGE^ def_id[false] spec_decl_part[#pkg]
| tsk:TASK^ task_type_or_single_decl[#tsk]
| pro:PROTECTED^ prot_type_or_single_decl[#pro] SEMI!
| subprog_decl[false]
| decl_common
;
task_type_or_single_decl [RefAdaAST tsk]
: TYPE! def_id[false] discrim_part_opt task_definition_opt
{ Set(tsk, TASK_TYPE_DECLARATION); }
| def_id[false] task_definition_opt
{ Set(tsk, SINGLE_TASK_DECLARATION); }
;
task_definition_opt
: IS! task_items_opt private_task_items_opt end_id_opt! SEMI!
| SEMI! { pop_def_id(); }
;
discrim_part_opt
: ( discrim_part_text )?
{ #discrim_part_opt =
#(#[DISCRIM_PART_OPT,
"DISCRIM_PART_OPT"], #discrim_part_opt); }
;
discrim_part_text : LPAREN! (BOX | discriminant_specifications) RPAREN!
;
known_discrim_part
: LPAREN! discriminant_specifications RPAREN!
{ #known_discrim_part =
#(#[DISCRIM_PART_OPT,
"DISCRIM_PART_OPT"], #known_discrim_part); }
;
empty_discrim_opt : /* empty */
{ #empty_discrim_opt =
#(#[DISCRIM_PART_OPT,
"DISCRIM_PART_OPT"], #empty_discrim_opt); }
;
discrim_part
: discrim_part_text
{ #discrim_part =
#(#[DISCRIM_PART_OPT,
"DISCRIM_PART_OPT"], #discrim_part); }
;
discriminant_specifications : discriminant_specification
( SEMI! discriminant_specification )*
{ #discriminant_specifications =
#(#[DISCRIMINANT_SPECIFICATIONS,
"DISCRIMINANT_SPECIFICATIONS"],
#discriminant_specifications); }
;
discriminant_specification : def_ids_colon access_opt subtype_mark init_opt
{ #discriminant_specification =
#(#[DISCRIMINANT_SPECIFICATION,
"DISCRIMINANT_SPECIFICATION"],
#discriminant_specification); }
;
access_opt : ( ACCESS )?
{ #access_opt = #(#[MODIFIERS, "MODIFIERS"], #access_opt); }
;
init_opt : ( ASSIGN! expression )?
{ #init_opt = #(#[INIT_OPT, "INIT_OPT"], #init_opt); }
; // `expression' is of course much too loose;
// semantic checks are required in the usage contexts.
task_items_opt : ( pragma )* entrydecls_repspecs_opt
{ #task_items_opt =
#(#[TASK_ITEMS_OPT, "TASK_ITEMS_OPT"], #task_items_opt); }
;
entrydecls_repspecs_opt : ( entry_declaration ( pragma | rep_spec )* )*
;
entry_declaration : e:ENTRY^ IDENTIFIER
discrete_subtype_def_opt formal_part_opt SEMI!
{ Set (#e, ENTRY_DECLARATION); }
;
discrete_subtype_def_opt : ( (LPAREN discrete_subtype_definition RPAREN) =>
LPAREN! discrete_subtype_definition RPAREN!
| /* empty */
)
{ #discrete_subtype_def_opt =
#(#[DISCRETE_SUBTYPE_DEF_OPT,
"DISCRETE_SUBTYPE_DEF_OPT"], #discrete_subtype_def_opt); }
;
discrete_subtype_definition : ( (range) => range
| subtype_ind
)
// Looks alot like discrete_range, but it's not
// (as soon as we start doing semantics.)
/* TBC: No need for extra node, just use the inner nodes?
{ #discrete_subtype_definition =
#(#[DISCRETE_SUBTYPE_DEFINITION,
"DISCRETE_SUBTYPE_DEFINITION"],
#discrete_subtype_definition); }
*/
;
rep_spec : r:FOR^ subtype_mark USE! rep_spec_part[#r] SEMI!
;
rep_spec_part [RefAdaAST t]
: RECORD! align_opt comp_loc_s END! RECORD! // record_type_spec
{ Set(t, RECORD_REPRESENTATION_CLAUSE); }
| AT! expression // address_spec (Ada83)
{ Set(t, AT_CLAUSE); }
| expression // attrib_def. Semantic check must ensure that the
// respective subtype_mark contains an attribute reference.
{ Set(t, ATTRIBUTE_DEFINITION_CLAUSE); }
;
align_opt : ( AT! MOD! expression SEMI! )?
{ #align_opt = #(#[MOD_CLAUSE_OPT, "MOD_CLAUSE_OPT"], #align_opt); }
;
comp_loc_s : ( pragma | subtype_mark AT! expression RANGE! range SEMI! )*
{ #comp_loc_s = #(#[COMPONENT_CLAUSES_OPT, "COMPONENT_CLAUSES_OPT"],
#comp_loc_s); }
;
private_task_items_opt : ( PRIVATE! ( pragma )* entrydecls_repspecs_opt )?
{ #private_task_items_opt =
#(#[PRIVATE_TASK_ITEMS_OPT,
"PRIVATE_TASK_ITEMS_OPT"], #private_task_items_opt); }
// Maybe we could just reuse TASK_ITEMS_OPT here instead of
// making a separate node type.
;
prot_type_or_single_decl [RefAdaAST pro]
: TYPE! def_id[false] discrim_part_opt protected_definition
{ Set(pro, PROTECTED_TYPE_DECLARATION); }
| def_id[false] protected_definition
{ Set(pro, SINGLE_PROTECTED_DECLARATION); }
;
protected_definition
: IS! prot_op_decl_s ( PRIVATE! prot_member_decl_s )? end_id_opt!
;
prot_op_decl_s : ( prot_op_decl )*
{ #prot_op_decl_s = #(#[PROT_OP_DECLARATIONS,
"PROT_OP_DECLARATIONS"], #prot_op_decl_s); }
;
prot_op_decl : entry_declaration
| p:PROCEDURE^ def_id[false] formal_part_opt SEMI!
{ pop_def_id(); Set(#p, PROCEDURE_DECLARATION); }
| f:FUNCTION^ def_designator[false] function_tail SEMI!
{ pop_def_id(); Set(#f, FUNCTION_DECLARATION); }
| rep_spec
| pragma
;
prot_member_decl_s : ( prot_op_decl | comp_decl )*
{ #prot_member_decl_s =
#(#[PROT_MEMBER_DECLARATIONS,
"PROT_MEMBER_DECLARATIONS"], #prot_member_decl_s); }
;
comp_decl : def_ids_colon component_subtype_def init_opt SEMI!
{ #comp_decl =
#(#[COMPONENT_DECLARATION,
"COMPONENT_DECLARATION"], #comp_decl); }
;
// decl_common is shared between declarative_item and basic_decl_item.
// decl_common only contains specifications.
decl_common
: t:TYPE^ IDENTIFIER
( IS! type_def[#t]
| ( discrim_part
( IS! derived_or_private_or_record[#t, true]
| { Set(#t, INCOMPLETE_TYPE_DECLARATION); }
)
| empty_discrim_opt
{ Set(#t, INCOMPLETE_TYPE_DECLARATION); }
// NB: In this case, the discrim_part_opt does not
// appear in the INCOMPLETE_TYPE_DECLARATION node.
)
/* The artificial derived_or_private_or_record rule
gives us some syntax-level control over where a
discrim_part may appear.
However, a semantic check is still necessary to make
sure the discrim_part is not given for a derived type
of an elementary type, or for the full view of a
private type that turns out to be such. */
)
SEMI!
| s:SUBTYPE^ IDENTIFIER IS! subtype_ind SEMI! // subtype_decl
{ Set(#s, SUBTYPE_DECLARATION); }
| generic_decl[false]
| use_clause
| r:FOR^ ( (local_enum_name USE LPAREN) => local_enum_name USE!
enumeration_aggregate
{ Set(#r, ENUMERATION_REPESENTATION_CLAUSE); }
| subtype_mark USE! rep_spec_part[#r]
)
SEMI!
| (IDENTIFIER COLON EXCEPTION RENAMES) =>
IDENTIFIER erd:COLON^ EXCEPTION! RENAMES! compound_name SEMI!
{ Set(#erd, EXCEPTION_RENAMING_DECLARATION); }
| (IDENTIFIER COLON subtype_mark RENAMES) =>
IDENTIFIER ord:COLON^ subtype_mark RENAMES! name SEMI!
{ Set(#ord, OBJECT_RENAMING_DECLARATION); }
| defining_identifier_list od:COLON^ // object_declaration
( EXCEPTION!
{ Set(#od, EXCEPTION_DECLARATION); }
| (CONSTANT ASSIGN) => CONSTANT! ASSIGN! expression
{ Set(#od, NUMBER_DECLARATION); }
| aliased_constant_opt
( array_type_definition[#od] init_opt
{ Set(#od, ARRAY_OBJECT_DECLARATION); }
// Not an RM rule, but simplifies distinction
// from the non-array object_declaration.
| subtype_ind init_opt
{ Set(#od, OBJECT_DECLARATION); }
)
)
SEMI!
;
type_def [RefAdaAST t]
: LPAREN! enum_id_s RPAREN!
{ Set(t, ENUMERATION_TYPE_DECLARATION); }
| RANGE! range
{ Set(t, SIGNED_INTEGER_TYPE_DECLARATION); }
| MOD! expression
{ Set(t, MODULAR_TYPE_DECLARATION); }
| DIGITS! expression range_constraint_opt
{ Set(t, FLOATING_POINT_DECLARATION); }
| DELTA! expression
( RANGE! range
{ Set(t, ORDINARY_FIXED_POINT_DECLARATION); }
| DIGITS! expression range_constraint_opt
{ Set(t, DECIMAL_FIXED_POINT_DECLARATION); }
)
| array_type_definition[t]
| access_type_definition[t]
| empty_discrim_opt derived_or_private_or_record[t, false]
;
enum_id_s : enumeration_literal_specification
( COMMA! enumeration_literal_specification )*
;
enumeration_literal_specification : IDENTIFIER | CHARACTER_LITERAL
;
range_constraint_opt : ( range_constraint )?
;
array_type_definition [RefAdaAST t]
: ARRAY! LPAREN! index_or_discrete_range_s RPAREN!
OF! component_subtype_def
{ Set(t, ARRAY_TYPE_DECLARATION); }
;
index_or_discrete_range_s
: index_or_discrete_range ( COMMA^ index_or_discrete_range )*
;
index_or_discrete_range
: simple_expression
( DOT_DOT^ simple_expression // constrained
| RANGE^ ( BOX // unconstrained
| range // constrained
)
)?
;
component_subtype_def : aliased_opt subtype_ind
;
aliased_opt : ( ALIASED )?
{ #aliased_opt = #(#[MODIFIERS, "MODIFIERS"], #aliased_opt); }
;
subtype_ind : subtype_mark constraint_opt
{ #subtype_ind = #(#[SUBTYPE_INDICATION, "SUBTYPE_INDICATION"],
#subtype_ind); }
;
constraint_opt : ( range_constraint
| digits_constraint
| delta_constraint
| (index_constraint) => index_constraint
| discriminant_constraint
)?
;
digits_constraint : d:DIGITS^ expression range_constraint_opt
{ Set(#d, DIGITS_CONSTRAINT); }
;
delta_constraint : d:DELTA^ expression range_constraint_opt
{ Set(#d, DELTA_CONSTRAINT); }
;
index_constraint : p:LPAREN^ discrete_range ( COMMA! discrete_range )* RPAREN!
{ Set(#p, INDEX_CONSTRAINT); }
;
discrete_range
: (range) => range
| subtype_ind
;
discriminant_constraint : p:LPAREN^ discriminant_association
( COMMA! discriminant_association )* RPAREN!
{ Set(#p, DISCRIMINANT_CONSTRAINT); }
;
discriminant_association : selector_names_opt expression
{ #discriminant_association =
#(#[DISCRIMINANT_ASSOCIATION,
"DISCRIMINANT_ASSOCIATION"], #discriminant_association); }
;
selector_names_opt : ( (association_head) => association_head
| /* empty */
)
{ #selector_names_opt =
#(#[SELECTOR_NAMES_OPT,
"SELECTOR_NAMES_OPT"], #selector_names_opt); }
;
association_head : selector_name ( PIPE! selector_name )* RIGHT_SHAFT!
;
selector_name : IDENTIFIER // TBD: sem pred
;
access_type_definition [RefAdaAST t]
: ACCESS!
( protected_opt
( PROCEDURE! formal_part_opt
{ Set(t, ACCESS_TO_PROCEDURE_DECLARATION); }
| FUNCTION! func_formal_part_opt RETURN! subtype_mark
{ Set(t, ACCESS_TO_FUNCTION_DECLARATION); }
)
| constant_all_opt subtype_ind
{ Set(t, ACCESS_TO_OBJECT_DECLARATION); }
)
;
protected_opt : ( PROTECTED )?
{ #protected_opt = #(#[MODIFIERS, "MODIFIERS"], #protected_opt); }
;
constant_all_opt : ( CONSTANT | ALL )?
{ #constant_all_opt =
#(#[MODIFIERS, "MODIFIERS"], #constant_all_opt); }
;
derived_or_private_or_record [RefAdaAST t, bool has_discrim]
: ( ( ABSTRACT )? NEW subtype_ind WITH ) =>
abstract_opt NEW! subtype_ind WITH!
( PRIVATE! { Set(t, PRIVATE_EXTENSION_DECLARATION); }
| record_definition[has_discrim]
{ Set(t, DERIVED_RECORD_EXTENSION); }
)
| NEW! subtype_ind { Set(t, ORDINARY_DERIVED_TYPE_DECLARATION); }
| abstract_tagged_limited_opt
( PRIVATE! { Set(t, PRIVATE_TYPE_DECLARATION); }
| record_definition[has_discrim]
{ Set(t, RECORD_TYPE_DECLARATION); }
)
;
abstract_opt : ( ABSTRACT )?
{ #abstract_opt = #(#[MODIFIERS, "MODIFIERS"], #abstract_opt); }
;
record_definition [bool has_discrim]
: RECORD! component_list[has_discrim] END! RECORD!
| NuLL! RECORD! // Thus the component_list is optional in the tree.
;
component_list [bool has_discrim]
: NuLL! SEMI! // Thus the component_list is optional in the tree.
| component_items ( variant_part { has_discrim }? )?
| empty_component_items variant_part { has_discrim }?
;
component_items : ( pragma | comp_decl )+
{ #component_items =
#(#[COMPONENT_ITEMS,
"COMPONENT_ITEMS"], #component_items); }
;
empty_component_items :
{ #empty_component_items =
#(#[COMPONENT_ITEMS,
"COMPONENT_ITEMS"], #empty_component_items); }
;
variant_part : c:CASE^ discriminant_direct_name IS! variant_s END! CASE! SEMI!
{ Set (#c, VARIANT_PART); }
;
discriminant_direct_name : IDENTIFIER // TBD: symtab lookup.
;
variant_s : ( variant )+
{ #variant_s = #(#[VARIANTS, "VARIANTS"], #variant_s); }
;
variant : w:WHEN^ choice_s RIGHT_SHAFT! component_list[true]
{ Set (#w, VARIANT); }
;
choice_s : choice ( PIPE^ choice )*
;
choice : OTHERS
| (discrete_with_range) => discrete_with_range
| expression // ( DOT_DOT^ simple_expression )?
; // No, that's already in discrete_with_range
discrete_with_range : (mark_with_constraint) => mark_with_constraint
| range
;
mark_with_constraint : subtype_mark range_constraint
{ #mark_with_constraint =
#(#[MARK_WITH_CONSTRAINT,
"MARK_WITH_CONSTRAINT"], #mark_with_constraint); }
;
abstract_tagged_limited_opt
: ( ABSTRACT TAGGED! | TAGGED )?
( LIMITED )?
{ #abstract_tagged_limited_opt =
#(#[MODIFIERS, "MODIFIERS"], #abstract_tagged_limited_opt); }
;
local_enum_name : IDENTIFIER // to be refined: do a symbol table lookup
;
enumeration_aggregate : parenth_values
;
aliased_constant_opt : ( ALIASED )? ( CONSTANT )?
{ #aliased_constant_opt =
#(#[MODIFIERS, "MODIFIERS"], #aliased_constant_opt); }
;
generic_decl [bool lib_level]
: g:GENERIC^ generic_formal_part_opt
( PACKAGE! def_id[lib_level]
( renames { Set(#g, GENERIC_PACKAGE_RENAMING); }
| IS! pkg_spec_part { Set(#g, GENERIC_PACKAGE_DECLARATION); }
)
| PROCEDURE! def_id[lib_level] formal_part_opt
( renames { Set(#g, GENERIC_PROCEDURE_RENAMING); }
// ^^^ Semantic check must ensure that the (generic_formal)*
// after GENERIC is not given here.
| { Set(#g, GENERIC_PROCEDURE_DECLARATION); pop_def_id(); }
)
| FUNCTION! def_designator[lib_level] function_tail
( renames { Set(#g, GENERIC_FUNCTION_RENAMING); }
// ^^^ Semantic check must ensure that the (generic_formal)*
// after GENERIC is not given here.
| { Set(#g, GENERIC_FUNCTION_DECLARATION); pop_def_id(); }
)
)
SEMI!
;
generic_formal_part_opt : ( use_clause | pragma | generic_formal_parameter )*
{ #generic_formal_part_opt =
#(#[GENERIC_FORMAL_PART,
"GENERIC_FORMAL_PART"],
#generic_formal_part_opt); }
;
generic_formal_parameter :
( t:TYPE^ def_id[false]
( IS!
( LPAREN! BOX! RPAREN!
{ Set (#t, FORMAL_DISCRETE_TYPE_DECLARATION); }
| RANGE! BOX!
{ Set (#t, FORMAL_SIGNED_INTEGER_TYPE_DECLARATION); }
| MOD! BOX!
{ Set (#t, FORMAL_MODULAR_TYPE_DECLARATION); }
| DELTA! BOX!
( DIGITS! BOX!
{ Set (#t, FORMAL_DECIMAL_FIXED_POINT_DECLARATION); }
| { Set (#t, FORMAL_ORDINARY_FIXED_POINT_DECLARATION); }
)
| DIGITS! BOX!
{ Set (#t, FORMAL_FLOATING_POINT_DECLARATION); }
| array_type_definition[#t]
| access_type_definition[#t]
| empty_discrim_opt discriminable_type_definition[#t]
)
| discrim_part IS! discriminable_type_definition[#t]
)
{ pop_def_id(); }
| w:WITH^ ( PROCEDURE! def_id[false] formal_part_opt subprogram_default_opt
{ Set(#w, FORMAL_PROCEDURE_DECLARATION); }
| FUNCTION! def_designator[false] function_tail subprogram_default_opt
{ Set(#w, FORMAL_FUNCTION_DECLARATION); }
| PACKAGE! def_id[false] IS! NEW! compound_name formal_package_actual_part_opt
{ Set(#w, FORMAL_PACKAGE_DECLARATION); }
)
{ pop_def_id(); }
| parameter_specification
)
SEMI!
;
discriminable_type_definition [RefAdaAST t]
: ( ( ABSTRACT )? NEW subtype_ind WITH ) =>
abstract_opt NEW! subtype_ind WITH! PRIVATE!
{ Set (t, FORMAL_PRIVATE_EXTENSION_DECLARATION); }
| NEW! subtype_ind
{ Set (t, FORMAL_ORDINARY_DERIVED_TYPE_DECLARATION); }
| abstract_tagged_limited_opt PRIVATE!
{ Set (t, FORMAL_PRIVATE_TYPE_DECLARATION); }
;
subprogram_default_opt : ( IS! ( BOX | name ) )?
;
formal_package_actual_part_opt
: ( LPAREN! ( BOX | defining_identifier_list ) RPAREN! )?
;
subprog_decl_or_rename_or_inst_or_body [bool lib_level]
{ RefAdaAST t; }
: p:PROCEDURE^ def_id[lib_level]
( generic_subp_inst
{ Set(#p, GENERIC_PROCEDURE_INSTANTIATION); }
| formal_part_opt
( renames { Set(#p, PROCEDURE_RENAMING_DECLARATION); }
| IS! ( separate_or_abstract[#p]
| body_part { Set(#p, PROCEDURE_BODY); }
)
| { pop_def_id();
Set(#p, PROCEDURE_DECLARATION); }
)
SEMI!
)
| f:FUNCTION^ def_designator[lib_level]
( generic_subp_inst
{ Set(#f, GENERIC_FUNCTION_INSTANTIATION); }
| function_tail
( renames { Set(#f, FUNCTION_RENAMING_DECLARATION); }
| IS! ( separate_or_abstract[#f]
| body_part { Set(#f, FUNCTION_BODY); }
)
| { pop_def_id();
Set(#f, FUNCTION_DECLARATION); }
)
SEMI!
)
;
body_part : declarative_part block_body end_id_opt!
;
declarative_part : ( pragma | declarative_item )*
{ #declarative_part =
#(#[DECLARATIVE_PART, "DECLARATIVE_PART"],
#declarative_part); }
;
// A declarative_item may appear in the declarative part of any body.
declarative_item :
( pkg:PACKAGE^ ( body_is
( separate { Set(#pkg, PACKAGE_BODY_STUB); }
| pkg_body_part end_id_opt!
{ Set(#pkg, PACKAGE_BODY); }
)
SEMI!
| def_id[false] spec_decl_part[#pkg]
)
| tsk:TASK^ ( body_is
( separate { Set(#tsk, TASK_BODY_STUB); }
| body_part { Set(#tsk, TASK_BODY); }
)
SEMI!
| task_type_or_single_decl[#tsk]
)
| pro:PROTECTED^
( body_is
( separate { Set(#pro, PROTECTED_BODY_STUB); }
| prot_op_bodies_opt end_id_opt!
{ Set(#pro, PROTECTED_BODY); }
)
| prot_type_or_single_decl[#pro]
)
SEMI!
| subprog_decl_or_rename_or_inst_or_body[false]
| decl_common
)
/* DECLARATIVE_ITEM is just a pass-thru node so we omit it.
Objections anybody?
{ #declarative_item =