Debian Codegraph @

gcc/m2/m2.flex

raw
1 %{
2 /* m2.flex implements lexical analysis for Modula-2.
3
4 Copyright (C) 2004-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 "gm2-gcc/gcc-consolidation.h"
24
25 #include "GM2Reserved.h"
26 #include "GM2LexBuf.h"
27 #include "input.h"
28 #include "m2options.h"
29 #include "Gm2linemap.h"
30
31 static int cpreprocessor = 0; /* Replace this with correct getter. */
32
33 #if defined(GM2USEGGC)
34 # include "ggc.h"
35 #endif
36
37 #include "timevar.h"
38
39 #define START_FILE(F,L) m2linemap_StartFile(F,L)
40 #define END_FILE() m2linemap_EndFile()
41 #define START_LINE(N,S) m2linemap_StartLine(N,S)
42 #define GET_LOCATION(COLUMN_START,COLUMN_END) \
43 m2linemap_GetLocationRange(COLUMN_START,COLUMN_END)
44 #define TIMEVAR_PUSH_LEX timevar_push (TV_LEX)
45 #define TIMEVAR_POP_LEX timevar_pop (TV_LEX)
46
47 #ifdef __cplusplus
48 #define EXTERN extern "C"
49 #endif
50
51 #define FIRST_COLUMN 1
52
53 /* m2.flex provides a lexical analyser for GNU Modula-2. */
54
55 struct lineInfo {
56 char *linebuf; /* line contents */
57 int linelen; /* length */
58 int tokenpos; /* start position of token within line */
59 int toklen; /* a copy of yylen (length of token) */
60 int nextpos; /* position after token */
61 int lineno; /* line number of this line */
62 int column; /* first column number of token on this line */
63 bool inuse; /* do we need to keep this line info? */
64 location_t location; /* the corresponding gcc location_t */
65 struct lineInfo *next;
66 };
67
68 struct functionInfo {
69 char *name; /* function name */
70 bool module; /* is it really a module? */
71 struct functionInfo *next; /* list of nested functions */
72 };
73
74 static int lineno =1; /* a running count of the file line number */
75 static char *filename =NULL;
76 static int commentLevel=0;
77 static int commentCLevel=0;
78 static struct lineInfo *currentLine=NULL;
79 static struct functionInfo *currentFunction=NULL;
80 static bool seenFunctionStart=false;
81 static bool seenEnd=false;
82 static bool seenModuleStart=false;
83 static bool isDefinitionModule=false;
84 static int totalLines=0;
85
86 static void pushLine (void);
87 static void popLine (void);
88 static void finishedLine (void);
89 static void resetpos (void);
90 static void consumeLine (void);
91 static void updatepos (void);
92 static void skippos (void);
93 static void poperrorskip (const char *);
94 static void endOfComment (void);
95 static void endOfCComment (void);
96 static void splitSlashStar (void);
97 static void handleDate (void);
98 static void handleLine (void);
99 static void handleFile (void);
100 static void handleFunction (void);
101 static void handleColumn (void);
102 static void pushFunction (char *function, bool module);
103 static void popFunction (void);
104 static void checkFunction (void);
105 EXTERN void m2flex_M2Error (const char *);
106 EXTERN location_t m2flex_GetLocation (void);
107 EXTERN int m2flex_GetColumnNo (void);
108 EXTERN bool m2flex_OpenSource (char *s);
109 EXTERN int m2flex_GetLineNo (void);
110 EXTERN void m2flex_CloseSource (void);
111 EXTERN char *m2flex_GetToken (void);
112 EXTERN void _M2_m2flex_init (void);
113 EXTERN int m2flex_GetTotalLines (void);
114 extern void yylex (void);
115
116 #define YY_DECL void yylex (void)
117 %}
118
119 %option nounput
120 %x COMMENT COMMENT1 COMMENTC LINE0 LINE1 LINE2
121
122 %%
123
124 "(*" { updatepos();
125 commentLevel=1; pushLine(); skippos();
126 BEGIN COMMENT; }
127 <COMMENT>"*)" { endOfComment(); }
128 <COMMENT>"(*" { commentLevel++; pushLine(); updatepos(); skippos(); }
129 <COMMENT>"<*" { if (commentLevel == 1) {
130 updatepos();
131 pushLine();
132 skippos();
133 BEGIN COMMENT1;
134 } else {
135 updatepos(); skippos();
136 }
137 }
138 <COMMENT>\n.* { consumeLine(); }
139 <COMMENT>. { updatepos(); skippos(); }
140 <COMMENT1>. { updatepos(); skippos(); }
141 <COMMENT1>"*>" { updatepos(); skippos(); finishedLine(); BEGIN COMMENT; }
142 <COMMENT1>\n.* { consumeLine(); }
143 <COMMENT1>"*)" { poperrorskip("unterminated source code directive, missing *>");
144 endOfComment(); }
145 <COMMENT1><<EOF>> { poperrorskip("unterminated source code directive, missing *>"); BEGIN COMMENT; }
146 <COMMENT><<EOF>> { poperrorskip("unterminated comment found at the end of the file, missing *)"); BEGIN INITIAL; }
147
148 "/*" { /* Possibly handle C preprocessor comment. */
149 if (cpreprocessor)
150 {
151 updatepos ();
152 commentCLevel++;
153 if (commentCLevel == 1)
154 {
155 pushLine ();
156 skippos ();
157 }
158 BEGIN COMMENTC;
159 }
160 else
161 splitSlashStar ();
162 }
163 <COMMENTC>. { updatepos(); skippos(); }
164 <COMMENTC>\n.* { consumeLine(); }
165 <COMMENTC>"*/" { endOfCComment(); }
166 ^\#.* { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */
167 if (M2Options_GetLineDirectives ())
168 BEGIN LINE0;
169 }
170 \n\#.* { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */
171 if (M2Options_GetLineDirectives ())
172 BEGIN LINE0;
173 }
174 <LINE0>\#[ \t]* { updatepos(); }
175 <LINE0>[0-9]+[ \t]*\" { updatepos(); lineno=atoi(yytext); BEGIN LINE1; }
176 <LINE0>\n { m2flex_M2Error("missing initial quote after #line directive"); resetpos(); BEGIN INITIAL; }
177 <LINE0>[^\n]
178 <LINE1>[^\"\n]+ { m2flex_M2Error("missing final quote after #line directive"); resetpos(); BEGIN INITIAL; }
179 <LINE1>.*\" { updatepos();
180 filename = (char *)xrealloc(filename, yyleng+1);
181 strcpy(filename, yytext);
182 filename[yyleng-1] = (char)0; /* remove trailing quote */
183 START_FILE (filename, lineno);
184 BEGIN LINE2;
185 }
186 <LINE2>[ \t]* { updatepos(); }
187 <LINE2>\n { M2LexBuf_SetFile(filename); updatepos(); BEGIN INITIAL; }
188 <LINE2>2[ \t]*\n { M2LexBuf_SetFile(filename); updatepos(); BEGIN INITIAL; }
189 <LINE2>1[ \t]*\n { M2LexBuf_SetFile(filename); updatepos(); BEGIN INITIAL; }
190 <LINE2>1[ \t]*.*\n { M2LexBuf_SetFile(filename); updatepos(); BEGIN INITIAL; }
191 <LINE2>2[ \t]*.*\n { M2LexBuf_SetFile(filename); updatepos(); BEGIN INITIAL; }
192 <LINE2>3[ \t]*.*\n { M2LexBuf_SetFile(filename); updatepos(); BEGIN INITIAL; }
193
194 \n[^\#].* { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */ }
195 \n { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */ }
196
197 \"[^\"\n]*\" { updatepos(); M2LexBuf_AddTokCharStar(M2Reserved_stringtok, yytext); return; }
198 \"[^\"\n]*$ { updatepos();
199 m2flex_M2Error("missing terminating quote, \"");
200 M2LexBuf_AddTokCharStar(M2Reserved_stringtok, yytext);
201 resetpos(); return;
202 }
203
204 '[^'\n]*' { updatepos(); M2LexBuf_AddTokCharStar(M2Reserved_stringtok, yytext); return; }
205 '[^'\n]*$ { updatepos();
206 m2flex_M2Error("missing terminating quote, '");
207 M2LexBuf_AddTokCharStar(M2Reserved_stringtok, yytext);
208 resetpos(); return;
209 }
210
211 <<EOF>> { updatepos(); M2LexBuf_AddTok(M2Reserved_eoftok); return; }
212 \+ { updatepos(); M2LexBuf_AddTok(M2Reserved_plustok); return; }
213 - { updatepos(); M2LexBuf_AddTok(M2Reserved_minustok); return; }
214 "*" { updatepos(); M2LexBuf_AddTok(M2Reserved_timestok); return; }
215 \/ { updatepos(); M2LexBuf_AddTok(M2Reserved_dividetok); return; }
216 := { updatepos(); M2LexBuf_AddTok(M2Reserved_becomestok); return; }
217 \& { updatepos(); M2LexBuf_AddTok(M2Reserved_ambersandtok); return; }
218 \. { updatepos(); M2LexBuf_AddTok(M2Reserved_periodtok); return; }
219 \, { updatepos(); M2LexBuf_AddTok(M2Reserved_commatok); return; }
220 \; { updatepos(); M2LexBuf_AddTok(M2Reserved_semicolontok); return; }
221 \( { updatepos(); M2LexBuf_AddTok(M2Reserved_lparatok); return; }
222 \) { updatepos(); M2LexBuf_AddTok(M2Reserved_rparatok); return; }
223 \[ { updatepos(); M2LexBuf_AddTok(M2Reserved_lsbratok); return; }
224 \] { updatepos(); M2LexBuf_AddTok(M2Reserved_rsbratok); return; }
225 \(\! { updatepos(); M2LexBuf_AddTok(M2Reserved_lsbratok); return; }
226 \!\) { updatepos(); M2LexBuf_AddTok(M2Reserved_rsbratok); return; }
227 \^ { updatepos(); M2LexBuf_AddTok(M2Reserved_uparrowtok); return; }
228 \@ { updatepos(); M2LexBuf_AddTok(M2Reserved_uparrowtok); return; }
229 \{ { updatepos(); M2LexBuf_AddTok(M2Reserved_lcbratok); return; }
230 \} { updatepos(); M2LexBuf_AddTok(M2Reserved_rcbratok); return; }
231 \(\: { updatepos(); M2LexBuf_AddTok(M2Reserved_lcbratok); return; }
232 \:\) { updatepos(); M2LexBuf_AddTok(M2Reserved_rcbratok); return; }
233 \' { updatepos(); M2LexBuf_AddTok(M2Reserved_singlequotetok); return; }
234 \= { updatepos(); M2LexBuf_AddTok(M2Reserved_equaltok); return; }
235 \# { updatepos(); M2LexBuf_AddTok(M2Reserved_hashtok); return; }
236 \< { updatepos(); M2LexBuf_AddTok(M2Reserved_lesstok); return; }
237 \> { updatepos(); M2LexBuf_AddTok(M2Reserved_greatertok); return; }
238 \<\> { updatepos(); M2LexBuf_AddTok(M2Reserved_lessgreatertok); return; }
239 \<\= { updatepos(); M2LexBuf_AddTok(M2Reserved_lessequaltok); return; }
240 \>\= { updatepos(); M2LexBuf_AddTok(M2Reserved_greaterequaltok); return; }
241 "<*" { updatepos(); M2LexBuf_AddTok(M2Reserved_ldirectivetok); return; }
242 "*>" { updatepos(); M2LexBuf_AddTok(M2Reserved_rdirectivetok); return; }
243 \.\. { updatepos(); M2LexBuf_AddTok(M2Reserved_periodperiodtok); return; }
244 \.\.\. { updatepos(); M2LexBuf_AddTok(M2Reserved_periodperiodperiodtok); return; }
245 \: { updatepos(); M2LexBuf_AddTok(M2Reserved_colontok); return; }
246 \" { updatepos(); M2LexBuf_AddTok(M2Reserved_doublequotestok); return; }
247 \| { updatepos(); M2LexBuf_AddTok(M2Reserved_bartok); return; }
248 \! { updatepos(); M2LexBuf_AddTok(M2Reserved_bartok); return; }
249 \~ { updatepos(); M2LexBuf_AddTok(M2Reserved_nottok); return; }
250 AND { updatepos(); M2LexBuf_AddTok(M2Reserved_andtok); return; }
251 ARRAY { updatepos(); M2LexBuf_AddTok(M2Reserved_arraytok); return; }
252 BEGIN { updatepos(); M2LexBuf_AddTok(M2Reserved_begintok); return; }
253 BY { updatepos(); M2LexBuf_AddTok(M2Reserved_bytok); return; }
254 CASE { updatepos(); M2LexBuf_AddTok(M2Reserved_casetok); return; }
255 CONST { updatepos(); M2LexBuf_AddTok(M2Reserved_consttok); return; }
256 DEFINITION { updatepos(); isDefinitionModule = true;
257 M2LexBuf_AddTok(M2Reserved_definitiontok); return; }
258 DIV { updatepos(); M2LexBuf_AddTok(M2Reserved_divtok); return; }
259 DO { updatepos(); M2LexBuf_AddTok(M2Reserved_dotok); return; }
260 ELSE { updatepos(); M2LexBuf_AddTok(M2Reserved_elsetok); return; }
261 ELSIF { updatepos(); M2LexBuf_AddTok(M2Reserved_elsiftok); return; }
262 END { updatepos(); seenEnd=true;
263 M2LexBuf_AddTok(M2Reserved_endtok); return; }
264 EXCEPT { updatepos(); M2LexBuf_AddTok(M2Reserved_excepttok); return; }
265 EXIT { updatepos(); M2LexBuf_AddTok(M2Reserved_exittok); return; }
266 EXPORT { updatepos(); M2LexBuf_AddTok(M2Reserved_exporttok); return; }
267 FINALLY { updatepos(); M2LexBuf_AddTok(M2Reserved_finallytok); return; }
268 FOR { updatepos(); M2LexBuf_AddTok(M2Reserved_fortok); return; }
269 FORWARD { updatepos(); M2LexBuf_AddTok(M2Reserved_forwardtok); return; }
270 FROM { updatepos(); M2LexBuf_AddTok(M2Reserved_fromtok); return; }
271 IF { updatepos(); M2LexBuf_AddTok(M2Reserved_iftok); return; }
272 IMPLEMENTATION { updatepos(); M2LexBuf_AddTok(M2Reserved_implementationtok); return; }
273 IMPORT { updatepos(); M2LexBuf_AddTok(M2Reserved_importtok); return; }
274 IN { updatepos(); M2LexBuf_AddTok(M2Reserved_intok); return; }
275 LOOP { updatepos(); M2LexBuf_AddTok(M2Reserved_looptok); return; }
276 MOD { updatepos(); M2LexBuf_AddTok(M2Reserved_modtok); return; }
277 MODULE { updatepos(); seenModuleStart=true;
278 M2LexBuf_AddTok(M2Reserved_moduletok); return; }
279 NOT { updatepos(); M2LexBuf_AddTok(M2Reserved_nottok); return; }
280 OF { updatepos(); M2LexBuf_AddTok(M2Reserved_oftok); return; }
281 OR { updatepos(); M2LexBuf_AddTok(M2Reserved_ortok); return; }
282 PACKEDSET { updatepos(); M2LexBuf_AddTok(M2Reserved_packedsettok); return; }
283 POINTER { updatepos(); M2LexBuf_AddTok(M2Reserved_pointertok); return; }
284 PROCEDURE { updatepos(); seenFunctionStart=true;
285 M2LexBuf_AddTok(M2Reserved_proceduretok); return; }
286 QUALIFIED { updatepos(); M2LexBuf_AddTok(M2Reserved_qualifiedtok); return; }
287 UNQUALIFIED { updatepos(); M2LexBuf_AddTok(M2Reserved_unqualifiedtok); return; }
288 RECORD { updatepos(); M2LexBuf_AddTok(M2Reserved_recordtok); return; }
289 REM { updatepos(); M2LexBuf_AddTok(M2Reserved_remtok); return; }
290 REPEAT { updatepos(); M2LexBuf_AddTok(M2Reserved_repeattok); return; }
291 RETRY { updatepos(); M2LexBuf_AddTok(M2Reserved_retrytok); return; }
292 RETURN { updatepos(); M2LexBuf_AddTok(M2Reserved_returntok); return; }
293 SET { updatepos(); M2LexBuf_AddTok(M2Reserved_settok); return; }
294 THEN { updatepos(); M2LexBuf_AddTok(M2Reserved_thentok); return; }
295 TO { updatepos(); M2LexBuf_AddTok(M2Reserved_totok); return; }
296 TYPE { updatepos(); M2LexBuf_AddTok(M2Reserved_typetok); return; }
297 UNTIL { updatepos(); M2LexBuf_AddTok(M2Reserved_untiltok); return; }
298 VAR { updatepos(); M2LexBuf_AddTok(M2Reserved_vartok); return; }
299 WHILE { updatepos(); M2LexBuf_AddTok(M2Reserved_whiletok); return; }
300 WITH { updatepos(); M2LexBuf_AddTok(M2Reserved_withtok); return; }
301 ASM { updatepos(); M2LexBuf_AddTok(M2Reserved_asmtok); return; }
302 VOLATILE { updatepos(); M2LexBuf_AddTok(M2Reserved_volatiletok); return; }
303 \_\_DATE\_\_ { updatepos(); handleDate(); return; }
304 \_\_LINE\_\_ { updatepos(); handleLine(); return; }
305 \_\_FILE\_\_ { updatepos(); handleFile(); return; }
306 \_\_FUNCTION\_\_ { updatepos(); handleFunction(); return; }
307 \_\_COLUMN\_\_ { updatepos(); handleColumn(); return; }
308 \_\_ATTRIBUTE\_\_ { updatepos(); M2LexBuf_AddTok(M2Reserved_attributetok); return; }
309 \_\_BUILTIN\_\_ { updatepos(); M2LexBuf_AddTok(M2Reserved_builtintok); return; }
310 \_\_INLINE\_\_ { updatepos(); M2LexBuf_AddTok(M2Reserved_inlinetok); return; }
311
312
313 (([0-9]*\.[0-9]+)(E[+-]?[0-9]+)?) { updatepos(); M2LexBuf_AddTokCharStar(M2Reserved_realtok, yytext); return; }
314 [0-9]*\.E[+-]?[0-9]+ { updatepos(); M2LexBuf_AddTokCharStar(M2Reserved_realtok, yytext); return; }
315 [a-zA-Z_][a-zA-Z0-9_]* { checkFunction(); updatepos(); M2LexBuf_AddTokCharStar(M2Reserved_identtok, yytext); return; }
316 [0-9]+ { updatepos(); M2LexBuf_AddTokCharStar(M2Reserved_integertok, yytext); return; }
317 [0-1]+A { updatepos(); M2LexBuf_AddTokCharStar(M2Reserved_integertok, yytext); return; }
318 [0-9]+B { updatepos(); M2LexBuf_AddTokCharStar(M2Reserved_integertok, yytext); return; }
319 [0-9]+C { updatepos(); M2LexBuf_AddTokCharStar(M2Reserved_integertok, yytext); return; }
320 [0-9A-F]+H { updatepos(); M2LexBuf_AddTokCharStar(M2Reserved_integertok, yytext); return; }
321 [\t\r ]+ { currentLine->tokenpos += yyleng; /* Ignore space. */; }
322 . { updatepos(); m2flex_M2Error("unrecognised symbol"); skippos(); }
323
324 %%
325
326 /* have removed the -? from the beginning of the real/integer constant literal rules */
327
328 /*
329 * hand built routines
330 */
331
332 /*
333 * handleFile - handles the __FILE__ construct by wraping it in double quotes and putting
334 * it into the token buffer as a string.
335 */
336
337 static void handleFile (void)
338 {
339 char *s = (char *)alloca(strlen(filename)+2+1);
340
341 strcpy(s, "\"");
342 strcat(s, filename);
343 strcat(s, "\"");
344 M2LexBuf_AddTokCharStar(M2Reserved_stringtok, s);
345 }
346
347 /*
348 * handleLine - handles the __LINE__ construct by passing an integer to
349 * the token buffer.
350 */
351
352 static void handleLine (void)
353 {
354 M2LexBuf_AddTokInteger(M2Reserved_integertok, lineno);
355 }
356
357 /*
358 * handleColumn - handles the __COLUMN__ construct by passing an integer to
359 * the token buffer.
360 */
361
362 static void handleColumn (void)
363 {
364 M2LexBuf_AddTokInteger(M2Reserved_integertok, m2flex_GetColumnNo());
365 }
366
367 /*
368 * handleDate - handles the __DATE__ construct by passing the date
369 * as a string to the token buffer.
370 */
371
372 static void handleDate (void)
373 {
374 time_t clock = time ((time_t *)0);
375 char *sdate = ctime (&clock);
376 char *s = (char *) alloca (strlen (sdate) + 2 + 1);
377 char *p = index (sdate, '\n');
378
379 if (p != NULL) {
380 *p = (char) 0;
381 }
382 strcpy(s, "\"");
383 strcat(s, sdate);
384 strcat(s, "\"");
385 M2LexBuf_AddTokCharStar (M2Reserved_stringtok, s);
386 }
387
388 /*
389 * handleFunction - handles the __FUNCTION__ construct by wrapping
390 * it in double quotes and putting it into the token
391 * buffer as a string.
392 */
393
394 static void handleFunction (void)
395 {
396 if (currentFunction == NULL)
397 M2LexBuf_AddTokCharStar(M2Reserved_stringtok, const_cast<char *>("\"\""));
398 else if (currentFunction->module) {
399 char *s = (char *) alloca(strlen(yytext) +
400 strlen("\"module initialization\"") + 1);
401 strcpy(s, "\"module ");
402 strcat(s, currentFunction->name);
403 strcat(s, " initialization\"");
404 M2LexBuf_AddTokCharStar(M2Reserved_stringtok, s);
405 } else {
406 char *function = currentFunction->name;
407 char *s = (char *)alloca(strlen(function)+2+1);
408 strcpy(s, "\"");
409 strcat(s, function);
410 strcat(s, "\"");
411 M2LexBuf_AddTokCharStar(M2Reserved_stringtok, s);
412 }
413 }
414
415 /*
416 * pushFunction - pushes the function name onto the stack.
417 */
418
419 static void pushFunction (char *function, bool module)
420 {
421 if (currentFunction == NULL) {
422 currentFunction = (struct functionInfo *)xmalloc (sizeof (struct functionInfo));
423 currentFunction->name = xstrdup(function);
424 currentFunction->next = NULL;
425 currentFunction->module = module;
426 } else {
427 struct functionInfo *f = (struct functionInfo *)xmalloc (sizeof (struct functionInfo));
428 f->name = xstrdup(function);
429 f->next = currentFunction;
430 f->module = module;
431 currentFunction = f;
432 }
433 }
434
435 /*
436 * popFunction - pops the current function.
437 */
438
439 static void popFunction (void)
440 {
441 if (currentFunction != NULL && currentFunction->next != NULL) {
442 struct functionInfo *f = currentFunction;
443
444 currentFunction = currentFunction->next;
445 if (f->name != NULL)
446 free(f->name);
447 free(f);
448 }
449 }
450
451 /*
452 * endOfComment - handles the end of comment
453 */
454
455 static void endOfComment (void)
456 {
457 commentLevel--;
458 updatepos();
459 skippos();
460 if (commentLevel==0) {
461 BEGIN INITIAL;
462 finishedLine();
463 } else
464 popLine();
465 }
466
467 /*
468 * endOfCComment - handles the end of C comment.
469 */
470
471 static void endOfCComment (void)
472 {
473 commentCLevel = 0;
474 updatepos();
475 skippos();
476 BEGIN INITIAL;
477 finishedLine();
478 }
479
480 /*
481 * m2flex_M2Error - displays the error message, s, after the code line and pointer
482 * to the erroneous token.
483 */
484
485 EXTERN void m2flex_M2Error (const char *s)
486 {
487 if (currentLine->linebuf != NULL) {
488 int i=1;
489
490 printf("%s:%d:%s\n", filename, currentLine->lineno, currentLine->linebuf);
491 printf("%s:%d:%*s", filename, currentLine->lineno, 1+currentLine->tokenpos, "^");
492 while (i<currentLine->toklen) {
493 putchar('^');
494 i++;
495 }
496 putchar('\n');
497 }
498 if (s == NULL)
499 printf("%s:%d\n", filename, currentLine->lineno);
500 else
501 printf("%s:%d:%s\n", filename, currentLine->lineno, s);
502 }
503
504 static void poperrorskip (const char *s)
505 {
506 int nextpos =currentLine->nextpos;
507 int tokenpos=currentLine->tokenpos;
508
509 popLine();
510 m2flex_M2Error(s);
511 if (currentLine != NULL) {
512 currentLine->nextpos = nextpos;
513 currentLine->tokenpos = tokenpos;
514 }
515 }
516
517 /* skipnewline skips all '\n' at the start of the line and returns
518 the new position. */
519
520 static
521 char *
522 skipnewline (char *line)
523 {
524 while (((*line) != (char)0) && ((*line) == '\n'))
525 line++;
526 return line;
527 }
528
529 /* traceLine display the source line providing -fdebug-trace-line was
530 enabled. */
531
532 static
533 void
534 traceLine (void)
535 {
536 if (M2Options_GetDebugTraceLine ())
537 {
538 char *line = skipnewline (currentLine->linebuf);
539 if (filename == NULL)
540 printf("<stdin>:%d:%s\n", currentLine->lineno, line);
541 else
542 printf("%s:%d:%s\n", filename, currentLine->lineno, line);
543 }
544 }
545
546 /*
547 * consumeLine - reads a line into a buffer, it then pushes back the whole
548 * line except the initial \n.
549 */
550
551 static void consumeLine (void)
552 {
553 if (currentLine->linelen<yyleng) {
554 currentLine->linebuf = (char *)xrealloc (currentLine->linebuf, yyleng);
555 currentLine->linelen = yyleng;
556 }
557 strcpy(currentLine->linebuf, yytext+1); /* copy all except the initial \n */
558 lineno++;
559 totalLines++;
560 currentLine->lineno = lineno;
561 currentLine->tokenpos=0;
562 currentLine->nextpos=0;
563 currentLine->column=FIRST_COLUMN;
564 START_LINE (lineno, yyleng);
565 yyless(1); /* push back all but the \n */
566 traceLine ();
567 }
568
569 static void assert_location (location_t location ATTRIBUTE_UNUSED)
570 {
571 #if 0
572 if ((location != BUILTINS_LOCATION) && (location != UNKNOWN_LOCATION) && (! M2Options_GetCpp ())) {
573 expanded_location xl = expand_location (location);
574 if (xl.line != currentLine->lineno) {
575 m2flex_M2Error ("mismatched gcc location and front end token number");
576 }
577 }
578 #endif
579 }
580
581 /*
582 * splitSlashStar - called if we are not tokenizing source code after it
583 * has been preprocessed by cpp. It is only called
584 * if the current token was a / immediately followed by * and
585 * therefore it will be split into two m2 tokens: / and *.
586 */
587
588 static void splitSlashStar (void)
589 {
590 seenFunctionStart = false;
591 seenEnd = false;
592 seenModuleStart = false;
593 currentLine->nextpos = currentLine->tokenpos+1; /* "/". */
594 currentLine->toklen = 1;
595 currentLine->column = currentLine->tokenpos+1;
596 currentLine->location =
597 M2Options_OverrideLocation (GET_LOCATION (currentLine->column,
598 currentLine->column+currentLine->toklen-1));
599 assert_location (GET_LOCATION (currentLine->column,
600 currentLine->column+currentLine->toklen-1));
601 M2LexBuf_AddTok (M2Reserved_dividetok);
602 currentLine->nextpos = currentLine->tokenpos+1; /* "*". */
603 currentLine->toklen = 1;
604 currentLine->column = currentLine->tokenpos+1;
605 currentLine->location =
606 M2Options_OverrideLocation (GET_LOCATION (currentLine->column,
607 currentLine->column+currentLine->toklen-1));
608 assert_location (GET_LOCATION (currentLine->column,
609 currentLine->column+currentLine->toklen-1));
610 M2LexBuf_AddTok (M2Reserved_timestok);
611 }
612
613
614 /*
615 * updatepos - updates the current token position.
616 * Should be used when a rule matches a token.
617 */
618
619 static void updatepos (void)
620 {
621 seenFunctionStart = false;
622 seenEnd = false;
623 seenModuleStart = false;
624 currentLine->nextpos = currentLine->tokenpos+yyleng;
625 currentLine->toklen = yyleng;
626 currentLine->column = currentLine->tokenpos+1;
627 currentLine->location =
628 M2Options_OverrideLocation (GET_LOCATION (currentLine->column,
629 currentLine->column+currentLine->toklen-1));
630 assert_location (GET_LOCATION (currentLine->column,
631 currentLine->column+currentLine->toklen-1));
632 }
633
634 /*
635 * checkFunction - checks to see whether we have seen the start
636 * or end of a function.
637 */
638
639 static void checkFunction (void)
640 {
641 if (! isDefinitionModule) {
642 if (seenModuleStart)
643 pushFunction(yytext, 1);
644 if (seenFunctionStart)
645 pushFunction(yytext, 0);
646 if (seenEnd && currentFunction != NULL &&
647 (strcmp(currentFunction->name, yytext) == 0))
648 popFunction();
649 }
650 seenFunctionStart = false;
651 seenEnd = false;
652 seenModuleStart = false;
653 }
654
655 /*
656 * skippos - skips over this token. This function should be called
657 * if we are not returning and thus not calling getToken.
658 */
659
660 static void skippos (void)
661 {
662 currentLine->tokenpos = currentLine->nextpos;
663 }
664
665 /*
666 * initLine - initializes a currentLine
667 */
668
669 static void initLine (void)
670 {
671 currentLine = (struct lineInfo *)xmalloc (sizeof(struct lineInfo));
672
673 if (currentLine == NULL)
674 perror("xmalloc");
675 currentLine->linebuf = NULL;
676 currentLine->linelen = 0;
677 currentLine->tokenpos = 0;
678 currentLine->toklen = 0;
679 currentLine->nextpos = 0;
680 currentLine->lineno = lineno;
681 currentLine->column = FIRST_COLUMN;
682 currentLine->inuse = true;
683 currentLine->next = NULL;
684 }
685
686 /*
687 * pushLine - pushes a new line structure.
688 */
689
690 static void pushLine (void)
691 {
692 if (currentLine == NULL)
693 initLine();
694 else if (currentLine->inuse) {
695 struct lineInfo *l = (struct lineInfo *)xmalloc (sizeof(struct lineInfo));
696
697 if (currentLine->linebuf == NULL) {
698 l->linebuf = NULL;
699 l->linelen = 0;
700 } else {
701 l->linebuf = (char *)xstrdup (currentLine->linebuf);
702 l->linelen = strlen (l->linebuf)+1;
703 }
704 l->tokenpos = currentLine->tokenpos;
705 l->toklen = currentLine->toklen;
706 l->nextpos = currentLine->nextpos;
707 l->lineno = currentLine->lineno;
708 l->column = currentLine->column;
709 l->next = currentLine;
710 currentLine = l;
711 }
712 currentLine->inuse = true;
713 }
714
715 /*
716 * popLine - pops a line structure.
717 */
718
719 static void popLine (void)
720 {
721 if (currentLine != NULL) {
722 struct lineInfo *l = currentLine;
723
724 if (currentLine->linebuf != NULL)
725 free(currentLine->linebuf);
726 currentLine = l->next;
727 free(l);
728 }
729 }
730
731 /*
732 * resetpos - resets the position of the next token to the start of the line.
733 */
734
735 static void resetpos (void)
736 {
737 if (currentLine != NULL)
738 currentLine->nextpos = 0;
739 }
740
741 /*
742 * finishedLine - indicates that the current line does not need to be
743 * preserved when a pushLine occurs.
744 */
745
746 static void finishedLine (void)
747 {
748 currentLine->inuse = false;
749 }
750
751 /*
752 * m2flex_GetToken - returns a new token.
753 */
754
755 EXTERN char *m2flex_GetToken (void)
756 {
757 TIMEVAR_PUSH_LEX;
758 if (currentLine == NULL)
759 initLine();
760 currentLine->tokenpos = currentLine->nextpos;
761 yylex();
762 TIMEVAR_POP_LEX;
763 return yytext;
764 }
765
766 /*
767 * CloseSource - provided for semantic sugar
768 */
769
770 EXTERN void m2flex_CloseSource (void)
771 {
772 END_FILE ();
773 }
774
775 /*
776 * OpenSource - returns true if file s can be opened and
777 * all tokens are taken from this file.
778 */
779
780 EXTERN bool m2flex_OpenSource (char *s)
781 {
782 FILE *f = fopen(s, "r");
783
784 if (f == NULL)
785 return( false );
786 else {
787 isDefinitionModule = false;
788 while (currentFunction != NULL)
789 {
790 struct functionInfo *f = currentFunction;
791 currentFunction = f->next;
792 if (f->name != NULL)
793 free(f->name);
794 free(f);
795 }
796 yy_delete_buffer (YY_CURRENT_BUFFER);
797 yy_switch_to_buffer (yy_create_buffer(f, YY_BUF_SIZE));
798 filename = xstrdup (s);
799 lineno = 1;
800 if (currentLine == NULL)
801 pushLine ();
802 else
803 currentLine->lineno = lineno;
804 START_FILE (filename, lineno);
805 BEGIN INITIAL; resetpos ();
806 return true;
807 }
808 }
809
810 /*
811 * m2flex_GetLineNo - returns the current line number.
812 */
813
814 EXTERN int m2flex_GetLineNo (void)
815 {
816 if (currentLine == NULL)
817 return 0;
818 else
819 return currentLine->lineno;
820 }
821
822 /*
823 * m2flex_GetColumnNo - returns the column where the current
824 * token starts.
825 */
826
827 EXTERN int m2flex_GetColumnNo (void)
828 {
829 if (currentLine == NULL)
830 return FIRST_COLUMN;
831 else
832 return currentLine->column;
833 }
834
835 /*
836 * m2flex_GetLocation - returns the gcc location_t of the current token.
837 */
838
839 EXTERN location_t m2flex_GetLocation (void)
840 {
841 if (currentLine == NULL)
842 return 0;
843 else
844 return currentLine->location;
845 }
846
847 /*
848 * GetTotalLines - returns the total number of lines parsed.
849 */
850
851 EXTERN int m2flex_GetTotalLines (void)
852 {
853 return totalLines;
854 }
855
856 /*
857 * yywrap is called when end of file is seen. We push an eof token
858 * and tell the lexical analysis to stop.
859 */
860
861 int yywrap (void)
862 {
863 updatepos(); M2LexBuf_AddTok(M2Reserved_eoftok); return 1;
864 }
865
866 EXTERN void _M2_m2flex_init (void) {}
867 EXTERN void _M2_m2flex_fini (void) {}