forked from knizhnik/ptoc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.y
More file actions
1422 lines (1160 loc) · 52.8 KB
/
Copy pathparser.y
File metadata and controls
1422 lines (1160 loc) · 52.8 KB
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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nmtbl.h"
#include "token.h"
#include "trnod.h"
#include "util.h"
#include "parser.h"
static int zzcnv_table[] = {
#define DEF_TOKEN(mnem, cat, cls, yacc) yacc,
#include "token.dpp"
};
#define YYINITDEPTH 100000
void zzerror(const char* text)
{
error(curr_token, "syntax error: %s", text);
}
%}
%define parse.error verbose // Enables detailed error messages (required for counterexamples)
//%define parse.counterexample timeout 15 // Set time limit to 15 seconds
//%define parse.lr.minimize timeout 30
%verbose
%no-lines
%define parse.trace
%define api.prefix {zz}
%union {
token *tok;
two_tokens *tok2;
token_list *toks;
node *n_node;
tpd_node *n_tpd;
block_node *n_block;
stmt_node *n_stmt;
decl_node *n_decl;
expr_node *n_expr;
expr_group_node *n_grp;
//write_param_node *n_wrtp;
//write_list_node *n_wrls;
case_node *n_case;
set_item_node *n_item;
const_def_node *n_cdef;
type_def_node *n_tdef;
var_decl_node *n_vdcl;
field_init_node *n_field;
param_list_node *n_plist;
idx_node *n_idx;
field_list_node *n_fldls;
variant_part_node *n_varp;
selector_node *n_sel;
variant_node *n_vari;
compound_node *n_comp;
asm_line_node *n_asm;
asm_block_node *n_basm;
import_list_node *n_imp;
attrib_node *n_attr;
}
%token <tok> ABSTRACT
ARRAY
ASM
BEGIN
CASE
CDECL
CELSE
CENDIF
CIF
CIFDEF
CIFNDEF
CLASS
CONST
DEFAULT
DEPRECATED
DO
DOTS
DYNAMIC
ELSE
END
EXCEPT
EXTERNAL
FIL
FINAL
FINALIZATION
FINALLY
// FAR
FOR
FORWARD
FUNCTION
GOTO
IDENT
ICONST
IF
IMPLEMENTATION
INDEX
INHERITED
INITIALIZATION
INLINE
INTERFACE
LABEL
// LOOPHOLE
OBJECT
OF
ON
OPERATOR
// ORIGIN
// OTHERWISE
OVERLOAD
OVERRIDE
OUT
PACKED
PASCAL
PROCEDURE
PROGRAM
PROPERTY
PRIVATE
PROTECTED
PUBLIC
PUBLISHED
RAISE
RCONST
READ
RECORD
REGISTER
REINTRODUCE
REPEAT
RESOURCESTRING
// RETURN
SAFECALL
SET
SCONST
STATIC
STDCALL
STORED
STRICT
STRING
THEN
TO
TRY
TYPE
UNTIL
UNIT
UNIT_END
UNSAFE
VAR
VARARGS
VIRTUAL
WHILE
WINAPI
WITH
WRITE
'.'
','
':'
';'
'('
')'
'['
']'
'^'
'@'
// SCOPE
%right <tok> LET LETADD LETSUB LETDIV LETMUL LETAND LETOR LETSHL LETSHR
%left <tok> EQ NE LT LE GT GE IN IS
%left <tok> PLUS MINUS OR XOR BINOR
%left <tok> MOD DIV DIVR MUL AND SHR SHL BINAND AS
%right <tok> UPLUS UMINUS NOT ADDRESS BINNOT
%left <tok> EQUAL NOTEQUAL LESSTH GREATERTH
%left <tok> ADD SUBSTRACT
%left <tok> MULTIPLY DIVIDE INTDIVIDE MODULUS IMPLICIT EXPLICIT
%type <toks> ident_list
%type <toks> label_list
%type <toks> qualifiers
%type <tok> fun_qualifier
meth_qualifier
qualifier
condition_start
%type <tok> packed
%type <tok> progend
//%type <tok> otherwise
%type <tok> class_or_object
%type <tok> class_access_spec_tok
%type <tok> record_access_spec_tok
%type <tok> const
%type <tok> ident_ext
%type <tok> optional_semicolon
%type <tok2> deprecatd
%type <n_imp> prog_param_list
%type <n_node> unit
%type <n_node> program
%type <n_node> module
%type <n_node> translation
%type <n_node> input_file
%type <n_block> block
%type <n_stmt> statement
%type <n_stmt> sequence
%type <n_stmt> try_finally
%type <n_stmt> try_except
%type <n_stmt> except
%type <n_stmt> except_many
%type <n_stmt> attr_content
//%type <n_grp> actual_params
%type <n_comp> compoundst
//%type <n_wrls> write_params
//%type <n_wrtp> write_list
//%type <n_wrtp> write_param
%type <n_expr> const_expr
%type <n_expr> const_simple_expr
%type <n_expr> expr
%type <n_expr> condition_expr
%type <n_expr> condition_const_expr
%type <n_expr> simple_expr
%type <n_expr> primary
%type <n_expr> constant
%type <n_expr> record_constant
%type <n_expr> expr_list
%type <n_expr> const_expr_list
%type <n_expr> const_act_param_list
%type <n_expr> const_act_param
%type <n_expr> act_param_list
%type <n_expr> act_param
%type <n_expr> case_elem
%type <n_expr> case_elem_list
%type <n_expr> inherited
//%type <n_grp> expr_group
%type <n_field> field_init_list
%type <n_field> field_init_item
%type <n_case> case_list
%type <n_case> case_item
%type <n_case> case_items
%type <n_item> set_elem_list
%type <n_item> set_elem
%type <n_decl> decl_part
%type <n_decl> unit_decl
%type <n_decl> unit_spec
%type <n_decl> unit_decl_list
%type <n_decl> unit_def_list
%type <n_decl> decl_part_list
%type <n_decl> label_decl_part
%type <n_decl> const_def_part
//%type <n_decl> init_const_def_part
%type <n_decl> var_const_decl_part_list
//%type <n_decl> guid
%type <n_decl> interface_methods
%type <n_decl> interface_method_decl
%type <n_decl> interface_method_decl_c
%type <n_decl> object_methods
%type <n_decl> condition_obj_method
%type <n_decl> condition_interface_method
%type <n_decl> condition_rec_method
//%type <n_decl> condition_obj_body
%type <n_decl> object_body
%type <n_decl> object_components
%type <n_decl> object_component
%type <n_decl> record_components
%type <n_decl> record_component
%type <n_decl> record_methods
%type <n_decl> record_body
%type <n_decl> record_field_list
%type <n_decl> record_access_spec_decl
%type <n_decl> class_access_spec_decl
%type <n_decl> init_finit
%type <n_decl> prop_type_def
%type <n_decl> prop_array
%type <n_decl> prop_index
%type <n_decl> prop_read
%type <n_decl> prop_write
%type <n_decl> prop_stored
%type <n_decl> prop_default
%type <n_decl> prop_default_directive
%type <n_cdef> const_def_list
//%type <n_cdef> init_const_def_list
%type <n_cdef> const_def
%type <n_decl> type_def_part
%type <n_tdef> type_def_list
%type <n_tdef> type_def
%type <n_decl> var_decl_part
%type <n_decl> var_const_decl_part
%type <n_decl> field_decl_part
%type <n_vdcl> var_decl_list
%type <n_vdcl> var_decl_c
%type <n_vdcl> var_decl_semi
%type <n_vdcl> var_decl
%type <n_vdcl> field_decl_list
%type <n_vdcl> param_decl
%type <n_vdcl> prop_param_decl
%type <n_vdcl> prop_param_list
//%type <n_decl> proc_decl
%type <n_decl> proc_fwd_decl
%type <n_decl> operator_fwd_decl
%type <n_decl> record_method_decl
%type <n_decl> method_decl_list
%type <n_decl> method_decl
%type <n_decl> proc_spec
%type <n_decl> proc_def
%type <n_decl> property_decl
//%type <n_decl> property_decl_list
%type <n_plist> formal_params
%type <n_decl> formal_param_list
%type <n_decl> formal_param
%type <n_tpd> type
%type <n_tpd> param_type
%type <n_tpd> simple_type
%type <n_tpd> simple_templ_type
%type <n_tpd> fptr_type
%type <n_tpd> const_type
%type <n_tpd> const_array_type
%type <n_tpd> const_set_type
%type <n_tpd> array_type
//%type <n_tpd> array_of_const_type
%type <n_tpd> string_type
%type <n_tpd> conformant_array_type
%type <n_tpd> enum_type
%type <n_tpd> range_type
%type <n_tpd> pointer_type
%type <n_tpd> set_type
%type <n_tpd> record_type
%type <n_tpd> class_type
%type <n_tpd> interface_type
%type <n_tpd> file_type
%type <n_idx> indices
//%type <n_idx> conformant_indices
//%type <n_idx> conformant_index
%type <n_idx> index_spec
%type <n_fldls> field_list
%type <n_vdcl> fixed_part
%type <n_varp> variant_part
%type <n_sel> selector
%type <n_vari> variant_list
%type <n_vari> variant
%type <tok> asm_kwd
//%type <toks> asm_ident_list
%type <n_asm> asm_line
%type <n_asm> asm_text
%type <n_basm> assemblerst
%type <n_attr> attr_decl
%precedence THEN
%precedence ELSE
%precedence PROCEDURE
%precedence FUNCTION
%precedence OF
%precedence OBJECT
%start translation
%printer { auto obj = dynamic_cast<base_obj_tpd_node*>($$); assert(obj); fprintf(yyo, "%s", obj->t_startof->in_text); } class_type
%printer { if($$) $$->print_debug(); } <tok>
%printer { $$->print_debug(); } <toks>
%printer { fprintf(yyo, "%s", $$? ((literal_node*)$$)->value_tkn->in_text: "NULL"); } constant
%printer { $$->print_debug(); } <n_expr>
%printer { if($$) $$->print_debug(); } <n_stmt>
%printer { $$->print_debug(); } <n_tpd>
//%glr-parser
%%
/*
Grammar section
*/
/*
//=============================================================================
// Program level grammar:
// program ::= PROGRAM ident [ '(' parameter_list ')' ] block '.'
// parameter_list ::= ident { ',' ident }
// block ::= decl_part_list compoundst
// decl_part_list ::= { decl_part }
// decl_part ::= label_decl_part | const_def_part | type_def_part
// | var_decl_part | proc_decl | proc_fwd_decl
//=============================================================================
*/
translation:
{
// if (turbo_pascal) {
// zzcnv_table[TKN_STRING] = STRING;
// zzcnv_table[TKN_STR] = WRITE;
// zzcnv_table[TKN_SHL] = SHL;
// zzcnv_table[TKN_SHR] = SHR;
// zzcnv_table[TKN_XOR] = XOR;
// zzcnv_table[TKN_UNIT] = UNIT;
// zzcnv_table[TKN_FAR] = FAR;
// zzcnv_table[TKN_IMPLEMENTATION] = IMPLEMENTATION;
// zzcnv_table[TKN_INTERFACE] = INTERFACE;
// zzcnv_table[TKN_OBJECT] = OBJECT;
// zzcnv_table[TKN_CLASS] = CLASS;
// zzcnv_table[TKN_CONSTRUCTOR] = PROCEDURE;
// zzcnv_table[TKN_DESTRUCTOR] = PROCEDURE;
// } else {
// zzcnv_table[TKN_ORIGIN] = ORIGIN;
// }
}
input_file {
$2->attrib(ctx_program);
$2->translate(ctx_program);
}
input_file: program | module | unit
program: block '.' { $$ = new program_node(NULL, NULL, NULL, NULL, $1, $2); }
| PROGRAM IDENT prog_param_list ';' block '.'
{ $$ = new program_node($1, $2, $3, $4, $5, $6); }
progend: { $$ = NULL; } | '.'
module: decl_part_list { $$ = new module_node(NULL, NULL, NULL, NULL, $1, NULL); }
| PROGRAM IDENT prog_param_list ';' decl_part_list progend
{ $$ = new module_node($1, $2, $3, $4, $5, $6); }
/* Turbo Pascal specific */
unit: UNIT IDENT ';' INTERFACE unit_decl_list IMPLEMENTATION unit_def_list END '.'
{ $$ = new unit_node($1, $2, $3, $4, $5, $6, $7, NULL, NULL, $8, $9); }
| UNIT IDENT ';' INTERFACE unit_decl_list IMPLEMENTATION unit_def_list compoundst '.'
{ $$ = new unit_node($1, $2, $3, $4, $5, $6, $7, $8, NULL, NULL, $9); }
| INTERFACE unit_decl_list IMPLEMENTATION unit_def_list END '.'
{ $$ = new unit_node(NULL, NULL, NULL, $1, $2, $3, $4, NULL, NULL, $5, $6); }
| INTERFACE unit_decl_list IMPLEMENTATION unit_def_list compoundst '.'
{ $$ = new unit_node(NULL, NULL, NULL, $1, $2, $3, $4, $5, NULL, NULL, $6); }
| UNIT IDENT ';' INTERFACE unit_decl_list IMPLEMENTATION unit_def_list init_finit END '.'
{ $$ = new unit_node($1, $2, $3, $4, $5, $6, $7, NULL, $8, $9, $10); }
init_finit:
INITIALIZATION var_const_decl_part_list sequence
{ $$ = new init_finit_node($1, $2, $3, NULL, NULL, NULL, NULL, NULL); }
| INITIALIZATION sequence
{ $$ = new init_finit_node($1, NULL, $2, NULL, NULL, NULL, NULL, NULL); }
| INITIALIZATION var_const_decl_part_list sequence FINALIZATION var_const_decl_part_list sequence
{ $$ = new init_finit_node($1, $2, $3, NULL, $4, $5, $6, NULL); }
| INITIALIZATION sequence FINALIZATION var_const_decl_part_list sequence
{ $$ = new init_finit_node($1, NULL, $2, NULL, $3, $4, $5, NULL); }
| INITIALIZATION var_const_decl_part_list sequence FINALIZATION sequence
{ $$ = new init_finit_node($1, $2, $3, NULL, $4, NULL, $5, NULL); }
| INITIALIZATION sequence FINALIZATION sequence
{ $$ = new init_finit_node($1, NULL, $2, NULL, $3, NULL, $4, NULL); }
var_const_decl_part_list: var_const_decl_part //{ $$ = NULL; }
| var_const_decl_part var_const_decl_part_list { $1->next = $2; $$ = $1; }
var_const_decl_part: const_def_part | var_decl_part
unit_def_list: decl_part_list
prog_param_list: { $$ = NULL; }
| '(' ident_list ')' { $$ = new import_list_node($1, $2, $3); }
// read, write, index are reserved keywords in Delphi. However they can be used as variable and function names in source code.
// ident_ext non-terminal is an implementation for that.
ident_ext: IDENT | INDEX | READ | WRITE
ident_list: ident_ext ',' ident_list { $$ = new token_list($1, $3); }
| ident_ext { $$ = new token_list($1); }
block: decl_part_list compoundst
{ $$ = new block_node($1, $2); }
| assemblerst
{ $$ = new block_node($1); }
decl_part_list: { $$ = NULL; }
| decl_part decl_part_list { $1->next = $2; $$ = $1; }
decl_part: label_decl_part | const_def_part | type_def_part | var_decl_part
| proc_def | proc_fwd_decl | unit_spec
unit_spec: UNIT IDENT ';' INTERFACE unit_decl_list UNIT_END
{ $$ = new unit_spec_node($1, $2, $3, $4, $5); }
| INTERFACE unit_decl_list UNIT_END
{ $$ = new unit_spec_node(NULL, NULL, NULL, $1, $2); }
unit_decl_list: { $$ = NULL; }
| unit_decl unit_decl_list { $1->next = $2; $$ = $1; }
unit_decl: label_decl_part | const_def_part | type_def_part | var_decl_part
| proc_spec | proc_fwd_decl | unit_spec
/*
//=============================================================================
// Statement level grammar
// statement ::= compoundst | assignmentst | gotost | switchst | ifst
// | forst | whilest | repeatst | procst | returnst
// | withst | labelst | emtyst | writest | readst
// sequence ::= statement { ';' statement }
// compoundst ::= BEGIN sequence END
// assignmentst ::= expr ':=' expr
// gotost ::= GOTO iconst
// labelst ::= iconst ':'
// switchst ::= CASE expr OF case_list END
// case_list ::= case_list_elem { ';' case-list-elem } [ ';' ]
// case_list_elem ::= expr_list ':' statement | OTHERWISE statement
// ifst ::= IF expr THEN statement [ELSE statement]
// forst ::= FOR identifier := expr (TO | DOWNTO) expr DO statement
// repeatst ::= REPEAT sequence UNTIL expr
// whilest ::= WHILE expr DO statement
// withst ::= WITH expr_list DO statement
// procst ::= ident [ expr_group ]
// writest ::= (WRITE | WRITELN) [ write_params ]
// readst ::= (READ | READLN) [ expr_group ]
// returnst ::= RETURN
// emptyst ::=
//=============================================================================
*/
inherited:
INHERITED
{ $$ = new inherited_node($1, NULL, NULL, NULL, NULL); }
| INHERITED IDENT
{ $$ = new inherited_node($1, $2, NULL, NULL, NULL); }
| INHERITED IDENT '(' act_param_list ')'
{ $$ = new inherited_node($1, $2, $3, $4, $5); }
// probably we need separate class for try* nterminals
try_finally: TRY sequence FINALLY sequence END
{ $$ = new try_finally_node($1, $2, $3, $4, $5); }
try_except: TRY sequence EXCEPT except_many END
{ $$ = new try_except_node($1, $2, $3, $4, NULL, NULL, $5); }
| TRY sequence EXCEPT except_many ELSE sequence END
{ $$ = new try_except_node($1, $2, $3, $4, $5, $6, $7); }
except_many: except
| except ';' except_many { $1->next = $3; $$ = $1; }
except: { $$ = NULL; }
| ON IDENT ':' simple_type DO statement //compoundst
{ $$ = new on_except_node($1, $2, $3, $4, $5, $6); }
statement: { $$ = new empty_node(curr_token->prev_relevant()); }
| primary LET expr { $$ = new assign_node($1, $2, $3); }
| primary LETADD expr { $$ = new assign_node($1, $2, $3); }
| primary LETSUB expr { $$ = new assign_node($1, $2, $3); }
| primary LETDIV expr { $$ = new assign_node($1, $2, $3); }
| primary LETMUL expr { $$ = new assign_node($1, $2, $3); }
| primary LETAND expr { $$ = new assign_node($1, $2, $3); }
| primary LETOR expr { $$ = new assign_node($1, $2, $3); }
| primary LETSHL expr { $$ = new assign_node($1, $2, $3); }
| primary LETSHR expr { $$ = new assign_node($1, $2, $3); }
| GOTO ICONST { $$ = new goto_node($1, $2); }
| GOTO IDENT { $$ = new goto_node($1, $2); }
| CASE expr OF case_list END { $$ = new switch_node($1, $2, $3, $4, $5); }
| IF expr THEN statement { $$ = new if_node($1, $2, $3, $4); }
| IF expr THEN statement ELSE statement
{ $$ = new if_node($1, $2, $3, $4, $5, $6); }
| FOR ident_ext LET expr TO expr DO statement
{ $$ = new for_node($1, $2, $3, $4, $5, $6, $7, $8); }
| WHILE expr DO statement { $$ = new while_node($1, $2, $3, $4); }
| REPEAT sequence UNTIL expr { $$ = new repeat_node($1, $2, $3, $4); }
// | WRITE write_params { $$ = new write_node($1, $2); }
// | READ actual_params { $$ = new read_node($1, $2); }
| RAISE expr { $$ = new raise_node($1, $2); }
| primary { $$ = new pcall_node($1); }
// | RETURN { $$ = new return_node($1); }
| WITH expr_list DO statement { $$ = new with_node($1, $2, $3, $4); }
| ICONST ':' statement { $$ = new label_node($1, $2, $3); } // label name can be just an integer
| IDENT ':' statement { $$ = new label_node($1, $2, $3); }
| compoundst { $$ = $1; }
| try_finally
| try_except
compoundst: BEGIN sequence END { $$ = new compound_node($1, $2, $3); }
assemblerst: ASM asm_text END { $$ = new asm_block_node($1, $2, $3); }
// this "stub" for pascal assembler functions, no real assembler parsing yet.
asm_kwd: IDENT | XOR | ICONST | '@' | ':' | '[' | ']' | '(' | ')' | ',' | PLUS | MINUS
asm_line: asm_kwd { $$ = new asm_line_node($1, NULL); }
asm_text: asm_line
| asm_line asm_text
sequence: statement | statement ';' sequence { $1->next = $3; $$ = $1; }
//actual_params: { $$ = NULL; } | expr_group { $$ = $1; }
//write_params: { $$ = NULL; }
// | '(' write_list ')' { $$ = new write_list_node($1, $2, $3); }
case_list: case_items
| case_items ELSE sequence
{
if ($1 != NULL) {
case_node** cpp;
for(cpp = &$1->next; *cpp != NULL; cpp = &(*cpp)->next);
*cpp = new case_node(NULL, $2, $3);
$$ = $1;
} else {
$$ = new case_node(NULL, $2, $3);
}
}
//otherwise: OTHERWISE | ELSE /* Turbo Pascal */
case_items: { $$ = NULL; }
| case_item
| case_item ';' case_items { $1->next = $3; $$ = $1; }
case_item: case_elem_list ':' statement { $$ = new case_node($1, $2, $3); }
case_elem_list: case_elem
| case_elem ',' case_elem_list { $1->next = $3; $$ = $1; }
case_elem: const_expr { $$ = $1; }
| const_expr DOTS const_expr { $$ = new case_range_node($1, $2, $3); }
/*
//=============================================================================
// Expression level syntax:
// constant ::= integer | real | string | set_construct | ident
// set_elem ::= expr | expr '..' expr
// set_construct ::= '[' set_elem { ',' set_elem } ']'
// expr_group ::= '(' expr_list ')'
// expr_list ::= expr { ',' expr }
// primary ::= '(' expr ')' | call_expr
// | deref_expr | idx_expr | access_expr | constant
// access_expr ::= expr '.' ident
// deref_expr ::= expr '^'
// idx_expr ::= expr '[' expr_list ']'
// call_expr ::= expr expr_group
// binary ::= expr op expr
// unary ::= op expr
// expr ::= primary | unary | binary
// write_list ::= '(' write_param { ',' write_param } ')'
// write_param ::= expr [ ':' constant [ ':' constant ] ]
//=============================================================================
*/
condition_start: CIFDEF | CIF | CIFNDEF
condition_const_expr:
condition_start const_expr CELSE const_expr CENDIF { $$ = new cond_expr_node($1, $2, $3, $4, $5); }
| condition_start const_expr CENDIF { $$ = new cond_expr_node($1, $2, NULL, NULL, $3); }
const_expr: const_simple_expr
| condition_const_expr
| const_expr PLUS const_expr { $$ = new op_node(tn_add, $1, $2, $3); }
| const_expr MINUS const_expr { $$ = new op_node(tn_sub, $1, $2, $3); }
| const_expr MOD const_expr { $$ = new op_node(tn_mod, $1, $2, $3); }
| const_expr MUL const_expr { $$ = new op_node(tn_mul, $1, $2, $3); }
| const_expr DIV const_expr { $$ = new op_node(tn_div, $1, $2, $3); }
| const_expr DIVR const_expr { $$ = new op_node(tn_divr, $1, $2, $3); }
| const_expr AND const_expr { $$ = new op_node(tn_and, $1, $2, $3); }
| const_expr BINAND const_expr { $$ = new op_node(tn_binand, $1, $2, $3); }
| const_expr SHL const_expr { $$ = new op_node(tn_shl, $1, $2, $3); }
| const_expr SHR const_expr { $$ = new op_node(tn_shr, $1, $2, $3); }
| const_expr OR const_expr { $$ = new op_node(tn_or, $1, $2, $3); }
| const_expr BINOR const_expr { $$ = new op_node(tn_binor, $1, $2, $3); }
| const_expr XOR const_expr { $$ = new op_node(tn_xor, $1, $2, $3); }
| const_expr GT const_expr { $$ = new op_node(tn_gt, $1, $2, $3); }
| const_expr LT const_expr { $$ = new op_node(tn_lt, $1, $2, $3); }
| const_expr LE const_expr { $$ = new op_node(tn_le, $1, $2, $3); }
| const_expr GE const_expr { $$ = new op_node(tn_ge, $1, $2, $3); }
// | const_expr EQ const_expr { $$ = new op_node(tn_eq, $1, $2, $3); } // this rule causes many warnings because we have IDENT EQ const_expr in const_def and var_decl, temporary commented out
| const_expr NE const_expr { $$ = new op_node(tn_ne, $1, $2, $3); }
| const_expr IN const_expr { $$ = new op_node(tn_in, $1, $2, $3); }
| const_expr IS const_expr { $$ = new op_node(tn_is, $1, $2, $3); }
| const_expr AS const_expr { $$ = new op_node(tn_as, $1, $2, $3); }
| '(' const_expr_list ')' { $$ = new expr_group_node($1, $2, $3); }
| const_simple_expr '(' const_act_param_list ')' { $$ = new fcall_node($1, $2, $3, $4); }
| '@' const_simple_expr { $$ = new address_node($1, $2); }
const_simple_expr: constant
| PLUS const_simple_expr %prec UPLUS
{ $$ = new op_node(tn_plus, NULL, $1, $2); }
| MINUS const_simple_expr %prec UMINUS
{ $$ = new op_node(tn_minus, NULL, $1, $2); }
| NOT const_simple_expr
{ $$ = new op_node(tn_not, NULL, $1, $2); }
| BINNOT const_simple_expr
{ $$ = new op_node(tn_binnot, NULL, $1, $2); }
const_expr_list: const_expr | const_expr ',' const_expr_list { $1->next = $3; $$ = $1; }
const_act_param_list: const_act_param
| const_act_param ',' const_act_param_list { $1->next = $3; $$ = $1; }
const_act_param: const_expr | { $$ = new skipped_node(curr_token->prev_relevant()); }
condition_expr:
condition_start expr CELSE expr CENDIF { $$ = new cond_expr_node($1, $2, $3, $4, $5); }
| condition_start expr CENDIF { $$ = new cond_expr_node($1, $2, NULL, NULL, $3); }
expr: simple_expr
| condition_expr
| expr PLUS expr { $$ = new op_node(tn_add, $1, $2, $3); }
| expr MINUS expr { $$ = new op_node(tn_sub, $1, $2, $3); }
| expr MOD expr { $$ = new op_node(tn_mod, $1, $2, $3); }
| expr MUL expr { $$ = new op_node(tn_mul, $1, $2, $3); }
| expr DIV expr { $$ = new op_node(tn_div, $1, $2, $3); }
| expr DIVR expr { $$ = new op_node(tn_divr, $1, $2, $3); }
| primary LET expr { $$ = new op_node(tn_let, $1, $2, $3); }
| primary LETADD expr { $$ = new op_node(tn_letadd, $1, $2, $3); }
| primary LETSUB expr { $$ = new op_node(tn_letsub, $1, $2, $3); }
| primary LETDIV expr { $$ = new op_node(tn_letdiv, $1, $2, $3); }
| primary LETMUL expr { $$ = new op_node(tn_letmul, $1, $2, $3); }
| primary LETSHL expr { $$ = new op_node(tn_letshl, $1, $2, $3); }
| primary LETSHR expr { $$ = new op_node(tn_letshr, $1, $2, $3); }
| primary LETAND expr { $$ = new op_node(tn_letand, $1, $2, $3); }
| primary LETOR expr { $$ = new op_node(tn_letor, $1, $2, $3); }
| expr AND expr { $$ = new op_node(tn_and, $1, $2, $3); }
| expr BINAND expr { $$ = new op_node(tn_binand, $1, $2, $3); }
| expr SHL expr { $$ = new op_node(tn_shl, $1, $2, $3); }
| expr SHR expr { $$ = new op_node(tn_shr, $1, $2, $3); }
| expr OR expr { $$ = new op_node(tn_or, $1, $2, $3); }
| expr BINOR expr { $$ = new op_node(tn_binor, $1, $2, $3); }
| expr XOR expr { $$ = new op_node(tn_xor, $1, $2, $3); }
| expr GT expr { $$ = new op_node(tn_gt, $1, $2, $3); }
| expr LT expr { $$ = new op_node(tn_lt, $1, $2, $3); }
| expr LE expr { $$ = new op_node(tn_le, $1, $2, $3); }
| expr GE expr { $$ = new op_node(tn_ge, $1, $2, $3); }
| expr EQ expr { $$ = new op_node(tn_eq, $1, $2, $3); } // this rule causes many warnings because we have IDENT EQ expr in const_def and var_decl, temporary commented out
| expr NE expr { $$ = new op_node(tn_ne, $1, $2, $3); }
| expr IN expr { $$ = new op_node(tn_in, $1, $2, $3); }
| expr IS expr { $$ = new op_node(tn_is, $1, $2, $3); }
| expr AS expr { $$ = new op_node(tn_as, $1, $2, $3); }
simple_expr: primary
| PLUS simple_expr %prec UPLUS
{ $$ = new op_node(tn_plus, NULL, $1, $2); }
| MINUS simple_expr %prec UMINUS
{ $$ = new op_node(tn_minus, NULL, $1, $2); }
| NOT simple_expr
{ $$ = new op_node(tn_not, NULL, $1, $2); }
| BINNOT simple_expr
{ $$ = new op_node(tn_binnot, NULL, $1, $2); }
| '@' primary { $$ = new address_node($1, $2); }
| AND primary %prec ADDRESS { $$ = new address_node($1, $2); }
primary: constant
| '(' expr_list ')' { $$ = new expr_group_node($1, $2, $3); }
| primary '(' act_param_list ')' { $$ = new fcall_node($1, $2, $3, $4); }
| primary '.' ident_ext { $$ = new access_expr_node($1, $2, $3); }
| primary '^' { $$ = new deref_expr_node($1, $2); }
| primary '[' expr_list ']' { $$ = new idx_expr_node($1, $2, $3, $4); }
| inherited
// | LOOPHOLE '(' type ',' expr ')' { $$ = new loophole_node($1, $2, $3, $4, $5, $6); }
constant: record_constant
| ICONST { $$ = new integer_node($1); }
| RCONST { $$ = new double_node($1); }
| SCONST { $$ = new string_node($1); }
| '[' set_elem_list ']' { $$ = new set_node($1, $2, $3); }
| IDENT { $$ = new atom_expr_node($1); }
| INDEX { $$ = new atom_expr_node($1); }
//| READ { $$ = new atom_expr_node($1); }
//| WRITE { $$ = new atom_expr_node($1); }
set_elem_list: { $$ = NULL; }
| set_elem
| set_elem ',' set_elem_list { $1->next = $3; $$ = $1; }
set_elem: const_expr { $$ = new set_elem_node($1); }
| const_expr DOTS const_expr { $$ = new set_range_node($1, $2, $3); }
expr_list: expr | expr ',' expr_list { $1->next = $3; $$ = $1; }
act_param_list: act_param
| act_param ',' act_param_list { $1->next = $3; $$ = $1; }
act_param: expr | { $$ = new skipped_node(curr_token->prev_relevant()); }
record_constant: '(' field_init_list ')' { $$ = new record_constant_node($1, $2, $3); }
field_init_list: field_init_item { $$ = $1; }
| field_init_item ';' field_init_list { $1->next = $3; $$ = $1; }
field_init_item: IDENT ':' const_expr { $$ = new field_init_node($1, $2, $3); }
//expr_group: '(' expr_list ')' { $$ = new expr_group_node($1, $2, $3); }
//write_list: write_param | write_param ',' write_list { $1->next = $3; $$ = $1; }
//write_param: expr { $$ = new write_param_node($1); }
// | expr ':' expr { $$ = new write_param_node($1, $2, $3); }
// | expr ':' expr ':' expr { $$ = new write_param_node($1, $2, $3, $4, $5); }
/*
//=============================================================================
// Declaration syntax:
// label_decl_part ::= [ LABEL ident { ',' ident } ';' ]
// const_def_part ::= [ CONST const_def ';' { const_def ';' } ]
// type_def_part ::= [ TYPE type_def ';' { type_def ';' } ]
// var_decl_part ::= [ VAR var_decls ';' ]
// proc_fwd_decl ::= proc_decl ';' ident ';'
// proc_decl ::= (PROCEDURE | FUNCTION) ident [ formal_params ] [ ':' type ]
// proc_def ::= proc_decl ';' body ';'
// formal_params ::= '(' formal_param { ';' formal_param } ')'
// formal_param ::= VAR var_decl | var_decl | proc_decl
// const_def ::= ident '=' expr
// type_def ::= ident '=' type
// var_decls ::= var_decl { ';' var_decl }
// var_decl ::= ident { ',' ident } ':' type
// type ::= simple_type | array_type | record_type | set_type |
// file_type | pointer_type | subrange_type | enum_type
// subrange_type ::= '[' expr '..' expr ']'
// enum_type ::= '(' expr { ',' expr } ')'
// pointer_type ::= '^' type
// file_type ::= FILE OF type_denoter
// set_type ::= SET OF type_denoter
// record_type ::= RECORD field_list END
// field_list ::= [ (fixed_part [';' variant_part] | variant_part) [;] ]
// fixed_part ::= var_decls
// variant_part ::= CASE selector OF
// variant { ';' variant }
// selector ::= [ tag_field ':' ] tag_type
// variant ::= constant { ',' constant } ':' '(' field_list ')'
// simple_type ::= ident
// array_type ::= ARRAY '[' index ']' OF type
// index ::= conformant_index | fixed_index
// conformant_index ::= var_decls
// fixed_index ::= range { ',' range }
// range ::= expr '..' expr | type
//=============================================================================
*/
label_decl_part: LABEL label_list ';'
{ $$ = new label_decl_part_node($1, $2, $3); }
label_list: ICONST { $$ = new token_list($1); }
| ICONST ',' label_list { $$ = new token_list($1, $3); }
| IDENT { $$ = new token_list($1); }
| IDENT ',' label_list { $$ = new token_list($1, $3); }
const: CONST | RESOURCESTRING
const_def_part: const const_def_list { $$ = new const_def_part_node($1, $2); }
const_def_list: const_def ';' { $$ = $1; }
| const_def ';' const_def_list { $1->next = $3; $$ = $1; }
const_def:
ident_ext EQ const_expr deprecatd
{ $$ = new const_def_node($1, $2, $3, $4); }
| ident_ext ':' const_type EQ const_expr deprecatd
{ $$ = new typed_const_def_node($1, $2, $3, $4, $5, $6); }
type_def_part: TYPE type_def_list
{ $$ = new type_def_part_node($1, $2); }
type_def_list: { $$ = NULL; }
| type_def ';' type_def_list { $1->next = $3; $$ = $1; }
type_def:
IDENT EQ type { $$ = new type_def_node($1, $2, $3); }
| IDENT EQ TYPE type { $$ = new type_def_node($1, $2, $4); } // this is so called 'strong type alias' in Delphi, example: type CppLongInt = type LongInt;
| IDENT EQ CLASS {$$ = new type_def_node($1, $2, new object_tpd_node($3, NULL, NULL, NULL, NULL, NULL, NULL)); }
| simple_templ_type EQ type { $$ = new type_def_templ_node($1, $2, $3); }
var_decl_part: VAR var_decl_list { $$ = new var_decl_part_node(NULL, $1, $2); }
var_decl_list: //var_decl
var_decl_c //';' { $$ = $1; }
| var_decl_c var_decl_list { $1->next = $2; $$ = $1; }
attr_content: SCONST { $$ = new attrib_content($1, NULL, NULL, NULL); }
| UNSAFE { $$ = new attrib_content($1, NULL, NULL, NULL); }
| IDENT { $$ = new attrib_content($1, NULL, NULL, NULL); }
| IDENT '(' act_param_list ')' { $$ = new attrib_content($1, $2, $3, $4); }
attr_decl: // { $$ = NULL; }
'[' attr_content ']' { $$ = new attrib_node($1, $2, $3); }
var_decl_c: var_decl_semi
| condition_start var_decl_semi CELSE var_decl_semi CENDIF { $$ = new cond_var_decl_node($1, $2, $3, $4, $5); }
| condition_start var_decl_semi CENDIF { $$ = new cond_var_decl_node($1, $2, NULL, NULL, $3); }
var_decl_semi: var_decl ';' {$$ = $1; }
var_decl:
ident_list ':' type deprecatd { $$ = new var_decl_node(NULL, $1, $2, $3, NULL, NULL, NULL); }
| ident_list ':' type EQ const_expr deprecatd { $$ = new var_decl_node(NULL, $1, $2, $3, $4, $5, $6); }
| attr_decl ident_list ':' type deprecatd { $$ = new var_decl_node($1, $2, $3, $4, NULL, NULL, $5); }
| attr_decl ident_list ':' type EQ const_expr deprecatd { $$ = new var_decl_node($1, $2, $3, $4, $5, $6, $7); }
// temporarity commented out since it generates too many bison warnings (ambiguities)
// | IDENT ORIGIN const_expr ':' const_simple_type
// { $$ = (var_decl_node*)new var_origin_decl_node($1, $2, $3, $4, $5); }
/* Temporary comment it out. Since we have proc_spec that does everything we need.
The only problem need to be checked is having proc_decl in formal_params
proc_decl:
PROCEDURE IDENT formal_params
{ $$ = new proc_decl_node($1, $2, $3); }
| FUNCTION IDENT formal_params ':' type
{ $$ = new proc_decl_node($1, $2, $3, $4, $5); }
*/
// proc_fwd_decl and proc_spec - we cannot replace IDENT by ident_ext because bison generates too many warnings for unknown reason
// while duplicated lines with IDENT replaced by READ, WRITE and INDEX work well
proc_fwd_decl:
PROCEDURE ident_ext formal_params optional_semicolon qualifiers ';'
{ $$ = new proc_fwd_decl_node(NULL, $1, $2, $3, NULL, NULL, $4, $5, $6); }
| FUNCTION ident_ext formal_params ':' type optional_semicolon qualifiers ';'
{ $$ = new proc_fwd_decl_node(NULL, $1, $2, $3, $4, $5, $6, $7, $8); }
| attr_decl PROCEDURE ident_ext formal_params optional_semicolon qualifiers ';'
{ $$ = new proc_fwd_decl_node($1, $2, $3, $4, NULL, NULL, $5, $6, $7); }
| attr_decl FUNCTION ident_ext formal_params ':' type optional_semicolon qualifiers ';'
{ $$ = new proc_fwd_decl_node($1, $2, $3, $4, $5, $6, $7, $8, $9); }
proc_spec:
PROCEDURE ident_ext formal_params ';'
{ $$ = new proc_fwd_decl_node(NULL, $1, $2, $3, NULL, NULL, $4, NULL, NULL); }
| FUNCTION ident_ext formal_params ':' type ';'
{ $$ = new proc_fwd_decl_node(NULL, $1, $2, $3, $4, $5, $6, NULL, NULL); }
| attr_decl PROCEDURE ident_ext formal_params ';'
{ $$ = new proc_fwd_decl_node($1, $2, $3, $4, NULL, NULL, $5, NULL, NULL); }
| attr_decl FUNCTION ident_ext formal_params ':' type ';'
{ $$ = new proc_fwd_decl_node($1, $2, $3, $4, $5, $6, $7, NULL, NULL); }
// operators cannot have Delphi attributes, that is why no version with attr_decl in front
operator_fwd_decl:
OPERATOR IDENT formal_params ':' type ';'
{ $$ = new operator_fwd_decl_node($1, $2, $3, $4, $5, $6, NULL, NULL); }
| OPERATOR IDENT formal_params ':' type optional_semicolon qualifiers ';' // here should be fun_qualifierS however we use qualifiers for simplicity.
{ $$ = new operator_fwd_decl_node($1, $2, $3, $4, $5, $6, $7, $8); }
proc_def:
PROCEDURE ident_ext formal_params ';' block ';'
{ $$ = new proc_def_node(NULL, $1, NULL, NULL, $2, $3, NULL, NULL, $4, $5, $6); }
| FUNCTION ident_ext formal_params ':' type ';' block ';'
{ $$ = new proc_def_node(NULL, $1, NULL, NULL, $2, $3, $4, $5, $6, $7, $8); }
| PROCEDURE IDENT '.' ident_ext formal_params ';' block ';'
{ $$ = new proc_def_node(NULL, $1, $2, $3, $4, $5, NULL, NULL, $6, $7, $8); }
| FUNCTION IDENT '.' ident_ext formal_params ':' type ';' block ';'
{ $$ = new proc_def_node(NULL, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10); }
| CLASS PROCEDURE IDENT '.' ident_ext formal_params ';' block ';'
{ $$ = new proc_def_node($1, $2, $3, $4, $5, $6, NULL, NULL, $7, $8, $9); }
| CLASS FUNCTION IDENT '.' ident_ext formal_params ':' type ';' block ';'
{ $$ = new proc_def_node($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11); }
| CLASS OPERATOR IDENT formal_params ':' type ';' block ';'
{ $$ = new proc_def_node($1, $2, NULL,NULL, $3, $4, $5, $6, $7, $8, $9); }
| CLASS OPERATOR IDENT '.' IDENT formal_params ':' type ';' block ';'
{ $$ = new proc_def_node($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11); }
// | FUNCTION IDENT ';' block ';'
// { $$ = new proc_def_node($1, NULL, NULL, $2, NULL, NULL, NULL, $3, NULL, NULL, $4, $5); }
/* we do not need FAR specifier any more */