Debian Codegraph @

gcc/m2/gm2-compiler/P1Build.bnf

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