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 */
87 %option never-interactive
89 /* Some shortcut definitions */
91 cident [a-zA-Z_][0-9a-zA-Z_]*
107 #include "newstruc.h"
111 #define YY_USE_PROTOS
113 #define YY_NO_TOP_STATE
115 /* Always update the current character position within a line */
116 #define YY_USER_ACTION char_number+=yyleng; wanted_id = want_id; want_id = 0;
118 static void addcchar(char c);
119 static void addwchar(short s);
120 static string_t *get_buffered_cstring(void);
121 static string_t *get_buffered_wstring(void);
122 static string_t *make_string(char *s);
124 static char *cbuffer; /* Buffers for string collection */
126 static int cbufalloc = 0;
127 static short *wbuffer;
129 static int wbufalloc = 0;
130 static int stripslevel = 0; /* Count {} during pp_strips/pp_stripe mode */
131 static int stripplevel = 0; /* Count () during pp_strips mode */
132 static int cjunk_tagline; /* Where did we start stripping (helps error tracking) */
135 * This one is a bit tricky.
136 * We set 'want_id' in the parser to get the first
137 * identifier we get across in the scanner, but we
138 * also want it to be reset at nearly any token we
139 * see. Exceptions are:
144 * The scanner will automatically reset 'want_id'
145 * after *each* scanner reduction and puts is value
146 * into the var below. In this way we can see the
147 * state after the YY_RULE_SETUP (i.e. the user action;
148 * see above) and don't have to worry too much when
149 * it needs to be reset.
151 static int wanted_id = 0;
152 static int save_wanted_id; /* To save across comment reductions */
162 static struct keyword keywords[] = {
163 { "ACCELERATORS", tACCELERATORS, 0, 0, 0},
164 { "ALT", tALT, 0, 0, 0},
165 { "ASCII", tASCII, 0, 0, 0},
166 { "AUTO3STATE", tAUTO3STATE, 1, 0, 0},
167 { "AUTOCHECKBOX", tAUTOCHECKBOX, 1, 0, 0},
168 { "AUTORADIOBUTTON", tAUTORADIOBUTTON, 1, 0, 0},
169 { "BEGIN", tBEGIN, 0, 0, 0},
170 { "BITMAP", tBITMAP, 0, 0, 0},
171 { "BLOCK", tBLOCK, 0, 0, 0},
172 { "BUTTON", tBUTTON, 1, 0, 0},
173 { "CAPTION", tCAPTION, 0, 0, 0},
174 { "CHARACTERISTICS", tCHARACTERISTICS, 1, 0, 0},
175 { "CHECKBOX", tCHECKBOX, 0, 0, 0},
176 { "CHECKED", tCHECKED, 0, 0, 0},
177 { "CLASS", tCLASS, 0, 0, 0},
178 { "COMBOBOX", tCOMBOBOX, 0, 0, 0},
179 { "CONTROL", tCONTROL, 0, 0, 0},
180 { "CTEXT", tCTEXT, 0, 0, 0},
181 { "CURSOR", tCURSOR, 0, 0, 0},
182 { "DEFPUSHBUTTON", tDEFPUSHBUTTON, 0, 0, 0},
183 { "DIALOG", tDIALOG, 0, 0, 0},
184 { "DIALOGEX", tDIALOGEX, 1, 0, 0},
185 { "DISCARDABLE", tDISCARDABLE, 0, 0, 0},
186 { "DLGINIT", tDLGINIT, 0, 0, 0},
187 { "EDITTEXT", tEDITTEXT, 0, 0, 0},
188 { "END", tEND, 0, 0, 0},
189 { "EXSTYLE", tEXSTYLE, 0, 0, 0},
190 { "FILEFLAGS", tFILEFLAGS, 0, 0, 0},
191 { "FILEFLAGSMASK", tFILEFLAGSMASK, 0, 0, 0},
192 { "FILEOS", tFILEOS, 0, 0, 0},
193 { "FILESUBTYPE", tFILESUBTYPE, 0, 0, 0},
194 { "FILETYPE", tFILETYPE, 0, 0, 0},
195 { "FILEVERSION", tFILEVERSION, 0, 0, 0},
196 { "FIXED", tFIXED, 0, 0, 0},
197 { "FONT", tFONT, 0, 0, 0},
198 { "FONTDIR", tFONTDIR, 0, 0, 0}, /* This is a Borland BRC extension */
199 { "GRAYED", tGRAYED, 0, 0, 0},
200 { "GROUPBOX", tGROUPBOX, 0, 0, 0},
201 { "HELP", tHELP, 0, 0, 0},
202 { "ICON", tICON, 0, 0, 0},
203 { "IMPURE", tIMPURE, 0, 0, 0},
204 { "INACTIVE", tINACTIVE, 0, 0, 0},
205 { "LANGUAGE", tLANGUAGE, 1, 0, 1},
206 { "LISTBOX", tLISTBOX, 0, 0, 0},
207 { "LOADONCALL", tLOADONCALL, 0, 0, 0},
208 { "LTEXT", tLTEXT, 0, 0, 0},
209 { "MENU", tMENU, 0, 0, 0},
210 { "MENUBARBREAK", tMENUBARBREAK, 0, 0, 0},
211 { "MENUBREAK", tMENUBREAK, 0, 0, 0},
212 { "MENUEX", tMENUEX, 1, 0, 0},
213 { "MENUITEM", tMENUITEM, 0, 0, 0},
214 { "MESSAGETABLE", tMESSAGETABLE, 1, 0, 0},
215 { "MOVEABLE", tMOVEABLE, 0, 0, 0},
216 { "NOINVERT", tNOINVERT, 0, 0, 0},
217 { "NOT", tNOT, 0, 0, 0},
218 { "POPUP", tPOPUP, 0, 0, 0},
219 { "PRELOAD", tPRELOAD, 0, 0, 0},
220 { "PRODUCTVERSION", tPRODUCTVERSION, 0, 0, 0},
221 { "PURE", tPURE, 0, 0, 0},
222 { "PUSHBUTTON", tPUSHBUTTON, 0, 0, 0},
223 { "RADIOBUTTON", tRADIOBUTTON, 0, 0, 0},
224 { "RCDATA", tRCDATA, 0, 0, 0},
225 { "RTEXT", tRTEXT, 0, 0, 0},
226 { "SCROLLBAR", tSCROLLBAR, 0, 0, 0},
227 { "SEPARATOR", tSEPARATOR, 0, 0, 0},
228 { "SHIFT", tSHIFT, 0, 0, 0},
229 { "STATE3", tSTATE3, 1, 0, 0},
230 { "STRING", tSTRING, 0, 0, 0},
231 { "STRINGTABLE", tSTRINGTABLE, 0, 0, 1},
232 { "STYLE", tSTYLE, 0, 0, 0},
233 { "TOOLBAR", tTOOLBAR, 1, 0, 0},
234 { "VALUE", tVALUE, 0, 0, 0},
235 { "VERSION", tVERSION, 1, 0, 0},
236 { "VERSIONINFO", tVERSIONINFO, 0, 0, 0},
237 { "VIRTKEY", tVIRTKEY, 0, 0, 0}
240 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
241 #define KWP(p) ((struct keyword *)(p))
242 static int kw_cmp_func(const void *s1, const void *s2)
245 ret = strcasecmp(KWP(s1)->keyword, KWP(s2)->keyword);
246 if(!ret && (KWP(s1)->needcase || KWP(s2)->needcase))
247 return strcmp(KWP(s1)->keyword, KWP(s2)->keyword);
254 static struct keyword *iskeyword(char *kw)
262 /* Make sure that it is sorted for bsearsh */
263 static int sorted = 0;
266 qsort(keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
272 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
276 for(i = 0; i < NKEYWORDS; i++)
278 if(!kw_cmp_func(&key, &keywords[i]))
288 if(kwp == NULL || (kwp->isextension && !extensions))
297 **************************************************************************
298 * The flexer starts here
299 **************************************************************************
303 * Catch the GCC-style line statements here and parse them.
304 * This has the advantage that you can #include at any
305 * stage in the resource file.
306 * The preprocessor generates line directives in the format:
307 * # <linenum> "filename" <codes>
309 * Codes can be a sequence of:
310 * - 1 start of new file
311 * - 2 returning to previous
313 * - 4 interpret as C-code
315 * 4 is not used and 1 mutually excludes 2
316 * Anyhow, we are not really interested in these at all
317 * because we only want to know the linenumber and
320 <INITIAL,pp_strips,pp_stripp>^{ws}*\#{ws}* yy_push_state(pp_line);
326 lineno = (int)strtol(yytext, &cptr, 10);
328 yyerror("Malformed '#...' line-directive; invalid linenumber");
329 fname = strchr(cptr, '"');
331 yyerror("Malformed '#...' line-directive; missing filename");
333 cptr = strchr(fname, '"');
335 yyerror("Malformed '#...' line-directive; missing terminating \"");
337 line_number = lineno - 1; /* We didn't read the newline */
338 input_name = xstrdup(fname);
342 * Strip everything until a ';' taking
343 * into account braces {} for structures,
346 <pp_strips>\{ stripslevel++;
347 <pp_strips>\} stripslevel--;
348 <pp_strips>; if(!stripslevel) yy_pop_state();
349 <pp_strips>\/[^*\n] ; /* To catch comments */
350 <pp_strips>[^\{\};\n#/]* ; /* Ignore rest */
351 <pp_strips>\n line_number++; char_number = 1;
353 <pp_stripp>\( stripplevel++;
359 yy_push_state(pp_stripp_final);
362 <pp_stripp>\/[^*\n] ; /* To catch comments */
363 <pp_stripp>[^\(\);\n#/]* ; /* Ignore rest */
364 <pp_stripp>\n line_number++; char_number = 1;
366 <pp_stripp_final>{ws}* ; /* Ignore */
367 <pp_stripp_final>; yy_pop_state(); /* Kill the semicolon */
368 <pp_stripp_final>\n line_number++; char_number = 1; yy_pop_state();
369 <pp_stripp_final>. yyless(0); yy_pop_state();
374 [0-9]+[lL]? { yylval.num = strtoul(yytext, 0, 10); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
375 0[xX][0-9A-Fa-f]+[lL]? { yylval.num = strtoul(yytext, 0, 16); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
376 0[oO][0-7]+[lL]? { yylval.num = strtoul(yytext+2, 0, 8); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
379 * The next two rules scan identifiers and filenames.
380 * This is achieved by using the priority ruling
381 * of the scanner where a '.' is valid in a filename
382 * and *only* in a filename. In this case, the second
383 * rule will be reduced because it is longer.
386 struct keyword *tok = iskeyword(yytext);
390 if(wanted_id && !tok->alwayskw)
392 yylval.str = make_string(yytext);
400 yylval.str = make_string(yytext);
404 [A-Za-z_0-9./\\]+ yylval.str = make_string(yytext); return tFILENAME;
407 * Wide string scanning
410 yy_push_state(yylstr);
413 yywarning("16bit resource contains unicode strings\n");
418 yylval.str = get_buffered_wstring();
421 <yylstr>\\[0-7]{1,6} { /* octal escape sequence */
423 result = strtol(yytext+1, 0, 8);
424 if ( result > 0xffff )
425 yyerror("Character constant out of range");
426 addwchar((short)result);
428 <yylstr>\\x[0-9a-fA-F]{4} { /* hex escape sequence */
430 result = strtol(yytext+2, 0, 16);
431 addwchar((short)result);
433 <yylstr>\\x[0-9a-fA-F]{1,3} { yyerror("Invalid hex escape sequence '%s'", yytext); }
435 <yylstr>\\[0-9]+ yyerror("Bad escape sequence");
436 <yylstr>\\a addwchar('\a');
437 <yylstr>\\b addwchar('\b');
438 <yylstr>\\f addwchar('\f');
439 <yylstr>\\n addwchar('\n');
440 <yylstr>\\r addwchar('\r');
441 <yylstr>\\t addwchar('\t');
442 <yylstr>\\v addwchar('\v');
443 <yylstr>\\(\n|.) addwchar(yytext[1]);
444 <yylstr>\\\r\n addwchar(yytext[2]);
445 <yylstr>\"\" addcchar('\"'); /* "bla""bla" -> "bla\"bla" */
446 <yylstr>\\\"\" addcchar('\"'); /* "bla\""bla" -> "bla\"bla" */
447 <yylstr>\"{ws}+\" ; /* "bla" "bla" -> "blabla" */
450 while(*yptr) /* FIXME: codepage translation */
451 addwchar(*yptr++ & 0xff);
453 <yylstr>\n yyerror("Unterminated string");
456 * Normal string scanning
458 \" yy_push_state(yystr); cbufidx = 0;
462 yylval.str = get_buffered_cstring();
465 <yystr>\\[0-7]{1,3} { /* octal escape sequence */
467 result = strtol(yytext+1, 0, 8);
469 yyerror("Character constant out of range");
470 addcchar((char)result);
472 <yystr>\\x[0-9a-fA-F]{2} { /* hex escape sequence */
474 result = strtol(yytext+2, 0, 16);
475 addcchar((char)result);
477 <yystr>\\x[0-9a-fA-F] { yyerror("Invalid hex escape sequence '%s'", yytext); }
479 <yystr>\\[0-9]+ yyerror("Bad escape secuence");
480 <yystr>\\a addcchar('\a');
481 <yystr>\\b addcchar('\b');
482 <yystr>\\f addcchar('\f');
483 <yystr>\\n addcchar('\n');
484 <yystr>\\r addcchar('\r');
485 <yystr>\\t addcchar('\t');
486 <yystr>\\v addcchar('\v');
487 <yystr>\\(\n|.) addcchar(yytext[1]);
488 <yystr>\\\r\n addcchar(yytext[2]);
494 <yystr>\"\" addcchar('\"'); /* "bla""bla" -> "bla\"bla" */
495 <yystr>\\\"\" addcchar('\"'); /* "bla\""bla" -> "bla\"bla" */
496 <yystr>\"{ws}+\" ; /* "bla" "bla" -> "blabla" */
497 <yystr>\n yyerror("Unterminated string");
502 \' yy_push_state(yyrcd); cbufidx = 0;
505 yylval.raw = new_raw_data();
506 yylval.raw->size = cbufidx;
507 yylval.raw->data = xmalloc(yylval.raw->size);
508 memcpy(yylval.raw->data, cbuffer, yylval.raw->size);
511 <yyrcd>[0-9a-fA-F]{2} {
513 result = strtol(yytext, 0, 16);
514 addcchar((char)result);
516 <yyrcd>{ws}+ ; /* Ignore space */
517 <yyrcd>\n line_number++; char_number = 1;
518 <yyrcd>. yyerror("Malformed data-line");
522 * Should never occur after preprocessing
524 <INITIAL,pp_stripp,pp_strips>"/*" {
525 yy_push_state(comment);
526 save_wanted_id = wanted_id;
528 yywarning("Found comments after preprocessing, please report");
531 <comment>"*"+[^*/\n]* ;
532 <comment>\n line_number++; char_number = 1;
533 <comment>"*"+"/" yy_pop_state(); want_id = save_wanted_id;
535 ;[^\n]* want_id = wanted_id; /* not really comment, but left-over c-junk */
536 "//"[^\n]* want_id = wanted_id; if(!no_preprocess) yywarning("Found comments after preprocessing, please report");
548 {ws}+ want_id = wanted_id; /* Eat whitespace */
550 <INITIAL>. return yytext[0];
553 if(YY_START == pp_strips || YY_START == pp_stripe || YY_START == pp_stripp || YY_START == pp_stripp_final)
554 yyerror("Unexpected end of file during c-junk scanning (started at %d)", cjunk_tagline);
560 /* Catch all rule to find any unmatched text */
566 yywarning("Unmatched text '%c' (0x%02x) YY_START=%d stripslevel=%d",
567 isprint(*yytext & 0xff) ? *yytext : '.', *yytext, YY_START,stripslevel);
576 if(bufferstackidx > 0)
585 /* These dup functions copy the enclosed '\0' from
586 * the resource string.
588 static void addcchar(char c)
590 if(cbufidx >= cbufalloc)
593 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
594 if(cbufalloc > 65536)
595 yywarning("Reallocating string buffer larger than 64kB");
597 cbuffer[cbufidx++] = c;
600 static void addwchar(short s)
602 if(wbufidx >= wbufalloc)
605 wbuffer = xrealloc(wbuffer, wbufalloc * sizeof(wbuffer[0]));
606 if(wbufalloc > 65536)
607 yywarning("Reallocating wide string buffer larger than 64kB");
611 * BS 08-Aug-1999 FIXME: The '& 0xff' is probably a bug, but I have
612 * not experienced it yet and I seem to remember that this was for
613 * a reason. But, as so many things you tend to forget why.
614 * I guess that there were problems due to the sign extension of
615 * shorts WRT chars (e.g. 0x80 becomes 0xff80 instead of 0x0080).
616 * This should then be fixed in the lexer calling the function.
618 wbuffer[wbufidx++] = (short)(s & 0xff);
621 static string_t *get_buffered_cstring(void)
623 string_t *str = new_string();
625 str->type = str_char;
626 str->str.cstr = (char *)xmalloc(cbufidx+1);
627 memcpy(str->str.cstr, cbuffer, cbufidx);
628 str->str.cstr[cbufidx] = '\0';
632 static string_t *get_buffered_wstring(void)
634 string_t *str = new_string();
636 str->type = str_unicode;
637 str->str.wstr = (short *)xmalloc(2*(wbufidx+1));
638 memcpy(str->str.wstr, wbuffer, wbufidx);
639 str->str.wstr[wbufidx] = 0;
643 static string_t *make_string(char *s)
645 string_t *str = new_string();
646 str->size = strlen(s);
647 str->type = str_char;
648 str->str.cstr = (char *)xmalloc(str->size+1);
649 memcpy(str->str.cstr, s, str->size+1);