3 * Copyright 1998-2000 Bertho A. Stultiens (BS)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * 21-May-2000 BS - Fixed the ident requirement of resource names
21 * which can be keywords.
22 * 30-Apr-2000 BS - Reintegration into the wine-tree
23 * 11-Jan-2000 BS - Very drastic cleanup because we don't have a
24 * preprocessor in here anymore.
25 * 02-Jan-2000 BS - Removed the preprocessor code
26 * 23-Dec-1999 BS - Removed the copyright for Martin von Loewis.
27 * There is really nothing left of his code in
29 * 20-Jun-1998 BS - Changed the filename conversion. Filenames are
30 * case-sensitive inder *nix, but not under dos.
31 * default behaviour is to convert to lower case.
32 * - All backslashes are converted to forward and
33 * both single and double slash is recognized as
35 * - Fixed a bug in 'yywf' case that prevented
36 * double quoted names to be scanned propperly.
38 * 19-May-1998 BS - Started to build a preprocessor.
39 * - Changed keyword processing completely to
42 * 20-Apr-1998 BS - Added ';' comment stripping
44 * 17-Apr-1998 BS - Made the win32 keywords optional when compiling in
47 * 15-Apr-1998 BS - Changed string handling to include escapes
48 * - Added unicode string handling (no codepage
49 * translation though).
50 * - 'Borrowed' the main idea of string scanning from
51 * the flex manual pages.
52 * - Added conditional handling of scanning depending
53 * on the state of the parser. This was mainly required
54 * to distinguish a file to load or raw data that
55 * follows. MS's definition of filenames is rather
56 * complex... It can be unquoted or double quoted. If
57 * double quoted, then the '\\' char is not automatically
58 * escaped according to Borland's rc compiler, but it
59 * accepts both "\\path\\file.rc" and "\path\file.rc".
60 * This makes life very hard! I go for the escaped
61 * version, as this seems to be the documented way...
62 * - Single quoted strings are now parsed and converted
64 * - Added comment stripping. The implementation is
65 * 'borrowed' from the flex manpages.
66 * - Rebuild string processing so that it may contain
70 /* Exclusive string handling */
72 /* Exclusive unicode string handling */
74 /* Exclusive rcdata single quoted data handling */
76 /* Exclusive comment eating... */
78 /* Set when stripping c-junk */
83 /* Set when scanning #line style directives */
85 /* Set when scanning #pragma */
90 %option nounput noyy_top_state
91 %option 8bit never-interactive
93 /* Some shortcut definitions */
95 cident [a-zA-Z_][0-9a-zA-Z_]*
107 #include "wine/unicode.h"
111 #include "newstruc.h"
113 #include "parser.tab.h"
115 #define YY_USE_PROTOS
117 #define YY_NO_TOP_STATE
119 /* Always update the current character position within a line */
120 #define YY_USER_ACTION char_number+=yyleng; wanted_id = want_id; want_id = 0;
122 static void addcchar(char c);
123 static void addwchar(WCHAR s);
124 static string_t *get_buffered_cstring(void);
125 static string_t *get_buffered_wstring(void);
126 static string_t *make_string(char *s);
128 static char *cbuffer; /* Buffers for string collection */
130 static int cbufalloc = 0;
131 static WCHAR *wbuffer;
133 static int wbufalloc = 0;
134 static int stripslevel = 0; /* Count {} during pp_strips/pp_stripe mode */
135 static int stripplevel = 0; /* Count () during pp_strips mode */
136 static int cjunk_tagline; /* Where did we start stripping (helps error tracking) */
138 static int current_codepage = -1; /* use language default */
141 * This one is a bit tricky.
142 * We set 'want_id' in the parser to get the first
143 * identifier we get across in the scanner, but we
144 * also want it to be reset at nearly any token we
145 * see. Exceptions are:
150 * The scanner will automatically reset 'want_id'
151 * after *each* scanner reduction and puts is value
152 * into the var below. In this way we can see the
153 * state after the YY_RULE_SETUP (i.e. the user action;
154 * see above) and don't have to worry too much when
155 * it needs to be reset.
157 static int wanted_id = 0;
158 static int save_wanted_id; /* To save across comment reductions */
168 static struct keyword keywords[] = {
169 { "ACCELERATORS", tACCELERATORS, 0, 0, 0},
170 { "ALT", tALT, 0, 0, 0},
171 { "ASCII", tASCII, 0, 0, 0},
172 { "AUTO3STATE", tAUTO3STATE, 1, 0, 0},
173 { "AUTOCHECKBOX", tAUTOCHECKBOX, 1, 0, 0},
174 { "AUTORADIOBUTTON", tAUTORADIOBUTTON, 1, 0, 0},
175 { "BEGIN", tBEGIN, 0, 0, 0},
176 { "BITMAP", tBITMAP, 0, 0, 0},
177 { "BLOCK", tBLOCK, 0, 0, 0},
178 { "BUTTON", tBUTTON, 1, 0, 0},
179 { "CAPTION", tCAPTION, 0, 0, 0},
180 { "CHARACTERISTICS", tCHARACTERISTICS, 1, 0, 0},
181 { "CHECKBOX", tCHECKBOX, 0, 0, 0},
182 { "CHECKED", tCHECKED, 0, 0, 0},
183 { "CLASS", tCLASS, 0, 0, 0},
184 { "COMBOBOX", tCOMBOBOX, 0, 0, 0},
185 { "CONTROL", tCONTROL, 0, 0, 0},
186 { "CTEXT", tCTEXT, 0, 0, 0},
187 { "CURSOR", tCURSOR, 0, 0, 0},
188 { "DEFPUSHBUTTON", tDEFPUSHBUTTON, 0, 0, 0},
189 { "DIALOG", tDIALOG, 0, 0, 0},
190 { "DIALOGEX", tDIALOGEX, 1, 0, 0},
191 { "DISCARDABLE", tDISCARDABLE, 0, 0, 0},
192 { "DLGINIT", tDLGINIT, 0, 0, 0},
193 { "EDITTEXT", tEDITTEXT, 0, 0, 0},
194 { "END", tEND, 0, 0, 0},
195 { "EXSTYLE", tEXSTYLE, 0, 0, 0},
196 { "FILEFLAGS", tFILEFLAGS, 0, 0, 0},
197 { "FILEFLAGSMASK", tFILEFLAGSMASK, 0, 0, 0},
198 { "FILEOS", tFILEOS, 0, 0, 0},
199 { "FILESUBTYPE", tFILESUBTYPE, 0, 0, 0},
200 { "FILETYPE", tFILETYPE, 0, 0, 0},
201 { "FILEVERSION", tFILEVERSION, 0, 0, 0},
202 { "FIXED", tFIXED, 0, 0, 0},
203 { "FONT", tFONT, 0, 0, 0},
204 { "FONTDIR", tFONTDIR, 0, 0, 0}, /* This is a Borland BRC extension */
205 { "GRAYED", tGRAYED, 0, 0, 0},
206 { "GROUPBOX", tGROUPBOX, 0, 0, 0},
207 { "HELP", tHELP, 0, 0, 0},
208 { "HTML", tHTML, 0, 0, 0},
209 { "ICON", tICON, 0, 0, 0},
210 { "IMPURE", tIMPURE, 0, 0, 0},
211 { "INACTIVE", tINACTIVE, 0, 0, 0},
212 { "LANGUAGE", tLANGUAGE, 1, 0, 1},
213 { "LISTBOX", tLISTBOX, 0, 0, 0},
214 { "LOADONCALL", tLOADONCALL, 0, 0, 0},
215 { "LTEXT", tLTEXT, 0, 0, 0},
216 { "MENU", tMENU, 0, 0, 0},
217 { "MENUBARBREAK", tMENUBARBREAK, 0, 0, 0},
218 { "MENUBREAK", tMENUBREAK, 0, 0, 0},
219 { "MENUEX", tMENUEX, 1, 0, 0},
220 { "MENUITEM", tMENUITEM, 0, 0, 0},
221 { "MESSAGETABLE", tMESSAGETABLE, 1, 0, 0},
222 { "MOVEABLE", tMOVEABLE, 0, 0, 0},
223 { "NOINVERT", tNOINVERT, 0, 0, 0},
224 { "NOT", tNOT, 0, 0, 0},
225 { "POPUP", tPOPUP, 0, 0, 0},
226 { "PRELOAD", tPRELOAD, 0, 0, 0},
227 { "PRODUCTVERSION", tPRODUCTVERSION, 0, 0, 0},
228 { "PURE", tPURE, 0, 0, 0},
229 { "PUSHBUTTON", tPUSHBUTTON, 0, 0, 0},
230 { "RADIOBUTTON", tRADIOBUTTON, 0, 0, 0},
231 { "RCDATA", tRCDATA, 0, 0, 0},
232 { "RTEXT", tRTEXT, 0, 0, 0},
233 { "SCROLLBAR", tSCROLLBAR, 0, 0, 0},
234 { "SEPARATOR", tSEPARATOR, 0, 0, 0},
235 { "SHIFT", tSHIFT, 0, 0, 0},
236 { "STATE3", tSTATE3, 1, 0, 0},
237 { "STRING", tSTRING, 0, 0, 0},
238 { "STRINGTABLE", tSTRINGTABLE, 0, 0, 1},
239 { "STYLE", tSTYLE, 0, 0, 0},
240 { "TOOLBAR", tTOOLBAR, 1, 0, 0},
241 { "VALUE", tVALUE, 0, 0, 0},
242 { "VERSION", tVERSION, 1, 0, 0},
243 { "VERSIONINFO", tVERSIONINFO, 0, 0, 0},
244 { "VIRTKEY", tVIRTKEY, 0, 0, 0}
247 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
248 #define KWP(p) ((const struct keyword *)(p))
249 static int kw_cmp_func(const void *s1, const void *s2)
252 ret = strcasecmp(KWP(s1)->keyword, KWP(s2)->keyword);
253 if(!ret && (KWP(s1)->needcase || KWP(s2)->needcase))
254 return strcmp(KWP(s1)->keyword, KWP(s2)->keyword);
261 static struct keyword *iskeyword(char *kw)
269 /* Make sure that it is sorted for bsearsh */
270 static int sorted = 0;
273 qsort(keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
279 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
283 for(i = 0; i < NKEYWORDS; i++)
285 if(!kw_cmp_func(&key, &keywords[i]))
295 if(kwp == NULL || (kwp->isextension && !extensions))
304 **************************************************************************
305 * The flexer starts here
306 **************************************************************************
310 * Catch the GCC-style line statements here and parse them.
311 * This has the advantage that you can #include at any
312 * stage in the resource file.
313 * The preprocessor generates line directives in the format:
314 * # <linenum> "filename" <codes>
316 * Codes can be a sequence of:
317 * - 1 start of new file
318 * - 2 returning to previous
320 * - 4 interpret as C-code
322 * 4 is not used and 1 mutually excludes 2
323 * Anyhow, we are not really interested in these at all
324 * because we only want to know the linenumber and
327 <INITIAL,pp_strips,pp_stripp>^{ws}*\#{ws}*pragma{ws}+ yy_push_state(pp_pragma);
328 <INITIAL,pp_strips,pp_stripp>^{ws}*\#{ws}* yy_push_state(pp_line);
334 lineno = (int)strtol(yytext, &cptr, 10);
336 yyerror("Malformed '#...' line-directive; invalid linenumber");
337 fname = strchr(cptr, '"');
339 yyerror("Malformed '#...' line-directive; missing filename");
341 cptr = strchr(fname, '"');
343 yyerror("Malformed '#...' line-directive; missing terminating \"");
345 line_number = lineno - 1; /* We didn't read the newline */
346 input_name = xstrdup(fname);
349 <pp_pragma>code_page[^\n]* yyless(9); yy_pop_state(); yy_push_state(pp_code_page);
350 <pp_pragma>[^\n]* yy_pop_state(); if (pedantic) yywarning("Unrecognized #pragma directive '%s'",yytext);
352 <pp_code_page>\({ws}*default{ws}*\)[^\n]* current_codepage = -1; yy_pop_state();
353 <pp_code_page>\({ws}*[0-9]+{ws}*\)[^\n]* {
356 while (*p < '0' || *p > '9') p++;
357 current_codepage = strtol( p, NULL, 10 );
358 if (current_codepage && !wine_cp_get_table( current_codepage ))
360 yyerror("Codepage %d not supported", current_codepage);
361 current_codepage = 0;
364 <pp_code_page>[^\n]* yy_pop_state(); yyerror("Malformed #pragma code_page directive");
367 * Strip everything until a ';' taking
368 * into account braces {} for structures,
371 <pp_strips>\{ stripslevel++;
372 <pp_strips>\} stripslevel--;
373 <pp_strips>; if(!stripslevel) yy_pop_state();
374 <pp_strips>\/[^*\n] ; /* To catch comments */
375 <pp_strips>[^\{\};\n#/]* ; /* Ignore rest */
376 <pp_strips>\n line_number++; char_number = 1;
378 <pp_stripp>\( stripplevel++;
384 yy_push_state(pp_stripp_final);
387 <pp_stripp>\/[^*\n] ; /* To catch comments */
388 <pp_stripp>[^\(\);\n#/]* ; /* Ignore rest */
389 <pp_stripp>\n line_number++; char_number = 1;
391 <pp_stripp_final>{ws}* ; /* Ignore */
392 <pp_stripp_final>; yy_pop_state(); /* Kill the semicolon */
393 <pp_stripp_final>\n line_number++; char_number = 1; yy_pop_state();
394 <pp_stripp_final>. yyless(0); yy_pop_state();
399 [0-9]+[lL]? { yylval.num = strtoul(yytext, 0, 10); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
400 0[xX][0-9A-Fa-f]+[lL]? { yylval.num = strtoul(yytext, 0, 16); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
401 0[oO][0-7]+[lL]? { yylval.num = strtoul(yytext+2, 0, 8); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
404 * The next two rules scan identifiers and filenames.
405 * This is achieved by using the priority ruling
406 * of the scanner where a '.' is valid in a filename
407 * and *only* in a filename. In this case, the second
408 * rule will be reduced because it is longer.
411 struct keyword *tok = iskeyword(yytext);
415 if(wanted_id && !tok->alwayskw)
417 yylval.str = make_string(yytext);
425 yylval.str = make_string(yytext);
429 [A-Za-z_0-9./\\]+ yylval.str = make_string(yytext); return tFILENAME;
432 * Wide string scanning
435 yy_push_state(yylstr);
438 yywarning("16bit resource contains unicode strings\n");
443 yylval.str = get_buffered_wstring();
446 <yylstr>\\[0-7]{1,6} { /* octal escape sequence */
448 result = strtoul(yytext+1, 0, 8);
449 if ( result > 0xffff )
450 yyerror("Character constant out of range");
451 addwchar((WCHAR)result);
453 <yylstr>\\x[0-9a-fA-F]{4} { /* hex escape sequence */
455 result = strtoul(yytext+2, 0, 16);
456 addwchar((WCHAR)result);
458 <yylstr>\\x[0-9a-fA-F]{1,3} { yyerror("Invalid hex escape sequence '%s'", yytext); }
460 <yylstr>\\[0-9]+ yyerror("Bad escape sequence");
461 <yylstr>\\\n{ws}* line_number++; char_number = 1; /* backslash at EOL continues string after leading whitespace on next line */
462 <yylstr>\\a addwchar('\a');
463 <yylstr>\\b addwchar('\b');
464 <yylstr>\\f addwchar('\f');
465 <yylstr>\\n addwchar('\n');
466 <yylstr>\\r addwchar('\r');
467 <yylstr>\\t addwchar('\t');
468 <yylstr>\\v addwchar('\v');
469 <yylstr>\\. addwchar(yytext[1]);
470 <yylstr>\\\r\n addwchar(yytext[2]); line_number++; char_number = 1;
471 <yylstr>\"\" addwchar('\"'); /* "bla""bla" -> "bla\"bla" */
472 <yylstr>\\\"\" addwchar('\"'); /* "bla\""bla" -> "bla\"bla" */
473 <yylstr>\"{ws}+\" ; /* "bla" "bla" -> "blabla" */
476 while(*yptr) /* FIXME: codepage translation */
477 addwchar(*yptr++ & 0xff);
479 <yylstr>\n yyerror("Unterminated string");
482 * Normal string scanning
484 \" yy_push_state(yystr); cbufidx = 0;
488 yylval.str = get_buffered_cstring();
491 <yystr>\\[0-7]{1,3} { /* octal escape sequence */
493 result = strtol(yytext+1, 0, 8);
495 yyerror("Character constant out of range");
496 addcchar((char)result);
498 <yystr>\\x[0-9a-fA-F]{2} { /* hex escape sequence */
500 result = strtol(yytext+2, 0, 16);
501 addcchar((char)result);
503 <yystr>\\x[0-9a-fA-F] { yyerror("Invalid hex escape sequence '%s'", yytext); }
505 <yystr>\\[0-9]+ yyerror("Bad escape sequence");
506 <yystr>\\\n{ws}* line_number++; char_number = 1; /* backslash at EOL continues string after leading whitespace on next line */
507 <yystr>\\a addcchar('\a');
508 <yystr>\\b addcchar('\b');
509 <yystr>\\f addcchar('\f');
510 <yystr>\\n addcchar('\n');
511 <yystr>\\r addcchar('\r');
512 <yystr>\\t addcchar('\t');
513 <yystr>\\v addcchar('\v');
514 <yystr>\\. addcchar(yytext[1]);
515 <yystr>\\\r\n addcchar(yytext[2]); line_number++; char_number = 1;
521 <yystr>\"\" addcchar('\"'); /* "bla""bla" -> "bla\"bla" */
522 <yystr>\\\"\" addcchar('\"'); /* "bla\""bla" -> "bla\"bla" */
523 <yystr>\"{ws}+\" ; /* "bla" "bla" -> "blabla" */
524 <yystr>\n yyerror("Unterminated string");
529 \' yy_push_state(yyrcd); cbufidx = 0;
532 yylval.raw = new_raw_data();
533 yylval.raw->size = cbufidx;
534 yylval.raw->data = xmalloc(yylval.raw->size);
535 memcpy(yylval.raw->data, cbuffer, yylval.raw->size);
538 <yyrcd>[0-9a-fA-F]{2} {
540 result = strtol(yytext, 0, 16);
541 addcchar((char)result);
543 <yyrcd>{ws}+ ; /* Ignore space */
544 <yyrcd>\n line_number++; char_number = 1;
545 <yyrcd>. yyerror("Malformed data-line");
549 * Should never occur after preprocessing
551 <INITIAL,pp_stripp,pp_strips>"/*" {
552 yy_push_state(comment);
553 save_wanted_id = wanted_id;
555 yywarning("Found comments after preprocessing, please report");
558 <comment>"*"+[^*/\n]* ;
559 <comment>\n line_number++; char_number = 1;
560 <comment>"*"+"/" yy_pop_state(); want_id = save_wanted_id;
562 ;[^\n]* want_id = wanted_id; /* not really comment, but left-over c-junk */
563 "//"[^\n]* want_id = wanted_id; if(!no_preprocess) yywarning("Found comments after preprocessing, please report");
575 {ws}+ want_id = wanted_id; /* Eat whitespace */
577 <INITIAL>. return yytext[0];
580 if(YY_START == pp_strips || YY_START == pp_stripe || YY_START == pp_stripp || YY_START == pp_stripp_final)
581 yyerror("Unexpected end of file during c-junk scanning (started at %d)", cjunk_tagline);
587 /* Catch all rule to find any unmatched text */
593 yywarning("Unmatched text '%c' (0x%02x) YY_START=%d stripslevel=%d",
594 isprint(*yytext & 0xff) ? *yytext : '.', *yytext, YY_START,stripslevel);
603 if(bufferstackidx > 0)
612 /* These dup functions copy the enclosed '\0' from
613 * the resource string.
615 static void addcchar(char c)
617 if(cbufidx >= cbufalloc)
620 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
621 if(cbufalloc > 65536)
622 yywarning("Reallocating string buffer larger than 64kB");
624 cbuffer[cbufidx++] = c;
627 static void addwchar(WCHAR s)
629 if(wbufidx >= wbufalloc)
632 wbuffer = xrealloc(wbuffer, wbufalloc * sizeof(wbuffer[0]));
633 if(wbufalloc > 65536)
634 yywarning("Reallocating wide string buffer larger than 64kB");
636 wbuffer[wbufidx++] = s;
639 static string_t *get_buffered_cstring(void)
641 string_t *str = new_string();
644 str->type = str_char;
645 str->str.cstr = (char *)xmalloc(cbufidx+1);
646 memcpy(str->str.cstr, cbuffer, cbufidx);
647 str->str.cstr[cbufidx] = '\0';
649 if (!current_codepage || current_codepage == -1 || !win32) /* store as ANSI string */
651 if (!current_codepage) yyerror("Codepage set to Unicode only, cannot use ASCII string here");
654 else /* convert to Unicode before storing */
656 string_t *str_w = convert_string( str, str_unicode, current_codepage );
657 if (!check_unicode_conversion( str, str_w, current_codepage ))
658 yyerror("String %s does not convert identically to Unicode and back in codepage %d. "
659 "Try using a Unicode string instead.", str->str.cstr, current_codepage );
665 static string_t *get_buffered_wstring(void)
667 string_t *str = new_string();
669 str->type = str_unicode;
670 str->str.wstr = xmalloc((wbufidx+1)*sizeof(WCHAR));
671 memcpy(str->str.wstr, wbuffer, wbufidx*sizeof(WCHAR));
672 str->str.wstr[wbufidx] = 0;
676 static string_t *make_string(char *s)
678 string_t *str = new_string();
679 str->size = strlen(s);
680 str->type = str_char;
681 str->str.cstr = (char *)xmalloc(str->size+1);
682 memcpy(str->str.cstr, s, str->size+1);