Debian Codegraph @

gcc/m2/gm2-compiler/M2LexBuf.mod

raw
1 (* M2LexBuf.mod provides a buffer for m2.lex.
2
3 Copyright (C) 2001-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE M2LexBuf ;
23
24 IMPORT m2flex ;
25 IMPORT FIO ;
26
27 FROM libc IMPORT strlen ;
28 FROM SYSTEM IMPORT ADDRESS, ADR ;
29 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
30 FROM DynamicStrings IMPORT string, InitString, InitStringCharStar, Equal, Mark, KillString ;
31 FROM FormatStrings IMPORT Sprintf1 ;
32 FROM NameKey IMPORT NulName, Name, makekey, MakeKey, KeyToCharStar ;
33 FROM M2Reserved IMPORT toktype, tokToTok ;
34 FROM M2Printf IMPORT printf0, printf1, printf2, printf3 ;
35 FROM M2Debug IMPORT Assert ;
36 FROM NameKey IMPORT makekey ;
37 FROM NumberIO IMPORT CardToStr ;
38 FROM gcctypes IMPORT location_t ;
39 FROM m2linemap IMPORT GetLocationBinary ;
40 FROM M2Emit IMPORT UnknownLocation, BuiltinsLocation ;
41 FROM M2Error IMPORT WarnStringAt ;
42 FROM M2MetaError IMPORT MetaErrorT0 ;
43 FROM M2Options IMPORT GetDebugTraceToken ;
44 FROM M2LangDump IMPORT GetDumpFile ;
45
46 FROM Indexing IMPORT Index, InitIndex, GetIndice, PutIndice,
47 KillIndex, ForeachIndiceInIndexDo,
48 LowIndice, HighIndice, IsEmpty, InBounds,
49 InitIndexTuned ;
50
51
52 CONST
53 Tracing = FALSE ;
54 Debugging = FALSE ;
55 DebugRecover = FALSE ;
56 BadTokenNo = 32579 ;
57 InitialSourceToken = 2 ; (* 0 is unknown, 1 is builtin. *)
58
59 TYPE
60 SourceList = POINTER TO RECORD
61 left,
62 right: SourceList ;
63 name : String ;
64 line : CARDINAL ;
65 col : CARDINAL ;
66 END ;
67
68 TokenDesc = POINTER TO RECORD
69 token : toktype ;
70 str : Name ; (* ident name or string literal. *)
71 int : INTEGER ;
72 line : CARDINAL ;
73 col : CARDINAL ;
74 file : SourceList ;
75 loc : location_t ;
76 insert: Index ; (* Contains any inserted tokens. *)
77 END ;
78
79 VAR
80 CurrentSource : SourceList ;
81 UseBufferedTokens,
82 CurrentUsed : BOOLEAN ;
83 ListOfTokens : Index ;
84 CurrentTokNo : CARDINAL ;
85 InsertionIndex : CARDINAL ;
86 SeenEof : BOOLEAN ; (* Have we seen eof since the last call
87 to OpenSource. *)
88
89
90 PROCEDURE stop ;
91 END stop ;
92
93
94 (*
95 InitTokenDesc - returns a TokenDesc filled in with the parameters and
96 the insert field set to NIL.
97 *)
98
99 PROCEDURE InitTokenDesc (token: toktype; str: Name; int: INTEGER;
100 line, col: CARDINAL;
101 file: SourceList; loc: location_t) : TokenDesc ;
102 VAR
103 tokdesc: TokenDesc ;
104 BEGIN
105 NEW (tokdesc) ;
106 tokdesc^.token := token ;
107 tokdesc^.str := str ;
108 tokdesc^.int := int ;
109 tokdesc^.line := line ;
110 tokdesc^.col := col ;
111 tokdesc^.file := file ;
112 tokdesc^.loc := loc ;
113 tokdesc^.insert := NIL ;
114 RETURN tokdesc
115 END InitTokenDesc ;
116
117
118 (*
119 DeleteTokenDesc - delete tokdesc and any sub indices.
120 *)
121
122 PROCEDURE DeleteTokenDesc (tokdesc: TokenDesc) ;
123 BEGIN
124 IF tokdesc^.insert # NIL
125 THEN
126 ForeachIndiceInIndexDo (tokdesc^.insert, DeleteTokenDesc)
127 END ;
128 DISPOSE (tokdesc)
129 END DeleteTokenDesc ;
130
131
132 (*
133 Append - appends tokdesc to the end of the list defined by index.
134 *)
135
136 PROCEDURE Append (index: Index; tokdesc: TokenDesc) ;
137 BEGIN
138 IF IsEmpty (index)
139 THEN
140 PutIndice (index, LowIndice (index), tokdesc)
141 ELSE
142 PutIndice (index, HighIndice (index) +1, tokdesc)
143 END
144 END Append ;
145
146
147 (*
148 InitTokenList - creates an empty token list, which starts the first source token
149 at position 2. This allows position 0 to be used for the unknown
150 location and position 1 for the builtin token.
151 *)
152
153 PROCEDURE InitTokenList ;
154 BEGIN
155 (* 65K elements in the array and when it becomes full it will grow to 1M, 16M etc elements. *)
156 ListOfTokens := InitIndexTuned (0, 1024*1024 DIV 16, 16) ;
157 Append (ListOfTokens, InitTokenDesc (eoftok, NulName, 0, 0, 0, NIL, UnknownLocation ())) ;
158 Append (ListOfTokens, InitTokenDesc (eoftok, NulName, 0, 0, 0, NIL, BuiltinsLocation ()))
159 END InitTokenList ;
160
161
162 (*
163 Init - initializes the token list and source list.
164 *)
165
166 PROCEDURE Init ;
167 BEGIN
168 SeenEof := FALSE ;
169 InsertionIndex := 0 ;
170 currenttoken := eoftok ;
171 CurrentTokNo := InitialSourceToken ;
172 CurrentSource := NIL ;
173 UseBufferedTokens := FALSE ;
174 InitTokenList
175 END Init ;
176
177
178 (*
179 AddTo - adds a new element to the end of SourceList, CurrentSource.
180 *)
181
182 PROCEDURE AddTo (l: SourceList) ;
183 BEGIN
184 l^.right := CurrentSource ;
185 l^.left := CurrentSource^.left ;
186 CurrentSource^.left^.right := l ;
187 CurrentSource^.left := l ;
188 WITH l^.left^ DO
189 line := m2flex.GetLineNo() ;
190 col := m2flex.GetColumnNo()
191 END
192 END AddTo ;
193
194
195 (*
196 SubFrom - subtracts, l, from the source list.
197 *)
198
199 PROCEDURE SubFrom (l: SourceList) ;
200 BEGIN
201 l^.left^.right := l^.right ;
202 l^.right^.left := l^.left
203 END SubFrom ;
204
205
206 (*
207 NewElement - returns a new SourceList
208 *)
209
210 PROCEDURE NewElement (s: ADDRESS) : SourceList ;
211 VAR
212 l: SourceList ;
213 BEGIN
214 NEW (l) ;
215 IF l = NIL
216 THEN
217 HALT
218 ELSE
219 WITH l^ DO
220 name := InitStringCharStar(s) ;
221 left := NIL ;
222 right := NIL
223 END
224 END ;
225 RETURN l
226 END NewElement ;
227
228
229 (*
230 NewList - initializes an empty list with the classic dummy header element.
231 *)
232
233 PROCEDURE NewList () : SourceList ;
234 VAR
235 l: SourceList ;
236 BEGIN
237 NEW (l) ;
238 WITH l^ DO
239 left := l ;
240 right := l ;
241 name := NIL
242 END ;
243 RETURN l
244 END NewList ;
245
246
247 (*
248 CheckIfNeedToDuplicate - checks to see whether the CurrentSource has
249 been used, if it has then duplicate the list.
250 *)
251
252 PROCEDURE CheckIfNeedToDuplicate ;
253 VAR
254 l, h: SourceList ;
255 BEGIN
256 IF CurrentUsed
257 THEN
258 l := CurrentSource^.right ;
259 h := CurrentSource ;
260 CurrentSource := NewList() ;
261 WHILE l#h DO
262 AddTo (NewElement (l^.name)) ;
263 l := l^.right
264 END
265 END
266 END CheckIfNeedToDuplicate ;
267
268
269 (*
270 PushFile - indicates that, filename, has just been included.
271 *)
272
273 PROCEDURE PushFile (filename: ADDRESS) ;
274 VAR
275 l: SourceList ;
276 BEGIN
277 CheckIfNeedToDuplicate ;
278 AddTo (NewElement (filename)) ;
279 IF Debugging
280 THEN
281 IF CurrentSource^.right#CurrentSource
282 THEN
283 l := CurrentSource ;
284 REPEAT
285 printf3('name = %s, line = %d, col = %d\n', l^.name, l^.line, l^.col) ;
286 l := l^.right
287 UNTIL l=CurrentSource
288 END
289 END
290 END PushFile ;
291
292
293 (*
294 PopFile - indicates that we are returning to, filename, having finished
295 an include.
296 *)
297
298 PROCEDURE PopFile (filename: ADDRESS) ;
299 VAR
300 l: SourceList ;
301 BEGIN
302 CheckIfNeedToDuplicate ;
303 IF (CurrentSource#NIL) AND (CurrentSource^.left#CurrentSource)
304 THEN
305 l := CurrentSource^.left ; (* last element *)
306 SubFrom (l) ;
307 DISPOSE (l) ;
308 IF (CurrentSource^.left#CurrentSource) AND
309 (NOT Equal(CurrentSource^.name, Mark(InitStringCharStar(filename))))
310 THEN
311 (* mismatch in source file names after preprocessing files *)
312 END
313 ELSE
314 (* source file list is empty, cannot pop an include.. *)
315 END
316 END PopFile ;
317
318
319 (*
320 KillList - kills the SourceList providing that it has not been used.
321 *)
322
323 PROCEDURE KillList ;
324 VAR
325 l, k: SourceList ;
326 BEGIN
327 IF (NOT CurrentUsed) AND (CurrentSource#NIL)
328 THEN
329 l := CurrentSource ;
330 REPEAT
331 k := l ;
332 l := l^.right ;
333 DISPOSE(k)
334 UNTIL l=CurrentSource
335 END
336 END KillList ;
337
338
339 (*
340 ReInitialize - re-initialize the all the data structures.
341 *)
342
343 PROCEDURE ReInitialize ;
344 BEGIN
345 ForeachIndiceInIndexDo (ListOfTokens, DeleteTokenDesc) ;
346 CurrentUsed := FALSE ;
347 KillList ;
348 Init
349 END ReInitialize ;
350
351
352 (*
353 SetFile - sets the current filename to, filename.
354 *)
355
356 PROCEDURE SetFile (filename: ADDRESS) ;
357 BEGIN
358 KillList ;
359 CurrentUsed := FALSE ;
360 CurrentSource := NewList () ;
361 AddTo (NewElement (filename))
362 END SetFile ;
363
364
365 (*
366 OpenSource - Attempts to open the source file, s.
367 The success of the operation is returned.
368 *)
369
370 PROCEDURE OpenSource (s: String) : BOOLEAN ;
371 BEGIN
372 tprintf1 ("OpenSource (%s)\n", s) ;
373 SeenEof := FALSE ;
374 IF UseBufferedTokens
375 THEN
376 GetToken ;
377 RETURN TRUE
378 ELSE
379 IF m2flex.OpenSource (string (s))
380 THEN
381 SetFile (string (s)) ;
382 GetToken ;
383 IF IsLastTokenEof ()
384 THEN
385 MetaErrorT0 (GetTokenNo (), "source file is empty")
386 END ;
387 RETURN TRUE
388 ELSE
389 RETURN FALSE
390 END
391 END
392 END OpenSource ;
393
394
395 (*
396 CloseSource - closes the current open file.
397 *)
398
399 PROCEDURE CloseSource ;
400 BEGIN
401 tprintf0 ("CloseSource\n") ;
402 IF UseBufferedTokens
403 THEN
404 WHILE currenttoken#eoftok DO
405 GetToken
406 END
407 ELSE
408 (* a subsequent call to m2flex.OpenSource will really close the file *)
409 END
410 END CloseSource ;
411
412
413 (*
414 ResetForNewPass - reset the buffer pointers to the beginning ready for
415 a new pass
416 *)
417
418 PROCEDURE ResetForNewPass ;
419 BEGIN
420 InsertionIndex := 0 ;
421 CurrentTokNo := InitialSourceToken ;
422 UseBufferedTokens := TRUE
423 END ResetForNewPass ;
424
425
426 (*
427 DisplayToken - display the token name using printf0 no newline is emitted.
428 *)
429
430 PROCEDURE DisplayToken (tok: toktype) ;
431 BEGIN
432 CASE tok OF
433
434 eoftok: printf0('eoftok') |
435 plustok: printf0('plustok') |
436 minustok: printf0('minustok') |
437 timestok: printf0('timestok') |
438 dividetok: printf0('dividetok') |
439 becomestok: printf0('becomestok') |
440 ambersandtok: printf0('ambersandtok') |
441 periodtok: printf0('periodtok') |
442 commatok: printf0('commatok') |
443 semicolontok: printf0('semicolontok') |
444 lparatok: printf0('lparatok') |
445 rparatok: printf0('rparatok') |
446 lsbratok: printf0('lsbratok') |
447 rsbratok: printf0('rsbratok') |
448 lcbratok: printf0('lcbratok') |
449 rcbratok: printf0('rcbratok') |
450 uparrowtok: printf0('uparrowtok') |
451 singlequotetok: printf0('singlequotetok') |
452 equaltok: printf0('equaltok') |
453 hashtok: printf0('hashtok') |
454 lesstok: printf0('lesstok') |
455 greatertok: printf0('greatertok') |
456 lessgreatertok: printf0('lessgreatertok') |
457 lessequaltok: printf0('lessequaltok') |
458 greaterequaltok: printf0('greaterequaltok') |
459 periodperiodtok: printf0('periodperiodtok') |
460 colontok: printf0('colontok') |
461 doublequotestok: printf0('doublequotestok') |
462 bartok: printf0('bartok') |
463 andtok: printf0('andtok') |
464 arraytok: printf0('arraytok') |
465 begintok: printf0('begintok') |
466 bytok: printf0('bytok') |
467 casetok: printf0('casetok') |
468 consttok: printf0('consttok') |
469 definitiontok: printf0('definitiontok') |
470 divtok: printf0('divtok') |
471 dotok: printf0('dotok') |
472 elsetok: printf0('elsetok') |
473 elsiftok: printf0('elsiftok') |
474 endtok: printf0('endtok') |
475 exittok: printf0('exittok') |
476 exporttok: printf0('exporttok') |
477 fortok: printf0('fortok') |
478 fromtok: printf0('fromtok') |
479 iftok: printf0('iftok') |
480 implementationtok: printf0('implementationtok') |
481 importtok: printf0('importtok') |
482 intok: printf0('intok') |
483 looptok: printf0('looptok') |
484 modtok: printf0('modtok') |
485 moduletok: printf0('moduletok') |
486 nottok: printf0('nottok') |
487 oftok: printf0('oftok') |
488 ortok: printf0('ortok') |
489 pointertok: printf0('pointertok') |
490 proceduretok: printf0('proceduretok') |
491 qualifiedtok: printf0('qualifiedtok') |
492 unqualifiedtok: printf0('unqualifiedtok') |
493 recordtok: printf0('recordtok') |
494 repeattok: printf0('repeattok') |
495 returntok: printf0('returntok') |
496 settok: printf0('settok') |
497 thentok: printf0('thentok') |
498 totok: printf0('totok') |
499 typetok: printf0('typetok') |
500 untiltok: printf0('untiltok') |
501 vartok: printf0('vartok') |
502 whiletok: printf0('whiletok') |
503 withtok: printf0('withtok') |
504 asmtok: printf0('asmtok') |
505 volatiletok: printf0('volatiletok') |
506 periodperiodperiodtok: printf0('periodperiodperiodtok') |
507 datetok: printf0('datetok') |
508 linetok: printf0('linetok') |
509 filetok: printf0('filetok') |
510 integertok: printf0('integertok') |
511 identtok: printf0('identtok') |
512 realtok: printf0('realtok') |
513 stringtok: printf0('stringtok')
514
515 ELSE
516 END
517 END DisplayToken ;
518
519
520 VAR
521 indent: CARDINAL ;
522
523
524 (*
525 DumpToken -
526 *)
527
528 PROCEDURE DumpToken (tokdesc: TokenDesc) ;
529 VAR
530 n: CARDINAL ;
531 BEGIN
532 n := indent ;
533 WHILE n > 0 DO
534 printf0 (" ") ;
535 DEC (n)
536 END ;
537 WITH tokdesc^ DO
538 DisplayToken (token) ;
539 IF str # NulName
540 THEN
541 printf1 (" %a", str)
542 END ;
543 IF insert # NIL
544 THEN
545 printf0 ("inserted error recovery tokens\n") ;
546 INC (indent, 2) ;
547 ForeachIndiceInIndexDo (insert, DumpToken) ;
548 DEC (indent, 2)
549 END
550 END
551 END DumpToken ;
552
553
554 (*
555 DumpTokens - displays all tokens.
556 *)
557
558 PROCEDURE DumpTokens ;
559 VAR
560 high,
561 ind : CARDINAL ;
562 BEGIN
563 IF IsEmpty (ListOfTokens)
564 THEN
565 printf0 ("The token buffer is empty\n")
566 ELSE
567 ind := LowIndice (ListOfTokens) ;
568 high := HighIndice (ListOfTokens) ;
569 WHILE ind <= high DO
570 printf1 ("%5d ", ind) ;
571 DumpToken (GetIndice (ListOfTokens, ind)) ;
572 INC (ind)
573 END
574 END
575 END DumpTokens ;
576
577
578 (*
579 CopyOutCurrent - copies the token in buffer[index][insertion] into
580 then current token global variables.
581 *)
582
583 PROCEDURE CopyOutCurrent (buffer: Index; index, insertion: CARDINAL) ;
584 VAR
585 tokdesc: TokenDesc ;
586 BEGIN
587 tokdesc := GetIndice (buffer, index) ;
588 IF insertion # 0
589 THEN
590 tokdesc := GetIndice (tokdesc^.insert, insertion)
591 END ;
592 WITH tokdesc^ DO
593 currenttoken := token ;
594 currentstring := KeyToCharStar (str) ;
595 currentcolumn := col ;
596 currentinteger := int
597 END
598 END CopyOutCurrent ;
599
600
601 (*
602 UpdateToken - update the global current token variables from buffer[index]
603 using inserted tokens if directed by InsertionIndex.
604 *)
605
606 PROCEDURE UpdateToken (buffer: Index; index: CARDINAL) ;
607 VAR
608 tokdesc: TokenDesc ;
609 BEGIN
610 tokdesc := GetIndice (buffer, index) ;
611 IF InsertionIndex > 0
612 THEN
613 (* We have an inserted token to use. *)
614 Assert (tokdesc^.insert # NIL) ;
615 CopyOutCurrent (buffer, index, InsertionIndex) ;
616 (* Move InsertionIndex to the next position. *)
617 INC (InsertionIndex) ;
618 IF InsertionIndex > HighIndice (tokdesc^.insert)
619 THEN
620 (* We are done consuming the inserted tokens, so move
621 onto the next original source token. *)
622 InsertionIndex := 0 ;
623 INC (CurrentTokNo)
624 END
625 ELSIF (tokdesc^.insert # NIL) AND (InsertionIndex = 0)
626 THEN
627 (* This source token has extra tokens appended after it by the error recovery.
628 Set the index ready for the next UpdateToken which will read the extra
629 tokens. *)
630 InsertionIndex := 1 ;
631 (* However this call must read the original token. *)
632 CopyOutCurrent (buffer, index, 0)
633 ELSE
634 CopyOutCurrent (buffer, index, 0) ;
635 (* Move onto the next original source token. *)
636 INC (CurrentTokNo)
637 END
638 END UpdateToken ;
639
640
641 (*
642 GetTokenFiltered - providing that we have not already seen an eof for this source
643 file call m2flex.GetToken and GetToken if requested.
644 *)
645
646 PROCEDURE GetTokenFiltered (callGetToken: BOOLEAN) ;
647 BEGIN
648 IF SeenEof
649 THEN
650 currenttoken := eoftok
651 ELSE
652 (* Call the lexical phase to place a new token into the last bucket. *)
653 m2flex.GetToken () ;
654 IF callGetToken
655 THEN
656 GetToken
657 END
658 END
659 END GetTokenFiltered ;
660
661
662 (*
663 GetToken - gets the next token into currenttoken.
664 *)
665
666 PROCEDURE GetToken ;
667 VAR
668 buf: ARRAY [0..20] OF CHAR ;
669 BEGIN
670 IF UseBufferedTokens
671 THEN
672 UpdateToken (ListOfTokens, CurrentTokNo) ;
673 IF GetDebugTraceToken ()
674 THEN
675 CardToStr (CurrentTokNo, 0, buf) ;
676 FIO.WriteString (GetDumpFile (), 'token: ') ;
677 FIO.WriteString (GetDumpFile (), buf) ;
678 FIO.WriteLine (GetDumpFile ())
679 END
680 ELSE
681 IF NOT InBounds (ListOfTokens, CurrentTokNo)
682 THEN
683 GetTokenFiltered (FALSE)
684 END ;
685 UpdateToken (ListOfTokens, CurrentTokNo) ;
686 IF GetDebugTraceToken ()
687 THEN
688 CardToStr (CurrentTokNo, 0, buf) ;
689 m2flex.M2Error (ADR (buf))
690 END
691 END
692 END GetToken ;
693
694
695 (*
696 AppendInsertToken -
697 *)
698
699 PROCEDURE AppendInsertToken (index: Index; tokdesc: TokenDesc) ;
700 BEGIN
701 IF IsEmpty (index)
702 THEN
703 PutIndice (index, LowIndice (index), tokdesc)
704 ELSE
705 PutIndice (index, HighIndice (index) +1, tokdesc)
706 END
707 END AppendInsertToken ;
708
709
710 (*
711 DupTok - duplicate tokdesc and replaces the token field with token.
712 *)
713
714 PROCEDURE DupTok (tokdesc: TokenDesc; token: toktype) : TokenDesc ;
715 VAR
716 dup: TokenDesc ;
717 BEGIN
718 NEW (dup) ;
719 Assert (dup # NIL) ;
720 dup^ := tokdesc^ ;
721 dup^.token := token ;
722 RETURN dup
723 END DupTok ;
724
725
726 (*
727 InsertToken - inserts a symbol token infront of the current token
728 ready for the next pass.
729 *)
730
731 PROCEDURE InsertToken (token: toktype) ;
732 VAR
733 prev : CARDINAL ;
734 tokdesc: TokenDesc ;
735 BEGIN
736 Assert (ListOfTokens # NIL) ;
737 Assert (NOT IsEmpty (ListOfTokens)) ;
738 prev := GetTokenNo () -1 ;
739 tokdesc := GetIndice (ListOfTokens, prev) ;
740 IF tokdesc^.insert = NIL
741 THEN
742 tokdesc^.insert := InitIndex (1)
743 END ;
744 AppendInsertToken (tokdesc^.insert, DupTok (tokdesc, token))
745 END InsertToken ;
746
747
748 (*
749 InsertTokenAndRewind - inserts a symbol token infront of the current token
750 and then moves the token stream back onto the inserted
751 token.
752 *)
753
754 PROCEDURE InsertTokenAndRewind (token: toktype) ;
755 VAR
756 position : CARDINAL ;
757 tokdesc : TokenDesc ;
758 BEGIN
759 IF GetTokenNo () > 0
760 THEN
761 InsertToken (token) ;
762 position := CurrentTokNo -2 ;
763 tokdesc := GetIndice (ListOfTokens, position) ;
764 IF tokdesc^.insert = NIL
765 THEN
766 tokdesc^.insert := InitIndex (1)
767 END ;
768 AppendInsertToken (tokdesc^.insert, DupTok (tokdesc, token)) ;
769 InsertionIndex := HighIndice (tokdesc^.insert) ;
770 DEC (CurrentTokNo, 2) ;
771 GetToken
772 END
773 END InsertTokenAndRewind ;
774
775
776 (*
777 GetPreviousTokenLineNo - returns the line number of the previous token.
778 *)
779
780 PROCEDURE GetPreviousTokenLineNo () : CARDINAL ;
781 BEGIN
782 RETURN GetLineNo ()
783 END GetPreviousTokenLineNo ;
784
785
786 (*
787 GetLineNo - returns the current line number where the symbol occurs in
788 the source file.
789 *)
790
791 PROCEDURE GetLineNo () : CARDINAL ;
792 BEGIN
793 IF CurrentTokNo = 0
794 THEN
795 RETURN 0
796 ELSE
797 RETURN TokenToLineNo (GetTokenNo (), 0)
798 END
799 END GetLineNo ;
800
801
802 (*
803 GetColumnNo - returns the current column where the symbol occurs in
804 the source file.
805 *)
806
807 PROCEDURE GetColumnNo () : CARDINAL ;
808 BEGIN
809 IF CurrentTokNo = 0
810 THEN
811 RETURN 0
812 ELSE
813 RETURN TokenToColumnNo (GetTokenNo (), 0)
814 END
815 END GetColumnNo ;
816
817
818 (*
819 GetTokenNo - returns the current token number.
820 *)
821
822 PROCEDURE GetTokenNo () : CARDINAL ;
823 BEGIN
824 IF CurrentTokNo = 0
825 THEN
826 RETURN 0
827 ELSE
828 RETURN CurrentTokNo-1
829 END
830 END GetTokenNo ;
831
832
833 (*
834 GetTokenName - returns the token name given the tokenno.
835 *)
836
837 PROCEDURE GetTokenName (tokenno: CARDINAL) : Name ;
838 VAR
839 tokdesc: TokenDesc ;
840 name : Name ;
841 BEGIN
842 IF InBounds (ListOfTokens, tokenno)
843 THEN
844 tokdesc := GetIndice (ListOfTokens, tokenno) ;
845 name := tokToTok (tokdesc^.token) ;
846 IF name = NulName
847 THEN
848 RETURN tokdesc^.str
849 ELSE
850 RETURN name
851 END
852 END ;
853 RETURN NulName
854 END GetTokenName ;
855
856
857 (*
858 TokenToLineNo - returns the line number of the current file for the
859 tokenno. The depth refers to the include depth.
860 A depth of 0 is the current file, depth of 1 is the file
861 which included the current file. Zero is returned if the
862 depth exceeds the file nesting level.
863 *)
864
865 PROCEDURE TokenToLineNo (tokenno: CARDINAL; depth: CARDINAL) : CARDINAL ;
866 VAR
867 tokdesc: TokenDesc ;
868 level : SourceList ;
869 BEGIN
870 IF (tokenno # UnknownTokenNo) AND (tokenno # BuiltinTokenNo)
871 THEN
872 IF InBounds (ListOfTokens, tokenno)
873 THEN
874 tokdesc := GetIndice (ListOfTokens, tokenno) ;
875 IF depth = 0
876 THEN
877 RETURN tokdesc^.line
878 ELSE
879 level := tokdesc^.file^.left ;
880 WHILE depth > 0 DO
881 level := level^.left ;
882 IF level = tokdesc^.file^.left
883 THEN
884 RETURN 0
885 END ;
886 DEC (depth)
887 END ;
888 RETURN level^.line
889 END
890 END
891 END ;
892 RETURN 0
893 END TokenToLineNo ;
894
895
896 (*
897 TokenToColumnNo - returns the column number of the current file for the
898 tokenno. The depth refers to the include depth.
899 A depth of 0 is the current file, depth of 1 is the file
900 which included the current file. Zero is returned if the
901 depth exceeds the file nesting level.
902 *)
903
904 PROCEDURE TokenToColumnNo (tokenno: CARDINAL; depth: CARDINAL) : CARDINAL ;
905 VAR
906 tokdesc: TokenDesc ;
907 level : SourceList ;
908 BEGIN
909 IF (tokenno # UnknownTokenNo) AND (tokenno # BuiltinTokenNo)
910 THEN
911 IF InBounds (ListOfTokens, tokenno)
912 THEN
913 tokdesc := GetIndice (ListOfTokens, tokenno) ;
914 IF depth = 0
915 THEN
916 RETURN tokdesc^.col
917 ELSE
918 level := tokdesc^.file^.left ;
919 WHILE depth > 0 DO
920 level := level^.left ;
921 IF level = tokdesc^.file^.left
922 THEN
923 RETURN 0
924 END ;
925 DEC (depth)
926 END ;
927 RETURN level^.col
928 END
929 END
930 END ;
931 RETURN 0
932 END TokenToColumnNo ;
933
934
935 (*
936 TokenToLocation - returns the location_t corresponding to tokenno.
937 *)
938
939 PROCEDURE TokenToLocation (tokenno: CARDINAL) : location_t ;
940 VAR
941 tokdesc: TokenDesc ;
942 BEGIN
943 IF tokenno = UnknownTokenNo
944 THEN
945 RETURN UnknownLocation ()
946 ELSIF tokenno = BuiltinTokenNo
947 THEN
948 RETURN BuiltinsLocation ()
949 ELSIF InBounds (ListOfTokens, tokenno)
950 THEN
951 tokdesc := GetIndice (ListOfTokens, tokenno) ;
952 RETURN tokdesc^.loc
953 END ;
954 RETURN UnknownLocation ()
955 END TokenToLocation ;
956
957
958 (*
959 FindFileNameFromToken - returns the complete FileName for the appropriate
960 source file yields the token number, TokenNo.
961 The, Depth, indicates the include level: 0..n
962 Level 0 is the current. NIL is returned if n+1
963 is requested.
964 *)
965
966 PROCEDURE FindFileNameFromToken (tokenno: CARDINAL; depth: CARDINAL) : String ;
967 VAR
968 tokdesc: TokenDesc ;
969 level : SourceList ;
970 BEGIN
971 IF (tokenno # UnknownTokenNo) AND (tokenno # BuiltinTokenNo)
972 THEN
973 IF InBounds (ListOfTokens, tokenno)
974 THEN
975 tokdesc := GetIndice (ListOfTokens, tokenno) ;
976 level := tokdesc^.file^.left ;
977 WHILE depth > 0 DO
978 level := level^.left ;
979 IF level = tokdesc^.file^.left
980 THEN
981 RETURN NIL
982 END ;
983 DEC (depth)
984 END ;
985 RETURN level^.name
986 END
987 END ;
988 RETURN NIL
989 END FindFileNameFromToken ;
990
991
992 (*
993 GetFileName - returns a String defining the current file.
994 *)
995
996 PROCEDURE GetFileName () : String ;
997 BEGIN
998 RETURN FindFileNameFromToken (GetTokenNo (), 0)
999 END GetFileName ;
1000
1001
1002 (*
1003 AddTokToList - adds a token to a dynamic list.
1004 *)
1005
1006 PROCEDURE AddTokToList (token: toktype; str: Name;
1007 int: INTEGER; line: CARDINAL; col: CARDINAL;
1008 file: SourceList; location: location_t) ;
1009 BEGIN
1010 Append (ListOfTokens, InitTokenDesc (token, str, int, line,
1011 col, file, location))
1012 END AddTokToList ;
1013
1014
1015 (*
1016 IsLastTokenEof - returns TRUE if the last token was an eoftok
1017 *)
1018
1019 PROCEDURE IsLastTokenEof () : BOOLEAN ;
1020 VAR
1021 tokdesc: TokenDesc ;
1022 BEGIN
1023 IF IsEmpty (ListOfTokens)
1024 THEN
1025 RETURN FALSE
1026 ELSE
1027 tokdesc := GetIndice (ListOfTokens, HighIndice (ListOfTokens)) ;
1028 RETURN tokdesc^.token = eoftok
1029 END
1030 END IsLastTokenEof ;
1031
1032
1033 (*
1034 PrintTokenNo - displays token and the location of the token.
1035 *)
1036
1037 PROCEDURE PrintTokenNo (tokenno: CARDINAL) ;
1038 VAR
1039 s: String ;
1040 BEGIN
1041 printf1 ("tokenno = %d, ", tokenno) ;
1042 s := InitStringCharStar (KeyToCharStar (GetTokenName (tokenno))) ;
1043 printf1 ("%s\n", s) ;
1044 s := KillString (s)
1045 END PrintTokenNo ;
1046
1047
1048 (*
1049 isSrcToken - returns TRUE if tokenno is associated with
1050 program source code.
1051 *)
1052
1053 PROCEDURE isSrcToken (tokenno: CARDINAL) : BOOLEAN ;
1054 BEGIN
1055 RETURN (tokenno # UnknownTokenNo) AND (tokenno # BuiltinTokenNo)
1056 END isSrcToken ;
1057
1058
1059 (*
1060 MakeVirtualTok - providing caret, left, right are associated with a source file
1061 and exist on the same src line then
1062 create and return a new tokenno which is created from
1063 tokenno left and right. Otherwise return caret.
1064 If caret is UnknownTokenNo then it is replaced with left or right
1065 in sequence to avoid an UnknownTokenNo.
1066 *)
1067
1068 PROCEDURE MakeVirtualTok (caret, left, right: CARDINAL) : CARDINAL ;
1069 VAR
1070 descLeft, descRight: TokenDesc ;
1071 lc, ll, lr : location_t ;
1072 BEGIN
1073 IF caret = UnknownTokenNo
1074 THEN
1075 caret := left
1076 END ;
1077 IF caret = UnknownTokenNo
1078 THEN
1079 caret := right
1080 END ;
1081 IF isSrcToken (caret) AND isSrcToken (left) AND isSrcToken (right)
1082 THEN
1083 lc := TokenToLocation (caret) ;
1084 ll := TokenToLocation (left) ;
1085 lr := TokenToLocation (right) ;
1086 IF InBounds (ListOfTokens, left) AND InBounds (ListOfTokens, right)
1087 THEN
1088 descLeft := GetIndice (ListOfTokens, left) ;
1089 descRight := GetIndice (ListOfTokens, right) ;
1090 IF (descLeft^.line = descRight^.line) AND
1091 (descLeft^.file = descRight^.file)
1092 THEN
1093 (* On the same line, create a new token and location. *)
1094 AddTokToList (virtualrangetok, NulName, 0,
1095 descLeft^.line, descLeft^.col, descLeft^.file,
1096 GetLocationBinary (lc, ll, lr)) ;
1097 caret := HighIndice (ListOfTokens)
1098 END
1099 END
1100 END ;
1101 IF caret = BadTokenNo
1102 THEN
1103 stop
1104 END ;
1105 RETURN caret
1106 END MakeVirtualTok ;
1107
1108
1109 (*
1110 MakeVirtual2Tok - creates and return a new tokenno which is created from
1111 two tokens left and right. It tries to avoid UnknownTokenNo
1112 and will fall back to left or right if necessary.
1113 *)
1114
1115 PROCEDURE MakeVirtual2Tok (left, right: CARDINAL) : CARDINAL ;
1116 BEGIN
1117 IF left = UnknownTokenNo
1118 THEN
1119 left := right
1120 ELSIF right = UnknownTokenNo
1121 THEN
1122 right := left
1123 END ;
1124 RETURN MakeVirtualTok (left, left, right) ;
1125 END MakeVirtual2Tok ;
1126
1127
1128 (*
1129 tprintf0 -
1130 *)
1131
1132 PROCEDURE tprintf0 (format: ARRAY OF CHAR) ;
1133 BEGIN
1134 IF Tracing
1135 THEN
1136 printf0 (format)
1137 END
1138 END tprintf0 ;
1139
1140
1141 (*
1142 tprintf1 -
1143 *)
1144
1145 PROCEDURE tprintf1 (format: ARRAY OF CHAR; str: String) ;
1146 BEGIN
1147 IF Tracing
1148 THEN
1149 printf1 (format, str)
1150 END
1151 END tprintf1 ;
1152
1153
1154 (* ***********************************************************************
1155 *
1156 * These functions allow m2.flex to deliver tokens into the buffer
1157 *
1158 ************************************************************************* *)
1159
1160 (*
1161 AddTok - adds a token to the buffer.
1162 *)
1163
1164 PROCEDURE AddTok (t: toktype) ;
1165 VAR
1166 s: String ;
1167 BEGIN
1168 IF Tracing
1169 THEN
1170 printf0 (" m2.flex -> AddTok ") ;
1171 DisplayToken (t) ;
1172 printf0 ("\n") ;
1173 END ;
1174 IF (t=eoftok) AND SeenEof
1175 THEN
1176 IF Debugging
1177 THEN
1178 printf0 ("extra eoftok ignored as buffer already contains eoftok\n")
1179 END
1180 ELSE
1181 IF Debugging
1182 THEN
1183 printf0 ("adding token: ") ; DisplayToken (t) ;
1184 printf0 ("\n")
1185 END ;
1186 AddTokToList(t, NulName, 0,
1187 m2flex.GetLineNo(), m2flex.GetColumnNo(), CurrentSource,
1188 m2flex.GetLocation()) ;
1189 CurrentUsed := TRUE ;
1190 IF Debugging
1191 THEN
1192 (* display each token as a warning. *)
1193 s := InitStringCharStar (KeyToCharStar (GetTokenName (GetTokenNo ()))) ;
1194 WarnStringAt (s, GetTokenNo ())
1195 END ;
1196 IF t = eoftok
1197 THEN
1198 SeenEof := TRUE
1199 END
1200 END
1201 END AddTok ;
1202
1203
1204 (*
1205 AddTokCharStar - adds a token to the buffer and an additional string, s.
1206 A copy of string, s, is made.
1207 *)
1208
1209 PROCEDURE AddTokCharStar (t: toktype; s: ADDRESS) ;
1210 VAR
1211 str: String ;
1212 BEGIN
1213 Assert (t # eoftok) ;
1214 IF Tracing
1215 THEN
1216 printf0 (" m2.flex -> AddTokCharStar ") ;
1217 DisplayToken (t) ;
1218 str := InitStringCharStar (s) ;
1219 printf1 (" %s\n", str) ;
1220 str := KillString (str)
1221 END ;
1222 IF Debugging
1223 THEN
1224 printf0 ("AddTokCharStar: ") ; DisplayToken (t) ; printf0 ("\n")
1225 END ;
1226 AddTokToList(t, makekey(s), 0, m2flex.GetLineNo(),
1227 m2flex.GetColumnNo(), CurrentSource, m2flex.GetLocation()) ;
1228 CurrentUsed := TRUE
1229 END AddTokCharStar ;
1230
1231
1232 (*
1233 AddTokInteger - adds a token and an integer to the buffer.
1234 *)
1235
1236 PROCEDURE AddTokInteger (t: toktype; i: INTEGER) ;
1237 VAR
1238 s: String ;
1239 c,
1240 l: CARDINAL ;
1241 BEGIN
1242 Assert (t # eoftok) ;
1243 IF Tracing
1244 THEN
1245 printf0 (" m2.flex -> AddTokInteger ") ;
1246 DisplayToken (t) ;
1247 printf1 (" %d\n", i) ;
1248 END ;
1249 l := m2flex.GetLineNo() ;
1250 c := m2flex.GetColumnNo() ;
1251 s := Sprintf1(Mark(InitString('%d')), i) ;
1252 AddTokToList(t, makekey(string(s)), i, l, c, CurrentSource, m2flex.GetLocation()) ;
1253 s := KillString(s) ;
1254 CurrentUsed := TRUE
1255 END AddTokInteger ;
1256
1257
1258 BEGIN
1259 Init
1260 END M2LexBuf.