Debian Codegraph @

gcc/m2/gm2-compiler/P3Build.bnf

raw
1 --
2 -- m2-3.bnf grammar and associated actions for pass 3.
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 P3Build begin
23 (* output from m2-3.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 P3Build ;
46
47 FROM M2LexBuf IMPORT currentstring, currenttoken, GetToken, InsertToken,
48 InsertTokenAndRewind, GetTokenNo, PrintTokenNo, MakeVirtualTok,
49 UnknownTokenNo ;
50
51 FROM M2Error IMPORT ErrorStringAt, WriteFormat1, WriteFormat2 ;
52 FROM NameKey IMPORT NulName, Name, makekey ;
53 FROM DynamicStrings IMPORT String, InitString, KillString, Mark, ConCat, ConCatChar ;
54 FROM M2Printf IMPORT printf0, printf1 ;
55 FROM M2Debug IMPORT Assert ;
56 FROM P2SymBuild IMPORT BuildString, BuildNumber ;
57 FROM M2MetaError IMPORT MetaErrorT0, MetaErrorT2 ;
58 FROM M2CaseList IMPORT ElseCase ;
59
60 FROM M2Reserved IMPORT tokToTok, toktype,
61 NulTok, ImportTok, ExportTok, QualifiedTok, UnQualifiedTok,
62 EqualTok, HashTok, LessGreaterTok, LessTok, LessEqualTok,
63 GreaterTok, GreaterEqualTok, InTok, PlusTok, MinusTok,
64 OrTok, TimesTok, DivTok, DivideTok, ModTok, RemTok,
65 AndTok, AmbersandTok, PeriodPeriodTok, ByTok ;
66
67 FROM M2Quads IMPORT PushT, PopT, PushTF, PopTF, PopNothing, Annotate,
68 PushTtok, PushTFtok, PopTtok, PopTFtok, OperandTok,
69 DupFrame, Top,
70 BuildModuleStart,
71 StartBuildDefFile, StartBuildModFile,
72 EndBuildFile,
73 StartBuildInit,
74 EndBuildInit,
75 StartBuildFinally,
76 EndBuildFinally,
77 BuildExceptInitial,
78 BuildExceptFinally,
79 BuildExceptProcedure,
80 BuildReThrow,
81 BuildProcedureStart,
82 BuildProcedureBegin,
83 BuildProcedureEnd,
84 BuildScaffold,
85 BuildStmtNote,
86 BuildFunctionCall, BuildConstFunctionCall,
87 BuildBinaryOp, BuildUnaryOp, BuildRelOp, BuildNot,
88 BuildEmptySet, BuildInclRange, BuildInclBit,
89 BuildSetStart, BuildSetEnd,
90 PushLineNo, BuildSizeCheckStart,
91 BuildBuiltinConst, BuildBuiltinTypeInfo,
92 BuildAssignment, BuildAssignConstant,
93 BuildAlignment,
94 BuildRepeat, BuildUntil,
95 BuildWhile, BuildDoWhile, BuildEndWhile,
96 BuildLoop, BuildExit, BuildEndLoop,
97 BuildThenIf, BuildElse, BuildEndIf,
98 BuildForToByDo, BuildPseudoBy, BuildEndFor,
99 BuildElsif1, BuildElsif2,
100 BuildProcedureCall, BuildReturn, BuildNulExpression,
101 CheckBuildFunction,
102 StartBuildWith, EndBuildWith,
103 BuildAsm,
104 BuildCaseStart,
105 BuildCaseOr,
106 BuildCaseElse,
107 BuildCaseEnd,
108 BuildCaseCheck,
109 BuildCaseStartStatementSequence,
110 BuildCaseEndStatementSequence,
111 BuildCaseList,
112 BuildCaseRange, BuildCaseEquality,
113 BuildConstructorStart,
114 BuildConstructorEnd,
115 SilentBuildConstructorStart,
116 NextConstructorField, BuildTypeForConstructor,
117 BuildComponentValue,
118 BeginVarient, EndVarient, ElseVarient,
119 BeginVarientList, EndVarientList,
120 RecordOp,
121 BuildNulParam,
122 BuildDesignatorRecord,
123 BuildDesignatorArray,
124 BuildDesignatorPointer,
125 BuildBooleanVariable,
126 CheckWithReference,
127 BuildModulePriority,
128 BuildRetry,
129 DisplayStack,
130 AddVarientRange, AddVarientEquality,
131 BuildAsmElement, BuildAsmTrash,
132 BeginVarient, EndVarient, BeginVarientList, EndVarientList,
133 PushInConstExpression, PopInConstExpression,
134 PushInConstParameters, PopInConstParameters, IsInConstParameters,
135 BuildDefaultFieldAlignment, BuildPragmaField,
136 OperandT, OperandTok,
137 IsAutoPushOn, PushAutoOff, PushAutoOn, PopAuto ;
138
139 FROM P3SymBuild IMPORT P3StartBuildProgModule,
140 P3EndBuildProgModule,
141
142 P3StartBuildDefModule,
143 P3EndBuildDefModule,
144
145 P3StartBuildImpModule,
146 P3EndBuildImpModule,
147
148 StartBuildInnerModule,
149 EndBuildInnerModule,
150
151 CheckImportListOuterModule,
152 CheckCanBeImported,
153 StartBuildProcedure,
154 BuildProcedureHeading,
155 EndBuildProcedure,
156 EndBuildForward,
157 BuildVarAtAddress,
158 BuildConst,
159 BuildSubrange,
160 BuildNulName,
161 BuildOptArgInitializer ;
162
163 FROM SymbolTable IMPORT MakeGnuAsm, PutGnuAsmVolatile, PutGnuAsm, PutGnuAsmInput,
164 PutGnuAsmOutput, PutGnuAsmTrash,
165 PutGnuAsmVolatile, PutGnuAsmSimple,
166 MakeRegInterface,
167 PutRegInterface,
168 IsRegInterface, IsGnuAsmVolatile, IsGnuAsm,
169 GetCurrentModule, IsInnerModule,
170 GetSymName, GetType, SkipType,
171 NulSym,
172 StartScope, EndScope,
173 PutIncluded,
174 IsVarParam, IsProcedure, IsDefImp, IsModule, IsProcType,
175 IsRecord,
176 RequestSym, IsExported, IsImported,
177 GetSym, GetLocalSym ;
178
179 FROM M2Batch IMPORT IsModuleKnown ;
180
181 FROM M2CaseList IMPORT BeginCaseList, EndCaseList ;
182
183 FROM M2StateCheck IMPORT StateCheck,
184 InitState, PushState, PopState,
185 InclConst, ExclConst,
186 InclType, ExclType,
187 InclVar, ExclVar,
188 InclConstructor, ExclConstructor,
189 InclConstFunc, CheckQualident ;
190
191 IMPORT M2Error ;
192
193 CONST
194 Debugging = FALSE ;
195 DebugAsm = FALSE ;
196
197 VAR
198 seenError : BOOLEAN ;
199 BlockState: StateCheck ;
200
201
202 PROCEDURE ErrorString (s: String) ;
203 BEGIN
204 ErrorStringAt (s, GetTokenNo ()) ;
205 seenError := TRUE
206 END ErrorString ;
207
208
209 PROCEDURE ErrorArray (a: ARRAY OF CHAR) ;
210 BEGIN
211 ErrorString (InitString (a))
212 END ErrorArray ;
213
214
215 % declaration P3Build begin
216
217
218 (*
219 SyntaxError - after a syntax error we skip all tokens up until we reach
220 a stop symbol.
221 *)
222
223 PROCEDURE SyntaxError (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
224 BEGIN
225 DescribeError ;
226 IF Debugging
227 THEN
228 printf0('\nskipping token *** ')
229 END ;
230 (* --fixme-- this assumes a 32 bit word size. *)
231 WHILE NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
232 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
233 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))
234 DO
235 GetToken
236 END ;
237 IF Debugging
238 THEN
239 printf0(' ***\n')
240 END
241 END SyntaxError ;
242
243
244 (*
245 SyntaxCheck -
246 *)
247
248 PROCEDURE SyntaxCheck (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
249 BEGIN
250 (* --fixme-- this assumes a 32 bit word size. *)
251 IF NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
252 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
253 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))
254 THEN
255 SyntaxError(stopset0, stopset1, stopset2)
256 END
257 END SyntaxCheck ;
258
259
260 (*
261 WarnMissingToken - generates a warning message about a missing token, t.
262 *)
263
264 PROCEDURE WarnMissingToken (t: toktype) ;
265 VAR
266 s0 : SetOfStop0 ;
267 s1 : SetOfStop1 ;
268 s2 : SetOfStop2 ;
269 str: String ;
270 BEGIN
271 s0 := SetOfStop0{} ;
272 s1 := SetOfStop1{} ;
273 s2 := SetOfStop2{} ;
274 IF ORD(t)<32
275 THEN
276 s0 := SetOfStop0{t}
277 ELSIF ORD(t)<64
278 THEN
279 s1 := SetOfStop1{t}
280 ELSE
281 s2 := SetOfStop2{t}
282 END ;
283 str := DescribeStop(s0, s1, s2) ;
284
285 str := ConCat(InitString('syntax error,'), Mark(str)) ;
286 ErrorStringAt (str, GetTokenNo ())
287 END WarnMissingToken ;
288
289
290 (*
291 MissingToken - generates a warning message about a missing token, t.
292 *)
293
294 PROCEDURE MissingToken (t: toktype) ;
295 BEGIN
296 WarnMissingToken(t) ;
297 IF (t#identtok) AND (t#integertok) AND (t#realtok) AND (t#stringtok)
298 THEN
299 IF Debugging
300 THEN
301 printf0('inserting token\n')
302 END ;
303 InsertToken(t)
304 END
305 END MissingToken ;
306
307
308 (*
309 CheckAndInsert -
310 *)
311
312 PROCEDURE CheckAndInsert (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) : BOOLEAN ;
313 BEGIN
314 IF ((ORD(t)<32) AND (t IN stopset0)) OR
315 ((ORD(t)>=32) AND (ORD(t)<64) AND (t IN stopset1)) OR
316 ((ORD(t)>=64) AND (t IN stopset2))
317 THEN
318 WarnMissingToken(t) ;
319 InsertTokenAndRewind(t) ;
320 RETURN( TRUE )
321 ELSE
322 RETURN( FALSE )
323 END
324 END CheckAndInsert ;
325
326
327 (*
328 InStopSet
329 *)
330
331 PROCEDURE InStopSet (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) : BOOLEAN ;
332 BEGIN
333 IF ((ORD(t)<32) AND (t IN stopset0)) OR
334 ((ORD(t)>=32) AND (ORD(t)<64) AND (t IN stopset1)) OR
335 ((ORD(t)>=64) AND (t IN stopset2))
336 THEN
337 RETURN( TRUE )
338 ELSE
339 RETURN( FALSE )
340 END
341 END InStopSet ;
342
343
344 (*
345 PeepToken - peep token checks to see whether the stopset is satisfied by currenttoken
346 If it is not then it will insert a token providing the token
347 is one of ; ] ) } . OF END ,
348
349 if the stopset contains <identtok> then we do not insert a token
350 *)
351
352 PROCEDURE PeepToken (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
353 BEGIN
354 (* and again (see above re: ORD)
355 *)
356 IF (NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
357 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
358 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))) AND
359 (NOT InStopSet(identtok, stopset0, stopset1, stopset2))
360 THEN
361 (* SyntaxCheck would fail since currentoken is not part of the stopset
362 we check to see whether any of currenttoken might be a commonly omitted token *)
363 IF CheckAndInsert(semicolontok, stopset0, stopset1, stopset2) OR
364 CheckAndInsert(rsbratok, stopset0, stopset1, stopset2) OR
365 CheckAndInsert(rparatok, stopset0, stopset1, stopset2) OR
366 CheckAndInsert(rcbratok, stopset0, stopset1, stopset2) OR
367 CheckAndInsert(periodtok, stopset0, stopset1, stopset2) OR
368 CheckAndInsert(oftok, stopset0, stopset1, stopset2) OR
369 CheckAndInsert(endtok, stopset0, stopset1, stopset2) OR
370 CheckAndInsert(commatok, stopset0, stopset1, stopset2)
371 THEN
372 END
373 END
374 END PeepToken ;
375
376
377 (*
378 Expect -
379 *)
380
381 PROCEDURE Expect (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
382 CONST
383 Pass1 = FALSE ;
384 BEGIN
385 IF currenttoken=t
386 THEN
387 GetToken ;
388 IF Pass1
389 THEN
390 PeepToken(stopset0, stopset1, stopset2)
391 END
392 ELSE
393 MissingToken(t)
394 END ;
395 SyntaxCheck(stopset0, stopset1, stopset2)
396 END Expect ;
397
398
399 (*
400 CompilationUnit - returns TRUE if the input was correct enough to parse
401 in future passes.
402 *)
403
404 PROCEDURE CompilationUnit () : BOOLEAN ;
405 BEGIN
406 seenError := FALSE ;
407 FileUnit(SetOfStop0{eoftok}, SetOfStop1{}, SetOfStop2{}) ;
408 RETURN NOT seenError
409 END CompilationUnit ;
410
411
412 (*
413 Ident - error checking varient of Ident
414 *)
415
416 PROCEDURE Ident (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
417 BEGIN
418 IF IsAutoPushOn()
419 THEN
420 PushTFtok (makekey (currentstring), identtok, GetTokenNo ())
421 (* ; MetaErrorT0 (GetTokenNo(), "{%W}an ident") *)
422 END ;
423 Expect(identtok, stopset0, stopset1, stopset2)
424 END Ident ;
425
426
427 (*
428 string -
429 *)
430
431 PROCEDURE string (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
432 BEGIN
433 IF IsAutoPushOn()
434 THEN
435 PushTFtok(makekey(currentstring), stringtok, GetTokenNo ()) ;
436 BuildString
437 END ;
438 Expect(stringtok, stopset0, stopset1, stopset2)
439 END string ;
440
441
442 (*
443 Integer -
444 *)
445
446 PROCEDURE Integer (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
447 BEGIN
448 IF IsAutoPushOn()
449 THEN
450 PushTFtok (makekey(currentstring), integertok, GetTokenNo ()) ;
451 BuildNumber
452 END ;
453 Expect(integertok, stopset0, stopset1, stopset2)
454 END Integer ;
455
456
457 (*
458 Real -
459 *)
460
461 PROCEDURE Real (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
462 BEGIN
463 IF IsAutoPushOn()
464 THEN
465 PushTFtok (makekey(currentstring), realtok, GetTokenNo ()) ;
466 BuildNumber
467 END ;
468 Expect(realtok, stopset0, stopset1, stopset2)
469 END Real ;
470
471
472 (*
473 PushTFQualident - push the result of the Qualident
474 to the stack. It checks to see if init
475 is a procedure or proc type and if so
476 it does not push the return type.
477 *)
478
479 PROCEDURE PushTFQualident (tok, tokstart: CARDINAL;
480 init: CARDINAL) ;
481 BEGIN
482 IF tok#tokstart
483 THEN
484 tok := MakeVirtualTok (tokstart, tokstart, tok)
485 END ;
486 IF IsProcedure (init) OR IsProcType (init) OR IsModule (init) OR IsDefImp (init)
487 THEN
488 PushTtok (init, tok) ;
489 Annotate ("%1s(%1d)||qualident procedure/proctype") ;
490 ELSE
491 Annotate ("%1s(%1d)|%1s(%1d)||qualident|type") ;
492 PushTFtok (init, GetType (init), tok) ;
493 END
494 END PushTFQualident ;
495
496
497 (*
498 CheckModuleQualident - check to see if the beginning ident of the qualident is an
499 imported module.
500 *)
501
502 PROCEDURE CheckModuleQualident (stopset0: SetOfStop0;
503 stopset1: SetOfStop1;
504 stopset2: SetOfStop2) ;
505 VAR
506 name : Name ;
507 init,
508 nextLevel,
509 tok, tokstart: CARDINAL ;
510 BEGIN
511 PopTtok (name, tokstart) ;
512 tok := tokstart ;
513 init := RequestSym (tok, name) ;
514 IF (IsImported (GetCurrentModule (), init) AND IsDefImp (init)) OR
515 IsModule (init)
516 THEN
517 WHILE IsDefImp (init) OR IsModule (init) DO
518 Expect (periodtok, stopset0, stopset1, stopset2 + SetOfStop2{identtok}) ;
519 StartScope (init) ;
520 Ident (stopset0, stopset1, stopset2) ;
521 PopTtok (name, tok) ;
522 nextLevel := RequestSym (tok, name) ;
523 EndScope ;
524 CheckCanBeImported (init, nextLevel) ;
525 init := nextLevel
526 END ;
527 PushTFQualident (tok, tokstart, init) ;
528 PutIncluded (init)
529 ELSE
530 PushTFQualident (tok, tokstart, init)
531 END
532 END CheckModuleQualident ;
533
534 % module P3Build end
535 BEGIN
536 BlockState := InitState ()
537 END P3Build.
538 % rules
539 error 'ErrorArray' 'ErrorString'
540 tokenfunc 'currenttoken'
541
542 token '' eoftok -- internal token
543 token '+' plustok
544 token '-' minustok
545 token '*' timestok
546 token '/' dividetok
547 token ':=' becomestok
548 token '&' ambersandtok
549 token "." periodtok
550 token "," commatok
551 token ";" semicolontok
552 token '(' lparatok
553 token ')' rparatok
554 token '[' lsbratok -- left square brackets
555 token ']' rsbratok -- right square brackets
556 token '{' lcbratok -- left curly brackets
557 token '}' rcbratok -- right curly brackets
558 token '^' uparrowtok
559 token "'" singlequotetok
560 token '=' equaltok
561 token '#' hashtok
562 token '<' lesstok
563 token '>' greatertok
564 token '<>' lessgreatertok
565 token '<=' lessequaltok
566 token '>=' greaterequaltok
567 token '<*' ldirectivetok
568 token '*>' rdirectivetok
569 token '..' periodperiodtok
570 token ':' colontok
571 token '"' doublequotestok
572 token '|' bartok
573 token 'AND' andtok
574 token 'ARRAY' arraytok
575 token 'BEGIN' begintok
576 token 'BY' bytok
577 token 'CASE' casetok
578 token 'CONST' consttok
579 token 'DEFINITION' definitiontok
580 token 'DIV' divtok
581 token 'DO' dotok
582 token 'ELSE' elsetok
583 token 'ELSIF' elsiftok
584 token 'END' endtok
585 token 'EXCEPT' excepttok
586 token 'EXIT' exittok
587 token 'EXPORT' exporttok
588 token 'FINALLY' finallytok
589 token 'FOR' fortok
590 token 'FORWARD' forwardtok
591 token 'FROM' fromtok
592 token 'IF' iftok
593 token 'IMPLEMENTATION' implementationtok
594 token 'IMPORT' importtok
595 token 'IN' intok
596 token 'LOOP' looptok
597 token 'MOD' modtok
598 token 'MODULE' moduletok
599 token 'NOT' nottok
600 token 'OF' oftok
601 token 'OR' ortok
602 token 'PACKEDSET' packedsettok
603 token 'POINTER' pointertok
604 token 'PROCEDURE' proceduretok
605 token 'QUALIFIED' qualifiedtok
606 token 'UNQUALIFIED' unqualifiedtok
607 token 'RECORD' recordtok
608 token 'REM' remtok
609 token 'REPEAT' repeattok
610 token 'RETRY' retrytok
611 token 'RETURN' returntok
612 token 'SET' settok
613 token 'THEN' thentok
614 token 'TO' totok
615 token 'TYPE' typetok
616 token 'UNTIL' untiltok
617 token 'VAR' vartok
618 token 'WHILE' whiletok
619 token 'WITH' withtok
620 token 'ASM' asmtok
621 token 'VOLATILE' volatiletok
622 token '...' periodperiodperiodtok
623 token '__DATE__' datetok
624 token '__LINE__' linetok
625 token '__FILE__' filetok
626 token '__ATTRIBUTE__' attributetok
627 token '__BUILTIN__' builtintok
628 token '__INLINE__' inlinetok
629 token 'integer number' integertok
630 token 'identifier' identtok
631 token 'real number' realtok
632 token 'string' stringtok
633
634 special Ident first { < identtok > } follow { }
635 special Integer first { < integertok > } follow { }
636 special Real first { < realtok > } follow { }
637 special string first { < stringtok > } follow { }
638
639 BNF
640
641 -- the following are provided by the module m2flex and also handbuild procedures below
642 -- Ident := Letter { ( Letter | Digit ) } =:
643 -- Integer := Digit { Digit } | OctalDigit { OctalDigit } ( " B " | " C " ) |
644 -- Digit { HexDigit } " H " =:
645 -- Real := Digit { Digit } " . " { Digit } [ ScaleFactor ] =:
646 -- ScaleFactor := " E " [ ( " + " | " - " ) ] Digit { Digit } =:
647 -- HexDigit := Digit | " A " | " B " | " C " | " D " | " E " | " F " =:
648 -- Digit := OctalDigit | " 8 " | " 9 " =:
649 -- OctalDigit := "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" =:
650 -- String
651
652 FileUnit := % PushAutoOff %
653 ( DefinitionModule |
654 ImplementationOrProgramModule ) % PopAuto %
655 =:
656
657 ProgramModule := % VAR modulet, endt: CARDINAL ; %
658 % modulet := GetTokenNo () %
659 "MODULE" % M2Error.DefaultProgramModule %
660 % PushAutoOn %
661 Ident % P3StartBuildProgModule %
662 % StartBuildModFile (modulet) %
663 % BuildModuleStart (modulet) %
664 % PushAutoOff %
665 [ Priority
666 ]
667 ";" % BuildScaffold (modulet,
668 GetCurrentModule ()) %
669 { Import }
670 Block % PushAutoOn %
671 % endt := GetTokenNo () -1 %
672 Ident % EndBuildFile (endt) %
673 % P3EndBuildProgModule %
674 "." % PopAuto ; PopAuto %
675 =:
676
677 ImplementationModule := % VAR modulet, endt: CARDINAL ; %
678 % modulet := GetTokenNo () %
679 "IMPLEMENTATION" % M2Error.DefaultImplementationModule %
680 "MODULE" % PushAutoOn %
681 Ident % StartBuildModFile (modulet) %
682 % P3StartBuildImpModule %
683 % BuildModuleStart (modulet) %
684 % PushAutoOff %
685 [ Priority
686 ] ";" % BuildScaffold (modulet,
687 GetCurrentModule ()) %
688 { Import }
689 Block % PushAutoOn %
690 % endt := GetTokenNo () -1 %
691 Ident % EndBuildFile (endt) %
692 % P3EndBuildImpModule %
693 "." % PopAuto ; PopAuto ; PopAuto %
694 =:
695
696 ImplementationOrProgramModule := % PushAutoOff %
697 ( ImplementationModule | ProgramModule ) % PopAuto %
698 =:
699
700 Number := Integer | Real =:
701
702 --
703 -- In pass 3 Qualident needs some care as we must only parse module.module.ident
704 -- and not ident.recordfield. We leave the ident.recordfield to be parsed by
705 -- SubDesignator. Note that Qualident is called by SubDesignator so if
706 -- IsAutoPushOff then we just consume tokens.
707 --
708
709 Qualident := Ident
710 % IF IsAutoPushOn()
711 THEN
712 CheckModuleQualident (stopset0, stopset1, stopset2)
713 ELSE (* just parse qualident *) %
714 { "." Ident } % END %
715 =:
716
717
718 QualidentCheck := % PushAutoOn %
719 Qualident
720 % PopAuto %
721 % CheckQualident (OperandTok (1), BlockState, OperandT (1)) %
722 % IF NOT IsAutoPushOn ()
723 THEN
724 PopNothing
725 END %
726 =:
727
728 ConstantDeclaration := % VAR tokno: CARDINAL ; %
729 % InclConst (BlockState) %
730 % PushAutoOn %
731 ( Ident "=" % tokno := GetTokenNo () -1 %
732 % BuildConst %
733 ConstExpression ) % BuildAssignConstant (tokno) %
734 % PopAuto %
735 % ExclConst (BlockState) %
736 =:
737
738 ConstExpression := % VAR tokpos: CARDINAL ; %
739 % PushInConstExpression %
740 % PushAutoOn %
741 SimpleConstExpr [ Relation % tokpos := GetTokenNo ()-1 %
742 SimpleConstExpr % BuildRelOp (tokpos) %
743 ] % PopAuto %
744 % PopInConstExpression %
745 =:
746
747 Relation := "=" % PushTtok(EqualTok, GetTokenNo() -1) ;
748 RecordOp %
749 | "#" % PushTtok(HashTok, GetTokenNo() -1) ;
750 RecordOp %
751 | "<>" % PushTtok(LessGreaterTok, GetTokenNo() -1) ;
752 RecordOp %
753 | "<" % PushTtok(LessTok, GetTokenNo() -1) %
754 | "<=" % PushTtok(LessEqualTok, GetTokenNo() -1) %
755 | ">" % PushTtok(GreaterTok, GetTokenNo() -1) %
756 | ">=" % PushTtok(GreaterEqualTok, GetTokenNo() -1) %
757 | "IN" % PushTtok(InTok, GetTokenNo() -1) %
758 =:
759
760 SimpleConstExpr := UnaryOrConstTerm { AddOperator ConstTerm % BuildBinaryOp %
761 } =:
762
763 UnaryOrConstTerm := "+" % PushTtok(PlusTok, GetTokenNo() -1) %
764 ConstTerm % BuildUnaryOp %
765 |
766 "-" % PushTtok(MinusTok, GetTokenNo() -1) %
767 ConstTerm % BuildUnaryOp %
768 |
769 ConstTerm =:
770
771 AddOperator := "+" % PushTtok(PlusTok, GetTokenNo() -1) ;
772 RecordOp %
773 | "-" % PushTtok(MinusTok, GetTokenNo() -1) ;
774 RecordOp %
775 | "OR" % PushTtok(OrTok, GetTokenNo() -1) ;
776 RecordOp %
777 =:
778
779 ConstTerm := ConstFactor { MulOperator ConstFactor % BuildBinaryOp %
780 } =:
781
782 MulOperator := "*" % PushTtok(TimesTok, GetTokenNo() -1) ;
783 RecordOp %
784 | "/" % PushTtok(DivideTok, GetTokenNo() -1) ;
785 RecordOp %
786 | "DIV" % PushTtok(DivTok, GetTokenNo() -1) ;
787 RecordOp %
788 | "MOD" % PushTtok(ModTok, GetTokenNo() -1) ;
789 RecordOp %
790 | "REM" % PushTtok(RemTok, GetTokenNo() -1) ;
791 RecordOp %
792 | "AND" % PushTtok(AndTok, GetTokenNo() -1) ;
793 RecordOp %
794 | "&" % PushTtok(AmbersandTok, GetTokenNo() -1) ;
795 RecordOp %
796 =:
797
798 ConstFactor := % VAR tokpos: CARDINAL ; %
799 Number | ConstString | ConstSetOrQualidentOrFunction |
800 "(" ConstExpression ")" | "NOT" % tokpos := GetTokenNo() -1 %
801 ConstFactor % BuildNot (tokpos) %
802 | ConstAttribute =:
803
804 -- to help satisfy LL1
805
806 ConstString := string =:
807
808 ComponentElement := ConstExpression ( ".." ConstExpression % PushTtok(PeriodPeriodTok, GetTokenNo() -1) %
809 | % PushT(NulTok) %
810 )
811 =:
812
813 ComponentValue := % VAR tokpos: CARDINAL ; %
814 (
815 % tokpos := GetTokenNo () %
816 ComponentElement ( % tokpos := GetTokenNo () %
817 'BY' ConstExpression % PushTtok (ByTok, tokpos) %
818
819 | % PushTtok (NulTok, tokpos) %
820 )
821 )
822 =:
823
824 ArraySetRecordValue := ComponentValue % BuildComponentValue %
825 { ',' % NextConstructorField %
826 ComponentValue % BuildComponentValue %
827 }
828 =:
829
830 Constructor := % VAR tokpos: CARDINAL ; %
831 % DisplayStack %
832 % InclConstructor (BlockState) %
833 % CheckQualident (OperandTok (1), BlockState, OperandT (1)) %
834 '{' % tokpos := GetTokenNo () -1 %
835 % BuildConstructorStart (tokpos) %
836 [ ArraySetRecordValue ] % BuildConstructorEnd (tokpos, GetTokenNo()) %
837 '}'
838 % ExclConstructor (BlockState) %
839 =:
840
841 ConstSetOrQualidentOrFunction := % VAR tokpos: CARDINAL ; %
842 % tokpos := GetTokenNo () %
843 (
844 Qualident
845 [ Constructor | ConstActualParameters % BuildConstFunctionCall %
846 ]
847 | % BuildTypeForConstructor (tokpos) %
848 Constructor
849 ) =:
850
851 ConstActualParameters := % PushState (BlockState) %
852 % InclConstFunc (BlockState) %
853 % CheckQualident (OperandTok (1), BlockState, OperandT (1)) %
854 % PushInConstParameters %
855 ActualParameters % PopInConstParameters %
856 % PopState (BlockState) %
857 =:
858
859 ConstAttribute := "__ATTRIBUTE__" "__BUILTIN__" "(" "(" % PushAutoOn %
860 ConstAttributeExpression % PopAuto %
861 ")" ")" =:
862
863 ConstAttributeExpression :=
864 Ident % BuildBuiltinConst %
865 | "<" Qualident ',' Ident % BuildBuiltinTypeInfo %
866 ">"
867 =:
868
869 ByteAlignment := % VAR tokpos: CARDINAL ; %
870 '<*' % PushAutoOn %
871 % tokpos := GetTokenNo () %
872 AttributeExpression % BuildAlignment (tokpos) %
873 '*>' % PopAuto %
874 =:
875
876 Alignment := [ ByteAlignment ] =:
877
878 TypeDeclaration := % InclType (BlockState) %
879 Ident "=" Type Alignment
880 % ExclType (BlockState) %
881 =:
882
883 Type :=
884 % PushAutoOff %
885 ( SimpleType | ArrayType
886 | RecordType
887 | SetType
888 | PointerType
889 | ProcedureType ) % PopAuto %
890 =:
891
892 SimpleType := QualidentCheck [ SubrangeType ] | Enumeration | SubrangeType =:
893
894 Enumeration := "("
895 ( IdentList
896 )
897 ")"
898 =:
899
900 IdentList := Ident % VAR
901 on: BOOLEAN ;
902 n : CARDINAL ; %
903 % on := IsAutoPushOn() ;
904 IF on
905 THEN
906 n := 1
907 END %
908 { "," Ident % IF on
909 THEN
910 INC(n)
911 END %
912 } % IF on
913 THEN
914 PushT(n)
915 END %
916 =:
917
918 SubrangeType := "[" ConstExpression ".." ConstExpression "]" % BuildSubrange ; %
919 =:
920
921 ArrayType := "ARRAY"
922
923 SimpleType
924 { ","
925 SimpleType
926 } "OF"
927 Type
928 =:
929
930 RecordType := "RECORD" [ DefaultRecordAttributes ] FieldListSequence "END" =:
931
932 DefaultRecordAttributes := '<*' % PushAutoOn %
933 AttributeExpression % BuildDefaultFieldAlignment %
934 % PopAuto %
935 '*>' =:
936
937 RecordFieldPragma := [ '<*' FieldPragmaExpression
938 { ',' FieldPragmaExpression } '*>' ] =:
939
940 FieldPragmaExpression := % PushAutoOn %
941 Ident PragmaConstExpression % BuildPragmaField %
942 % PopAuto %
943 =:
944
945 PragmaConstExpression := ( '(' ConstExpression ')' | % PushT(NulSym) %
946 % Annotate('NulSym||no pragma const') %
947 ) =:
948
949 AttributeExpression := Ident '(' ConstExpression ')' =:
950
951 FieldListSequence := FieldListStatement { ";" FieldListStatement } =:
952
953 -- at present FieldListStatement is as follows:
954 FieldListStatement := [ FieldList ] =:
955 -- later replace it with FieldList to comply with PIM2
956
957 -- sadly the PIM rules are not LL1 as Ident and Qualident have the same first
958 -- symbols. We rewrite FieldList to inline qualident
959 -- was
960 -- FieldList := IdentList ":" % BuildNulName %
961 -- Type |
962 -- "CASE" [ Ident ":" ] Qualident "OF" Varient { "|" Varient }
963 -- [ "ELSE" FieldListSequence ] "END" =:
964
965 FieldList := IdentList ":"
966 Type RecordFieldPragma
967 |
968 "CASE" % BeginVarient %
969 CaseTag "OF"
970 Varient { "|" Varient }
971 [ "ELSE" % ElseVarient %
972 FieldListSequence
973 ] "END" % EndVarient %
974 =:
975
976 TagIdent := [ Ident ] =:
977
978 CaseTag := TagIdent [":" QualidentCheck ] =:
979
980 Varient := [ % BeginVarientList %
981 VarientCaseLabelList ":" FieldListSequence % EndVarientList %
982 ] =:
983
984 VarientCaseLabelList := VarientCaseLabels { "," VarientCaseLabels } =:
985
986 VarientCaseLabels := ConstExpression ( ".." ConstExpression % AddVarientRange %
987 | % AddVarientEquality ; (* epsilon *) %
988 )
989 =:
990
991 --
992 -- the following rules are a copy of the ConstExpression ebnf rules but without
993 -- any actions all prefixed with Silent.
994 -- At present they are only used by CaseLabels, if this continues to be true we
995 -- might consider restricting the SilentConstExpression. Eg it makes no sence to allow
996 -- String in these circumstances!
997 --
998
999 SilentConstExpression := % PushAutoOff %
1000 SilentSimpleConstExpr
1001 [ SilentRelation SilentSimpleConstExpr ] % PopAuto %
1002 =:
1003
1004 SilentRelation := "=" | "#" | "<>" | "<" | "<=" | ">" | ">=" | "IN" =:
1005
1006 SilentSimpleConstExpr := SilentUnaryOrConstTerm { SilentAddOperator SilentConstTerm } =:
1007
1008 SilentUnaryOrConstTerm := "+" SilentConstTerm | "-" SilentConstTerm | SilentConstTerm =:
1009
1010 SilentAddOperator := "+" | "-" | "OR" =:
1011
1012 SilentConstTerm := SilentConstFactor { SilentMulOperator SilentConstFactor } =:
1013
1014 SilentMulOperator := "*" | "/" | "DIV" | "MOD" | "REM" | "AND" | "&" =:
1015
1016 SilentConstFactor := Number | SilentConstString | SilentConstSetOrQualidentOrFunction |
1017 "(" SilentConstExpression ")" | "NOT" SilentConstFactor
1018 | SilentConstAttribute =:
1019
1020 SilentConstString := string =:
1021
1022 SilentConstAttribute := "__ATTRIBUTE__" "__BUILTIN__" "(" "(" SilentConstAttributeExpression ")" ")" =:
1023
1024 SilentConstAttributeExpression := Ident | "<" Ident ',' SilentConstString ">" =:
1025
1026 SilentComponentElement := SilentConstExpression [ ".." SilentConstExpression ] =:
1027
1028 SilentComponentValue := SilentComponentElement [ 'BY' SilentConstExpression ] =:
1029
1030 SilentArraySetRecordValue := SilentComponentValue { ',' SilentComponentValue } =:
1031
1032 SilentConstructor := '{' % SilentBuildConstructorStart %
1033 [ SilentArraySetRecordValue ] '}' =:
1034
1035 SilentConstSetOrQualidentOrFunction := SilentConstructor | Qualident
1036 [ SilentConstructor | SilentActualParameters ] =:
1037
1038 SilentActualParameters := "(" [ SilentExpList ] ")" =:
1039
1040 SilentExpList := SilentConstExpression { "," SilentConstExpression } =:
1041
1042 -- end of the Silent constant rules
1043
1044 SetType := ( "SET" | "PACKEDSET" ) "OF" SimpleType =:
1045
1046 PointerType := "POINTER" "TO"
1047 Type
1048 =:
1049
1050 ProcedureType := "PROCEDURE"
1051 [ FormalTypeList ] =:
1052
1053 FormalTypeList := "(" ( ")" FormalReturn |
1054 ProcedureParameters ")" FormalReturn ) =:
1055
1056 FormalReturn := [ ":" OptReturnType ] =:
1057
1058 OptReturnType := "[" QualidentCheck "]" | QualidentCheck =:
1059
1060 ProcedureParameters := ProcedureParameter
1061 { "," ProcedureParameter } =:
1062
1063 ProcedureParameter := "..." | "VAR" FormalType | FormalType =:
1064
1065
1066 VarIdent := % VAR
1067 Sym, Type: CARDINAL ;
1068 on: BOOLEAN ; %
1069 % on := IsAutoPushOn() %
1070 % IF NOT on
1071 THEN
1072 PushAutoOn
1073 END %
1074 Ident % IF on
1075 THEN
1076 PopTF(Sym, Type) ;
1077 PushTF(Sym, Type) ;
1078 PushTF(Sym, Type)
1079 END %
1080 [ "[" ConstExpression % BuildVarAtAddress %
1081 "]" ]
1082 % PopNothing ;
1083 PopAuto %
1084 =:
1085
1086 VarIdentList := VarIdent % VAR
1087 on: BOOLEAN ;
1088 n : CARDINAL ; %
1089 % on := IsAutoPushOn() ;
1090 IF on
1091 THEN
1092 n := 1
1093 END %
1094 { "," VarIdent % IF on
1095 THEN
1096 INC(n)
1097 END %
1098 } % IF on
1099 THEN
1100 PushT(n)
1101 END %
1102 =:
1103
1104 VariableDeclaration := VarIdentList ":"
1105 % InclVar (BlockState) %
1106 Type Alignment
1107 % ExclVar (BlockState) %
1108 =:
1109
1110 Designator := QualidentCheck % CheckWithReference %
1111 { SubDesignator } =:
1112
1113 SubDesignator := "." % VAR Sym, Type, tok,
1114 dotpostok : CARDINAL ;
1115 name, n1 : Name ; %
1116 % dotpostok := GetTokenNo () -1 ;
1117 PopTFtok (Sym, Type, tok) ;
1118 Type := SkipType(Type) ;
1119 PushTFtok(Sym, Type, tok) ;
1120 IF Type=NulSym
1121 THEN
1122 n1 := GetSymName(Sym) ;
1123 IF IsModuleKnown(GetSymName(Sym))
1124 THEN
1125 WriteFormat2('%a looks like a module which has not been globally imported (eg. suggest that you IMPORT %a)',
1126 n1, n1)
1127 ELSE
1128 WriteFormat1('%a is not a record variable', n1)
1129 END
1130 ELSIF NOT IsRecord(Type)
1131 THEN
1132 MetaErrorT2 (tok, "the type of {%1ad} is not a record (but {%2ad}) and therefore it has no field", Sym, Type) ;
1133 END ;
1134 StartScope(Type) %
1135 Ident
1136 % PopTtok (name, tok) ;
1137 Sym := GetLocalSym(Type, name) ;
1138 IF Sym=NulSym
1139 THEN
1140 n1 := GetSymName(Type) ;
1141 WriteFormat2('field %a does not exist within record %a', name, n1)
1142 END ;
1143 Type := GetType(Sym) ;
1144 PushTFtok (Sym, Type, tok) ;
1145 EndScope ;
1146 PushT(1) ;
1147 BuildDesignatorRecord (dotpostok) %
1148 | "[" ArrayExpList
1149 "]"
1150 | "^" % BuildDesignatorPointer (GetTokenNo () -1) %
1151 =:
1152
1153 ArrayExpList :=
1154 Expression % BuildBooleanVariable %
1155 % BuildDesignatorArray %
1156 { ","
1157 Expression % BuildBooleanVariable %
1158 % BuildDesignatorArray %
1159 }
1160 =:
1161
1162 ExpList := % VAR n: CARDINAL ; %
1163 Expression % BuildBooleanVariable %
1164 % n := 1 %
1165 { ","
1166 Expression % BuildBooleanVariable %
1167 % INC(n) %
1168 }
1169 % PushT(n) %
1170 =:
1171
1172 Expression := % VAR tokpos: CARDINAL ; %
1173 % PushAutoOn %
1174 SimpleExpression [ Relation % tokpos := GetTokenNo ()-1 %
1175 SimpleExpression % BuildRelOp (tokpos) %
1176 ] % PopAuto %
1177 =:
1178
1179 SimpleExpression := UnaryOrTerm { AddOperator Term % BuildBinaryOp %
1180 } =:
1181
1182 UnaryOrTerm := "+" % PushTtok(PlusTok, GetTokenNo() -1) %
1183 Term % BuildUnaryOp %
1184 | "-" % PushTtok(MinusTok, GetTokenNo() -1) %
1185 Term % BuildUnaryOp %
1186 | Term =:
1187
1188 Term := Factor
1189 { MulOperator Factor % BuildBinaryOp %
1190 } =:
1191
1192 Factor := % VAR tokpos: CARDINAL ; %
1193 Number | string | SetOrDesignatorOrFunction |
1194 "(" Expression ")" | "NOT" % tokpos := GetTokenNo ()-1 %
1195 ( Factor % BuildNot (tokpos) %
1196 | ConstAttribute
1197 ) =:
1198
1199 SetOrDesignatorOrFunction := % VAR tokpos: CARDINAL ; %
1200 % tokpos := GetTokenNo () %
1201 (
1202 Qualident
1203 % Assert (OperandTok (1) # UnknownTokenNo) %
1204 % CheckWithReference %
1205 % Assert (OperandTok (1) # UnknownTokenNo) %
1206 [ Constructor |
1207 SimpleDes % (* Assert (OperandTok(1) # UnknownTokenNo) *) %
1208 [ ActualParameters % IF IsInConstParameters ()
1209 THEN
1210 BuildConstFunctionCall
1211 ELSE
1212 BuildFunctionCall (FALSE)
1213 END %
1214 ]
1215 ] |
1216 % BuildTypeForConstructor (tokpos) %
1217 Constructor ) =:
1218
1219 -- SimpleDes := { "." Ident | "[" ExpList "]" | "^" } =:
1220 SimpleDes := { SubDesignator } =:
1221
1222 ActualParameters := "(" % BuildSizeCheckStart %
1223 ( ExpList | % BuildNulParam %
1224 ) ")" =:
1225
1226 ExitStatement := "EXIT" % BuildExit %
1227 =:
1228
1229 ReturnStatement := "RETURN" % VAR tokno: CARDINAL ; %
1230 % tokno := GetTokenNo () -1 %
1231 ( Expression | % BuildNulExpression (tokno) %
1232 ) % BuildReturn (tokno) %
1233 =:
1234
1235 Statement := % BuildStmtNote (0) %
1236 % PushAutoOn ; DisplayStack %
1237 [ AssignmentOrProcedureCall | IfStatement | CaseStatement |
1238 WhileStatement | RepeatStatement | LoopStatement |
1239 ForStatement | WithStatement | AsmStatement |
1240 ExitStatement | ReturnStatement | RetryStatement
1241 ] % PopAuto ; %
1242 =:
1243
1244 RetryStatement := "RETRY" % BuildRetry (GetTokenNo () -1) %
1245 =:
1246
1247 AssignmentOrProcedureCall := % VAR isFunc: BOOLEAN ;
1248 tokno : CARDINAL ; %
1249 % DisplayStack %
1250 Designator
1251 % tokno := GetTokenNo () %
1252 ( ":="
1253 % (* PrintTokenNo (tokno) *) %
1254 Expression % BuildAssignment (tokno) %
1255 | % isFunc := CheckBuildFunction() %
1256 ( ActualParameters | % BuildNulParam (* in epsilon *) %
1257 ) % IF isFunc
1258 THEN
1259 BuildFunctionCall (FALSE) ;
1260 BuildAssignment (tokno)
1261 ELSE
1262 BuildProcedureCall (tokno - 1)
1263 END %
1264 ) % DisplayStack %
1265 =:
1266
1267 -- these two break LL1 as both start with a Designator
1268 -- ProcedureCall := Designator [ ActualParameters ] =:
1269 -- Assignment := Designator ":=" Expression =:
1270
1271 StatementSequence :=
1272 Statement
1273 { ";"
1274 Statement }
1275 =:
1276
1277 IfStatement := "IF"
1278 Expression
1279 "THEN" % BuildThenIf %
1280 % BuildStmtNote (-1) %
1281 StatementSequence
1282 { "ELSIF"
1283 % BuildElsif1 %
1284 % BuildStmtNote (-1) %
1285 Expression
1286 "THEN" % BuildThenIf %
1287 % BuildStmtNote (-1) %
1288 StatementSequence % BuildElsif2 %
1289 }
1290 [
1291 "ELSE" % BuildElse %
1292 % BuildStmtNote (-1) %
1293 StatementSequence ] "END" % BuildEndIf %
1294 % BuildStmtNote (-1) %
1295 =:
1296
1297 CaseStatement := "CASE"
1298 Expression % BuildCaseStart %
1299 "OF" Case { "|" Case }
1300 CaseEndStatement
1301 =:
1302
1303 CaseEndStatement := "END" % BuildStmtNote (-1) %
1304 % BuildCaseElse %
1305 % BuildCaseCheck %
1306 % BuildCaseEnd %
1307 | "ELSE" % BuildStmtNote (-1) %
1308 % BuildCaseElse %
1309 % ElseCase (NulSym) %
1310 StatementSequence % BuildStmtNote (0) %
1311 "END"
1312 % BuildCaseEnd %
1313 =:
1314
1315 Case := [ % BuildStmtNote (-1) %
1316 CaseLabelList % BuildCaseStartStatementSequence %
1317 ":"
1318 StatementSequence % BuildCaseEndStatementSequence %
1319 % EndCaseList %
1320 ]
1321 =:
1322
1323 CaseLabelList := % BeginCaseList(NulSym) %
1324 CaseLabels { "," % BuildCaseOr %
1325 CaseLabels } =:
1326
1327 CaseLabels := ConstExpression ( ".." ConstExpression % BuildCaseRange ;
1328 BuildCaseList %
1329 | % BuildCaseEquality ; (* epsilon *)
1330 BuildCaseList %
1331 ) =:
1332
1333 WhileStatement := "WHILE" % BuildWhile %
1334 % BuildStmtNote (0) %
1335 Expression
1336 % BuildStmtNote (0) %
1337 "DO" % BuildDoWhile %
1338 StatementSequence % BuildStmtNote (0) %
1339 "END" % DisplayStack ; BuildEndWhile (-1) %
1340 =:
1341
1342 RepeatStatement := "REPEAT"
1343 % BuildRepeat %
1344 StatementSequence % BuildStmtNote (0) %
1345 "UNTIL"
1346 Expression % BuildUntil %
1347 =:
1348
1349 ForStatement := % VAR endpostok: CARDINAL ; %
1350 % PushLineNo %
1351 "FOR" Ident ":=" Expression "TO" Expression
1352 ( "BY" ConstExpression | % BuildPseudoBy (* epsilon *) %
1353 ) % PushLineNo %
1354 % BuildStmtNote (0) %
1355 "DO" % BuildForToByDo %
1356 StatementSequence % BuildStmtNote (0) %
1357 % endpostok := GetTokenNo () %
1358 "END" % BuildEndFor (endpostok) %
1359 =:
1360
1361 LoopStatement := "LOOP"
1362 % BuildLoop %
1363 StatementSequence % BuildStmtNote (0) %
1364 "END" % BuildEndLoop %
1365 =:
1366
1367 WithStatement := % VAR
1368 tok: CARDINAL ; %
1369 "WITH" % tok := GetTokenNo () -1 %
1370 Designator % StartBuildWith (tok) %
1371 % BuildStmtNote (0) %
1372 "DO"
1373 StatementSequence
1374 % BuildStmtNote (0) %
1375 "END" % EndBuildWith %
1376 =:
1377
1378 ProcedureDeclaration := % VAR top: CARDINAL ; %
1379 % top := Top () %
1380 ProcedureHeading ";" PostProcedureHeading % Assert (top = Top ()) %
1381 =:
1382
1383 PostProcedureHeading := ProperProcedure | ForwardDeclaration =:
1384
1385 ForwardDeclaration := "FORWARD" % EndBuildForward %
1386 =:
1387
1388 ProperProcedure := ProcedureBlock % BuildProcedureEnd ;
1389 PushAutoOn %
1390 Ident % EndBuildProcedure ;
1391 PopAuto %
1392 =:
1393
1394 DefineBuiltinProcedure := [ "__ATTRIBUTE__" "__BUILTIN__"
1395 "(" "(" % PushAutoOff %
1396 Ident % PopAuto %
1397 ")" ")" | "__INLINE__" ]
1398 =:
1399
1400 ProcedureHeading := "PROCEDURE" % M2Error.DefaultProcedure %
1401 % PushAutoOn %
1402 DefineBuiltinProcedure
1403 ( Ident
1404 % StartBuildProcedure ;
1405 PushAutoOff %
1406 [ FormalParameters ] AttributeNoReturn
1407 % BuildProcedureHeading ;
1408 PopAuto %
1409 ) % PopAuto %
1410 =:
1411
1412 Builtin := [ "__BUILTIN__" | "__INLINE__" ] =:
1413
1414 DefProcedureHeading := "PROCEDURE" % M2Error.DefaultProcedure %
1415 % PushAutoOn %
1416 Builtin
1417 ( Ident
1418 % StartBuildProcedure ;
1419 PushAutoOff %
1420 [ DefFormalParameters ] AttributeNoReturn
1421 % BuildProcedureHeading ;
1422 PopAuto %
1423 ) % PopAuto %
1424 % M2Error.LeaveErrorScope %
1425 =:
1426
1427 AttributeNoReturn := [ "<*" Ident "*>" ] =:
1428
1429 AttributeUnused := [ "<*" Ident "*>" ] =:
1430
1431 -- introduced procedure block so we can produce more informative
1432 -- error messages
1433
1434 ProcedureBlock := % BuildProcedureStart %
1435 { Declaration } % BuildProcedureBegin %
1436 [ "BEGIN" % BuildStmtNote (-1) %
1437 ProcedureBlockBody ] % BuildStmtNote (0) %
1438 "END"
1439 =:
1440
1441 Block := { Declaration }
1442 % StartBuildInit (GetTokenNo ()) %
1443 InitialBlock % EndBuildInit (GetTokenNo ()) ;
1444 StartBuildFinally (GetTokenNo ()) %
1445 FinalBlock % EndBuildFinally (GetTokenNo ()) %
1446 "END"
1447 =:
1448
1449 InitialBlock := [ "BEGIN" % BuildStmtNote (-1) %
1450 InitialBlockBody ] =:
1451
1452 FinalBlock := [ "FINALLY" % BuildStmtNote (-1) %
1453 FinalBlockBody ] =:
1454
1455 InitialBlockBody := NormalPart [
1456 "EXCEPT" % BuildStmtNote (-1) %
1457 % BuildExceptInitial (GetTokenNo() -1) %
1458 ExceptionalPart ] =:
1459
1460 FinalBlockBody := NormalPart [
1461 "EXCEPT" % BuildStmtNote (-1) %
1462 % BuildExceptFinally (GetTokenNo() -1) %
1463 ExceptionalPart ] =:
1464
1465 ProcedureBlockBody := NormalPart [
1466 "EXCEPT" % BuildStmtNote (-1) %
1467 % BuildExceptProcedure (GetTokenNo() -1) %
1468 ExceptionalPart ] =:
1469
1470 NormalPart := StatementSequence =:
1471
1472 ExceptionalPart := StatementSequence % BuildReThrow (GetTokenNo()) %
1473 =:
1474
1475 Declaration := "CONST" { ConstantDeclaration ";" } |
1476 "TYPE" { TypeDeclaration ";" } |
1477 "VAR" { VariableDeclaration ";" } |
1478 ProcedureDeclaration ";" |
1479 ModuleDeclaration ";" =:
1480
1481 DefFormalParameters := "(" [ DefMultiFPSection ] ")" FormalReturn =:
1482
1483 DefMultiFPSection := DefExtendedFP |
1484 FPSection [ ";" DefMultiFPSection ] =:
1485
1486 FormalParameters := "(" [ MultiFPSection ] ")" FormalReturn =:
1487
1488 MultiFPSection := ExtendedFP |
1489 FPSection [ ";" MultiFPSection ] =:
1490
1491 FPSection := NonVarFPSection | VarFPSection =:
1492
1493 DefExtendedFP := DefOptArg | "..." =:
1494
1495 ExtendedFP := OptArg | "..." =:
1496
1497 VarFPSection := "VAR" IdentList ":" FormalType [ AttributeUnused ] =:
1498
1499 NonVarFPSection := IdentList ":" FormalType [ AttributeUnused ] =:
1500
1501 OptArg := "[" Ident ":" FormalType [ "=" ConstExpression % BuildOptArgInitializer %
1502 ] "]" =:
1503
1504 DefOptArg := "[" Ident ":" FormalType "=" ConstExpression % BuildOptArgInitializer %
1505 "]" =:
1506
1507 FormalType := { "ARRAY" "OF" } QualidentCheck =:
1508
1509 ModuleDeclaration := % VAR modulet: CARDINAL ; %
1510 % modulet := GetTokenNo () %
1511 "MODULE" % M2Error.DefaultInnerModule %
1512 % PushAutoOn %
1513 Ident % StartBuildInnerModule %
1514 % BuildModuleStart (modulet) ;
1515 PushAutoOff %
1516 [ Priority ] ";"
1517 { Import } [ Export ]
1518 Block % PushAutoOn %
1519 Ident % EndBuildInnerModule %
1520 % PopAuto ; PopAuto ; PopAuto %
1521 =:
1522
1523 Priority := "[" % PushAutoOn %
1524 ConstExpression % BuildModulePriority ;
1525 PopAuto %
1526 "]" =:
1527
1528 Export := "EXPORT" ( "QUALIFIED"
1529 IdentList |
1530 "UNQUALIFIED"
1531 IdentList |
1532 IdentList ) ";" =:
1533
1534 FromImport := % PushAutoOn %
1535 "FROM" Ident "IMPORT" IdentList ";" % CheckImportListOuterModule %
1536 % PopAuto %
1537 =:
1538
1539 WithoutFromImport := % PushAutoOff %
1540 "IMPORT" IdentList ";"
1541 % PopAuto %
1542 =:
1543
1544 Import := FromImport | WithoutFromImport =:
1545
1546 DefinitionModule := % VAR deft, endt: CARDINAL ; %
1547 % deft := GetTokenNo () %
1548 "DEFINITION" % M2Error.DefaultDefinitionModule %
1549 "MODULE" % PushAutoOn %
1550 [ "FOR" string ]
1551 Ident % StartBuildDefFile (deft) ;
1552 P3StartBuildDefModule ;
1553 PushAutoOff %
1554 ";"
1555 { Import } [ Export
1556 ]
1557 { Definition } % endt := GetTokenNo () %
1558 "END" % PushAutoOn %
1559 Ident % EndBuildFile (endt) ;
1560 P3EndBuildDefModule %
1561 "." % PopAuto ; PopAuto ; PopAuto %
1562 =:
1563
1564 Definition := "CONST" { ConstantDeclaration ";" } |
1565 "TYPE"
1566 { Ident ( ";"
1567 | "=" Type Alignment ";" )
1568 }
1569 |
1570 "VAR" { VariableDeclaration ";" } |
1571 DefProcedureHeading ";" =:
1572
1573 AsmStatement := % VAR CurrentAsm: CARDINAL ;
1574 tok: CARDINAL ; %
1575 % tok := GetTokenNo () %
1576 'ASM' % PushAutoOn ;
1577 PushT (0) ; (* operand count *)
1578 PushT (MakeGnuAsm ())
1579 %
1580 [ 'VOLATILE' % PopT (CurrentAsm) ;
1581 PutGnuAsmVolatile (CurrentAsm) ;
1582 PushT (CurrentAsm)
1583 %
1584 ] '(' AsmOperands % PopNothing ; (* throw away interface sym *)
1585 BuildAsm (tok) ;
1586 PopNothing ; (* throw away count *)
1587 PopAuto
1588 %
1589 ')' =:
1590
1591 AsmOperands := % VAR CurrentAsm, count: CARDINAL ;
1592 str: CARDINAL ;
1593 %
1594 ConstExpression % PopT (str) ;
1595 PopT (CurrentAsm) ;
1596 Assert (IsGnuAsm (CurrentAsm) OR IsGnuAsmVolatile (CurrentAsm)) ;
1597 PopT (count) ;
1598 IF DebugAsm
1599 THEN
1600 printf1 ('1: count of asm operands: %d\n', count)
1601 END ;
1602 PushT (count) ;
1603 (* adds the name/instruction for this asm *)
1604 PutGnuAsm (CurrentAsm, str) ;
1605 PushT (CurrentAsm) ;
1606 PushT (NulSym) (* the InterfaceSym *)
1607 %
1608 ( AsmOperandSpec | % (* epsilon *)
1609 PutGnuAsmSimple (CurrentAsm)
1610 %
1611 )
1612 =:
1613
1614 AsmOperandSpec := % VAR CurrentAsm, outputs, inputs, trash, count: CARDINAL ;
1615 %
1616 ':' AsmOutputList % PopT(outputs) ;
1617 PopT(CurrentAsm) ;
1618 Assert(IsGnuAsm(CurrentAsm) OR IsGnuAsmVolatile(CurrentAsm)) ;
1619 PopT(count) ;
1620 IF DebugAsm
1621 THEN
1622 printf1('2: output count of asm operands: %d\n', count)
1623 END ;
1624 PutGnuAsmOutput(CurrentAsm, outputs) ;
1625 PushT(0) ; (* reset count *)
1626 PushT(CurrentAsm) ;
1627 PushT(NulSym) (* the InterfaceSym *)
1628 %
1629 [ ':' AsmInputList % PopT(inputs) ;
1630 PopT(CurrentAsm) ;
1631 Assert(IsGnuAsm(CurrentAsm) OR IsGnuAsmVolatile(CurrentAsm)) ;
1632 PopT(count) ;
1633 IF DebugAsm
1634 THEN
1635 printf1('3: input count of asm operands: %d\n', count)
1636 END ;
1637 PutGnuAsmInput(CurrentAsm, inputs) ;
1638 PushT(0) ; (* reset count *)
1639 PushT(CurrentAsm) ;
1640 PushT(NulSym) (* the InterfaceSym *)
1641 %
1642 [ ':' AsmTrashList % PopT(trash) ;
1643 PopT(CurrentAsm) ;
1644 Assert(IsGnuAsm(CurrentAsm) OR IsGnuAsmVolatile(CurrentAsm)) ;
1645 PopT(count) ;
1646 IF DebugAsm
1647 THEN
1648 printf1('4: trash count of asm operands: %d\n', count)
1649 END ;
1650 PutGnuAsmTrash(CurrentAsm, trash) ;
1651 PushT(0) ; (* reset count *)
1652 PushT(CurrentAsm) ;
1653 PushT(NulSym) (* the InterfaceSym *)
1654 %
1655 ] ]
1656 =:
1657
1658 AsmOutputList := [ AsmOutputElement ] { ',' AsmOutputElement } =:
1659
1660 AsmInputList := [ AsmInputElement ] { ',' AsmInputElement } =:
1661
1662 NamedOperand := '[' Ident ']' =:
1663
1664 AsmOperandName := ( NamedOperand
1665 | % IF IsAutoPushOn()
1666 THEN
1667 PushTF (NulName, identtok)
1668 END
1669 %
1670 )
1671 =:
1672
1673 AsmInputElement := AsmOperandName
1674 ConstExpression '(' Expression % BuildAsmElement (TRUE, FALSE)
1675 %
1676 ')'
1677 =:
1678
1679 AsmOutputElement := AsmOperandName
1680 ConstExpression '(' Expression % BuildAsmElement (FALSE, TRUE)
1681 %
1682 ')'
1683 =:
1684
1685 AsmTrashList := [ ConstExpression % BuildAsmTrash
1686 %
1687 ] { ',' ConstExpression % BuildAsmTrash
1688 %
1689 } =:
1690
1691 FNB