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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 */
80 /* Set when scanning #line style directives */
82 /* Set when scanning #pragma */
87 %option nounput noyy_top_state noyywrap
88 %option 8bit never-interactive
89 %option prefix="parser_"
91 /* Some shortcut definitions */
93 cident [a-zA-Z_][0-9a-zA-Z_]*
105 #include "wine/unicode.h"
109 #include "newstruc.h"
111 #include "parser.tab.h"
113 /* Always update the current character position within a line */
114 #define YY_USER_ACTION char_number+=yyleng; wanted_id = want_id; want_id = 0;
116 static void addcchar(char c);
117 static void addwchar(WCHAR s);
118 static string_t *get_buffered_cstring(void);
119 static string_t *get_buffered_wstring(void);
120 static string_t *make_string(char *s);
122 static char *cbuffer; /* Buffers for string collection */
124 static int cbufalloc = 0;
125 static WCHAR *wbuffer;
127 static int wbufalloc = 0;
129 static int current_codepage = -1; /* use language default */
132 * This one is a bit tricky.
133 * We set 'want_id' in the parser to get the first
134 * identifier we get across in the scanner, but we
135 * also want it to be reset at nearly any token we
136 * see. Exceptions are:
141 * The scanner will automatically reset 'want_id'
142 * after *each* scanner reduction and puts is value
143 * into the var below. In this way we can see the
144 * state after the YY_RULE_SETUP (i.e. the user action;
145 * see above) and don't have to worry too much when
146 * it needs to be reset.
148 static int wanted_id = 0;
149 static int save_wanted_id; /* To save across comment reductions */
159 static struct keyword keywords[] = {
160 { "ACCELERATORS", tACCELERATORS, 0, 0, 0},
161 { "ALT", tALT, 0, 0, 0},
162 { "ASCII", tASCII, 0, 0, 0},
163 { "AUTO3STATE", tAUTO3STATE, 1, 0, 0},
164 { "AUTOCHECKBOX", tAUTOCHECKBOX, 1, 0, 0},
165 { "AUTORADIOBUTTON", tAUTORADIOBUTTON, 1, 0, 0},
166 { "BEGIN", tBEGIN, 0, 0, 0},
167 { "BITMAP", tBITMAP, 0, 0, 0},
168 { "BLOCK", tBLOCK, 0, 0, 0},
169 { "BUTTON", tBUTTON, 1, 0, 0},
170 { "CAPTION", tCAPTION, 0, 0, 0},
171 { "CHARACTERISTICS", tCHARACTERISTICS, 1, 0, 0},
172 { "CHECKBOX", tCHECKBOX, 0, 0, 0},
173 { "CHECKED", tCHECKED, 0, 0, 0},
174 { "CLASS", tCLASS, 0, 0, 0},
175 { "COMBOBOX", tCOMBOBOX, 0, 0, 0},
176 { "CONTROL", tCONTROL, 0, 0, 0},
177 { "CTEXT", tCTEXT, 0, 0, 0},
178 { "CURSOR", tCURSOR, 0, 0, 0},
179 { "DEFPUSHBUTTON", tDEFPUSHBUTTON, 0, 0, 0},
180 { "DIALOG", tDIALOG, 0, 0, 0},
181 { "DIALOGEX", tDIALOGEX, 1, 0, 0},
182 { "DISCARDABLE", tDISCARDABLE, 0, 0, 0},
183 { "DLGINIT", tDLGINIT, 0, 0, 0},
184 { "EDITTEXT", tEDITTEXT, 0, 0, 0},
185 { "END", tEND, 0, 0, 0},
186 { "EXSTYLE", tEXSTYLE, 0, 0, 0},
187 { "FILEFLAGS", tFILEFLAGS, 0, 0, 0},
188 { "FILEFLAGSMASK", tFILEFLAGSMASK, 0, 0, 0},
189 { "FILEOS", tFILEOS, 0, 0, 0},
190 { "FILESUBTYPE", tFILESUBTYPE, 0, 0, 0},
191 { "FILETYPE", tFILETYPE, 0, 0, 0},
192 { "FILEVERSION", tFILEVERSION, 0, 0, 0},
193 { "FIXED", tFIXED, 0, 0, 0},
194 { "FONT", tFONT, 0, 0, 0},
195 { "FONTDIR", tFONTDIR, 0, 0, 0}, /* This is a Borland BRC extension */
196 { "GRAYED", tGRAYED, 0, 0, 0},
197 { "GROUPBOX", tGROUPBOX, 0, 0, 0},
198 { "HELP", tHELP, 0, 0, 0},
199 { "HTML", tHTML, 0, 0, 0},
200 { "ICON", tICON, 0, 0, 0},
201 { "IMPURE", tIMPURE, 0, 0, 0},
202 { "INACTIVE", tINACTIVE, 0, 0, 0},
203 { "LANGUAGE", tLANGUAGE, 1, 0, 1},
204 { "LISTBOX", tLISTBOX, 0, 0, 0},
205 { "LOADONCALL", tLOADONCALL, 0, 0, 0},
206 { "LTEXT", tLTEXT, 0, 0, 0},
207 { "MENU", tMENU, 0, 0, 0},
208 { "MENUBARBREAK", tMENUBARBREAK, 0, 0, 0},
209 { "MENUBREAK", tMENUBREAK, 0, 0, 0},
210 { "MENUEX", tMENUEX, 1, 0, 0},
211 { "MENUITEM", tMENUITEM, 0, 0, 0},
212 { "MESSAGETABLE", tMESSAGETABLE, 1, 0, 0},
213 { "MOVEABLE", tMOVEABLE, 0, 0, 0},
214 { "NOINVERT", tNOINVERT, 0, 0, 0},
215 { "NOT", tNOT, 0, 0, 0},
216 { "POPUP", tPOPUP, 0, 0, 0},
217 { "PRELOAD", tPRELOAD, 0, 0, 0},
218 { "PRODUCTVERSION", tPRODUCTVERSION, 0, 0, 0},
219 { "PURE", tPURE, 0, 0, 0},
220 { "PUSHBUTTON", tPUSHBUTTON, 0, 0, 0},
221 { "RADIOBUTTON", tRADIOBUTTON, 0, 0, 0},
222 { "RCDATA", tRCDATA, 0, 0, 0},
223 { "RTEXT", tRTEXT, 0, 0, 0},
224 { "SCROLLBAR", tSCROLLBAR, 0, 0, 0},
225 { "SEPARATOR", tSEPARATOR, 0, 0, 0},
226 { "SHIFT", tSHIFT, 0, 0, 0},
227 { "STATE3", tSTATE3, 1, 0, 0},
228 { "STRING", tSTRING, 0, 0, 0},
229 { "STRINGTABLE", tSTRINGTABLE, 0, 0, 1},
230 { "STYLE", tSTYLE, 0, 0, 0},
231 { "TOOLBAR", tTOOLBAR, 1, 0, 0},
232 { "VALUE", tVALUE, 0, 0, 0},
233 { "VERSION", tVERSION, 1, 0, 0},
234 { "VERSIONINFO", tVERSIONINFO, 0, 0, 0},
235 { "VIRTKEY", tVIRTKEY, 0, 0, 0}
238 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
239 #define KWP(p) ((const struct keyword *)(p))
240 static int kw_cmp_func(const void *s1, const void *s2)
243 ret = strcasecmp(KWP(s1)->keyword, KWP(s2)->keyword);
244 if(!ret && (KWP(s1)->needcase || KWP(s2)->needcase))
245 return strcmp(KWP(s1)->keyword, KWP(s2)->keyword);
252 static struct keyword *iskeyword(char *kw)
260 /* Make sure that it is sorted for bsearsh */
261 static int sorted = 0;
264 qsort(keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
270 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
274 for(i = 0; i < NKEYWORDS; i++)
276 if(!kw_cmp_func(&key, &keywords[i]))
286 if(kwp == NULL || (kwp->isextension && !extensions))
295 **************************************************************************
296 * The flexer starts here
297 **************************************************************************
301 * Catch the GCC-style line statements here and parse them.
302 * This has the advantage that you can #include at any
303 * stage in the resource file.
304 * The preprocessor generates line directives in the format:
305 * # <linenum> "filename" <codes>
307 * Codes can be a sequence of:
308 * - 1 start of new file
309 * - 2 returning to previous
311 * - 4 interpret as C-code
313 * 4 is not used and 1 mutually excludes 2
314 * Anyhow, we are not really interested in these at all
315 * because we only want to know the linenumber and
318 <INITIAL,pp_cstrip>^{ws}*\#{ws}*pragma{ws}+ yy_push_state(pp_pragma);
319 <INITIAL,pp_cstrip>^{ws}*\#{ws}* yy_push_state(pp_line);
325 lineno = (int)strtol(yytext, &cptr, 10);
327 parser_error("Malformed '#...' line-directive; invalid linenumber\n");
328 fname = strchr(cptr, '"');
330 parser_error("Malformed '#...' line-directive; missing filename\n");
332 cptr = strchr(fname, '"');
334 parser_error("Malformed '#...' line-directive; missing terminating \"");
336 line_number = lineno - 1; /* We didn't read the newline */
337 input_name = xstrdup(fname);
338 /* ignore contents of C include files */
339 len = strlen(input_name);
340 if (len > 1 && !strcasecmp( input_name + len - 2, ".h" ))
346 <pp_pragma>code_page[^\n]* yyless(9); yy_pop_state(); yy_push_state(pp_code_page);
347 <pp_pragma>[^\n]* yy_pop_state(); if (pedantic) parser_warning("Unrecognized #pragma directive '%s'\n",yytext);
349 <pp_code_page>\({ws}*default{ws}*\)[^\n]* current_codepage = -1; yy_pop_state();
350 <pp_code_page>\({ws}*utf8{ws}*\)[^\n]* current_codepage = CP_UTF8; yy_pop_state();
351 <pp_code_page>\({ws}*[0-9]+{ws}*\)[^\n]* {
354 while (*p < '0' || *p > '9') p++;
355 current_codepage = strtol( p, NULL, 10 );
356 if (current_codepage && current_codepage != CP_UTF8 && !wine_cp_get_table( current_codepage ))
358 parser_error("Codepage %d not supported\n", current_codepage);
359 current_codepage = 0;
362 <pp_code_page>[^\n]* yy_pop_state(); parser_error("Malformed #pragma code_page directive\n");
365 * Strip everything until a ';' taking
366 * into account braces {} for structures,
369 <pp_cstrip>\n line_number++; char_number = 1;
370 <pp_cstrip>. ; /* ignore */
375 [0-9]+[lL]? { parser_lval.num = strtoul(yytext, 0, 10); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
376 0[xX][0-9A-Fa-f]+[lL]? { parser_lval.num = strtoul(yytext, 0, 16); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
377 0[oO][0-7]+[lL]? { parser_lval.num = strtoul(yytext+2, 0, 8); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
380 * The next two rules scan identifiers and filenames.
381 * This is achieved by using the priority ruling
382 * of the scanner where a '.' is valid in a filename
383 * and *only* in a filename. In this case, the second
384 * rule will be reduced because it is longer.
387 struct keyword *tok = iskeyword(yytext);
391 if(wanted_id && !tok->alwayskw)
393 parser_lval.str = make_string(yytext);
401 parser_lval.str = make_string(yytext);
405 [A-Za-z_0-9./\\]+ parser_lval.str = make_string(yytext); return tFILENAME;
408 * Wide string scanning
411 yy_push_state(tklstr);
414 parser_warning("16bit resource contains unicode strings\n");
419 parser_lval.str = get_buffered_wstring();
422 <tklstr>\\[0-7]{1,6} { /* octal escape sequence */
424 result = strtoul(yytext+1, 0, 8);
425 if ( result > 0xffff )
426 parser_error("Character constant out of range\n");
427 addwchar((WCHAR)result);
429 <tklstr>\\x[0-9a-fA-F]{4} { /* hex escape sequence */
431 result = strtoul(yytext+2, 0, 16);
432 addwchar((WCHAR)result);
434 <tklstr>\\x[0-9a-fA-F]{1,3} { parser_error("Invalid hex escape sequence '%s'\n", yytext); }
436 <tklstr>\\[0-9]+ parser_error("Bad escape sequence\n");
437 <tklstr>\\\n{ws}* line_number++; char_number = 1; /* backslash at EOL continues string after leading whitespace on next line */
438 <tklstr>\\a addwchar('\a');
439 <tklstr>\\b addwchar('\b');
440 <tklstr>\\f addwchar('\f');
441 <tklstr>\\n addwchar('\n');
442 <tklstr>\\r addwchar('\r');
443 <tklstr>\\t addwchar('\t');
444 <tklstr>\\v addwchar('\v');
445 <tklstr>\\. addwchar(yytext[1]);
446 <tklstr>\\\r\n addwchar(yytext[2]); line_number++; char_number = 1;
447 <tklstr>\"\" addwchar('\"'); /* "bla""bla" -> "bla\"bla" */
448 <tklstr>\\\"\" addwchar('\"'); /* "bla\""bla" -> "bla\"bla" */
449 <tklstr>\"{ws}+\" ; /* "bla" "bla" -> "blabla" */
452 while(*yptr) /* FIXME: codepage translation */
453 addwchar(*yptr++ & 0xff);
455 <tklstr>\n parser_error("Unterminated string\n");
458 * Normal string scanning
460 \" yy_push_state(tkstr); cbufidx = 0;
464 parser_lval.str = get_buffered_cstring();
467 <tkstr>\\[0-7]{1,3} { /* octal escape sequence */
469 result = strtol(yytext+1, 0, 8);
471 parser_error("Character constant out of range\n");
472 addcchar((char)result);
474 <tkstr>\\x[0-9a-fA-F]{2} { /* hex escape sequence */
476 result = strtol(yytext+2, 0, 16);
477 addcchar((char)result);
479 <tkstr>\\x[0-9a-fA-F] { parser_error("Invalid hex escape sequence '%s'\n", yytext); }
481 <tkstr>\\[0-9]+ parser_error("Bad escape sequence\n");
482 <tkstr>\\\n{ws}* line_number++; char_number = 1; /* backslash at EOL continues string after leading whitespace on next line */
483 <tkstr>\\a addcchar('\a');
484 <tkstr>\\b addcchar('\b');
485 <tkstr>\\f addcchar('\f');
486 <tkstr>\\n addcchar('\n');
487 <tkstr>\\r addcchar('\r');
488 <tkstr>\\t addcchar('\t');
489 <tkstr>\\v addcchar('\v');
490 <tkstr>\\. addcchar(yytext[1]);
491 <tkstr>\\\r\n addcchar(yytext[2]); line_number++; char_number = 1;
497 <tkstr>\"\" addcchar('\"'); /* "bla""bla" -> "bla\"bla" */
498 <tkstr>\\\"\" addcchar('\"'); /* "bla\""bla" -> "bla\"bla" */
499 <tkstr>\"{ws}+\" ; /* "bla" "bla" -> "blabla" */
500 <tkstr>\n parser_error("Unterminated string\n");
505 \' yy_push_state(tkrcd); cbufidx = 0;
508 parser_lval.raw = new_raw_data();
509 parser_lval.raw->size = cbufidx;
510 parser_lval.raw->data = xmalloc(parser_lval.raw->size);
511 memcpy(parser_lval.raw->data, cbuffer, parser_lval.raw->size);
514 <tkrcd>[0-9a-fA-F]{2} {
516 result = strtol(yytext, 0, 16);
517 addcchar((char)result);
519 <tkrcd>{ws}+ ; /* Ignore space */
520 <tkrcd>\n line_number++; char_number = 1;
521 <tkrcd>. parser_error("Malformed data-line\n");
525 * Should never occur after preprocessing
527 <INITIAL,pp_cstrip>"/*" {
528 yy_push_state(comment);
529 save_wanted_id = wanted_id;
531 parser_warning("Found comments after preprocessing, please report\n");
534 <comment>"*"+[^*/\n]* ;
535 <comment>\n line_number++; char_number = 1;
536 <comment>"*"+"/" yy_pop_state(); want_id = save_wanted_id;
538 ;[^\n]* want_id = wanted_id; /* not really comment, but left-over c-junk */
539 "//"[^\n]* want_id = wanted_id; if(!no_preprocess) parser_warning("Found comments after preprocessing, please report\n");
551 {ws}+ want_id = wanted_id; /* Eat whitespace */
553 <INITIAL>. return yytext[0];
556 /* Catch all rule to find any unmatched text */
562 parser_warning("Unmatched text '%c' (0x%02x) YY_START=%d\n",
563 isprint(*yytext & 0xff) ? *yytext : '.', *yytext, YY_START);
568 /* These dup functions copy the enclosed '\0' from
569 * the resource string.
571 static void addcchar(char c)
573 if(cbufidx >= cbufalloc)
576 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
577 if(cbufalloc > 65536)
578 parser_warning("Reallocating string buffer larger than 64kB\n");
580 cbuffer[cbufidx++] = c;
583 static void addwchar(WCHAR s)
585 if(wbufidx >= wbufalloc)
588 wbuffer = xrealloc(wbuffer, wbufalloc * sizeof(wbuffer[0]));
589 if(wbufalloc > 65536)
590 parser_warning("Reallocating wide string buffer larger than 64kB\n");
592 wbuffer[wbufidx++] = s;
595 static string_t *get_buffered_cstring(void)
597 string_t *str = new_string();
600 str->type = str_char;
601 str->str.cstr = xmalloc(cbufidx+1);
602 memcpy(str->str.cstr, cbuffer, cbufidx);
603 str->str.cstr[cbufidx] = '\0';
605 if (!current_codepage || current_codepage == -1 || !win32) /* store as ANSI string */
607 if (!current_codepage) parser_error("Codepage set to Unicode only, cannot use ASCII string here\n");
610 else /* convert to Unicode before storing */
612 string_t *str_w = convert_string( str, str_unicode, current_codepage );
613 if (!check_unicode_conversion( str, str_w, current_codepage ))
614 parser_error("String %s does not convert identically to Unicode and back in codepage %d. "
615 "Try using a Unicode string instead\n", str->str.cstr, current_codepage );
621 static string_t *get_buffered_wstring(void)
623 string_t *str = new_string();
625 str->type = str_unicode;
626 str->str.wstr = xmalloc((wbufidx+1)*sizeof(WCHAR));
627 memcpy(str->str.wstr, wbuffer, wbufidx*sizeof(WCHAR));
628 str->str.wstr[wbufidx] = 0;
632 static string_t *make_string(char *s)
634 string_t *str = new_string();
635 str->size = strlen(s);
636 str->type = str_char;
637 str->str.cstr = xmalloc(str->size+1);
638 memcpy(str->str.cstr, s, str->size+1);