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.
115 * Special flex options and exclusive scanner start-conditions
118 %option 8bit never-interactive
119 %option noinput nounput
120 %option prefix="ppy_"
146 cident [a-zA-Z_][0-9a-zA-Z_]*
147 ul [uUlL]|[uUlL][lL]|[lL][uU]|[lL][lL][uU]|[uU][lL][lL]|[lL][uU][lL]
158 #define YY_NO_UNISTD_H
160 #include "wpp_private.h"
164 * Make sure that we are running an appropriate version of flex.
166 #if !defined(YY_FLEX_MAJOR_VERSION) || (1000 * YY_FLEX_MAJOR_VERSION + YY_FLEX_MINOR_VERSION < 2005)
167 #error Must use flex version 2.5.1 or higher (yy_scan_* routines are required).
170 #define YY_READ_BUF_SIZE 65536 /* So we read most of a file at once */
172 #define yy_current_state() YY_START
173 #define yy_pp_state(x) yy_pop_state(); yy_push_state(x)
176 * Always update the current character position within a line
178 #define YY_USER_ACTION pp_status.char_number+=ppy_leng;
181 * Buffer management for includes and expansions
183 #define MAXBUFFERSTACK 128 /* Nesting more than 128 includes or macro expansion textss is insane */
185 typedef struct bufferstackentry {
186 YY_BUFFER_STATE bufferstate; /* Buffer to switch back to */
187 void *filehandle; /* Handle to be used with wpp_callbacks->read */
188 pp_entry_t *define; /* Points to expanding define or NULL if handling includes */
189 int line_number; /* Line that we were handling */
190 int char_number; /* The current position on that line */
191 char *filename; /* Filename that we were handling */
192 int if_depth; /* How many #if:s deep to check matching #endif:s */
193 int ncontinuations; /* Remember the continuation state */
194 int should_pop; /* Set if we must pop the start-state on EOF */
195 /* Include management */
196 include_state_t incl;
197 char *include_filename;
198 } bufferstackentry_t;
200 #define ALLOCBLOCKSIZE (1 << 10) /* Allocate these chunks at a time for string-buffers */
203 * Macro expansion nesting
204 * We need the stack to handle expansions while scanning
205 * a macro's arguments. The TOS must always be the macro
206 * that receives the current expansion from the scanner.
208 #define MAXMACEXPSTACK 128 /* Nesting more than 128 macro expansions is insane */
210 typedef struct macexpstackentry {
211 pp_entry_t *ppp; /* This macro we are scanning */
212 char **args; /* With these arguments */
213 char **ppargs; /* Resulting in these preprocessed arguments */
214 int *nnls; /* Number of newlines per argument */
215 int nargs; /* And this many arguments scanned */
216 int parentheses; /* Nesting level of () */
217 int curargsize; /* Current scanning argument's size */
218 int curargalloc; /* Current scanning argument's block allocated */
219 char *curarg; /* Current scanning argument's content */
220 } macexpstackentry_t;
222 #define MACROPARENTHESES() (top_macro()->parentheses)
227 static void newline(int);
228 static int make_number(int radix, YYSTYPE *val, const char *str, int len);
229 static void put_buffer(const char *s, int len);
230 /* Buffer management */
231 static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop);
232 static bufferstackentry_t *pop_buffer(void);
233 /* String functions */
234 static void new_string(void);
235 static void add_string(const char *str, int len);
236 static char *get_string(void);
237 static void put_string(void);
238 static int string_start(void);
239 /* Macro functions */
240 static void push_macro(pp_entry_t *ppp);
241 static macexpstackentry_t *top_macro(void);
242 static macexpstackentry_t *pop_macro(void);
243 static void free_macro(macexpstackentry_t *mep);
244 static void add_text_to_macro(const char *text, int len);
245 static void macro_add_arg(int last);
246 static void macro_add_expansion(void);
248 static void expand_special(pp_entry_t *ppp);
249 static void expand_define(pp_entry_t *ppp);
250 static void expand_macro(macexpstackentry_t *mep);
255 static int ncontinuations;
257 static int strbuf_idx = 0;
258 static int strbuf_alloc = 0;
259 static char *strbuffer = NULL;
260 static int str_startline;
262 static macexpstackentry_t *macexpstack[MAXMACEXPSTACK];
263 static int macexpstackidx = 0;
265 static bufferstackentry_t bufferstack[MAXBUFFERSTACK];
266 static int bufferstackidx = 0;
271 include_state_t pp_incl_state =
279 includelogicentry_t *pp_includelogiclist = NULL;
281 #define YY_INPUT(buf,result,max_size) \
283 result = wpp_read(pp_status.file, buf, max_size); \
286 #define BUFFERINITIALCAPACITY 256
288 void WINAPIV pp_writestring(const char *format, ...)
293 static int buffercapacity;
296 if(buffercapacity == 0)
298 buffer = pp_xmalloc(BUFFERINITIALCAPACITY);
301 buffercapacity = BUFFERINITIALCAPACITY;
304 __ms_va_start(valist, format);
305 len = vsnprintf(buffer, buffercapacity,
308 /* If the string is longer than buffersize, vsnprintf returns
309 * the string length with glibc >= 2.1, -1 with glibc < 2.1 */
310 while(len > buffercapacity || len < 0)
315 } while(len > buffercapacity);
317 new_buffer = pp_xrealloc(buffer, buffercapacity);
318 if(new_buffer == NULL)
321 __ms_va_start(valist, format);
322 len = vsnprintf(buffer, buffercapacity,
327 wpp_write(buffer, len);
333 **************************************************************************
334 * The scanner starts here
335 **************************************************************************
340 * Catch line-continuations.
341 * Note: Gcc keeps the line-continuations in, for example, strings
342 * intact. However, I prefer to remove them all so that the next
343 * scanner will not need to reduce the continuation state.
345 * <*>\\\n newline(0);
349 * Detect the leading # of a preprocessor directive.
351 <INITIAL,pp_ignore>^{ws}*# pp_incl_state.seen_junk++; yy_push_state(pp_pp);
354 * Scan for the preprocessor directives
356 <pp_pp>{ws}*include{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_inc); return tINCLUDE;} else {yy_pp_state(pp_eol);}
357 <pp_pp>{ws}*define{ws}* yy_pp_state(yy_current_state() != pp_ignore ? pp_def : pp_eol);
358 <pp_pp>{ws}*error{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tERROR;
359 <pp_pp>{ws}*warning{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tWARNING;
360 <pp_pp>{ws}*pragma{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPRAGMA;
361 <pp_pp>{ws}*ident{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPPIDENT;
362 <pp_pp>{ws}*undef{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_ifd); return tUNDEF;} else {yy_pp_state(pp_eol);}
363 <pp_pp>{ws}*ifdef{ws}* yy_pp_state(pp_ifd); return tIFDEF;
364 <pp_pp>{ws}*ifndef{ws}* pp_incl_state.seen_junk--; yy_pp_state(pp_ifd); return tIFNDEF;
365 <pp_pp>{ws}*if{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_if);} else {yy_pp_state(pp_ifignored);} return tIF;
366 <pp_pp>{ws}*elif{ws}* yy_pp_state(pp_if); return tELIF;
367 <pp_pp>{ws}*else{ws}* yy_pp_state(pp_endif); return tELSE;
368 <pp_pp>{ws}*endif{ws}* yy_pp_state(pp_endif); return tENDIF;
369 <pp_pp>{ws}*line{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tLINE;} else {yy_pp_state(pp_eol);}
370 <pp_pp>{ws}+ if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tGCCLINE;} else {yy_pp_state(pp_eol);}
371 <pp_pp>{ws}*[a-z]+ ppy_error("Invalid preprocessor token '%s'", ppy_text);
372 <pp_pp>\r?\n newline(1); yy_pop_state(); return tNL; /* This could be the null-token */
373 <pp_pp>\\\r?\n newline(0);
374 <pp_pp>\\\r? ppy_error("Preprocessor junk '%s'", ppy_text);
375 <pp_pp>. return *ppy_text;
378 * Handle #include and #line
380 <pp_line>[0-9]+ return make_number(10, &ppy_lval, ppy_text, ppy_leng);
381 <pp_inc>\< new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_iqs);
382 <pp_inc,pp_line>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
383 <pp_inc,pp_line>{ws}+ ;
384 <pp_inc,pp_line>\n newline(1); yy_pop_state(); return tNL;
385 <pp_inc,pp_line>\\\r?\n newline(0);
386 <pp_inc,pp_line>(\\\r?)|(.) ppy_error(yy_current_state() == pp_inc ? "Trailing junk in #include" : "Trailing junk in #line");
389 * Ignore all input when a false clause is parsed
391 <pp_ignore>[^#/\\\n]+ ;
392 <pp_ignore>\n newline(1);
393 <pp_ignore>\\\r?\n newline(0);
394 <pp_ignore>(\\\r?)|(.) ;
397 * Handle #if and #elif.
398 * These require conditionals to be evaluated, but we do not
399 * want to jam the scanner normally when we see these tokens.
400 * Note: tIDENT is handled below.
403 <pp_if>0[0-7]*{ul}? return make_number(8, &ppy_lval, ppy_text, ppy_leng);
404 <pp_if>0[0-7]*[8-9]+{ul}? ppy_error("Invalid octal digit");
405 <pp_if>[1-9][0-9]*{ul}? return make_number(10, &ppy_lval, ppy_text, ppy_leng);
406 <pp_if>0[xX][0-9a-fA-F]+{ul}? return make_number(16, &ppy_lval, ppy_text, ppy_leng);
407 <pp_if>0[xX] ppy_error("Invalid hex number");
408 <pp_if>defined yy_push_state(pp_defined); return tDEFINED;
409 <pp_if>"<<" return tLSHIFT;
410 <pp_if>">>" return tRSHIFT;
411 <pp_if>"&&" return tLOGAND;
412 <pp_if>"||" return tLOGOR;
413 <pp_if>"==" return tEQ;
414 <pp_if>"!=" return tNE;
415 <pp_if>"<=" return tLTE;
416 <pp_if>">=" return tGTE;
417 <pp_if>\n newline(1); yy_pop_state(); return tNL;
418 <pp_if>\\\r?\n newline(0);
419 <pp_if>\\\r? ppy_error("Junk in conditional expression");
421 <pp_if>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
422 <pp_if>\" ppy_error("String constants not allowed in conditionals");
423 <pp_if>. return *ppy_text;
425 <pp_ifignored>[^\n]+ ppy_lval.sint = 0; return tSINT;
426 <pp_ifignored>\n newline(1); yy_pop_state(); return tNL;
429 * Handle #ifdef, #ifndef and #undef
430 * to get only an untranslated/unexpanded identifier
432 <pp_ifd>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
434 <pp_ifd>\n newline(1); yy_pop_state(); return tNL;
435 <pp_ifd>\\\r?\n newline(0);
436 <pp_ifd>(\\\r?)|(.) ppy_error("Identifier expected");
439 * Handle #else and #endif.
442 <pp_endif>\n newline(1); yy_pop_state(); return tNL;
443 <pp_endif>\\\r?\n newline(0);
444 <pp_endif>. ppy_error("Garbage after #else or #endif.");
447 * Handle the special 'defined' keyword.
448 * This is necessary to get the identifier prior to any
451 <pp_defined>{cident} yy_pop_state(); ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
453 <pp_defined>(\()|(\)) return *ppy_text;
454 <pp_defined>\\\r?\n newline(0);
455 <pp_defined>(\\.)|(\n)|(.) ppy_error("Identifier expected");
458 * Handle #error, #warning, #pragma and #ident.
459 * Pass everything literally to the parser, which
460 * will act appropriately.
461 * Comments are stripped from the literal text.
463 <pp_eol>[^/\\\n]+ if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
464 <pp_eol>\/[^/\\\n*]* if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
465 <pp_eol>(\\\r?)|(\/[^/*]) if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
466 <pp_eol>\n newline(1); yy_pop_state(); if(yy_current_state() != pp_ignore) { return tNL; }
467 <pp_eol>\\\r?\n newline(0);
470 * Handle left side of #define
472 <pp_def>{cident}\( ppy_lval.cptr = pp_xstrdup(ppy_text); if(ppy_lval.cptr) ppy_lval.cptr[ppy_leng-1] = '\0'; yy_pp_state(pp_macro); return tMACRO;
473 <pp_def>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); yy_pp_state(pp_define); return tDEFINE;
475 <pp_def>\\\r?\n newline(0);
476 <pp_def>(\\\r?)|(\n)|(.) perror("Identifier expected");
479 * Scan the substitution of a define
481 <pp_define>[^'"/\\\n]+ ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
482 <pp_define>(\\\r?)|(\/[^/*]) ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
483 <pp_define>\\\r?\n{ws}+ newline(0); ppy_lval.cptr = pp_xstrdup(" "); return tLITERAL;
484 <pp_define>\\\r?\n newline(0);
485 <pp_define>\n newline(1); yy_pop_state(); return tNL;
486 <pp_define>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
487 <pp_define>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
490 * Scan the definition macro arguments
492 <pp_macro>\){ws}* yy_pp_state(pp_mbody); return tMACROEND;
494 <pp_macro>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
495 <pp_macro>, return ',';
496 <pp_macro>"..." return tELIPSIS;
497 <pp_macro>(\\\r?)|(\n)|(.)|(\.\.?) ppy_error("Argument identifier expected");
498 <pp_macro>\\\r?\n newline(0);
501 * Scan the substitution of a macro
503 <pp_mbody>[^a-zA-Z0-9'"#/\\\n]+ ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
504 <pp_mbody>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
505 <pp_mbody>\#\# return tCONCAT;
506 <pp_mbody>\# return tSTRINGIZE;
507 <pp_mbody>[0-9][a-zA-Z0-9]*[^a-zA-Z0-9'"#/\\\n]* ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
508 <pp_mbody>(\\\r?)|(\/[^/*'"#\\\n]*) ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
509 <pp_mbody>\\\r?\n{ws}+ newline(0); ppy_lval.cptr = pp_xstrdup(" "); return tLITERAL;
510 <pp_mbody>\\\r?\n newline(0);
511 <pp_mbody>\n newline(1); yy_pop_state(); return tNL;
512 <pp_mbody>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
513 <pp_mbody>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
516 * Macro expansion text scanning.
517 * This state is active just after the identifier is scanned
518 * that triggers an expansion. We *must* delete the leading
519 * whitespace before we can start scanning for arguments.
521 * If we do not see a '(' as next trailing token, then we have
522 * a false alarm. We just continue with a nose-bleed...
524 <pp_macign>{ws}*/\( yy_pp_state(pp_macscan);
526 if(yy_top_state() != pp_macscan)
529 <pp_macign>{ws}*\\\r?\n newline(0);
530 <pp_macign>{ws}+|{ws}*\\\r?|. {
531 macexpstackentry_t *mac = pop_macro();
533 put_buffer(mac->ppp->ident, strlen(mac->ppp->ident));
534 put_buffer(ppy_text, ppy_leng);
539 * Macro expansion argument text scanning.
540 * This state is active when a macro's arguments are being read for expansion.
543 if(++MACROPARENTHESES() > 1)
544 add_text_to_macro(ppy_text, ppy_leng);
547 if(--MACROPARENTHESES() == 0)
553 add_text_to_macro(ppy_text, ppy_leng);
556 if(MACROPARENTHESES() > 1)
557 add_text_to_macro(ppy_text, ppy_leng);
561 <pp_macscan>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
562 <pp_macscan>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
563 <pp_macscan>"/*" yy_push_state(pp_comment); add_text_to_macro(" ", 1);
564 <pp_macscan>\n pp_status.line_number++; pp_status.char_number = 1; add_text_to_macro(ppy_text, ppy_leng);
565 <pp_macscan>([^/(),\\\n"']+)|(\/[^/*(),\\\n'"]*)|(\\\r?)|(.) add_text_to_macro(ppy_text, ppy_leng);
566 <pp_macscan>\\\r?\n newline(0);
569 * Comment handling (almost all start-conditions)
571 <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);
572 <pp_comment>[^*\n]*|"*"+[^*/\n]* ;
573 <pp_comment>\n newline(0);
574 <pp_comment>"*"+"/" yy_pop_state();
577 * Remove C++ style comment (almost all start-conditions)
579 <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]* {
580 if(ppy_text[ppy_leng-1] == '\\')
581 ppy_warning("C++ style comment ends with an escaped newline (escape ignored)");
585 * Single, double and <> quoted constants
587 <INITIAL,pp_macexp>\" pp_incl_state.seen_junk++; new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
588 <INITIAL,pp_macexp>\' pp_incl_state.seen_junk++; new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
589 <pp_dqs>[^"\\\n]+ add_string(ppy_text, ppy_leng);
591 add_string(ppy_text, ppy_leng);
593 switch(yy_current_state())
600 if (yy_current_state()==RCINCL) yy_pop_state();
601 ppy_lval.cptr = get_string();
604 ppy_lval.cptr = get_string();
610 <pp_sqs>[^'\\\n]+ add_string(ppy_text, ppy_leng);
612 add_string(ppy_text, ppy_leng);
614 switch(yy_current_state())
619 ppy_lval.cptr = get_string();
625 <pp_iqs>[^\>\\\n]+ add_string(ppy_text, ppy_leng);
627 add_string(ppy_text, ppy_leng);
629 ppy_lval.cptr = get_string();
634 * This is tricky; we need to remove the line-continuation
635 * from preprocessor strings, but OTOH retain them in all
636 * other strings. This is because the resource grammar is
637 * even more braindead than initially analysed and line-
638 * continuations in strings introduce, sigh, newlines in
639 * the output. There goes the concept of non-breaking, non-
640 * spacing whitespace.
642 switch(yy_top_state())
652 add_string(ppy_text, ppy_leng);
656 <pp_iqs,pp_dqs,pp_sqs>\\. add_string(ppy_text, ppy_leng);
657 <pp_iqs,pp_dqs,pp_sqs>\n {
659 add_string(ppy_text, ppy_leng);
660 ppy_warning("Newline in string constant encountered (started line %d)", string_start());
664 * Identifier scanning
666 <INITIAL,pp_if,pp_inc,pp_macexp>{cident} {
668 pp_incl_state.seen_junk++;
669 if(!(ppp = pplookup(ppy_text)))
671 if(yy_current_state() == pp_inc)
672 ppy_error("Expected include filename");
674 else if(yy_current_state() == pp_if)
676 ppy_lval.cptr = pp_xstrdup(ppy_text);
680 if((yy_current_state()==INITIAL) && (strcasecmp(ppy_text,"RCINCLUDE")==0)){
681 yy_push_state(RCINCL);
684 else put_buffer(ppy_text, ppy_leng);
687 else if(!ppp->expanding)
698 yy_push_state(pp_macign);
702 pp_internal_error(__FILE__, __LINE__, "Invalid define type %d\n", ppp->type);
705 else put_buffer(ppy_text, ppy_leng);
709 * Everything else that needs to be passed and
710 * newline and continuation handling
712 <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);
713 <INITIAL,pp_macexp>{ws}+ put_buffer(ppy_text, ppy_leng);
714 <INITIAL>\n newline(1);
715 <INITIAL>\\\r?\n newline(0);
716 <INITIAL>\\\r? pp_incl_state.seen_junk++; put_buffer(ppy_text, ppy_leng);
719 * Special catcher for macro argmument expansion to prevent
720 * newlines to propagate to the output or admin.
722 <pp_macexp>(\n)|(.)|(\\\r?(\n|.)) put_buffer(ppy_text, ppy_leng);
724 <RCINCL>[A-Za-z0-9_\.\\/]+ {
725 ppy_lval.cptr=pp_xstrdup(ppy_text);
727 return tRCINCLUDEPATH;
733 new_string(); add_string(ppy_text,ppy_leng);yy_push_state(pp_dqs);
737 * This is a 'catch-all' rule to discover errors in the scanner
738 * in an orderly manner.
740 <*>. pp_incl_state.seen_junk++; ppy_warning("Unmatched text '%c' (0x%02x); please report\n", isprint(*ppy_text & 0xff) ? *ppy_text : ' ', *ppy_text);
743 YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
744 bufferstackentry_t *bep = pop_buffer();
746 if((!bep && pp_get_if_depth()) || (bep && pp_get_if_depth() != bep->if_depth))
747 ppy_warning("Unmatched #if/#endif at end of file");
751 if(YY_START != INITIAL)
753 ppy_error("Unexpected end of file during preprocessing");
758 else if(bep->should_pop == 2)
760 macexpstackentry_t *mac;
764 ppy__delete_buffer(b);
769 **************************************************************************
771 **************************************************************************
783 *-------------------------------------------------------------------------
784 * Output newlines or set them as continuations
786 * Input: -1 - Don't count this one, but update local position (see pp_dqs)
787 * 0 - Line-continuation seen and cache output
788 * 1 - Newline seen and flush output
789 *-------------------------------------------------------------------------
791 static void newline(int dowrite)
793 pp_status.line_number++;
794 pp_status.char_number = 1;
802 for(;ncontinuations; ncontinuations--)
809 *-------------------------------------------------------------------------
810 * Make a number out of an any-base and suffixed string
812 * Possible number extensions:
815 * - "LL" long long int
817 * - "UL" unsigned long int
818 * - "ULL" unsigned long long int
819 * - "LU" unsigned long int
820 * - "LLU" unsigned long long int
824 * The sizes of resulting 'int' and 'long' are compiler specific.
825 * I depend on sizeof(int) > 2 here (although a relatively safe
827 * Long longs are not yet implemented because this is very compiler
828 * specific and I don't want to think too much about the problems.
830 *-------------------------------------------------------------------------
832 static int make_number(int radix, YYSTYPE *val, const char *str, int len)
841 ext[2] = toupper(str[len-1]);
842 ext[1] = len > 1 ? toupper(str[len-2]) : ' ';
843 ext[0] = len > 2 ? toupper(str[len-3]) : ' ';
845 if(!strcmp(ext, "LUL"))
847 ppy_error("Invalid constant suffix");
850 else if(!strcmp(ext, "LLU") || !strcmp(ext, "ULL"))
855 else if(!strcmp(ext+1, "LU") || !strcmp(ext+1, "UL"))
860 else if(!strcmp(ext+1, "LL"))
864 else if(!strcmp(ext+2, "L"))
868 else if(!strcmp(ext+2, "U"))
876 val->ull = strtoull(str, NULL, radix);
877 if (val->ull == ULLONG_MAX && errno == ERANGE)
878 ppy_error("integer constant %s is too large\n", str);
881 else if(!is_u && is_ll)
884 val->sll = strtoll(str, NULL, radix);
885 if ((val->sll == LLONG_MIN || val->sll == LLONG_MAX) && errno == ERANGE)
886 ppy_error("integer constant %s is too large\n", str);
889 else if(is_u && is_l)
892 val->ulong = strtoul(str, NULL, radix);
893 if (val->ulong == ULONG_MAX && errno == ERANGE)
894 ppy_error("integer constant %s is too large\n", str);
897 else if(!is_u && is_l)
900 val->slong = strtol(str, NULL, radix);
901 if ((val->slong == LONG_MIN || val->slong == LONG_MAX) && errno == ERANGE)
902 ppy_error("integer constant %s is too large\n", str);
905 else if(is_u && !is_l)
909 ul = strtoul(str, NULL, radix);
910 if ((ul == ULONG_MAX && errno == ERANGE) || (ul > UINT_MAX))
911 ppy_error("integer constant %s is too large\n", str);
912 val->uint = (unsigned int)ul;
916 /* Else it must be an int... */
918 l = strtol(str, NULL, radix);
919 if (((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
920 (l > INT_MAX) || (l < INT_MIN))
921 ppy_error("integer constant %s is too large\n", str);
928 *-------------------------------------------------------------------------
929 * Macro and define expansion support
931 * FIXME: Variable macro arguments.
932 *-------------------------------------------------------------------------
934 static void expand_special(pp_entry_t *ppp)
936 static char *buf = NULL;
939 assert(ppp->type == def_special);
941 if(!strcmp(ppp->ident, "__LINE__"))
943 new_buf = pp_xrealloc(buf, 32);
947 sprintf(buf, "%d", pp_status.line_number);
949 else if(!strcmp(ppp->ident, "__FILE__"))
951 new_buf = pp_xrealloc(buf, strlen(pp_status.input) + 3);
955 sprintf(buf, "\"%s\"", pp_status.input);
958 pp_internal_error(__FILE__, __LINE__, "Special macro '%s' not found...\n", ppp->ident);
962 push_buffer(ppp, NULL, NULL, 0);
967 static void expand_define(pp_entry_t *ppp)
969 assert(ppp->type == def_define);
971 if(ppp->subst.text && ppp->subst.text[0])
973 push_buffer(ppp, NULL, NULL, 0);
974 yy_scan_string(ppp->subst.text);
978 static int curdef_idx = 0;
979 static int curdef_alloc = 0;
980 static char *curdef_text = NULL;
982 static void add_text(const char *str, int len)
989 if(curdef_idx >= curdef_alloc || curdef_alloc - curdef_idx < len)
991 new_alloc = curdef_alloc + ((len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1));
992 new_text = pp_xrealloc(curdef_text, new_alloc * sizeof(curdef_text[0]));
995 curdef_text = new_text;
996 curdef_alloc = new_alloc;
997 if(curdef_alloc > 65536)
998 ppy_warning("Reallocating macro-expansion buffer larger than 64kB");
1000 memcpy(&curdef_text[curdef_idx], str, len);
1004 static mtext_t *add_expand_text(mtext_t *mtp, macexpstackentry_t *mep, int *nnl)
1017 add_text(mtp->subst.text, strlen(mtp->subst.text));
1021 cptr = mep->args[mtp->subst.argidx];
1025 if(*cptr == '"' || *cptr == '\\')
1034 /* Remove trailing whitespace from current expansion text */
1037 if(isspace(curdef_text[curdef_idx-1] & 0xff))
1042 /* tag current position and recursively expand the next part */
1044 mtp = add_expand_text(mtp->next, mep, nnl);
1046 /* Now get rid of the leading space of the expansion */
1047 cptr = &curdef_text[tag];
1048 n = curdef_idx - tag;
1051 if(isspace(*cptr & 0xff))
1059 if(cptr != &curdef_text[tag])
1061 memmove(&curdef_text[tag], cptr, n);
1062 curdef_idx -= (curdef_idx - tag) - n;
1067 if((mtp->next && mtp->next->type == exp_concat) || (mtp->prev && mtp->prev->type == exp_concat))
1068 exp = mep->args[mtp->subst.argidx];
1070 exp = mep->ppargs[mtp->subst.argidx];
1073 add_text(exp, strlen(exp));
1074 *nnl -= mep->nnls[mtp->subst.argidx];
1075 cptr = strchr(exp, '\n');
1079 cptr = strchr(cptr+1, '\n');
1081 mep->nnls[mtp->subst.argidx] = 0;
1086 pp_internal_error(__FILE__, __LINE__, "Invalid expansion type (%d) in macro expansion\n", mtp->type);
1091 static void expand_macro(macexpstackentry_t *mep)
1097 pp_entry_t *ppp = mep->ppp;
1098 int nargs = mep->nargs;
1100 assert(ppp->type == def_macro);
1101 assert(ppp->expanding == 0);
1103 if((ppp->nargs >= 0 && nargs != ppp->nargs) || (ppp->nargs < 0 && nargs < -ppp->nargs))
1105 ppy_error("Too %s macro arguments (%d)", nargs < abs(ppp->nargs) ? "few" : "many", nargs);
1109 for(n = 0; n < nargs; n++)
1110 nnl += mep->nnls[n];
1114 for(mtp = ppp->subst.mtext; mtp; mtp = mtp->next)
1116 if(!(mtp = add_expand_text(mtp, mep, &nnl)))
1120 for(n = 0; n < nnl; n++)
1123 /* To make sure there is room and termination (see below) */
1126 /* Strip trailing whitespace from expansion */
1127 for(k = curdef_idx, cptr = &curdef_text[curdef_idx-1]; k > 0; k--, cptr--)
1129 if(!isspace(*cptr & 0xff))
1134 * We must add *one* whitespace to make sure that there
1135 * is a token-separation after the expansion.
1141 /* Strip leading whitespace from expansion */
1142 for(n = 0, cptr = curdef_text; n < k; n++, cptr++)
1144 if(!isspace(*cptr & 0xff))
1150 push_buffer(ppp, NULL, NULL, 0);
1151 /*yy_scan_bytes(curdef_text + n, k - n);*/
1152 yy_scan_string(curdef_text + n);
1157 *-------------------------------------------------------------------------
1158 * String collection routines
1159 *-------------------------------------------------------------------------
1161 static void new_string(void)
1165 ppy_warning("new_string: strbuf_idx != 0");
1168 str_startline = pp_status.line_number;
1171 static void add_string(const char *str, int len)
1178 if(strbuf_idx >= strbuf_alloc || strbuf_alloc - strbuf_idx < len)
1180 new_alloc = strbuf_alloc + ((len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1));
1181 new_buffer = pp_xrealloc(strbuffer, new_alloc * sizeof(strbuffer[0]));
1184 strbuffer = new_buffer;
1185 strbuf_alloc = new_alloc;
1186 if(strbuf_alloc > 65536)
1187 ppy_warning("Reallocating string buffer larger than 64kB");
1189 memcpy(&strbuffer[strbuf_idx], str, len);
1193 static char *get_string(void)
1195 char *str = pp_xmalloc(strbuf_idx + 1);
1198 memcpy(str, strbuffer, strbuf_idx);
1199 str[strbuf_idx] = '\0';
1206 static void put_string(void)
1208 put_buffer(strbuffer, strbuf_idx);
1214 static int string_start(void)
1216 return str_startline;
1221 *-------------------------------------------------------------------------
1223 *-------------------------------------------------------------------------
1225 static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop)
1227 if(bufferstackidx >= MAXBUFFERSTACK)
1228 pp_internal_error(__FILE__, __LINE__, "Buffer stack overflow");
1230 memset(&bufferstack[bufferstackidx], 0, sizeof(bufferstack[0]));
1231 bufferstack[bufferstackidx].bufferstate = YY_CURRENT_BUFFER;
1232 bufferstack[bufferstackidx].filehandle = pp_status.file;
1233 bufferstack[bufferstackidx].define = ppp;
1234 bufferstack[bufferstackidx].line_number = pp_status.line_number;
1235 bufferstack[bufferstackidx].char_number = pp_status.char_number;
1236 bufferstack[bufferstackidx].if_depth = pp_get_if_depth();
1237 bufferstack[bufferstackidx].should_pop = pop;
1238 bufferstack[bufferstackidx].filename = pp_status.input;
1239 bufferstack[bufferstackidx].ncontinuations = ncontinuations;
1240 bufferstack[bufferstackidx].incl = pp_incl_state;
1241 bufferstack[bufferstackidx].include_filename = incname;
1247 /* These will track the ppy_error to the correct file and line */
1248 pp_status.line_number = 1;
1249 pp_status.char_number = 1;
1250 pp_status.input = filename;
1254 pp_internal_error(__FILE__, __LINE__, "Pushing buffer without knowing where to go to");
1258 static bufferstackentry_t *pop_buffer(void)
1260 if(bufferstackidx < 0)
1261 pp_internal_error(__FILE__, __LINE__, "Bufferstack underflow?");
1263 if(bufferstackidx == 0)
1268 if(bufferstack[bufferstackidx].define)
1269 bufferstack[bufferstackidx].define->expanding = 0;
1272 includelogicentry_t *iep = NULL;
1274 if(!bufferstack[bufferstackidx].should_pop)
1276 wpp_close(pp_status.file);
1277 pp_writestring("# %d \"%s\" 2\n", bufferstack[bufferstackidx].line_number, bufferstack[bufferstackidx].filename);
1279 /* We have EOF, check the include logic */
1280 if(pp_incl_state.state == 2 && !pp_incl_state.seen_junk && pp_incl_state.ppp)
1282 pp_entry_t *ppp = pplookup(pp_incl_state.ppp);
1285 iep = pp_xmalloc(sizeof(includelogicentry_t));
1290 iep->filename = bufferstack[bufferstackidx].include_filename;
1292 iep->next = pp_includelogiclist;
1294 iep->next->prev = iep;
1295 pp_includelogiclist = iep;
1299 free(pp_incl_state.ppp);
1300 pp_incl_state = bufferstack[bufferstackidx].incl;
1303 if (bufferstack[bufferstackidx].include_filename)
1305 free(pp_status.input);
1306 pp_status.input = bufferstack[bufferstackidx].filename;
1308 pp_status.line_number = bufferstack[bufferstackidx].line_number;
1309 pp_status.char_number = bufferstack[bufferstackidx].char_number;
1310 ncontinuations = bufferstack[bufferstackidx].ncontinuations;
1312 free(bufferstack[bufferstackidx].include_filename);
1315 pp_status.file = bufferstack[bufferstackidx].filehandle;
1316 ppy__switch_to_buffer(bufferstack[bufferstackidx].bufferstate);
1318 if(bufferstack[bufferstackidx].should_pop)
1320 if(yy_current_state() == pp_macexp)
1321 macro_add_expansion();
1323 pp_internal_error(__FILE__, __LINE__, "Pop buffer and state without macro expansion state");
1327 return &bufferstack[bufferstackidx];
1332 *-------------------------------------------------------------------------
1333 * Macro nestng support
1334 *-------------------------------------------------------------------------
1336 static void push_macro(pp_entry_t *ppp)
1338 if(macexpstackidx >= MAXMACEXPSTACK)
1340 ppy_error("Too many nested macros");
1344 macexpstack[macexpstackidx] = pp_xmalloc(sizeof(macexpstack[0][0]));
1345 if(!macexpstack[macexpstackidx])
1347 memset( macexpstack[macexpstackidx], 0, sizeof(macexpstack[0][0]));
1348 macexpstack[macexpstackidx]->ppp = ppp;
1352 static macexpstackentry_t *top_macro(void)
1354 return macexpstackidx > 0 ? macexpstack[macexpstackidx-1] : NULL;
1357 static macexpstackentry_t *pop_macro(void)
1359 if(macexpstackidx <= 0)
1360 pp_internal_error(__FILE__, __LINE__, "Macro expansion stack underflow\n");
1361 return macexpstack[--macexpstackidx];
1364 static void free_macro(macexpstackentry_t *mep)
1368 for(i = 0; i < mep->nargs; i++)
1376 static void add_text_to_macro(const char *text, int len)
1378 macexpstackentry_t *mep = top_macro();
1380 assert(mep->ppp->expanding == 0);
1382 if(mep->curargalloc - mep->curargsize <= len+1) /* +1 for '\0' */
1385 int new_alloc = mep->curargalloc + ((ALLOCBLOCKSIZE > len+1) ? ALLOCBLOCKSIZE : len+1);
1386 new_curarg = pp_xrealloc(mep->curarg, new_alloc * sizeof(mep->curarg[0]));
1389 mep->curarg = new_curarg;
1390 mep->curargalloc = new_alloc;
1392 memcpy(mep->curarg + mep->curargsize, text, len);
1393 mep->curargsize += len;
1394 mep->curarg[mep->curargsize] = '\0';
1397 static void macro_add_arg(int last)
1401 char **new_args, **new_ppargs;
1403 macexpstackentry_t *mep = top_macro();
1405 assert(mep->ppp->expanding == 0);
1407 new_args = pp_xrealloc(mep->args, (mep->nargs+1) * sizeof(mep->args[0]));
1410 mep->args = new_args;
1412 new_ppargs = pp_xrealloc(mep->ppargs, (mep->nargs+1) * sizeof(mep->ppargs[0]));
1415 mep->ppargs = new_ppargs;
1417 new_nnls = pp_xrealloc(mep->nnls, (mep->nargs+1) * sizeof(mep->nnls[0]));
1420 mep->nnls = new_nnls;
1422 mep->args[mep->nargs] = pp_xstrdup(mep->curarg ? mep->curarg : "");
1423 if(!mep->args[mep->nargs])
1425 cptr = mep->args[mep->nargs]-1;
1426 while((cptr = strchr(cptr+1, '\n')))
1430 mep->nnls[mep->nargs] = nnl;
1433 mep->curargalloc = mep->curargsize = 0;
1436 /* Each macro argument must be expanded to cope with stingize */
1437 if(last || mep->args[mep->nargs-1][0])
1439 yy_push_state(pp_macexp);
1440 push_buffer(NULL, NULL, NULL, last ? 2 : 1);
1441 yy_scan_string(mep->args[mep->nargs-1]);
1442 /*mep->bufferstackidx = bufferstackidx; But not nested! */
1446 static void macro_add_expansion(void)
1448 macexpstackentry_t *mep = top_macro();
1450 assert(mep->ppp->expanding == 0);
1452 mep->ppargs[mep->nargs-1] = pp_xstrdup(mep->curarg ? mep->curarg : "");
1454 mep->curargalloc = mep->curargsize = 0;
1460 *-------------------------------------------------------------------------
1462 *-------------------------------------------------------------------------
1464 static void put_buffer(const char *s, int len)
1467 add_text_to_macro(s, len);
1474 *-------------------------------------------------------------------------
1475 * Include management
1476 *-------------------------------------------------------------------------
1478 void pp_do_include(char *fname, int type)
1482 includelogicentry_t *iep;
1488 for(iep = pp_includelogiclist; iep; iep = iep->next)
1490 if(!strcmp(iep->filename, fname))
1493 * We are done. The file was included before.
1494 * If the define was deleted, then this entry would have
1506 ppy_error("Empty include filename");
1511 /* Undo the effect of the quotation */
1514 if((fp = pp_open_include(fname+1, type, pp_status.input, &newpath)) == NULL)
1516 ppy_error("Unable to open include file %s", fname+1);
1521 fname[n-1] = *fname; /* Redo the quotes */
1522 push_buffer(NULL, newpath, fname, 0);
1523 pp_incl_state.seen_junk = 0;
1524 pp_incl_state.state = 0;
1525 pp_incl_state.ppp = NULL;
1527 pp_status.file = fp;
1528 ppy__switch_to_buffer(ppy__create_buffer(NULL, YY_BUF_SIZE));
1530 pp_writestring("# 1 \"%s\" 1%s\n", newpath, type ? "" : " 3");
1534 *-------------------------------------------------------------------------
1535 * Push/pop preprocessor ignore state when processing conditionals
1537 *-------------------------------------------------------------------------
1539 void pp_push_ignore_state(void)
1541 yy_push_state(pp_ignore);
1544 void pp_pop_ignore_state(void)