2 * Wrc preprocessor lexical analysis
4 * Copyright 1999-2000 Bertho A. Stultiens (BS)
6 * 24-Apr-2000 BS - Started from scratch to restructure everything
7 * and reintegrate the source into the wine-tree.
8 * 04-Jan-2000 BS - Added comments about the lexicographical
9 * grammar to give some insight in the complexity.
10 * 28-Dec-1999 BS - Eliminated backing-up of the flexer by running
11 * `flex -b' on the source. This results in some
12 * weirdo extra rules, but a much faster scanner.
13 * 23-Dec-1999 BS - Started this file
15 *-------------------------------------------------------------------------
16 * The preprocessor's lexographical grammar (approximately):
18 * pp := {ws} # {ws} if {ws} {expr} {ws} \n
19 * | {ws} # {ws} ifdef {ws} {id} {ws} \n
20 * | {ws} # {ws} ifndef {ws} {id} {ws} \n
21 * | {ws} # {ws} elif {ws} {expr} {ws} \n
22 * | {ws} # {ws} else {ws} \n
23 * | {ws} # {ws} endif {ws} \n
24 * | {ws} # {ws} include {ws} < {anytext} > \n
25 * | {ws} # {ws} include {ws} " {anytext} " \n
26 * | {ws} # {ws} define {ws} {anytext} \n
27 * | {ws} # {ws} define( {arglist} ) {ws} {expansion} \n
28 * | {ws} # {ws} pragma {ws} {anytext} \n
29 * | {ws} # {ws} ident {ws} {anytext} \n
30 * | {ws} # {ws} error {ws} {anytext} \n
31 * | {ws} # {ws} warning {ws} {anytext} \n
32 * | {ws} # {ws} line {ws} " {anytext} " {number} \n
33 * | {ws} # {ws} {number} " {anytext} " {number} [{number} [{number}]] \n
38 * expr := {expr} [+-*%^/|&] {expr}
39 * | {expr} {logor|logand} {expr}
41 * | {expr} ? {expr} : {expr}
47 * id := [a-zA-Z_][a-zA-Z0-9_]*
49 * anytext := [^\n]* (see note)
54 * | {arglist} , {id} ...
59 * | {anytext} ## {anytext}
63 * Note: "anytext" is not always "[^\n]*". This is because the
64 * trailing context must be considered as well.
66 * The only certain assumption for the preprocessor to make is that
67 * directives start at the beginning of the line, followed by a '#'
68 * and end with a newline.
69 * Any directive may be suffixed with a line-continuation. Also
70 * classical comment / *...* / (note: no comments within comments,
71 * therefore spaces) is considered to be a line-continuation
72 * (according to gcc and egcs AFAIK, ANSI is a bit vague).
73 * Comments have not been added to the above grammer for simplicity
74 * reasons. However, it is allowed to enter comment anywhere within
75 * the directives as long as they do not interfere with the context.
76 * All comments are considered to be deletable whitespace (both
77 * classical form "/ *...* /" and C++ form "//...\n").
79 * All recursive scans, except for macro-expansion, are done by the
80 * parser, whereas the simple state transitions of non-recursive
81 * directives are done in the scanner. This results in the many
82 * exclusive start-conditions of the scanner.
84 * Macro expansions are slightly more difficult because they have to
85 * prescan the arguments. Parameter substitution is literal if the
86 * substitution is # or ## (either side). This enables new identifiers
87 * to be created (see 'info cpp' node Macro|Pitfalls|Prescan for more
90 * FIXME: Variable macro parameters is recognized, but not yet
91 * expanded. I have to reread the ANSI standard on the subject (yes,
94 * The following special defines are supported:
95 * __FILE__ -> "thissource.c"
97 * __DATE__ -> "May 1 2000"
98 * __TIME__ -> "23:59:59"
99 * These macros expand, as expected, into their ANSI defined values.
101 * The same include prevention is implemented as gcc and egcs does.
102 * This results in faster processing because we do not read the text
103 * at all. Some wine-sources attempt to include the same file 4 or 5
104 * times. This strategy also saves a lot blank output-lines, which in
105 * its turn improves the real resource scanner/parser.
110 * Special flex options and exclusive scanner start-conditions
113 %option never-interactive
136 cident [a-zA-Z_][0-9a-zA-Z_]*
137 ul [uUlL]|[uUlL][lL]|[lL][uU]|[lL][lL][uU]|[uU][lL][lL]|[lL][uU][lL]
155 * Make sure that we are running an appropriate version of flex.
157 #if !defined(YY_FLEX_MAJOR_VERSION) || (1000 * YY_FLEX_MAJOR_VERSION + YY_FLEX_MINOR_VERSION < 2005)
158 #error Must use flex version 2.5.1 or higher (yy_scan_* routines are required).
161 #define YY_USE_PROTOS
163 #define YY_READ_BUF_SIZE 65536 /* So we read most of a file at once */
165 #define yy_current_state() YY_START
166 #define yy_pp_state(x) yy_pop_state(); yy_push_state(x)
169 * Always update the current character position within a line
171 #define YY_USER_ACTION char_number+=ppleng;
174 * Buffer management for includes and expansions
176 #define MAXBUFFERSTACK 128 /* Nesting more than 128 includes or macro expansion textss is insane */
178 typedef struct bufferstackentry {
179 YY_BUFFER_STATE bufferstate; /* Buffer to switch back to */
180 pp_entry_t *define; /* Points to expanding define or NULL if handling includes */
181 int line_number; /* Line that we were handling */
182 int char_number; /* The current position on that line */
183 char *filename; /* Filename that we were handling */
184 int if_depth; /* How many #if:s deep to check matching #endif:s */
185 int ncontinuations; /* Remember the continuation state */
186 int should_pop; /* Set if we must pop the start-state on EOF */
187 /* Include management */
190 char *include_filename;
193 } bufferstackentry_t;
195 #define ALLOCBLOCKSIZE (1 << 10) /* Allocate these chunks at a time for string-buffers */
198 * Macro expansion nesting
199 * We need the stack to handle expansions while scanning
200 * a macro's arguments. The TOS must always be the macro
201 * that receives the current expansion from the scanner.
203 #define MAXMACEXPSTACK 128 /* Nesting more than 128 macro expansions is insane */
205 typedef struct macexpstackentry {
206 pp_entry_t *ppp; /* This macro we are scanning */
207 char **args; /* With these arguments */
208 char **ppargs; /* Resulting in these preprocessed arguments */
209 int *nnls; /* Number of newlines per argument */
210 int nargs; /* And this many arguments scanned */
211 int parentheses; /* Nesting level of () */
212 int curargsize; /* Current scanning argument's size */
213 int curargalloc; /* Current scanning argument's block allocated */
214 char *curarg; /* Current scanning argument's content */
215 } macexpstackentry_t;
217 #define MACROPARENTHESES() (top_macro()->parentheses)
222 static void newline(int);
223 static int make_number(int radix, YYSTYPE *val, char *str, int len);
224 static void put_buffer(char *s, int len);
225 /* Buffer management */
226 static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop);
227 static bufferstackentry_t *pop_buffer(void);
228 /* String functions */
229 static void new_string(void);
230 static void add_string(char *str, int len);
231 static char *get_string(void);
232 static void put_string(void);
233 static int string_start(void);
234 /* Macro functions */
235 static void push_macro(pp_entry_t *ppp);
236 static macexpstackentry_t *top_macro(void);
237 static macexpstackentry_t *pop_macro(void);
238 static void free_macro(macexpstackentry_t *mep);
239 static void add_text_to_macro(char *text, int len);
240 static void macro_add_arg(int last);
241 static void macro_add_expansion(void);
243 static void expand_special(pp_entry_t *ppp);
244 static void expand_define(pp_entry_t *ppp);
245 static void expand_macro(macexpstackentry_t *mep);
250 static int ncontinuations;
252 static int strbuf_idx = 0;
253 static int strbuf_alloc = 0;
254 static char *strbuffer = NULL;
255 static int str_startline;
257 static macexpstackentry_t *macexpstack[MAXMACEXPSTACK];
258 static int macexpstackidx = 0;
260 static bufferstackentry_t bufferstack[MAXBUFFERSTACK];
261 static int bufferstackidx = 0;
267 * Trace the include files to prevent double reading.
268 * This save 20..30% of processing time for most stuff
269 * that uses complex includes.
271 * -1 Don't track or seen junk
272 * 0 New include, waiting for "#ifndef __xxx_h"
273 * 1 Seen #ifndef, waiting for "#define __xxx_h ..."
274 * 2 Seen #endif, waiting for EOF
276 int include_state = -1;
277 char *include_ppp = NULL; /* The define to be set from the #ifndef */
278 int include_ifdepth = 0; /* The level of ifs at the #ifdef */
279 int seen_junk = 0; /* Set when junk is seen */
280 includelogicentry_t *includelogiclist = NULL;
285 **************************************************************************
286 * The scanner starts here
287 **************************************************************************
292 * Catch line-continuations.
293 * Note: Gcc keeps the line-continuations in, for example, strings
294 * intact. However, I prefer to remove them all so that the next
295 * scanner will not need to reduce the continuation state.
297 * <*>\\\n newline(0);
301 * Detect the leading # of a preprocessor directive.
303 <INITIAL,pp_ignore>^{ws}*# seen_junk++; yy_push_state(pp_pp);
306 * Scan for the preprocessor directives
308 <pp_pp>{ws}*include{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_inc); return tINCLUDE;} else {yy_pp_state(pp_eol);}
309 <pp_pp>{ws}*define{ws}* yy_pp_state(yy_current_state() != pp_ignore ? pp_def : pp_eol);
310 <pp_pp>{ws}*error{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tERROR;
311 <pp_pp>{ws}*warning{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tWARNING;
312 <pp_pp>{ws}*pragma{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPRAGMA;
313 <pp_pp>{ws}*ident{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPPIDENT;
314 <pp_pp>{ws}*undef{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_ifd); return tUNDEF;} else {yy_pp_state(pp_eol);}
315 <pp_pp>{ws}*ifdef{ws}* yy_pp_state(pp_ifd); return tIFDEF;
316 <pp_pp>{ws}*ifndef{ws}* seen_junk--; yy_pp_state(pp_ifd); return tIFNDEF;
317 <pp_pp>{ws}*if{ws}* yy_pp_state(pp_if); return tIF;
318 <pp_pp>{ws}*elif{ws}* yy_pp_state(pp_if); return tELIF;
319 <pp_pp>{ws}*else{ws}* return tELSE;
320 <pp_pp>{ws}*endif{ws}* return tENDIF;
321 <pp_pp>{ws}*line{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tLINE;} else {yy_pp_state(pp_eol);}
322 <pp_pp>{ws}+ if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tGCCLINE;} else {yy_pp_state(pp_eol);}
323 <pp_pp>{ws}*[a-z]+ pperror("Invalid preprocessor token '%s'", pptext);
324 <pp_pp>\r?\n newline(1); yy_pop_state(); return tNL; /* This could be the null-token */
325 <pp_pp>\\\r?\n newline(0);
326 <pp_pp>\\\r? pperror("Preprocessor junk '%s'", pptext);
327 <pp_pp>. return *pptext;
330 * Handle #include and #line
332 <pp_line>[0-9]+ return make_number(10, &pplval, pptext, ppleng);
333 <pp_inc>\< new_string(); add_string(pptext, ppleng); yy_push_state(pp_iqs);
334 <pp_inc,pp_line>\" new_string(); add_string(pptext, ppleng); yy_push_state(pp_dqs);
335 <pp_inc,pp_line>{ws}+ ;
336 <pp_inc,pp_line>\n newline(1); yy_pop_state(); return tNL;
337 <pp_inc,pp_line>\\\r?\n newline(0);
338 <pp_inc,pp_line>(\\\r?)|(.) pperror(yy_current_state() == pp_inc ? "Trailing junk in #include" : "Trailing junk in #line");
341 * Ignore all input when a false clause is parsed
343 <pp_ignore>[^#/\\\n]+ ;
344 <pp_ignore>\n newline(1);
345 <pp_ignore>\\\r?\n newline(0);
346 <pp_ignore>(\\\r?)|(.) ;
349 * Handle #if and #elif.
350 * These require conditionals to be evaluated, but we do not
351 * want to jam the scanner normally when we see these tokens.
352 * Note: tIDENT is handled below.
355 <pp_if>0[0-7]*{ul}? return make_number(8, &pplval, pptext, ppleng);
356 <pp_if>0[0-7]*[8-9]+{ul}? pperror("Invalid octal digit");
357 <pp_if>[1-9][0-9]*{ul}? return make_number(10, &pplval, pptext, ppleng);
358 <pp_if>0[xX][0-9a-fA-F]+{ul}? return make_number(16, &pplval, pptext, ppleng);
359 <pp_if>0[xX] pperror("Invalid hex number");
360 <pp_if>defined yy_push_state(pp_defined); return tDEFINED;
361 <pp_if>"<<" return tLSHIFT;
362 <pp_if>">>" return tRSHIFT;
363 <pp_if>"&&" return tLOGAND;
364 <pp_if>"||" return tLOGOR;
365 <pp_if>"==" return tEQ;
366 <pp_if>"!=" return tNE;
367 <pp_if>"<=" return tLTE;
368 <pp_if>">=" return tGTE;
369 <pp_if>\n newline(1); yy_pop_state(); return tNL;
370 <pp_if>\\\r?\n newline(0);
371 <pp_if>\\\r? pperror("Junk in conditional expression");
373 <pp_if>\' new_string(); add_string(pptext, ppleng); yy_push_state(pp_sqs);
374 <pp_if>\" pperror("String constants not allowed in conditionals");
375 <pp_if>. return *pptext;
378 * Handle #ifdef, #ifndef and #undef
379 * to get only an untranslated/unexpanded identifier
381 <pp_ifd>{cident} pplval.cptr = xstrdup(pptext); return tIDENT;
383 <pp_ifd>\n newline(1); yy_pop_state(); return tNL;
384 <pp_ifd>\\\r?\n newline(0);
385 <pp_ifd>(\\\r?)|(.) pperror("Identifier expected");
388 * Handle the special 'defined' keyword.
389 * This is necessary to get the identifier prior to any
392 <pp_defined>{cident} yy_pop_state(); pplval.cptr = xstrdup(pptext); return tIDENT;
394 <pp_defined>(\()|(\)) return *pptext;
395 <pp_defined>\\\r?\n newline(0);
396 <pp_defined>(\\.)|(\n)|(.) pperror("Identifier expected");
399 * Handle #error, #warning, #pragma and #ident.
400 * Pass everything literally to the parser, which
401 * will act appropriately.
402 * Comments are stripped from the literal text.
404 <pp_eol>[^/\\\n]+ if(yy_top_state() != pp_ignore) { pplval.cptr = xstrdup(pptext); return tLITERAL; }
405 <pp_eol>\/[^/\\\n*]* if(yy_top_state() != pp_ignore) { pplval.cptr = xstrdup(pptext); return tLITERAL; }
406 <pp_eol>(\\\r?)|(\/[^/*]) if(yy_top_state() != pp_ignore) { pplval.cptr = xstrdup(pptext); return tLITERAL; }
407 <pp_eol>\n newline(1); yy_pop_state(); if(yy_current_state() != pp_ignore) { return tNL; }
408 <pp_eol>\\\r?\n newline(0);
411 * Handle left side of #define
413 <pp_def>{cident}\( pplval.cptr = xstrdup(pptext); pplval.cptr[ppleng-1] = '\0'; yy_pp_state(pp_macro); return tMACRO;
414 <pp_def>{cident} pplval.cptr = xstrdup(pptext); yy_pp_state(pp_define); return tDEFINE;
416 <pp_def>\\\r?\n newline(0);
417 <pp_def>(\\\r?)|(\n)|(.) perror("Identifier expected");
420 * Scan the substitution of a define
422 <pp_define>[^'"/\\\n]+ pplval.cptr = xstrdup(pptext); return tLITERAL;
423 <pp_define>(\\\r?)|(\/[^/*]) pplval.cptr = xstrdup(pptext); return tLITERAL;
424 <pp_define>\\\r?\n{ws}+ newline(0); pplval.cptr = xstrdup(" "); return tLITERAL;
425 <pp_define>\\\r?\n newline(0);
426 <pp_define>\n newline(1); yy_pop_state(); return tNL;
427 <pp_define>\' new_string(); add_string(pptext, ppleng); yy_push_state(pp_sqs);
428 <pp_define>\" new_string(); add_string(pptext, ppleng); yy_push_state(pp_dqs);
431 * Scan the definition macro arguments
433 <pp_macro>\){ws}* yy_pp_state(pp_mbody); return tMACROEND;
435 <pp_macro>{cident} pplval.cptr = xstrdup(pptext); return tIDENT;
436 <pp_macro>, return ',';
437 <pp_macro>"..." return tELIPSIS;
438 <pp_macro>(\\\r?)|(\n)|(.)|(\.\.?) pperror("Argument identifier expected");
439 <pp_macro>\\\r?\n newline(0);
442 * Scan the substitution of a macro
444 <pp_mbody>[^a-zA-Z0-9'"#/\\\n]+ pplval.cptr = xstrdup(pptext); return tLITERAL;
445 <pp_mbody>{cident} pplval.cptr = xstrdup(pptext); return tIDENT;
446 <pp_mbody>\#\# return tCONCAT;
447 <pp_mbody>\# return tSTRINGIZE;
448 <pp_mbody>[0-9][^'"#/\\\n]* pplval.cptr = xstrdup(pptext); return tLITERAL;
449 <pp_mbody>(\\\r?)|(\/[^/*'"#\\\n]*) pplval.cptr = xstrdup(pptext); return tLITERAL;
450 <pp_mbody>\\\r?\n{ws}+ newline(0); pplval.cptr = xstrdup(" "); return tLITERAL;
451 <pp_mbody>\\\r?\n newline(0);
452 <pp_mbody>\n newline(1); yy_pop_state(); return tNL;
453 <pp_mbody>\' new_string(); add_string(pptext, ppleng); yy_push_state(pp_sqs);
454 <pp_mbody>\" new_string(); add_string(pptext, ppleng); yy_push_state(pp_dqs);
457 * Macro expansion text scanning.
458 * This state is active just after the identifier is scanned
459 * that triggers an expansion. We *must* delete the leading
460 * whitespace before we can start scanning for arguments.
462 * If we do not see a '(' as next trailing token, then we have
463 * a false alarm. We just continue with a nose-bleed...
465 <pp_macign>{ws}*/\( yy_pp_state(pp_macscan);
467 if(yy_top_state() != pp_macscan)
470 <pp_macign>{ws}*\\\r?\n newline(0);
471 <pp_macign>{ws}+|{ws}*\\\r?|. {
472 macexpstackentry_t *mac = pop_macro();
474 put_buffer(mac->ppp->ident, strlen(mac->ppp->ident));
475 put_buffer(pptext, ppleng);
480 * Macro expansion argument text scanning.
481 * This state is active when a macro's arguments are being read for expansion.
484 if(++MACROPARENTHESES() > 1)
485 add_text_to_macro(pptext, ppleng);
488 if(--MACROPARENTHESES() == 0)
494 add_text_to_macro(pptext, ppleng);
497 if(MACROPARENTHESES() > 1)
498 add_text_to_macro(pptext, ppleng);
502 <pp_macscan>\" new_string(); add_string(pptext, ppleng); yy_push_state(pp_dqs);
503 <pp_macscan>\' new_string(); add_string(pptext, ppleng); yy_push_state(pp_sqs);
504 <pp_macscan>"/*" yy_push_state(pp_comment); add_text_to_macro(" ", 1);
505 <pp_macscan>\n line_number++; char_number = 1; add_text_to_macro(pptext, ppleng);
506 <pp_macscan>([^/(),\\\n"']+)|(\/[^/*(),\\\n'"]*)|(\\\r?)|(.) add_text_to_macro(pptext, ppleng);
507 <pp_macscan>\\\r?\n newline(0);
510 * Comment handling (almost all start-conditions)
512 <INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_defined,pp_def,pp_define,pp_macro,pp_mbody>"/*" yy_push_state(pp_comment);
513 <pp_comment>[^*\n]*|"*"+[^*/\n]* ;
514 <pp_comment>\n newline(0);
515 <pp_comment>"*"+"/" yy_pop_state();
518 * Remove C++ style comment (almost all start-conditions)
520 <INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_defined,pp_def,pp_define,pp_macro,pp_mbody,pp_macscan>"//"[^\n]* {
521 if(pptext[ppleng-1] == '\\')
522 ppwarning("C++ style comment ends with an escaped newline (escape ignored)");
526 * Single, double and <> quoted constants
528 <INITIAL,pp_macexp>\" seen_junk++; new_string(); add_string(pptext, ppleng); yy_push_state(pp_dqs);
529 <INITIAL,pp_macexp>\' seen_junk++; new_string(); add_string(pptext, ppleng); yy_push_state(pp_sqs);
530 <pp_dqs>[^"\\\n]+ add_string(pptext, ppleng);
532 add_string(pptext, ppleng);
534 switch(yy_current_state())
541 pplval.cptr = get_string();
547 <pp_sqs>[^'\\\n]+ add_string(pptext, ppleng);
549 add_string(pptext, ppleng);
551 switch(yy_current_state())
556 pplval.cptr = get_string();
562 <pp_iqs>[^\>\\\n]+ add_string(pptext, ppleng);
564 add_string(pptext, ppleng);
566 pplval.cptr = get_string();
571 * This is tricky; we need to remove the line-continuation
572 * from preprocessor strings, but OTOH retain them in all
573 * other strings. This is because the resource grammar is
574 * even more braindead than initially analysed and line-
575 * continuations in strings introduce, sigh, newlines in
576 * the output. There goes the concept of non-breaking, non-
577 * spacing whitespace.
579 switch(yy_top_state())
589 add_string(pptext, ppleng);
593 <pp_iqs,pp_dqs,pp_sqs>\\. add_string(pptext, ppleng);
594 <pp_iqs,pp_dqs,pp_sqs>\n {
596 add_string(pptext, ppleng);
597 ppwarning("Newline in string constant encounterd (started line %d)", string_start());
601 * Identifier scanning
603 <INITIAL,pp_if,pp_inc,pp_macexp>{cident} {
606 if(!(ppp = pplookup(pptext)))
608 if(yy_current_state() == pp_inc)
609 pperror("Expected include filename");
611 if(yy_current_state() == pp_if)
613 pplval.cptr = xstrdup(pptext);
617 put_buffer(pptext, ppleng);
619 else if(!ppp->expanding)
630 yy_push_state(pp_macign);
634 internal_error(__FILE__, __LINE__, "Invalid define type %d\n", ppp->type);
640 * Everything else that needs to be passed and
641 * newline and continuation handling
643 <INITIAL,pp_macexp>[^a-zA-Z_#'"/\\\n \r\t\f\v]+|(\/|\\)[^a-zA-Z_/*'"\\\n \r\t\v\f]* seen_junk++; put_buffer(pptext, ppleng);
644 <INITIAL,pp_macexp>{ws}+ put_buffer(pptext, ppleng);
645 <INITIAL>\n newline(1);
646 <INITIAL>\\\r?\n newline(0);
647 <INITIAL>\\\r? seen_junk++; put_buffer(pptext, ppleng);
650 * Special catcher for macro argmument expansion to prevent
651 * newlines to propagate to the output or admin.
653 <pp_macexp>(\n)|(.)|(\\\r?(\n|.)) put_buffer(pptext, ppleng);
656 * This is a 'catch-all' rule to discover errors in the scanner
657 * in an orderly manner.
659 <*>. seen_junk++; ppwarning("Unmatched text '%c' (0x%02x); please report\n", isprint(*pptext & 0xff) ? *pptext : ' ', *pptext);
662 YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
663 bufferstackentry_t *bep = pop_buffer();
665 if((!bep && get_if_depth()) || (bep && get_if_depth() != bep->if_depth))
666 ppwarning("Unmatched #if/#endif at end of file");
670 if(YY_START != INITIAL)
671 pperror("Unexpected end of file during preprocessing");
674 else if(bep->should_pop == 2)
676 macexpstackentry_t *mac;
685 **************************************************************************
687 **************************************************************************
699 *-------------------------------------------------------------------------
700 * Output newlines or set them as continuations
702 * Input: -1 - Don't count this one, but update local position (see pp_dqs)
703 * 0 - Line-continuation seen and cache output
704 * 1 - Newline seen and flush output
705 *-------------------------------------------------------------------------
707 static void newline(int dowrite)
718 for(;ncontinuations; ncontinuations--)
725 *-------------------------------------------------------------------------
726 * Make a number out of an any-base and suffixed string
728 * Possible number extensions:
731 * - "LL" long long int
733 * - "UL" unsigned long int
734 * - "ULL" unsigned long long int
735 * - "LU" unsigned long int
736 * - "LLU" unsigned long long int
740 * The sizes of resulting 'int' and 'long' are compiler specific.
741 * I depend on sizeof(int) > 2 here (although a relatively safe
743 * Long longs are not yet implemented because this is very compiler
744 * specific and I don't want to think too much about the problems.
746 *-------------------------------------------------------------------------
748 static int make_number(int radix, YYSTYPE *val, char *str, int len)
756 ext[2] = toupper(str[len-1]);
757 ext[1] = len > 1 ? toupper(str[len-2]) : ' ';
758 ext[0] = len > 2 ? toupper(str[len-3]) : ' ';
760 if(!strcmp(ext, "LUL"))
761 pperror("Invalid constant suffix");
762 else if(!strcmp(ext, "LLU") || !strcmp(ext, "ULL"))
767 else if(!strcmp(ext+1, "LU") || !strcmp(ext+1, "UL"))
772 else if(!strcmp(ext+1, "LL"))
776 else if(!strcmp(ext+2, "L"))
780 else if(!strcmp(ext+2, "U"))
786 internal_error(__FILE__, __LINE__, "long long constants not implemented yet");
790 val->ulong = strtoul(str, NULL, radix);
793 else if(!is_u && is_l)
795 val->slong = strtol(str, NULL, radix);
798 else if(is_u && !is_l)
800 val->uint = (unsigned int)strtoul(str, NULL, radix);
801 if(!win32 && val->uint > 65535)
803 pperror("Constant overflow");
808 /* Else it must be an int... */
809 val->sint = (int)strtol(str, NULL, radix);
810 if(!win32 && (val->sint < -32768 || val->sint > 32768))
813 * Note: test must be > 32768 because unary minus
814 * is handled as an expression! This can result in
815 * failure and must be checked in the parser.
817 pperror("Constant overflow");
824 *-------------------------------------------------------------------------
825 * Macro and define expansion support
827 * FIXME: Variable macro arguments.
828 *-------------------------------------------------------------------------
830 static void expand_special(pp_entry_t *ppp)
833 static char *buf = NULL;
835 assert(ppp->type == def_special);
837 if(!strcmp(ppp->ident, "__LINE__"))
839 dbgtext = "def_special(__LINE__)";
840 buf = xrealloc(buf, 32);
841 sprintf(buf, "%d", line_number);
843 else if(!strcmp(ppp->ident, "__FILE__"))
845 dbgtext = "def_special(__FILE__)";
846 buf = xrealloc(buf, strlen(input_name) + 3);
847 sprintf(buf, "\"%s\"", input_name);
849 else if(!strcmp(ppp->ident, "__DATE__"))
851 dbgtext = "def_special(__DATE__)";
852 buf = xrealloc(buf, 32);
853 strftime(buf, 32, "\"%b %d %Y\"", localtime(&now));
855 else if(!strcmp(ppp->ident, "__TIME__"))
857 dbgtext = "def_special(__TIME__)";
858 buf = xrealloc(buf, 32);
859 strftime(buf, 32, "\"%H:%M:%S\"", localtime(&now));
862 internal_error(__FILE__, __LINE__, "Special macro '%s' not found...\n", ppp->ident);
864 if(debuglevel & DEBUGLEVEL_PPLEX)
865 fprintf(stderr, "expand_special(%d): %s:%d: '%s' -> '%s'\n",
874 push_buffer(ppp, NULL, NULL, 0);
879 static void expand_define(pp_entry_t *ppp)
881 assert(ppp->type == def_define);
883 if(debuglevel & DEBUGLEVEL_PPLEX)
884 fprintf(stderr, "expand_define(%d): %s:%d: '%s' -> '%s'\n",
890 if(ppp->subst.text && ppp->subst.text[0])
892 push_buffer(ppp, NULL, NULL, 0);
893 yy_scan_string(ppp->subst.text);
897 static int curdef_idx = 0;
898 static int curdef_alloc = 0;
899 static char *curdef_text = NULL;
901 static void add_text(char *str, int len)
905 if(curdef_idx >= curdef_alloc || curdef_alloc - curdef_idx < len)
907 curdef_alloc += (len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1);
908 curdef_text = xrealloc(curdef_text, curdef_alloc * sizeof(curdef_text[0]));
909 if(curdef_alloc > 65536)
910 ppwarning("Reallocating macro-expansion buffer larger than 64kB");
912 memcpy(&curdef_text[curdef_idx], str, len);
916 static mtext_t *add_expand_text(mtext_t *mtp, macexpstackentry_t *mep, int *nnl)
929 if(debuglevel & DEBUGLEVEL_PPLEX)
930 fprintf(stderr, "add_expand_text: exp_text: '%s'\n", mtp->subst.text);
931 add_text(mtp->subst.text, strlen(mtp->subst.text));
935 if(debuglevel & DEBUGLEVEL_PPLEX)
936 fprintf(stderr, "add_expand_text: exp_stringize(%d): '%s'\n",
938 mep->args[mtp->subst.argidx]);
939 cptr = mep->args[mtp->subst.argidx];
943 if(*cptr == '"' || *cptr == '\\')
952 if(debuglevel & DEBUGLEVEL_PPLEX)
953 fprintf(stderr, "add_expand_text: exp_concat\n");
954 /* Remove trailing whitespace from current expansion text */
957 if(isspace(curdef_text[curdef_idx-1] & 0xff))
962 /* tag current position and recursively expand the next part */
964 mtp = add_expand_text(mtp->next, mep, nnl);
966 /* Now get rid of the leading space of the expansion */
967 cptr = &curdef_text[tag];
968 n = curdef_idx - tag;
971 if(isspace(*cptr & 0xff))
979 if(cptr != &curdef_text[tag])
981 memmove(&curdef_text[tag], cptr, n);
982 curdef_idx -= (curdef_idx - tag) - n;
987 if((mtp->next && mtp->next->type == exp_concat) || (mtp->prev && mtp->prev->type == exp_concat))
988 exp = mep->args[mtp->subst.argidx];
990 exp = mep->ppargs[mtp->subst.argidx];
993 add_text(exp, strlen(exp));
994 *nnl -= mep->nnls[mtp->subst.argidx];
995 cptr = strchr(exp, '\n');
999 cptr = strchr(cptr+1, '\n');
1001 mep->nnls[mtp->subst.argidx] = 0;
1003 if(debuglevel & DEBUGLEVEL_PPLEX)
1004 fprintf(stderr, "add_expand_text: exp_subst(%d): '%s'\n", mtp->subst.argidx, exp);
1008 internal_error(__FILE__, __LINE__, "Invalid expansion type (%d) in macro expansion\n", mtp->type);
1013 static void expand_macro(macexpstackentry_t *mep)
1019 pp_entry_t *ppp = mep->ppp;
1020 int nargs = mep->nargs;
1022 assert(ppp->type == def_macro);
1023 assert(ppp->expanding == 0);
1025 if((ppp->nargs >= 0 && nargs != ppp->nargs) || (ppp->nargs < 0 && nargs < -ppp->nargs))
1026 pperror("Too %s macro arguments (%d)", nargs < abs(ppp->nargs) ? "few" : "many", nargs);
1028 for(n = 0; n < nargs; n++)
1029 nnl += mep->nnls[n];
1031 if(debuglevel & DEBUGLEVEL_PPLEX)
1032 fprintf(stderr, "expand_macro(%d): %s:%d: '%s'(%d,%d) -> ...\n",
1042 for(mtp = ppp->subst.mtext; mtp; mtp = mtp->next)
1044 if(!(mtp = add_expand_text(mtp, mep, &nnl)))
1048 for(n = 0; n < nnl; n++)
1051 /* To make sure there is room and termination (see below) */
1054 /* Strip trailing whitespace from expansion */
1055 for(k = curdef_idx, cptr = &curdef_text[curdef_idx-1]; k > 0; k--, cptr--)
1057 if(!isspace(*cptr & 0xff))
1062 * We must add *one* whitespace to make sure that there
1063 * is a token-seperation after the expansion.
1069 /* Strip leading whitespace from expansion */
1070 for(n = 0, cptr = curdef_text; n < k; n++, cptr++)
1072 if(!isspace(*cptr & 0xff))
1078 if(debuglevel & DEBUGLEVEL_PPLEX)
1079 fprintf(stderr, "expand_text: '%s'\n", curdef_text + n);
1080 push_buffer(ppp, NULL, NULL, 0);
1081 /*yy_scan_bytes(curdef_text + n, k - n);*/
1082 yy_scan_string(curdef_text + n);
1087 *-------------------------------------------------------------------------
1088 * String collection routines
1089 *-------------------------------------------------------------------------
1091 static void new_string(void)
1095 ppwarning("new_string: strbuf_idx != 0");
1098 str_startline = line_number;
1101 static void add_string(char *str, int len)
1105 if(strbuf_idx >= strbuf_alloc || strbuf_alloc - strbuf_idx < len)
1107 strbuf_alloc += (len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1);
1108 strbuffer = xrealloc(strbuffer, strbuf_alloc * sizeof(strbuffer[0]));
1109 if(strbuf_alloc > 65536)
1110 ppwarning("Reallocating string buffer larger than 64kB");
1112 memcpy(&strbuffer[strbuf_idx], str, len);
1116 static char *get_string(void)
1118 char *str = (char *)xmalloc(strbuf_idx + 1);
1119 memcpy(str, strbuffer, strbuf_idx);
1120 str[strbuf_idx] = '\0';
1127 static void put_string(void)
1129 put_buffer(strbuffer, strbuf_idx);
1135 static int string_start(void)
1137 return str_startline;
1142 *-------------------------------------------------------------------------
1144 *-------------------------------------------------------------------------
1146 static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop)
1149 printf("push_buffer(%d): %p %p %p %d\n", bufferstackidx, ppp, filename, incname, pop);
1150 if(bufferstackidx >= MAXBUFFERSTACK)
1151 internal_error(__FILE__, __LINE__, "Buffer stack overflow");
1153 memset(&bufferstack[bufferstackidx], 0, sizeof(bufferstack[0]));
1154 bufferstack[bufferstackidx].bufferstate = YY_CURRENT_BUFFER;
1155 bufferstack[bufferstackidx].define = ppp;
1156 bufferstack[bufferstackidx].line_number = line_number;
1157 bufferstack[bufferstackidx].char_number = char_number;
1158 bufferstack[bufferstackidx].if_depth = get_if_depth();
1159 bufferstack[bufferstackidx].should_pop = pop;
1160 bufferstack[bufferstackidx].filename = input_name;
1161 bufferstack[bufferstackidx].ncontinuations = ncontinuations;
1162 bufferstack[bufferstackidx].include_state = include_state;
1163 bufferstack[bufferstackidx].include_ppp = include_ppp;
1164 bufferstack[bufferstackidx].include_filename = incname;
1165 bufferstack[bufferstackidx].include_ifdepth = include_ifdepth;
1166 bufferstack[bufferstackidx].seen_junk = seen_junk;
1172 /* These will track the pperror to the correct file and line */
1175 input_name = filename;
1179 internal_error(__FILE__, __LINE__, "Pushing buffer without knowing where to go to");
1183 static bufferstackentry_t *pop_buffer(void)
1185 if(bufferstackidx < 0)
1186 internal_error(__FILE__, __LINE__, "Bufferstack underflow?");
1188 if(bufferstackidx == 0)
1193 if(bufferstack[bufferstackidx].define)
1194 bufferstack[bufferstackidx].define->expanding = 0;
1197 line_number = bufferstack[bufferstackidx].line_number;
1198 char_number = bufferstack[bufferstackidx].char_number;
1199 input_name = bufferstack[bufferstackidx].filename;
1200 ncontinuations = bufferstack[bufferstackidx].ncontinuations;
1201 if(!bufferstack[bufferstackidx].should_pop)
1204 fprintf(ppout, "# %d \"%s\" 2\n", line_number, input_name);
1206 /* We have EOF, check the include logic */
1207 if(include_state == 2 && !seen_junk && include_ppp)
1209 pp_entry_t *ppp = pplookup(include_ppp);
1212 includelogicentry_t *iep = xmalloc(sizeof(includelogicentry_t));
1215 iep->filename = bufferstack[bufferstackidx].include_filename;
1216 iep->next = includelogiclist;
1218 iep->next->prev = iep;
1219 includelogiclist = iep;
1220 if(debuglevel & DEBUGLEVEL_PPMSG)
1221 fprintf(stderr, "pop_buffer: %s:%d: includelogic added, include_ppp='%s', file='%s'\n", input_name, line_number, include_ppp, iep->filename);
1223 else if(bufferstack[bufferstackidx].include_filename)
1224 free(bufferstack[bufferstackidx].include_filename);
1228 include_state = bufferstack[bufferstackidx].include_state;
1229 include_ppp = bufferstack[bufferstackidx].include_ppp;
1230 include_ifdepth = bufferstack[bufferstackidx].include_ifdepth;
1231 seen_junk = bufferstack[bufferstackidx].seen_junk;
1236 printf("pop_buffer(%d): %p %p (%d, %d, %d) %p %d\n",
1238 bufferstack[bufferstackidx].bufferstate,
1239 bufferstack[bufferstackidx].define,
1240 bufferstack[bufferstackidx].line_number,
1241 bufferstack[bufferstackidx].char_number,
1242 bufferstack[bufferstackidx].if_depth,
1243 bufferstack[bufferstackidx].filename,
1244 bufferstack[bufferstackidx].should_pop);
1246 pp_switch_to_buffer(bufferstack[bufferstackidx].bufferstate);
1248 if(bufferstack[bufferstackidx].should_pop)
1250 if(yy_current_state() == pp_macexp)
1251 macro_add_expansion();
1253 internal_error(__FILE__, __LINE__, "Pop buffer and state without macro expansion state");
1257 return &bufferstack[bufferstackidx];
1262 *-------------------------------------------------------------------------
1263 * Macro nestng support
1264 *-------------------------------------------------------------------------
1266 static void push_macro(pp_entry_t *ppp)
1268 if(macexpstackidx >= MAXMACEXPSTACK)
1269 pperror("Too many nested macros");
1271 macexpstack[macexpstackidx] = xmalloc(sizeof(macexpstack[0][0]));
1273 macexpstack[macexpstackidx]->ppp = ppp;
1277 static macexpstackentry_t *top_macro(void)
1279 return macexpstackidx > 0 ? macexpstack[macexpstackidx-1] : NULL;
1282 static macexpstackentry_t *pop_macro(void)
1284 if(macexpstackidx <= 0)
1285 internal_error(__FILE__, __LINE__, "Macro expansion stack underflow\n");
1286 return macexpstack[--macexpstackidx];
1289 static void free_macro(macexpstackentry_t *mep)
1293 for(i = 0; i < mep->nargs; i++)
1304 static void add_text_to_macro(char *text, int len)
1306 macexpstackentry_t *mep = top_macro();
1308 assert(mep->ppp->expanding == 0);
1310 if(mep->curargalloc - mep->curargsize <= len+1) /* +1 for '\0' */
1312 mep->curargalloc += MAX(ALLOCBLOCKSIZE, len+1);
1313 mep->curarg = xrealloc(mep->curarg, mep->curargalloc * sizeof(mep->curarg[0]));
1315 memcpy(mep->curarg + mep->curargsize, text, len);
1316 mep->curargsize += len;
1317 mep->curarg[mep->curargsize] = '\0';
1320 static void macro_add_arg(int last)
1324 macexpstackentry_t *mep = top_macro();
1326 assert(mep->ppp->expanding == 0);
1328 mep->args = xrealloc(mep->args, (mep->nargs+1) * sizeof(mep->args[0]));
1329 mep->ppargs = xrealloc(mep->ppargs, (mep->nargs+1) * sizeof(mep->ppargs[0]));
1330 mep->nnls = xrealloc(mep->nnls, (mep->nargs+1) * sizeof(mep->nnls[0]));
1331 mep->args[mep->nargs] = xstrdup(mep->curarg ? mep->curarg : "");
1332 cptr = mep->args[mep->nargs]-1;
1333 while((cptr = strchr(cptr+1, '\n')))
1337 mep->nnls[mep->nargs] = nnl;
1340 mep->curargalloc = mep->curargsize = 0;
1343 if(debuglevel & DEBUGLEVEL_PPLEX)
1344 fprintf(stderr, "macro_add_arg: %s:%d: %d -> '%s'\n",
1348 mep->args[mep->nargs-1]);
1350 /* Each macro argument must be expanded to cope with stingize */
1351 if(last || mep->args[mep->nargs-1][0])
1353 yy_push_state(pp_macexp);
1354 push_buffer(NULL, NULL, NULL, last ? 2 : 1);
1355 yy_scan_string(mep->args[mep->nargs-1]);
1356 /*mep->bufferstackidx = bufferstackidx; But not nested! */
1360 static void macro_add_expansion(void)
1362 macexpstackentry_t *mep = top_macro();
1364 assert(mep->ppp->expanding == 0);
1366 mep->ppargs[mep->nargs-1] = xstrdup(mep->curarg ? mep->curarg : "");
1368 mep->curargalloc = mep->curargsize = 0;
1371 if(debuglevel & DEBUGLEVEL_PPLEX)
1372 fprintf(stderr, "macro_add_expansion: %s:%d: %d -> '%s'\n",
1376 mep->ppargs[mep->nargs-1]);
1381 *-------------------------------------------------------------------------
1383 *-------------------------------------------------------------------------
1385 static void put_buffer(char *s, int len)
1388 add_text_to_macro(s, len);
1390 fwrite(s, 1, len, ppout);
1395 *-------------------------------------------------------------------------
1396 * Include management
1397 *-------------------------------------------------------------------------
1399 void do_include(char *fname, int type)
1403 includelogicentry_t *iep;
1405 for(iep = includelogiclist; iep; iep = iep->next)
1407 if(!strcmp(iep->filename, fname))
1410 * We are done. The file was included before.
1411 * If the define was deleted, then this entry would have
1421 pperror("Empty include filename");
1423 /* Undo the effect of the quotation */
1426 if((ppin = open_include(fname+1, type, &newpath)) == NULL)
1427 pperror("Unable to open include file %s", fname+1);
1429 fname[n-1] = *fname; /* Redo the quotes */
1430 push_buffer(NULL, newpath, fname, 0);
1434 if(debuglevel & DEBUGLEVEL_PPMSG)
1435 fprintf(stderr, "do_include: %s:%d: include_state=%d, include_ppp='%s', include_ifdepth=%d\n", input_name, line_number, include_state, include_ppp, include_ifdepth);
1436 pp_switch_to_buffer(pp_create_buffer(ppin, YY_BUF_SIZE));
1438 fprintf(ppout, "# 1 \"%s\" 1%s\n", newpath, type ? "" : " 3");
1442 *-------------------------------------------------------------------------
1443 * Push/pop preprocessor ignore state when processing conditionals
1445 *-------------------------------------------------------------------------
1447 void push_ignore_state(void)
1449 yy_push_state(pp_ignore);
1452 void pop_ignore_state(void)