Debian Codegraph @

gcc/m2/mc/mcp4.bnf

raw
1 --
2 -- mc-4.bnf grammar and associated actions for mcp4.
3 --
4 -- Copyright (C) 2016-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 mcp4 begin
23 (* output from mc-4.bnf, automatically generated do not edit.
24
25 Copyright (C) 2016-2025 Free Software Foundation, Inc.
26 Contributed by Gaius Mulley <[email protected]>.
27
28 This file is part of GNU Modula-2.
29
30 GNU Modula-2 is free software; you can redistribute it and/or modify
31 it under the terms of the GNU General Public License as published by
32 the Free Software Foundation; either version 3, or (at your option)
33 any later version.
34
35 GNU Modula-2 is distributed in the hope that it will be useful, but
36 WITHOUT ANY WARRANTY; without even the implied warranty of
37 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38 General Public License for more details.
39
40 You should have received a copy of the GNU General Public License
41 along with GNU Modula-2; see the file COPYING. If not,
42 see <https://www.gnu.org/licenses/>. *)
43
44 IMPLEMENTATION MODULE mcp4 ;
45
46 FROM DynamicStrings IMPORT String, InitString, KillString, Mark,
47 ConCat, ConCatChar ;
48
49 FROM mcError IMPORT errorStringAt ;
50 FROM nameKey IMPORT NulName, Name, makekey ;
51 FROM mcPrintf IMPORT printf0, printf1 ;
52 FROM mcDebug IMPORT assert ;
53 FROM mcReserved IMPORT toktype ;
54 FROM mcMetaError IMPORT metaError1, metaError2 ;
55 FROM mcStack IMPORT stack ;
56
57 IMPORT mcStack ;
58
59 FROM mcLexBuf IMPORT currentstring, currenttoken, getToken, insertToken,
60 insertTokenAndRewind, getTokenNo ;
61
62 FROM decl IMPORT node, lookupDef, lookupImp, lookupModule, getSymName,
63 enterScope, leaveScope,
64 putType, lookupSym, isDef, makeSubrange,
65 makeSet, makePointer, makeProcType,
66 addParameter,
67 makeVarargs, makeVarParameter, makeNonVarParameter,
68 putSubrangeType, putConst,
69 makeArray, putUnbounded, getCardinal, makeBinaryTok, makeUnaryTok,
70 makeRecord, isRecord, isRecordField, isVarientField, makeVarient,
71 addFieldsToRecord, isVarient, buildVarientSelector,
72 buildVarientFieldRecord, makeVarDecl, paramEnter, paramLeave,
73 makeIdentList, putIdent, addVarParameters, addNonVarParameters,
74 lookupInScope, import, lookupExported, isImp, isModule, isConst,
75 makeLiteralInt, makeLiteralReal, makeString, getBuiltinConst,
76 getNextConstExp, fixupConstExp, makeFuncCall,
77 makeExpList, putExpList, isExpList,
78 makeSetValue, putSetValue, includeSetValue,
79 addCommentBody,
80 resetConstExpPos ;
81
82
83 CONST
84 Pass1 = FALSE ;
85 Debugging = FALSE ;
86
87 VAR
88 WasNoError : BOOLEAN ;
89 curstring,
90 curident : Name ;
91 curproc,
92 typeDes,
93 typeExp,
94 curmodule : node ;
95 stk : stack ;
96
97
98 (*
99 push -
100 *)
101
102 PROCEDURE push (n: node) : node ;
103 BEGIN
104 RETURN mcStack.push (stk, n)
105 END push ;
106
107
108 (*
109 pop -
110 *)
111
112 PROCEDURE pop () : node ;
113 BEGIN
114 RETURN mcStack.pop (stk)
115 END pop ;
116
117
118 (*
119 replace -
120 *)
121
122 PROCEDURE replace (n: node) : node ;
123 BEGIN
124 RETURN mcStack.replace (stk, n)
125 END replace ;
126
127
128 (*
129 peep - returns the top node on the stack without removing it.
130 *)
131
132 PROCEDURE peep () : node ;
133 BEGIN
134 RETURN push (pop ())
135 END peep ;
136
137
138 (*
139 depth - returns the depth of the stack.
140 *)
141
142 PROCEDURE depth () : CARDINAL ;
143 BEGIN
144 RETURN mcStack.depth (stk)
145 END depth ;
146
147
148 (*
149 checkDuplicate -
150 *)
151
152 PROCEDURE checkDuplicate (b: BOOLEAN) ;
153 BEGIN
154
155 END checkDuplicate ;
156
157
158 PROCEDURE ErrorString (s: String) ;
159 BEGIN
160 errorStringAt (s, getTokenNo ()) ;
161 WasNoError := FALSE
162 END ErrorString ;
163
164
165 PROCEDURE ErrorArray (a: ARRAY OF CHAR) ;
166 BEGIN
167 ErrorString (InitString (a))
168 END ErrorArray ;
169
170
171 (*
172 pushNunbounded -
173 *)
174
175 PROCEDURE pushNunbounded (c: CARDINAL) ;
176 VAR
177 type,
178 array,
179 subrange: node ;
180 BEGIN
181 WHILE c#0 DO
182 type := pop () ;
183 subrange := makeSubrange (NIL, NIL) ;
184 putSubrangeType (subrange, getCardinal ()) ;
185
186 array := makeArray (subrange, type) ;
187 putUnbounded (array) ;
188 type := push (array) ;
189 DEC (c)
190 END
191 END pushNunbounded ;
192
193
194 (*
195 makeIndexedArray - builds and returns an array of type, t, with, c, indices.
196 *)
197
198 PROCEDURE makeIndexedArray (c: CARDINAL; t: node) : node ;
199 VAR
200 i: node ;
201 BEGIN
202 WHILE c>0 DO
203 t := makeArray (pop (), t) ;
204 DEC (c)
205 END ;
206 RETURN t
207 END makeIndexedArray ;
208
209
210 (*
211 importInto - from, m, import, name, into module, current.
212 It checks to see if curident is an enumeration type
213 and if so automatically includes all enumeration fields
214 as well.
215 *)
216
217 PROCEDURE importInto (m: node; name: Name; current: node) ;
218 VAR
219 s, o: node ;
220 BEGIN
221 assert (isDef (m)) ;
222 assert (isDef (current) OR isModule (current) OR isImp (current)) ;
223 s := lookupExported (m, name) ;
224 IF s=NIL
225 THEN
226 metaError2 ('{%1k} was not exported from definition module {%2a}', name, m)
227 ELSE
228 o := import (current, s) ;
229 IF s#o
230 THEN
231 metaError2 ('{%1ad} cannot be imported into the current module as it causes a name clash with {%2ad}',
232 s, o)
233 END
234 END
235 END importInto ;
236
237
238 (*
239 checkEndName - if module does not have, name, then issue an error containing, desc.
240 *)
241
242 PROCEDURE checkEndName (module: node; name: Name; desc: ARRAY OF CHAR) ;
243 VAR
244 s: String ;
245 BEGIN
246 IF getSymName (module)#name
247 THEN
248 s := InitString ('inconsistent module name found with this ') ;
249 s := ConCat (s, Mark (InitString (desc))) ;
250 ErrorString (s)
251 END
252 END checkEndName ;
253
254 % declaration mcp4 begin
255
256
257 (*
258 SyntaxError - after a syntax error we skip all tokens up until we reach
259 a stop symbol.
260 *)
261
262 PROCEDURE SyntaxError (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
263 BEGIN
264 DescribeError ;
265 IF Debugging
266 THEN
267 printf0('\nskipping token *** ')
268 END ;
269 (*
270 yes the ORD(currenttoken) looks ugly, but it is *much* safer than
271 using currenttoken<sometok as a change to the ordering of the
272 token declarations below would cause this to break. Using ORD() we are
273 immune from such changes
274 *)
275 WHILE NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
276 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
277 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))
278 DO
279 getToken
280 END ;
281 IF Debugging
282 THEN
283 printf0(' ***\n')
284 END
285 END SyntaxError ;
286
287
288 (*
289 SyntaxCheck -
290 *)
291
292 PROCEDURE SyntaxCheck (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
293 BEGIN
294 (* and again (see above re: ORD)
295 *)
296 IF NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
297 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
298 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))
299 THEN
300 SyntaxError (stopset0, stopset1, stopset2)
301 END
302 END SyntaxCheck ;
303
304
305 (*
306 WarnMissingToken - generates a warning message about a missing token, t.
307 *)
308
309 PROCEDURE WarnMissingToken (t: toktype) ;
310 VAR
311 s0 : SetOfStop0 ;
312 s1 : SetOfStop1 ;
313 s2 : SetOfStop2 ;
314 str: String ;
315 BEGIN
316 s0 := SetOfStop0{} ;
317 s1 := SetOfStop1{} ;
318 s2 := SetOfStop2{} ;
319 IF ORD(t)<32
320 THEN
321 s0 := SetOfStop0{t}
322 ELSIF ORD(t)<64
323 THEN
324 s1 := SetOfStop1{t}
325 ELSE
326 s2 := SetOfStop2{t}
327 END ;
328 str := DescribeStop (s0, s1, s2) ;
329
330 str := ConCat (InitString ('syntax error,'), Mark (str)) ;
331 errorStringAt (str, getTokenNo ())
332 END WarnMissingToken ;
333
334
335 (*
336 MissingToken - generates a warning message about a missing token, t.
337 *)
338
339 PROCEDURE MissingToken (t: toktype) ;
340 BEGIN
341 WarnMissingToken (t) ;
342 IF (t#identtok) AND (t#integertok) AND (t#realtok) AND (t#stringtok)
343 THEN
344 IF Debugging
345 THEN
346 printf0 ('inserting token\n')
347 END ;
348 insertToken (t)
349 END
350 END MissingToken ;
351
352
353 (*
354 CheckAndInsert -
355 *)
356
357 PROCEDURE CheckAndInsert (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) : BOOLEAN ;
358 BEGIN
359 IF ((ORD(t)<32) AND (t IN stopset0)) OR
360 ((ORD(t)>=32) AND (ORD(t)<64) AND (t IN stopset1)) OR
361 ((ORD(t)>=64) AND (t IN stopset2))
362 THEN
363 WarnMissingToken (t) ;
364 insertTokenAndRewind (t) ;
365 RETURN( TRUE )
366 ELSE
367 RETURN( FALSE )
368 END
369 END CheckAndInsert ;
370
371
372 (*
373 InStopSet
374 *)
375
376 PROCEDURE InStopSet (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) : BOOLEAN ;
377 BEGIN
378 IF ((ORD(t)<32) AND (t IN stopset0)) OR
379 ((ORD(t)>=32) AND (ORD(t)<64) AND (t IN stopset1)) OR
380 ((ORD(t)>=64) AND (t IN stopset2))
381 THEN
382 RETURN( TRUE )
383 ELSE
384 RETURN( FALSE )
385 END
386 END InStopSet ;
387
388
389 (*
390 PeepToken - peep token checks to see whether the stopset is satisfied by currenttoken
391 If it is not then it will insert a token providing the token
392 is one of ; ] ) } . OF END ,
393
394 if the stopset contains <identtok> then we do not insert a token
395 *)
396
397 PROCEDURE PeepToken (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
398 BEGIN
399 (* and again (see above re: ORD)
400 *)
401 IF (NOT (((ORD(currenttoken)<32) AND (currenttoken IN stopset0)) OR
402 ((ORD(currenttoken)>=32) AND (ORD(currenttoken)<64) AND (currenttoken IN stopset1)) OR
403 ((ORD(currenttoken)>=64) AND (currenttoken IN stopset2)))) AND
404 (NOT InStopSet(identtok, stopset0, stopset1, stopset2))
405 THEN
406 (* SyntaxCheck would fail since currentoken is not part of the stopset
407 we check to see whether any of currenttoken might be a commonly omitted token *)
408 IF CheckAndInsert(semicolontok, stopset0, stopset1, stopset2) OR
409 CheckAndInsert(rsbratok, stopset0, stopset1, stopset2) OR
410 CheckAndInsert(rparatok, stopset0, stopset1, stopset2) OR
411 CheckAndInsert(rcbratok, stopset0, stopset1, stopset2) OR
412 CheckAndInsert(periodtok, stopset0, stopset1, stopset2) OR
413 CheckAndInsert(oftok, stopset0, stopset1, stopset2) OR
414 CheckAndInsert(endtok, stopset0, stopset1, stopset2) OR
415 CheckAndInsert(commatok, stopset0, stopset1, stopset2)
416 THEN
417 END
418 END
419 END PeepToken ;
420
421
422 (*
423 Expect -
424 *)
425
426 PROCEDURE Expect (t: toktype; stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
427 BEGIN
428 IF currenttoken=t
429 THEN
430 getToken ;
431 IF Pass1
432 THEN
433 PeepToken(stopset0, stopset1, stopset2)
434 END
435 ELSE
436 MissingToken(t)
437 END ;
438 SyntaxCheck(stopset0, stopset1, stopset2)
439 END Expect ;
440
441
442 (*
443 CompilationUnit - returns TRUE if the input was correct enough to parse
444 in future passes.
445 *)
446
447 PROCEDURE CompilationUnit () : BOOLEAN ;
448 BEGIN
449 stk := mcStack.init () ;
450 WasNoError := TRUE ;
451 FileUnit(SetOfStop0{eoftok}, SetOfStop1{}, SetOfStop2{}) ;
452 mcStack.kill (stk) ;
453 RETURN WasNoError
454 END CompilationUnit ;
455
456
457 (*
458 Ident - error checking varient of Ident
459 *)
460
461 PROCEDURE Ident (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
462 BEGIN
463 curident := makekey (currentstring) ;
464 Expect(identtok, stopset0, stopset1, stopset2)
465 END Ident ;
466
467
468 (*
469 string -
470 *)
471
472 PROCEDURE string (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
473 BEGIN
474 curstring := makekey (currentstring) ;
475 Expect(stringtok, stopset0, stopset1, stopset2)
476 END string ;
477
478
479 (*
480 Integer -
481 *)
482
483 PROCEDURE Integer (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
484 VAR
485 n: node ;
486 BEGIN
487 n := push (makeLiteralInt (makekey (currentstring))) ;
488 Expect(integertok, stopset0, stopset1, stopset2)
489 END Integer ;
490
491
492 (*
493 Real -
494 *)
495
496 PROCEDURE Real (stopset0: SetOfStop0; stopset1: SetOfStop1; stopset2: SetOfStop2) ;
497 VAR
498 n: node ;
499 BEGIN
500 n := push (makeLiteralReal (makekey (currentstring))) ;
501 Expect(realtok, stopset0, stopset1, stopset2)
502 END Real ;
503
504 % module mcp4 end
505 END mcp4.
506 % rules
507 error 'ErrorArray' 'ErrorString'
508 tokenfunc 'currenttoken'
509
510 token '' eoftok -- internal token
511 token '+' plustok
512 token '-' minustok
513 token '*' timestok
514 token '/' dividetok
515 token ':=' becomestok
516 token '&' ambersandtok
517 token "." periodtok
518 token "," commatok
519 token ";" semicolontok
520 token '(' lparatok
521 token ')' rparatok
522 token '[' lsbratok -- left square brackets
523 token ']' rsbratok -- right square brackets
524 token '{' lcbratok -- left curly brackets
525 token '}' rcbratok -- right curly brackets
526 token '^' uparrowtok
527 token "'" singlequotetok
528 token '=' equaltok
529 token '#' hashtok
530 token '<' lesstok
531 token '>' greatertok
532 token '<>' lessgreatertok
533 token '<=' lessequaltok
534 token '>=' greaterequaltok
535 token '<*' ldirectivetok
536 token '*>' rdirectivetok
537 token '..' periodperiodtok
538 token ':' colontok
539 token '"' doublequotestok
540 token '|' bartok
541 token 'AND' andtok
542 token 'ARRAY' arraytok
543 token 'BEGIN' begintok
544 token 'BY' bytok
545 token 'CASE' casetok
546 token 'CONST' consttok
547 token 'DEFINITION' definitiontok
548 token 'DIV' divtok
549 token 'DO' dotok
550 token 'ELSE' elsetok
551 token 'ELSIF' elsiftok
552 token 'END' endtok
553 token 'EXCEPT' excepttok
554 token 'EXIT' exittok
555 token 'EXPORT' exporttok
556 token 'FINALLY' finallytok
557 token 'FOR' fortok
558 token 'FROM' fromtok
559 token 'IF' iftok
560 token 'IMPLEMENTATION' implementationtok
561 token 'IMPORT' importtok
562 token 'IN' intok
563 token 'LOOP' looptok
564 token 'MOD' modtok
565 token 'MODULE' moduletok
566 token 'NOT' nottok
567 token 'OF' oftok
568 token 'OR' ortok
569 token 'PACKEDSET' packedsettok
570 token 'POINTER' pointertok
571 token 'PROCEDURE' proceduretok
572 token 'QUALIFIED' qualifiedtok
573 token 'UNQUALIFIED' unqualifiedtok
574 token 'RECORD' recordtok
575 token 'REM' remtok
576 token 'REPEAT' repeattok
577 token 'RETRY' retrytok
578 token 'RETURN' returntok
579 token 'SET' settok
580 token 'THEN' thentok
581 token 'TO' totok
582 token 'TYPE' typetok
583 token 'UNTIL' untiltok
584 token 'VAR' vartok
585 token 'WHILE' whiletok
586 token 'WITH' withtok
587 token 'ASM' asmtok
588 token 'VOLATILE' volatiletok
589 token '...' periodperiodperiodtok
590 token '__DATE__' datetok
591 token '__LINE__' linetok
592 token '__FILE__' filetok
593 token '__ATTRIBUTE__' attributetok
594 token '__BUILTIN__' builtintok
595 token '__INLINE__' inlinetok
596 token 'integer number' integertok
597 token 'identifier' identtok
598 token 'real number' realtok
599 token 'string' stringtok
600
601 special Ident first { < identtok > } follow { }
602 special Integer first { < integertok > } follow { }
603 special Real first { < realtok > } follow { }
604 special string first { < stringtok > } follow { }
605
606 BNF
607
608 -- the following are provided by the module m2flex and also handbuild procedures below
609 -- Ident := Letter { ( Letter | Digit ) } =:
610 -- Integer := Digit { Digit } | OctalDigit { OctalDigit } ( " B " | " C " ) |
611 -- Digit { HexDigit } " H " =:
612 -- Real := Digit { Digit } " . " { Digit } [ ScaleFactor ] =:
613 -- ScaleFactor := " E " [ ( " + " | " - " ) ] Digit { Digit } =:
614 -- HexDigit := Digit | " A " | " B " | " C " | " D " | " E " | " F " =:
615 -- Digit := OctalDigit | " 8 " | " 9 " =:
616 -- OctalDigit := "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" =:
617 -- String
618
619 FileUnit := DefinitionModule | ImplementationOrProgramModule
620 =:
621
622 ProgramModule := "MODULE"
623 Ident % curmodule := lookupModule (curident) %
624 % enterScope (curmodule) %
625 % resetConstExpPos (curmodule) %
626 [ Priority
627 ]
628 ";"
629 { Import }
630 Block
631 Ident % checkEndName (curmodule, curident, 'program module') %
632 % leaveScope %
633
634 "."
635 =:
636
637 ImplementationModule := "IMPLEMENTATION" "MODULE"
638 Ident % curmodule := lookupImp (curident) %
639 % enterScope (lookupDef (curident)) %
640 % enterScope (curmodule) %
641 % resetConstExpPos (curmodule) %
642 [ Priority
643 ] ";"
644 { Import }
645 Block
646 Ident % checkEndName (curmodule, curident, 'implementation module') %
647 % leaveScope ; leaveScope %
648 "."
649 =:
650
651 ImplementationOrProgramModule := ImplementationModule | ProgramModule
652 =:
653
654 Number := Integer | Real =:
655
656 Qualident :=
657 Ident { "." Ident }
658 =:
659
660 ConstantDeclaration := % VAR d, e: node ; %
661 Ident % d := lookupSym (curident) %
662 "=" ConstExpression % e := pop () %
663 % assert (isConst (d)) %
664 % putConst (d, e) %
665 =:
666
667 ConstExpression := % VAR c, l, r: node ; op: toktype ; d: CARDINAL ; %
668 % d := depth () %
669 % c := push (getNextConstExp ()) %
670 SimpleConstExpr
671 % op := currenttoken %
672 [ Relation
673 SimpleConstExpr
674 % r := pop () %
675 % l := pop () %
676 % l := push (makeBinaryTok (op, l, r)) %
677 ]
678 % c := replace (fixupConstExp (c, pop ())) %
679 % assert (d+1 = depth ()) %
680 =:
681
682 Relation := "="
683 | "#"
684 | "<>"
685 | "<"
686 | "<="
687 | ">"
688 | ">="
689 | "IN"
690 =:
691
692 SimpleConstExpr := % VAR op: toktype ; n: node ; %
693 UnaryOrConstTerm % n := pop () %
694 { % op := currenttoken %
695 AddOperator ConstTerm % n := makeBinaryTok (op, n, pop ()) %
696 } % n := push (n) %
697 =:
698
699 UnaryOrConstTerm := % VAR n: node ; %
700 "+"
701 ConstTerm % n := push (makeUnaryTok (plustok, pop ())) %
702 |
703 "-"
704 ConstTerm % n := push (makeUnaryTok (minustok, pop ())) %
705 |
706 ConstTerm =:
707
708 AddOperator := "+"
709 | "-"
710 | "OR"
711 =:
712
713 ConstTerm := % VAR op: toktype ; n: node ; %
714 ConstFactor % n := pop () %
715 { % op := currenttoken %
716 MulOperator ConstFactor % n := makeBinaryTok (op, n, pop ()) %
717 } % n := push (n) %
718 =:
719
720 MulOperator := "*"
721 | "/"
722 | "DIV"
723 | "MOD"
724 | "REM"
725 | "AND"
726 | "&"
727 =:
728
729 NotConstFactor := "NOT" ConstFactor % VAR n: node ; %
730 % n := push (makeUnaryTok (nottok, pop ())) %
731 =:
732
733 ConstFactor := Number | ConstString | ConstSetOrQualidentOrFunction |
734 "(" ConstExpression ")" | NotConstFactor
735 | ConstAttribute =:
736
737 -- to help satisfy LL1
738
739 ConstString := string % VAR n: node ; %
740 % n := push (makeString (curstring)) %
741 =:
742
743 ConstComponentElement := ConstExpression % VAR l, h, n: node ; %
744 % l := pop () %
745 % h := NIL %
746 [ ".." ConstExpression % h := pop () %
747 % ErrorArray ('implementation restriction range is not allowed') %
748 ] % n := push (includeSetValue (pop (), l, h)) %
749 =:
750
751 ConstComponentValue := ConstComponentElement [ 'BY' % ErrorArray ('implementation restriction BY not allowed') %
752 ConstExpression ]
753 =:
754
755 ConstArraySetRecordValue := ConstComponentValue { ',' ConstComponentValue }
756 =:
757
758 ConstConstructor := '{' % VAR n: node ; %
759 % n := push (makeSetValue ()) %
760 [ ConstArraySetRecordValue ]
761 '}' =:
762
763 ConstSetOrQualidentOrFunction := % VAR q, p, n: node ; d: CARDINAL ; %
764 % d := depth () %
765 PushQualident
766 % assert (d+1 = depth ()) %
767 [ ConstConstructor % p := pop () %
768 % q := pop () %
769 % n := push (putSetValue (p, q)) %
770 % assert (d+1 = depth ()) %
771 | ConstActualParameters % p := pop () %
772 % q := pop () %
773 % n := push (makeFuncCall (q, p)) %
774 % assert (d+1 = depth ()) %
775 ]
776 | % d := depth () %
777 ConstConstructor % assert (d+1 = depth ()) %
778 =:
779
780 ConstActualParameters := "(" % VAR n: node ; %
781 % n := push (makeExpList ()) %
782 [ ConstExpList ] ")" % assert (isExpList (peep ())) %
783 =:
784
785 ConstExpList := % VAR p, n: node ; %
786 % p := peep () %
787 % assert (isExpList (p)) %
788 ConstExpression % putExpList (p, pop ()) %
789 % assert (p = peep ()) %
790 % assert (isExpList (peep ())) %
791 { "," ConstExpression % putExpList (p, pop ()) %
792 % assert (isExpList (peep ())) %
793 }
794 =:
795
796 ConstAttribute := "__ATTRIBUTE__" "__BUILTIN__" "(" "("
797 ConstAttributeExpression
798 ")" ")" =:
799
800 ConstAttributeExpression :=
801 Ident % VAR n: node ; %
802 % n := push (getBuiltinConst (curident)) %
803 | "<" Qualident ',' Ident
804 ">"
805 =:
806
807 ByteAlignment := '<*' AttributeExpression '*>'
808 =:
809
810 OptAlignmentExpression := [ AlignmentExpression ] =:
811
812 AlignmentExpression := "(" ConstExpression ")" =:
813
814 Alignment := [ ByteAlignment ] =:
815
816 IdentList := Ident { "," Ident }
817 =:
818
819 PushIdentList := % VAR n: node ; %
820 % n := makeIdentList () %
821 Ident % checkDuplicate (putIdent (n, curident)) %
822 { "," Ident % checkDuplicate (putIdent (n, curident)) %
823 } % n := push (n) %
824 =:
825
826
827 SubrangeType := "[" ConstExpression ".." ConstExpression "]" =:
828
829 ArrayType := "ARRAY" SimpleType { "," SimpleType } "OF" Type =:
830
831 RecordType := "RECORD" [ DefaultRecordAttributes ]
832 FieldListSequence
833 "END" =:
834
835 DefaultRecordAttributes := '<*' AttributeExpression '*>' =:
836
837 RecordFieldPragma := [ '<*' FieldPragmaExpression
838 { ',' FieldPragmaExpression } '*>' ] =:
839
840 FieldPragmaExpression := Ident PragmaConstExpression =:
841
842 PragmaConstExpression := [ '(' ConstExpression ')' ] =:
843
844 AttributeExpression := Ident '(' ConstExpression ')' =:
845
846 FieldListSequence := FieldListStatement { ";" FieldListStatement } =:
847
848 FieldListStatement := [ FieldList ] =:
849
850 FieldList := IdentList ":" Type RecordFieldPragma
851 | "CASE" CaseTag "OF" Varient { "|" Varient }
852 [ "ELSE"
853 FieldListSequence
854 ] "END"
855 =:
856
857 TagIdent := Ident | % curident := NulName %
858 =:
859
860 CaseTag := TagIdent [ ":" Qualident ]
861 =:
862
863 Varient := [ VarientCaseLabelList ":" FieldListSequence ] =:
864
865 VarientCaseLabelList := VarientCaseLabels { "," VarientCaseLabels } =:
866
867 VarientCaseLabels := ConstExpression [ ".." ConstExpression ]
868 =:
869
870 SetType := ( "SET" | "PACKEDSET" ) "OF" SimpleType =:
871
872 PointerType := "POINTER" "TO" Type =:
873
874 ProcedureType := "PROCEDURE" [ FormalTypeList ] =:
875
876 FormalTypeList := "(" ( ")" FormalReturn |
877 ProcedureParameters ")" FormalReturn ) =:
878
879 FormalReturn := [ ":" OptReturnType ] =:
880
881 OptReturnType := "[" Qualident "]" | Qualident
882 =:
883
884 ProcedureParameters := ProcedureParameter
885 { "," ProcedureParameter } =:
886
887 ProcedureParameter := "..." | "VAR" FormalType | FormalType =:
888
889
890 VarIdent := Ident [ "[" ConstExpression % VAR n: node ; %
891 % n := pop () %
892 "]" ]
893 =:
894
895 VarIdentList := VarIdent { "," VarIdent }
896 =:
897
898 VariableDeclaration := VarIdentList ":"
899 Type Alignment
900 =:
901
902 Designator := Qualident
903 { SubDesignator } =:
904
905 SubDesignator := "."
906 Ident
907 | "[" ArrayExpList
908 "]"
909 | "^"
910 =:
911
912 ArrayExpList :=
913 Expression
914 { ","
915 Expression
916 }
917 =:
918
919 ExpList := Expression { "," Expression }
920 =:
921
922 Expression := SimpleExpression [ Relation SimpleExpression ]
923 =:
924
925 SimpleExpression := UnaryOrTerm { AddOperator Term } =:
926
927 UnaryOrTerm := "+" Term
928 | "-" Term
929 | Term
930 =:
931
932 Term := Factor { MulOperator Factor
933 } =:
934
935 Factor := Number | string | SetOrDesignatorOrFunction |
936 "(" Expression ")" | "NOT" ( Factor
937 | ConstAttribute
938 ) =:
939
940 ComponentElement := Expression
941 [ ".." Expression % ErrorArray ('implementation restriction range not allowed') %
942 ]
943 =:
944
945 ComponentValue := ComponentElement [ 'BY' % ErrorArray ('implementation restriction BY not allowed') %
946 Expression ]
947 =:
948
949 ArraySetRecordValue := ComponentValue { ',' ComponentValue }
950 =:
951
952 Constructor := '{' [ ArraySetRecordValue ] '}' =:
953
954 SetOrDesignatorOrFunction := Qualident
955 [ Constructor |
956 SimpleDes [ ActualParameters ]
957 ] |
958 Constructor =:
959
960 -- SimpleDes := { "." Ident | "[" ExpList "]" | "^" } =:
961 SimpleDes := { SubDesignator } =:
962
963 ActualParameters := "(" [ ExpList ] ")" =:
964
965 ExitStatement := "EXIT"
966 =:
967
968 ReturnStatement := "RETURN" [ Expression ]
969 =:
970
971 Statement := [ AssignmentOrProcedureCall | IfStatement | CaseStatement |
972 WhileStatement | RepeatStatement | LoopStatement |
973 ForStatement | WithStatement | AsmStatement |
974 ExitStatement | ReturnStatement | RetryStatement
975 ]
976 =:
977
978 RetryStatement := "RETRY"
979 =:
980
981 AssignmentOrProcedureCall := Designator
982 ( ":=" Expression
983 |
984 ActualParameters | % (* epsilon *) %
985 )
986 =:
987
988 -- these two break LL1 as both start with a Designator
989 -- ProcedureCall := Designator [ ActualParameters ] =:
990 -- Assignment := Designator ":=" Expression =:
991
992 StatementSequence := Statement { ";" Statement }
993 =:
994
995 IfStatement := "IF"
996 Expression "THEN"
997 StatementSequence
998 { "ELSIF"
999
1000 Expression "THEN"
1001 StatementSequence
1002 }
1003 [ "ELSE"
1004 StatementSequence ] "END"
1005 =:
1006
1007 CaseStatement := "CASE"
1008 Expression
1009 "OF" Case { "|" Case }
1010 CaseEndStatement
1011 =:
1012
1013 CaseEndStatement := "END"
1014 | "ELSE"
1015 StatementSequence "END"
1016 =:
1017
1018 Case := [ CaseLabelList ":" StatementSequence ]
1019 =:
1020
1021 CaseLabelList := CaseLabels { "," CaseLabels } =:
1022
1023 CaseLabels := ConstExpression [ ".." ConstExpression ]
1024 =:
1025
1026 WhileStatement := "WHILE" Expression "DO"
1027 StatementSequence
1028 "END"
1029 =:
1030
1031 RepeatStatement := "REPEAT"
1032 StatementSequence
1033 "UNTIL" Expression
1034 =:
1035
1036 ForStatement :=
1037 "FOR" Ident ":=" Expression "TO" Expression [ "BY" ConstExpression ] "DO"
1038 StatementSequence
1039 "END"
1040 =:
1041
1042 LoopStatement := "LOOP"
1043 StatementSequence
1044 "END"
1045 =:
1046
1047 WithStatement := "WITH" Designator "DO"
1048 StatementSequence
1049 "END"
1050 =:
1051
1052 ProcedureDeclaration := ProcedureHeading ";" ProcedureBlock
1053 Ident % leaveScope %
1054 =:
1055
1056 ProcedureIdent := Ident % curproc := lookupSym (curident) %
1057 % enterScope (curproc) %
1058 =:
1059
1060 DefProcedureIdent := Ident % curproc := lookupSym (curident) %
1061 =:
1062
1063 DefineBuiltinProcedure := [ "__ATTRIBUTE__" "__BUILTIN__" "(" "(" Ident ")" ")" | "__INLINE__" ]
1064 =:
1065
1066 ProcedureHeading := "PROCEDURE" DefineBuiltinProcedure ( ProcedureIdent
1067 [ FormalParameters ]
1068 AttributeNoReturn )
1069 =:
1070
1071 Builtin := [ "__BUILTIN__" | "__INLINE__" ] =:
1072
1073 DefProcedureHeading := "PROCEDURE" Builtin ( DefProcedureIdent
1074 [ DefFormalParameters ]
1075 AttributeNoReturn )
1076 =:
1077
1078 -- introduced procedure block so we can produce more informative
1079 -- error messages
1080
1081 ProcedureBlock := { Declaration } [ "BEGIN" ProcedureBlockBody ] "END"
1082 =:
1083
1084 Block := { Declaration } InitialBlock FinalBlock "END"
1085 =:
1086
1087 InitialBlock := [ "BEGIN" InitialBlockBody ] =:
1088
1089 FinalBlock := [ "FINALLY" FinalBlockBody ] =:
1090
1091 InitialBlockBody := NormalPart [ "EXCEPT" ExceptionalPart ] =:
1092
1093 FinalBlockBody := NormalPart [ "EXCEPT" ExceptionalPart ] =:
1094
1095 ProcedureBlockBody := NormalPart [ "EXCEPT" ExceptionalPart ] =:
1096
1097 NormalPart := StatementSequence =:
1098
1099 ExceptionalPart := StatementSequence
1100 =:
1101
1102 Declaration := "CONST" { ConstantDeclaration ";" } |
1103 "TYPE" { TypeDeclaration } |
1104 "VAR" { VariableDeclaration ";" } |
1105 ProcedureDeclaration ";" |
1106 ModuleDeclaration ";" =:
1107
1108 DefFormalParameters := "(" % paramEnter (curproc) %
1109 [ DefMultiFPSection ] ")" % paramLeave (curproc) %
1110 FormalReturn =:
1111
1112 DefMultiFPSection := DefExtendedFP | FPSection [ ";" DefMultiFPSection ] =:
1113
1114 FormalParameters := "(" % paramEnter (curproc) %
1115 [ MultiFPSection ] ")" % paramLeave (curproc) %
1116 FormalReturn =:
1117
1118 AttributeNoReturn := [ "<*" Ident "*>" ] =:
1119
1120 AttributeUnused := [ "<*" Ident "*>" ] =:
1121
1122 MultiFPSection := ExtendedFP | FPSection [ ";" MultiFPSection ] =:
1123
1124 FPSection := NonVarFPSection | VarFPSection =:
1125
1126 DefExtendedFP := DefOptArg | "..." =:
1127
1128 ExtendedFP := OptArg | "..." =:
1129
1130 VarFPSection := "VAR" PushIdentList ":" FormalType [ AttributeUnused ] =:
1131
1132 NonVarFPSection := PushIdentList ":" FormalType [ AttributeUnused ] =:
1133
1134 OptArg := "[" Ident ":" FormalType [ "=" ConstExpression ] "]" =:
1135
1136 DefOptArg := "[" Ident ":" FormalType "=" ConstExpression "]" =:
1137
1138 FormalType := { "ARRAY" "OF" } PushQualident =:
1139
1140 ModuleDeclaration := "MODULE" Ident [ Priority ] ";"
1141 { Import } [ Export ]
1142 Block Ident
1143 =:
1144
1145 Priority := "[" ConstExpression "]" =:
1146
1147 Export := "EXPORT" ( "QUALIFIED"
1148 IdentList |
1149 "UNQUALIFIED"
1150 IdentList |
1151 IdentList ) ";" =:
1152
1153 FromIdentList := Ident { "," Ident } =:
1154
1155 FromImport := "FROM" Ident "IMPORT" FromIdentList ";"
1156 =:
1157
1158 ImportModuleList := Ident { "," Ident } =:
1159
1160 WithoutFromImport := "IMPORT" ImportModuleList ";"
1161 =:
1162
1163 Import := FromImport | WithoutFromImport =:
1164
1165 DefinitionModule := "DEFINITION" "MODULE" [ "FOR" string ] Ident % curmodule := lookupDef (curident) %
1166 % addCommentBody (curmodule) %
1167 ";"
1168 % enterScope (curmodule) %
1169 % resetConstExpPos (curmodule) %
1170 { Import } [ Export ]
1171 { Definition }
1172 "END" Ident "." % checkEndName (curmodule, curident, 'definition module') %
1173 % leaveScope %
1174 =:
1175
1176 PushQualident := Ident % typeExp := push (lookupSym (curident)) %
1177 % IF typeExp = NIL
1178 THEN
1179 metaError1 ('the symbol {%1k} is not visible in this scope (or any other nested scope)', curident)
1180 END %
1181 [ "."
1182 % IF NOT isDef (typeExp)
1183 THEN
1184 ErrorArray ('the first component of this qualident must be a definition module')
1185 END %
1186 Ident % typeExp := replace (lookupInScope (typeExp, curident)) ;
1187 IF typeExp=NIL
1188 THEN
1189 ErrorArray ('identifier not found in definition module')
1190 END %
1191 ]
1192 =:
1193
1194 OptSubrange := [ SubrangeType ] =:
1195
1196 TypeEquiv := PushQualident OptSubrange =:
1197
1198 EnumIdentList := Ident { "," Ident } =:
1199
1200 Enumeration := "(" EnumIdentList ")" =:
1201
1202 SimpleType := TypeEquiv | Enumeration | SubrangeType =:
1203
1204 Type := SimpleType | ArrayType | RecordType | SetType |
1205 PointerType | ProcedureType
1206 =:
1207
1208 TypeDeclaration := { Ident ( ";" | "=" Type Alignment ";" ) } =:
1209
1210
1211 DefQualident :=
1212 Ident % typeExp := lookupSym (curident) %
1213 [ "." % IF NOT isDef (typeExp)
1214 THEN
1215 ErrorArray ('the first component of this qualident must be a definition module')
1216 END %
1217 Ident % typeExp := lookupInScope (typeExp, curident) ;
1218 IF typeExp=NIL
1219 THEN
1220 ErrorArray ('identifier not found in definition module')
1221 END %
1222 ]
1223 =:
1224
1225 DefTypeEquiv := DefQualident OptSubrange =:
1226
1227 DefEnumIdentList := Ident { "," Ident } =:
1228
1229 DefEnumeration := "(" DefEnumIdentList ")" =:
1230
1231 DefSimpleType := DefTypeEquiv | DefEnumeration | SubrangeType
1232 =:
1233
1234 DefType := DefSimpleType | ArrayType | RecordType | SetType |
1235 PointerType | ProcedureType
1236 =:
1237
1238 DefTypeDeclaration := { Ident ( ";" | "=" DefType Alignment ";" ) } =:
1239
1240 DefConstantDeclaration := Ident "=" ConstExpression =:
1241
1242 Definition := "CONST" { DefConstantDeclaration ";" } |
1243 "TYPE" { DefTypeDeclaration } |
1244 "VAR" { VariableDeclaration ";" } |
1245 DefProcedureHeading ";" =:
1246
1247 AsmStatement := 'ASM' [ 'VOLATILE' ] '(' AsmOperands ')' =:
1248
1249 AsmOperands := string [ AsmOperandSpec ]
1250 =:
1251
1252 AsmOperandSpec := [ ':' AsmList [ ':' AsmList [ ':' TrashList ] ] ]
1253 =:
1254
1255 AsmList := [ AsmElement ] { ',' AsmElement } =:
1256
1257 NamedOperand := '[' Ident ']' =:
1258
1259 AsmOperandName := [ NamedOperand ]
1260 =:
1261
1262 AsmElement := AsmOperandName string '(' Expression ')'
1263 =:
1264
1265 TrashList := [ string ] { ',' string } =:
1266
1267 FNB