Debian Codegraph @

gcc/m2/mc-boot/GmcLexBuf.cc

raw
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.
3
4 Copyright (C) 2015-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
23 #include "config.h"
24 #include "system.h"
25 #include <stdbool.h>
26 # if !defined (PROC_D)
27 # define PROC_D
28 typedef void (*PROC_t) (void);
29 typedef struct { PROC_t proc; } PROC;
30 # endif
31
32 # if !defined (TRUE)
33 # define TRUE (1==1)
34 # endif
35
36 # if !defined (FALSE)
37 # define FALSE (1==0)
38 # endif
39
40 # include "GStorage.h"
41 #if defined(__cplusplus)
42 # undef NULL
43 # define NULL 0
44 #endif
45 #define _mcLexBuf_C
46
47 #include "GmcLexBuf.h"
48 # include "Gmcflex.h"
49 # include "Glibc.h"
50 # include "GSYSTEM.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"
59 # include "GM2RTS.h"
60
61 # define MaxBucketSize 100
62 # define Debugging false
63 typedef struct mcLexBuf_tokenDesc_r mcLexBuf_tokenDesc;
64
65 typedef struct mcLexBuf_listDesc_r mcLexBuf_listDesc;
66
67 typedef struct mcLexBuf__T1_r mcLexBuf__T1;
68
69 typedef mcLexBuf__T1 *mcLexBuf_sourceList;
70
71 typedef struct mcLexBuf__T2_r mcLexBuf__T2;
72
73 typedef mcLexBuf__T2 *mcLexBuf_tokenBucket;
74
75 typedef struct mcLexBuf__T3_a mcLexBuf__T3;
76
77 struct mcLexBuf_tokenDesc_r {
78 mcReserved_toktype token;
79 nameKey_Name str;
80 int int_;
81 mcComment_commentDesc com;
82 unsigned int line;
83 unsigned int col;
84 mcLexBuf_sourceList file;
85 };
86
87 struct mcLexBuf_listDesc_r {
88 mcLexBuf_tokenBucket head;
89 mcLexBuf_tokenBucket tail;
90 unsigned int lastBucketOffset;
91 };
92
93 struct mcLexBuf__T1_r {
94 mcLexBuf_sourceList left;
95 mcLexBuf_sourceList right;
96 DynamicStrings_String name;
97 unsigned int line;
98 unsigned int col;
99 };
100
101 struct mcLexBuf__T3_a { mcLexBuf_tokenDesc array[MaxBucketSize+1]; };
102 struct mcLexBuf__T2_r {
103 mcLexBuf__T3 buf;
104 unsigned int len;
105 mcLexBuf_tokenBucket next;
106 };
107
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;
116
117 /*
118 getProcedureComment - returns the procedure comment if it exists,
119 or NIL otherwise.
120 */
121
122 extern "C" mcComment_commentDesc mcLexBuf_getProcedureComment (void);
123
124 /*
125 getBodyComment - returns the body comment if it exists,
126 or NIL otherwise. The body comment is
127 removed if found.
128 */
129
130 extern "C" mcComment_commentDesc mcLexBuf_getBodyComment (void);
131
132 /*
133 getAfterComment - returns the after comment if it exists,
134 or NIL otherwise. The after comment is
135 removed if found.
136 */
137
138 extern "C" mcComment_commentDesc mcLexBuf_getAfterComment (void);
139
140 /*
141 openSource - attempts to open the source file, s.
142 The success of the operation is returned.
143 */
144
145 extern "C" bool mcLexBuf_openSource (DynamicStrings_String s);
146
147 /*
148 closeSource - closes the current open file.
149 */
150
151 extern "C" void mcLexBuf_closeSource (void);
152
153 /*
154 reInitialize - re-initialize the all the data structures.
155 */
156
157 extern "C" void mcLexBuf_reInitialize (void);
158
159 /*
160 resetForNewPass - reset the buffer pointers to the beginning ready for
161 a new pass
162 */
163
164 extern "C" void mcLexBuf_resetForNewPass (void);
165
166 /*
167 getToken - gets the next token into currenttoken.
168 */
169
170 extern "C" void mcLexBuf_getToken (void);
171
172 /*
173 insertToken - inserts a symbol, token, infront of the current token
174 ready for the next pass.
175 */
176
177 extern "C" void mcLexBuf_insertToken (mcReserved_toktype token);
178
179 /*
180 insertTokenAndRewind - inserts a symbol, token, infront of the current token
181 and then moves the token stream back onto the inserted token.
182 */
183
184 extern "C" void mcLexBuf_insertTokenAndRewind (mcReserved_toktype token);
185
186 /*
187 getPreviousTokenLineNo - returns the line number of the previous token.
188 */
189
190 extern "C" unsigned int mcLexBuf_getPreviousTokenLineNo (void);
191
192 /*
193 getLineNo - returns the current line number where the symbol occurs in
194 the source file.
195 */
196
197 extern "C" unsigned int mcLexBuf_getLineNo (void);
198
199 /*
200 getTokenNo - returns the current token number.
201 */
202
203 extern "C" unsigned int mcLexBuf_getTokenNo (void);
204
205 /*
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.
211 */
212
213 extern "C" unsigned int mcLexBuf_tokenToLineNo (unsigned int tokenNo, unsigned int depth);
214
215 /*
216 getColumnNo - returns the current column where the symbol occurs in
217 the source file.
218 */
219
220 extern "C" unsigned int mcLexBuf_getColumnNo (void);
221
222 /*
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.
228 */
229
230 extern "C" unsigned int mcLexBuf_tokenToColumnNo (unsigned int tokenNo, unsigned int depth);
231
232 /*
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
237 is requested.
238 */
239
240 extern "C" DynamicStrings_String mcLexBuf_findFileNameFromToken (unsigned int tokenNo, unsigned int depth);
241
242 /*
243 getFileName - returns a String defining the current file.
244 */
245
246 extern "C" DynamicStrings_String mcLexBuf_getFileName (void);
247
248 /*
249 addTok - adds a token to the buffer.
250 */
251
252 extern "C" void mcLexBuf_addTok (mcReserved_toktype t);
253
254 /*
255 addTokCharStar - adds a token to the buffer and an additional string, s.
256 A copy of string, s, is made.
257 */
258
259 extern "C" void mcLexBuf_addTokCharStar (mcReserved_toktype t, void * s);
260
261 /*
262 addTokInteger - adds a token and an integer to the buffer.
263 */
264
265 extern "C" void mcLexBuf_addTokInteger (mcReserved_toktype t, int i);
266
267 /*
268 addTokComment - adds a token to the buffer and a comment descriptor, com.
269 */
270
271 extern "C" void mcLexBuf_addTokComment (mcReserved_toktype t, mcComment_commentDesc com);
272
273 /*
274 setFile - sets the current filename to, filename.
275 */
276
277 extern "C" void mcLexBuf_setFile (void * filename);
278
279 /*
280 pushFile - indicates that, filename, has just been included.
281 */
282
283 extern "C" void mcLexBuf_pushFile (void * filename);
284
285 /*
286 popFile - indicates that we are returning to, filename, having finished
287 an include.
288 */
289
290 extern "C" void mcLexBuf_popFile (void * filename);
291
292 /*
293 debugLex - display the last, n, tokens.
294 */
295
296 static void debugLex (unsigned int n);
297
298 /*
299 seekTo -
300 */
301
302 static void seekTo (unsigned int t);
303
304 /*
305 peeptokenBucket -
306 */
307
308 static mcLexBuf_tokenBucket peeptokenBucket (unsigned int *t);
309
310 /*
311 peepAfterComment - peeps ahead looking for an after statement comment. It stops at an END token
312 or if the line number changes.
313 */
314
315 static void peepAfterComment (void);
316
317 /*
318 init - initializes the token list and source list.
319 */
320
321 static void init (void);
322
323 /*
324 addTo - adds a new element to the end of sourceList, currentSource.
325 */
326
327 static void addTo (mcLexBuf_sourceList l);
328
329 /*
330 subFrom - subtracts, l, from the source list.
331 */
332
333 static void subFrom (mcLexBuf_sourceList l);
334
335 /*
336 newElement - returns a new sourceList
337 */
338
339 static mcLexBuf_sourceList newElement (void * s);
340
341 /*
342 newList - initializes an empty list with the classic dummy header element.
343 */
344
345 static mcLexBuf_sourceList newList (void);
346
347 /*
348 checkIfNeedToDuplicate - checks to see whether the currentSource has
349 been used, if it has then duplicate the list.
350 */
351
352 static void checkIfNeedToDuplicate (void);
353
354 /*
355 killList - kills the sourceList providing that it has not been used.
356 */
357
358 static void killList (void);
359
360 /*
361 displayToken -
362 */
363
364 static void displayToken (mcReserved_toktype t);
365
366 /*
367 updateFromBucket - updates the global variables: currenttoken,
368 currentstring, currentcolumn and currentinteger
369 from tokenBucket, b, and, offset.
370 */
371
372 static void updateFromBucket (mcLexBuf_tokenBucket b, unsigned int offset);
373
374 /*
375 doGetToken - fetch the next token into currenttoken.
376 */
377
378 static void doGetToken (void);
379
380 /*
381 syncOpenWithBuffer - synchronise the buffer with the start of a file.
382 Skips all the tokens to do with the previous file.
383 */
384
385 static void syncOpenWithBuffer (void);
386
387 /*
388 findtokenBucket - returns the tokenBucket corresponding to the tokenNo.
389 */
390
391 static mcLexBuf_tokenBucket findtokenBucket (unsigned int *tokenNo);
392
393 /*
394 getFileName - returns a String defining the current file.
395 */
396
397 static void stop (void);
398
399 /*
400 addTokToList - adds a token to a dynamic list.
401 */
402
403 static void addTokToList (mcReserved_toktype t, nameKey_Name n, int i, mcComment_commentDesc comment, unsigned int l, unsigned int c, mcLexBuf_sourceList f);
404
405 /*
406 isLastTokenEof - returns TRUE if the last token was an eoftok
407 */
408
409 static bool isLastTokenEof (void);
410
411
412 /*
413 debugLex - display the last, n, tokens.
414 */
415
416 static void debugLex (unsigned int n)
417 {
418 unsigned int c;
419 unsigned int i;
420 unsigned int o;
421 unsigned int t;
422 mcLexBuf_tokenBucket b;
423
424 if (nextTokNo > n)
425 {
426 o = nextTokNo-n;
427 }
428 else
429 {
430 o = 0;
431 }
432 i = 0;
433 do {
434 t = o+i;
435 if (nextTokNo == t)
436 {
437 mcPrintf_printf0 ((const char *) "nextTokNo ", 10);
438 }
439 b = findtokenBucket (&t);
440 if (b == NULL)
441 {
442 t = o+i;
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));
444 }
445 else
446 {
447 c = o+i;
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);
451 i += 1;
452 }
453 } while (! (b == NULL));
454 }
455
456
457 /*
458 seekTo -
459 */
460
461 static void seekTo (unsigned int t)
462 {
463 mcLexBuf_tokenBucket b;
464
465 nextTokNo = t;
466 if (t > 0)
467 {
468 t -= 1;
469 b = findtokenBucket (&t);
470 if (b == NULL)
471 {
472 updateFromBucket (b, t);
473 }
474 }
475 }
476
477
478 /*
479 peeptokenBucket -
480 */
481
482 static mcLexBuf_tokenBucket peeptokenBucket (unsigned int *t)
483 {
484 mcReserved_toktype ct;
485 unsigned int old;
486 unsigned int n;
487 mcLexBuf_tokenBucket b;
488 mcLexBuf_tokenBucket c;
489
490 ct = mcLexBuf_currenttoken;
491 if (Debugging)
492 {
493 debugLex (5);
494 }
495 old = mcLexBuf_getTokenNo ();
496 do {
497 n = (*t);
498 b = findtokenBucket (&n);
499 if (b == NULL)
500 {
501 doGetToken ();
502 n = (*t);
503 b = findtokenBucket (&n);
504 if ((b == NULL) || (mcLexBuf_currenttoken == mcReserved_eoftok))
505 {
506 /* bailing out. */
507 nextTokNo = old+1;
508 b = findtokenBucket (&old);
509 updateFromBucket (b, old);
510 return NULL;
511 }
512 }
513 } while (! ((b != NULL) || (mcLexBuf_currenttoken == mcReserved_eoftok)));
514 (*t) = n;
515 nextTokNo = old+1;
516 if (Debugging)
517 {
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));
519 }
520 b = findtokenBucket (&old);
521 if (Debugging)
522 {
523 mcPrintf_printf1 ((const char *) " adjusted old = %d\\n", 21, (const unsigned char *) &old, (sizeof (old)-1));
524 }
525 if (b != NULL)
526 {
527 updateFromBucket (b, old);
528 }
529 if (Debugging)
530 {
531 debugLex (5);
532 }
533 mcDebug_assert (ct == mcLexBuf_currenttoken);
534 return b;
535 /* static analysis guarentees a RETURN statement will be used before here. */
536 __builtin_unreachable ();
537 }
538
539
540 /*
541 peepAfterComment - peeps ahead looking for an after statement comment. It stops at an END token
542 or if the line number changes.
543 */
544
545 static void peepAfterComment (void)
546 {
547 unsigned int oldTokNo;
548 unsigned int t;
549 unsigned int peep;
550 unsigned int cno;
551 unsigned int nextline;
552 unsigned int curline;
553 mcLexBuf_tokenBucket b;
554 bool finished;
555
556 oldTokNo = nextTokNo;
557 cno = mcLexBuf_getTokenNo ();
558 curline = mcLexBuf_tokenToLineNo (cno, 0);
559 nextline = curline;
560 peep = 0;
561 finished = false;
562 do {
563 t = cno+peep;
564 b = peeptokenBucket (&t);
565 if ((b == NULL) || (mcLexBuf_currenttoken == mcReserved_eoftok))
566 {
567 finished = true;
568 }
569 else
570 {
571 nextline = b->buf.array[t].line;
572 if (nextline == curline)
573 {
574 switch (b->buf.array[t].token)
575 {
576 case mcReserved_eoftok:
577 case mcReserved_endtok:
578 finished = true;
579 break;
580
581 case mcReserved_commenttok:
582 if (mcComment_isAfterComment (b->buf.array[t].com))
583 {
584 afterComment = b->buf.array[t].com;
585 }
586 break;
587
588
589 default:
590 break;
591 }
592 }
593 else
594 {
595 finished = true;
596 }
597 }
598 peep += 1;
599 } while (! (finished));
600 seekTo (oldTokNo);
601 }
602
603
604 /*
605 init - initializes the token list and source list.
606 */
607
608 static void init (void)
609 {
610 mcLexBuf_currenttoken = mcReserved_eoftok;
611 nextTokNo = 0;
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);
620 }
621
622
623 /*
624 addTo - adds a new element to the end of sourceList, currentSource.
625 */
626
627 static void addTo (mcLexBuf_sourceList l)
628 {
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 ();
635 }
636
637
638 /*
639 subFrom - subtracts, l, from the source list.
640 */
641
642 static void subFrom (mcLexBuf_sourceList l)
643 {
644 l->left->right = l->right;
645 l->right->left = l->left;
646 }
647
648
649 /*
650 newElement - returns a new sourceList
651 */
652
653 static mcLexBuf_sourceList newElement (void * s)
654 {
655 mcLexBuf_sourceList l;
656
657 Storage_ALLOCATE ((void **) &l, sizeof (mcLexBuf__T1));
658 if (l == NULL)
659 {
660 M2RTS_HALT (-1);
661 __builtin_unreachable ();
662 }
663 else
664 {
665 l->name = DynamicStrings_InitStringCharStar (s);
666 l->left = NULL;
667 l->right = NULL;
668 }
669 return l;
670 /* static analysis guarentees a RETURN statement will be used before here. */
671 __builtin_unreachable ();
672 }
673
674
675 /*
676 newList - initializes an empty list with the classic dummy header element.
677 */
678
679 static mcLexBuf_sourceList newList (void)
680 {
681 mcLexBuf_sourceList l;
682
683 Storage_ALLOCATE ((void **) &l, sizeof (mcLexBuf__T1));
684 l->left = l;
685 l->right = l;
686 l->name = static_cast<DynamicStrings_String> (NULL);
687 return l;
688 /* static analysis guarentees a RETURN statement will be used before here. */
689 __builtin_unreachable ();
690 }
691
692
693 /*
694 checkIfNeedToDuplicate - checks to see whether the currentSource has
695 been used, if it has then duplicate the list.
696 */
697
698 static void checkIfNeedToDuplicate (void)
699 {
700 mcLexBuf_sourceList l;
701 mcLexBuf_sourceList h;
702
703 if (currentUsed)
704 {
705 l = currentSource->right;
706 h = currentSource;
707 currentSource = newList ();
708 while (l != h)
709 {
710 addTo (newElement (reinterpret_cast <void *> (l->name)));
711 l = l->right;
712 }
713 }
714 }
715
716
717 /*
718 killList - kills the sourceList providing that it has not been used.
719 */
720
721 static void killList (void)
722 {
723 mcLexBuf_sourceList l;
724 mcLexBuf_sourceList k;
725
726 if (! currentUsed && (currentSource != NULL))
727 {
728 l = currentSource;
729 do {
730 k = l;
731 l = l->right;
732 Storage_DEALLOCATE ((void **) &k, sizeof (mcLexBuf__T1));
733 } while (! (l == currentSource));
734 }
735 }
736
737
738 /*
739 displayToken -
740 */
741
742 static void displayToken (mcReserved_toktype t)
743 {
744 switch (t)
745 {
746 case mcReserved_eoftok:
747 mcPrintf_printf0 ((const char *) "eoftok\\n", 8);
748 break;
749
750 case mcReserved_plustok:
751 mcPrintf_printf0 ((const char *) "plustok\\n", 9);
752 break;
753
754 case mcReserved_minustok:
755 mcPrintf_printf0 ((const char *) "minustok\\n", 10);
756 break;
757
758 case mcReserved_timestok:
759 mcPrintf_printf0 ((const char *) "timestok\\n", 10);
760 break;
761
762 case mcReserved_dividetok:
763 mcPrintf_printf0 ((const char *) "dividetok\\n", 11);
764 break;
765
766 case mcReserved_becomestok:
767 mcPrintf_printf0 ((const char *) "becomestok\\n", 12);
768 break;
769
770 case mcReserved_ambersandtok:
771 mcPrintf_printf0 ((const char *) "ambersandtok\\n", 14);
772 break;
773
774 case mcReserved_periodtok:
775 mcPrintf_printf0 ((const char *) "periodtok\\n", 11);
776 break;
777
778 case mcReserved_commatok:
779 mcPrintf_printf0 ((const char *) "commatok\\n", 10);
780 break;
781
782 case mcReserved_commenttok:
783 mcPrintf_printf0 ((const char *) "commenttok\\n", 12);
784 break;
785
786 case mcReserved_semicolontok:
787 mcPrintf_printf0 ((const char *) "semicolontok\\n", 14);
788 break;
789
790 case mcReserved_lparatok:
791 mcPrintf_printf0 ((const char *) "lparatok\\n", 10);
792 break;
793
794 case mcReserved_rparatok:
795 mcPrintf_printf0 ((const char *) "rparatok\\n", 10);
796 break;
797
798 case mcReserved_lsbratok:
799 mcPrintf_printf0 ((const char *) "lsbratok\\n", 10);
800 break;
801
802 case mcReserved_rsbratok:
803 mcPrintf_printf0 ((const char *) "rsbratok\\n", 10);
804 break;
805
806 case mcReserved_lcbratok:
807 mcPrintf_printf0 ((const char *) "lcbratok\\n", 10);
808 break;
809
810 case mcReserved_rcbratok:
811 mcPrintf_printf0 ((const char *) "rcbratok\\n", 10);
812 break;
813
814 case mcReserved_uparrowtok:
815 mcPrintf_printf0 ((const char *) "uparrowtok\\n", 12);
816 break;
817
818 case mcReserved_singlequotetok:
819 mcPrintf_printf0 ((const char *) "singlequotetok\\n", 16);
820 break;
821
822 case mcReserved_equaltok:
823 mcPrintf_printf0 ((const char *) "equaltok\\n", 10);
824 break;
825
826 case mcReserved_hashtok:
827 mcPrintf_printf0 ((const char *) "hashtok\\n", 9);
828 break;
829
830 case mcReserved_lesstok:
831 mcPrintf_printf0 ((const char *) "lesstok\\n", 9);
832 break;
833
834 case mcReserved_greatertok:
835 mcPrintf_printf0 ((const char *) "greatertok\\n", 12);
836 break;
837
838 case mcReserved_lessgreatertok:
839 mcPrintf_printf0 ((const char *) "lessgreatertok\\n", 16);
840 break;
841
842 case mcReserved_lessequaltok:
843 mcPrintf_printf0 ((const char *) "lessequaltok\\n", 14);
844 break;
845
846 case mcReserved_greaterequaltok:
847 mcPrintf_printf0 ((const char *) "greaterequaltok\\n", 17);
848 break;
849
850 case mcReserved_periodperiodtok:
851 mcPrintf_printf0 ((const char *) "periodperiodtok\\n", 17);
852 break;
853
854 case mcReserved_colontok:
855 mcPrintf_printf0 ((const char *) "colontok\\n", 10);
856 break;
857
858 case mcReserved_doublequotestok:
859 mcPrintf_printf0 ((const char *) "doublequotestok\\n", 17);
860 break;
861
862 case mcReserved_bartok:
863 mcPrintf_printf0 ((const char *) "bartok\\n", 8);
864 break;
865
866 case mcReserved_andtok:
867 mcPrintf_printf0 ((const char *) "andtok\\n", 8);
868 break;
869
870 case mcReserved_arraytok:
871 mcPrintf_printf0 ((const char *) "arraytok\\n", 10);
872 break;
873
874 case mcReserved_begintok:
875 mcPrintf_printf0 ((const char *) "begintok\\n", 10);
876 break;
877
878 case mcReserved_bytok:
879 mcPrintf_printf0 ((const char *) "bytok\\n", 7);
880 break;
881
882 case mcReserved_casetok:
883 mcPrintf_printf0 ((const char *) "casetok\\n", 9);
884 break;
885
886 case mcReserved_consttok:
887 mcPrintf_printf0 ((const char *) "consttok\\n", 10);
888 break;
889
890 case mcReserved_definitiontok:
891 mcPrintf_printf0 ((const char *) "definitiontok\\n", 15);
892 break;
893
894 case mcReserved_divtok:
895 mcPrintf_printf0 ((const char *) "divtok\\n", 8);
896 break;
897
898 case mcReserved_dotok:
899 mcPrintf_printf0 ((const char *) "dotok\\n", 7);
900 break;
901
902 case mcReserved_elsetok:
903 mcPrintf_printf0 ((const char *) "elsetok\\n", 9);
904 break;
905
906 case mcReserved_elsiftok:
907 mcPrintf_printf0 ((const char *) "elsiftok\\n", 10);
908 break;
909
910 case mcReserved_endtok:
911 mcPrintf_printf0 ((const char *) "endtok\\n", 8);
912 break;
913
914 case mcReserved_exittok:
915 mcPrintf_printf0 ((const char *) "exittok\\n", 9);
916 break;
917
918 case mcReserved_exporttok:
919 mcPrintf_printf0 ((const char *) "exporttok\\n", 11);
920 break;
921
922 case mcReserved_fortok:
923 mcPrintf_printf0 ((const char *) "fortok\\n", 8);
924 break;
925
926 case mcReserved_fromtok:
927 mcPrintf_printf0 ((const char *) "fromtok\\n", 9);
928 break;
929
930 case mcReserved_iftok:
931 mcPrintf_printf0 ((const char *) "iftok\\n", 7);
932 break;
933
934 case mcReserved_implementationtok:
935 mcPrintf_printf0 ((const char *) "implementationtok\\n", 19);
936 break;
937
938 case mcReserved_importtok:
939 mcPrintf_printf0 ((const char *) "importtok\\n", 11);
940 break;
941
942 case mcReserved_intok:
943 mcPrintf_printf0 ((const char *) "intok\\n", 7);
944 break;
945
946 case mcReserved_looptok:
947 mcPrintf_printf0 ((const char *) "looptok\\n", 9);
948 break;
949
950 case mcReserved_modtok:
951 mcPrintf_printf0 ((const char *) "modtok\\n", 8);
952 break;
953
954 case mcReserved_moduletok:
955 mcPrintf_printf0 ((const char *) "moduletok\\n", 11);
956 break;
957
958 case mcReserved_nottok:
959 mcPrintf_printf0 ((const char *) "nottok\\n", 8);
960 break;
961
962 case mcReserved_oftok:
963 mcPrintf_printf0 ((const char *) "oftok\\n", 7);
964 break;
965
966 case mcReserved_ortok:
967 mcPrintf_printf0 ((const char *) "ortok\\n", 7);
968 break;
969
970 case mcReserved_pointertok:
971 mcPrintf_printf0 ((const char *) "pointertok\\n", 12);
972 break;
973
974 case mcReserved_proceduretok:
975 mcPrintf_printf0 ((const char *) "proceduretok\\n", 14);
976 break;
977
978 case mcReserved_qualifiedtok:
979 mcPrintf_printf0 ((const char *) "qualifiedtok\\n", 14);
980 break;
981
982 case mcReserved_unqualifiedtok:
983 mcPrintf_printf0 ((const char *) "unqualifiedtok\\n", 16);
984 break;
985
986 case mcReserved_recordtok:
987 mcPrintf_printf0 ((const char *) "recordtok\\n", 11);
988 break;
989
990 case mcReserved_repeattok:
991 mcPrintf_printf0 ((const char *) "repeattok\\n", 11);
992 break;
993
994 case mcReserved_returntok:
995 mcPrintf_printf0 ((const char *) "returntok\\n", 11);
996 break;
997
998 case mcReserved_settok:
999 mcPrintf_printf0 ((const char *) "settok\\n", 8);
1000 break;
1001
1002 case mcReserved_thentok:
1003 mcPrintf_printf0 ((const char *) "thentok\\n", 9);
1004 break;
1005
1006 case mcReserved_totok:
1007 mcPrintf_printf0 ((const char *) "totok\\n", 7);
1008 break;
1009
1010 case mcReserved_typetok:
1011 mcPrintf_printf0 ((const char *) "typetok\\n", 9);
1012 break;
1013
1014 case mcReserved_untiltok:
1015 mcPrintf_printf0 ((const char *) "untiltok\\n", 10);
1016 break;
1017
1018 case mcReserved_vartok:
1019 mcPrintf_printf0 ((const char *) "vartok\\n", 8);
1020 break;
1021
1022 case mcReserved_whiletok:
1023 mcPrintf_printf0 ((const char *) "whiletok\\n", 10);
1024 break;
1025
1026 case mcReserved_withtok:
1027 mcPrintf_printf0 ((const char *) "withtok\\n", 9);
1028 break;
1029
1030 case mcReserved_asmtok:
1031 mcPrintf_printf0 ((const char *) "asmtok\\n", 8);
1032 break;
1033
1034 case mcReserved_volatiletok:
1035 mcPrintf_printf0 ((const char *) "volatiletok\\n", 13);
1036 break;
1037
1038 case mcReserved_periodperiodperiodtok:
1039 mcPrintf_printf0 ((const char *) "periodperiodperiodtok\\n", 23);
1040 break;
1041
1042 case mcReserved_datetok:
1043 mcPrintf_printf0 ((const char *) "datetok\\n", 9);
1044 break;
1045
1046 case mcReserved_linetok:
1047 mcPrintf_printf0 ((const char *) "linetok\\n", 9);
1048 break;
1049
1050 case mcReserved_filetok:
1051 mcPrintf_printf0 ((const char *) "filetok\\n", 9);
1052 break;
1053
1054 case mcReserved_integertok:
1055 mcPrintf_printf0 ((const char *) "integertok\\n", 12);
1056 break;
1057
1058 case mcReserved_identtok:
1059 mcPrintf_printf0 ((const char *) "identtok\\n", 10);
1060 break;
1061
1062 case mcReserved_realtok:
1063 mcPrintf_printf0 ((const char *) "realtok\\n", 9);
1064 break;
1065
1066 case mcReserved_stringtok:
1067 mcPrintf_printf0 ((const char *) "stringtok\\n", 11);
1068 break;
1069
1070
1071 default:
1072 mcPrintf_printf0 ((const char *) "unknown tok (--fixme--)\\n", 25);
1073 break;
1074 }
1075 }
1076
1077
1078 /*
1079 updateFromBucket - updates the global variables: currenttoken,
1080 currentstring, currentcolumn and currentinteger
1081 from tokenBucket, b, and, offset.
1082 */
1083
1084 static void updateFromBucket (mcLexBuf_tokenBucket b, unsigned int offset)
1085 {
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)
1092 {
1093 mcLexBuf_lastcomment = mcLexBuf_currentcomment;
1094 }
1095 if (Debugging)
1096 {
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));
1098 }
1099 }
1100
1101
1102 /*
1103 doGetToken - fetch the next token into currenttoken.
1104 */
1105
1106 static void doGetToken (void)
1107 {
1108 void * a;
1109 unsigned int t;
1110 mcLexBuf_tokenBucket b;
1111
1112 if (useBufferedTokens)
1113 {
1114 t = nextTokNo;
1115 b = findtokenBucket (&t);
1116 updateFromBucket (b, t);
1117 }
1118 else
1119 {
1120 if (listOfTokens.tail == NULL)
1121 {
1122 a = mcflex_getToken ();
1123 if (listOfTokens.tail == NULL)
1124 {
1125 M2RTS_HALT (-1);
1126 __builtin_unreachable ();
1127 }
1128 }
1129 if (nextTokNo >= listOfTokens.lastBucketOffset)
1130 {
1131 /* nextTokNo is in the last bucket or needs to be read. */
1132 if ((nextTokNo-listOfTokens.lastBucketOffset) < listOfTokens.tail->len)
1133 {
1134 if (Debugging)
1135 {
1136 mcPrintf_printf0 ((const char *) "fetching token from buffer (updateFromBucket)\\n", 47);
1137 }
1138 updateFromBucket (listOfTokens.tail, nextTokNo-listOfTokens.lastBucketOffset);
1139 }
1140 else
1141 {
1142 if (Debugging)
1143 {
1144 mcPrintf_printf0 ((const char *) "calling flex to place token into buffer\\n", 41);
1145 }
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. */
1150 }
1151 }
1152 else
1153 {
1154 if (Debugging)
1155 {
1156 mcPrintf_printf0 ((const char *) "fetching token from buffer\\n", 28);
1157 }
1158 t = nextTokNo;
1159 b = findtokenBucket (&t);
1160 updateFromBucket (b, t);
1161 }
1162 }
1163 if (Debugging)
1164 {
1165 displayToken (mcLexBuf_currenttoken);
1166 }
1167 nextTokNo += 1;
1168 }
1169
1170
1171 /*
1172 syncOpenWithBuffer - synchronise the buffer with the start of a file.
1173 Skips all the tokens to do with the previous file.
1174 */
1175
1176 static void syncOpenWithBuffer (void)
1177 {
1178 if (listOfTokens.tail != NULL)
1179 {
1180 nextTokNo = listOfTokens.lastBucketOffset+listOfTokens.tail->len;
1181 }
1182 }
1183
1184
1185 /*
1186 findtokenBucket - returns the tokenBucket corresponding to the tokenNo.
1187 */
1188
1189 static mcLexBuf_tokenBucket findtokenBucket (unsigned int *tokenNo)
1190 {
1191 mcLexBuf_tokenBucket b;
1192
1193 b = listOfTokens.head;
1194 while (b != NULL)
1195 {
1196 if ((*tokenNo) < b->len)
1197 {
1198 return b;
1199 }
1200 else
1201 {
1202 (*tokenNo) -= b->len;
1203 }
1204 b = b->next;
1205 }
1206 return NULL;
1207 /* static analysis guarentees a RETURN statement will be used before here. */
1208 __builtin_unreachable ();
1209 }
1210
1211
1212 /*
1213 getFileName - returns a String defining the current file.
1214 */
1215
1216 static void stop (void)
1217 {
1218 }
1219
1220
1221 /*
1222 addTokToList - adds a token to a dynamic list.
1223 */
1224
1225 static void addTokToList (mcReserved_toktype t, nameKey_Name n, int i, mcComment_commentDesc comment, unsigned int l, unsigned int c, mcLexBuf_sourceList f)
1226 {
1227 mcLexBuf_tokenBucket b;
1228
1229 if (listOfTokens.head == NULL)
1230 {
1231 Storage_ALLOCATE ((void **) &listOfTokens.head, sizeof (mcLexBuf__T2));
1232 if (listOfTokens.head == NULL)
1233 {} /* empty. */
1234 /* list error */
1235 listOfTokens.tail = listOfTokens.head;
1236 listOfTokens.tail->len = 0;
1237 }
1238 else if (listOfTokens.tail->len == MaxBucketSize)
1239 {
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)
1244 {} /* empty. */
1245 else
1246 {
1247 /* list error */
1248 listOfTokens.tail = listOfTokens.tail->next;
1249 listOfTokens.tail->len = 0;
1250 }
1251 listOfTokens.lastBucketOffset += MaxBucketSize;
1252 }
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;
1263 }
1264
1265
1266 /*
1267 isLastTokenEof - returns TRUE if the last token was an eoftok
1268 */
1269
1270 static bool isLastTokenEof (void)
1271 {
1272 unsigned int t;
1273 mcLexBuf_tokenBucket b;
1274
1275 if (listOfTokens.tail != NULL)
1276 {
1277 if (listOfTokens.tail->len == 0)
1278 {
1279 b = listOfTokens.head;
1280 if (b == listOfTokens.tail)
1281 {
1282 return false;
1283 }
1284 while (b->next != listOfTokens.tail)
1285 {
1286 b = b->next;
1287 }
1288 }
1289 else
1290 {
1291 b = listOfTokens.tail;
1292 }
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 */
1295 }
1296 return false;
1297 /* static analysis guarentees a RETURN statement will be used before here. */
1298 __builtin_unreachable ();
1299 }
1300
1301
1302 /*
1303 getProcedureComment - returns the procedure comment if it exists,
1304 or NIL otherwise.
1305 */
1306
1307 extern "C" mcComment_commentDesc mcLexBuf_getProcedureComment (void)
1308 {
1309 return procedureComment;
1310 /* static analysis guarentees a RETURN statement will be used before here. */
1311 __builtin_unreachable ();
1312 }
1313
1314
1315 /*
1316 getBodyComment - returns the body comment if it exists,
1317 or NIL otherwise. The body comment is
1318 removed if found.
1319 */
1320
1321 extern "C" mcComment_commentDesc mcLexBuf_getBodyComment (void)
1322 {
1323 mcComment_commentDesc b;
1324
1325 b = bodyComment;
1326 bodyComment = static_cast<mcComment_commentDesc> (NULL);
1327 return b;
1328 /* static analysis guarentees a RETURN statement will be used before here. */
1329 __builtin_unreachable ();
1330 }
1331
1332
1333 /*
1334 getAfterComment - returns the after comment if it exists,
1335 or NIL otherwise. The after comment is
1336 removed if found.
1337 */
1338
1339 extern "C" mcComment_commentDesc mcLexBuf_getAfterComment (void)
1340 {
1341 mcComment_commentDesc a;
1342
1343 peepAfterComment ();
1344 a = afterComment;
1345 afterComment = static_cast<mcComment_commentDesc> (NULL);
1346 return a;
1347 /* static analysis guarentees a RETURN statement will be used before here. */
1348 __builtin_unreachable ();
1349 }
1350
1351
1352 /*
1353 openSource - attempts to open the source file, s.
1354 The success of the operation is returned.
1355 */
1356
1357 extern "C" bool mcLexBuf_openSource (DynamicStrings_String s)
1358 {
1359 if (useBufferedTokens)
1360 {
1361 mcLexBuf_getToken ();
1362 return true;
1363 }
1364 else
1365 {
1366 if (mcflex_openSource (DynamicStrings_string (s)))
1367 {
1368 mcLexBuf_setFile (DynamicStrings_string (s));
1369 syncOpenWithBuffer ();
1370 mcLexBuf_getToken ();
1371 return true;
1372 }
1373 else
1374 {
1375 return false;
1376 }
1377 }
1378 /* static analysis guarentees a RETURN statement will be used before here. */
1379 __builtin_unreachable ();
1380 }
1381
1382
1383 /*
1384 closeSource - closes the current open file.
1385 */
1386
1387 extern "C" void mcLexBuf_closeSource (void)
1388 {
1389 if (useBufferedTokens)
1390 {
1391 while (mcLexBuf_currenttoken != mcReserved_eoftok)
1392 {
1393 mcLexBuf_getToken ();
1394 }
1395 }
1396 /* a subsequent call to mcflex.OpenSource will really close the file */
1397 }
1398
1399
1400 /*
1401 reInitialize - re-initialize the all the data structures.
1402 */
1403
1404 extern "C" void mcLexBuf_reInitialize (void)
1405 {
1406 mcLexBuf_tokenBucket s;
1407 mcLexBuf_tokenBucket t;
1408
1409 if (listOfTokens.head != NULL)
1410 {
1411 t = listOfTokens.head;
1412 do {
1413 s = t;
1414 t = t->next;
1415 Storage_DEALLOCATE ((void **) &s, sizeof (mcLexBuf__T2));
1416 } while (! (t == NULL));
1417 currentUsed = false;
1418 killList ();
1419 }
1420 init ();
1421 }
1422
1423
1424 /*
1425 resetForNewPass - reset the buffer pointers to the beginning ready for
1426 a new pass
1427 */
1428
1429 extern "C" void mcLexBuf_resetForNewPass (void)
1430 {
1431 nextTokNo = 0;
1432 useBufferedTokens = true;
1433 }
1434
1435
1436 /*
1437 getToken - gets the next token into currenttoken.
1438 */
1439
1440 extern "C" void mcLexBuf_getToken (void)
1441 {
1442 do {
1443 doGetToken ();
1444 if (mcLexBuf_currenttoken == mcReserved_commenttok)
1445 {
1446 /* avoid gcc warning by using compound statement even if not strictly necessary. */
1447 if (mcComment_isProcedureComment (mcLexBuf_currentcomment))
1448 {
1449 procedureComment = mcLexBuf_currentcomment;
1450 bodyComment = static_cast<mcComment_commentDesc> (NULL);
1451 afterComment = static_cast<mcComment_commentDesc> (NULL);
1452 }
1453 else if (mcComment_isBodyComment (mcLexBuf_currentcomment))
1454 {
1455 /* avoid dangling else. */
1456 bodyComment = mcLexBuf_currentcomment;
1457 afterComment = static_cast<mcComment_commentDesc> (NULL);
1458 }
1459 else if (mcComment_isAfterComment (mcLexBuf_currentcomment))
1460 {
1461 /* avoid dangling else. */
1462 procedureComment = static_cast<mcComment_commentDesc> (NULL);
1463 bodyComment = static_cast<mcComment_commentDesc> (NULL);
1464 afterComment = mcLexBuf_currentcomment;
1465 }
1466 }
1467 } while (! (mcLexBuf_currenttoken != mcReserved_commenttok));
1468 }
1469
1470
1471 /*
1472 insertToken - inserts a symbol, token, infront of the current token
1473 ready for the next pass.
1474 */
1475
1476 extern "C" void mcLexBuf_insertToken (mcReserved_toktype token)
1477 {
1478 if (listOfTokens.tail != NULL)
1479 {
1480 if (listOfTokens.tail->len > 0)
1481 {
1482 listOfTokens.tail->buf.array[listOfTokens.tail->len-1].token = token;
1483 }
1484 addTokToList (mcLexBuf_currenttoken, nameKey_NulName, 0, static_cast<mcComment_commentDesc> (NULL), mcLexBuf_getLineNo (), mcLexBuf_getColumnNo (), currentSource);
1485 mcLexBuf_getToken ();
1486 }
1487 }
1488
1489
1490 /*
1491 insertTokenAndRewind - inserts a symbol, token, infront of the current token
1492 and then moves the token stream back onto the inserted token.
1493 */
1494
1495 extern "C" void mcLexBuf_insertTokenAndRewind (mcReserved_toktype token)
1496 {
1497 if (listOfTokens.tail != NULL)
1498 {
1499 if (listOfTokens.tail->len > 0)
1500 {
1501 listOfTokens.tail->buf.array[listOfTokens.tail->len-1].token = token;
1502 }
1503 addTokToList (mcLexBuf_currenttoken, nameKey_NulName, 0, static_cast<mcComment_commentDesc> (NULL), mcLexBuf_getLineNo (), mcLexBuf_getColumnNo (), currentSource);
1504 mcLexBuf_currenttoken = token;
1505 }
1506 }
1507
1508
1509 /*
1510 getPreviousTokenLineNo - returns the line number of the previous token.
1511 */
1512
1513 extern "C" unsigned int mcLexBuf_getPreviousTokenLineNo (void)
1514 {
1515 return mcLexBuf_getLineNo ();
1516 /* static analysis guarentees a RETURN statement will be used before here. */
1517 __builtin_unreachable ();
1518 }
1519
1520
1521 /*
1522 getLineNo - returns the current line number where the symbol occurs in
1523 the source file.
1524 */
1525
1526 extern "C" unsigned int mcLexBuf_getLineNo (void)
1527 {
1528 if (nextTokNo == 0)
1529 {
1530 return 0;
1531 }
1532 else
1533 {
1534 return mcLexBuf_tokenToLineNo (mcLexBuf_getTokenNo (), 0);
1535 }
1536 /* static analysis guarentees a RETURN statement will be used before here. */
1537 __builtin_unreachable ();
1538 }
1539
1540
1541 /*
1542 getTokenNo - returns the current token number.
1543 */
1544
1545 extern "C" unsigned int mcLexBuf_getTokenNo (void)
1546 {
1547 if (nextTokNo == 0)
1548 {
1549 return 0;
1550 }
1551 else
1552 {
1553 return nextTokNo-1;
1554 }
1555 /* static analysis guarentees a RETURN statement will be used before here. */
1556 __builtin_unreachable ();
1557 }
1558
1559
1560 /*
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.
1566 */
1567
1568 extern "C" unsigned int mcLexBuf_tokenToLineNo (unsigned int tokenNo, unsigned int depth)
1569 {
1570 mcLexBuf_tokenBucket b;
1571 mcLexBuf_sourceList l;
1572
1573 b = findtokenBucket (&tokenNo);
1574 if (b == NULL)
1575 {
1576 return 0;
1577 }
1578 else
1579 {
1580 if (depth == 0)
1581 {
1582 return b->buf.array[tokenNo].line;
1583 }
1584 else
1585 {
1586 l = b->buf.array[tokenNo].file->left;
1587 while (depth > 0)
1588 {
1589 l = l->left;
1590 if (l == b->buf.array[tokenNo].file->left)
1591 {
1592 return 0;
1593 }
1594 depth -= 1;
1595 }
1596 return l->line;
1597 }
1598 }
1599 /* static analysis guarentees a RETURN statement will be used before here. */
1600 __builtin_unreachable ();
1601 }
1602
1603
1604 /*
1605 getColumnNo - returns the current column where the symbol occurs in
1606 the source file.
1607 */
1608
1609 extern "C" unsigned int mcLexBuf_getColumnNo (void)
1610 {
1611 if (nextTokNo == 0)
1612 {
1613 return 0;
1614 }
1615 else
1616 {
1617 return mcLexBuf_tokenToColumnNo (mcLexBuf_getTokenNo (), 0);
1618 }
1619 /* static analysis guarentees a RETURN statement will be used before here. */
1620 __builtin_unreachable ();
1621 }
1622
1623
1624 /*
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.
1630 */
1631
1632 extern "C" unsigned int mcLexBuf_tokenToColumnNo (unsigned int tokenNo, unsigned int depth)
1633 {
1634 mcLexBuf_tokenBucket b;
1635 mcLexBuf_sourceList l;
1636
1637 b = findtokenBucket (&tokenNo);
1638 if (b == NULL)
1639 {
1640 return 0;
1641 }
1642 else
1643 {
1644 if (depth == 0)
1645 {
1646 return b->buf.array[tokenNo].col;
1647 }
1648 else
1649 {
1650 l = b->buf.array[tokenNo].file->left;
1651 while (depth > 0)
1652 {
1653 l = l->left;
1654 if (l == b->buf.array[tokenNo].file->left)
1655 {
1656 return 0;
1657 }
1658 depth -= 1;
1659 }
1660 return l->col;
1661 }
1662 }
1663 /* static analysis guarentees a RETURN statement will be used before here. */
1664 __builtin_unreachable ();
1665 }
1666
1667
1668 /*
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
1673 is requested.
1674 */
1675
1676 extern "C" DynamicStrings_String mcLexBuf_findFileNameFromToken (unsigned int tokenNo, unsigned int depth)
1677 {
1678 mcLexBuf_tokenBucket b;
1679 mcLexBuf_sourceList l;
1680
1681 b = findtokenBucket (&tokenNo);
1682 if (b == NULL)
1683 {
1684 return static_cast<DynamicStrings_String> (NULL);
1685 }
1686 else
1687 {
1688 l = b->buf.array[tokenNo].file->left;
1689 while (depth > 0)
1690 {
1691 l = l->left;
1692 if (l == b->buf.array[tokenNo].file->left)
1693 {
1694 return static_cast<DynamicStrings_String> (NULL);
1695 }
1696 depth -= 1;
1697 }
1698 return l->name;
1699 }
1700 /* static analysis guarentees a RETURN statement will be used before here. */
1701 __builtin_unreachable ();
1702 }
1703
1704
1705 /*
1706 getFileName - returns a String defining the current file.
1707 */
1708
1709 extern "C" DynamicStrings_String mcLexBuf_getFileName (void)
1710 {
1711 return mcLexBuf_findFileNameFromToken (mcLexBuf_getTokenNo (), 0);
1712 /* static analysis guarentees a RETURN statement will be used before here. */
1713 __builtin_unreachable ();
1714 }
1715
1716
1717 /*
1718 addTok - adds a token to the buffer.
1719 */
1720
1721 extern "C" void mcLexBuf_addTok (mcReserved_toktype t)
1722 {
1723 if (! ((t == mcReserved_eoftok) && (isLastTokenEof ())))
1724 {
1725 addTokToList (t, nameKey_NulName, 0, static_cast<mcComment_commentDesc> (NULL), mcflex_getLineNo (), mcflex_getColumnNo (), currentSource);
1726 currentUsed = true;
1727 }
1728 }
1729
1730
1731 /*
1732 addTokCharStar - adds a token to the buffer and an additional string, s.
1733 A copy of string, s, is made.
1734 */
1735
1736 extern "C" void mcLexBuf_addTokCharStar (mcReserved_toktype t, void * s)
1737 {
1738 if ((libc_strlen (s)) > 80)
1739 {
1740 stop ();
1741 }
1742 addTokToList (t, nameKey_makekey (s), 0, static_cast<mcComment_commentDesc> (NULL), mcflex_getLineNo (), mcflex_getColumnNo (), currentSource);
1743 currentUsed = true;
1744 }
1745
1746
1747 /*
1748 addTokInteger - adds a token and an integer to the buffer.
1749 */
1750
1751 extern "C" void mcLexBuf_addTokInteger (mcReserved_toktype t, int i)
1752 {
1753 DynamicStrings_String s;
1754 unsigned int c;
1755 unsigned int l;
1756
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);
1762 currentUsed = true;
1763 }
1764
1765
1766 /*
1767 addTokComment - adds a token to the buffer and a comment descriptor, com.
1768 */
1769
1770 extern "C" void mcLexBuf_addTokComment (mcReserved_toktype t, mcComment_commentDesc com)
1771 {
1772 addTokToList (t, nameKey_NulName, 0, com, mcflex_getLineNo (), mcflex_getColumnNo (), currentSource);
1773 currentUsed = true;
1774 }
1775
1776
1777 /*
1778 setFile - sets the current filename to, filename.
1779 */
1780
1781 extern "C" void mcLexBuf_setFile (void * filename)
1782 {
1783 killList ();
1784 currentUsed = false;
1785 currentSource = newList ();
1786 addTo (newElement (filename));
1787 }
1788
1789
1790 /*
1791 pushFile - indicates that, filename, has just been included.
1792 */
1793
1794 extern "C" void mcLexBuf_pushFile (void * filename)
1795 {
1796 mcLexBuf_sourceList l;
1797
1798 checkIfNeedToDuplicate ();
1799 addTo (newElement (filename));
1800 if (Debugging)
1801 {
1802 if (currentSource->right != currentSource)
1803 {
1804 l = currentSource;
1805 do {
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));
1807 l = l->right;
1808 } while (! (l == currentSource));
1809 }
1810 }
1811 }
1812
1813
1814 /*
1815 popFile - indicates that we are returning to, filename, having finished
1816 an include.
1817 */
1818
1819 extern "C" void mcLexBuf_popFile (void * filename)
1820 {
1821 mcLexBuf_sourceList l;
1822
1823 checkIfNeedToDuplicate ();
1824 if ((currentSource != NULL) && (currentSource->left != currentSource))
1825 {
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))))))
1831 {} /* empty. */
1832 /* mismatch in source file names after preprocessing files */
1833 }
1834 /* source file list is empty, cannot pop an include.. */
1835 }
1836
1837 extern "C" void _M2_mcLexBuf_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
1838 {
1839 init ();
1840 }
1841
1842 extern "C" void _M2_mcLexBuf_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
1843 {
1844 }