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