Debian Codegraph @

gcc/m2/mc/mc.flex

raw
1 %{
2 /* mc.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
24 #include "mcReserved.h"
25 #include "mcLexBuf.h"
26 #include "mcComment.h"
27
28 #include <time.h>
29 #include <ctype.h>
30 #include <stdbool.h>
31
32 #ifndef alloca
33 #ifdef __GNUC__
34 #define alloca __builtin_alloca
35 #endif
36 #endif
37
38 #if !defined(TRUE)
39 # define TRUE (1==1)
40 #endif
41 #if !defined(FALSE)
42 # define FALSE (1==0)
43 #endif
44
45 #define START_FILE(F,L)
46 #define END_FILE()
47 #define START_LINE(N,S)
48 #define TIMEVAR_PUSH_LEX
49 #define TIMEVAR_POP_LEX
50
51
52 /* m2.flex - provides a lexical analyser for GNU Modula-2. */
53
54 struct lineInfo {
55 char *linebuf; /* Line contents. */
56 unsigned int linelen; /* Length. */
57 int tokenpos; /* Start position of token within line. */
58 int toklen; /* A copy of yylen (length of token). */
59 int nextpos; /* Position after token. */
60 int actualline; /* Line number of this line. */
61 int column; /* First column number of token on this line. */
62 int inuse; /* Do we need to keep this line info? */
63 struct lineInfo *next;
64 };
65
66 struct functionInfo {
67 char *name; /* Function name. */
68 int module; /* Is it really a module? */
69 struct functionInfo *next; /* List of nested functions. */
70 };
71
72 static int lineno =1; /* A running count of the file line number. */
73 static char *filename =NULL;
74 static int commentLevel=0;
75 static struct lineInfo *currentLine=NULL;
76 static struct functionInfo *currentFunction=NULL;
77 static int seenFunctionStart=FALSE;
78 static int seenEnd=FALSE;
79 static int seenModuleStart=FALSE;
80 static int isDefinitionModule=FALSE;
81 static int totalLines=0;
82 static int seenOnlySpaces=TRUE;
83 static void *currentComment = NULL;
84 static FILE *inputFile = NULL;
85
86 void mcflex_mcError (const char *);
87 static void pushLine (void);
88 static void popLine (void);
89 static void finishedLine (void);
90 static void resetpos (void);
91 static void consumeLine (void);
92 static void updatepos (void);
93 static void skippos (void);
94 static void poperrorskip (const char *);
95 static void endOfComment (void);
96 static void handleDate (void);
97 static void handleLine (void);
98 static void handleFile (void);
99 static void handleFunction (void);
100 static void handleColumn (void);
101 static void pushFunction (char *function, int module);
102 static void popFunction (void);
103 static void checkFunction (void);
104 int mcflex_getColumnNo (void);
105 bool mcflex_openSource (char *s);
106 int mcflex_getLineNo (void);
107 void mcflex_closeSource (void);
108 char *mcflex_getToken (void);
109 void _M2_mcflex_init (int argc, char *argv[], char *envp[]);
110 int mcflex_getTotalLines (void);
111 extern void yylex (void);
112
113 #if !defined(TRUE)
114 # define TRUE (1==1)
115 #endif
116 #if !defined(FALSE)
117 # define FALSE (1==0)
118 #endif
119
120 #define YY_DECL void yylex (void)
121 %}
122
123 %x COMMENT COMMENT1 LINE0 LINE1 LINE2
124
125 %%
126
127 "(*" { updatepos();
128 commentLevel=1; pushLine(); skippos(); currentComment = mcComment_initComment (seenOnlySpaces);
129 BEGIN COMMENT; }
130 <COMMENT>"*)" { endOfComment(); }
131 <COMMENT>"(*" { commentLevel++; pushLine(); updatepos(); skippos(); }
132 <COMMENT>"<*" { if (commentLevel == 1) {
133 updatepos();
134 pushLine();
135 skippos();
136 BEGIN COMMENT1;
137 } else
138 updatepos(); skippos();
139 }
140 <COMMENT>\n { mcComment_addText (currentComment, yytext); consumeLine(); }
141 <COMMENT>. { mcComment_addText (currentComment, yytext); updatepos(); skippos(); }
142 <COMMENT1>. { updatepos(); skippos(); }
143 <COMMENT1>"*>" { updatepos(); skippos(); finishedLine(); BEGIN COMMENT; }
144 <COMMENT1>\n.* { consumeLine(); }
145 <COMMENT1>"*)" { poperrorskip("unterminated source code directive, missing *>");
146 endOfComment(); }
147 <COMMENT1><<EOF>> { poperrorskip("unterminated source code directive, missing *>"); BEGIN COMMENT; }
148 <COMMENT><<EOF>> { poperrorskip("unterminated comment found at the end of the file, missing *)"); BEGIN INITIAL; }
149
150 ^\#.* { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */ BEGIN LINE0; }
151 \n\#.* { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */ BEGIN LINE0; }
152 <LINE0>\#[ \t]* { updatepos(); }
153 <LINE0>[0-9]+[ \t]*\" { updatepos(); lineno=atoi(yytext)-1; BEGIN LINE1; }
154 <LINE0>\n { mcflex_mcError("missing initial quote after #line directive"); resetpos(); BEGIN INITIAL; }
155 <LINE0>[^\n]
156 <LINE1>[^\"\n]+ { mcflex_mcError("missing final quote after #line directive"); resetpos(); BEGIN INITIAL; }
157 <LINE1>.*\" { updatepos();
158 filename = (char *)realloc(filename, yyleng+1);
159 strcpy(filename, yytext);
160 filename[yyleng-1] = (char)0; /* remove trailing quote */
161 START_FILE (filename, lineno);
162 BEGIN LINE2;
163 }
164 <LINE2>[ \t]* { updatepos(); }
165 <LINE2>\n { mcLexBuf_setFile(filename); updatepos(); BEGIN INITIAL; }
166 <LINE2>2[ \t]*\n { mcLexBuf_setFile(filename); updatepos(); BEGIN INITIAL; }
167 <LINE2>1[ \t]*\n { mcLexBuf_setFile(filename); updatepos(); BEGIN INITIAL; }
168
169 \n[^\#].* { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */ }
170 \n { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */ }
171
172 \"[^\"\n]*\" { updatepos(); mcLexBuf_addTokCharStar(mcReserved_stringtok, yytext); return; }
173 \"[^\"\n]*$ { updatepos();
174 mcflex_mcError("missing terminating quote, \"");
175 resetpos(); return;
176 }
177
178 '[^'\n]*' { updatepos(); mcLexBuf_addTokCharStar(mcReserved_stringtok, yytext); return; }
179 '[^'\n]*$ { updatepos();
180 mcflex_mcError("missing terminating quote, '");
181 resetpos(); return;
182 }
183
184 <<EOF>> { updatepos(); mcLexBuf_addTok(mcReserved_eoftok); return; }
185 \+ { updatepos(); mcLexBuf_addTok(mcReserved_plustok); return; }
186 - { updatepos(); mcLexBuf_addTok(mcReserved_minustok); return; }
187 "*" { updatepos(); mcLexBuf_addTok(mcReserved_timestok); return; }
188 \/ { updatepos(); mcLexBuf_addTok(mcReserved_dividetok); return; }
189 := { updatepos(); mcLexBuf_addTok(mcReserved_becomestok); return; }
190 \& { updatepos(); mcLexBuf_addTok(mcReserved_ambersandtok); return; }
191 \. { updatepos(); mcLexBuf_addTok(mcReserved_periodtok); return; }
192 \, { updatepos(); mcLexBuf_addTok(mcReserved_commatok); return; }
193 \; { updatepos(); mcLexBuf_addTok(mcReserved_semicolontok); return; }
194 \( { updatepos(); mcLexBuf_addTok(mcReserved_lparatok); return; }
195 \) { updatepos(); mcLexBuf_addTok(mcReserved_rparatok); return; }
196 \[ { updatepos(); mcLexBuf_addTok(mcReserved_lsbratok); return; }
197 \] { updatepos(); mcLexBuf_addTok(mcReserved_rsbratok); return; }
198 \(\! { updatepos(); mcLexBuf_addTok(mcReserved_lsbratok); return; }
199 \!\) { updatepos(); mcLexBuf_addTok(mcReserved_rsbratok); return; }
200 \^ { updatepos(); mcLexBuf_addTok(mcReserved_uparrowtok); return; }
201 \@ { updatepos(); mcLexBuf_addTok(mcReserved_uparrowtok); return; }
202 \{ { updatepos(); mcLexBuf_addTok(mcReserved_lcbratok); return; }
203 \} { updatepos(); mcLexBuf_addTok(mcReserved_rcbratok); return; }
204 \(\: { updatepos(); mcLexBuf_addTok(mcReserved_lcbratok); return; }
205 \:\) { updatepos(); mcLexBuf_addTok(mcReserved_rcbratok); return; }
206 \' { updatepos(); mcLexBuf_addTok(mcReserved_singlequotetok); return; }
207 \= { updatepos(); mcLexBuf_addTok(mcReserved_equaltok); return; }
208 \# { updatepos(); mcLexBuf_addTok(mcReserved_hashtok); return; }
209 \< { updatepos(); mcLexBuf_addTok(mcReserved_lesstok); return; }
210 \> { updatepos(); mcLexBuf_addTok(mcReserved_greatertok); return; }
211 \<\> { updatepos(); mcLexBuf_addTok(mcReserved_lessgreatertok); return; }
212 \<\= { updatepos(); mcLexBuf_addTok(mcReserved_lessequaltok); return; }
213 \>\= { updatepos(); mcLexBuf_addTok(mcReserved_greaterequaltok); return; }
214 "<*" { updatepos(); mcLexBuf_addTok(mcReserved_ldirectivetok); return; }
215 "*>" { updatepos(); mcLexBuf_addTok(mcReserved_rdirectivetok); return; }
216 \.\. { updatepos(); mcLexBuf_addTok(mcReserved_periodperiodtok); return; }
217 \.\.\. { updatepos(); mcLexBuf_addTok(mcReserved_periodperiodperiodtok); return; }
218 \: { updatepos(); mcLexBuf_addTok(mcReserved_colontok); return; }
219 \" { updatepos(); mcLexBuf_addTok(mcReserved_doublequotestok); return; }
220 \| { updatepos(); mcLexBuf_addTok(mcReserved_bartok); return; }
221 \! { updatepos(); mcLexBuf_addTok(mcReserved_bartok); return; }
222 \~ { updatepos(); mcLexBuf_addTok(mcReserved_nottok); return; }
223 AND { updatepos(); mcLexBuf_addTok(mcReserved_andtok); return; }
224 ARRAY { updatepos(); mcLexBuf_addTok(mcReserved_arraytok); return; }
225 BEGIN { updatepos(); mcLexBuf_addTok(mcReserved_begintok); return; }
226 BY { updatepos(); mcLexBuf_addTok(mcReserved_bytok); return; }
227 CASE { updatepos(); mcLexBuf_addTok(mcReserved_casetok); return; }
228 CONST { updatepos(); mcLexBuf_addTok(mcReserved_consttok); return; }
229 DEFINITION { updatepos(); isDefinitionModule = TRUE;
230 mcLexBuf_addTok(mcReserved_definitiontok); return; }
231 DIV { updatepos(); mcLexBuf_addTok(mcReserved_divtok); return; }
232 DO { updatepos(); mcLexBuf_addTok(mcReserved_dotok); return; }
233 ELSE { updatepos(); mcLexBuf_addTok(mcReserved_elsetok); return; }
234 ELSIF { updatepos(); mcLexBuf_addTok(mcReserved_elsiftok); return; }
235 END { updatepos(); seenEnd=TRUE;
236 mcLexBuf_addTok(mcReserved_endtok); return; }
237 EXCEPT { updatepos(); mcLexBuf_addTok(mcReserved_excepttok); return; }
238 EXIT { updatepos(); mcLexBuf_addTok(mcReserved_exittok); return; }
239 EXPORT { updatepos(); mcLexBuf_addTok(mcReserved_exporttok); return; }
240 FINALLY { updatepos(); mcLexBuf_addTok(mcReserved_finallytok); return; }
241 FOR { updatepos(); mcLexBuf_addTok(mcReserved_fortok); return; }
242 FROM { updatepos(); mcLexBuf_addTok(mcReserved_fromtok); return; }
243 IF { updatepos(); mcLexBuf_addTok(mcReserved_iftok); return; }
244 IMPLEMENTATION { updatepos(); mcLexBuf_addTok(mcReserved_implementationtok); return; }
245 IMPORT { updatepos(); mcLexBuf_addTok(mcReserved_importtok); return; }
246 IN { updatepos(); mcLexBuf_addTok(mcReserved_intok); return; }
247 LOOP { updatepos(); mcLexBuf_addTok(mcReserved_looptok); return; }
248 MOD { updatepos(); mcLexBuf_addTok(mcReserved_modtok); return; }
249 MODULE { updatepos(); seenModuleStart=TRUE;
250 mcLexBuf_addTok(mcReserved_moduletok); return; }
251 NOT { updatepos(); mcLexBuf_addTok(mcReserved_nottok); return; }
252 OF { updatepos(); mcLexBuf_addTok(mcReserved_oftok); return; }
253 OR { updatepos(); mcLexBuf_addTok(mcReserved_ortok); return; }
254 PACKEDSET { updatepos(); mcLexBuf_addTok(mcReserved_packedsettok); return; }
255 POINTER { updatepos(); mcLexBuf_addTok(mcReserved_pointertok); return; }
256 PROCEDURE { updatepos(); seenFunctionStart=TRUE;
257 mcLexBuf_addTok(mcReserved_proceduretok); return; }
258 QUALIFIED { updatepos(); mcLexBuf_addTok(mcReserved_qualifiedtok); return; }
259 UNQUALIFIED { updatepos(); mcLexBuf_addTok(mcReserved_unqualifiedtok); return; }
260 RECORD { updatepos(); mcLexBuf_addTok(mcReserved_recordtok); return; }
261 REM { updatepos(); mcLexBuf_addTok(mcReserved_remtok); return; }
262 REPEAT { updatepos(); mcLexBuf_addTok(mcReserved_repeattok); return; }
263 RETRY { updatepos(); mcLexBuf_addTok(mcReserved_retrytok); return; }
264 RETURN { updatepos(); mcLexBuf_addTok(mcReserved_returntok); return; }
265 SET { updatepos(); mcLexBuf_addTok(mcReserved_settok); return; }
266 THEN { updatepos(); mcLexBuf_addTok(mcReserved_thentok); return; }
267 TO { updatepos(); mcLexBuf_addTok(mcReserved_totok); return; }
268 TYPE { updatepos(); mcLexBuf_addTok(mcReserved_typetok); return; }
269 UNTIL { updatepos(); mcLexBuf_addTok(mcReserved_untiltok); return; }
270 VAR { updatepos(); mcLexBuf_addTok(mcReserved_vartok); return; }
271 WHILE { updatepos(); mcLexBuf_addTok(mcReserved_whiletok); return; }
272 WITH { updatepos(); mcLexBuf_addTok(mcReserved_withtok); return; }
273 ASM { updatepos(); mcLexBuf_addTok(mcReserved_asmtok); return; }
274 VOLATILE { updatepos(); mcLexBuf_addTok(mcReserved_volatiletok); return; }
275 \_\_DATE\_\_ { updatepos(); handleDate(); return; }
276 \_\_LINE\_\_ { updatepos(); handleLine(); return; }
277 \_\_FILE\_\_ { updatepos(); handleFile(); return; }
278 \_\_FUNCTION\_\_ { updatepos(); handleFunction(); return; }
279 \_\_COLUMN\_\_ { updatepos(); handleColumn(); return; }
280 \_\_ATTRIBUTE\_\_ { updatepos(); mcLexBuf_addTok(mcReserved_attributetok); return; }
281 \_\_BUILTIN\_\_ { updatepos(); mcLexBuf_addTok(mcReserved_builtintok); return; }
282 \_\_INLINE\_\_ { updatepos(); mcLexBuf_addTok(mcReserved_inlinetok); return; }
283
284
285 (([0-9]*\.[0-9]+)(E[+-]?[0-9]+)?) { updatepos(); mcLexBuf_addTokCharStar(mcReserved_realtok, yytext); return; }
286 [a-zA-Z_][a-zA-Z0-9_]* { checkFunction(); updatepos(); mcLexBuf_addTokCharStar(mcReserved_identtok, yytext); return; }
287 [0-9]+ { updatepos(); mcLexBuf_addTokCharStar(mcReserved_integertok, yytext); return; }
288 [0-9]+B { updatepos(); mcLexBuf_addTokCharStar(mcReserved_integertok, yytext); return; }
289 [0-9]+C { updatepos(); mcLexBuf_addTokCharStar(mcReserved_integertok, yytext); return; }
290 [0-9A-F]+H { updatepos(); mcLexBuf_addTokCharStar(mcReserved_integertok, yytext); return; }
291 [\t\r ]+ { currentLine->tokenpos += yyleng; /* ignore whitespace */; }
292 . { updatepos(); mcflex_mcError("unrecognised symbol"); skippos(); }
293
294 %%
295
296 /* Hand built routines. */
297
298 /* handleFile handles the __FILE__ construct by wraping it in double quotes and putting
299 it into the token buffer as a string. */
300
301 static void
302 handleFile (void)
303 {
304 char *s = (char *)alloca (strlen (filename) + 2 + 1);
305
306 strcpy (s, "\"");
307 strcat (s, filename);
308 strcat (s, "\"");
309 mcLexBuf_addTokCharStar (mcReserved_stringtok, s);
310 }
311
312 /* handleLine handles the __LINE__ construct by passing an integer to
313 the token buffer. */
314
315 static void
316 handleLine (void)
317 {
318 mcLexBuf_addTokInteger(mcReserved_integertok, lineno);
319 }
320
321 /* handleColumn handles the __COLUMN__ construct by passing an integer to
322 the token buffer. */
323
324 static void
325 handleColumn (void)
326 {
327 mcLexBuf_addTokInteger(mcReserved_integertok, mcflex_getColumnNo());
328 }
329
330 /* handleDate handles the __DATE__ construct by passing the date
331 as a string to the token buffer. */
332
333 static void
334 handleDate (void)
335 {
336 time_t clock = time ((time_t *)0);
337 char *sdate = ctime (&clock);
338 char *s = (char *)alloca (strlen (sdate)+2+1);
339 char *p = strchr(sdate, '\n');
340
341 if (p != NULL) {
342 *p = (char) 0;
343 }
344 strcpy (s, "\"");
345 strcat (s, sdate);
346 strcat (s, "\"");
347 mcLexBuf_addTokCharStar (mcReserved_stringtok, s);
348 }
349
350 /* handleFunction handles the __FUNCTION__ construct by wrapping
351 it in double quotes and putting it into the token buffer as a string. */
352
353 static void
354 handleFunction (void)
355 {
356 if (currentFunction == NULL)
357 mcLexBuf_addTokCharStar (mcReserved_stringtok, (char *)("\"\""));
358 else if (currentFunction->module)
359 {
360 char *s = (char *) alloca (strlen (yytext) +
361 strlen ("\"module initialization\"") + 1);
362 strcpy (s, "\"module ");
363 strcat (s, currentFunction->name);
364 strcat (s, " initialization\"");
365 mcLexBuf_addTokCharStar (mcReserved_stringtok, s);
366 }
367 else
368 {
369 char *function = currentFunction->name;
370 char *s = (char *)alloca (strlen (function) + 2 + 1);
371 strcpy (s, "\"");
372 strcat (s, function);
373 strcat (s, "\"");
374 mcLexBuf_addTokCharStar (mcReserved_stringtok, s);
375 }
376 }
377
378 /* pushFunction pushes the function name onto the stack. */
379
380 static void
381 pushFunction (char *function, int module)
382 {
383 if (currentFunction == NULL)
384 {
385 currentFunction = (struct functionInfo *)malloc (sizeof (struct functionInfo));
386 currentFunction->name = strdup (function);
387 currentFunction->next = NULL;
388 currentFunction->module = module;
389 }
390 else
391 {
392 struct functionInfo *f = (struct functionInfo *)malloc (sizeof (struct functionInfo));
393 f->name = strdup (function);
394 f->next = currentFunction;
395 f->module = module;
396 currentFunction = f;
397 }
398 }
399
400 /* popFunction pops the current function. */
401
402 static void
403 popFunction (void)
404 {
405 if (currentFunction != NULL && currentFunction->next != NULL)
406 {
407 struct functionInfo *f = currentFunction;
408
409 currentFunction = currentFunction->next;
410 if (f->name != NULL)
411 free (f->name);
412 free (f);
413 }
414 }
415
416 /* endOfComment - handles the end of comment. */
417
418 static void
419 endOfComment (void)
420 {
421 if (commentLevel == 1) {
422 mcLexBuf_addTokComment (mcReserved_commenttok, currentComment);
423 }
424 commentLevel--;
425 updatepos ();
426 skippos ();
427 if (commentLevel==0) {
428 BEGIN INITIAL;
429 finishedLine ();
430 } else
431 popLine ();
432 }
433
434 /* mcflex_mcError displays the error message s after the code line and pointer
435 to the erroneous token. */
436
437 void
438 mcflex_mcError (const char *s)
439 {
440 if (currentLine->linebuf != NULL) {
441 int i=1;
442
443 printf("%s:%d:%s\n", filename, currentLine->actualline, currentLine->linebuf);
444 printf("%s:%d:%*s", filename, currentLine->actualline, 1+currentLine->tokenpos, "^");
445 while (i<currentLine->toklen) {
446 putchar('^');
447 i++;
448 }
449 putchar('\n');
450 }
451 printf("%s:%d:%s\n", filename, currentLine->actualline, s);
452 }
453
454 static void
455 poperrorskip (const char *s)
456 {
457 int nextpos =currentLine->nextpos;
458 int tokenpos=currentLine->tokenpos;
459
460 popLine();
461 mcflex_mcError(s);
462 if (currentLine != NULL) {
463 currentLine->nextpos = nextpos;
464 currentLine->tokenpos = tokenpos;
465 }
466 }
467
468 /* consumeLine reads a line into a buffer, it then pushes back the whole
469 line except the initial \n. */
470
471 static void
472 consumeLine (void)
473 {
474 if (currentLine->linelen<yyleng) {
475 currentLine->linebuf = (char *)realloc (currentLine->linebuf, yyleng);
476 currentLine->linelen = yyleng;
477 }
478 strcpy(currentLine->linebuf, yytext+1); /* Copy all except the initial \n */
479 lineno++;
480 totalLines++;
481 currentLine->actualline = lineno;
482 currentLine->tokenpos=0;
483 currentLine->nextpos=0;
484 currentLine->column=0;
485 START_LINE (lineno, yyleng);
486 yyless(1); /* Push back all but the \n */
487 seenOnlySpaces=TRUE;
488 }
489
490 /* detectSpaces scan yytext to see if only spaces have been seen. */
491
492 static void
493 detectSpaces (void)
494 {
495 char *p = yytext;
496 int i = 0;
497
498 if ((strcmp (yytext, "(*") != 0) &&
499 (strcmp (yytext, "*)") != 0))
500 {
501 while (i < strlen (p))
502 {
503 if (! isspace (p[i]))
504 seenOnlySpaces = FALSE;
505 else if (p[i] == '\n')
506 seenOnlySpaces = TRUE;
507 i++;
508 }
509 }
510 }
511
512 /* updatepos updates the current token position.
513 Should be used when a rule matches a token. */
514
515 static void
516 updatepos (void)
517 {
518 seenFunctionStart = FALSE;
519 seenEnd = FALSE;
520 seenModuleStart = FALSE;
521 currentLine->nextpos = currentLine->tokenpos+yyleng;
522 currentLine->toklen = yyleng;
523 if (currentLine->column == 0)
524 currentLine->column = currentLine->tokenpos;
525 if (commentLevel == 0)
526 detectSpaces ();
527 }
528
529 /* checkFunction checks to see whether we have seen the start
530 or end of a function. */
531
532 static void
533 checkFunction (void)
534 {
535 if (! isDefinitionModule) {
536 if (seenModuleStart)
537 pushFunction(yytext, 1);
538 if (seenFunctionStart)
539 pushFunction(yytext, 0);
540 if (seenEnd && currentFunction != NULL &&
541 (strcmp(currentFunction->name, yytext) == 0))
542 popFunction();
543 }
544 seenFunctionStart = FALSE;
545 seenEnd = FALSE;
546 seenModuleStart = FALSE;
547 }
548
549 /* skippos skips over this token. This function should be called
550 if we are not returning and thus not calling getToken. */
551
552 static void
553 skippos (void)
554 {
555 currentLine->tokenpos = currentLine->nextpos;
556 }
557
558 /* initLine initializes a currentLine. */
559
560 static void initLine (void)
561 {
562 currentLine = (struct lineInfo *)malloc (sizeof(struct lineInfo));
563
564 if (currentLine == NULL)
565 perror("malloc");
566 currentLine->linebuf = NULL;
567 currentLine->linelen = 0;
568 currentLine->tokenpos = 0;
569 currentLine->toklen = 0;
570 currentLine->nextpos = 0;
571 currentLine->actualline = lineno;
572 currentLine->column = 0;
573 currentLine->inuse = TRUE;
574 currentLine->next = NULL;
575 }
576
577 /* pushLine pushes a new line structure. */
578
579 static void
580 pushLine (void)
581 {
582 if (currentLine == NULL)
583 initLine();
584 else if (currentLine->inuse) {
585 struct lineInfo *l = (struct lineInfo *)malloc (sizeof(struct lineInfo));
586
587 if (currentLine->linebuf == NULL) {
588 l->linebuf = NULL;
589 l->linelen = 0;
590 } else {
591 l->linebuf = (char *)strdup (currentLine->linebuf);
592 l->linelen = strlen (l->linebuf)+1;
593 }
594 l->tokenpos = currentLine->tokenpos;
595 l->toklen = currentLine->toklen;
596 l->nextpos = currentLine->nextpos;
597 l->actualline = currentLine->actualline;
598 l->column = currentLine->column;
599 l->next = currentLine;
600 currentLine = l;
601 }
602 currentLine->inuse = TRUE;
603 }
604
605 /* popLine pops a line structure. */
606
607 static void
608 popLine (void)
609 {
610 if (currentLine != NULL) {
611 struct lineInfo *l = currentLine;
612
613 if (currentLine->linebuf != NULL)
614 free(currentLine->linebuf);
615 currentLine = l->next;
616 free(l);
617 }
618 }
619
620 /* resetpos resets the position of the next token to the start of the line. */
621
622 static void
623 resetpos (void)
624 {
625 if (currentLine != NULL)
626 currentLine->nextpos = 0;
627 }
628
629 /* finishedLine indicates that the current line does not need to be preserved
630 when a pushLine occurs. */
631
632 static void
633 finishedLine (void)
634 {
635 currentLine->inuse = FALSE;
636 }
637
638 /* mcflex_getToken returns a new token. */
639
640 char *
641 mcflex_getToken (void)
642 {
643 TIMEVAR_PUSH_LEX;
644 if (currentLine == NULL)
645 initLine ();
646 currentLine->tokenpos = currentLine->nextpos;
647 yylex ();
648 TIMEVAR_POP_LEX;
649 return yytext;
650 }
651
652 /* closeSource - provided for semantic sugar. */
653
654 void
655 mcflex_closeSource (void)
656 {
657 END_FILE ();
658 }
659
660 /* openSource returns TRUE if file s can be opened and
661 all tokens are taken from this file. */
662
663 bool
664 mcflex_openSource (char *s)
665 {
666 FILE *newInputFile = fopen (s, "r");
667
668 if (newInputFile == NULL)
669 return false;
670 else
671 {
672 isDefinitionModule = FALSE;
673 while (currentFunction != NULL)
674 {
675 struct functionInfo *f = currentFunction;
676 currentFunction = f->next;
677 if (f->name != NULL)
678 free (f->name);
679 free (f);
680 }
681 yy_delete_buffer (YY_CURRENT_BUFFER);
682 if (inputFile != NULL)
683 fclose (inputFile);
684 inputFile = newInputFile;
685 yy_switch_to_buffer (yy_create_buffer (inputFile, YY_BUF_SIZE));
686 filename = strdup (s);
687 lineno =1;
688 if (currentLine != NULL)
689 currentLine->actualline = lineno;
690 START_FILE (filename, lineno);
691 return true;
692 }
693 }
694
695 /* mcflex_getLineNo returns the current line number. */
696
697 int
698 mcflex_getLineNo (void)
699 {
700 if (currentLine != NULL)
701 return currentLine->actualline;
702 else
703 return 0;
704 }
705
706 /* mcflex_getColumnNo returns the column where the current
707 token starts. */
708
709 int
710 mcflex_getColumnNo (void)
711 {
712 if (currentLine != NULL)
713 return currentLine->column;
714 else
715 return 0;
716 }
717
718 /* getTotalLines returns the total number of lines parsed. */
719
720 int
721 mcflex_getTotalLines (void)
722 {
723 return totalLines;
724 }
725
726 /* yywrap is called when end of file is seen. We push an eof token
727 and tell the lexical analysis to stop. */
728
729 int
730 yywrap (void)
731 {
732 updatepos ();
733 mcLexBuf_addTok (mcReserved_eoftok);
734 return 1;
735 }
736
737 void
738 _M2_mcflex_init (int argc, char *argv[], char *envp[])
739 {
740 }
741
742 void
743 _M2_mcflex_fini (int argc, char *argv[], char *envp[])
744 {
745 }
746
747 void
748 _M2_mcflex_finish (void)
749 {
750 }
751
752 /* This is a gross hack to satisfy linking. */
753
754 void
755 _M2_mcflex_ctor (void)
756 {
757 }