-
Notifications
You must be signed in to change notification settings - Fork 0
/
oberonparser.py
852 lines (626 loc) · 19 KB
/
oberonparser.py
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
#!/bin/python
# PyOberon 0.1.1 - Oberon 07 compiler (re-)written in Python
# Copyright (C) 2016 John "The Blue Wizard" Rogers
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Translated from several sources, including ORP.Mod (N. Wirth 1.7.97 / 7.6.2014
# Oberon 07 parser for RISC in Python
# This version of Oberon parser does parsing of source codes, with a few differences.
# It uses scanner.py to get symbols, and it uses yacc from PLY to parse them. The
# output is an AST. Note that unlike ORP, it does not do any type checking, nor
# generate code. that is being done by modules analyzer.py, optimizer.py and
# codegen.py.
# see http://www.cs.may.ie/~jpower/Oberon/oberon.y (it's for Oberon-2 though)
import sys
sys.path.insert(0,r"C:\Users\user1\wbench\ply-3.8")
from ply import *
import scanner
tokens = scanner.tokens
precedence = (
('nonassoc', 'EQ', 'NE', 'LT', 'LE', 'GT', 'GE', 'IN', 'IS'),
('left', 'PLUS', 'MINUS', 'OR'),
('right', 'UPLUS', 'UMINUS'),
('left', 'STAR', 'SLASH', 'DIV', 'MOD', 'AND'),
('right', 'TILDE')
)
# support routines
def starred_as(s, text):
if s == '*':
return text
return ''
#### module
def p_module(p):
'''module : MODULE star_opt ident SEMI import_statement_opt declaration_sequence body_opt END ident DOT'''
version = starred_as(p[2], 'OLD')
p[0] = ['MODULE', version, p[3], p[5], p[6], p[7], p[9]]
#### import statement
def p_import_statement_opt_0(p):
'''import_statement_opt : IMPORT import_list SEMI'''
p[0] = p[2]
def p_import_statement_opt_1(p):
'''import_statement_opt : '''
# empty import
p[0] = None
# import list
def p_import_list_0(p):
'''import_list : import_list COMMA module_name'''
p[0] = p[1] + [p[3]]
def p_import_list_1(p):
'''import_list : module_name'''
p[0] = [p[1]]
# import name
def p_module_name_0(p):
'''module_name : ident BECOMES ident'''
p[0] = [p[1], p[3]]
def p_module_name_1(p):
'''module_name : ident'''
p[0] = [p[1], p[1]]
#### declaration sequence
def p_declaration_sequence(p):
'''declaration_sequence : constants types variables procedures'''
p[0] = [p[1], p[2], p[3], p[4]]
#### constants
def p_constants_0(p):
'''constants : CONST constant_list'''
p[0] = p[2]
def p_constants_1(p):
'''constants : '''
p[0] = None
# constant list
def p_constant_list_0(p):
'''constant_list : constant_list constant_declaration SEMI'''
p[0] = p[1] + [p[2]]
def p_constant_list_1(p):
'''constant_list : constant_declaration SEMI'''
p[0] = [p[1]]
# constant declaration
def p_constant_declaration(p):
'''constant_declaration : identdef EQ constant_expr'''
p[0] = [p[1], p[3]]
#### types
def p_types_0(p):
'''types : TYPE type_list'''
p[0] = p[2]
def p_types_1(p):
'''types : '''
p[0] = None
# type list
def p_type_list_0(p):
'''type_list : type_list type_declaration'''
p[0] = p[1] + [p[2]]
def p_type_list_1(p):
'''type_list : type_declaration'''
p[0] = [p[1]]
# type declaration
# Note: A careful examination reveal the discrepancy in the
# type declaration syntax as defined in Oberon 07 grammar and
# that found in ORP.Mod's Type0 routine, namely that it parses
# qualident in addition to StrucType. It pays to have the
# original source code at hand!
def p_type_declaration(p):
'''type_declaration : identdef EQ type SEMI'''
# was: '''type_declaration : identdef EQ structure_type SEMI'''
p[0] = [p[1], p[3]]
# structure type
def p_structure_type_0(p):
'''structure_type : array_type'''
p[0] = p[1]
def p_structure_type_1(p):
'''structure_type : record_type'''
p[0] = p[1]
def p_structure_type_2(p):
'''structure_type : pointer_type'''
p[0] = p[1]
def p_structure_type_3(p):
'''structure_type : procedure_type'''
p[0] = p[1]
# array type
# Note: The optimizer will expand "ARRAY n1, n2,
# ..., nk OF not_array_type" into "ARRAY n1 OF
# ARRAY n2 OF ... ARRAY nk OF not_array_type"
# and create appropriate AST. I elected to not
# do that in analyzer.py, so analyzer can return
# a fully validated, typed AST that faithfully
# capture the structure as is, for future trans-
# formation by a utility.
def p_array_type_0(p):
'''array_type : ARRAY constant_expr_list OF type'''
p[0] = ['ARRAY', p[2], p[4]]
def p_array_type_1(p):
'''array_type : ARRAY OF type'''
p[0] = ['ARRAY', None, p[3]]
def p_constant_expr_list_0(p):
'''constant_expr_list : constant_expr_list COMMA constant_expr'''
p[0] = p[1] + [p[3]]
def p_constant_expr_list_1(p):
'''constant_expr_list : constant_expr'''
p[0] = [p[1]]
# record type
# Note that this deviates a bit from the Oberon
# grammar documentation. This is confirmed by
# looking at the source code in ORP.Mod.Txt (in
# procedure RecordType), and the record TypeDesc
# in the ORB.Mod.Txt file has this extra semicolon.
# Therefore this parser must support that.
def p_record_type_0(p):
'''record_type : RECORD base_type_opt field_list semi_opt END'''
p[0] = ['RECORD', p[2], p[3]]
def p_record_type_1(p):
'''record_type : RECORD base_type_opt END'''
p[0] = ['RECORD', p[2], None]
def p_base_type_opt_0(p):
'''base_type_opt : LPAREN qualident RPAREN'''
p[0] = p[2]
def p_base_type_opt_1(p):
'''base_type_opt : '''
p[0] = None
def p_field_list_0(p):
'''field_list : field_list SEMI field'''
p[0] = p[1] + [p[3]]
def p_field_list_1(p):
'''field_list : field'''
p[0] = [p[1]]
def p_field(p):
'''field : ident_list COLON type'''
p[0] = [p[1], p[3]]
def p_semi_opt_0(p):
'''semi_opt : SEMI'''
p[0] = ';'
def p_semi_opt_1(p):
'''semi_opt : '''
p[0] = ''
# pointer type
def p_pointer_type(p):
'''pointer_type : POINTER TO type'''
p[0] = ['POINTER', p[3]]
# procedure type
def p_procedure_type(p):
'''procedure_type : PROCEDURE formal_parameters_opt'''
p[0] = ['PROCEDURE', p[2]]
def p_formal_parameters_opt_0(p):
'''formal_parameters_opt : formal_parameters'''
p[0] = p[1]
def p_formal_parameters_opt_1(p):
'''formal_parameters_opt : '''
p[0] = None
def p_formal_parameters_0(p):
'''formal_parameters : LPAREN FP_list_opt RPAREN COLON qualident'''
p[0] = [p[2], p[5]]
def p_formal_parameters_1(p):
'''formal_parameters : LPAREN FP_list_opt RPAREN'''
p[0] = [p[2], None]
def p_FP_list_opt_0(p):
'''FP_list_opt : FP_list'''
p[0] = p[1]
def p_FP_list_opt_1(p):
'''FP_list_opt : '''
p[0] = None
def p_FP_list_0(p):
'''FP_list : FP_list SEMI formal_param'''
p[0] = p[1] + [p[3]]
def p_FP_list_1(p):
'''FP_list : formal_param'''
p[0] = [p[1]]
def p_formal_param(p):
'''formal_param : VAR_opt fp_ident_list COLON formal_type'''
p[0] = [p[2], p[1], p[4]]
def p_VAR_opt_0(p):
'''VAR_opt : VAR'''
p[0] = 'VAR'
def p_VAR_opt_1(p):
'''VAR_opt : '''
p[0] = None
def p_fp_ident_list_0(p):
'''fp_ident_list : fp_ident_list COMMA fp_ident'''
p[0] = p[1] + [p[3]]
def p_fp_ident_list_1(p):
'''fp_ident_list : fp_ident'''
p[0] = [p[1]]
def p_fp_ident(p):
'''fp_ident : ident'''
p[0] = p[1]
# Note: Contrary to Oberon 07 Report (revised???) which states in
# section 10.1 that the formal types is just "{ARRAY OF} qualident",
# GRAPHICS.MOD does use PROCEDURE type. This code follows what's
# found in ORP.MOD, which turned out to be just one more type added.
def p_formal_type_0(p):
'''formal_type : qualident'''
p[0] = ['QUALIDENT', p[1]]
def p_formal_type_1(p):
'''formal_type : ARRAY OF formal_type'''
count = 0
if p[3][0] == 'ARRAY':
count = p[3][1]
type = p[3][2]
else:
count = 0
type = p[3]
p[0] = ['ARRAY', count+1, type]
def p_formal_type_2(p):
'''formal_type : procedure_type'''
p[0] = p[1]
#### variables
def p_variables_0(p):
'''variables : VAR variable_list'''
p[0] = p[2]
def p_variables_1(p):
'''variables : '''
p[0] = None
# variable list
def p_variable_list_0(p):
'''variable_list : variable_list variable_declaration SEMI'''
p[0] = p[1] + [p[2]]
def p_variable_list_1(p):
'''variable_list : variable_declaration SEMI'''
p[0] = [p[1]]
# variable declaration
def p_variable_declaration(p):
'''variable_declaration : ident_list COLON type'''
p[0] = [p[1], p[3]]
def p_ident_list_0(p):
'''ident_list : ident_list COMMA identdef'''
p[0] = p[1] + [p[3]]
def p_ident_list_1(p):
'''ident_list : identdef'''
p[0] = [p[1]]
# type
def p_type_0(p):
'''type : qualident'''
p[0] = p[1]
def p_type_1(p):
'''type : structure_type'''
p[0] = p[1]
#### procedures
def p_procedures_0(p):
'''procedures : procedures procedure_declaration SEMI'''
p[0] = p[1] + [p[2]]
def p_procedures_1(p):
'''procedures : '''
p[0] = []
def p_procedure_declaration(p):
'''procedure_declaration : procedure_heading SEMI procedure_body ident'''
p[0] = [p[1], p[3], p[4]]
def p_procedure_heading(p):
'''procedure_heading : PROCEDURE star_opt identdef formal_parameters_opt'''
p[0] = ['PROCEDURE', starred_as(p[2], 'INTERRUPT'), p[3], p[4]]
def p_procedure_body(p):
'''procedure_body : declaration_sequence body_opt procedure_return_opt END'''
p[0] = [p[1], p[2], p[3]]
def p_procedure_return_opt_0(p):
'''procedure_return_opt : RETURN expr'''
p[0] = p[2]
def p_procedure_return_opt_1(p):
'''procedure_return_opt : '''
p[0] = None
#### body
def p_body_opt_0(p):
'''body_opt : BEGIN statement_sequence'''
p[0] = p[2]
def p_body_opt_1(p):
'''body_opt : '''
# empty body
p[0] = None
# statement sequence
def p_statement_sequence_0(p):
'''statement_sequence : statement_sequence SEMI statement'''
p[0] = p[1] + [p[3]]
def p_statement_sequence_1(p):
'''statement_sequence : statement'''
p[0] = [p[1]]
# statement
def p_statement_0(p):
'''statement : assignment'''
p[0] = p[1]
def p_statement_1(p):
'''statement : procedure_call'''
p[0] = p[1]
def p_statement_2(p):
'''statement : if_statement'''
p[0] = p[1]
def p_statement_3(p):
'''statement : case_statement'''
p[0] = p[1]
def p_statement_4(p):
'''statement : while_statement'''
p[0] = p[1]
def p_statement_5(p):
'''statement : repeat_statement'''
p[0] = p[1]
def p_statement_6(p):
'''statement : for_statement'''
p[0] = p[1]
def p_statement_7(p):
'''statement : '''
# empty statement
p[0] = []
# assignment
def p_assignment(p):
'''assignment : designator BECOMES expr'''
p[0] = ['ASSIGN', p[1], p[3]]
# procedure call
def p_procedure_call(p):
'''procedure_call : designator'''
p[0] = ['CALL', p[1]]
# if statement
def p_if_statement(p):
'''if_statement : IF if_condition_clause_list else_opt END'''
p[0] = ['IF'] + p[2]
if p[3] != []:
p[0].append(p[3])
def p_if_condition_clause_list_0(p):
'''if_condition_clause_list : if_condition_clause_list ELSIF if_condition_clause'''
p[0] = p[1] + [p[3]]
def p_if_condition_clause_list_1(p):
'''if_condition_clause_list : if_condition_clause'''
p[0] = [p[1]]
def p_if_condition_clause(p):
'''if_condition_clause : expr THEN statement_sequence'''
p[0] = [p[1], p[3]]
def p_else_opt_0(p):
'''else_opt : ELSE statement_sequence'''
p[0] = [None, p[2]]
def p_else_opt_1(p):
'''else_opt : '''
p[0] = []
# case statement
def p_case_statement(p):
'''case_statement : CASE expr OF case_list END'''
p[0] = ['CASE', p[2]] + p[4]
def p_case_list_0(p):
'''case_list : case_list BAR case'''
p[0] = p[1] + [p[3]]
def p_case_list_1(p):
'''case_list : case'''
p[0] = [p[1]]
def p_case(p):
'''case : case_label_list COLON statement_sequence'''
p[0] = [p[1], p[3]]
def p_case_label_list_0(p):
'''case_label_list : case_label_list COMMA label_range'''
p[0] = p[1] + [p[3]]
def p_case_label_list_1(p):
'''case_label_list : label_range'''
p[0] = [p[1]]
def p_label_range_0(p):
'''label_range : label UPTO label'''
p[0] = [p[1], p[3]]
def p_label_range_1(p):
'''label_range : label'''
p[0] = [p[1], None]
def p_label_0(p):
'''label : INTEGER'''
p[0] = p[1]
def p_label_1(p):
'''label : STRING'''
p[0] = p[1]
def p_label_2(p):
'''label : qualident'''
p[0] = p[1]
# while statement
def p_while_statement(p):
'''while_statement : WHILE while_condition_clause_list END'''
p[0] = ['WHILE'] + p[2]
def p_while_condition_clause_list_0(p):
'''while_condition_clause_list : while_condition_clause_list ELSIF while_condition_clause'''
p[0] = p[1] + [p[3]]
def p_while_condition_clause_list_1(p):
'''while_condition_clause_list : while_condition_clause'''
p[0] = [p[1]]
def p_while_condition_clause(p):
'''while_condition_clause : expr DO statement_sequence'''
p[0] = [p[1], p[3]]
# repeat statement
def p_repeat_statement(p):
'''repeat_statement : REPEAT statement_sequence UNTIL expr'''
p[0] = ['REPEAT', p[2], p[4]]
# for statement
def p_for_statement(p):
'''for_statement : FOR qualident BECOMES expr TO expr by_opt DO statement_sequence END'''
# was: '''for_statement : FOR ident BECOMES expr TO expr by_opt DO statement_sequence END'''
# Note: According to the grammar, it should be an 'ident', but ORP.Mod treats the
# FOR variable as a qualident. So I elect to code it accordingly.
p[0] = ['FOR', p[2], p[4], p[6], p[7], p[8]]
def p_by_opt_0(p):
'''by_opt : BY constant_expr'''
p[0] = p[2]
def p_by_opt_1(p):
'''by_opt : '''
p[0] = None
# constant expr
def p_constant_expr(p):
'''constant_expr : expr'''
p[0] = p[1]
# expr
def p_expr_0(p):
'''expr : expr LT expr'''
p[0] = ['LT', p[1], p[3]]
def p_expr_1(p):
'''expr : expr LE expr'''
p[0] = ['LE', p[1], p[3]]
def p_expr_2(p):
'''expr : expr GT expr'''
p[0] = ['GT', p[1], p[3]]
def p_expr_3(p):
'''expr : expr GE expr'''
p[0] = ['GE', p[1], p[3]]
def p_expr_4(p):
'''expr : expr EQ expr'''
p[0] = ['EQUALS', p[1], p[3]]
def p_expr_5(p):
'''expr : expr NE expr'''
p[0] = ['NE', p[1], p[3]]
def p_expr_6(p):
'''expr : expr AND expr'''
p[0] = ['AND', p[1], p[3]]
def p_expr_7(p):
'''expr : expr OR expr'''
p[0] = ['OR', p[1], p[3]]
def p_expr_8(p):
'''expr : expr IN expr'''
p[0] = ['IN', p[1], p[3]]
def p_expr_9(p):
'''expr : expr IS expr'''
p[0] = ['IS', p[1], p[3]]
def p_expr_10(p):
'''expr : expr MOD expr'''
p[0] = ['MOD', p[1], p[3]]
def p_expr_11(p):
'''expr : expr PLUS expr'''
p[0] = ['ADD', p[1], p[3]]
def p_expr_12(p):
'''expr : expr MINUS expr'''
p[0] = ['SUBTRACT', p[1], p[3]]
def p_expr_13(p):
'''expr : expr STAR expr'''
p[0] = ['TIMES', p[1], p[3]]
def p_expr_14(p):
'''expr : expr DIV expr'''
p[0] = ['INT_DIVIDE', p[1], p[3]]
def p_expr_15(p):
'''expr : expr SLASH expr'''
p[0] = ['DIVIDE', p[1], p[3]]
def p_expr_16(p):
'''expr : PLUS expr %prec UPLUS'''
p[0] = ['UPLUS', p[2]]
def p_expr_17(p):
'''expr : MINUS expr %prec UMINUS'''
p[0] = ['UMINUS', p[2]]
def p_expr_18(p):
'''expr : INTEGER'''
p[0] = ['INTEGER', p[1]]
def p_expr_19(p):
'''expr : REAL'''
p[0] = ['REAL', p[1]]
def p_expr_20(p):
'''expr : CHAR'''
p[0] = ['CHAR', p[1]]
def p_expr_21(p):
'''expr : STRING'''
p[0] = ['STRING', p[1]]
def p_expr_22(p):
'''expr : HEXSTRING'''
p[0] = ['HEXSTRING', p[1]]
def p_expr_23(p):
'''expr : NIL'''
p[0] = ['NIL']
def p_expr_24(p):
'''expr : TRUE'''
p[0] = ['TRUE']
def p_expr_25(p):
'''expr : FALSE'''
p[0] = ['FALSE']
def p_expr_26(p):
'''expr : set'''
p[0] = p[1]
def p_expr_27(p):
'''expr : designator'''
p[0] = p[1]
def p_expr_28(p):
'''expr : LPAREN expr RPAREN'''
p[0] = p[2]
def p_expr_29(p):
'''expr : TILDE expr'''
p[0] = ['NOT', p[2]]
def p_set_0(p):
'''set : LBRACE element_list RBRACE'''
p[0] = ['SET', p[2]]
def p_set_1(p):
'''set : LBRACE RBRACE'''
p[0] = ['SET', None]
def p_element_list_0(p):
'''element_list : element_list COMMA element'''
p[0] = p[1] + [p[3]]
def p_element_list_1(p):
'''element_list : element'''
p[0] = [p[1]]
def p_element_0(p):
'''element : expr UPTO expr'''
p[0] = [p[1], p[3]]
def p_element_1(p):
'''element : expr'''
p[0] = [p[1], None]
# designator
# parser can't tell whether the first ident is a module
# ident, so I modify the designator so it just use
# ident instead of qualident, and let the analyzer
# fix it.
def p_designator(p):
'''designator : ident selector_list'''
# was: '''designator : qualident selector_list'''
p[0] = ['DESIGNATOR', p[1]] + p[2]
# selector_list
def p_selector_list_0(p):
'''selector_list : selector_list selector'''
p[0] = p[1] + [p[2]]
def p_selector_list_1(p):
'''selector_list : '''
p[0] = []
# selector
def p_selector_0(p):
'''selector : DOT ident'''
p[0] = ['DOT', p[2]]
def p_selector_1(p):
'''selector : LBRAK expr_list RBRAK'''
p[0] = ['ARRAYINDICES', p[2]]
def p_selector_2(p):
'''selector : ARROW'''
p[0] = ['ARROW']
# parser can't tell between variable, procedure/function
# call and typeguard by syntax alone, so the selector is
# deliberately overgeneralized, and let the analyzer fix
# it.
# hypothetical def p_selector_3(p):
# '''selector : LPAREN qualident RPAREN'''
# p[0] = ['TYPEGUARD', p[2]]
# note that expr can include qualident via designator,
# thus satisfying the TYPEGUARD syntactic construct.
# this construct identifies it as PROCEDURE/FUNCTION call
def p_selector_3(p):
'''selector : LPAREN expr_list RPAREN'''
p[0] = ['PARAMS', p[2]]
def p_selector_4(p):
'''selector : LPAREN RPAREN'''
p[0] = ['PARAMS', None]
# expr list
def p_expr_list_0(p):
'''expr_list : expr_list COMMA expr'''
p[0] = p[1] + [p[3]]
def p_expr_list_1(p):
'''expr_list : expr'''
p[0] = [p[1]]
# qualident
def p_qualident_0(p):
'''qualident : ident DOT ident'''
p[0] = [p[1], p[3]]
def p_qualident_1(p):
'''qualident : ident'''
p[0] = [None, p[1]]
# identdef
def p_identdef(p):
'''identdef : ident star_opt'''
p[0] = [p[1], starred_as(p[2], 'EXPORTED')]
def p_star_opt_0(p):
'''star_opt : STAR'''
p[0] = p[1]
def p_star_opt_1(p):
'''star_opt : '''
p[0] = ''
# ident
def p_ident(p):
'''ident : ID'''
p[0] = p[1]
oberonparser = yacc.yacc()
def parse(data,debug=0):
oberonparser.error = 0
p = oberonparser.parse(data,debug=debug)
if oberonparser.error:
return None
return p