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