Debian Codegraph @

gcc/m2/gm2-compiler/P0SyntaxCheck.bnf

raw
1 --
2 -- m2.bnf grammar and associated actions for pass 0.
3 --
4 -- Copyright (C) 2001-2025 Free Software Foundation, Inc.
5 -- Contributed by Gaius Mulley <[email protected]>.
6 --
7 -- This file is part of GNU Modula-2.
8 --
9 -- GNU Modula-2 is free software; you can redistribute it and/or modify
10 -- it under the terms of the GNU General Public License as published by
11 -- the Free Software Foundation; either version 3, or (at your option)
12 -- any later version.
13 --
14 -- GNU Modula-2 is distributed in the hope that it will be useful, but
15 -- WITHOUT ANY WARRANTY; without even the implied warranty of
16 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 -- General Public License for more details.
18 --
19 -- You should have received a copy of the GNU General Public License
20 -- along with GNU Modula-2; see the file COPYING3. If not see
21 -- <http://www.gnu.org/licenses/>.
22 % module P0SyntaxCheck begin
23 (* output from m2.bnf, automatically generated do not edit if these
24 are the top two lines in the file.
25
26 Copyright (C) 2001-2025 Free Software Foundation, Inc.
27 Contributed by Gaius Mulley <[email protected]>.
28
29 This file is part of GNU Modula-2.
30
31 GNU Modula-2 is free software; you can redistribute it and/or modify
32 it under the terms of the GNU General Public License as published by
33 the Free Software Foundation; either version 3, or (at your option)
34 any later version.
35
36 GNU Modula-2 is distributed in the hope that it will be useful, but
37 WITHOUT ANY WARRANTY; without even the implied warranty of
38 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 General Public License for more details.
40
41 You should have received a copy of the GNU General Public License
42 along with GNU Modula-2; see the file COPYING. If not,
43 see <https://www.gnu.org/licenses/>. *)
44
45 IMPLEMENTATION MODULE P0SyntaxCheck ;
46
47 FROM M2LexBuf IMPORT currentstring, currenttoken, GetToken, InsertToken,
48 InsertTokenAndRewind, GetTokenNo, DisplayToken, DumpTokens ;
49
50 FROM M2Quads IMPORT PushT, PushTF, IsAutoPushOn, PushAutoOff, PushAutoOn,
51 PopAuto, DisplayStack, PushTFtok, PushTtok, DupFrame,
52 Top ;
53
54 FROM M2Reserved IMPORT tokToTok, toktype, NulTok, ImportTok, ExportTok,
55 QualifiedTok, UnQualifiedTok, BuiltinTok, InlineTok ;
56
57 FROM M2MetaError IMPORT MetaErrorStringT0 ;
58 FROM P2SymBuild IMPORT BuildString, BuildNumber ;
59 FROM NameKey IMPORT Name, NulName, makekey ;
60 FROM StrLib IMPORT StrCopy, StrConCat, StrEqual ;
61 FROM M2Batch IMPORT MakeProgramSource, MakeDefinitionSource, MakeImplementationSource ;
62 FROM DynamicStrings IMPORT String, InitString, KillString, Mark, ConCat, ConCatChar ;
63 FROM M2Debug IMPORT Assert ;
64 FROM M2Printf IMPORT printf0 ;
65
66 (* imports for Pass0 *)
67
68 FROM P0SymBuild IMPORT RegisterImports, RegisterInnerImports,
69 RegisterProgramModule,
70 RegisterImplementationModule, RegisterDefinitionModule,
71 RegisterInnerModule, EndModule,
72 RegisterProcedure, EndProcedure, EndForward ;
73
74 FROM SymbolTable IMPORT NulSym, PutModuleContainsBuiltin, PutHiddenTypeDeclared ;
75
76 IMPORT M2Error ;
77
78
79 CONST
80 Debugging = FALSE ;
81 DebugRecover = FALSE ;
82 Pass0 = TRUE ;
83 Pass1 = FALSE ;
84 Pass2 = FALSE ; (* permanently disabled for the time being *)
85 Pass3 = FALSE ; (* permanently disabled for the time being *)
86 MaxInsert = 10 ; (* allow 10 tokens to be inserted before *)
87 (* giving up. *)
88
89 VAR
90 EnableImportCheck,
91 seenError : BOOLEAN ;
92 LastIdent : Name ;
93 InsertCount : CARDINAL ;
94
95
96 (*
97 BlockAssert - wrap an Assert specifically for blocks.
98 *)
99
100 PROCEDURE BlockAssert (value: BOOLEAN) ;
101 BEGIN
102 Assert (value) ;
103 END BlockAssert ;
104
105
106 PROCEDURE ErrorString (s: String) ;
107 BEGIN
108 MetaErrorStringT0 (GetTokenNo (), s) ;
109 seenError := TRUE
110 END ErrorString ;
111
112
113 PROCEDURE ErrorArray (a: ARRAY OF CHAR) ;
114 BEGIN
115 ErrorString (InitString (a))
116 END ErrorArray ;
117
118
119 % declaration P0SyntaxCheck begin
120
121 (*
122 SyntaxError - after a syntax error we skip all tokens up until we reach
123 a stop symbol.
124 *)
125
126 PROCEDURE SyntaxError (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
127 BEGIN
128 DescribeError ;
129 IF Debugging
130 THEN
131 printf0('\nskipping token *** ')
132 END ;
133 (* --fixme-- this assumes a 32 bit word size. *)
134 WHILE NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
135 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
136 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))
137 DO
138 GetToken
139 END ;
140 IF Debugging
141 THEN
142 printf0(' ***\n')
143 END
144 END SyntaxError ;
145
146
147 (*
148 SyntaxCheck -
149 *)
150
151 PROCEDURE SyntaxCheck (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
152 BEGIN
153 (* --fixme-- this assumes a 32 bit word size. *)
154 IF NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
155 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
156 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))
157 THEN
158 SyntaxError (stopset0, stopset1, stopset2)
159 END
160 END SyntaxCheck ;
161
162
163 (*
164 GetMissingTokenMessage - generates and returns a string about a missing token, t.
165 *)
166
167 PROCEDURE GetMissingTokenMessage (t: toktype) : String ;
168 VAR
169 s0 : SetOfStop0 ;
170 s1 : SetOfStop1 ;
171 s2 : SetOfStop2 ;
172 str: String ;
173 BEGIN
174 s0 := SetOfStop0{} ;
175 s1 := SetOfStop1{} ;
176 s2 := SetOfStop2{} ;
177 IF ORD(t)<32
178 THEN
179 s0 := SetOfStop0{t}
180 ELSIF ORD(t)<64
181 THEN
182 s1 := SetOfStop1{t}
183 ELSE
184 s2 := SetOfStop2{t}
185 END ;
186 str := DescribeStop (s0, s1, s2) ;
187 RETURN str
188 END GetMissingTokenMessage ;
189
190
191 (*
192 ErrorMissingToken - generates an error message about a missing token, t.
193 *)
194
195 PROCEDURE ErrorMissingToken (t: toktype) ;
196 VAR
197 str: String ;
198 BEGIN
199 str := GetMissingTokenMessage (t) ;
200 str := ConCat (InitString ('syntax error,'), Mark (str)) ;
201 MetaErrorStringT0 (GetTokenNo (), str)
202 END ErrorMissingToken ;
203
204
205 (*
206 WarnMissingToken - generates a warning message about a missing token, t.
207 *)
208
209 PROCEDURE WarnMissingToken (t: toktype) ;
210 VAR
211 str: String ;
212 BEGIN
213 str := GetMissingTokenMessage (t) ;
214 str := ConCat (InitString ('{%W}syntax warning,'), Mark (str)) ;
215 MetaErrorStringT0 (GetTokenNo (), str) ;
216 IF DebugRecover
217 THEN
218 printf0 ("warning note created\n")
219 END
220 END WarnMissingToken ;
221
222
223 (*
224 MissingToken - generates a warning message about a missing token, t.
225 *)
226
227 PROCEDURE MissingToken (t: toktype) ;
228 BEGIN
229 IF (InsertCount<MaxInsert) AND
230 ((t#identtok) AND (t#integertok) AND (t#realtok) AND (t#stringtok))
231 THEN
232 IF DebugRecover
233 THEN
234 printf0 ("missing token detected and going to be inserted: ");
235 DisplayToken (t)
236 END ;
237 WarnMissingToken (t) ;
238 INC (InsertCount) ;
239 IF DebugRecover
240 THEN
241 printf0 ('inserting token\n')
242 END ;
243 InsertToken (t)
244 ELSE
245 IF DebugRecover
246 THEN
247 printf0 ("missing token detected but cannot be inserted: ");
248 DisplayToken (t)
249 END ;
250 ErrorMissingToken (t)
251 END
252 END MissingToken ;
253
254
255 (*
256 CheckInsertCandidate -
257 *)
258
259 PROCEDURE CheckInsertCandidate (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) : BOOLEAN ;
260 BEGIN
261 IF ((ORD(t)<32) AND (t IN stopset0)) OR
262 ((ORD(t)>=32) AND (ORD(t)<64) AND (t IN stopset1)) OR
263 ((ORD(t)>=64) AND (t IN stopset2))
264 THEN
265 INC (InsertCount) ;
266 IF InsertCount < MaxInsert
267 THEN
268 WarnMissingToken (t) ;
269 IF DebugRecover
270 THEN
271 printf0 ('buffer before\n') ;
272 DumpTokens
273 END ;
274 InsertTokenAndRewind (t) ;
275 IF DebugRecover
276 THEN
277 printf0 ('inserting token: buffer after insertion\n') ;
278 DumpTokens
279 END ;
280 RETURN TRUE
281 END
282 END ;
283 RETURN FALSE
284 END CheckInsertCandidate ;
285
286
287 (*
288 InStopSet
289 *)
290
291 PROCEDURE InStopSet (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) : BOOLEAN ;
292 BEGIN
293 IF ((ORD(t)<32) AND (t IN stopset0)) OR
294 ((ORD(t)>=32) AND (ORD(t)<64) AND (t IN stopset1)) OR
295 ((ORD(t)>=64) AND (t IN stopset2))
296 THEN
297 RETURN( TRUE )
298 ELSE
299 RETURN( FALSE )
300 END
301 END InStopSet ;
302
303
304 (*
305 PeepToken - peep token checks to see whether the stopset is satisfied by currenttoken
306 If it is not then it will insert a token providing the token
307 is one of ; ] ) } . OF END ,
308
309 if the stopset contains <identtok> then we do not insert a token
310 *)
311
312 PROCEDURE PeepToken (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
313 BEGIN
314 (* and again (see above re: ORD)
315 *)
316 IF (NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
317 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
318 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))) AND
319 (NOT InStopSet(identtok, stopset0, stopset1, stopset2))
320 THEN
321 (* SyntaxCheck will fail since currentoken is not part of the stopset
322 we check to see whether one of the following is in the stopset and
323 if not emit a warning and also the token. *)
324 IF CheckInsertCandidate (semicolontok, stopset0, stopset1, stopset2) OR
325 CheckInsertCandidate (rsbratok, stopset0, stopset1, stopset2) OR
326 CheckInsertCandidate (rparatok, stopset0, stopset1, stopset2) OR
327 CheckInsertCandidate (rcbratok, stopset0, stopset1, stopset2) OR
328 CheckInsertCandidate (periodtok, stopset0, stopset1, stopset2) OR
329 CheckInsertCandidate (oftok, stopset0, stopset1, stopset2) OR
330 CheckInsertCandidate (endtok, stopset0, stopset1, stopset2) OR
331 CheckInsertCandidate (commatok, stopset0, stopset1, stopset2)
332 THEN
333 END
334 END
335 END PeepToken ;
336
337
338 (*
339 DetectImport - checks whether the next token is an import or from and if so
340 generates an error message. This is called after an export
341 statement to notify the user that the ordering is incorrect.
342 *)
343
344 PROCEDURE DetectImport ;
345 BEGIN
346 IF (currenttoken = importtok) OR (currenttoken = fromtok)
347 THEN
348 ErrorArray ('an {%AkIMPORT} statement must preceed an {%kEXPORT} statement')
349 END
350 END DetectImport ;
351
352
353 (*
354 Expect -
355 *)
356
357 PROCEDURE Expect (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
358 BEGIN
359 IF currenttoken=t
360 THEN
361 GetToken ;
362 IF EnableImportCheck
363 THEN
364 DetectImport
365 END ;
366 IF Pass0
367 THEN
368 PeepToken (stopset0, stopset1, stopset2)
369 END
370 ELSE
371 MissingToken (t)
372 END ;
373 SyntaxCheck (stopset0, stopset1, stopset2)
374 END Expect ;
375
376
377 (*
378 CompilationUnit - returns TRUE if the input was correct enough to parse
379 in future passes.
380 *)
381
382 PROCEDURE CompilationUnit () : BOOLEAN ;
383 BEGIN
384 seenError := FALSE ;
385 EnableImportCheck := FALSE ;
386 InsertCount := 0 ;
387 FileUnit (SetOfStop0{eoftok}, SetOfStop1{}, SetOfStop2{}) ;
388 RETURN NOT seenError
389 END CompilationUnit ;
390
391
392 (*
393 Ident - error checking varient of Ident
394 *)
395
396 PROCEDURE Ident (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
397 BEGIN
398 LastIdent := makekey (currentstring) ;
399 IF IsAutoPushOn ()
400 THEN
401 PushTFtok (LastIdent, identtok, GetTokenNo())
402 END ;
403 Expect (identtok, stopset0, stopset1, stopset2)
404 END Ident ;
405
406
407 (*
408 string -
409 *)
410
411 PROCEDURE string (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
412 BEGIN
413 IF IsAutoPushOn ()
414 THEN
415 PushTF (makekey (currentstring), stringtok) ;
416 BuildString
417 END ;
418 Expect (stringtok, stopset0, stopset1, stopset2)
419 END string ;
420
421
422 (*
423 Integer -
424 *)
425
426 PROCEDURE Integer (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
427 BEGIN
428 IF IsAutoPushOn()
429 THEN
430 PushTFtok (makekey (currentstring), integertok, GetTokenNo ()) ;
431 BuildNumber
432 END ;
433 Expect(integertok, stopset0, stopset1, stopset2)
434 END Integer ;
435
436
437 (*
438 Real -
439 *)
440
441 PROCEDURE Real (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
442 BEGIN
443 IF IsAutoPushOn()
444 THEN
445 PushTFtok (makekey (currentstring), realtok, GetTokenNo ()) ;
446 BuildNumber
447 END ;
448 Expect(realtok, stopset0, stopset1, stopset2)
449 END Real ;
450
451 % module P0SyntaxCheck end
452 END P0SyntaxCheck.
453 % rules
454 error 'ErrorArray' 'ErrorString'
455 tokenfunc 'currenttoken'
456
457 token '' eoftok -- internal token
458 token '+' plustok
459 token '-' minustok
460 token '*' timestok
461 token '/' dividetok
462 token ':=' becomestok
463 token '&' ambersandtok
464 token "." periodtok
465 token "," commatok
466 token ";" semicolontok
467 token '(' lparatok
468 token ')' rparatok
469 token '[' lsbratok -- left square brackets
470 token ']' rsbratok -- right square brackets
471 token '{' lcbratok -- left curly brackets
472 token '}' rcbratok -- right curly brackets
473 token '^' uparrowtok
474 token "'" singlequotetok
475 token '=' equaltok
476 token '#' hashtok
477 token '<' lesstok
478 token '>' greatertok
479 token '<>' lessgreatertok
480 token '<=' lessequaltok
481 token '>=' greaterequaltok
482 token '<*' ldirectivetok
483 token '*>' rdirectivetok
484 token '..' periodperiodtok
485 token ':' colontok
486 token '"' doublequotestok
487 token '|' bartok
488 token 'AND' andtok
489 token 'ARRAY' arraytok
490 token 'BEGIN' begintok
491 token 'BY' bytok
492 token 'CASE' casetok
493 token 'CONST' consttok
494 token 'DEFINITION' definitiontok
495 token 'DIV' divtok
496 token 'DO' dotok
497 token 'ELSE' elsetok
498 token 'ELSIF' elsiftok
499 token 'END' endtok
500 token 'EXCEPT' excepttok
501 token 'EXIT' exittok
502 token 'EXPORT' exporttok
503 token 'FINALLY' finallytok
504 token 'FOR' fortok
505 token 'FORWARD' forwardtok
506 token 'FROM' fromtok
507 token 'IF' iftok
508 token 'IMPLEMENTATION' implementationtok
509 token 'IMPORT' importtok
510 token 'IN' intok
511 token 'LOOP' looptok
512 token 'MOD' modtok
513 token 'MODULE' moduletok
514 token 'NOT' nottok
515 token 'OF' oftok
516 token 'OR' ortok
517 token 'PACKEDSET' packedsettok
518 token 'POINTER' pointertok
519 token 'PROCEDURE' proceduretok
520 token 'QUALIFIED' qualifiedtok
521 token 'UNQUALIFIED' unqualifiedtok
522 token 'RECORD' recordtok
523 token 'REM' remtok
524 token 'REPEAT' repeattok
525 token 'RETRY' retrytok
526 token 'RETURN' returntok
527 token 'SET' settok
528 token 'THEN' thentok
529 token 'TO' totok
530 token 'TYPE' typetok
531 token 'UNTIL' untiltok
532 token 'VAR' vartok
533 token 'WHILE' whiletok
534 token 'WITH' withtok
535 token 'ASM' asmtok
536 token 'VOLATILE' volatiletok
537 token '...' periodperiodperiodtok
538 token '__DATE__' datetok
539 token '__LINE__' linetok
540 token '__FILE__' filetok
541 token '__ATTRIBUTE__' attributetok
542 token '__BUILTIN__' builtintok
543 token '__INLINE__' inlinetok
544 token 'integer number' integertok
545 token 'identifier' identtok
546 token 'real number' realtok
547 token 'string' stringtok
548
549 special Ident first { < identtok > } follow { }
550 '@i{is a builtin and checks for an identifier}'
551 special Integer first { < integertok > } follow { }
552 '@i{is a builtin and checks for an integer}'
553 special Real first { < realtok > } follow { }
554 '@i{is a builtin and checks for an real constant}'
555 special string first { < stringtok > } follow { }
556 '@i{is a builtin and checks for an string constant}'
557 BNF
558
559 -- the following are provided by the module m2flex and also hand built procedures below
560 -- Ident := Letter { ( Letter | Digit ) } =:
561 -- Integer := Digit { Digit } | OctalDigit { OctalDigit } ( " B " | " C " ) |
562 -- Digit { HexDigit } " H " =:
563 -- Real := Digit { Digit } " . " { Digit } [ ScaleFactor ] =:
564 -- ScaleFactor := " E " [ ( " + " | " - " ) ] Digit { Digit } =:
565 -- HexDigit := Digit | " A " | " B " | " C " | " D " | " E " | " F " =:
566 -- Digit := OctalDigit | " 8 " | " 9 " =:
567 -- OctalDigit := "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" =:
568 -- String
569
570 FileUnit := % PushAutoOff %
571 ( DefinitionModule | ImplementationOrProgramModule ) % PopAuto %
572 =:
573
574 ProgramModule := "MODULE" % PushAutoOn ; %
575 % M2Error.DefaultProgramModule %
576 Ident % RegisterProgramModule ; %
577 % PushAutoOff ; %
578 [ Priority ]
579 ";"
580 % PushAutoOn ; %
581 { Import % RegisterImports %
582 } % PopAuto %
583 Block % PopAuto %
584 Ident "." % EndModule %
585 % PopAuto %
586 =:
587
588 ImplementationModule := "IMPLEMENTATION" % M2Error.DefaultImplementationModule %
589 "MODULE" % PushAutoOn ; %
590 Ident % RegisterImplementationModule ; %
591 % PushAutoOff ; %
592 [ Priority ] ";" % PushAutoOn ; %
593 { Import % RegisterImports %
594 } % PopAuto %
595 Block % PopAuto %
596
597 Ident % EndModule %
598 % PopAuto %
599 "." =:
600
601 ImplementationOrProgramModule := ImplementationModule | ProgramModule =:
602
603 Number := Integer | Real =:
604
605 Qualident := Ident { "." Ident } =:
606
607 ConstantDeclaration := Ident "=" ConstExpression =:
608
609 ConstExpression := SimpleConstExpr [ Relation SimpleConstExpr ] =:
610
611 Relation := "=" | "#" | "<>" | "<" | "<=" | ">" | ">=" | "IN" =:
612
613 SimpleConstExpr := UnaryOrConstTerm { AddOperator ConstTerm } =:
614
615 UnaryOrConstTerm := "+" ConstTerm | "-" ConstTerm | ConstTerm =:
616
617 AddOperator := "+" | "-" | "OR" =:
618
619 ConstTerm := ConstFactor { MulOperator ConstFactor } =:
620
621 MulOperator := "*" | "/" | "DIV" | "MOD" | "REM" | "AND" | "&" =:
622
623 ConstFactor := Number | ConstString | ConstSetOrQualidentOrFunction |
624 "(" ConstExpression ")" | "NOT" ConstFactor |
625 ConstAttribute =:
626
627 -- to help satisfy LL1
628
629 ConstString := string =:
630
631 ComponentElement := ConstExpression [ ".." ConstExpression ] =:
632
633 ComponentValue := ComponentElement [ 'BY' ConstExpression ] =:
634
635 ArraySetRecordValue := ComponentValue { ',' ComponentValue } =:
636
637 Constructor := '{' [ ArraySetRecordValue ] '}' =:
638
639 ConstSetOrQualidentOrFunction := Constructor | Qualident
640 [ Constructor | ConstActualParameters ] =:
641
642 ConstActualParameters := "(" [ ExpList ] ")" =:
643
644 ConstAttribute := "__ATTRIBUTE__" "__BUILTIN__" "(" "(" ConstAttributeExpression ")" ")" =:
645
646 ConstAttributeExpression := Ident | "<" Qualident ',' Ident ">" =:
647
648 ByteAlignment := '<*' AttributeExpression '*>' =:
649
650 -- AlignmentExpression := "(" ConstExpression ")" =:
651
652 Alignment := [ ByteAlignment ] =:
653
654 TypeDeclaration := Ident "=" Type Alignment =:
655
656 Type := SimpleType | ArrayType | RecordType | SetType |
657 PointerType | ProcedureType =:
658
659 SimpleType := Qualident [ SubrangeType ] | Enumeration | SubrangeType =:
660
661 Enumeration := "(" IdentList ")" =:
662
663 IdentList := Ident % VAR
664 on: BOOLEAN ;
665 n : CARDINAL ; %
666 % on := IsAutoPushOn() ;
667 IF on
668 THEN
669 n := 1
670 END %
671 { "," Ident % IF on
672 THEN
673 INC(n)
674 END %
675 } % IF on
676 THEN
677 PushT(n)
678 END %
679 =:
680
681 SubrangeType := "[" ConstExpression ".." ConstExpression "]" =:
682
683 ArrayType := "ARRAY" SimpleType { "," SimpleType } "OF" Type =:
684
685 RecordType := "RECORD" [ DefaultRecordAttributes ] FieldListSequence "END" =:
686
687 DefaultRecordAttributes := '<*' AttributeExpression '*>' =:
688
689 RecordFieldPragma := [ '<*' FieldPragmaExpression
690 { ',' FieldPragmaExpression } '*>' ] =:
691
692 FieldPragmaExpression := Ident [ '(' ConstExpression ')' ] =:
693
694 AttributeExpression := Ident '(' ConstExpression ')' =:
695
696 FieldListSequence := FieldListStatement { ";" FieldListStatement } =:
697
698 FieldListStatement := [ FieldList ] =:
699
700 FieldList := IdentList ":" Type RecordFieldPragma
701 |
702 "CASE" CaseTag "OF" Varient { "|" Varient }
703 [ "ELSE" FieldListSequence ] "END"
704 =:
705
706 TagIdent := [ Ident ] =:
707
708 CaseTag := TagIdent [ ":" Qualident ] =:
709
710 Varient := [ VarientCaseLabelList ":" FieldListSequence ] =:
711
712 VarientCaseLabelList := VarientCaseLabels { "," VarientCaseLabels } =:
713
714 VarientCaseLabels := ConstExpression [ ".." ConstExpression ] =:
715
716 CaseLabelList := CaseLabels { "," CaseLabels } =:
717
718 CaseLabels := ConstExpression [ ".." ConstExpression ] =:
719
720 SetType := ( "SET" | "PACKEDSET" ) "OF" SimpleType =:
721
722 PointerType := "POINTER" "TO" Type =:
723
724 ProcedureType := "PROCEDURE" [ FormalTypeList ] =:
725
726 FormalTypeList := "(" ( ")" FormalReturn |
727 ProcedureParameters ")" FormalReturn ) =:
728
729 FormalReturn := [ ":" OptReturnType ] =:
730
731 OptReturnType := "[" Qualident "]" | Qualident =:
732
733 ProcedureParameters := ProcedureParameter
734 { "," ProcedureParameter } =:
735
736 ProcedureParameter := "..." | "VAR" FormalType | FormalType =:
737
738 VarIdent := Ident [ "[" ConstExpression "]" ]
739 =:
740
741 VariableDeclaration := VarIdentList ":" Type Alignment =:
742
743 VarIdentList := VarIdent % VAR
744 on: BOOLEAN ;
745 n : CARDINAL ; %
746 % on := IsAutoPushOn() ;
747 IF on
748 THEN
749 n := 1
750 END %
751 { "," VarIdent % IF on
752 THEN
753 INC(n)
754 END %
755 } % IF on
756 THEN
757 PushT(n)
758 END %
759 =:
760
761 Designator := Qualident { SubDesignator } =:
762
763 SubDesignator := "." Ident | "[" ExpList "]" | "^" =:
764
765 ExpList := Expression { "," Expression } =:
766
767 Expression := SimpleExpression [ Relation SimpleExpression ] =:
768
769 SimpleExpression := [ "+" | "-" ] Term { AddOperator Term } =:
770
771 Term := Factor { MulOperator Factor } =:
772
773 Factor := Number | string | SetOrDesignatorOrFunction |
774 "(" Expression ")" | "NOT" Factor | ConstAttribute =:
775
776 SetOrDesignatorOrFunction := ( Qualident [ Constructor |
777 SimpleDes [ ActualParameters ]
778 ] | Constructor
779 )
780 =:
781
782 SimpleDes := { "." Ident | "[" ExpList "]" | "^" } =:
783
784 ActualParameters := "(" [ ExpList ] ")" =:
785
786 Statement := [ AssignmentOrProcedureCall | IfStatement | CaseStatement |
787 WhileStatement | RepeatStatement | LoopStatement |
788 ForStatement | WithStatement | AsmStatement |
789 "EXIT" | "RETURN" [ Expression ] | RetryStatement ] =:
790
791 RetryStatement := "RETRY" =:
792
793 AssignmentOrProcedureCall := Designator ( ":=" Expression |
794 ActualParameters | % (* epsilon *) %
795 ) =:
796
797 -- these two break LL1 as both start with a Designator
798 -- ProcedureCall := Designator [ ActualParameters ] =:
799 -- Assignment := Designator ":=" Expression =:
800
801 StatementSequence := Statement { ";" Statement } =:
802
803 IfStatement := "IF" Expression "THEN" StatementSequence
804 { "ELSIF" Expression "THEN" StatementSequence }
805 [ "ELSE" StatementSequence ] "END" =:
806
807 CaseStatement := "CASE" Expression "OF" Case { "|" Case }
808 [ "ELSE" StatementSequence ] "END" =:
809
810 Case := [ CaseLabelList ":" StatementSequence ] =:
811
812 WhileStatement := "WHILE" Expression "DO" StatementSequence "END" =:
813
814 RepeatStatement := "REPEAT" StatementSequence "UNTIL" Expression =:
815
816 ForStatement := "FOR" Ident ":=" Expression "TO" Expression
817 [ "BY" ConstExpression ] "DO"
818 StatementSequence "END" =:
819
820 LoopStatement := "LOOP" StatementSequence "END" =:
821
822 WithStatement := "WITH" Designator "DO" StatementSequence "END" =:
823
824 ProcedureDeclaration := % VAR top: CARDINAL ; %
825 % top := Top () %
826 ProcedureHeading ";" PostProcedureHeading % BlockAssert (top = Top ()) %
827 =:
828
829 PostProcedureHeading := ProperProcedure | ForwardDeclaration =:
830
831 ForwardDeclaration := "FORWARD" % EndForward %
832 =:
833
834 ProperProcedure := ProcedureBlock % PushAutoOn %
835 Ident % EndProcedure %
836 % PopAuto %
837 =:
838
839 DefineBuiltinProcedure := [ "__ATTRIBUTE__" "__BUILTIN__" "(" "(" Ident ")" ")" |
840 "__INLINE__" ] =:
841
842 ProcedureHeading := "PROCEDURE" % M2Error.DefaultProcedure %
843 DefineBuiltinProcedure
844 ( % PushAutoOn %
845 Ident % RegisterProcedure %
846 % PopAuto %
847 [ FormalParameters ] AttributeNoReturn ) =:
848
849 AttributeNoReturn := [ "<*" Ident "*>" ] =:
850
851 AttributeUnused := [ "<*" Ident "*>" ] =:
852
853 -- note that we do need to know whether builtins are used as they
854 -- determine whether we need to parse the implementation module
855 -- the same is true for hidden types
856
857 Builtin := [ "__BUILTIN__" % PutModuleContainsBuiltin %
858 | "__INLINE__" ] =:
859
860 DefProcedureHeading := "PROCEDURE" % M2Error.DefaultProcedure %
861 Builtin
862 ( Ident [ DefFormalParameters ] AttributeNoReturn )
863 % M2Error.LeaveErrorScope %
864 =:
865
866 -- introduced procedure block so we can produce more informative
867 -- error messages
868
869 ProcedureBlock := { Declaration } [ "BEGIN" BlockBody ] "END" =:
870
871 Block := { Declaration } InitialBlock FinalBlock "END" =:
872
873 InitialBlock := [ "BEGIN" BlockBody ] =:
874
875 FinalBlock := [ "FINALLY" BlockBody ] =:
876
877 BlockBody := NormalPart [ "EXCEPT" ExceptionalPart ] =:
878
879 NormalPart := StatementSequence =:
880
881 ExceptionalPart := StatementSequence =:
882
883 Declaration := "CONST" { ConstantDeclaration ";" } |
884 "TYPE" { TypeDeclaration ";" } |
885 "VAR" { VariableDeclaration ";" } |
886 ProcedureDeclaration ";" |
887 ModuleDeclaration ";" =:
888
889 DefFormalParameters := "(" [ DefMultiFPSection ] ")" FormalReturn =:
890
891 DefMultiFPSection := DefExtendedFP |
892 FPSection [ ";" DefMultiFPSection ] =:
893
894 FormalParameters := "(" [ MultiFPSection ] ")" FormalReturn =:
895
896 MultiFPSection := ExtendedFP |
897 FPSection [ ";" MultiFPSection ] =:
898
899 FPSection := NonVarFPSection | VarFPSection =:
900
901 DefExtendedFP := DefOptArg | "..." =:
902
903 ExtendedFP := OptArg | "..." =:
904
905 VarFPSection := "VAR" IdentList ":" FormalType [ AttributeUnused ] =:
906
907 NonVarFPSection := IdentList ":" FormalType [ AttributeUnused ] =:
908
909 OptArg := "[" Ident ":" FormalType [ "=" ConstExpression ] "]" =:
910
911 DefOptArg := "[" Ident ":" FormalType "=" ConstExpression "]" =:
912
913 FormalType := { "ARRAY" "OF" } Qualident =:
914
915 ModuleDeclaration := "MODULE" % PushAutoOn %
916 % M2Error.DefaultInnerModule %
917 Ident % RegisterInnerModule %
918 % PushAutoOff %
919 [ Priority ] ";" % PushAutoOn %
920 { Import % RegisterInnerImports %
921 } % PopAuto %
922 [ Export ] Block % PopAuto %
923 Ident % EndModule %
924 % PopAuto %
925 =:
926
927 Priority := "[" ConstExpression "]" =:
928
929 Export := "EXPORT" ( "QUALIFIED" IdentList |
930 "UNQUALIFIED" IdentList |
931 IdentList
932 ) % EnableImportCheck := TRUE %
933 ";" % EnableImportCheck := FALSE %
934 =:
935
936 Import := "FROM" Ident "IMPORT" IdentList ";" |
937 "IMPORT" % PushTtok (ImportTok, GetTokenNo () -1)
938 (* determines whether Ident or Module *) %
939 IdentList ";" =:
940
941 DefinitionModule := % VAR forC: BOOLEAN ; %
942 % forC := FALSE %
943 "DEFINITION" % M2Error.DefaultDefinitionModule %
944 "MODULE" [ "FOR" string % forC := TRUE %
945 ] % PushAutoOn %
946 Ident % RegisterDefinitionModule (forC) %
947 ";"
948 { Import % RegisterImports %
949 } % PushAutoOff %
950 [ Export
951 ]
952 { Definition } % PopAuto %
953 "END" Ident % EndModule %
954 "." % PopAuto %
955 =:
956
957 Definition := "CONST" { ConstantDeclaration ";" } |
958 "TYPE" { Ident
959 ( ";" % PutHiddenTypeDeclared %
960 | "=" Type Alignment ";" ) }
961 |
962 "VAR" { VariableDeclaration ";" } |
963 DefProcedureHeading ";" =:
964
965 AsmStatement := 'ASM' [ 'VOLATILE' ] '(' AsmOperands ')' =:
966
967 NamedOperand := '[' Ident ']' =:
968
969 AsmOperandName := [ NamedOperand ] =:
970
971 AsmOperands := ConstExpression [ ':' AsmList [ ':' AsmList [ ':' TrashList ] ] ]
972 =:
973
974 AsmList := [ AsmElement ] { ',' AsmElement } =:
975
976 AsmElement := AsmOperandName ConstExpression '(' Expression ')'
977 =:
978
979 TrashList := [ ConstExpression ] { ',' ConstExpression } =:
980
981 FNB