1 /* do not edit automatically generated by mc from mcLexBuf. */
2 /* mcLexBuf.mod provides a buffer for the all the tokens created by m2.lex.
4 Copyright (C) 2015-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
7 This file is part of GNU Modula-2.
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)
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.
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/>. */
26 # if !defined (PROC_D)
28 typedef void (*PROC_t
) (void);
29 typedef struct { PROC_t proc
; } PROC
;
40 # include "GStorage.h"
41 #if defined(__cplusplus)
47 #include "GmcLexBuf.h"
51 # include "GStorage.h"
52 # include "GDynamicStrings.h"
53 # include "GFormatStrings.h"
54 # include "GnameKey.h"
55 # include "GmcReserved.h"
56 # include "GmcComment.h"
57 # include "GmcPrintf.h"
58 # include "GmcDebug.h"
61 # define MaxBucketSize 100
62 # define Debugging false
63 typedef struct mcLexBuf_tokenDesc_r mcLexBuf_tokenDesc
;
65 typedef struct mcLexBuf_listDesc_r mcLexBuf_listDesc
;
67 typedef struct mcLexBuf__T1_r mcLexBuf__T1
;
69 typedef mcLexBuf__T1
*mcLexBuf_sourceList
;
71 typedef struct mcLexBuf__T2_r mcLexBuf__T2
;
73 typedef mcLexBuf__T2
*mcLexBuf_tokenBucket
;
75 typedef struct mcLexBuf__T3_a mcLexBuf__T3
;
77 struct mcLexBuf_tokenDesc_r
{
78 mcReserved_toktype token
;
81 mcComment_commentDesc com
;
84 mcLexBuf_sourceList file
;
87 struct mcLexBuf_listDesc_r
{
88 mcLexBuf_tokenBucket head
;
89 mcLexBuf_tokenBucket tail
;
90 unsigned int lastBucketOffset
;
93 struct mcLexBuf__T1_r
{
94 mcLexBuf_sourceList left
;
95 mcLexBuf_sourceList right
;
96 DynamicStrings_String name
;
101 struct mcLexBuf__T3_a
{ mcLexBuf_tokenDesc array
[MaxBucketSize
+1]; };
102 struct mcLexBuf__T2_r
{
105 mcLexBuf_tokenBucket next
;
108 static mcComment_commentDesc procedureComment
;
109 static mcComment_commentDesc bodyComment
;
110 static mcComment_commentDesc afterComment
;
111 static mcLexBuf_sourceList currentSource
;
112 static bool useBufferedTokens
;
113 static bool currentUsed
;
114 static mcLexBuf_listDesc listOfTokens
;
115 static unsigned int nextTokNo
;
118 getProcedureComment - returns the procedure comment if it exists,
122 extern "C" mcComment_commentDesc
mcLexBuf_getProcedureComment (void);
125 getBodyComment - returns the body comment if it exists,
126 or NIL otherwise. The body comment is
130 extern "C" mcComment_commentDesc
mcLexBuf_getBodyComment (void);
133 getAfterComment - returns the after comment if it exists,
134 or NIL otherwise. The after comment is
138 extern "C" mcComment_commentDesc
mcLexBuf_getAfterComment (void);
141 openSource - attempts to open the source file, s.
142 The success of the operation is returned.
145 extern "C" bool mcLexBuf_openSource (DynamicStrings_String s
);
148 closeSource - closes the current open file.
151 extern "C" void mcLexBuf_closeSource (void);
154 reInitialize - re-initialize the all the data structures.
157 extern "C" void mcLexBuf_reInitialize (void);
160 resetForNewPass - reset the buffer pointers to the beginning ready for
164 extern "C" void mcLexBuf_resetForNewPass (void);
167 getToken - gets the next token into currenttoken.
170 extern "C" void mcLexBuf_getToken (void);
173 insertToken - inserts a symbol, token, infront of the current token
174 ready for the next pass.
177 extern "C" void mcLexBuf_insertToken (mcReserved_toktype token
);
180 insertTokenAndRewind - inserts a symbol, token, infront of the current token
181 and then moves the token stream back onto the inserted token.
184 extern "C" void mcLexBuf_insertTokenAndRewind (mcReserved_toktype token
);
187 getPreviousTokenLineNo - returns the line number of the previous token.
190 extern "C" unsigned int mcLexBuf_getPreviousTokenLineNo (void);
193 getLineNo - returns the current line number where the symbol occurs in
197 extern "C" unsigned int mcLexBuf_getLineNo (void);
200 getTokenNo - returns the current token number.
203 extern "C" unsigned int mcLexBuf_getTokenNo (void);
206 tokenToLineNo - returns the line number of the current file for the
207 tokenNo. The depth refers to the include depth.
208 A depth of 0 is the current file, depth of 1 is the file
209 which included the current file. Zero is returned if the
210 depth exceeds the file nesting level.
213 extern "C" unsigned int mcLexBuf_tokenToLineNo (unsigned int tokenNo
, unsigned int depth
);
216 getColumnNo - returns the current column where the symbol occurs in
220 extern "C" unsigned int mcLexBuf_getColumnNo (void);
223 tokenToColumnNo - returns the column number of the current file for the
224 tokenNo. The depth refers to the include depth.
225 A depth of 0 is the current file, depth of 1 is the file
226 which included the current file. Zero is returned if the
227 depth exceeds the file nesting level.
230 extern "C" unsigned int mcLexBuf_tokenToColumnNo (unsigned int tokenNo
, unsigned int depth
);
233 findFileNameFromToken - returns the complete FileName for the appropriate
234 source file yields the token number, tokenNo.
235 The, Depth, indicates the include level: 0..n
236 Level 0 is the current. NIL is returned if n+1
240 extern "C" DynamicStrings_String
mcLexBuf_findFileNameFromToken (unsigned int tokenNo
, unsigned int depth
);
243 getFileName - returns a String defining the current file.
246 extern "C" DynamicStrings_String
mcLexBuf_getFileName (void);
249 addTok - adds a token to the buffer.
252 extern "C" void mcLexBuf_addTok (mcReserved_toktype t
);
255 addTokCharStar - adds a token to the buffer and an additional string, s.
256 A copy of string, s, is made.
259 extern "C" void mcLexBuf_addTokCharStar (mcReserved_toktype t
, void * s
);
262 addTokInteger - adds a token and an integer to the buffer.
265 extern "C" void mcLexBuf_addTokInteger (mcReserved_toktype t
, int i
);
268 addTokComment - adds a token to the buffer and a comment descriptor, com.
271 extern "C" void mcLexBuf_addTokComment (mcReserved_toktype t
, mcComment_commentDesc com
);
274 setFile - sets the current filename to, filename.
277 extern "C" void mcLexBuf_setFile (void * filename
);
280 pushFile - indicates that, filename, has just been included.
283 extern "C" void mcLexBuf_pushFile (void * filename
);
286 popFile - indicates that we are returning to, filename, having finished
290 extern "C" void mcLexBuf_popFile (void * filename
);
293 debugLex - display the last, n, tokens.
296 static void debugLex (unsigned int n
);
302 static void seekTo (unsigned int t
);
308 static mcLexBuf_tokenBucket
peeptokenBucket (unsigned int *t
);
311 peepAfterComment - peeps ahead looking for an after statement comment. It stops at an END token
312 or if the line number changes.
315 static void peepAfterComment (void);
318 init - initializes the token list and source list.
321 static void init (void);
324 addTo - adds a new element to the end of sourceList, currentSource.
327 static void addTo (mcLexBuf_sourceList l
);
330 subFrom - subtracts, l, from the source list.
333 static void subFrom (mcLexBuf_sourceList l
);
336 newElement - returns a new sourceList
339 static mcLexBuf_sourceList
newElement (void * s
);
342 newList - initializes an empty list with the classic dummy header element.
345 static mcLexBuf_sourceList
newList (void);
348 checkIfNeedToDuplicate - checks to see whether the currentSource has
349 been used, if it has then duplicate the list.
352 static void checkIfNeedToDuplicate (void);
355 killList - kills the sourceList providing that it has not been used.
358 static void killList (void);
364 static void displayToken (mcReserved_toktype t
);
367 updateFromBucket - updates the global variables: currenttoken,
368 currentstring, currentcolumn and currentinteger
369 from tokenBucket, b, and, offset.
372 static void updateFromBucket (mcLexBuf_tokenBucket b
, unsigned int offset
);
375 doGetToken - fetch the next token into currenttoken.
378 static void doGetToken (void);
381 syncOpenWithBuffer - synchronise the buffer with the start of a file.
382 Skips all the tokens to do with the previous file.
385 static void syncOpenWithBuffer (void);
388 findtokenBucket - returns the tokenBucket corresponding to the tokenNo.
391 static mcLexBuf_tokenBucket
findtokenBucket (unsigned int *tokenNo
);
394 getFileName - returns a String defining the current file.
397 static void stop (void);
400 addTokToList - adds a token to a dynamic list.
403 static void addTokToList (mcReserved_toktype t
, nameKey_Name n
, int i
, mcComment_commentDesc comment
, unsigned int l
, unsigned int c
, mcLexBuf_sourceList f
);
406 isLastTokenEof - returns TRUE if the last token was an eoftok
409 static bool isLastTokenEof (void);
413 debugLex - display the last, n, tokens.
416 static void debugLex (unsigned int n
)
422 mcLexBuf_tokenBucket b
;
437 mcPrintf_printf0 ((const char *) "nextTokNo ", 10);
439 b
= findtokenBucket (&t
);
443 mcPrintf_printf1 ((const char *) "end of buf (%d is further ahead than the buffer contents)\\n", 60, (const unsigned char *) &t
, (sizeof (t
)-1));
448 mcPrintf_printf2 ((const char *) "entry %d %d ", 13, (const unsigned char *) &c
, (sizeof (c
)-1), (const unsigned char *) &t
, (sizeof (t
)-1));
449 displayToken (b
->buf
.array
[t
].token
);
450 mcPrintf_printf0 ((const char *) "\\n", 2);
453 } while (! (b
== NULL
));
461 static void seekTo (unsigned int t
)
463 mcLexBuf_tokenBucket b
;
469 b
= findtokenBucket (&t
);
472 updateFromBucket (b
, t
);
482 static mcLexBuf_tokenBucket
peeptokenBucket (unsigned int *t
)
484 mcReserved_toktype ct
;
487 mcLexBuf_tokenBucket b
;
488 mcLexBuf_tokenBucket c
;
490 ct
= mcLexBuf_currenttoken
;
495 old
= mcLexBuf_getTokenNo ();
498 b
= findtokenBucket (&n
);
503 b
= findtokenBucket (&n
);
504 if ((b
== NULL
) || (mcLexBuf_currenttoken
== mcReserved_eoftok
))
508 b
= findtokenBucket (&old
);
509 updateFromBucket (b
, old
);
513 } while (! ((b
!= NULL
) || (mcLexBuf_currenttoken
== mcReserved_eoftok
)));
518 mcPrintf_printf2 ((const char *) "nextTokNo = %d, old = %d\\n", 26, (const unsigned char *) &nextTokNo
, (sizeof (nextTokNo
)-1), (const unsigned char *) &old
, (sizeof (old
)-1));
520 b
= findtokenBucket (&old
);
523 mcPrintf_printf1 ((const char *) " adjusted old = %d\\n", 21, (const unsigned char *) &old
, (sizeof (old
)-1));
527 updateFromBucket (b
, old
);
533 mcDebug_assert (ct
== mcLexBuf_currenttoken
);
535 /* static analysis guarentees a RETURN statement will be used before here. */
536 __builtin_unreachable ();
541 peepAfterComment - peeps ahead looking for an after statement comment. It stops at an END token
542 or if the line number changes.
545 static void peepAfterComment (void)
547 unsigned int oldTokNo
;
551 unsigned int nextline
;
552 unsigned int curline
;
553 mcLexBuf_tokenBucket b
;
556 oldTokNo
= nextTokNo
;
557 cno
= mcLexBuf_getTokenNo ();
558 curline
= mcLexBuf_tokenToLineNo (cno
, 0);
564 b
= peeptokenBucket (&t
);
565 if ((b
== NULL
) || (mcLexBuf_currenttoken
== mcReserved_eoftok
))
571 nextline
= b
->buf
.array
[t
].line
;
572 if (nextline
== curline
)
574 switch (b
->buf
.array
[t
].token
)
576 case mcReserved_eoftok
:
577 case mcReserved_endtok
:
581 case mcReserved_commenttok
:
582 if (mcComment_isAfterComment (b
->buf
.array
[t
].com
))
584 afterComment
= b
->buf
.array
[t
].com
;
599 } while (! (finished
));
605 init - initializes the token list and source list.
608 static void init (void)
610 mcLexBuf_currenttoken
= mcReserved_eoftok
;
612 currentSource
= NULL
;
613 listOfTokens
.head
= NULL
;
614 listOfTokens
.tail
= NULL
;
615 useBufferedTokens
= false;
616 procedureComment
= static_cast<mcComment_commentDesc
> (NULL
);
617 bodyComment
= static_cast<mcComment_commentDesc
> (NULL
);
618 afterComment
= static_cast<mcComment_commentDesc
> (NULL
);
619 mcLexBuf_lastcomment
= static_cast<mcComment_commentDesc
> (NULL
);
624 addTo - adds a new element to the end of sourceList, currentSource.
627 static void addTo (mcLexBuf_sourceList l
)
629 l
->right
= currentSource
;
630 l
->left
= currentSource
->left
;
631 currentSource
->left
->right
= l
;
632 currentSource
->left
= l
;
633 l
->left
->line
= mcflex_getLineNo ();
634 l
->left
->col
= mcflex_getColumnNo ();
639 subFrom - subtracts, l, from the source list.
642 static void subFrom (mcLexBuf_sourceList l
)
644 l
->left
->right
= l
->right
;
645 l
->right
->left
= l
->left
;
650 newElement - returns a new sourceList
653 static mcLexBuf_sourceList
newElement (void * s
)
655 mcLexBuf_sourceList l
;
657 Storage_ALLOCATE ((void **) &l
, sizeof (mcLexBuf__T1
));
661 __builtin_unreachable ();
665 l
->name
= DynamicStrings_InitStringCharStar (s
);
670 /* static analysis guarentees a RETURN statement will be used before here. */
671 __builtin_unreachable ();
676 newList - initializes an empty list with the classic dummy header element.
679 static mcLexBuf_sourceList
newList (void)
681 mcLexBuf_sourceList l
;
683 Storage_ALLOCATE ((void **) &l
, sizeof (mcLexBuf__T1
));
686 l
->name
= static_cast<DynamicStrings_String
> (NULL
);
688 /* static analysis guarentees a RETURN statement will be used before here. */
689 __builtin_unreachable ();
694 checkIfNeedToDuplicate - checks to see whether the currentSource has
695 been used, if it has then duplicate the list.
698 static void checkIfNeedToDuplicate (void)
700 mcLexBuf_sourceList l
;
701 mcLexBuf_sourceList h
;
705 l
= currentSource
->right
;
707 currentSource
= newList ();
710 addTo (newElement (reinterpret_cast <void *> (l
->name
)));
718 killList - kills the sourceList providing that it has not been used.
721 static void killList (void)
723 mcLexBuf_sourceList l
;
724 mcLexBuf_sourceList k
;
726 if (! currentUsed
&& (currentSource
!= NULL
))
732 Storage_DEALLOCATE ((void **) &k
, sizeof (mcLexBuf__T1
));
733 } while (! (l
== currentSource
));
742 static void displayToken (mcReserved_toktype t
)
746 case mcReserved_eoftok
:
747 mcPrintf_printf0 ((const char *) "eoftok\\n", 8);
750 case mcReserved_plustok
:
751 mcPrintf_printf0 ((const char *) "plustok\\n", 9);
754 case mcReserved_minustok
:
755 mcPrintf_printf0 ((const char *) "minustok\\n", 10);
758 case mcReserved_timestok
:
759 mcPrintf_printf0 ((const char *) "timestok\\n", 10);
762 case mcReserved_dividetok
:
763 mcPrintf_printf0 ((const char *) "dividetok\\n", 11);
766 case mcReserved_becomestok
:
767 mcPrintf_printf0 ((const char *) "becomestok\\n", 12);
770 case mcReserved_ambersandtok
:
771 mcPrintf_printf0 ((const char *) "ambersandtok\\n", 14);
774 case mcReserved_periodtok
:
775 mcPrintf_printf0 ((const char *) "periodtok\\n", 11);
778 case mcReserved_commatok
:
779 mcPrintf_printf0 ((const char *) "commatok\\n", 10);
782 case mcReserved_commenttok
:
783 mcPrintf_printf0 ((const char *) "commenttok\\n", 12);
786 case mcReserved_semicolontok
:
787 mcPrintf_printf0 ((const char *) "semicolontok\\n", 14);
790 case mcReserved_lparatok
:
791 mcPrintf_printf0 ((const char *) "lparatok\\n", 10);
794 case mcReserved_rparatok
:
795 mcPrintf_printf0 ((const char *) "rparatok\\n", 10);
798 case mcReserved_lsbratok
:
799 mcPrintf_printf0 ((const char *) "lsbratok\\n", 10);
802 case mcReserved_rsbratok
:
803 mcPrintf_printf0 ((const char *) "rsbratok\\n", 10);
806 case mcReserved_lcbratok
:
807 mcPrintf_printf0 ((const char *) "lcbratok\\n", 10);
810 case mcReserved_rcbratok
:
811 mcPrintf_printf0 ((const char *) "rcbratok\\n", 10);
814 case mcReserved_uparrowtok
:
815 mcPrintf_printf0 ((const char *) "uparrowtok\\n", 12);
818 case mcReserved_singlequotetok
:
819 mcPrintf_printf0 ((const char *) "singlequotetok\\n", 16);
822 case mcReserved_equaltok
:
823 mcPrintf_printf0 ((const char *) "equaltok\\n", 10);
826 case mcReserved_hashtok
:
827 mcPrintf_printf0 ((const char *) "hashtok\\n", 9);
830 case mcReserved_lesstok
:
831 mcPrintf_printf0 ((const char *) "lesstok\\n", 9);
834 case mcReserved_greatertok
:
835 mcPrintf_printf0 ((const char *) "greatertok\\n", 12);
838 case mcReserved_lessgreatertok
:
839 mcPrintf_printf0 ((const char *) "lessgreatertok\\n", 16);
842 case mcReserved_lessequaltok
:
843 mcPrintf_printf0 ((const char *) "lessequaltok\\n", 14);
846 case mcReserved_greaterequaltok
:
847 mcPrintf_printf0 ((const char *) "greaterequaltok\\n", 17);
850 case mcReserved_periodperiodtok
:
851 mcPrintf_printf0 ((const char *) "periodperiodtok\\n", 17);
854 case mcReserved_colontok
:
855 mcPrintf_printf0 ((const char *) "colontok\\n", 10);
858 case mcReserved_doublequotestok
:
859 mcPrintf_printf0 ((const char *) "doublequotestok\\n", 17);
862 case mcReserved_bartok
:
863 mcPrintf_printf0 ((const char *) "bartok\\n", 8);
866 case mcReserved_andtok
:
867 mcPrintf_printf0 ((const char *) "andtok\\n", 8);
870 case mcReserved_arraytok
:
871 mcPrintf_printf0 ((const char *) "arraytok\\n", 10);
874 case mcReserved_begintok
:
875 mcPrintf_printf0 ((const char *) "begintok\\n", 10);
878 case mcReserved_bytok
:
879 mcPrintf_printf0 ((const char *) "bytok\\n", 7);
882 case mcReserved_casetok
:
883 mcPrintf_printf0 ((const char *) "casetok\\n", 9);
886 case mcReserved_consttok
:
887 mcPrintf_printf0 ((const char *) "consttok\\n", 10);
890 case mcReserved_definitiontok
:
891 mcPrintf_printf0 ((const char *) "definitiontok\\n", 15);
894 case mcReserved_divtok
:
895 mcPrintf_printf0 ((const char *) "divtok\\n", 8);
898 case mcReserved_dotok
:
899 mcPrintf_printf0 ((const char *) "dotok\\n", 7);
902 case mcReserved_elsetok
:
903 mcPrintf_printf0 ((const char *) "elsetok\\n", 9);
906 case mcReserved_elsiftok
:
907 mcPrintf_printf0 ((const char *) "elsiftok\\n", 10);
910 case mcReserved_endtok
:
911 mcPrintf_printf0 ((const char *) "endtok\\n", 8);
914 case mcReserved_exittok
:
915 mcPrintf_printf0 ((const char *) "exittok\\n", 9);
918 case mcReserved_exporttok
:
919 mcPrintf_printf0 ((const char *) "exporttok\\n", 11);
922 case mcReserved_fortok
:
923 mcPrintf_printf0 ((const char *) "fortok\\n", 8);
926 case mcReserved_fromtok
:
927 mcPrintf_printf0 ((const char *) "fromtok\\n", 9);
930 case mcReserved_iftok
:
931 mcPrintf_printf0 ((const char *) "iftok\\n", 7);
934 case mcReserved_implementationtok
:
935 mcPrintf_printf0 ((const char *) "implementationtok\\n", 19);
938 case mcReserved_importtok
:
939 mcPrintf_printf0 ((const char *) "importtok\\n", 11);
942 case mcReserved_intok
:
943 mcPrintf_printf0 ((const char *) "intok\\n", 7);
946 case mcReserved_looptok
:
947 mcPrintf_printf0 ((const char *) "looptok\\n", 9);
950 case mcReserved_modtok
:
951 mcPrintf_printf0 ((const char *) "modtok\\n", 8);
954 case mcReserved_moduletok
:
955 mcPrintf_printf0 ((const char *) "moduletok\\n", 11);
958 case mcReserved_nottok
:
959 mcPrintf_printf0 ((const char *) "nottok\\n", 8);
962 case mcReserved_oftok
:
963 mcPrintf_printf0 ((const char *) "oftok\\n", 7);
966 case mcReserved_ortok
:
967 mcPrintf_printf0 ((const char *) "ortok\\n", 7);
970 case mcReserved_pointertok
:
971 mcPrintf_printf0 ((const char *) "pointertok\\n", 12);
974 case mcReserved_proceduretok
:
975 mcPrintf_printf0 ((const char *) "proceduretok\\n", 14);
978 case mcReserved_qualifiedtok
:
979 mcPrintf_printf0 ((const char *) "qualifiedtok\\n", 14);
982 case mcReserved_unqualifiedtok
:
983 mcPrintf_printf0 ((const char *) "unqualifiedtok\\n", 16);
986 case mcReserved_recordtok
:
987 mcPrintf_printf0 ((const char *) "recordtok\\n", 11);
990 case mcReserved_repeattok
:
991 mcPrintf_printf0 ((const char *) "repeattok\\n", 11);
994 case mcReserved_returntok
:
995 mcPrintf_printf0 ((const char *) "returntok\\n", 11);
998 case mcReserved_settok
:
999 mcPrintf_printf0 ((const char *) "settok\\n", 8);
1002 case mcReserved_thentok
:
1003 mcPrintf_printf0 ((const char *) "thentok\\n", 9);
1006 case mcReserved_totok
:
1007 mcPrintf_printf0 ((const char *) "totok\\n", 7);
1010 case mcReserved_typetok
:
1011 mcPrintf_printf0 ((const char *) "typetok\\n", 9);
1014 case mcReserved_untiltok
:
1015 mcPrintf_printf0 ((const char *) "untiltok\\n", 10);
1018 case mcReserved_vartok
:
1019 mcPrintf_printf0 ((const char *) "vartok\\n", 8);
1022 case mcReserved_whiletok
:
1023 mcPrintf_printf0 ((const char *) "whiletok\\n", 10);
1026 case mcReserved_withtok
:
1027 mcPrintf_printf0 ((const char *) "withtok\\n", 9);
1030 case mcReserved_asmtok
:
1031 mcPrintf_printf0 ((const char *) "asmtok\\n", 8);
1034 case mcReserved_volatiletok
:
1035 mcPrintf_printf0 ((const char *) "volatiletok\\n", 13);
1038 case mcReserved_periodperiodperiodtok
:
1039 mcPrintf_printf0 ((const char *) "periodperiodperiodtok\\n", 23);
1042 case mcReserved_datetok
:
1043 mcPrintf_printf0 ((const char *) "datetok\\n", 9);
1046 case mcReserved_linetok
:
1047 mcPrintf_printf0 ((const char *) "linetok\\n", 9);
1050 case mcReserved_filetok
:
1051 mcPrintf_printf0 ((const char *) "filetok\\n", 9);
1054 case mcReserved_integertok
:
1055 mcPrintf_printf0 ((const char *) "integertok\\n", 12);
1058 case mcReserved_identtok
:
1059 mcPrintf_printf0 ((const char *) "identtok\\n", 10);
1062 case mcReserved_realtok
:
1063 mcPrintf_printf0 ((const char *) "realtok\\n", 9);
1066 case mcReserved_stringtok
:
1067 mcPrintf_printf0 ((const char *) "stringtok\\n", 11);
1072 mcPrintf_printf0 ((const char *) "unknown tok (--fixme--)\\n", 25);
1079 updateFromBucket - updates the global variables: currenttoken,
1080 currentstring, currentcolumn and currentinteger
1081 from tokenBucket, b, and, offset.
1084 static void updateFromBucket (mcLexBuf_tokenBucket b
, unsigned int offset
)
1086 mcLexBuf_currenttoken
= b
->buf
.array
[offset
].token
;
1087 mcLexBuf_currentstring
= nameKey_keyToCharStar (b
->buf
.array
[offset
].str
);
1088 mcLexBuf_currentcolumn
= b
->buf
.array
[offset
].col
;
1089 mcLexBuf_currentinteger
= b
->buf
.array
[offset
].int_
;
1090 mcLexBuf_currentcomment
= b
->buf
.array
[offset
].com
;
1091 if (mcLexBuf_currentcomment
!= NULL
)
1093 mcLexBuf_lastcomment
= mcLexBuf_currentcomment
;
1097 mcPrintf_printf3 ((const char *) "line %d (# %d %d) ", 19, (const unsigned char *) &b
->buf
.array
[offset
].line
, (sizeof (b
->buf
.array
[offset
].line
)-1), (const unsigned char *) &offset
, (sizeof (offset
)-1), (const unsigned char *) &nextTokNo
, (sizeof (nextTokNo
)-1));
1103 doGetToken - fetch the next token into currenttoken.
1106 static void doGetToken (void)
1110 mcLexBuf_tokenBucket b
;
1112 if (useBufferedTokens
)
1115 b
= findtokenBucket (&t
);
1116 updateFromBucket (b
, t
);
1120 if (listOfTokens
.tail
== NULL
)
1122 a
= mcflex_getToken ();
1123 if (listOfTokens
.tail
== NULL
)
1126 __builtin_unreachable ();
1129 if (nextTokNo
>= listOfTokens
.lastBucketOffset
)
1131 /* nextTokNo is in the last bucket or needs to be read. */
1132 if ((nextTokNo
-listOfTokens
.lastBucketOffset
) < listOfTokens
.tail
->len
)
1136 mcPrintf_printf0 ((const char *) "fetching token from buffer (updateFromBucket)\\n", 47);
1138 updateFromBucket (listOfTokens
.tail
, nextTokNo
-listOfTokens
.lastBucketOffset
);
1144 mcPrintf_printf0 ((const char *) "calling flex to place token into buffer\\n", 41);
1146 /* call the lexical phase to place a new token into the last bucket. */
1147 a
= mcflex_getToken ();
1148 mcLexBuf_getToken (); /* and call ourselves again to collect the token from bucket. */
1149 return; /* and call ourselves again to collect the token from bucket. */
1156 mcPrintf_printf0 ((const char *) "fetching token from buffer\\n", 28);
1159 b
= findtokenBucket (&t
);
1160 updateFromBucket (b
, t
);
1165 displayToken (mcLexBuf_currenttoken
);
1172 syncOpenWithBuffer - synchronise the buffer with the start of a file.
1173 Skips all the tokens to do with the previous file.
1176 static void syncOpenWithBuffer (void)
1178 if (listOfTokens
.tail
!= NULL
)
1180 nextTokNo
= listOfTokens
.lastBucketOffset
+listOfTokens
.tail
->len
;
1186 findtokenBucket - returns the tokenBucket corresponding to the tokenNo.
1189 static mcLexBuf_tokenBucket
findtokenBucket (unsigned int *tokenNo
)
1191 mcLexBuf_tokenBucket b
;
1193 b
= listOfTokens
.head
;
1196 if ((*tokenNo
) < b
->len
)
1202 (*tokenNo
) -= b
->len
;
1207 /* static analysis guarentees a RETURN statement will be used before here. */
1208 __builtin_unreachable ();
1213 getFileName - returns a String defining the current file.
1216 static void stop (void)
1222 addTokToList - adds a token to a dynamic list.
1225 static void addTokToList (mcReserved_toktype t
, nameKey_Name n
, int i
, mcComment_commentDesc comment
, unsigned int l
, unsigned int c
, mcLexBuf_sourceList f
)
1227 mcLexBuf_tokenBucket b
;
1229 if (listOfTokens
.head
== NULL
)
1231 Storage_ALLOCATE ((void **) &listOfTokens
.head
, sizeof (mcLexBuf__T2
));
1232 if (listOfTokens
.head
== NULL
)
1235 listOfTokens
.tail
= listOfTokens
.head
;
1236 listOfTokens
.tail
->len
= 0;
1238 else if (listOfTokens
.tail
->len
== MaxBucketSize
)
1240 /* avoid dangling else. */
1241 mcDebug_assert (listOfTokens
.tail
->next
== NULL
);
1242 Storage_ALLOCATE ((void **) &listOfTokens
.tail
->next
, sizeof (mcLexBuf__T2
));
1243 if (listOfTokens
.tail
->next
== NULL
)
1248 listOfTokens
.tail
= listOfTokens
.tail
->next
;
1249 listOfTokens
.tail
->len
= 0;
1251 listOfTokens
.lastBucketOffset
+= MaxBucketSize
;
1253 listOfTokens
.tail
->next
= NULL
;
1254 mcDebug_assert (listOfTokens
.tail
->len
!= MaxBucketSize
);
1255 listOfTokens
.tail
->buf
.array
[listOfTokens
.tail
->len
].token
= t
;
1256 listOfTokens
.tail
->buf
.array
[listOfTokens
.tail
->len
].str
= n
;
1257 listOfTokens
.tail
->buf
.array
[listOfTokens
.tail
->len
].int_
= i
;
1258 listOfTokens
.tail
->buf
.array
[listOfTokens
.tail
->len
].com
= comment
;
1259 listOfTokens
.tail
->buf
.array
[listOfTokens
.tail
->len
].line
= l
;
1260 listOfTokens
.tail
->buf
.array
[listOfTokens
.tail
->len
].col
= c
;
1261 listOfTokens
.tail
->buf
.array
[listOfTokens
.tail
->len
].file
= f
;
1262 listOfTokens
.tail
->len
+= 1;
1267 isLastTokenEof - returns TRUE if the last token was an eoftok
1270 static bool isLastTokenEof (void)
1273 mcLexBuf_tokenBucket b
;
1275 if (listOfTokens
.tail
!= NULL
)
1277 if (listOfTokens
.tail
->len
== 0)
1279 b
= listOfTokens
.head
;
1280 if (b
== listOfTokens
.tail
)
1284 while (b
->next
!= listOfTokens
.tail
)
1291 b
= listOfTokens
.tail
;
1293 mcDebug_assert (b
->len
> 0); /* len should always be >0 */
1294 return b
->buf
.array
[b
->len
-1].token
== mcReserved_eoftok
; /* len should always be >0 */
1297 /* static analysis guarentees a RETURN statement will be used before here. */
1298 __builtin_unreachable ();
1303 getProcedureComment - returns the procedure comment if it exists,
1307 extern "C" mcComment_commentDesc
mcLexBuf_getProcedureComment (void)
1309 return procedureComment
;
1310 /* static analysis guarentees a RETURN statement will be used before here. */
1311 __builtin_unreachable ();
1316 getBodyComment - returns the body comment if it exists,
1317 or NIL otherwise. The body comment is
1321 extern "C" mcComment_commentDesc
mcLexBuf_getBodyComment (void)
1323 mcComment_commentDesc b
;
1326 bodyComment
= static_cast<mcComment_commentDesc
> (NULL
);
1328 /* static analysis guarentees a RETURN statement will be used before here. */
1329 __builtin_unreachable ();
1334 getAfterComment - returns the after comment if it exists,
1335 or NIL otherwise. The after comment is
1339 extern "C" mcComment_commentDesc
mcLexBuf_getAfterComment (void)
1341 mcComment_commentDesc a
;
1343 peepAfterComment ();
1345 afterComment
= static_cast<mcComment_commentDesc
> (NULL
);
1347 /* static analysis guarentees a RETURN statement will be used before here. */
1348 __builtin_unreachable ();
1353 openSource - attempts to open the source file, s.
1354 The success of the operation is returned.
1357 extern "C" bool mcLexBuf_openSource (DynamicStrings_String s
)
1359 if (useBufferedTokens
)
1361 mcLexBuf_getToken ();
1366 if (mcflex_openSource (DynamicStrings_string (s
)))
1368 mcLexBuf_setFile (DynamicStrings_string (s
));
1369 syncOpenWithBuffer ();
1370 mcLexBuf_getToken ();
1378 /* static analysis guarentees a RETURN statement will be used before here. */
1379 __builtin_unreachable ();
1384 closeSource - closes the current open file.
1387 extern "C" void mcLexBuf_closeSource (void)
1389 if (useBufferedTokens
)
1391 while (mcLexBuf_currenttoken
!= mcReserved_eoftok
)
1393 mcLexBuf_getToken ();
1396 /* a subsequent call to mcflex.OpenSource will really close the file */
1401 reInitialize - re-initialize the all the data structures.
1404 extern "C" void mcLexBuf_reInitialize (void)
1406 mcLexBuf_tokenBucket s
;
1407 mcLexBuf_tokenBucket t
;
1409 if (listOfTokens
.head
!= NULL
)
1411 t
= listOfTokens
.head
;
1415 Storage_DEALLOCATE ((void **) &s
, sizeof (mcLexBuf__T2
));
1416 } while (! (t
== NULL
));
1417 currentUsed
= false;
1425 resetForNewPass - reset the buffer pointers to the beginning ready for
1429 extern "C" void mcLexBuf_resetForNewPass (void)
1432 useBufferedTokens
= true;
1437 getToken - gets the next token into currenttoken.
1440 extern "C" void mcLexBuf_getToken (void)
1444 if (mcLexBuf_currenttoken
== mcReserved_commenttok
)
1446 /* avoid gcc warning by using compound statement even if not strictly necessary. */
1447 if (mcComment_isProcedureComment (mcLexBuf_currentcomment
))
1449 procedureComment
= mcLexBuf_currentcomment
;
1450 bodyComment
= static_cast<mcComment_commentDesc
> (NULL
);
1451 afterComment
= static_cast<mcComment_commentDesc
> (NULL
);
1453 else if (mcComment_isBodyComment (mcLexBuf_currentcomment
))
1455 /* avoid dangling else. */
1456 bodyComment
= mcLexBuf_currentcomment
;
1457 afterComment
= static_cast<mcComment_commentDesc
> (NULL
);
1459 else if (mcComment_isAfterComment (mcLexBuf_currentcomment
))
1461 /* avoid dangling else. */
1462 procedureComment
= static_cast<mcComment_commentDesc
> (NULL
);
1463 bodyComment
= static_cast<mcComment_commentDesc
> (NULL
);
1464 afterComment
= mcLexBuf_currentcomment
;
1467 } while (! (mcLexBuf_currenttoken
!= mcReserved_commenttok
));
1472 insertToken - inserts a symbol, token, infront of the current token
1473 ready for the next pass.
1476 extern "C" void mcLexBuf_insertToken (mcReserved_toktype token
)
1478 if (listOfTokens
.tail
!= NULL
)
1480 if (listOfTokens
.tail
->len
> 0)
1482 listOfTokens
.tail
->buf
.array
[listOfTokens
.tail
->len
-1].token
= token
;
1484 addTokToList (mcLexBuf_currenttoken
, nameKey_NulName
, 0, static_cast<mcComment_commentDesc
> (NULL
), mcLexBuf_getLineNo (), mcLexBuf_getColumnNo (), currentSource
);
1485 mcLexBuf_getToken ();
1491 insertTokenAndRewind - inserts a symbol, token, infront of the current token
1492 and then moves the token stream back onto the inserted token.
1495 extern "C" void mcLexBuf_insertTokenAndRewind (mcReserved_toktype token
)
1497 if (listOfTokens
.tail
!= NULL
)
1499 if (listOfTokens
.tail
->len
> 0)
1501 listOfTokens
.tail
->buf
.array
[listOfTokens
.tail
->len
-1].token
= token
;
1503 addTokToList (mcLexBuf_currenttoken
, nameKey_NulName
, 0, static_cast<mcComment_commentDesc
> (NULL
), mcLexBuf_getLineNo (), mcLexBuf_getColumnNo (), currentSource
);
1504 mcLexBuf_currenttoken
= token
;
1510 getPreviousTokenLineNo - returns the line number of the previous token.
1513 extern "C" unsigned int mcLexBuf_getPreviousTokenLineNo (void)
1515 return mcLexBuf_getLineNo ();
1516 /* static analysis guarentees a RETURN statement will be used before here. */
1517 __builtin_unreachable ();
1522 getLineNo - returns the current line number where the symbol occurs in
1526 extern "C" unsigned int mcLexBuf_getLineNo (void)
1534 return mcLexBuf_tokenToLineNo (mcLexBuf_getTokenNo (), 0);
1536 /* static analysis guarentees a RETURN statement will be used before here. */
1537 __builtin_unreachable ();
1542 getTokenNo - returns the current token number.
1545 extern "C" unsigned int mcLexBuf_getTokenNo (void)
1555 /* static analysis guarentees a RETURN statement will be used before here. */
1556 __builtin_unreachable ();
1561 tokenToLineNo - returns the line number of the current file for the
1562 tokenNo. The depth refers to the include depth.
1563 A depth of 0 is the current file, depth of 1 is the file
1564 which included the current file. Zero is returned if the
1565 depth exceeds the file nesting level.
1568 extern "C" unsigned int mcLexBuf_tokenToLineNo (unsigned int tokenNo
, unsigned int depth
)
1570 mcLexBuf_tokenBucket b
;
1571 mcLexBuf_sourceList l
;
1573 b
= findtokenBucket (&tokenNo
);
1582 return b
->buf
.array
[tokenNo
].line
;
1586 l
= b
->buf
.array
[tokenNo
].file
->left
;
1590 if (l
== b
->buf
.array
[tokenNo
].file
->left
)
1599 /* static analysis guarentees a RETURN statement will be used before here. */
1600 __builtin_unreachable ();
1605 getColumnNo - returns the current column where the symbol occurs in
1609 extern "C" unsigned int mcLexBuf_getColumnNo (void)
1617 return mcLexBuf_tokenToColumnNo (mcLexBuf_getTokenNo (), 0);
1619 /* static analysis guarentees a RETURN statement will be used before here. */
1620 __builtin_unreachable ();
1625 tokenToColumnNo - returns the column number of the current file for the
1626 tokenNo. The depth refers to the include depth.
1627 A depth of 0 is the current file, depth of 1 is the file
1628 which included the current file. Zero is returned if the
1629 depth exceeds the file nesting level.
1632 extern "C" unsigned int mcLexBuf_tokenToColumnNo (unsigned int tokenNo
, unsigned int depth
)
1634 mcLexBuf_tokenBucket b
;
1635 mcLexBuf_sourceList l
;
1637 b
= findtokenBucket (&tokenNo
);
1646 return b
->buf
.array
[tokenNo
].col
;
1650 l
= b
->buf
.array
[tokenNo
].file
->left
;
1654 if (l
== b
->buf
.array
[tokenNo
].file
->left
)
1663 /* static analysis guarentees a RETURN statement will be used before here. */
1664 __builtin_unreachable ();
1669 findFileNameFromToken - returns the complete FileName for the appropriate
1670 source file yields the token number, tokenNo.
1671 The, Depth, indicates the include level: 0..n
1672 Level 0 is the current. NIL is returned if n+1
1676 extern "C" DynamicStrings_String
mcLexBuf_findFileNameFromToken (unsigned int tokenNo
, unsigned int depth
)
1678 mcLexBuf_tokenBucket b
;
1679 mcLexBuf_sourceList l
;
1681 b
= findtokenBucket (&tokenNo
);
1684 return static_cast<DynamicStrings_String
> (NULL
);
1688 l
= b
->buf
.array
[tokenNo
].file
->left
;
1692 if (l
== b
->buf
.array
[tokenNo
].file
->left
)
1694 return static_cast<DynamicStrings_String
> (NULL
);
1700 /* static analysis guarentees a RETURN statement will be used before here. */
1701 __builtin_unreachable ();
1706 getFileName - returns a String defining the current file.
1709 extern "C" DynamicStrings_String
mcLexBuf_getFileName (void)
1711 return mcLexBuf_findFileNameFromToken (mcLexBuf_getTokenNo (), 0);
1712 /* static analysis guarentees a RETURN statement will be used before here. */
1713 __builtin_unreachable ();
1718 addTok - adds a token to the buffer.
1721 extern "C" void mcLexBuf_addTok (mcReserved_toktype t
)
1723 if (! ((t
== mcReserved_eoftok
) && (isLastTokenEof ())))
1725 addTokToList (t
, nameKey_NulName
, 0, static_cast<mcComment_commentDesc
> (NULL
), mcflex_getLineNo (), mcflex_getColumnNo (), currentSource
);
1732 addTokCharStar - adds a token to the buffer and an additional string, s.
1733 A copy of string, s, is made.
1736 extern "C" void mcLexBuf_addTokCharStar (mcReserved_toktype t
, void * s
)
1738 if ((libc_strlen (s
)) > 80)
1742 addTokToList (t
, nameKey_makekey (s
), 0, static_cast<mcComment_commentDesc
> (NULL
), mcflex_getLineNo (), mcflex_getColumnNo (), currentSource
);
1748 addTokInteger - adds a token and an integer to the buffer.
1751 extern "C" void mcLexBuf_addTokInteger (mcReserved_toktype t
, int i
)
1753 DynamicStrings_String s
;
1757 l
= mcflex_getLineNo ();
1758 c
= mcflex_getColumnNo ();
1759 s
= FormatStrings_Sprintf1 (DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "%d", 2)), (const unsigned char *) &i
, (sizeof (i
)-1));
1760 addTokToList (t
, nameKey_makekey (DynamicStrings_string (s
)), i
, static_cast<mcComment_commentDesc
> (NULL
), l
, c
, currentSource
);
1761 s
= DynamicStrings_KillString (s
);
1767 addTokComment - adds a token to the buffer and a comment descriptor, com.
1770 extern "C" void mcLexBuf_addTokComment (mcReserved_toktype t
, mcComment_commentDesc com
)
1772 addTokToList (t
, nameKey_NulName
, 0, com
, mcflex_getLineNo (), mcflex_getColumnNo (), currentSource
);
1778 setFile - sets the current filename to, filename.
1781 extern "C" void mcLexBuf_setFile (void * filename
)
1784 currentUsed
= false;
1785 currentSource
= newList ();
1786 addTo (newElement (filename
));
1791 pushFile - indicates that, filename, has just been included.
1794 extern "C" void mcLexBuf_pushFile (void * filename
)
1796 mcLexBuf_sourceList l
;
1798 checkIfNeedToDuplicate ();
1799 addTo (newElement (filename
));
1802 if (currentSource
->right
!= currentSource
)
1806 mcPrintf_printf3 ((const char *) "name = %s, line = %d, col = %d\\n", 32, (const unsigned char *) &l
->name
, (sizeof (l
->name
)-1), (const unsigned char *) &l
->line
, (sizeof (l
->line
)-1), (const unsigned char *) &l
->col
, (sizeof (l
->col
)-1));
1808 } while (! (l
== currentSource
));
1815 popFile - indicates that we are returning to, filename, having finished
1819 extern "C" void mcLexBuf_popFile (void * filename
)
1821 mcLexBuf_sourceList l
;
1823 checkIfNeedToDuplicate ();
1824 if ((currentSource
!= NULL
) && (currentSource
->left
!= currentSource
))
1826 /* avoid dangling else. */
1827 l
= currentSource
->left
; /* last element */
1828 subFrom (l
); /* last element */
1829 Storage_DEALLOCATE ((void **) &l
, sizeof (mcLexBuf__T1
));
1830 if ((currentSource
->left
!= currentSource
) && (! (DynamicStrings_Equal (currentSource
->name
, DynamicStrings_Mark (DynamicStrings_InitStringCharStar (filename
))))))
1832 /* mismatch in source file names after preprocessing files */
1834 /* source file list is empty, cannot pop an include.. */
1837 extern "C" void _M2_mcLexBuf_init (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])
1842 extern "C" void _M2_mcLexBuf_fini (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])