summaryrefslogtreecommitdiffstats
path: root/languages/ada/ada.tree.g
blob: 6f343f978b84fed0856d48fc3b05b86dde5e1229 (plain)
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
/*
 * ANTLR Ada95 tree super grammar (base class)
 *
 * Oliver M. Kellogg  <okellogg@users.sourceforge.net>
 *
 * $Id$
 *
 */


options {
  language="Cpp";
}

class AdaTreeParserSuper extends TreeParser;
options {
  importVocab=Ada;
}

// Compilation Unit:  This is the start rule.

compilation_unit
	: context_items_opt ( library_item | subunit ) ( pragma )*
	;

pragma  : #(PRAGMA IDENTIFIER ( pragma_arg )* )
	;

pragma_arg
	: #(RIGHT_SHAFT IDENTIFIER expression)
	| expression
	;

context_items_opt
	: #(CONTEXT_CLAUSE ( pragma | with_clause | use_clause )* )
	;

with_clause
	: #(WITH_CLAUSE ( compound_name )+ )
	;

compound_name
	: IDENTIFIER
	| #(DOT compound_name IDENTIFIER)
	;

use_clause
	: #(USE_TYPE_CLAUSE ( subtype_mark )+ )
	| #(USE_CLAUSE ( compound_name )+ )
	;

subtype_mark
	: compound_name
	| #(TIC compound_name attribute_id)
	;

attribute_id : RANGE
	| DIGITS
	| DELTA
	| ACCESS
	| IDENTIFIER
	;

library_item :
	#(LIBRARY_ITEM modifiers
		( subprog_decl_or_rename_or_inst_or_body
		| #(PACKAGE_BODY def_id pkg_body_part)
		| #(GENERIC_PACKAGE_INSTANTIATION def_id generic_inst)
		| #(PACKAGE_SPECIFICATION def_id pkg_spec_part)
		| #(PACKAGE_RENAMING_DECLARATION def_id renames)
		| generic_decl
		)
	)
	;

modifiers : #(MODIFIERS
		( ABSTRACT
		| ACCESS
		| ALIASED
		| ALL
		| CONSTANT
		| IN
                | LIMITED
		| OUT
		| PRIVATE
		| PROTECTED
		| REVERSE
		| TAGGED
		)*
		// We can afford to be this loose because the parser
		// has already fended off illegal usages, i.e. we can
		// be sure that we are only dealing with permissible
		// values for each usage context here.
		// Only exception: PRIVATE as the prefix of private
		// {generic|package|subprogram} library-level decl.
		// The parser has let this in for bodies as well
		// (which is not legal), so we need a semantic check.
	     )
	;

id_opt : #(ID_OPT ( def_designator )? )
	;

end_id_opt : #(END ( def_designator )? )
	;

subprog_decl
	: #(GENERIC_PROCEDURE_INSTANTIATION def_id generic_inst)
	| #(PROCEDURE_RENAMING_DECLARATION  def_id formal_part_opt renames)
	| #(PROCEDURE_DECLARATION           def_id formal_part_opt)
	| #(PROCEDURE_BODY_STUB             def_id formal_part_opt)
	| #(ABSTRACT_PROCEDURE_DECLARATION  def_id formal_part_opt)
	| #(GENERIC_FUNCTION_INSTANTIATION  def_designator generic_inst)
	| #(FUNCTION_RENAMING_DECLARATION   def_designator function_tail renames)
	| #(FUNCTION_DECLARATION            def_designator function_tail)
	| #(FUNCTION_BODY_STUB              def_designator function_tail)
	| #(ABSTRACT_FUNCTION_DECLARATION   def_id function_tail)
	;

def_id : compound_name  // Can afford looseness because parser is tight.
	;

generic_inst : compound_name ( value_s )?
	;

value   : #(OTHERS expression)
	| #(RIGHT_SHAFT ranged_expr_s expression)
	| ranged_expr_s
	;

ranged_expr_s
	: #(PIPE ranged_expr_s ranged_expr)
	| ranged_expr
	;

ranged_expr
	: #(DOT_DOT expression simple_expression)
	| #(RANGE expression range)
	| expression
	;

range_constraint : #(RANGE_CONSTRAINT range)
	;

range : ( range_dots
	| range_attrib_ref
	)
	;

range_dots : #(DOT_DOT simple_expression simple_expression)
	;

range_attrib_ref : #(RANGE_ATTRIBUTE_REFERENCE prefix ( expression )? )
	;

prefix  : IDENTIFIER
	| #(DOT prefix ( ALL | IDENTIFIER ) )
	| #(INDEXED_COMPONENT prefix value_s )
	;

formal_part_opt : #(FORMAL_PART_OPT ( parameter_specification )* )
	;

parameter_specification
	: #(PARAMETER_SPECIFICATION defining_identifier_list modifiers
		subtype_mark init_opt)
	;

defining_identifier_list : #(DEFINING_IDENTIFIER_LIST ( IDENTIFIER )+ )
	;

renames : CHARACTER_STRING  // CHARACTER_STRING should not really be there.
	| OPERATOR_SYMBOL   // OPERATOR_SYMBOL should be used instead.
	| name
	;

name    : IDENTIFIER
	| #(DOT name
		( ALL
		| IDENTIFIER
		| CHARACTER_LITERAL
		| OPERATOR_SYMBOL
		)
	   )
	| #(INDEXED_COMPONENT name value_s)
	| #(TIC name attribute_id)
	;

definable_operator_symbol : OPERATOR_SYMBOL
	;

tqparenthesized_primary
	: #(PARENTHESIZED_PRIMARY ( NuLL | value_s extension_opt ) )
	;

extension_opt :  #(EXTENSION_OPT ( NuLL | value_s )? )
	;

def_designator
	: compound_name
	| definable_operator_symbol
	;

function_tail : formal_part_opt subtype_mark
	;

spec_decl_part
	: #(GENERIC_PACKAGE_INSTANTIATION def_id generic_inst)
	| #(PACKAGE_SPECIFICATION def_id pkg_spec_part)
	| #(PACKAGE_RENAMING_DECLARATION def_id renames)
	;

pkg_spec_part
	: basic_declarative_items_opt
	  private_declarative_items_opt
	  end_id_opt
	;

basic_declarative_items_opt
	: #(BASIC_DECLARATIVE_ITEMS_OPT ( basic_decl_item )* )
	;

private_declarative_items_opt
	: #(PRIVATE_DECLARATIVE_ITEMS_OPT ( basic_decl_item )* )
	;

basic_decl_item
	: spec_decl_part
	| task_type_or_single_decl
	| prot_type_or_single_decl
	| subprog_decl
	| decl_common
	;

task_type_or_single_decl
	: #(TASK_TYPE_DECLARATION def_id discrim_part_opt task_definition_opt)
	| #(SINGLE_TASK_DECLARATION def_id task_definition_opt)
	;

task_definition_opt
	: task_items_opt private_task_items_opt
	;

discrim_part_opt
	: #(DISCRIM_PART_OPT ( BOX | discriminant_specifications )? )
	;

discriminant_specifications
	: #(DISCRIMINANT_SPECIFICATIONS ( discriminant_specification )* )
	;

discriminant_specification
	: #(DISCRIMINANT_SPECIFICATION defining_identifier_list
		modifiers subtype_mark init_opt)
	;

init_opt : #(INIT_OPT ( expression )? )
	;  // `expression' is of course much too loose;
	   // semantic checks are required in the usage contexts.

task_items_opt
	: #(TASK_ITEMS_OPT entrydecls_repspecs_opt)
	;

entrydecls_repspecs_opt
	: ( entry_declaration | pragma | rep_spec )*
	;

entry_declaration
	: #(ENTRY_DECLARATION IDENTIFIER
		discrete_subtype_def_opt formal_part_opt)
	;

discrete_subtype_def_opt
	: #(DISCRETE_SUBTYPE_DEF_OPT ( discrete_subtype_definition )? )
	;

discrete_subtype_definition  // TBD
	: range
	| subtype_ind
	;

rep_spec : #(RECORD_REPRESENTATION_CLAUSE subtype_mark align_opt comp_loc_s)
	| #(AT_CLAUSE subtype_mark expression)
	| #(ATTRIBUTE_DEFINITION_CLAUSE subtype_mark expression)
	        // attrib_def. Semantic check must ensure that the
		// subtype_mark contains an attribute reference.
	| #(ENUMERATION_REPESENTATION_CLAUSE local_enum_name enumeration_aggregate)
	;

align_opt : #(MOD_CLAUSE_OPT ( expression )? )
	;

comp_loc_s
	: #(COMPONENT_CLAUSES_OPT
		( pragma
		| subtype_mark expression range
		)*
	   )
	;

private_task_items_opt
	: #(PRIVATE_TASK_ITEMS_OPT entrydecls_repspecs_opt )
	;

prot_type_or_single_decl
	: #(PROTECTED_TYPE_DECLARATION def_id discrim_part_opt protected_definition)
	| #(SINGLE_PROTECTED_DECLARATION def_id protected_definition)
	;

prot_private_opt : #(PROT_PRIVATE_OPT ( prot_member_decl_s )? )
        ;

protected_definition
	: prot_op_decl_s prot_private_opt end_id_opt
	;

prot_op_decl_s
	: #(PROT_OP_DECLARATIONS ( prot_op_decl )* )
	;

prot_op_decl
	: entry_declaration
	| #(PROCEDURE_DECLARATION def_id formal_part_opt)
	| #(FUNCTION_DECLARATION def_designator function_tail)
	| rep_spec
	| pragma
	;

prot_member_decl_s
	: #(PROT_MEMBER_DECLARATIONS ( prot_op_decl | comp_decl )* )
	;

comp_decl
	: #(COMPONENT_DECLARATION defining_identifier_list component_subtype_def init_opt)
	;

// decl_common is shared between declarative_item and basic_decl_item.
// decl_common only contains specifications.
decl_common
	: // type_def:
	  #(ENUMERATION_TYPE_DECLARATION IDENTIFIER enum_id_s)
	| #(SIGNED_INTEGER_TYPE_DECLARATION IDENTIFIER range)
	| #(MODULAR_TYPE_DECLARATION IDENTIFIER expression)
	| #(FLOATING_POINT_DECLARATION IDENTIFIER expression range_constraint_opt)
	| #(ORDINARY_FIXED_POINT_DECLARATION IDENTIFIER expression range)
	| #(DECIMAL_FIXED_POINT_DECLARATION IDENTIFIER expression expression range_constraint_opt)
	| array_type_declaration
	| access_type_declaration
	//
	| #(INCOMPLETE_TYPE_DECLARATION IDENTIFIER discrim_part_opt)
	// derived_or_private_or_record
	| #(PRIVATE_EXTENSION_DECLARATION id_and_discrim modifiers subtype_ind)
	| #(DERIVED_RECORD_EXTENSION id_and_discrim modifiers subtype_ind record_definition)
	| #(ORDINARY_DERIVED_TYPE_DECLARATION id_and_discrim subtype_ind)
	| #(PRIVATE_TYPE_DECLARATION id_and_discrim modifiers)
	| #(RECORD_TYPE_DECLARATION id_and_discrim modifiers record_definition)
	//
	| #(SUBTYPE_DECLARATION IDENTIFIER subtype_ind)
	| generic_decl
	| use_clause
	| rep_spec  // enumeration_representation_clause only
	| #(EXCEPTION_RENAMING_DECLARATION def_id compound_name)
	| #(OBJECT_RENAMING_DECLARATION def_id subtype_mark name)
	| #(EXCEPTION_DECLARATION defining_identifier_list)
	| #(NUMBER_DECLARATION defining_identifier_list expression)
	| #(ARRAY_OBJECT_DECLARATION defining_identifier_list modifiers
		array_type_definition init_opt)
	| #(OBJECT_DECLARATION defining_identifier_list modifiers
		subtype_ind init_opt)
	;

id_and_discrim
	: IDENTIFIER discrim_part_opt
	;

enum_id_s : ( enumeration_literal_specification )+
	;

enumeration_literal_specification : IDENTIFIER | CHARACTER_LITERAL
	;

array_type_definition
	: index_or_discrete_range_s component_subtype_def
	;

range_constraint_opt : ( range_constraint )?
	;

array_type_declaration
	: #(ARRAY_TYPE_DECLARATION IDENTIFIER array_type_definition)
	;

index_or_discrete_range_s
	: index_or_discrete_range
	| #(COMMA index_or_discrete_range_s index_or_discrete_range)
	;

index_or_discrete_range
	: #(DOT_DOT simple_expression simple_expression)
	| #(RANGE simple_expression ( BOX | range) )
	| simple_expression
	;

component_subtype_def : modifiers subtype_ind  // mod=aliased_opt
	;

subtype_ind
	: #(SUBTYPE_INDICATION subtype_mark constraint_opt)
	;

constraint_opt
	:	( range_constraint
		| digits_constraint
		| delta_constraint
		| index_constraint
		| discriminant_constraint
		)?
	;

digits_constraint
	: #(DIGITS_CONSTRAINT expression range_constraint_opt)
	;

delta_constraint
	: #(DELTA_CONSTRAINT expression range_constraint_opt)
	;

index_constraint
	: #(INDEX_CONSTRAINT ( discrete_range )+ )
	;

discrete_range
	: range
	| subtype_ind
	;

discriminant_constraint
	: #(DISCRIMINANT_CONSTRAINT ( discriminant_association )+ )
	;

discriminant_association
	: #(DISCRIMINANT_ASSOCIATION selector_names_opt expression)
	;

selector_names_opt
	: #(SELECTOR_NAMES_OPT ( selector_name )* )
	;

selector_name : IDENTIFIER  // TBD: sem pred
	;

access_type_declaration
	: #(ACCESS_TO_PROCEDURE_DECLARATION IDENTIFIER modifiers formal_part_opt)
	| #(ACCESS_TO_FUNCTION_DECLARATION IDENTIFIER modifiers function_tail)
	| #(ACCESS_TO_OBJECT_DECLARATION IDENTIFIER modifiers subtype_ind)
	;

record_definition : ( component_list )?
	;

component_list
	: component_items ( variant_part )?
	;

component_items
	: #(COMPONENT_ITEMS ( pragma | comp_decl )* )
	;

variant_part
	: #(VARIANT_PART discriminant_direct_name variant_s)
	;

discriminant_direct_name : IDENTIFIER  // TBD: symtab lookup.
	;

variant_s : #(VARIANTS ( variant )+ )
	;

variant : #(VARIANT choice_s ( component_list )? )
	;

choice_s : #(PIPE choice_s choice )
	| choice
	;

choice  : OTHERS
	| discrete_with_range
	| expression
	;

discrete_with_range : mark_with_constraint | range
	;

mark_with_constraint
	: #(MARK_WITH_CONSTRAINT subtype_mark range_constraint)
	;

local_enum_name : IDENTIFIER  // to be refined: do a symbol table lookup
	;

enumeration_aggregate : ( value )*
	;

generic_decl
	: #(GENERIC_PACKAGE_RENAMING generic_formal_part_opt def_id renames)
	| #(GENERIC_PACKAGE_DECLARATION generic_formal_part_opt def_id
		pkg_spec_part)
	| #(GENERIC_PROCEDURE_RENAMING generic_formal_part_opt def_id
		formal_part_opt renames)
	| #(GENERIC_PROCEDURE_DECLARATION generic_formal_part_opt def_id
		formal_part_opt)
	| #(GENERIC_FUNCTION_RENAMING generic_formal_part_opt def_designator
		function_tail renames)
	| #(GENERIC_FUNCTION_DECLARATION generic_formal_part_opt def_id
		function_tail)
	;

generic_formal_part_opt
	: #(GENERIC_FORMAL_PART
		( pragma | use_clause | generic_formal_parameter )*
	   )
	;

generic_formal_parameter
	: // FORMAL_TYPE_DECLARATIONs:
	  #(FORMAL_DISCRETE_TYPE_DECLARATION def_id)
	| #(FORMAL_SIGNED_INTEGER_TYPE_DECLARATION def_id)
	| #(FORMAL_MODULAR_TYPE_DECLARATION def_id)
	| #(FORMAL_DECIMAL_FIXED_POINT_DECLARATION def_id)
	| #(FORMAL_ORDINARY_FIXED_POINT_DECLARATION def_id)
	| #(FORMAL_FLOATING_POINT_DECLARATION def_id)
	| formal_array_type_declaration
	| formal_access_type_declaration
	| #(FORMAL_PRIVATE_TYPE_DECLARATION id_part modifiers)
	| #(FORMAL_ORDINARY_DERIVED_TYPE_DECLARATION id_part subtype_ind)
	| #(FORMAL_PRIVATE_EXTENSION_DECLARATION id_part modifiers subtype_ind)
	| #(FORMAL_PROCEDURE_DECLARATION def_id formal_part_opt
		subprogram_default_opt)
	| #(FORMAL_FUNCTION_DECLARATION def_designator function_tail
		subprogram_default_opt)
	| #(FORMAL_PACKAGE_DECLARATION def_id compound_name formal_package_actual_part_opt)
	| parameter_specification
	;


id_part : def_id discrim_part_opt
	;

formal_array_type_declaration : array_type_declaration
	;

formal_access_type_declaration : access_type_declaration
	;

subprogram_default_opt : ( BOX | name )?
                      // #(SUBPROGRAM_DEFAULT_OPT ( BOX | name )? )
	;

formal_package_actual_part_opt
	: ( BOX | defining_identifier_list )?
	;

subprog_decl_or_rename_or_inst_or_body
	: subprog_decl
	| procedure_body
	| function_body
	;

procedure_body
	: #(PROCEDURE_BODY def_id formal_part_opt body_part)
	;

function_body
	: #(FUNCTION_BODY def_designator function_tail body_part)
	;

body_part : declarative_part block_body end_id_opt
	;

declarative_part
	: #(DECLARATIVE_PART ( pragma | declarative_item )* )
	;

// A declarative_item may appear in the declarative part of any body.
declarative_item
	: #(PACKAGE_BODY_STUB def_id)
	| #(PACKAGE_BODY def_id pkg_body_part)
	| spec_decl_part
	| #(TASK_BODY_STUB def_id)
	| #(TASK_BODY def_id body_part end_id_opt)
	| task_type_or_single_decl
	| #(PROTECTED_BODY_STUB def_id)
	| #(PROTECTED_BODY def_id prot_op_bodies_opt end_id_opt)
	| prot_type_or_single_decl
	| subprog_decl_or_rename_or_inst_or_body
	| decl_common
	;

pkg_body_part : declarative_part block_body_opt end_id_opt
	;

block_body_opt
	: #(BLOCK_BODY_OPT ( handled_stmt_s )? )
	;

prot_op_bodies_opt
	: #(PROT_OP_BODIES_OPT
		( pragma | entry_body | subprog_decl_or_body )*
	   )
	;

subprog_decl_or_body
	: procedure_body
	| #(PROCEDURE_DECLARATION def_id formal_part_opt)
	| function_body
	| #(FUNCTION_DECLARATION def_designator function_tail)
	;

block_body : #(BLOCK_BODY handled_stmt_s)
	;

handled_stmt_s
	: #(HANDLED_SEQUENCE_OF_STATEMENTS statements except_handler_part_opt)
	;

handled_stmts_opt
	: #(HANDLED_STMTS_OPT ( statements except_handler_part_opt )? )
	;

statements
	: #(SEQUENCE_OF_STATEMENTS ( pragma | statement )+ )
	;

statement : #(STATEMENT def_label_opt
		( null_stmt
		| exit_stmt
		| return_stmt
		| goto_stmt
		| delay_stmt
		| abort_stmt
		| raise_stmt
		| requeue_stmt
		| accept_stmt
		| select_stmt
		| if_stmt
		| case_stmt
		| loop_stmt id_opt
		| block end_id_opt
		| call_or_assignment
		// | code_stmt  // not yet implemented in parser
		)
	     )
	;

def_label_opt : #(LABEL_OPT ( IDENTIFIER )? )
	;

null_stmt : NULL_STATEMENT
	;

if_stmt : #(IF_STATEMENT cond_clause elsifs_opt else_opt)
	;

cond_clause : #(COND_CLAUSE condition statements)
	;

condition : expression
	;

elsifs_opt : #(ELSIFS_OPT ( cond_clause )* )
	;

else_opt : #(ELSE_OPT ( statements )? )
	;

case_stmt : #(CASE_STATEMENT expression alternative_s)
	;

alternative_s : ( case_statement_alternative )+
	;

case_statement_alternative
	: #(CASE_STATEMENT_ALTERNATIVE choice_s statements)
	;

loop_stmt
	: #(LOOP_STATEMENT iteration_scheme_opt statements)
        ;

iteration_scheme_opt
	: #(ITERATION_SCHEME_OPT
		( #(WHILE condition)
		| #(FOR IDENTIFIER modifiers discrete_subtype_definition)
		)?                // mod=reverse_opt
	   )
	;

block   : #(BLOCK_STATEMENT declare_opt block_body)
	;

declare_opt
	: #(DECLARE_OPT ( declarative_part )? )
	;

exit_stmt
	: #(EXIT_STATEMENT ( label_name )? ( WHEN condition )? ) // TBD !!!
	;

label_name : IDENTIFIER
	;

return_stmt
	: #(RETURN_STATEMENT ( expression )? )
	;

goto_stmt : #(GOTO_STATEMENT label_name)
	;

call_or_assignment
	: #(ASSIGNMENT_STATEMENT name expression)
	| #(CALL_STATEMENT name)
	;

entry_body
	: #(ENTRY_BODY def_id entry_body_formal_part entry_barrier body_part)
	;

entry_body_formal_part : entry_index_spec_opt formal_part_opt
	;

entry_index_spec_opt
	: #(ENTRY_INDEX_SPECIFICATION ( def_id discrete_subtype_definition )? )
	;

entry_barrier : condition
	;

entry_call_stmt
	: #(ENTRY_CALL_STATEMENT name)
	;

accept_stmt
	: #(ACCEPT_STATEMENT def_id entry_index_opt formal_part_opt
		handled_stmts_opt end_id_opt )
	;

entry_index_opt
	: #(ENTRY_INDEX_OPT ( expression )? )
	;

delay_stmt
	: #(DELAY_STATEMENT modifiers expression)  //mod=until_opt
	;

// SELECT_STATEMENT itself is not modeled since it is trivially
// reconstructed:
//   select_statement ::= selective_accept | timed_entry_call
//             | conditional_entry_call | asynchronous_select
//
select_stmt
	: #(ASYNCHRONOUS_SELECT triggering_alternative abortable_part)
	| #(SELECTIVE_ACCEPT selective_accept)
	| #(TIMED_ENTRY_CALL entry_call_alternative delay_alternative)
	| #(CONDITIONAL_ENTRY_CALL entry_call_alternative statements)
	;

triggering_alternative
	: #(TRIGGERING_ALTERNATIVE
		( delay_stmt | entry_call_stmt ) stmts_opt
	   )
	;

abortable_part
	: #(ABORTABLE_PART stmts_opt)
	;

entry_call_alternative
	: #(ENTRY_CALL_ALTERNATIVE entry_call_stmt stmts_opt)
	;

selective_accept : guard_opt select_alternative or_select_opt else_opt
	;

guard_opt : #(GUARD_OPT ( condition ( pragma )* )? )
	;

select_alternative
	: accept_alternative
        | delay_alternative
	| TERMINATE_ALTERNATIVE
	;

accept_alternative
	: #(ACCEPT_ALTERNATIVE accept_stmt stmts_opt)
	;

delay_alternative
	: #(DELAY_ALTERNATIVE delay_stmt stmts_opt)
	;

stmts_opt : ( pragma | statement )*
	;

or_select_opt
	: #(OR_SELECT_OPT ( guard_opt select_alternative )* )
	;

abort_stmt
	: #(ABORT_STATEMENT ( name )+ )
	;

except_handler_part_opt
	: #(EXCEPT_HANDLER_PART_OPT ( exception_handler )* )
	;

exception_handler
	: #(EXCEPTION_HANDLER identifier_colon_opt except_choice_s
		statements
	   )
	;

identifier_colon_opt
	: #(IDENTIFIER_COLON_OPT ( IDENTIFIER )? )
	;

except_choice_s
	: #(PIPE except_choice_s exception_choice)
	| exception_choice
	;

exception_choice : compound_name
	| OTHERS
	;

raise_stmt
	: #(RAISE_STATEMENT ( compound_name )? )
	;

requeue_stmt
	: #(REQUEUE_STATEMENT name ( ABORT )? )
	;

operator_call : #(OPERATOR_SYMBOL value_s)
	;

value_s : #(VALUES ( value )+ )
	;

expression
	: #(AND expression relation)
	| #(AND_THEN expression relation)
	| #(OR expression relation)
	| #(OR_ELSE expression relation)
	| #(XOR expression relation)
	| relation
	;

relation
	: #(IN simple_expression range_or_mark)
	| #(NOT_IN simple_expression range_or_mark)
	| #(EQ simple_expression simple_expression)
	| #(NE simple_expression simple_expression)
	| #(LT_ simple_expression simple_expression)
	| #(LE simple_expression simple_expression)
	| #(GT simple_expression simple_expression)
	| #(GE simple_expression simple_expression)
	| simple_expression
	;

range_or_mark
	: range
	| subtype_mark
	;

simple_expression
	: #(PLUS simple_expression signed_term)
	| #(MINUS simple_expression signed_term)
	| #(CONCAT simple_expression signed_term)
	| signed_term
	;

signed_term
	: #(UNARY_PLUS term)
	| #(UNARY_MINUS term)
	| term
	;

term    : #(STAR term factor)
	| #(DIV term factor)
	| #(MOD term factor)
	| #(REM term factor)
	| factor
	;

factor  : #(NOT primary)
	| #(ABS primary)
	| #(EXPON primary primary)
	| primary
	;

primary :
	( name_or_qualified
	| tqparenthesized_primary
	| allocator
	| NuLL
	| NUMERIC_LIT
	| CHARACTER_LITERAL
	| CHAR_STRING
	| operator_call
	)
	;

// Temporary, to be turned into just `qualified'.
// We get away with it because `qualified' is always mentioned
// together with `name'.
// Only exception: `code_stmt', which is not yet implemented.
name_or_qualified
	: IDENTIFIER
	| #(DOT name_or_qualified
		( ALL
		| IDENTIFIER
		| CHARACTER_LITERAL
		| OPERATOR_SYMBOL
		)
	   )
	| #(INDEXED_COMPONENT name_or_qualified value_s)
	| #(TIC name_or_qualified
		( tqparenthesized_primary
		| attribute_id
		)
	   )
	;

allocator : #(ALLOCATOR name_or_qualified)
	;

subunit : #(SUBUNIT compound_name
		( subprogram_body
		| package_body
		| task_body
		| protected_body
		)
	   )
	;

subprogram_body
	: procedure_body
	| function_body
	;

package_body : #(PACKAGE_BODY def_id pkg_body_part)
	;

task_body : #(TASK_BODY def_id body_part)
	;
 
protected_body : #(PROTECTED_BODY def_id prot_op_bodies_opt)
	;

// TBD
// code_stmt : #(CODE_STATEMENT qualified)
//  	;