Debian Codegraph @

gcc/m2/gm2-compiler/P2Build.bnf

raw
1 --
2 -- m2-2.bnf grammar and associated actions for pass 2.
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 P2Build begin
23 (* output from m2-2.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
46 IMPLEMENTATION MODULE P2Build ;
47
48 FROM M2LexBuf IMPORT currentstring, currenttoken, GetToken, InsertToken,
49 InsertTokenAndRewind, GetTokenNo, MakeVirtual2Tok,
50 MakeVirtualTok ;
51
52 FROM M2MetaError IMPORT MetaErrorStringT0, MetaErrorT1 ;
53 FROM NameKey IMPORT NulName, Name, makekey, MakeKey ;
54 FROM M2Reserved IMPORT tokToTok, toktype, NulTok, ImportTok, ExportTok, QualifiedTok, UnQualifiedTok ;
55 FROM DynamicStrings IMPORT String, InitString, KillString, Mark, ConCat, ConCatChar ;
56 FROM M2Printf IMPORT printf0 ;
57 FROM M2Debug IMPORT Assert ;
58
59 FROM M2Quads IMPORT PushT, PopT, PushTF, PopTF, PopNothing, OperandT, PushTFA, Top, Annotate,
60 OperandTok,
61 PushTFtok, PopTFtok, PushTFAtok, PopTtok, PushTtok,
62 StartBuildInit,
63 EndBuildInit,
64 BuildProcedureStart,
65 BuildProcedureEnd,
66 BuildAssignment,
67 AddRecordToList, AddVarientToList,
68 IsAutoPushOn, PushAutoOff, PushAutoOn, PopAuto, DisplayStack ;
69
70 FROM P2SymBuild IMPORT P2StartBuildProgramModule,
71 P2EndBuildProgramModule,
72 P2StartBuildDefModule,
73 P2EndBuildDefModule,
74 P2StartBuildImplementationModule,
75 P2EndBuildImplementationModule,
76 StartBuildInnerModule,
77 EndBuildInnerModule,
78
79 BuildImportOuterModule,
80 BuildImportInnerModule,
81 BuildExportOuterModule,
82 BuildExportInnerModule,
83
84 BlockStart, BlockEnd, BlockBegin, BlockFinally,
85 BuildString, BuildNumber,
86 BuildConst,
87 BuildVariable,
88 BuildTypeEnd,
89 BuildNulName,
90 BuildType,
91 StartBuildEnumeration,
92
93 StartBuildFormalParameters,
94 EndBuildFormalParameters,
95 BuildFPSection,
96 BuildVarArgs,
97 BuildOptArg,
98 BuildFormalVarArgs,
99 BuildProcedureHeading,
100 StartBuildProcedure,
101 EndBuildProcedure,
102 BuildFunction, BuildOptFunction,
103 BuildNoReturnAttribute,
104 EndBuildForward,
105
106 BuildPointerType,
107 BuildRecord, BuildFieldRecord,
108 StartBuildVarient, EndBuildVarient,
109 BuildVarientSelector,
110 StartBuildVarientFieldRecord,
111 EndBuildVarientFieldRecord,
112 StartBuildArray,
113 EndBuildArray,
114 BuildFieldArray, BuildArrayComma,
115 BuildSubrange, BuildAligned,
116 BuildTypeAlignment, BuildVarAlignment,
117 P2BuildDefaultFieldAlignment, BuildPragmaConst,
118 BuildSetType,
119 BuildFormalType, BuildFunction, BuildProcedureType,
120 DetermineType, PushType, PopType,
121 SeenUnknown, SeenSet, SeenString, SeenArray, SeenConstructor,
122 SeenCast,
123 PushRememberConstant, PopRememberConstant,
124 CheckProcedure ;
125
126 FROM M2Reserved IMPORT ArrayTok, VarTok ;
127
128 FROM SymbolTable IMPORT MakeGnuAsm, PutGnuAsmVolatile, PutGnuAsm, PutGnuAsmInput,
129 PutGnuAsmOutput, PutGnuAsmTrash, PutGnuAsmVolatile,
130 MakeRegInterface,
131 PutRegInterface, GetRegInterface,
132 GetSymName, GetType, MakeConstLit, IsProcType,
133 NulSym,
134 StartScope, EndScope, GetCurrentModule,
135 PutIncluded,
136 PutExceptionFinally, PutExceptionBlock, GetCurrentScope,
137 IsVarParam, IsProcedure, IsDefImp, IsModule,
138 IsRecord, IsAModula2Type, IsImported,
139 RequestSym ;
140
141 IMPORT M2Error ;
142
143
144 CONST
145 Debugging = FALSE ;
146 Pass1 = FALSE ; (* permanently disabled for the time being *)
147 Pass2 = TRUE ;
148 Pass3 = FALSE ; (* permanently disabled for the time being *)
149
150 VAR
151 WasNoError: BOOLEAN ;
152
153
154 PROCEDURE ErrorString (s: String) ;
155 BEGIN
156 MetaErrorStringT0 (GetTokenNo (), s) ;
157 WasNoError := FALSE
158 END ErrorString ;
159
160
161 PROCEDURE ErrorArray (a: ARRAY OF CHAR) ;
162 BEGIN
163 ErrorString (InitString (a))
164 END ErrorArray ;
165
166
167 % declaration P2Build begin
168
169 (*
170 checkReturnAttribute -
171 *)
172
173 PROCEDURE checkReturnAttribute ;
174 VAR
175 ident: Name ;
176 tok : CARDINAL ;
177 BEGIN
178 PopTtok (ident, tok) ;
179 IF ident # MakeKey ('noreturn')
180 THEN
181 MetaErrorT1 (tok, 'attribute {%1k} is not allowed in the procedure return type, only noreturn is allowed', ident)
182 END
183 END checkReturnAttribute ;
184
185
186 (*
187 checkParameterAttribute -
188 *)
189
190 PROCEDURE checkParameterAttribute ;
191 VAR
192 ident: Name ;
193 tok : CARDINAL ;
194 BEGIN
195 PopTtok (ident, tok) ;
196 IF ident # MakeKey ('unused')
197 THEN
198 MetaErrorT1 (tok, 'attribute {%1k} is not allowed in the parameter formal type section, only unused is allowed', ident)
199 END
200 END checkParameterAttribute ;
201
202
203 (*
204 SyntaxError - after a syntax error we skip all tokens up until we reach
205 a stop symbol.
206 *)
207
208 PROCEDURE SyntaxError (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
209 BEGIN
210 DescribeError ;
211 IF Debugging
212 THEN
213 printf0('\nskipping token *** ')
214 END ;
215 (* --fixme-- this assumes a 32 bit word size. *)
216 WHILE NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
217 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
218 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))
219 DO
220 GetToken
221 END ;
222 IF Debugging
223 THEN
224 printf0(' ***\n')
225 END
226 END SyntaxError ;
227
228
229 (*
230 SyntaxCheck -
231 *)
232
233 PROCEDURE SyntaxCheck (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
234 BEGIN
235 (* --fixme-- this assumes a 32 bit word size. *)
236 IF NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
237 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
238 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))
239 THEN
240 SyntaxError(stopset0, stopset1, stopset2)
241 END
242 END SyntaxCheck ;
243
244
245 (*
246 WarnMissingToken - generates a warning message about a missing token, t.
247 *)
248
249 PROCEDURE WarnMissingToken (t: toktype) ;
250 VAR
251 s0 : SetOfStop0 ;
252 s1 : SetOfStop1 ;
253 s2 : SetOfStop2 ;
254 str: String ;
255 BEGIN
256 s0 := SetOfStop0{} ;
257 s1 := SetOfStop1{} ;
258 s2 := SetOfStop2{} ;
259 IF ORD (t) <32
260 THEN
261 INCL (s0, t)
262 ELSIF ORD (t) <64
263 THEN
264 INCL (s1, t)
265 ELSE
266 INCL (s2, t)
267 END ;
268 str := DescribeStop (s0, s1, s2) ;
269
270 str := ConCat (InitString ('syntax error,'), Mark (str)) ;
271 MetaErrorStringT0 (GetTokenNo (), str)
272 END WarnMissingToken ;
273
274
275 (*
276 MissingToken - generates a warning message about a missing token, t.
277 *)
278
279 PROCEDURE MissingToken (t: toktype) ;
280 BEGIN
281 WarnMissingToken(t) ;
282 IF (t#identtok) AND (t#integertok) AND (t#realtok) AND (t#stringtok)
283 THEN
284 IF Debugging
285 THEN
286 printf0('inserting token\n')
287 END ;
288 InsertToken(t)
289 END
290 END MissingToken ;
291
292
293 (*
294 CheckAndInsert -
295 *)
296
297 PROCEDURE CheckAndInsert (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) : BOOLEAN ;
298 BEGIN
299 IF ((ORD(t)<32) AND (t IN stopset0)) OR
300 ((ORD(t)>=32) AND (ORD(t)<64) AND (t IN stopset1)) OR
301 ((ORD(t)>=64) AND (t IN stopset2))
302 THEN
303 WarnMissingToken(t) ;
304 InsertTokenAndRewind(t) ;
305 RETURN( TRUE )
306 ELSE
307 RETURN( FALSE )
308 END
309 END CheckAndInsert ;
310
311
312 (*
313 InStopSet
314 *)
315
316 PROCEDURE InStopSet (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) : BOOLEAN ;
317 BEGIN
318 IF ((ORD(t)<32) AND (t IN stopset0)) OR
319 ((ORD(t)>=32) AND (ORD(t)<64) AND (t IN stopset1)) OR
320 ((ORD(t)>=64) AND (t IN stopset2))
321 THEN
322 RETURN( TRUE )
323 ELSE
324 RETURN( FALSE )
325 END
326 END InStopSet ;
327
328
329 (*
330 PeepToken - peep token checks to see whether the stopset is satisfied by currenttoken
331 If it is not then it will insert a token providing the token
332 is one of ; ] ) } . OF END ,
333
334 if the stopset contains <identtok> then we do not insert a token
335 *)
336
337 PROCEDURE PeepToken (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
338 BEGIN
339 (* and again (see above re: ORD)
340 *)
341 IF (NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
342 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
343 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))) AND
344 (NOT InStopSet(identtok, stopset0, stopset1, stopset2))
345 THEN
346 (* SyntaxCheck would fail since currentoken is not part of the stopset
347 we check to see whether any of currenttoken might be a commonly omitted token *)
348 IF CheckAndInsert(semicolontok, stopset0, stopset1, stopset2) OR
349 CheckAndInsert(rsbratok, stopset0, stopset1, stopset2) OR
350 CheckAndInsert(rparatok, stopset0, stopset1, stopset2) OR
351 CheckAndInsert(rcbratok, stopset0, stopset1, stopset2) OR
352 CheckAndInsert(periodtok, stopset0, stopset1, stopset2) OR
353 CheckAndInsert(oftok, stopset0, stopset1, stopset2) OR
354 CheckAndInsert(endtok, stopset0, stopset1, stopset2) OR
355 CheckAndInsert(commatok, stopset0, stopset1, stopset2)
356 THEN
357 END
358 END
359 END PeepToken ;
360
361
362 (*
363 Expect -
364 *)
365
366 PROCEDURE Expect (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
367 BEGIN
368 IF currenttoken=t
369 THEN
370 GetToken ;
371 IF Pass1
372 THEN
373 PeepToken(stopset0, stopset1, stopset2)
374 END
375 ELSE
376 MissingToken(t)
377 END ;
378 SyntaxCheck(stopset0, stopset1, stopset2)
379 END Expect ;
380
381
382 (*
383 CompilationUnit - returns TRUE if the input was correct enough to parse
384 in future passes.
385 *)
386
387 PROCEDURE CompilationUnit () : BOOLEAN ;
388 BEGIN
389 WasNoError := TRUE ;
390 FileUnit(SetOfStop0{eoftok}, SetOfStop1{}, SetOfStop2{}) ;
391 RETURN( WasNoError )
392 END CompilationUnit ;
393
394
395 (*
396 Ident - error checking varient of Ident
397 *)
398
399 PROCEDURE Ident (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
400 BEGIN
401 IF IsAutoPushOn()
402 THEN
403 PushTFtok(makekey(currentstring), identtok, GetTokenNo()) ;
404 Annotate("%1n|identtok||Ident rule")
405 END ;
406 Expect(identtok, stopset0, stopset1, stopset2)
407 END Ident ;
408
409
410 (*
411 string -
412 *)
413
414 PROCEDURE string (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
415 BEGIN
416 IF IsAutoPushOn()
417 THEN
418 PushTF(makekey(currentstring), stringtok) ;
419 BuildString
420 END ;
421 Expect(stringtok, stopset0, stopset1, stopset2)
422 END string ;
423
424
425 (*
426 Integer -
427 *)
428
429 PROCEDURE Integer (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
430 BEGIN
431 IF IsAutoPushOn()
432 THEN
433 PushTFtok (makekey(currentstring), integertok, GetTokenNo ()) ;
434 BuildNumber
435 END ;
436 Expect(integertok, stopset0, stopset1, stopset2)
437 END Integer ;
438
439
440 (*
441 Real -
442 *)
443
444 PROCEDURE Real (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
445 BEGIN
446 IF IsAutoPushOn()
447 THEN
448 PushTFtok (makekey(currentstring), realtok, GetTokenNo ()) ;
449 BuildNumber
450 END ;
451 Expect(realtok, stopset0, stopset1, stopset2)
452 END Real ;
453
454
455 (*
456 CheckModuleQualident - check to see if the beginning ident of the qualident is an
457 imported module.
458 *)
459
460 PROCEDURE CheckModuleQualident (stopset0: SetOfStop0;
461 stopset1: SetOfStop1;
462 stopset2: SetOfStop2) ;
463 VAR
464 name : Name ;
465 init,
466 nextLevel,
467 tok, tokstart: CARDINAL ;
468 BEGIN
469 PopTtok (name, tokstart) ;
470 tok := tokstart ;
471 init := RequestSym (tok, name) ;
472 IF IsImported (GetCurrentModule (), init) AND (IsDefImp (init) OR IsModule (init))
473 THEN
474 WHILE IsDefImp (init) OR IsModule (init) DO
475 Expect (periodtok, stopset0, stopset1, stopset2 + SetOfStop2{identtok}) ;
476 StartScope (init) ;
477 Ident (stopset0, stopset1, stopset2) ;
478 PopTtok (name, tok) ;
479 nextLevel := RequestSym (tok, name) ;
480 EndScope ;
481 init := nextLevel
482 END ;
483 IF tok#tokstart
484 THEN
485 tok := MakeVirtualTok (tokstart, tokstart, tok)
486 END ;
487 IF IsProcedure (init) OR IsProcType (init)
488 THEN
489 PushTtok (init, tok)
490 ELSE
491 Annotate ("%1s(%1d)|%1s(%1d)||qualident|type") ;
492 PushTFtok (init, GetType (init), tok) ;
493 END ;
494 PutIncluded (init)
495 ELSE
496 PushTFtok (init, GetType (init), tok) ;
497 Annotate ("%1s(%1d)|%1s(%1d)||qualident|type")
498 END
499 END CheckModuleQualident ;
500
501
502 % module P2Build end
503 END P2Build.
504 % rules
505 error 'ErrorArray' 'ErrorString'
506 tokenfunc 'currenttoken'
507
508 token '' eoftok -- internal token
509 token '+' plustok
510 token '-' minustok
511 token '*' timestok
512 token '/' dividetok
513 token ':=' becomestok
514 token '&' ambersandtok
515 token "." periodtok
516 token "," commatok
517 token ";" semicolontok
518 token '(' lparatok
519 token ')' rparatok
520 token '[' lsbratok -- left square brackets
521 token ']' rsbratok -- right square brackets
522 token '{' lcbratok -- left curly brackets
523 token '}' rcbratok -- right curly brackets
524 token '^' uparrowtok
525 token "'" singlequotetok
526 token '=' equaltok
527 token '#' hashtok
528 token '<' lesstok
529 token '>' greatertok
530 token '<>' lessgreatertok
531 token '<=' lessequaltok
532 token '>=' greaterequaltok
533 token '<*' ldirectivetok
534 token '*>' rdirectivetok
535 token '..' periodperiodtok
536 token ':' colontok
537 token '"' doublequotestok
538 token '|' bartok
539 token 'AND' andtok
540 token 'ARRAY' arraytok
541 token 'BEGIN' begintok
542 token 'BY' bytok
543 token 'CASE' casetok
544 token 'CONST' consttok
545 token 'DEFINITION' definitiontok
546 token 'DIV' divtok
547 token 'DO' dotok
548 token 'ELSE' elsetok
549 token 'ELSIF' elsiftok
550 token 'END' endtok
551 token 'EXCEPT' excepttok
552 token 'EXIT' exittok
553 token 'EXPORT' exporttok
554 token 'FINALLY' finallytok
555 token 'FOR' fortok
556 token 'FORWARD' forwardtok
557 token 'FROM' fromtok
558 token 'IF' iftok
559 token 'IMPLEMENTATION' implementationtok
560 token 'IMPORT' importtok
561 token 'IN' intok
562 token 'LOOP' looptok
563 token 'MOD' modtok
564 token 'MODULE' moduletok
565 token 'NOT' nottok
566 token 'OF' oftok
567 token 'OR' ortok
568 token 'PACKEDSET' packedsettok
569 token 'POINTER' pointertok
570 token 'PROCEDURE' proceduretok
571 token 'QUALIFIED' qualifiedtok
572 token 'UNQUALIFIED' unqualifiedtok
573 token 'RECORD' recordtok
574 token 'REM' remtok
575 token 'REPEAT' repeattok
576 token 'RETRY' retrytok
577 token 'RETURN' returntok
578 token 'SET' settok
579 token 'THEN' thentok
580 token 'TO' totok
581 token 'TYPE' typetok
582 token 'UNTIL' untiltok
583 token 'VAR' vartok
584 token 'WHILE' whiletok
585 token 'WITH' withtok
586 token 'ASM' asmtok
587 token 'VOLATILE' volatiletok
588 token '...' periodperiodperiodtok
589 token '__DATE__' datetok
590 token '__LINE__' linetok
591 token '__FILE__' filetok
592 token '__ATTRIBUTE__' attributetok
593 token '__BUILTIN__' builtintok
594 token '__INLINE__' inlinetok
595 token 'integer number' integertok
596 token 'identifier' identtok
597 token 'real number' realtok
598 token 'string' stringtok
599
600 special Ident first { < identtok > } follow { }
601 special Integer first { < integertok > } follow { }
602 special Real first { < realtok > } follow { }
603 special string first { < stringtok > } follow { }
604
605 BNF
606
607 -- the following are provided by the module m2flex and also handbuilt procedures below
608 -- Ident := Letter { ( Letter | Digit ) } =:
609 -- Integer := Digit { Digit } | OctalDigit { OctalDigit } ( " B " | " C " ) |
610 -- Digit { HexDigit } " H " =:
611 -- Real := Digit { Digit } " . " { Digit } [ ScaleFactor ] =:
612 -- ScaleFactor := " E " [ ( " + " | " - " ) ] Digit { Digit } =:
613 -- HexDigit := Digit | " A " | " B " | " C " | " D " | " E " | " F " =:
614 -- Digit := OctalDigit | " 8 " | " 9 " =:
615 -- OctalDigit := "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" =:
616 -- String
617
618 FileUnit := % PushAutoOn %
619 ( DefinitionModule |
620 ImplementationOrProgramModule ) % PopAuto %
621 =:
622
623 ProgramModule := "MODULE" % M2Error.DefaultProgramModule %
624 % BlockStart (GetTokenNo () -1) %
625 Ident % P2StartBuildProgramModule ; %
626
627
628
629
630 [ Priority
631 ]
632 ";"
633
634 { Import % BuildImportOuterModule ; %
635 }
636
637 Block
638 % BlockEnd (GetTokenNo () -1) %
639 Ident % P2EndBuildProgramModule ; %
640 "."
641 =:
642
643 ImplementationModule := "IMPLEMENTATION" % M2Error.DefaultImplementationModule %
644 % BlockStart (GetTokenNo () -1) %
645 "MODULE"
646 Ident % P2StartBuildImplementationModule ; %
647
648 [ Priority
649 ] ";"
650 { Import % BuildImportOuterModule %
651 }
652 Block
653 % BlockEnd (GetTokenNo () -1) %
654 Ident % P2EndBuildImplementationModule ; %
655 "." =:
656
657 ImplementationOrProgramModule := ImplementationModule | ProgramModule =:
658
659 Number := Integer | Real =:
660
661 Qualident := Ident
662 % IF IsAutoPushOn()
663 THEN
664 CheckModuleQualident (stopset0, stopset1, stopset2)
665 ELSE (* just parse qualident *) %
666 { "." Ident } % END %
667 =:
668
669 ConstantDeclaration := Ident "=" % SeenUnknown ;
670 BuildConst %
671 ConstExpressionInitial % DetermineType ;
672 PopNothing %
673 =:
674
675 ConstExpressionInitial := % PushAutoOff %
676 SimpleConstExpr [ Relation SimpleConstExpr ] % PopAuto %
677 =:
678
679 ConstExpression := % PushType ; SeenUnknown ; PushAutoOff %
680 SimpleConstExpr [ Relation SimpleConstExpr ] % PopAuto ; PopType %
681 =:
682
683 Relation := % SeenUnknown (* actually it will be a BOOLEAN, but this is not a constructor and not a string *) %
684 "=" | "#" | "<>" | "<" | "<=" | ">" | ">=" | "IN" =:
685
686 SimpleConstExpr := UnaryOrConstTerm { AddOperator ConstTerm } =:
687
688 UnaryOrConstTerm := "+" ConstTerm | "-" ConstTerm | ConstTerm =:
689
690 AddOperator := "+" | "-" |
691 "OR" % SeenUnknown (* actually it will be a BOOLEAN, but this is not a constructor and not a string *) %
692 =:
693
694 ConstTerm := ConstFactor { MulOperator ConstFactor } =:
695
696 MulOperator := "*" | "/"
697 | "DIV" % SeenUnknown %
698 | "MOD" % SeenUnknown %
699 | "REM" % SeenUnknown %
700 | "AND" % SeenUnknown %
701 | "&" % SeenUnknown %
702 =:
703
704 ConstFactor := Number | ConstString | ConstSetOrQualidentOrFunction |
705 "(" ConstExpression ")" | "NOT" ConstFactor |
706 ConstAttribute =:
707
708 -- to help satisfy LL1
709
710 ConstString := string % SeenString %
711 =:
712
713 ComponentElement := ConstExpression [ ".." % PopType ; SeenSet ; PushType ; SeenUnknown %
714 ConstExpression ] =:
715
716 ComponentValue := % PushType ; SeenUnknown ; PushRememberConstant %
717 ComponentElement [ 'BY' % PopType ; SeenArray ; PushType ; SeenUnknown %
718 ConstExpression ]
719 % PopType (* double check position, it must balance PushType *) %
720 % PopRememberConstant %
721 =:
722
723 ArraySetRecordValue := ComponentValue { ',' ComponentValue } =:
724
725 Constructor := '{' % SeenConstructor %
726 [ ArraySetRecordValue ] '}' =:
727
728 ConstSetOrQualidentOrFunction := Constructor | Qualident
729 [ Constructor | ConstActualParameters ] =:
730
731
732 ConstActualParameters := % PushType ; SeenUnknown %
733 "(" [ ExpList ] ")" % PopType %
734 =:
735
736 -- to help satisfy LL1
737
738 ConstAttribute := "__ATTRIBUTE__" "__BUILTIN__" "(" "(" ConstAttributeExpression ")" ")" =:
739
740 ConstAttributeExpression := Ident | "<" Qualident ',' Ident ">" =:
741
742 ByteAlignment := '<*' % PushAutoOn %
743 AttributeExpression % BuildAligned %
744 % PopAuto %
745 '*>' =:
746
747 -- OptAlignmentExpression := [ AlignmentExpression ] =:
748
749 -- AlignmentExpression := "(" ConstExpression ")" =:
750
751 Alignment := ByteAlignment | % PushT(NulSym) %
752 =:
753
754 TypeDeclaration := % VAR top: CARDINAL ; %
755 % top := Top() %
756 Ident "=" Type Alignment % BuildTypeAlignment %
757 % Assert(top=Top()) %
758 =:
759
760 Type :=
761 % PushAutoOn ; %
762 ( SimpleType | ArrayType % BuildType ; %
763 | RecordType % BuildType ; %
764 | SetType % BuildType ; %
765 | PointerType % BuildType ; %
766 | ProcedureType % BuildType ; %
767 ) % PopAuto ; %
768 =:
769
770 SimpleType :=
771 ( Qualident [ PrefixedSubrangeType ] % BuildType ; %
772 | Enumeration % BuildType ; %
773 | SubrangeType % BuildType ; %
774 )
775 =:
776
777 Enumeration := "("
778 ( IdentList
779 )
780 ")" % StartBuildEnumeration ; %
781 =:
782
783 IdentList := Ident % VAR
784 on: BOOLEAN ;
785 n : CARDINAL ; %
786 % on := IsAutoPushOn() ;
787 IF on
788 THEN
789 n := 1
790 END %
791 { "," Ident % IF on
792 THEN
793 INC(n)
794 END %
795 } % IF on
796 THEN
797 PushT(n)
798 END %
799 =:
800
801 SubrangeType := % VAR start, combined: CARDINAL ; %
802 % start := GetTokenNo () %
803 "[" ConstExpression ".." ConstExpression "]" % combined := MakeVirtual2Tok (start, GetTokenNo ()-1) %
804 % BuildSubrange (combined, NulSym) %
805 =:
806
807 PrefixedSubrangeType := % VAR qual, start, combined: CARDINAL ; %
808 % PopTtok (qual, start) %
809 "[" ConstExpression ".." ConstExpression "]"
810 % combined := MakeVirtual2Tok (start, GetTokenNo ()-1) %
811 % BuildSubrange (combined, qual) %
812 =:
813
814 ArrayType := "ARRAY" % VAR arrayType, tok: CARDINAL ; %
815 % StartBuildArray ;
816 PopTtok(arrayType, tok) ;
817 PushTtok(arrayType, tok) ;
818 BuildNulName ; %
819 SimpleType % BuildFieldArray ; %
820 { "," % BuildArrayComma ;
821 BuildNulName ; %
822 SimpleType % BuildFieldArray ; %
823 } "OF" % BuildNulName ; %
824 Type % EndBuildArray ;
825 tok := OperandTok (1) ;
826 PopNothing ;
827 PushTtok (arrayType, tok) %
828 =:
829
830 RecordType := "RECORD" % BuildRecord %
831 [ DefaultRecordAttributes ]
832 FieldListSequence "END" =:
833
834 DefaultRecordAttributes := '<*' % PushAutoOn %
835 AttributeExpression % P2BuildDefaultFieldAlignment %
836 % PopAuto %
837 '*>' =:
838
839 RecordFieldPragma := ( '<*' FieldPragmaExpression % VAR n: CARDINAL ; %
840 % n := 1 %
841 % PushT(n) %
842 % Annotate('(%1d)||pragma count') %
843 { ',' % PopT(n) %
844 FieldPragmaExpression % INC(n) %
845 % PushT(n) %
846 % Annotate('(%1d)||pragma count') %
847 } '*>' | % n := 0 %
848 % PushT(n) %
849 % Annotate('(%1d)||pragma count') %
850 ) =:
851
852 FieldPragmaExpression := % PushAutoOn %
853 Ident PragmaConstExpression
854 % PopAuto %
855 =:
856
857 PragmaConstExpression := ( % PushAutoOff %
858 '(' ConstExpression % BuildPragmaConst %
859 ')' % PopAuto %
860 | % PushT(NulSym) %
861 % Annotate('NulSym||no pragma const') %
862 ) =:
863
864 AttributeExpression := Ident '(' ConstExpression ')' =:
865
866 FieldListSequence := FieldListStatement { ";" FieldListStatement } =:
867
868 FieldListStatement := [ FieldList ] =:
869
870 -- sadly the PIM rules are not LL1 as Ident and Qualident have the same first
871 -- symbols. We rewrite FieldList to inline qualident
872
873 FieldList := IdentList ":" % BuildNulName %
874 Type RecordFieldPragma % BuildFieldRecord %
875 |
876 "CASE" % AddRecordToList %
877 % StartBuildVarient %
878 % AddVarientToList %
879 CaseTag "OF"
880 Varient { "|" Varient }
881 [ "ELSE" % StartBuildVarientFieldRecord %
882 FieldListSequence % EndBuildVarientFieldRecord %
883 ] "END" % EndBuildVarient %
884 =:
885
886 TagIdent := Ident | % BuildNulName %
887 =:
888
889 CaseTag := TagIdent ( ":" Qualident | % PushT(NulSym) %
890 ) % BuildVarientSelector %
891 =:
892
893 Varient := [ % StartBuildVarientFieldRecord %
894 VarientCaseLabelList ":" FieldListSequence % EndBuildVarientFieldRecord %
895 ]
896 =:
897
898 VarientCaseLabelList := VarientCaseLabels { "," VarientCaseLabels } =:
899
900 VarientCaseLabels := ConstExpression [ ".." ConstExpression ] =:
901
902 CaseLabelList := CaseLabels { "," CaseLabels } =:
903
904 CaseLabels := ConstExpression [ ".." ConstExpression ] =:
905
906 SetType := % VAR ispacked: BOOLEAN ;
907 setpos : CARDINAL ; %
908 % setpos := GetTokenNo () %
909 % ispacked := FALSE %
910 ( "SET" % ispacked := FALSE %
911 | "PACKEDSET" % ispacked := TRUE %
912 ) "OF" % BuildNulName %
913 SimpleType % BuildSetType (setpos, ispacked) %
914 =:
915
916
917 PointerType := % VAR pointerpos: CARDINAL ; %
918 % pointerpos := GetTokenNo () %
919 "POINTER" "TO" % BuildNulName %
920 Type % BuildPointerType (pointerpos) %
921 =:
922
923 ProcedureType := "PROCEDURE" % BuildProcedureType ; %
924 [ FormalTypeList ] =:
925
926 FormalTypeList := "(" ( ")" FormalReturn |
927 ProcedureParameters
928 ")" FormalReturn ) =:
929
930 FormalReturn := ( ":" OptReturnType | % CheckProcedure %
931 )
932 =:
933
934 OptReturnType := "[" Qualident % BuildOptFunction %
935 "]" | Qualident % BuildFunction %
936 =:
937
938 ProcedureParameters := ProcedureParameter
939 { "," ProcedureParameter } =:
940
941 ProcedureParameter := "..." % BuildFormalVarArgs %
942 | "VAR" % PushT(VarTok) ; %
943 FormalType % BuildFormalType ; %
944 | % PushT(NulTok) ; %
945 FormalType % BuildFormalType ; %
946 =:
947
948 VarIdent := Ident % VAR
949 on : BOOLEAN ;
950 Sym, Type, tok: CARDINAL ; %
951 [ "[" % on := IsAutoPushOn() ;
952 IF on
953 THEN
954 PopTFtok(Sym, Type, tok) ;
955 PushTFAtok(Sym, Type, Sym, tok)
956 END %
957 ConstExpression "]" ]
958 =:
959
960 VarIdentList := VarIdent % VAR
961 on: BOOLEAN ;
962 n : CARDINAL ; %
963 % on := IsAutoPushOn() ;
964 IF on
965 THEN
966 n := 1
967 END %
968 { "," VarIdent % IF on
969 THEN
970 INC(n)
971 END %
972 } % IF on
973 THEN
974 PushT(n)
975 END %
976 =:
977
978 VariableDeclaration := VarIdentList ":" % BuildNulName %
979 % DisplayStack %
980 Type % DisplayStack %
981 Alignment % DisplayStack %
982 % BuildVarAlignment %
983 % DisplayStack %
984 % BuildVariable %
985 =:
986
987 Designator := Qualident { SubDesignator } =:
988
989 SubDesignator := "." Ident | "[" ExpList "]" | "^" =:
990
991 ExpList := Expression { "," Expression } =:
992
993 Expression := % PushType ; SeenUnknown ; PushAutoOff %
994 SimpleExpression [ Relation SimpleExpression ] % PopAuto ; PopType %
995 =:
996
997 SimpleExpression := UnaryOrTerm { AddOperator Term } =:
998
999 UnaryOrTerm := "+" Term | "-" Term | Term =:
1000
1001 Term := Factor { MulOperator Factor } =:
1002
1003 Factor := Number | string | SetOrDesignatorOrFunction |
1004 "(" Expression ")" | "NOT" Factor | ConstAttribute =:
1005
1006 SetOrDesignatorOrFunction := ( Qualident [ Constructor |
1007 SimpleDes [ ActualParameters ]
1008 ] | Constructor
1009 )
1010 =:
1011
1012 SimpleDes := { "." Ident | "[" ExpList "]" | "^" } =:
1013
1014 ActualParameters := "(" [ ExpList ] ")" =:
1015
1016 Statement := % PushAutoOff ; %
1017 [ AssignmentOrProcedureCall | IfStatement | CaseStatement |
1018 WhileStatement | RepeatStatement | LoopStatement |
1019 ForStatement | WithStatement | AsmStatement |
1020 "EXIT" | "RETURN" [ Expression ] | RetryStatement ] % PopAuto ; %
1021 =:
1022
1023 RetryStatement := "RETRY" =:
1024
1025 AssignmentOrProcedureCall := Designator ( ":=" Expression |
1026 ActualParameters | % (* epsilon *) %
1027 ) =:
1028
1029 -- these two break LL1 as both start with a Designator
1030 -- ProcedureCall := Designator [ ActualParameters ] =:
1031 -- Assignment := Designator ":=" Expression =:
1032
1033 StatementSequence := Statement { ";" Statement } =:
1034
1035 IfStatement := "IF" Expression "THEN" StatementSequence
1036 { "ELSIF" Expression "THEN" StatementSequence }
1037 [ "ELSE" StatementSequence ] "END" =:
1038
1039 CaseStatement := "CASE" Expression "OF" Case { "|" Case }
1040 [ "ELSE" StatementSequence ] "END" =:
1041
1042 Case := [ CaseLabelList ":" StatementSequence ] =:
1043
1044 WhileStatement := "WHILE" Expression "DO" StatementSequence "END" =:
1045
1046 RepeatStatement := "REPEAT" StatementSequence "UNTIL" Expression =:
1047
1048 ForStatement := "FOR" Ident ":=" Expression "TO" Expression
1049 [ "BY" ConstExpression ] "DO"
1050 StatementSequence "END" =:
1051
1052 LoopStatement := "LOOP" StatementSequence "END" =:
1053
1054 WithStatement := "WITH" Designator "DO" StatementSequence "END" =:
1055
1056 ProcedureDeclaration := % VAR top: CARDINAL ; %
1057 % top := Top () %
1058 ProcedureHeading % Assert(IsProcedure(OperandT(1))) %
1059 ";" PostProcedureHeading % Assert (top = Top ()) %
1060 =:
1061
1062 PostProcedureHeading := ProperProcedure | ForwardDeclaration =:
1063
1064 ForwardDeclaration := "FORWARD" % Assert (IsProcedure (OperandT (1))) %
1065 % EndBuildForward %
1066 =:
1067
1068 ProperProcedure := ProcedureBlock % Assert(IsProcedure(OperandT(1))) %
1069 Ident % EndBuildProcedure %
1070 =:
1071
1072 DefineBuiltinProcedure := [ "__ATTRIBUTE__" "__BUILTIN__"
1073 "(" "(" % PushAutoOff %
1074 Ident % PopAuto %
1075 ")" ")" | "__INLINE__" ]
1076 =:
1077
1078 ProcedureHeading := "PROCEDURE" % M2Error.DefaultProcedure %
1079 DefineBuiltinProcedure
1080 ( Ident
1081 % StartBuildProcedure %
1082 % Assert(IsProcedure(OperandT(1))) %
1083 % StartBuildFormalParameters %
1084 % Assert(IsProcedure(OperandT(2))) %
1085 [ FormalParameters
1086 % Assert(IsProcedure(OperandT(2))) %
1087 ] % EndBuildFormalParameters %
1088 AttributeNoReturn
1089 % BuildProcedureHeading %
1090 )
1091 =:
1092
1093 Builtin := [ "__BUILTIN__" | "__INLINE__" ] =:
1094
1095 DefProcedureHeading := "PROCEDURE" % M2Error.DefaultProcedure %
1096 Builtin
1097 ( Ident
1098 % StartBuildProcedure %
1099 % Assert(IsProcedure(OperandT(1))) %
1100 % DisplayStack %
1101 % StartBuildFormalParameters %
1102 % DisplayStack %
1103 [ DefFormalParameters % DisplayStack %
1104 ] % DisplayStack %
1105 % EndBuildFormalParameters %
1106 AttributeNoReturn
1107 % BuildProcedureHeading %
1108 ) % M2Error.LeaveErrorScope %
1109 =:
1110
1111 AttributeNoReturn := [ "<*" % PushAutoOn %
1112 Ident % PopAuto %
1113 % checkReturnAttribute %
1114 % Assert(IsProcedure(OperandT(1))) %
1115 % BuildNoReturnAttribute %
1116 "*>" ] =:
1117
1118 AttributeUnused := [ "<*" % PushAutoOn %
1119 Ident % PopAuto %
1120 % checkParameterAttribute %
1121 "*>" ] =:
1122
1123 -- introduced procedure block so we can produce more informative
1124 -- error messages
1125
1126 ProcedureBlock := % Assert(IsProcedure(OperandT(1))) %
1127 { % Assert(IsProcedure(OperandT(1))) %
1128 Declaration % Assert(IsProcedure(OperandT(1))) %
1129 } [ "BEGIN" ProcedureBlockBody ] "END" % Assert(IsProcedure(OperandT(1))) %
1130 =:
1131
1132 Block := { Declaration } InitialBlock FinalBlock "END" =:
1133
1134 InitialBlock := [ "BEGIN" % BlockBegin (GetTokenNo () -1) %
1135 InitialBlockBody ] =:
1136
1137 FinalBlock := [ "FINALLY" % BlockFinally (GetTokenNo () -1) %
1138 FinalBlockBody ] =:
1139
1140 InitialBlockBody := NormalPart [ "EXCEPT" % PutExceptionBlock(GetCurrentScope()) %
1141 ExceptionalPart ] =:
1142
1143 FinalBlockBody := NormalPart [ "EXCEPT" % PutExceptionFinally(GetCurrentScope()) %
1144 ExceptionalPart ] =:
1145
1146 ProcedureBlockBody := NormalPart [ "EXCEPT" % PutExceptionBlock(GetCurrentScope()) %
1147 ExceptionalPart ] =:
1148
1149 NormalPart := StatementSequence =:
1150
1151 ExceptionalPart := StatementSequence =:
1152
1153 Declaration := "CONST" { ConstantDeclaration ";" } |
1154 "TYPE" { TypeDeclaration ";" } |
1155 "VAR" { VariableDeclaration ";" } |
1156 ProcedureDeclaration ";" |
1157 ModuleDeclaration ";" =:
1158
1159 DefFormalParameters := "("
1160 [ DefMultiFPSection ] % VAR n: CARDINAL; %
1161 % PopT(n) ; (* remove param count *) %
1162 ")"
1163 FormalReturn % PushT(n) ; (* restore param count *)
1164 Annotate ("%1d||running total of no. of parameters") %
1165 =:
1166
1167 DefMultiFPSection := DefExtendedFP |
1168 FPSection [ ";" DefMultiFPSection ] =:
1169
1170 FormalParameters := "("
1171 [ MultiFPSection ] % VAR n: CARDINAL; %
1172 % PopT(n) ; (* remove param count *) %
1173 ")"
1174 FormalReturn % PushT(n) ; (* restore param count *) ;
1175 Annotate ("%1d||running total of no. of parameters") %
1176 =:
1177
1178 MultiFPSection := ExtendedFP |
1179 FPSection [ ";" MultiFPSection ] =:
1180
1181 FPSection := NonVarFPSection | VarFPSection
1182 =:
1183
1184 DefExtendedFP := DefOptArg | "..." % BuildVarArgs %
1185 =:
1186
1187 ExtendedFP := OptArg | "..." % BuildVarArgs %
1188 =:
1189
1190 OptArg := "[" % VAR n: CARDINAL ; %
1191 % PopT(n) %
1192 % PushT(NulTok) %
1193 Ident % PushT(1) %
1194 ":" FormalType % PushT(n) %
1195 % Annotate ("%1d||running total of no. of parameters") %
1196 % BuildFPSection %
1197 % BuildOptArg %
1198 [ "=" ConstExpression ]
1199 "]" =:
1200
1201 DefOptArg := "[" % VAR n: CARDINAL ; %
1202 % PopT(n) %
1203 % PushT(NulTok) %
1204 Ident % PushT(1) %
1205 ":" FormalType % PushT(n) %
1206 % Annotate ("%1d||running total of no. of parameters") %
1207 % BuildFPSection %
1208 % BuildOptArg %
1209 "=" ConstExpression
1210 "]" =:
1211
1212 VarFPSection := "VAR" % VAR n: CARDINAL ; %
1213 % PopT(n) ; %
1214 % PushT(VarTok) ; %
1215 IdentList ":" FormalType % PushT(n) %
1216 % Annotate ("%1d||running total of no. of parameters") %
1217 [ AttributeUnused ]
1218 % BuildFPSection %
1219 =:
1220
1221 NonVarFPSection := % VAR n: CARDINAL ; %
1222 % PopT(n) %
1223 % PushT(NulTok) %
1224 IdentList ":" FormalType % PushT(n) %
1225 % Annotate ("%1d||running total of no. of parameters") %
1226 [ AttributeUnused ]
1227 % BuildFPSection %
1228 =:
1229
1230 FormalType := "ARRAY" "OF" % VAR n, tok: CARDINAL ; %
1231 % PushTF(ArrayTok, 1) %
1232 { "ARRAY" "OF" % PopTF(ArrayTok, n) %
1233 % INC(n) %
1234 % PushTF(ArrayTok, n) %
1235 } Qualident
1236 | % VAR Sym, Type: CARDINAL ; %
1237 Qualident
1238 % PopTFtok (Sym, Type, tok) ;
1239 PushT(NulTok) ;
1240 PushTFtok (Sym, Type, tok) %
1241 =:
1242
1243 ModuleDeclaration := "MODULE" % M2Error.DefaultInnerModule %
1244 % BlockStart (GetTokenNo () -1) %
1245 Ident % StartBuildInnerModule %
1246 [ Priority
1247 ] ";"
1248 { Import % BuildImportInnerModule %
1249 } [ Export % BuildExportInnerModule %
1250 ]
1251 Block
1252 % BlockEnd (GetTokenNo () -1) %
1253 Ident % EndBuildInnerModule %
1254 =:
1255
1256 Priority := "[" ConstExpression "]" =:
1257
1258 Export := "EXPORT" ( "QUALIFIED" % PushT(QualifiedTok) %
1259 IdentList |
1260 "UNQUALIFIED" % PushT(UnQualifiedTok) %
1261 IdentList | % PushT(ExportTok) %
1262 IdentList ) ";" =:
1263
1264 Import := "FROM" Ident "IMPORT" IdentList ";" |
1265 "IMPORT" % PushT(ImportTok)
1266 (* determines whether Ident or Module *) %
1267 IdentList ";" =:
1268
1269 DefinitionModule := "DEFINITION" % M2Error.DefaultDefinitionModule %
1270 "MODULE"
1271 [ "FOR" string ]
1272 Ident % P2StartBuildDefModule %
1273 ";"
1274 { Import % BuildImportOuterModule %
1275 } [ Export % BuildExportOuterModule %
1276 ]
1277 { Definition }
1278 "END" Ident % P2EndBuildDefModule %
1279 "."
1280 =:
1281
1282 Definition := "CONST" { ConstantDeclaration ";" } |
1283 "TYPE"
1284 { Ident ( ";"
1285 | "=" Type Alignment % BuildVarAlignment %
1286 ";" ) % BuildTypeEnd %
1287 }
1288 |
1289 "VAR" { VariableDeclaration ";" } |
1290 DefProcedureHeading ";" =:
1291
1292 AsmStatement := 'ASM' [ 'VOLATILE' ] '(' AsmOperands ')' =:
1293
1294 NamedOperand := '[' Ident ']' =:
1295
1296 AsmOperandName := [ NamedOperand ] =:
1297
1298 AsmOperands := ConstExpression [ ':' AsmList [ ':' AsmList [ ':' TrashList ] ] ]
1299 =:
1300
1301 AsmList := [ AsmElement ] { ',' AsmElement } =:
1302
1303 AsmElement := AsmOperandName ConstExpression '(' Expression ')'
1304 =:
1305
1306 TrashList := [ ConstExpression ] { ',' ConstExpression } =:
1307
1308 FNB