Debian Codegraph @

gcc/m2/pge-boot/Gbnflex.cc

raw
1 /* do not edit automatically generated by mc from bnflex. */
2 /* bnflex.mod provides a simple lexical package for pg.
3
4 Copyright (C) 2001-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <[email protected]>.
6
7 This file is part of GNU Modula-2.
8
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Modula-2; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include <stdbool.h>
24 # if !defined (PROC_D)
25 # define PROC_D
26 typedef void (*PROC_t) (void);
27 typedef struct { PROC_t proc; } PROC;
28 # endif
29
30 # if !defined (TRUE)
31 # define TRUE (1==1)
32 # endif
33
34 # if !defined (FALSE)
35 # define FALSE (1==0)
36 # endif
37
38 #include <string.h>
39 #include <limits.h>
40 #define _bnflex_C
41
42 #include "Gbnflex.h"
43 # include "GPushBackInput.h"
44 # include "GSymbolKey.h"
45 # include "GASCII.h"
46 # include "GDebug.h"
47 # include "GNameKey.h"
48 # include "GStrLib.h"
49 # include "GFIO.h"
50 # include "GStrCase.h"
51 # include "GStdIO.h"
52
53 # define MaxNameLength 8192
54 static FIO_File f;
55 static SymbolKey_SymbolTree ReservedWords;
56 static NameKey_Name CurrentToken;
57 static bnflex_TokenType CurrentType;
58 static bool Debugging;
59 static bool InQuote;
60 static char QuoteChar;
61
62 /*
63 OpenSource - Attempts to open the source file, a.
64 The success of the operation is returned.
65 */
66
67 extern "C" bool bnflex_OpenSource (const char *a_, unsigned int _a_high);
68
69 /*
70 CloseSource - Closes the current open file.
71 */
72
73 extern "C" void bnflex_CloseSource (void);
74
75 /*
76 GetChar - returns the current character on the input stream.
77 */
78
79 extern "C" char bnflex_GetChar (void);
80
81 /*
82 PutChar - pushes a character onto the push back stack, it also
83 returns the character which has been pushed.
84 */
85
86 extern "C" char bnflex_PutChar (char ch);
87
88 /*
89 SymIs - if t is equal to the current token the next token is read
90 and true is returned, otherwise false is returned.
91 */
92
93 extern "C" bool bnflex_SymIs (bnflex_TokenType t);
94
95 /*
96 IsSym - returns the result of the comparison between the current token
97 type and t.
98 */
99
100 extern "C" bool bnflex_IsSym (bnflex_TokenType t);
101
102 /*
103 GetCurrentTokenType - returns the type of current token.
104 */
105
106 extern "C" bnflex_TokenType bnflex_GetCurrentTokenType (void);
107
108 /*
109 GetCurrentToken - returns the NameKey of the current token.
110 */
111
112 extern "C" NameKey_Name bnflex_GetCurrentToken (void);
113
114 /*
115 SkipUntilWhite - skips all characters until white space is seen.
116 */
117
118 extern "C" void bnflex_SkipUntilWhite (void);
119
120 /*
121 SkipWhite - skips all white space.
122 */
123
124 extern "C" void bnflex_SkipWhite (void);
125
126 /*
127 SkipUntilEoln - skips until a lf is seen. It consumes the lf.
128 */
129
130 extern "C" void bnflex_SkipUntilEoln (void);
131
132 /*
133 AdvanceToken - advances to the next token.
134 */
135
136 extern "C" void bnflex_AdvanceToken (void);
137
138 /*
139 IsReserved - returns TRUE if the name is a reserved word.
140 */
141
142 extern "C" bool bnflex_IsReserved (NameKey_Name name);
143
144 /*
145 PushBackToken - pushes a token back onto input.
146 */
147
148 extern "C" void bnflex_PushBackToken (NameKey_Name t);
149
150 /*
151 SetDebugging - sets the debugging flag.
152 */
153
154 extern "C" void bnflex_SetDebugging (bool flag);
155
156 /*
157 EatChar - consumes the next character in the input.
158 */
159
160 static void EatChar (void);
161
162 /*
163 IsWhite - returns TRUE if, ch, is a space or a tab.
164 */
165
166 static bool IsWhite (char ch);
167
168 /*
169 SkipComments - consumes comments.
170 */
171
172 static void SkipComments (void);
173
174 /*
175 WriteToken -
176 */
177
178 static void WriteToken (void);
179
180 /*
181 Init - initialize the modules global variables.
182 */
183
184 static void Init (void);
185
186
187 /*
188 EatChar - consumes the next character in the input.
189 */
190
191 static void EatChar (void)
192 {
193 if ((PushBackInput_GetCh (f)) == ASCII_nul)
194 {} /* empty. */
195 }
196
197
198 /*
199 IsWhite - returns TRUE if, ch, is a space or a tab.
200 */
201
202 static bool IsWhite (char ch)
203 {
204 return ((ch == ' ') || (ch == ASCII_tab)) || (ch == ASCII_lf);
205 /* static analysis guarentees a RETURN statement will be used before here. */
206 __builtin_unreachable ();
207 }
208
209
210 /*
211 SkipComments - consumes comments.
212 */
213
214 static void SkipComments (void)
215 {
216 bnflex_SkipWhite ();
217 while ((bnflex_PutChar (bnflex_GetChar ())) == '-')
218 {
219 if (((bnflex_GetChar ()) == '-') && ((bnflex_PutChar (bnflex_GetChar ())) == '-'))
220 {
221 /* found comment, skip it */
222 bnflex_SkipUntilEoln ();
223 bnflex_SkipWhite ();
224 }
225 else
226 {
227 /* no second '-' found thus restore first '-' */
228 if ((bnflex_PutChar ('-')) == '-')
229 {} /* empty. */
230 return ;
231 }
232 }
233 }
234
235
236 /*
237 WriteToken -
238 */
239
240 static void WriteToken (void)
241 {
242 NameKey_WriteKey (CurrentToken);
243 StdIO_Write (' ');
244 }
245
246
247 /*
248 Init - initialize the modules global variables.
249 */
250
251 static void Init (void)
252 {
253 typedef struct Init__T1_a Init__T1;
254
255 struct Init__T1_a { char array[1+1]; };
256 Init__T1 a;
257
258 SymbolKey_InitTree (&ReservedWords);
259 Debugging = false;
260 a.array[0] = ASCII_nul;
261 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) &a.array[0], 1), ((unsigned int) (bnflex_eoftok)));
262 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "%", 1), ((unsigned int) (bnflex_codetok)));
263 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) ":=", 2), ((unsigned int) (bnflex_lbecomestok)));
264 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "=:", 2), ((unsigned int) (bnflex_rbecomestok)));
265 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "|", 1), ((unsigned int) (bnflex_bartok)));
266 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "[", 1), ((unsigned int) (bnflex_lsparatok)));
267 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "]", 1), ((unsigned int) (bnflex_rsparatok)));
268 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "{", 1), ((unsigned int) (bnflex_lcparatok)));
269 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "}", 1), ((unsigned int) (bnflex_rcparatok)));
270 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "(", 1), ((unsigned int) (bnflex_lparatok)));
271 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) ")", 1), ((unsigned int) (bnflex_rparatok)));
272 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "<", 1), ((unsigned int) (bnflex_lesstok)));
273 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) ">", 1), ((unsigned int) (bnflex_gretok)));
274 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "error", 5), ((unsigned int) (bnflex_errortok)));
275 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "tokenfunc", 9), ((unsigned int) (bnflex_tfunctok)));
276 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "symfunc", 7), ((unsigned int) (bnflex_symfunctok)));
277 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "'", 1), ((unsigned int) (bnflex_squotetok)));
278 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "\"", 1), ((unsigned int) (bnflex_dquotetok)));
279 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "module", 6), ((unsigned int) (bnflex_moduletok)));
280 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "begin", 5), ((unsigned int) (bnflex_begintok)));
281 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "rules", 5), ((unsigned int) (bnflex_rulestok)));
282 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "end", 3), ((unsigned int) (bnflex_endtok)));
283 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "declaration", 11), ((unsigned int) (bnflex_declarationtok)));
284 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "token", 5), ((unsigned int) (bnflex_tokentok)));
285 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "special", 7), ((unsigned int) (bnflex_specialtok)));
286 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "first", 5), ((unsigned int) (bnflex_firsttok)));
287 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "follow", 6), ((unsigned int) (bnflex_followtok)));
288 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "epsilon", 7), ((unsigned int) (bnflex_epsilontok)));
289 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "BNF", 3), ((unsigned int) (bnflex_BNFtok)));
290 SymbolKey_PutSymKey (ReservedWords, NameKey_MakeKey ((const char *) "FNB", 3), ((unsigned int) (bnflex_FNBtok)));
291 CurrentToken = NameKey_NulName;
292 CurrentType = bnflex_identtok;
293 InQuote = false;
294 }
295
296
297 /*
298 OpenSource - Attempts to open the source file, a.
299 The success of the operation is returned.
300 */
301
302 extern "C" bool bnflex_OpenSource (const char *a_, unsigned int _a_high)
303 {
304 char a[_a_high+1];
305
306 /* make a local copy of each unbounded array. */
307 memcpy (a, a_, _a_high+1);
308
309 f = PushBackInput_Open ((const char *) a, _a_high);
310 return FIO_IsNoError (f);
311 /* static analysis guarentees a RETURN statement will be used before here. */
312 __builtin_unreachable ();
313 }
314
315
316 /*
317 CloseSource - Closes the current open file.
318 */
319
320 extern "C" void bnflex_CloseSource (void)
321 {
322 PushBackInput_Close (f);
323 }
324
325
326 /*
327 GetChar - returns the current character on the input stream.
328 */
329
330 extern "C" char bnflex_GetChar (void)
331 {
332 return PushBackInput_GetCh (f);
333 /* static analysis guarentees a RETURN statement will be used before here. */
334 __builtin_unreachable ();
335 }
336
337
338 /*
339 PutChar - pushes a character onto the push back stack, it also
340 returns the character which has been pushed.
341 */
342
343 extern "C" char bnflex_PutChar (char ch)
344 {
345 return PushBackInput_PutCh (ch);
346 /* static analysis guarentees a RETURN statement will be used before here. */
347 __builtin_unreachable ();
348 }
349
350
351 /*
352 SymIs - if t is equal to the current token the next token is read
353 and true is returned, otherwise false is returned.
354 */
355
356 extern "C" bool bnflex_SymIs (bnflex_TokenType t)
357 {
358 if (CurrentType == t)
359 {
360 bnflex_AdvanceToken ();
361 return true;
362 }
363 else
364 {
365 return false;
366 }
367 /* static analysis guarentees a RETURN statement will be used before here. */
368 __builtin_unreachable ();
369 }
370
371
372 /*
373 IsSym - returns the result of the comparison between the current token
374 type and t.
375 */
376
377 extern "C" bool bnflex_IsSym (bnflex_TokenType t)
378 {
379 return t == CurrentType;
380 /* static analysis guarentees a RETURN statement will be used before here. */
381 __builtin_unreachable ();
382 }
383
384
385 /*
386 GetCurrentTokenType - returns the type of current token.
387 */
388
389 extern "C" bnflex_TokenType bnflex_GetCurrentTokenType (void)
390 {
391 return CurrentType;
392 /* static analysis guarentees a RETURN statement will be used before here. */
393 __builtin_unreachable ();
394 }
395
396
397 /*
398 GetCurrentToken - returns the NameKey of the current token.
399 */
400
401 extern "C" NameKey_Name bnflex_GetCurrentToken (void)
402 {
403 return CurrentToken;
404 /* static analysis guarentees a RETURN statement will be used before here. */
405 __builtin_unreachable ();
406 }
407
408
409 /*
410 SkipUntilWhite - skips all characters until white space is seen.
411 */
412
413 extern "C" void bnflex_SkipUntilWhite (void)
414 {
415 while (((! (IsWhite (bnflex_PutChar (bnflex_GetChar ())))) && ((bnflex_PutChar (bnflex_GetChar ())) != ASCII_nul)) || ((bnflex_PutChar (bnflex_GetChar ())) == ASCII_lf))
416 {
417 EatChar ();
418 }
419 }
420
421
422 /*
423 SkipWhite - skips all white space.
424 */
425
426 extern "C" void bnflex_SkipWhite (void)
427 {
428 while (IsWhite (bnflex_PutChar (bnflex_GetChar ())))
429 {
430 EatChar ();
431 }
432 }
433
434
435 /*
436 SkipUntilEoln - skips until a lf is seen. It consumes the lf.
437 */
438
439 extern "C" void bnflex_SkipUntilEoln (void)
440 {
441 while (((bnflex_PutChar (bnflex_GetChar ())) != ASCII_lf) && ((bnflex_PutChar (bnflex_GetChar ())) != ASCII_nul))
442 {
443 EatChar ();
444 }
445 if ((bnflex_PutChar (bnflex_GetChar ())) == ASCII_lf)
446 {
447 EatChar ();
448 }
449 }
450
451
452 /*
453 AdvanceToken - advances to the next token.
454 */
455
456 extern "C" void bnflex_AdvanceToken (void)
457 {
458 typedef struct AdvanceToken__T2_a AdvanceToken__T2;
459
460 struct AdvanceToken__T2_a { char array[MaxNameLength+1]; };
461 AdvanceToken__T2 a;
462 unsigned int i;
463
464 i = 0;
465 if (InQuote)
466 {
467 if (CurrentType == bnflex_literaltok)
468 {
469 if ((bnflex_PutChar (bnflex_GetChar ())) == QuoteChar)
470 {
471 a.array[i] = bnflex_GetChar ();
472 InQuote = false;
473 i += 1;
474 a.array[i] = ASCII_nul;
475 CurrentToken = NameKey_MakeKey ((const char *) &a.array[0], MaxNameLength);
476 CurrentType = (bnflex_TokenType) (SymbolKey_GetSymKey (ReservedWords, CurrentToken));
477 }
478 else
479 {
480 if (QuoteChar == '"')
481 {
482 PushBackInput_WarnError ((const char *) "missing \" at the end of a literal", 33);
483 }
484 else
485 {
486 PushBackInput_WarnError ((const char *) "missing ' at the end of a literal", 33);
487 }
488 InQuote = false; /* to avoid a contineous list of the same error message */
489 }
490 }
491 else
492 {
493 while ((((i < MaxNameLength) && ((bnflex_PutChar (bnflex_GetChar ())) != ASCII_nul)) && ((bnflex_PutChar (bnflex_GetChar ())) != ASCII_lf)) && ((bnflex_PutChar (bnflex_GetChar ())) != QuoteChar))
494 {
495 a.array[i] = bnflex_GetChar ();
496 i += 1;
497 }
498 if ((bnflex_PutChar (bnflex_GetChar ())) == QuoteChar)
499 {
500 CurrentType = bnflex_literaltok;
501 a.array[i] = ASCII_nul;
502 CurrentToken = NameKey_MakeKey ((const char *) &a.array[0], MaxNameLength);
503 }
504 else
505 {
506 if (QuoteChar == '"')
507 {
508 PushBackInput_WarnError ((const char *) "missing \" at the end of a literal", 33);
509 }
510 else
511 {
512 PushBackInput_WarnError ((const char *) "missing ' at the end of a literal", 33);
513 }
514 InQuote = false; /* to avoid a contineous list of the same error message */
515 }
516 }
517 }
518 else
519 {
520 SkipComments ();
521 if (((bnflex_PutChar (bnflex_GetChar ())) == '"') || ((bnflex_PutChar (bnflex_GetChar ())) == '\''))
522 {
523 a.array[i] = bnflex_GetChar ();
524 QuoteChar = a.array[i];
525 i += 1;
526 InQuote = true;
527 a.array[i] = ASCII_nul;
528 CurrentToken = NameKey_MakeKey ((const char *) &a.array[0], MaxNameLength);
529 CurrentType = (bnflex_TokenType) (SymbolKey_GetSymKey (ReservedWords, CurrentToken));
530 }
531 else
532 {
533 while (((((i < MaxNameLength) && ((bnflex_PutChar (bnflex_GetChar ())) != ASCII_nul)) && ((bnflex_PutChar (bnflex_GetChar ())) != ASCII_lf)) && ((bnflex_PutChar (bnflex_GetChar ())) != QuoteChar)) && (! (IsWhite (bnflex_PutChar (bnflex_GetChar ())))))
534 {
535 a.array[i] = bnflex_GetChar ();
536 i += 1;
537 }
538 a.array[i] = ASCII_nul;
539 CurrentToken = NameKey_MakeKey ((const char *) &a.array[0], MaxNameLength);
540 if ((SymbolKey_GetSymKey (ReservedWords, CurrentToken)) == 0)
541 {
542 CurrentType = bnflex_identtok;
543 }
544 else
545 {
546 CurrentType = (bnflex_TokenType) (SymbolKey_GetSymKey (ReservedWords, CurrentToken));
547 }
548 }
549 }
550 if (Debugging)
551 {
552 WriteToken ();
553 }
554 }
555
556
557 /*
558 IsReserved - returns TRUE if the name is a reserved word.
559 */
560
561 extern "C" bool bnflex_IsReserved (NameKey_Name name)
562 {
563 return (SymbolKey_GetSymKey (ReservedWords, name)) != 0;
564 /* static analysis guarentees a RETURN statement will be used before here. */
565 __builtin_unreachable ();
566 }
567
568
569 /*
570 PushBackToken - pushes a token back onto input.
571 */
572
573 extern "C" void bnflex_PushBackToken (NameKey_Name t)
574 {
575 typedef struct PushBackToken__T3_a PushBackToken__T3;
576
577 struct PushBackToken__T3_a { char array[MaxNameLength+1]; };
578 PushBackToken__T3 a;
579
580 NameKey_GetKey (t, (char *) &a.array[0], MaxNameLength);
581 PushBackInput_PutString ((const char *) &a.array[0], MaxNameLength);
582 }
583
584
585 /*
586 SetDebugging - sets the debugging flag.
587 */
588
589 extern "C" void bnflex_SetDebugging (bool flag)
590 {
591 Debugging = flag;
592 }
593
594 extern "C" void _M2_bnflex_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
595 {
596 Init ();
597 }
598
599 extern "C" void _M2_bnflex_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
600 {
601 }