-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
1069 lines (982 loc) · 41.4 KB
/
parser.py
File metadata and controls
1069 lines (982 loc) · 41.4 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
from __future__ import annotations
import argparse
import sys
from typing import List, Optional, Sequence, Union
from ast_nodes import (AdrExpr, AdsExpr, ArrayType, AssignStmt, ASTNode, BinOp, Block, BoolLiteral, BreakStmt, BuiltinType, CaseElement, CaseStmt, CharLiteral, CompoundStmt,
ConstDecl, CycleStmt, Declaration, Designator, EmptyStmt, EnumType, Expression, FileType, ForStmt, FuncCall, FuncDecl, GotoStmt, Identifier, IfStmt,
ImplementationUnit, IndexRange, InterfaceUnit, IntLiteral, LabelDecl, LabelStmt, LowerExpr, LStringType, ModuleUnit, NamedType, NilLiteral, Param,
PointerType, ProcCallStmt, ProcDecl, ProgramUnit, RangeExpr, RealLiteral, RecordType, RepeatStmt, ReturnStmt, RetypeExpr, Selector, SetConstructor, SetType,
SizeofExpr, Statement, StringLiteral, SubrangeType, Type, TypeDecl, UnaryOp, UpperExpr, UseClause, ValueDecl, VarDecl, WhileStmt, WithStmt, WriteArg)
from lexer import ALL_CODES, KEYWORD_CODES, LexerError, Token, lex_file
class ParserError(Exception):
pass
class Parser:
def __init__(self, tokens: Sequence[Token]):
self.tokens = list(tokens)
self.pos = 0
def current(self) -> Token:
return self.tokens[self.pos]
def next_kind(self, offset: int = 1) -> str:
index = self.pos + offset
if index < len(self.tokens):
return self.tokens[index].kind
return 'EOF'
def match(self, kind: str) -> bool:
if self.current().kind == kind:
self.pos += 1
return True
return False
def expect(self, kind: str) -> Token:
tok = self.current()
if tok.kind != kind:
self.error(f"expected {kind}, got {tok.kind}")
self.pos += 1
return tok
def error(self, message: str) -> None:
tok = self.current()
raise ParserError(f"{message} at line {tok.line}, column {tok.column} (token {tok.kind} {tok.lexeme!r})")
def parse(self) -> Union[ProgramUnit, ModuleUnit, InterfaceUnit, ImplementationUnit]:
self.skip_include_directives()
interface: Optional[InterfaceUnit] = None
if self.current().kind == 'INTERFACE':
interface = self.parse_interface_unit()
self.skip_include_directives()
if self.current().kind == 'EOF':
return interface
unit = self.parse_compilation_unit()
if interface is not None and isinstance(unit, ImplementationUnit):
unit.interface = interface
self.skip_include_directives()
self.expect('EOF')
return unit
def parse_compilation_unit(self) -> Union[ProgramUnit, ModuleUnit, InterfaceUnit, ImplementationUnit]:
if self.current().kind == 'PROGRAM':
return self.parse_program_unit()
elif self.current().kind == 'MODULE':
return self.parse_module_unit()
elif self.current().kind == 'INTERFACE':
return self.parse_interface_unit()
elif self.current().kind == 'IMPLEMENTATION':
return self.parse_implementation_unit()
else:
self.error('expected compilation unit start')
def parse_program_unit(self) -> ProgramUnit:
self.expect('PROGRAM')
name = self.expect('IDENTIFIER').lexeme
params: List[str] = []
if self.match('LPAREN'):
params = self.parse_identifier_list()
self.expect('RPAREN')
self.expect('SEMICOLON')
self.skip_include_directives()
uses: List[UseClause] = []
while self.current().kind == 'USES':
uses.extend(self.parse_uses_clause())
self.skip_include_directives()
block = self.parse_block()
self.skip_include_directives()
self.expect('DOT')
return ProgramUnit(name, params, uses, block)
def parse_module_unit(self) -> ModuleUnit:
self.expect('MODULE')
name = self.expect('IDENTIFIER').lexeme
self.expect('SEMICOLON')
self.skip_include_directives()
uses: List[UseClause] = []
while self.current().kind == 'USES':
uses.extend(self.parse_uses_clause())
self.skip_include_directives()
decls: List[Declaration] = []
while self.current().kind in self.declaration_starters():
decls.extend(self.parse_declaration_section())
self.skip_include_directives()
self.expect('DOT')
return ModuleUnit(name, uses, decls)
def parse_interface_unit(self) -> InterfaceUnit:
self.expect('INTERFACE')
self.expect('SEMICOLON')
self.skip_include_directives()
self.expect('UNIT')
name = self.expect('IDENTIFIER').lexeme
params: List[str] = []
if self.match('LPAREN'):
params = self.parse_identifier_list()
self.expect('RPAREN')
self.expect('SEMICOLON')
self.skip_include_directives()
uses: List[UseClause] = []
while self.current().kind == 'USES':
uses.extend(self.parse_uses_clause())
self.skip_include_directives()
decls: List[Declaration] = []
while self.current().kind in self.declaration_starters():
decls.extend(self.parse_interface_declaration())
self.skip_include_directives()
# Interface terminator (grammar: {BEGIN}- END ;): exactly one END, optionally
# preceded by a BEGIN initialization block. The END closes the optional block
# AND terminates the interface -- there is no second END. The unit is therefore
# self-delimiting: when include-spliced, this END;/`;` is immediately followed by
# the IMPLEMENTATION/PROGRAM/MODULE that included it, with no special-casing.
if self.current().kind == 'BEGIN':
self.parse_compound_statement() # consumes BEGIN [statements] END
self.expect('SEMICOLON')
else:
self.expect('END')
self.expect('SEMICOLON')
return InterfaceUnit(name, params, uses, decls)
def parse_implementation_unit(self) -> ImplementationUnit:
self.expect('IMPLEMENTATION')
self.expect('OF')
name = self.expect('IDENTIFIER').lexeme
self.expect('SEMICOLON')
self.skip_include_directives()
uses: List[UseClause] = []
while self.current().kind == 'USES':
uses.extend(self.parse_uses_clause())
self.skip_include_directives()
decls: List[Declaration] = []
while self.current().kind in self.declaration_starters():
decls.extend(self.parse_declaration_section())
self.skip_include_directives()
init_body: Optional[List[Statement]] = None
if self.current().kind == 'BEGIN':
init_body = self.parse_compound_statement().stmts
self.skip_include_directives()
self.expect('DOT')
return ImplementationUnit(name, uses, decls, init_body)
def parse_uses_clause(self) -> List[UseClause]:
self.expect('USES')
clauses: List[UseClause] = []
clauses.append(self.parse_uses_import())
while self.match('COMMA'):
clauses.append(self.parse_uses_import())
self.match('SEMICOLON')
return clauses
def parse_uses_import(self) -> UseClause:
name = self.expect('IDENTIFIER').lexeme
imports: Optional[List[str]] = None
if self.match('LPAREN'):
imports = self.parse_identifier_list()
self.expect('RPAREN')
return UseClause(name, imports)
def declaration_starters(self) -> set[str]:
return {'CONST', 'TYPE', 'VAR', 'VALUE', 'LABEL', 'PROCEDURE', 'FUNCTION'}
def parse_block(self) -> Block:
self.skip_include_directives()
decls: List[Declaration] = []
while self.current().kind in self.declaration_starters():
decls.extend(self.parse_declaration_section())
self.skip_include_directives()
body = self.parse_compound_statement().stmts
return Block(decls, body)
def parse_declaration_section(self) -> List[Declaration]:
self.skip_include_directives()
kind = self.current().kind
if kind == 'CONST':
return self.parse_const_decl()
elif kind == 'TYPE':
return self.parse_type_decl()
elif kind == 'VAR':
return self.parse_var_decl()
elif kind == 'VALUE':
return self.parse_value_decl()
elif kind == 'LABEL':
return [self.parse_label_decl()]
elif kind == 'PROCEDURE':
return [self.parse_proc_decl()]
elif kind == 'FUNCTION':
return [self.parse_func_decl()]
else:
self.error('expected declaration section')
def parse_interface_declaration(self) -> List[Declaration]:
kind = self.current().kind
if kind in {'CONST', 'TYPE', 'VAR', 'LABEL'}:
return self.parse_declaration_section()
if kind == 'PROCEDURE':
name, params, attributes = self.parse_proc_decl_header()
self.expect('SEMICOLON')
# In an interface, procedures have no body (signature-only)
return [ProcDecl(name, params, attributes, body=None)]
if kind == 'FUNCTION':
name, params, return_type, attributes = self.parse_func_decl_header()
self.expect('SEMICOLON')
# In an interface, functions have no body (signature-only)
return [FuncDecl(name, params, return_type, attributes, body=None)]
self.error('expected interface declaration')
def parse_const_decl(self) -> List[ConstDecl]:
self.expect('CONST')
decls: List[ConstDecl] = []
while self.current().kind == 'IDENTIFIER':
name = self.expect('IDENTIFIER').lexeme
self.expect('EQ')
value = self.parse_constant()
self.expect('SEMICOLON')
decls.append(ConstDecl(name, value))
return decls
def parse_type_decl(self) -> List[TypeDecl]:
self.expect('TYPE')
decls: List[TypeDecl] = []
while self.current().kind == 'IDENTIFIER':
name = self.expect('IDENTIFIER').lexeme
self.expect('EQ')
type_expr = self.parse_type()
self.expect('SEMICOLON')
decls.append(TypeDecl(name, type_expr))
return decls
def parse_var_decl(self) -> List[VarDecl]:
self.expect('VAR')
decls: List[VarDecl] = []
while self.current().kind == 'IDENTIFIER' or self.current().kind == 'LBRACKET':
attributes = self.parse_attribute_section_optional()
names = self.parse_identifier_list()
self.expect('COLON')
type_expr = self.parse_type()
self.expect('SEMICOLON')
decls.append(VarDecl(names, type_expr, attributes))
return decls
def parse_value_decl(self) -> List[ValueDecl]:
self.expect('VALUE')
decls: List[ValueDecl] = []
while self.current().kind == 'IDENTIFIER':
name = self.expect('IDENTIFIER').lexeme
if self.current().kind in {'EQ', 'ASSIGN'}:
self.pos += 1
else:
self.error('expected = or := in value declaration')
value = self.parse_constant()
self.expect('SEMICOLON')
decls.append(ValueDecl(name, value))
return decls
def parse_label_decl(self) -> LabelDecl:
self.expect('LABEL')
labels: List[Union[int, str]] = []
labels.append(self.parse_label_id())
while self.match('COMMA'):
labels.append(self.parse_label_id())
self.expect('SEMICOLON')
return LabelDecl(labels)
def parse_proc_decl(self) -> ProcDecl:
name, params, attributes = self.parse_proc_decl_header()
self.expect('SEMICOLON')
body: Optional[Block] = None
if self.current().kind in {'EXTERN', 'EXTERNAL', 'FORWARD'}:
self.pos += 1
self.expect('SEMICOLON')
else:
body = self.parse_block()
self.expect('SEMICOLON')
return ProcDecl(name, params, attributes, body)
def parse_func_decl(self) -> FuncDecl:
name, params, return_type, attributes = self.parse_func_decl_header()
self.expect('SEMICOLON')
body: Optional[Block] = None
if self.current().kind in {'EXTERN', 'EXTERNAL', 'FORWARD'}:
self.pos += 1
self.expect('SEMICOLON')
else:
body = self.parse_block()
self.expect('SEMICOLON')
return FuncDecl(name, params, return_type, attributes, body)
def parse_proc_decl_header(self) -> tuple[str, List[Param], List[str]]:
self.expect('PROCEDURE')
name = self.expect('IDENTIFIER').lexeme
params: List[Param] = []
if self.match('LPAREN'):
params = self.parse_parameter_list()
self.expect('RPAREN')
attributes = self.parse_attribute_section_optional()
return name, params, attributes
def parse_func_decl_header(self) -> tuple[str, List[Param], Type, List[str]]:
self.expect('FUNCTION')
name = self.expect('IDENTIFIER').lexeme
params: List[Param] = []
if self.match('LPAREN'):
params = self.parse_parameter_list()
self.expect('RPAREN')
self.expect('COLON')
return_type = self.parse_type()
attributes = self.parse_attribute_section_optional()
return name, params, return_type, attributes
def parse_parameter_list(self) -> List[Param]:
params: List[Param] = []
params.append(self.parse_parameter_group())
while self.match('SEMICOLON'):
if self.current().kind == 'RPAREN':
break
params.append(self.parse_parameter_group())
return params
def parse_parameter_group(self) -> Param:
mode: Optional[str] = None
if self.current().kind in {'VAR', 'VARS', 'CONST', 'CONSTS'}:
mode = self.current().kind
self.pos += 1
names = self.parse_identifier_list()
self.expect('COLON')
type_expr = self.parse_type()
return Param(mode, names, type_expr)
def parse_attribute_section_optional(self) -> List[str]:
attributes: List[str] = []
if not self.match('LBRACKET'):
return attributes
if self.current().kind != 'RBRACKET':
attributes.append(self.parse_attribute_item())
while self.match('COMMA'):
attributes.append(self.parse_attribute_item())
self.expect('RBRACKET')
return attributes
def parse_attribute_item(self) -> str:
# The current implementation intentionally handles the six confirmed
# attribute keywords only. ORIGIN/PORT are documented elsewhere or
# unverified, and will be added in a separate pass if needed.
if self.current().kind in {'READONLY', 'PUBLIC', 'STATIC', 'EXTERNAL', 'EXTERN', 'PURE'}:
attr = self.current().kind
self.pos += 1
return attr
self.error('expected attribute item')
def parse_compound_statement(self) -> CompoundStmt:
self.expect('BEGIN')
stmts: List[Statement] = []
if self.current().kind != 'END':
stmts.append(self.parse_statement())
while self.match('SEMICOLON'):
if self.current().kind == 'END':
break
stmts.append(self.parse_statement())
self.expect('END')
return CompoundStmt(stmts)
def parse_statement(self) -> Statement:
kind = self.current().kind
if kind == 'BEGIN':
return self.parse_compound_statement()
if kind == 'IF':
return self.parse_if_statement()
if kind == 'FOR':
return self.parse_for_statement()
if kind == 'REPEAT':
return self.parse_repeat_statement()
if kind == 'WHILE':
return self.parse_while_statement()
if kind == 'CASE':
return self.parse_case_statement()
if kind == 'WITH':
return self.parse_with_statement()
if kind == 'GOTO':
self.pos += 1
label = self.parse_label_id()
return GotoStmt(label)
if kind == 'RETURN':
self.pos += 1
return ReturnStmt()
if kind == 'BREAK':
self.pos += 1
label = self.parse_optional_label_id()
return BreakStmt(label)
if kind == 'CYCLE':
self.pos += 1
label = self.parse_optional_label_id()
return CycleStmt(label)
if kind in {'INTEGER_LITERAL', 'IDENTIFIER'} and self.next_kind() == 'COLON':
return self.parse_label_statement()
if kind == 'IDENTIFIER':
return self.parse_assignment_or_proc_call()
if kind in {'SEMICOLON', 'END', 'UNTIL', 'ELSE', 'OTHERWISE', 'RPAREN'}:
return EmptyStmt()
self.error('expected statement')
def parse_assignment_or_proc_call(self) -> Statement:
name = self.expect('IDENTIFIER').lexeme
selectors: List[Selector] = []
while self.current().kind in {'LBRACKET', 'DOT', 'POINTER'}:
selectors.extend(self.parse_selector())
if self.current().kind == 'ASSIGN':
self.pos += 1
expr = self.parse_expression()
target = Designator(name, selectors)
return AssignStmt(target, expr)
if selectors:
self.error('designator statement must be an assignment')
args: List[Union[Expression, WriteArg]] = []
if self.current().kind == 'LPAREN':
self.pos += 1
if self.current().kind != 'RPAREN':
if name.upper() in {'WRITE', 'WRITELN'}:
args = self.parse_write_actual_parameter_list()
else:
args = self.parse_actual_parameter_list()
self.expect('RPAREN')
# Bare procedure call is allowed.
return ProcCallStmt(name, args)
def parse_actual_parameter_list(self) -> List[Expression]:
exprs: List[Expression] = []
exprs.append(self.parse_actual_parameter())
while self.match('COMMA'):
exprs.append(self.parse_actual_parameter())
return exprs
def parse_actual_parameter(self) -> Expression:
return self.parse_expression()
def parse_write_actual_parameter_list(self) -> List[WriteArg]:
args: List[WriteArg] = []
args.append(self.parse_write_actual_parameter())
while self.match('COMMA'):
args.append(self.parse_write_actual_parameter())
return args
def parse_write_actual_parameter(self) -> WriteArg:
expr = self.parse_expression()
width: Optional[Expression] = None
precision: Optional[Expression] = None
if self.match('COLON'):
width = self.parse_expression()
if self.match('COLON'):
precision = self.parse_expression()
return WriteArg(expr, width, precision)
def parse_if_statement(self) -> IfStmt:
self.expect('IF')
cond = self.parse_boolean_expression()
self.expect('THEN')
then_branch = self.parse_statement()
else_branch: Optional[Statement] = None
if self.match('ELSE'):
else_branch = self.parse_statement()
return IfStmt(cond, then_branch, else_branch)
def parse_for_statement(self) -> ForStmt:
self.expect('FOR')
static = self.match('STATIC')
var = self.expect('IDENTIFIER').lexeme
self.expect('ASSIGN')
start = self.parse_expression()
if self.current().kind in {'TO', 'DOWNTO'}:
direction = self.current().kind
self.pos += 1
else:
self.error('expected TO or DOWNTO')
end = self.parse_expression()
self.expect('DO')
body = self.parse_statement()
return ForStmt(var, start, end, direction, body, static)
def parse_repeat_statement(self) -> RepeatStmt:
self.expect('REPEAT')
stmts: List[Statement] = []
if self.current().kind != 'UNTIL':
stmts.append(self.parse_statement())
while self.match('SEMICOLON'):
if self.current().kind == 'UNTIL':
break
stmts.append(self.parse_statement())
self.expect('UNTIL')
cond = self.parse_boolean_expression()
return RepeatStmt(stmts, cond)
def parse_while_statement(self) -> WhileStmt:
self.expect('WHILE')
cond = self.parse_boolean_expression()
self.expect('DO')
body = self.parse_statement()
return WhileStmt(cond, body)
def parse_case_statement(self) -> CaseStmt:
self.expect('CASE')
expr = self.parse_expression()
self.expect('OF')
elements: List[CaseElement] = []
if self.current().kind != 'END':
elements.append(self.parse_case_element())
while self.match('SEMICOLON'):
if self.current().kind in {'OTHERWISE', 'END'}:
break
elements.append(self.parse_case_element())
otherwise: Optional[Statement] = None
if self.match('OTHERWISE'):
otherwise = self.parse_statement()
self.expect('END')
return CaseStmt(expr, elements, otherwise)
def parse_case_element(self) -> CaseElement:
constants = self.parse_case_constant_list()
self.expect('COLON')
stmt = self.parse_statement()
return CaseElement(constants, stmt)
def parse_case_constant_list(self) -> List[Expression]:
exprs: List[Expression] = []
exprs.append(self.parse_case_constant())
while self.match('COMMA'):
exprs.append(self.parse_case_constant())
return exprs
def parse_case_constant(self) -> Expression:
expr = self.parse_constant()
if self.match('RANGE'):
high = self.parse_constant()
return RangeExpr(expr, high)
return expr
def parse_with_statement(self) -> WithStmt:
self.expect('WITH')
targets: List[Designator] = []
targets.append(self.parse_with_target())
while self.match('COMMA'):
targets.append(self.parse_with_target())
self.expect('DO')
body = self.parse_statement()
return WithStmt(targets, body)
def parse_with_target(self) -> Designator:
name = self.expect('IDENTIFIER').lexeme
selectors: List[Selector] = []
while self.current().kind in {'LBRACKET', 'DOT', 'POINTER'}:
selectors.extend(self.parse_selector())
return Designator(name, selectors)
def parse_label_statement(self) -> LabelStmt:
label = self.parse_label_id()
self.expect('COLON')
stmt = self.parse_statement()
return LabelStmt(label, stmt)
def parse_selector(self) -> List[Selector]:
kind = self.current().kind
if kind == 'LBRACKET':
self.pos += 1
selectors: List[Selector] = [Selector('INDEX', self.parse_expression())]
while self.match('COMMA'):
selectors.append(Selector('INDEX', self.parse_expression()))
self.expect('RBRACKET')
return selectors
if kind == 'DOT':
self.pos += 1
field = self.expect('IDENTIFIER').lexeme
return [Selector('FIELD', field)]
if kind == 'POINTER':
self.pos += 1
return [Selector('DEREF', None)]
self.error('expected selector')
def parse_boolean_expression(self) -> Expression:
left = self.parse_expression()
while (self.current().kind == 'AND' and self.next_kind() == 'THEN') or \
(self.current().kind == 'OR' and self.next_kind() == 'ELSE'):
if self.current().kind == 'AND':
op = 'AND_THEN'
self.pos += 2
else:
op = 'OR_ELSE'
self.pos += 2
right = self.parse_expression()
left = BinOp(op, left, right)
return left
def parse_expression(self) -> Expression:
left = self.parse_simple_expression()
if self.current().kind in {'EQ', 'NEQ', 'LT', 'LE', 'GT', 'GE', 'IN'}:
op = self.current().kind
self.pos += 1
right = self.parse_simple_expression()
return BinOp(op, left, right)
return left
def parse_simple_expression(self) -> Expression:
sign: Optional[str] = None
if self.current().kind in {'PLUS', 'MINUS'}:
sign = self.current().kind
self.pos += 1
left = self.parse_term()
if sign == 'MINUS':
left = UnaryOp('MINUS', left)
while self.current().kind in {'PLUS', 'MINUS', 'OR', 'XOR'}:
if self.current().kind == 'OR' and self.next_kind() == 'ELSE':
break
op = self.current().kind
self.pos += 1
right = self.parse_term()
left = BinOp(op, left, right)
return left
def parse_term(self) -> Expression:
left = self.parse_factor()
while self.current().kind in {'MUL', 'SLASH', 'DIV', 'MOD', 'AND'}:
if self.current().kind == 'AND' and self.next_kind() == 'THEN':
break
op = self.current().kind
self.pos += 1
right = self.parse_factor()
left = BinOp(op, left, right)
return left
def parse_factor(self) -> Expression:
kind = self.current().kind
if kind == 'NOT':
self.pos += 1
operand = self.parse_factor()
return UnaryOp('NOT', operand)
if kind == 'IDENTIFIER':
name = self.current().lexeme
if name.upper() == 'RETYPE' and self.next_kind() == 'LPAREN':
self.pos += 2 # consume 'RETYPE' and '('
type_id = self.expect('IDENTIFIER').lexeme
self.expect('COMMA')
expr = self.parse_expression()
self.expect('RPAREN')
selectors = []
while self.current().kind in {'LBRACKET', 'DOT', 'POINTER'}:
selectors.extend(self.parse_selector())
return RetypeExpr(type_id, expr, selectors)
elif self.next_kind() == 'LPAREN':
self.pos += 1
self.pos += 1
args: List[Expression] = []
if self.current().kind != 'RPAREN':
args = self.parse_actual_parameter_list()
self.expect('RPAREN')
return FuncCall(name, args)
elif self.next_kind() == 'LBRACKET' and self.bracket_payload_contains_range(self.pos + 1):
self.pos += 1 # consume type identifier
self.expect('LBRACKET')
elements: List[Expression] = []
if self.current().kind != 'RBRACKET':
elements.append(self.parse_set_element())
while self.match('COMMA'):
elements.append(self.parse_set_element())
self.expect('RBRACKET')
return SetConstructor(elements, name)
else:
self.pos += 1 # consume IDENTIFIER
designator = self.parse_designator_rest(name)
return designator
if kind == 'INTEGER_LITERAL':
# The lexer already computed the integer value, handling decimal
# and radix (n#digits) forms uniformly.
value = self.current().value
self.pos += 1
return IntLiteral(value)
if kind == 'REAL_LITERAL':
value = float(self.current().lexeme)
self.pos += 1
return RealLiteral(value)
if kind == 'CHAR_LITERAL':
value = self.current().value
self.pos += 1
return CharLiteral(value)
if kind == 'STRING_LITERAL':
value = self.current().lexeme
self.pos += 1
return StringLiteral(value)
if kind == 'BOOLEAN_LITERAL':
value = self.current().lexeme.upper() == 'TRUE'
self.pos += 1
return BoolLiteral(value)
if kind == 'NIL':
self.pos += 1
return NilLiteral()
if kind == 'LPAREN':
self.pos += 1
expr = self.parse_expression()
self.expect('RPAREN')
return expr
if kind == 'ADR':
self.pos += 1
name = self.expect('IDENTIFIER').lexeme
return AdrExpr(name)
if kind == 'ADS':
self.pos += 1
name = self.expect('IDENTIFIER').lexeme
return AdsExpr(name)
if kind == 'SIZEOF':
self.pos += 1
self.expect('LPAREN')
target: Union[str, Type]
if self.current().kind == 'IDENTIFIER':
target = self.current().lexeme
self.pos += 1
else:
target = self.parse_type()
self.expect('RPAREN')
return SizeofExpr(target)
if kind == 'UPPER':
self.pos += 1
self.expect('LPAREN')
name = self.expect('IDENTIFIER').lexeme
self.expect('RPAREN')
return UpperExpr(name)
if kind == 'LOWER':
self.pos += 1
self.expect('LPAREN')
name = self.expect('IDENTIFIER').lexeme
self.expect('RPAREN')
return LowerExpr(name)
if kind == 'LBRACKET':
self.pos += 1
elements: List[Expression] = []
if self.current().kind != 'RBRACKET':
elements.append(self.parse_set_element())
while self.match('COMMA'):
elements.append(self.parse_set_element())
self.expect('RBRACKET')
return SetConstructor(elements)
self.error('expected factor')
def bracket_payload_contains_range(self, lbracket_pos: int) -> bool:
"""Return True when a bracketed IDENTIFIER[...] payload contains '..'.
This conservatively disambiguates typed set constants from array
indexing without symbol information in the parser.
"""
depth = 0
for i in range(lbracket_pos, len(self.tokens)):
kind = self.tokens[i].kind
if kind == 'LBRACKET':
depth += 1
elif kind == 'RBRACKET':
depth -= 1
if depth == 0:
return False
elif kind == 'RANGE' and depth == 1:
return True
return False
def parse_designator_rest(self, name: str) -> Expression:
"""Continue parsing a designator or return as identifier."""
selectors: List[Selector] = []
while self.current().kind in {'LBRACKET', 'DOT', 'POINTER'}:
selectors.extend(self.parse_selector())
if selectors:
return Designator(name, selectors)
else:
return Identifier(name)
def parse_designator(self) -> Designator:
name = self.expect('IDENTIFIER').lexeme
selectors: List[Selector] = []
while self.current().kind in {'LBRACKET', 'DOT', 'POINTER'}:
selectors.extend(self.parse_selector())
return Designator(name, selectors)
def parse_constant(self) -> Expression:
kind = self.current().kind
if kind == 'INTEGER_LITERAL':
value = self.current().value
self.pos += 1
return IntLiteral(value)
if kind == 'REAL_LITERAL':
value = float(self.current().lexeme)
self.pos += 1
return RealLiteral(value)
if kind == 'CHAR_LITERAL':
value = self.current().value
self.pos += 1
return CharLiteral(value)
if kind == 'STRING_LITERAL':
value = self.current().lexeme
self.pos += 1
return StringLiteral(value)
if kind == 'BOOLEAN_LITERAL':
value = self.current().lexeme.upper() == 'TRUE'
self.pos += 1
return BoolLiteral(value)
if kind == 'NIL':
self.pos += 1
return NilLiteral()
if kind == 'IDENTIFIER':
name = self.current().lexeme
self.pos += 1
# WRD(x) and BYWORD(hi,lo) may appear as constant expressions
# (manual p.6-5, p.11-8); parse them as FuncCall nodes so the
# constant-folder in codegen can evaluate them at compile time.
if name.upper() in {'WRD', 'BYWORD'} and self.current().kind == 'LPAREN':
self.pos += 1 # consume '('
args = [self.parse_constant()]
while self.current().kind == 'COMMA':
self.pos += 1
args.append(self.parse_constant())
self.expect('RPAREN')
return FuncCall(name=name, args=args)
return Identifier(name)
if kind in {'PLUS', 'MINUS'}:
sign = self.current().kind
self.pos += 1
if self.current().kind == 'INTEGER_LITERAL':
value = self.current().value
if sign == 'MINUS':
value = -value
self.pos += 1
return IntLiteral(value)
if self.current().kind == 'REAL_LITERAL':
value = float(self.current().lexeme)
if sign == 'MINUS':
value = -value
self.pos += 1
return RealLiteral(value)
self.error('expected numeric constant')
self.error('expected constant')
def parse_type(self) -> Type:
packed = self.match('PACKED')
kind = self.current().kind
if kind in {'ARRAY', 'SUPER'}:
is_super = kind == 'SUPER'
if is_super:
self.pos += 1
self.expect('ARRAY')
else:
self.pos += 1
self.expect('LBRACKET')
index_range = self.parse_index_range(allow_star=is_super)
self.expect('RBRACKET')
self.expect('OF')
element_type = self.parse_type()
return ArrayType(index_range, element_type, packed, is_super)
if kind == 'RECORD':
self.pos += 1
fields: List[tuple[List[str], Type]] = []
while self.current().kind != 'END':
names = self.parse_identifier_list()
self.expect('COLON')
field_type = self.parse_type()
fields.append((names, field_type))
if self.current().kind == 'SEMICOLON':
self.pos += 1
else:
break
self.expect('END')
return RecordType(fields, packed)
if kind == 'SET':
self.pos += 1
self.expect('OF')
base = self.parse_set_base()
return SetType(base)
if kind == 'FILE':
self.pos += 1
self.expect('OF')
element_type = self.parse_type()
return FileType(element_type)
if kind == 'LPAREN':
self.pos += 1
values = self.parse_identifier_list()
self.expect('RPAREN')
return EnumType(values)
if kind == 'LSTRING':
self.pos += 1
self.expect('LPAREN')
max_len_expr = self.parse_constant()
self.expect('RPAREN')
# Extract integer value from expression
if isinstance(max_len_expr, IntLiteral):
max_len = max_len_expr.value
else:
max_len = 256 # fallback
return LStringType(max_len)
if kind == 'POINTER':
self.pos += 1
base = self.parse_type()
return PointerType(base, 'POINTER')
if kind == 'ADR':
self.pos += 1
self.expect('OF')
base = self.parse_type()
return PointerType(base, 'ADR')
if kind == 'ADS':
self.pos += 1
self.expect('OF')
base = self.parse_type()
return PointerType(base, 'ADS')
if kind == 'IDENTIFIER':
name = self.current().lexeme
self.pos += 1
param: Optional[Union[int, str]] = None
if self.match('LPAREN'):
param_expr = self.parse_constant()
self.expect('RPAREN')
if isinstance(param_expr, IntLiteral):
param = param_expr.value
elif isinstance(param_expr, Identifier):
param = param_expr.name
return NamedType(name, param)
if kind in {'INTEGER', 'REAL', 'BOOLEAN', 'CHAR', 'WORD', 'ADRMEM'}:
name = self.current().kind
self.pos += 1
return BuiltinType(name)
self.error('expected type')
def parse_index_range(self, allow_star: bool = False) -> IndexRange:
low = self.parse_constant()
self.expect('RANGE')
high: Optional[Expression] = None
if allow_star:
self.expect('MUL')
high = None
else:
high = self.parse_constant()
return IndexRange(low, high)
def parse_set_base(self) -> Type:
"""Parse a set base type: a named/builtin ordinal type, or a subrange.
Subranges (`SET OF 1..10`, `SET OF 'A'..'Z'`, `SET OF lo..hi`) preserve
both bounds in a SubrangeType rather than collapsing to the host type,
so the declared range survives into the AST.
"""
# Named type, possibly the low end of a subrange (`color` or `red..blue`).
if self.current().kind == 'IDENTIFIER':
if self.next_kind() == 'RANGE':
low_expr = self.parse_constant()
self.expect('RANGE')
high_expr = self.parse_constant()
return SubrangeType(low_expr, high_expr, self._subrange_host(low_expr, high_expr))
name = self.current().lexeme
self.pos += 1
return NamedType(name, None)
# Literal, possibly the low end of a subrange (`1..10`, `'A'..'Z'`).
if self.current().kind in {'INTEGER_LITERAL', 'REAL_LITERAL', 'CHAR_LITERAL', 'STRING_LITERAL', 'BOOLEAN_LITERAL'}:
expr = self.parse_constant()
if self.match('RANGE'):
high_expr = self.parse_constant()
return SubrangeType(expr, high_expr, self._subrange_host(expr, high_expr))
# A bare literal as a set base is unusual; treat it as the host type.
host = self._subrange_host(expr, expr)
return BuiltinType(host if host else 'INTEGER')
if self.current().kind in {'INTEGER', 'REAL', 'BOOLEAN', 'CHAR', 'WORD', 'ADRMEM'}:
name = self.current().kind
self.pos += 1
return BuiltinType(name)
self.error('expected set base type or range')
@staticmethod
def _subrange_host(low: Expression, high: Expression) -> Optional[str]:
"""Best-effort host ordinal type for a subrange, inferred from its bound
literals. Returns None when the bounds are named constants/identifiers
whose type can't be known at parse time (resolved later by the type
checker)."""
for ordinal, literal in (('INTEGER', IntLiteral), ('CHAR', CharLiteral), ('BOOLEAN', BoolLiteral)):
if isinstance(low, literal) and isinstance(high, literal):
return ordinal
return None