2 * Wrc preprocessor lexical analysis
4 * Copyright 1999-2000 Bertho A. Stultiens (BS)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 *-------------------------------------------------------------------------
21 * The preprocessor's lexographical grammar (approximately):
23 * pp := {ws} # {ws} if {ws} {expr} {ws} \n
24 * | {ws} # {ws} ifdef {ws} {id} {ws} \n
25 * | {ws} # {ws} ifndef {ws} {id} {ws} \n
26 * | {ws} # {ws} elif {ws} {expr} {ws} \n
27 * | {ws} # {ws} else {ws} \n
28 * | {ws} # {ws} endif {ws} \n
29 * | {ws} # {ws} include {ws} < {anytext} > \n
30 * | {ws} # {ws} include {ws} " {anytext} " \n
31 * | {ws} # {ws} define {ws} {anytext} \n
32 * | {ws} # {ws} define( {arglist} ) {ws} {expansion} \n
33 * | {ws} # {ws} pragma {ws} {anytext} \n
34 * | {ws} # {ws} ident {ws} {anytext} \n
35 * | {ws} # {ws} error {ws} {anytext} \n
36 * | {ws} # {ws} warning {ws} {anytext} \n
37 * | {ws} # {ws} line {ws} " {anytext} " {number} \n
38 * | {ws} # {ws} {number} " {anytext} " {number} [ {number} [{number}] ] \n
43 * expr := {expr} [+-*%^/|&] {expr}
44 * | {expr} {logor|logand} {expr}
46 * | {expr} ? {expr} : {expr}
52 * id := [a-zA-Z_][a-zA-Z0-9_]*
54 * anytext := [^\n]* (see note)
59 * | {arglist} , {id} ...
64 * | {anytext} ## {anytext}
68 * Note: "anytext" is not always "[^\n]*". This is because the
69 * trailing context must be considered as well.
71 * The only certain assumption for the preprocessor to make is that
72 * directives start at the beginning of the line, followed by a '#'
73 * and end with a newline.
74 * Any directive may be suffixed with a line-continuation. Also
75 * classical comment / *...* / (note: no comments within comments,
76 * therefore spaces) is considered to be a line-continuation
77 * (according to gcc and egcs AFAIK, ANSI is a bit vague).
78 * Comments have not been added to the above grammar for simplicity
79 * reasons. However, it is allowed to enter comment anywhere within
80 * the directives as long as they do not interfere with the context.
81 * All comments are considered to be deletable whitespace (both
82 * classical form "/ *...* /" and C++ form "//...\n").
84 * All recursive scans, except for macro-expansion, are done by the
85 * parser, whereas the simple state transitions of non-recursive
86 * directives are done in the scanner. This results in the many
87 * exclusive start-conditions of the scanner.
89 * Macro expansions are slightly more difficult because they have to
90 * prescan the arguments. Parameter substitution is literal if the
91 * substitution is # or ## (either side). This enables new identifiers
92 * to be created (see 'info cpp' node Macro|Pitfalls|Prescan for more
95 * FIXME: Variable macro parameters is recognized, but not yet
96 * expanded. I have to reread the ANSI standard on the subject (yes,
99 * The following special defines are supported:
100 * __FILE__ -> "thissource.c"
102 * __DATE__ -> "May 1 2000"
103 * __TIME__ -> "23:59:59"
104 * These macros expand, as expected, into their ANSI defined values.
106 * The same include prevention is implemented as gcc and egcs does.
107 * This results in faster processing because we do not read the text
108 * at all. Some wine-sources attempt to include the same file 4 or 5
109 * times. This strategy also saves a lot blank output-lines, which in
110 * its turn improves the real resource scanner/parser.
119 * Special flex options and exclusive scanner start-conditions
122 %option 8bit never-interactive
123 %option noinput nounput
124 %option prefix="ppy_"
150 cident [a-zA-Z_][0-9a-zA-Z_]*
151 ul [uUlL]|[uUlL][lL]|[lL][uU]|[lL][lL][uU]|[uU][lL][lL]|[lL][uU][lL]
164 # define LLONG_MAX ((__int64)0x7fffffff << 32 | 0xffffffff)
165 # define LLONG_MIN (-LLONG_MAX - 1)
168 # define ULLONG_MAX ((__int64)0xffffffff << 32 | 0xffffffff)
171 #define YY_NO_UNISTD_H
173 #include "../tools.h"
175 #include "wpp_private.h"
179 * Make sure that we are running an appropriate version of flex.
181 #if !defined(YY_FLEX_MAJOR_VERSION) || (1000 * YY_FLEX_MAJOR_VERSION + YY_FLEX_MINOR_VERSION < 2005)
182 #error Must use flex version 2.5.1 or higher (yy_scan_* routines are required).
185 #define YY_READ_BUF_SIZE 65536 /* So we read most of a file at once */
187 #define yy_current_state() YY_START
188 #define yy_pp_state(x) yy_pop_state(); yy_push_state(x)
191 * Always update the current character position within a line
193 #define YY_USER_ACTION pp_status.char_number+=ppy_leng;
196 * Buffer management for includes and expansions
198 #define MAXBUFFERSTACK 128 /* Nesting more than 128 includes or macro expansion textss is insane */
200 typedef struct bufferstackentry {
201 YY_BUFFER_STATE bufferstate; /* Buffer to switch back to */
202 FILE *file; /* File handle */
203 pp_entry_t *define; /* Points to expanding define or NULL if handling includes */
204 int line_number; /* Line that we were handling */
205 int char_number; /* The current position on that line */
206 char *filename; /* Filename that we were handling */
207 int if_depth; /* How many #if:s deep to check matching #endif:s */
208 int ncontinuations; /* Remember the continuation state */
209 int should_pop; /* Set if we must pop the start-state on EOF */
210 /* Include management */
211 include_state_t incl;
212 char *include_filename;
213 } bufferstackentry_t;
215 #define ALLOCBLOCKSIZE (1 << 10) /* Allocate these chunks at a time for string-buffers */
218 * Macro expansion nesting
219 * We need the stack to handle expansions while scanning
220 * a macro's arguments. The TOS must always be the macro
221 * that receives the current expansion from the scanner.
223 #define MAXMACEXPSTACK 128 /* Nesting more than 128 macro expansions is insane */
225 typedef struct macexpstackentry {
226 pp_entry_t *ppp; /* This macro we are scanning */
227 char **args; /* With these arguments */
228 char **ppargs; /* Resulting in these preprocessed arguments */
229 int *nnls; /* Number of newlines per argument */
230 int nargs; /* And this many arguments scanned */
231 int parentheses; /* Nesting level of () */
232 int curargsize; /* Current scanning argument's size */
233 int curargalloc; /* Current scanning argument's block allocated */
234 char *curarg; /* Current scanning argument's content */
235 } macexpstackentry_t;
237 #define MACROPARENTHESES() (top_macro()->parentheses)
242 static void newline(int);
243 static int make_number(int radix, PPY_STYPE *val, const char *str, int len);
244 static void put_buffer(const char *s, int len);
245 /* Buffer management */
246 static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop);
247 static bufferstackentry_t *pop_buffer(void);
248 /* String functions */
249 static void new_string(void);
250 static void add_string(const char *str, int len);
251 static char *get_string(void);
252 static void put_string(void);
253 static int string_start(void);
254 /* Macro functions */
255 static void push_macro(pp_entry_t *ppp);
256 static macexpstackentry_t *top_macro(void);
257 static macexpstackentry_t *pop_macro(void);
258 static void free_macro(macexpstackentry_t *mep);
259 static void add_text_to_macro(const char *text, int len);
260 static void macro_add_arg(int last);
261 static void macro_add_expansion(void);
263 static void expand_special(pp_entry_t *ppp);
264 static void expand_define(pp_entry_t *ppp);
265 static void expand_macro(macexpstackentry_t *mep);
270 static int ncontinuations;
272 static int strbuf_idx = 0;
273 static int strbuf_alloc = 0;
274 static char *strbuffer = NULL;
275 static int str_startline;
277 static macexpstackentry_t *macexpstack[MAXMACEXPSTACK];
278 static int macexpstackidx = 0;
280 static bufferstackentry_t bufferstack[MAXBUFFERSTACK];
281 static int bufferstackidx = 0;
286 include_state_t pp_incl_state =
294 static struct list pp_includelogiclist = LIST_INIT( pp_includelogiclist );
296 #define YY_INPUT(buf,result,max_size) \
298 result = fread(buf, 1, max_size, pp_status.file); \
304 **************************************************************************
305 * The scanner starts here
306 **************************************************************************
311 * Catch line-continuations.
312 * Note: Gcc keeps the line-continuations in, for example, strings
313 * intact. However, I prefer to remove them all so that the next
314 * scanner will not need to reduce the continuation state.
316 * <*>\\\n newline(0);
320 * Detect the leading # of a preprocessor directive.
322 <INITIAL,pp_ignore>^{ws}*# pp_incl_state.seen_junk++; yy_push_state(pp_pp);
325 * Scan for the preprocessor directives
327 <pp_pp>{ws}*include{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_inc); return tINCLUDE;} else {yy_pp_state(pp_eol);}
328 <pp_pp>{ws}*define{ws}* yy_pp_state(yy_current_state() != pp_ignore ? pp_def : pp_eol);
329 <pp_pp>{ws}*error{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tERROR;
330 <pp_pp>{ws}*warning{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tWARNING;
331 <pp_pp>{ws}*pragma{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPRAGMA;
332 <pp_pp>{ws}*ident{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPPIDENT;
333 <pp_pp>{ws}*undef{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_ifd); return tUNDEF;} else {yy_pp_state(pp_eol);}
334 <pp_pp>{ws}*ifdef{ws}* yy_pp_state(pp_ifd); return tIFDEF;
335 <pp_pp>{ws}*ifndef{ws}* pp_incl_state.seen_junk--; yy_pp_state(pp_ifd); return tIFNDEF;
336 <pp_pp>{ws}*if{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_if);} else {yy_pp_state(pp_ifignored);} return tIF;
337 <pp_pp>{ws}*elif{ws}* yy_pp_state(pp_if); return tELIF;
338 <pp_pp>{ws}*else{ws}* yy_pp_state(pp_endif); return tELSE;
339 <pp_pp>{ws}*endif{ws}* yy_pp_state(pp_endif); return tENDIF;
340 <pp_pp>{ws}*line{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tLINE;} else {yy_pp_state(pp_eol);}
341 <pp_pp>{ws}+ if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tGCCLINE;} else {yy_pp_state(pp_eol);}
342 <pp_pp>{ws}*[a-z]+ ppy_error("Invalid preprocessor token '%s'", ppy_text);
343 <pp_pp>\r?\n newline(1); yy_pop_state(); return tNL; /* This could be the null-token */
344 <pp_pp>\\\r?\n newline(0);
345 <pp_pp>\\\r? ppy_error("Preprocessor junk '%s'", ppy_text);
346 <pp_pp>. return *ppy_text;
349 * Handle #include and #line
351 <pp_line>[0-9]+ return make_number(10, &ppy_lval, ppy_text, ppy_leng);
352 <pp_inc>\< new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_iqs);
353 <pp_inc,pp_line>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
354 <pp_inc,pp_line>{ws}+ ;
355 <pp_inc,pp_line>\n newline(1); yy_pop_state(); return tNL;
356 <pp_inc,pp_line>\\\r?\n newline(0);
357 <pp_inc,pp_line>(\\\r?)|(.) ppy_error(yy_current_state() == pp_inc ? "Trailing junk in #include" : "Trailing junk in #line");
360 * Ignore all input when a false clause is parsed
362 <pp_ignore>[^#/\\\n]+ ;
363 <pp_ignore>\n newline(1);
364 <pp_ignore>\\\r?\n newline(0);
365 <pp_ignore>(\\\r?)|(.) ;
368 * Handle #if and #elif.
369 * These require conditionals to be evaluated, but we do not
370 * want to jam the scanner normally when we see these tokens.
371 * Note: tIDENT is handled below.
374 <pp_if>0[0-7]*{ul}? return make_number(8, &ppy_lval, ppy_text, ppy_leng);
375 <pp_if>0[0-7]*[8-9]+{ul}? ppy_error("Invalid octal digit");
376 <pp_if>[1-9][0-9]*{ul}? return make_number(10, &ppy_lval, ppy_text, ppy_leng);
377 <pp_if>0[xX][0-9a-fA-F]+{ul}? return make_number(16, &ppy_lval, ppy_text, ppy_leng);
378 <pp_if>0[xX] ppy_error("Invalid hex number");
379 <pp_if>defined yy_push_state(pp_defined); return tDEFINED;
380 <pp_if>"<<" return tLSHIFT;
381 <pp_if>">>" return tRSHIFT;
382 <pp_if>"&&" return tLOGAND;
383 <pp_if>"||" return tLOGOR;
384 <pp_if>"==" return tEQ;
385 <pp_if>"!=" return tNE;
386 <pp_if>"<=" return tLTE;
387 <pp_if>">=" return tGTE;
388 <pp_if>\n newline(1); yy_pop_state(); return tNL;
389 <pp_if>\\\r?\n newline(0);
390 <pp_if>\\\r? ppy_error("Junk in conditional expression");
392 <pp_if>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
393 <pp_if>\" ppy_error("String constants not allowed in conditionals");
394 <pp_if>. return *ppy_text;
396 <pp_ifignored>[^\n]+ ppy_lval.sint = 0; return tSINT;
397 <pp_ifignored>\n newline(1); yy_pop_state(); return tNL;
400 * Handle #ifdef, #ifndef and #undef
401 * to get only an untranslated/unexpanded identifier
403 <pp_ifd>{cident} ppy_lval.cptr = xstrdup(ppy_text); return tIDENT;
405 <pp_ifd>\n newline(1); yy_pop_state(); return tNL;
406 <pp_ifd>\\\r?\n newline(0);
407 <pp_ifd>(\\\r?)|(.) ppy_error("Identifier expected");
410 * Handle #else and #endif.
413 <pp_endif>\n newline(1); yy_pop_state(); return tNL;
414 <pp_endif>\\\r?\n newline(0);
415 <pp_endif>. ppy_error("Garbage after #else or #endif.");
418 * Handle the special 'defined' keyword.
419 * This is necessary to get the identifier prior to any
422 <pp_defined>{cident} yy_pop_state(); ppy_lval.cptr = xstrdup(ppy_text); return tIDENT;
424 <pp_defined>(\()|(\)) return *ppy_text;
425 <pp_defined>\\\r?\n newline(0);
426 <pp_defined>(\\.)|(\n)|(.) ppy_error("Identifier expected");
429 * Handle #error, #warning, #pragma and #ident.
430 * Pass everything literally to the parser, which
431 * will act appropriately.
432 * Comments are stripped from the literal text.
434 <pp_eol>[^/\\\n]+ if(yy_top_state() != pp_ignore) { ppy_lval.cptr = xstrdup(ppy_text); return tLITERAL; }
435 <pp_eol>\/[^/\\\n*]* if(yy_top_state() != pp_ignore) { ppy_lval.cptr = xstrdup(ppy_text); return tLITERAL; }
436 <pp_eol>(\\\r?)|(\/[^/*]) if(yy_top_state() != pp_ignore) { ppy_lval.cptr = xstrdup(ppy_text); return tLITERAL; }
437 <pp_eol>\n newline(1); yy_pop_state(); if(yy_current_state() != pp_ignore) { return tNL; }
438 <pp_eol>\\\r?\n newline(0);
441 * Handle left side of #define
443 <pp_def>{cident}\( ppy_lval.cptr = xstrdup(ppy_text); ppy_lval.cptr[ppy_leng-1] = '\0'; yy_pp_state(pp_macro); return tMACRO;
444 <pp_def>{cident} ppy_lval.cptr = xstrdup(ppy_text); yy_pp_state(pp_define); return tDEFINE;
446 <pp_def>\\\r?\n newline(0);
447 <pp_def>(\\\r?)|(\n)|(.) perror("Identifier expected");
450 * Scan the substitution of a define
452 <pp_define>[^'"/\\\n]+ ppy_lval.cptr = xstrdup(ppy_text); return tLITERAL;
453 <pp_define>(\\\r?)|(\/[^/*]) ppy_lval.cptr = xstrdup(ppy_text); return tLITERAL;
454 <pp_define>\\\r?\n{ws}+ newline(0); ppy_lval.cptr = xstrdup(" "); return tLITERAL;
455 <pp_define>\\\r?\n newline(0);
456 <pp_define>\n newline(1); yy_pop_state(); return tNL;
457 <pp_define>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
458 <pp_define>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
461 * Scan the definition macro arguments
463 <pp_macro>\){ws}* yy_pp_state(pp_mbody); return tMACROEND;
465 <pp_macro>{cident} ppy_lval.cptr = xstrdup(ppy_text); return tIDENT;
466 <pp_macro>, return ',';
467 <pp_macro>"..." return tELLIPSIS;
468 <pp_macro>(\\\r?)|(\n)|(.)|(\.\.?) ppy_error("Argument identifier expected");
469 <pp_macro>\\\r?\n newline(0);
472 * Scan the substitution of a macro
474 <pp_mbody>[^a-zA-Z0-9'"#/\\\n]+ ppy_lval.cptr = xstrdup(ppy_text); return tLITERAL;
475 <pp_mbody>{cident} ppy_lval.cptr = xstrdup(ppy_text); return tIDENT;
476 <pp_mbody>\#\# return tCONCAT;
477 <pp_mbody>\# return tSTRINGIZE;
478 <pp_mbody>[0-9][a-zA-Z0-9]*[^a-zA-Z0-9'"#/\\\n]* ppy_lval.cptr = xstrdup(ppy_text); return tLITERAL;
479 <pp_mbody>(\\\r?)|(\/[^/*'"#\\\n]*) ppy_lval.cptr = xstrdup(ppy_text); return tLITERAL;
480 <pp_mbody>\\\r?\n{ws}+ newline(0); ppy_lval.cptr = xstrdup(" "); return tLITERAL;
481 <pp_mbody>\\\r?\n newline(0);
482 <pp_mbody>\n newline(1); yy_pop_state(); return tNL;
483 <pp_mbody>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
484 <pp_mbody>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
487 * Macro expansion text scanning.
488 * This state is active just after the identifier is scanned
489 * that triggers an expansion. We *must* delete the leading
490 * whitespace before we can start scanning for arguments.
492 * If we do not see a '(' as next trailing token, then we have
493 * a false alarm. We just continue with a nose-bleed...
495 <pp_macign>{ws}*/\( yy_pp_state(pp_macscan);
497 if(yy_top_state() != pp_macscan)
500 <pp_macign>{ws}*\\\r?\n newline(0);
501 <pp_macign>{ws}+|{ws}*\\\r?|. {
502 macexpstackentry_t *mac = pop_macro();
504 put_buffer(mac->ppp->ident, strlen(mac->ppp->ident));
505 put_buffer(ppy_text, ppy_leng);
510 * Macro expansion argument text scanning.
511 * This state is active when a macro's arguments are being read for expansion.
514 if(++MACROPARENTHESES() > 1)
515 add_text_to_macro(ppy_text, ppy_leng);
518 if(--MACROPARENTHESES() == 0)
524 add_text_to_macro(ppy_text, ppy_leng);
527 if(MACROPARENTHESES() > 1)
528 add_text_to_macro(ppy_text, ppy_leng);
532 <pp_macscan>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
533 <pp_macscan>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
534 <pp_macscan>"/*" yy_push_state(pp_comment); add_text_to_macro(" ", 1);
535 <pp_macscan>\n pp_status.line_number++; pp_status.char_number = 1; add_text_to_macro(ppy_text, ppy_leng);
536 <pp_macscan>([^/(),\\\n"']+)|(\/[^/*(),\\\n'"]*)|(\\\r?)|(.) add_text_to_macro(ppy_text, ppy_leng);
537 <pp_macscan>\\\r?\n newline(0);
540 * Comment handling (almost all start-conditions)
542 <INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_endif,pp_defined,pp_def,pp_define,pp_macro,pp_mbody,RCINCL>"/*" yy_push_state(pp_comment);
543 <pp_comment>[^*\n]*|"*"+[^*/\n]* ;
544 <pp_comment>\n newline(0);
545 <pp_comment>"*"+"/" yy_pop_state();
548 * Remove C++ style comment (almost all start-conditions)
550 <INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_endif,pp_defined,pp_def,pp_define,pp_macro,pp_mbody,pp_macscan,RCINCL>"//"[^\n]* {
551 if(ppy_text[ppy_leng-1] == '\\')
552 ppy_warning("C++ style comment ends with an escaped newline (escape ignored)");
556 * Single, double and <> quoted constants
558 <INITIAL,pp_macexp>\" pp_incl_state.seen_junk++; new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
559 <INITIAL,pp_macexp>\' pp_incl_state.seen_junk++; new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
560 <pp_dqs>[^"\\\n]+ add_string(ppy_text, ppy_leng);
562 add_string(ppy_text, ppy_leng);
564 switch(yy_current_state())
571 if (yy_current_state()==RCINCL) yy_pop_state();
572 ppy_lval.cptr = get_string();
575 ppy_lval.cptr = get_string();
581 <pp_sqs>[^'\\\n]+ add_string(ppy_text, ppy_leng);
583 add_string(ppy_text, ppy_leng);
585 switch(yy_current_state())
590 ppy_lval.cptr = get_string();
596 <pp_iqs>[^\>\\\n]+ add_string(ppy_text, ppy_leng);
598 add_string(ppy_text, ppy_leng);
600 ppy_lval.cptr = get_string();
605 * This is tricky; we need to remove the line-continuation
606 * from preprocessor strings, but OTOH retain them in all
607 * other strings. This is because the resource grammar is
608 * even more braindead than initially analysed and line-
609 * continuations in strings introduce, sigh, newlines in
610 * the output. There goes the concept of non-breaking, non-
611 * spacing whitespace.
613 switch(yy_top_state())
623 add_string(ppy_text, ppy_leng);
627 <pp_iqs,pp_dqs,pp_sqs>\\. add_string(ppy_text, ppy_leng);
628 <pp_iqs,pp_dqs,pp_sqs>\n {
630 add_string(ppy_text, ppy_leng);
631 ppy_warning("Newline in string constant encountered (started line %d)", string_start());
635 * Identifier scanning
637 <INITIAL,pp_if,pp_inc,pp_macexp>{cident} {
639 pp_incl_state.seen_junk++;
640 if(!(ppp = pplookup(ppy_text)))
642 if(yy_current_state() == pp_inc)
643 ppy_error("Expected include filename");
645 else if(yy_current_state() == pp_if)
647 ppy_lval.cptr = xstrdup(ppy_text);
651 if((yy_current_state()==INITIAL) && (strcasecmp(ppy_text,"RCINCLUDE")==0)){
652 yy_push_state(RCINCL);
655 else put_buffer(ppy_text, ppy_leng);
658 else if(!ppp->expanding)
669 yy_push_state(pp_macign);
676 else put_buffer(ppy_text, ppy_leng);
680 * Everything else that needs to be passed and
681 * newline and continuation handling
683 <INITIAL,pp_macexp>[^a-zA-Z_#'"/\\\n \r\t\f\v]+|(\/|\\)[^a-zA-Z_/*'"\\\n \r\t\v\f]* pp_incl_state.seen_junk++; put_buffer(ppy_text, ppy_leng);
684 <INITIAL,pp_macexp>{ws}+ put_buffer(ppy_text, ppy_leng);
685 <INITIAL>\n newline(1);
686 <INITIAL>\\\r?\n newline(0);
687 <INITIAL>\\\r? pp_incl_state.seen_junk++; put_buffer(ppy_text, ppy_leng);
690 * Special catcher for macro argmument expansion to prevent
691 * newlines to propagate to the output or admin.
693 <pp_macexp>(\n)|(.)|(\\\r?(\n|.)) put_buffer(ppy_text, ppy_leng);
695 <RCINCL>[A-Za-z0-9_\.\\/]+ {
696 ppy_lval.cptr = xstrdup(ppy_text);
698 return tRCINCLUDEPATH;
704 new_string(); add_string(ppy_text,ppy_leng);yy_push_state(pp_dqs);
708 * This is a 'catch-all' rule to discover errors in the scanner
709 * in an orderly manner.
711 <*>. pp_incl_state.seen_junk++; ppy_warning("Unmatched text '%c' (0x%02x); please report\n", isprint(*ppy_text & 0xff) ? *ppy_text : ' ', *ppy_text);
714 YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
715 bufferstackentry_t *bep = pop_buffer();
717 if((!bep && pp_get_if_depth()) || (bep && pp_get_if_depth() != bep->if_depth))
718 ppy_warning("Unmatched #if/#endif at end of file");
722 if(YY_START != INITIAL)
724 ppy_error("Unexpected end of file during preprocessing");
729 else if(bep->should_pop == 2)
731 macexpstackentry_t *mac;
735 ppy__delete_buffer(b);
740 **************************************************************************
742 **************************************************************************
754 *-------------------------------------------------------------------------
755 * Output newlines or set them as continuations
757 * Input: -1 - Don't count this one, but update local position (see pp_dqs)
758 * 0 - Line-continuation seen and cache output
759 * 1 - Newline seen and flush output
760 *-------------------------------------------------------------------------
762 static void newline(int dowrite)
764 pp_status.line_number++;
765 pp_status.char_number = 1;
773 for(;ncontinuations; ncontinuations--)
780 *-------------------------------------------------------------------------
781 * Make a number out of an any-base and suffixed string
783 * Possible number extensions:
786 * - "LL" long long int
788 * - "UL" unsigned long int
789 * - "ULL" unsigned long long int
790 * - "LU" unsigned long int
791 * - "LLU" unsigned long long int
795 * The sizes of resulting 'int' and 'long' are compiler specific.
797 *-------------------------------------------------------------------------
799 static int make_number(int radix, PPY_STYPE *val, const char *str, int len)
808 ext[2] = toupper(str[len-1]);
809 ext[1] = len > 1 ? toupper(str[len-2]) : ' ';
810 ext[0] = len > 2 ? toupper(str[len-3]) : ' ';
812 if(!strcmp(ext, "LUL"))
814 ppy_error("Invalid constant suffix");
817 else if(!strcmp(ext, "LLU") || !strcmp(ext, "ULL"))
822 else if(!strcmp(ext+1, "LU") || !strcmp(ext+1, "UL"))
827 else if(!strcmp(ext+1, "LL"))
831 else if(!strcmp(ext+2, "L"))
835 else if(!strcmp(ext+2, "U"))
843 val->ull = strtoull(str, NULL, radix);
844 if (val->ull == ULLONG_MAX && errno == ERANGE)
845 ppy_error("integer constant %s is too large\n", str);
848 else if(!is_u && is_ll)
851 val->sll = strtoll(str, NULL, radix);
852 if ((val->sll == LLONG_MIN || val->sll == LLONG_MAX) && errno == ERANGE)
853 ppy_error("integer constant %s is too large\n", str);
856 else if(is_u && is_l)
859 val->ulong = strtoul(str, NULL, radix);
860 if (val->ulong == ULONG_MAX && errno == ERANGE)
861 ppy_error("integer constant %s is too large\n", str);
864 else if(!is_u && is_l)
867 val->slong = strtol(str, NULL, radix);
868 if ((val->slong == LONG_MIN || val->slong == LONG_MAX) && errno == ERANGE)
869 ppy_error("integer constant %s is too large\n", str);
872 else if(is_u && !is_l)
876 ul = strtoul(str, NULL, radix);
877 if ((ul == ULONG_MAX && errno == ERANGE) || (ul > UINT_MAX))
878 ppy_error("integer constant %s is too large\n", str);
879 val->uint = (unsigned int)ul;
883 /* Else it must be an int... */
885 l = strtol(str, NULL, radix);
886 if (((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
887 (l > INT_MAX) || (l < INT_MIN))
888 ppy_error("integer constant %s is too large\n", str);
895 *-------------------------------------------------------------------------
896 * Macro and define expansion support
898 * FIXME: Variable macro arguments.
899 *-------------------------------------------------------------------------
901 static void expand_special(pp_entry_t *ppp)
903 static char *buf = NULL;
905 assert(ppp->type == def_special);
907 if(!strcmp(ppp->ident, "__LINE__"))
909 buf = xrealloc(buf, 32);
910 sprintf(buf, "%d", pp_status.line_number);
912 else if(!strcmp(ppp->ident, "__FILE__"))
914 buf = xrealloc(buf, strlen(pp_status.input) + 3);
915 sprintf(buf, "\"%s\"", pp_status.input);
919 fprintf(stderr, "expand_special(%d): %s:%d: '%s' -> '%s'\n",
922 pp_status.line_number,
928 push_buffer(ppp, NULL, NULL, 0);
933 static void expand_define(pp_entry_t *ppp)
935 assert(ppp->type == def_define);
938 fprintf(stderr, "expand_define(%d): %s:%d: '%s' -> '%s'\n",
941 pp_status.line_number,
944 if(ppp->subst.text && ppp->subst.text[0])
946 push_buffer(ppp, NULL, NULL, 0);
947 yy_scan_string(ppp->subst.text);
951 static int curdef_idx = 0;
952 static int curdef_alloc = 0;
953 static char *curdef_text = NULL;
955 static void add_text(const char *str, int len)
959 if(curdef_idx >= curdef_alloc || curdef_alloc - curdef_idx < len)
961 curdef_alloc += (len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1);
962 curdef_text = xrealloc(curdef_text, curdef_alloc * sizeof(curdef_text[0]));
964 memcpy(&curdef_text[curdef_idx], str, len);
968 static mtext_t *add_expand_text(mtext_t *mtp, macexpstackentry_t *mep, int *nnl)
982 fprintf(stderr, "add_expand_text: exp_text: '%s'\n", mtp->subst.text);
983 add_text(mtp->subst.text, strlen(mtp->subst.text));
988 fprintf(stderr, "add_expand_text: exp_stringize(%d): '%s'\n",
990 mep->args[mtp->subst.argidx]);
991 cptr = mep->args[mtp->subst.argidx];
995 if(*cptr == '"' || *cptr == '\\')
1005 fprintf(stderr, "add_expand_text: exp_concat\n");
1006 /* Remove trailing whitespace from current expansion text */
1009 if(isspace(curdef_text[curdef_idx-1] & 0xff))
1014 /* tag current position and recursively expand the next part */
1016 mtp = add_expand_text(mtp->next, mep, nnl);
1018 /* Now get rid of the leading space of the expansion */
1019 cptr = &curdef_text[tag];
1020 n = curdef_idx - tag;
1023 if(isspace(*cptr & 0xff))
1031 if(cptr != &curdef_text[tag])
1033 memmove(&curdef_text[tag], cptr, n);
1034 curdef_idx -= (curdef_idx - tag) - n;
1039 if((mtp->next && mtp->next->type == exp_concat) || (mtp->prev && mtp->prev->type == exp_concat))
1040 exp = mep->args[mtp->subst.argidx];
1042 exp = mep->ppargs[mtp->subst.argidx];
1045 add_text(exp, strlen(exp));
1046 *nnl -= mep->nnls[mtp->subst.argidx];
1047 cptr = strchr(exp, '\n');
1051 cptr = strchr(cptr+1, '\n');
1053 mep->nnls[mtp->subst.argidx] = 0;
1056 fprintf(stderr, "add_expand_text: exp_subst(%d): '%s'\n", mtp->subst.argidx, exp);
1062 static void expand_macro(macexpstackentry_t *mep)
1068 pp_entry_t *ppp = mep->ppp;
1069 int nargs = mep->nargs;
1071 assert(ppp->type == def_macro);
1072 assert(ppp->expanding == 0);
1074 if((!ppp->variadic && nargs != ppp->nargs) || (ppp->variadic && nargs < ppp->nargs))
1076 ppy_error("Too %s macro arguments (%d)", nargs < ppp->nargs ? "few" : "many", nargs);
1080 for(n = 0; n < nargs; n++)
1081 nnl += mep->nnls[n];
1084 fprintf(stderr, "expand_macro(%d): %s:%d: '%s'(%d,%d) -> ...\n",
1087 pp_status.line_number,
1094 for(mtp = ppp->subst.mtext; mtp; mtp = mtp->next)
1096 if(!(mtp = add_expand_text(mtp, mep, &nnl)))
1100 for(n = 0; n < nnl; n++)
1103 /* To make sure there is room and termination (see below) */
1106 /* Strip trailing whitespace from expansion */
1107 for(k = curdef_idx, cptr = &curdef_text[curdef_idx-1]; k > 0; k--, cptr--)
1109 if(!isspace(*cptr & 0xff))
1114 * We must add *one* whitespace to make sure that there
1115 * is a token-separation after the expansion.
1121 /* Strip leading whitespace from expansion */
1122 for(n = 0, cptr = curdef_text; n < k; n++, cptr++)
1124 if(!isspace(*cptr & 0xff))
1131 fprintf(stderr, "expand_text: '%s'\n", curdef_text + n);
1132 push_buffer(ppp, NULL, NULL, 0);
1133 /*yy_scan_bytes(curdef_text + n, k - n);*/
1134 yy_scan_string(curdef_text + n);
1139 *-------------------------------------------------------------------------
1140 * String collection routines
1141 *-------------------------------------------------------------------------
1143 static void new_string(void)
1146 str_startline = pp_status.line_number;
1149 static void add_string(const char *str, int len)
1153 if(strbuf_idx >= strbuf_alloc || strbuf_alloc - strbuf_idx < len)
1155 strbuf_alloc += (len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1);
1156 strbuffer = xrealloc(strbuffer, strbuf_alloc * sizeof(strbuffer[0]));
1158 memcpy(&strbuffer[strbuf_idx], str, len);
1162 static char *get_string(void)
1164 char *str = xmalloc(strbuf_idx + 1);
1166 memcpy(str, strbuffer, strbuf_idx);
1167 str[strbuf_idx] = '\0';
1171 static void put_string(void)
1173 put_buffer(strbuffer, strbuf_idx);
1176 static int string_start(void)
1178 return str_startline;
1183 *-------------------------------------------------------------------------
1185 *-------------------------------------------------------------------------
1187 static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop)
1190 printf("push_buffer(%d): %p %p %p %d\n", bufferstackidx, ppp, filename, incname, pop);
1191 if(bufferstackidx >= MAXBUFFERSTACK)
1192 error("Buffer stack overflow\n");
1194 memset(&bufferstack[bufferstackidx], 0, sizeof(bufferstack[0]));
1195 bufferstack[bufferstackidx].bufferstate = YY_CURRENT_BUFFER;
1196 bufferstack[bufferstackidx].file = pp_status.file;
1197 bufferstack[bufferstackidx].define = ppp;
1198 bufferstack[bufferstackidx].line_number = pp_status.line_number;
1199 bufferstack[bufferstackidx].char_number = pp_status.char_number;
1200 bufferstack[bufferstackidx].if_depth = pp_get_if_depth();
1201 bufferstack[bufferstackidx].should_pop = pop;
1202 bufferstack[bufferstackidx].filename = pp_status.input;
1203 bufferstack[bufferstackidx].ncontinuations = ncontinuations;
1204 bufferstack[bufferstackidx].incl = pp_incl_state;
1205 bufferstack[bufferstackidx].include_filename = incname;
1211 /* These will track the ppy_error to the correct file and line */
1212 pp_status.line_number = 1;
1213 pp_status.char_number = 1;
1214 pp_status.input = filename;
1220 static bufferstackentry_t *pop_buffer(void)
1222 if(bufferstackidx == 0)
1227 if(bufferstack[bufferstackidx].define)
1228 bufferstack[bufferstackidx].define->expanding = 0;
1231 includelogicentry_t *iep = NULL;
1233 if(!bufferstack[bufferstackidx].should_pop)
1235 fclose(pp_status.file);
1236 fprintf(ppy_out, "# %d \"%s\" 2\n", bufferstack[bufferstackidx].line_number, bufferstack[bufferstackidx].filename);
1238 /* We have EOF, check the include logic */
1239 if(pp_incl_state.state == 2 && !pp_incl_state.seen_junk && pp_incl_state.ppp)
1241 pp_entry_t *ppp = pplookup(pp_incl_state.ppp);
1244 iep = xmalloc(sizeof(includelogicentry_t));
1247 iep->filename = bufferstack[bufferstackidx].include_filename;
1248 list_add_head( &pp_includelogiclist, &iep->entry );
1250 fprintf(stderr, "pop_buffer: %s:%d: includelogic added, include_ppp='%s', file='%s'\n",
1251 bufferstack[bufferstackidx].filename, bufferstack[bufferstackidx].line_number, pp_incl_state.ppp, iep->filename);
1254 free(pp_incl_state.ppp);
1255 pp_incl_state = bufferstack[bufferstackidx].incl;
1258 if (bufferstack[bufferstackidx].include_filename)
1260 free(pp_status.input);
1261 pp_status.input = bufferstack[bufferstackidx].filename;
1263 pp_status.line_number = bufferstack[bufferstackidx].line_number;
1264 pp_status.char_number = bufferstack[bufferstackidx].char_number;
1265 ncontinuations = bufferstack[bufferstackidx].ncontinuations;
1267 free(bufferstack[bufferstackidx].include_filename);
1271 printf("pop_buffer(%d): %p %p (%d, %d, %d) %p %d\n",
1273 bufferstack[bufferstackidx].bufferstate,
1274 bufferstack[bufferstackidx].define,
1275 bufferstack[bufferstackidx].line_number,
1276 bufferstack[bufferstackidx].char_number,
1277 bufferstack[bufferstackidx].if_depth,
1278 bufferstack[bufferstackidx].filename,
1279 bufferstack[bufferstackidx].should_pop);
1281 pp_status.file = bufferstack[bufferstackidx].file;
1282 ppy__switch_to_buffer(bufferstack[bufferstackidx].bufferstate);
1284 if(bufferstack[bufferstackidx].should_pop)
1286 assert( yy_current_state() == pp_macexp );
1287 macro_add_expansion();
1291 return &bufferstack[bufferstackidx];
1296 *-------------------------------------------------------------------------
1297 * Macro nestng support
1298 *-------------------------------------------------------------------------
1300 static void push_macro(pp_entry_t *ppp)
1302 if(macexpstackidx >= MAXMACEXPSTACK)
1304 ppy_error("Too many nested macros");
1308 macexpstack[macexpstackidx] = xmalloc(sizeof(macexpstack[0][0]));
1309 memset( macexpstack[macexpstackidx], 0, sizeof(macexpstack[0][0]));
1310 macexpstack[macexpstackidx]->ppp = ppp;
1314 static macexpstackentry_t *top_macro(void)
1316 return macexpstackidx > 0 ? macexpstack[macexpstackidx-1] : NULL;
1319 static macexpstackentry_t *pop_macro(void)
1321 assert(macexpstackidx > 0);
1322 return macexpstack[--macexpstackidx];
1325 static void free_macro(macexpstackentry_t *mep)
1329 for(i = 0; i < mep->nargs; i++)
1337 static void add_text_to_macro(const char *text, int len)
1339 macexpstackentry_t *mep = top_macro();
1341 assert(mep->ppp->expanding == 0);
1343 if(mep->curargalloc - mep->curargsize <= len+1) /* +1 for '\0' */
1345 mep->curargalloc += (ALLOCBLOCKSIZE > len+1) ? ALLOCBLOCKSIZE : len+1;
1346 mep->curarg = xrealloc(mep->curarg, mep->curargalloc * sizeof(mep->curarg[0]));
1348 memcpy(mep->curarg + mep->curargsize, text, len);
1349 mep->curargsize += len;
1350 mep->curarg[mep->curargsize] = '\0';
1353 static void macro_add_arg(int last)
1357 macexpstackentry_t *mep = top_macro();
1359 assert(mep->ppp->expanding == 0);
1361 mep->args = xrealloc(mep->args, (mep->nargs+1) * sizeof(mep->args[0]));
1362 mep->ppargs = xrealloc(mep->ppargs, (mep->nargs+1) * sizeof(mep->ppargs[0]));
1363 mep->nnls = xrealloc(mep->nnls, (mep->nargs+1) * sizeof(mep->nnls[0]));
1365 mep->args[mep->nargs] = xstrdup(mep->curarg ? mep->curarg : "");
1366 cptr = mep->args[mep->nargs]-1;
1367 while((cptr = strchr(cptr+1, '\n')))
1371 mep->nnls[mep->nargs] = nnl;
1374 mep->curargalloc = mep->curargsize = 0;
1378 fprintf(stderr, "macro_add_arg: %s:%d: %d -> '%s'\n",
1380 pp_status.line_number,
1382 mep->args[mep->nargs-1]);
1384 /* Each macro argument must be expanded to cope with stingize */
1385 if(last || mep->args[mep->nargs-1][0])
1387 yy_push_state(pp_macexp);
1388 push_buffer(NULL, NULL, NULL, last ? 2 : 1);
1389 yy_scan_string(mep->args[mep->nargs-1]);
1390 /*mep->bufferstackidx = bufferstackidx; But not nested! */
1394 static void macro_add_expansion(void)
1396 macexpstackentry_t *mep = top_macro();
1398 assert(mep->ppp->expanding == 0);
1400 mep->ppargs[mep->nargs-1] = xstrdup(mep->curarg ? mep->curarg : "");
1402 mep->curargalloc = mep->curargsize = 0;
1406 fprintf(stderr, "macro_add_expansion: %s:%d: %d -> '%s'\n",
1408 pp_status.line_number,
1410 mep->ppargs[mep->nargs-1] ? mep->ppargs[mep->nargs-1] : "");
1415 *-------------------------------------------------------------------------
1417 *-------------------------------------------------------------------------
1419 static void put_buffer(const char *s, int len)
1422 add_text_to_macro(s, len);
1424 fwrite(s, 1, len, ppy_out);
1429 *-------------------------------------------------------------------------
1430 * Include management
1431 *-------------------------------------------------------------------------
1433 void pp_do_include(char *fname, int type)
1437 includelogicentry_t *iep;
1443 LIST_FOR_EACH_ENTRY( iep, &pp_includelogiclist, includelogicentry_t, entry )
1445 if(!strcmp(iep->filename, fname))
1448 * We are done. The file was included before.
1449 * If the define was deleted, then this entry would have
1461 ppy_error("Empty include filename");
1466 /* Undo the effect of the quotation */
1469 if((fp = pp_open_include(fname+1, type, pp_status.input, &newpath)) == NULL)
1471 ppy_error("Unable to open include file %s", fname+1);
1476 fname[n-1] = *fname; /* Redo the quotes */
1477 push_buffer(NULL, newpath, fname, 0);
1478 pp_incl_state.seen_junk = 0;
1479 pp_incl_state.state = 0;
1480 pp_incl_state.ppp = NULL;
1483 fprintf(stderr, "pp_do_include: %s:%d: include_state=%d, include_ifdepth=%d\n",
1484 pp_status.input, pp_status.line_number, pp_incl_state.state, pp_incl_state.ifdepth);
1485 pp_status.file = fp;
1486 ppy__switch_to_buffer(ppy__create_buffer(NULL, YY_BUF_SIZE));
1488 fprintf(ppy_out, "# 1 \"%s\" 1%s\n", newpath, type ? "" : " 3");
1492 *-------------------------------------------------------------------------
1493 * Push/pop preprocessor ignore state when processing conditionals
1495 *-------------------------------------------------------------------------
1497 void pp_push_ignore_state(void)
1499 yy_push_state(pp_ignore);
1502 void pp_pop_ignore_state(void)