Avoid assertion failure when loading an empty data file.
[wine] / tools / wrc / parser.l
1 /* -*-C-*-
2  *
3  * Copyright 1998-2000  Bertho A. Stultiens (BS)
4  *
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.
9  *
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.
14  *
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
18  *
19  * History:
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
28  *                        this parser.
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
34  *                        MS/Borland does.
35  *                      - Fixed a bug in 'yywf' case that prevented
36  *                        double quoted names to be scanned propperly.
37  *
38  * 19-May-1998 BS       - Started to build a preprocessor.
39  *                      - Changed keyword processing completely to
40  *                        table-lookups.
41  *
42  * 20-Apr-1998 BS       - Added ';' comment stripping
43  *
44  * 17-Apr-1998 BS       - Made the win32 keywords optional when compiling in
45  *                        16bit mode
46  *
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
63  *                        here.
64  *                      - Added comment stripping. The implementation is
65  *                        'borrowed' from the flex manpages.
66  *                      - Rebuild string processing so that it may contain
67  *                        escaped '\0'.
68  */
69
70 /* Exclusive string handling */
71 %x yystr
72 /* Exclusive unicode string handling */
73 %x yylstr
74 /* Exclusive rcdata single quoted data handling */
75 %x yyrcd
76 /* Exclusive comment eating... */
77 %x comment
78 /* Set when stripping c-junk */
79 %x pp_stripe
80 %x pp_strips
81 %x pp_stripp
82 %x pp_stripp_final
83 /* Set when scanning #line style directives */
84 %x pp_line
85 /* Set when scanning #pragma */
86 %x pp_pragma
87 %x pp_code_page
88
89 %option stack
90 %option never-interactive
91
92 /* Some shortcut definitions */
93 ws      [ \f\t\r]
94 cident  [a-zA-Z_][0-9a-zA-Z_]*
95
96 %{
97
98 /*#define LEX_DEBUG*/
99
100 #include <stdio.h>
101 #include <stdlib.h>
102 #include <string.h>
103 #include <ctype.h>
104 #include <assert.h>
105
106 #include "wine/unicode.h"
107 #include "wrc.h"
108 #include "utils.h"
109 #include "parser.h"
110 #include "newstruc.h"
111
112 #include "y.tab.h"
113
114 #define YY_USE_PROTOS
115 #define YY_NO_UNPUT
116 #define YY_NO_TOP_STATE
117
118 /* Always update the current character position within a line */
119 #define YY_USER_ACTION  char_number+=yyleng; wanted_id = want_id; want_id = 0;
120
121 static void addcchar(char c);
122 static void addwchar(WCHAR s);
123 static string_t *get_buffered_cstring(void);
124 static string_t *get_buffered_wstring(void);
125 static string_t *make_string(char *s);
126
127 static char *cbuffer;           /* Buffers for string collection */
128 static int cbufidx;
129 static int cbufalloc = 0;
130 static WCHAR *wbuffer;
131 static int wbufidx;
132 static int wbufalloc = 0;
133 static int stripslevel = 0;     /* Count {} during pp_strips/pp_stripe mode */
134 static int stripplevel = 0;     /* Count () during pp_strips mode */
135 static int cjunk_tagline;       /* Where did we start stripping (helps error tracking) */
136
137 static int current_codepage = -1;  /* use language default */
138
139 /*
140  * This one is a bit tricky.
141  * We set 'want_id' in the parser to get the first
142  * identifier we get across in the scanner, but we
143  * also want it to be reset at nearly any token we
144  * see. Exceptions are:
145  * - newlines
146  * - comments
147  * - whitespace
148  *
149  * The scanner will automatically reset 'want_id'
150  * after *each* scanner reduction and puts is value
151  * into the var below. In this way we can see the
152  * state after the YY_RULE_SETUP (i.e. the user action;
153  * see above) and don't have to worry too much when
154  * it needs to be reset.
155  */
156 static int wanted_id = 0;
157 static int save_wanted_id;      /* To save across comment reductions */
158
159 struct keyword {
160         const char      *keyword;
161         int             token;
162         int             isextension;
163         int             needcase;
164         int             alwayskw;
165 };
166
167 static struct keyword keywords[] = {
168         { "ACCELERATORS",       tACCELERATORS,          0, 0, 0},
169         { "ALT",                tALT,                   0, 0, 0},
170         { "ASCII",              tASCII,                 0, 0, 0},
171         { "AUTO3STATE",         tAUTO3STATE,            1, 0, 0},
172         { "AUTOCHECKBOX",       tAUTOCHECKBOX,          1, 0, 0},
173         { "AUTORADIOBUTTON",    tAUTORADIOBUTTON,       1, 0, 0},
174         { "BEGIN",              tBEGIN,                 0, 0, 0},
175         { "BITMAP",             tBITMAP,                0, 0, 0},
176         { "BLOCK",              tBLOCK,                 0, 0, 0},
177         { "BUTTON",             tBUTTON,                1, 0, 0},
178         { "CAPTION",            tCAPTION,               0, 0, 0},
179         { "CHARACTERISTICS",    tCHARACTERISTICS,       1, 0, 0},
180         { "CHECKBOX",           tCHECKBOX,              0, 0, 0},
181         { "CHECKED",            tCHECKED,               0, 0, 0},
182         { "CLASS",              tCLASS,                 0, 0, 0},
183         { "COMBOBOX",           tCOMBOBOX,              0, 0, 0},
184         { "CONTROL",            tCONTROL,               0, 0, 0},
185         { "CTEXT",              tCTEXT,                 0, 0, 0},
186         { "CURSOR",             tCURSOR,                0, 0, 0},
187         { "DEFPUSHBUTTON",      tDEFPUSHBUTTON,         0, 0, 0},
188         { "DIALOG",             tDIALOG,                0, 0, 0},
189         { "DIALOGEX",           tDIALOGEX,              1, 0, 0},
190         { "DISCARDABLE",        tDISCARDABLE,           0, 0, 0},
191         { "DLGINIT",            tDLGINIT,               0, 0, 0},
192         { "EDITTEXT",           tEDITTEXT,              0, 0, 0},
193         { "END",                tEND,                   0, 0, 0},
194         { "EXSTYLE",            tEXSTYLE,               0, 0, 0},
195         { "FILEFLAGS",          tFILEFLAGS,             0, 0, 0},
196         { "FILEFLAGSMASK",      tFILEFLAGSMASK,         0, 0, 0},
197         { "FILEOS",             tFILEOS,                0, 0, 0},
198         { "FILESUBTYPE",        tFILESUBTYPE,           0, 0, 0},
199         { "FILETYPE",           tFILETYPE,              0, 0, 0},
200         { "FILEVERSION",        tFILEVERSION,           0, 0, 0},
201         { "FIXED",              tFIXED,                 0, 0, 0},
202         { "FONT",               tFONT,                  0, 0, 0},
203         { "FONTDIR",            tFONTDIR,               0, 0, 0},       /* This is a Borland BRC extension */
204         { "GRAYED",             tGRAYED,                0, 0, 0},
205         { "GROUPBOX",           tGROUPBOX,              0, 0, 0},
206         { "HELP",               tHELP,                  0, 0, 0},
207         { "ICON",               tICON,                  0, 0, 0},
208         { "IMPURE",             tIMPURE,                0, 0, 0},
209         { "INACTIVE",           tINACTIVE,              0, 0, 0},
210         { "LANGUAGE",           tLANGUAGE,              1, 0, 1},
211         { "LISTBOX",            tLISTBOX,               0, 0, 0},
212         { "LOADONCALL",         tLOADONCALL,            0, 0, 0},
213         { "LTEXT",              tLTEXT,                 0, 0, 0},
214         { "MENU",               tMENU,                  0, 0, 0},
215         { "MENUBARBREAK",       tMENUBARBREAK,          0, 0, 0},
216         { "MENUBREAK",          tMENUBREAK,             0, 0, 0},
217         { "MENUEX",             tMENUEX,                1, 0, 0},
218         { "MENUITEM",           tMENUITEM,              0, 0, 0},
219         { "MESSAGETABLE",       tMESSAGETABLE,          1, 0, 0},
220         { "MOVEABLE",           tMOVEABLE,              0, 0, 0},
221         { "NOINVERT",           tNOINVERT,              0, 0, 0},
222         { "NOT",                tNOT,                   0, 0, 0},
223         { "POPUP",              tPOPUP,                 0, 0, 0},
224         { "PRELOAD",            tPRELOAD,               0, 0, 0},
225         { "PRODUCTVERSION",     tPRODUCTVERSION,        0, 0, 0},
226         { "PURE",               tPURE,                  0, 0, 0},
227         { "PUSHBUTTON",         tPUSHBUTTON,            0, 0, 0},
228         { "RADIOBUTTON",        tRADIOBUTTON,           0, 0, 0},
229         { "RCDATA",             tRCDATA,                0, 0, 0},
230         { "RTEXT",              tRTEXT,                 0, 0, 0},
231         { "SCROLLBAR",          tSCROLLBAR,             0, 0, 0},
232         { "SEPARATOR",          tSEPARATOR,             0, 0, 0},
233         { "SHIFT",              tSHIFT,                 0, 0, 0},
234         { "STATE3",             tSTATE3,                1, 0, 0},
235         { "STRING",             tSTRING,                0, 0, 0},
236         { "STRINGTABLE",        tSTRINGTABLE,           0, 0, 1},
237         { "STYLE",              tSTYLE,                 0, 0, 0},
238         { "TOOLBAR",            tTOOLBAR,               1, 0, 0},
239         { "VALUE",              tVALUE,                 0, 0, 0},
240         { "VERSION",            tVERSION,               1, 0, 0},
241         { "VERSIONINFO",        tVERSIONINFO,           0, 0, 0},
242         { "VIRTKEY",            tVIRTKEY,               0, 0, 0}
243 };
244
245 #define NKEYWORDS       (sizeof(keywords)/sizeof(keywords[0]))
246 #define KWP(p)          ((const struct keyword *)(p))
247 static int kw_cmp_func(const void *s1, const void *s2)
248 {
249         int ret;
250         ret = strcasecmp(KWP(s1)->keyword, KWP(s2)->keyword);
251         if(!ret && (KWP(s1)->needcase || KWP(s2)->needcase))
252                 return strcmp(KWP(s1)->keyword, KWP(s2)->keyword);
253         else
254                 return ret;
255 }
256
257 #define KW_BSEARCH
258 #define DO_SORT
259 static struct keyword *iskeyword(char *kw)
260 {
261         struct keyword *kwp;
262         struct keyword key;
263         key.keyword = kw;
264         key.needcase = 0;
265 #ifdef DO_SORT
266         {
267                 /* Make sure that it is sorted for bsearsh */
268                 static int sorted = 0;
269                 if(!sorted)
270                 {
271                         qsort(keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
272                         sorted = 1;
273                 }
274         }
275 #endif
276 #ifdef KW_BSEARCH
277         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
278 #else
279         {
280                 int i;
281                 for(i = 0; i < NKEYWORDS; i++)
282                 {
283                         if(!kw_cmp_func(&key, &keywords[i]))
284                                 break;
285                 }
286                 if(i < NKEYWORDS)
287                         kwp = &keywords[i];
288                 else
289                         kwp = NULL;
290         }
291 #endif
292
293         if(kwp == NULL || (kwp->isextension && !extensions))
294                 return NULL;
295         else
296                 return kwp;
297 }
298
299 %}
300
301 /*
302  **************************************************************************
303  * The flexer starts here
304  **************************************************************************
305  */
306 %%
307         /*
308          * Catch the GCC-style line statements here and parse them.
309          * This has the advantage that you can #include at any
310          * stage in the resource file.
311          * The preprocessor generates line directives in the format:
312          * # <linenum> "filename" <codes>
313          *
314          * Codes can be a sequence of:
315          * - 1 start of new file
316          * - 2 returning to previous
317          * - 3 system header
318          * - 4 interpret as C-code
319          *
320          * 4 is not used and 1 mutually excludes 2
321          * Anyhow, we are not really interested in these at all
322          * because we only want to know the linenumber and
323          * filename.
324          */
325 <INITIAL,pp_strips,pp_stripp>^{ws}*\#{ws}*pragma{ws}+   yy_push_state(pp_pragma);
326 <INITIAL,pp_strips,pp_stripp>^{ws}*\#{ws}*      yy_push_state(pp_line);
327 <pp_line>[^\n]* {
328                 int lineno;
329                 char *cptr;
330                 char *fname;
331                 yy_pop_state();
332                 lineno = (int)strtol(yytext, &cptr, 10);
333                 if(!lineno)
334                         yyerror("Malformed '#...' line-directive; invalid linenumber");
335                 fname = strchr(cptr, '"');
336                 if(!fname)
337                         yyerror("Malformed '#...' line-directive; missing filename");
338                 fname++;
339                 cptr = strchr(fname, '"');
340                 if(!cptr)
341                         yyerror("Malformed '#...' line-directive; missing terminating \"");
342                 *cptr = '\0';
343                 line_number = lineno - 1;       /* We didn't read the newline */
344                 input_name = xstrdup(fname);
345         }
346
347 <pp_pragma>code_page[^\n]*      yyless(9); yy_pop_state(); yy_push_state(pp_code_page);
348 <pp_pragma>[^\n]*               yy_pop_state(); if (pedantic) yywarning("Unrecognized #pragma directive '%s'",yytext);
349
350 <pp_code_page>\({ws}*default{ws}*\)[^\n]*       current_codepage = -1; yy_pop_state();
351 <pp_code_page>\({ws}*[0-9]+{ws}*\)[^\n]* {
352         char *p = yytext;
353         yy_pop_state();
354         while (*p < '0' || *p > '9') p++;
355         current_codepage = strtol( p, NULL, 10 );
356         if (current_codepage && !wine_cp_get_table( current_codepage ))
357         {
358             yyerror("Codepage %d not supported", current_codepage);
359             current_codepage = 0;
360         }
361     }
362 <pp_code_page>[^\n]*    yy_pop_state(); yyerror("Malformed #pragma code_page directive");
363
364         /*
365          * Strip everything until a ';' taking
366          * into account braces {} for structures,
367          * classes and enums.
368          */
369 <pp_strips>\{                   stripslevel++;
370 <pp_strips>\}                   stripslevel--;
371 <pp_strips>;                    if(!stripslevel) yy_pop_state();
372 <pp_strips>\/[^*\n]             ; /* To catch comments */
373 <pp_strips>[^\{\};\n#/]*        ; /* Ignore rest */
374 <pp_strips>\n                   line_number++; char_number = 1;
375
376 <pp_stripp>\(                   stripplevel++;
377 <pp_stripp>\)                   {
378                                         stripplevel--;
379                                         if(!stripplevel)
380                                         {
381                                                 yy_pop_state();
382                                                 yy_push_state(pp_stripp_final);
383                                         }
384                                 }
385 <pp_stripp>\/[^*\n]             ; /* To catch comments */
386 <pp_stripp>[^\(\);\n#/]*        ; /* Ignore rest */
387 <pp_stripp>\n                   line_number++; char_number = 1;
388
389 <pp_stripp_final>{ws}*          ; /* Ignore */
390 <pp_stripp_final>;              yy_pop_state(); /* Kill the semicolon */
391 <pp_stripp_final>\n             line_number++; char_number = 1; yy_pop_state();
392 <pp_stripp_final>.              yyless(0); yy_pop_state();
393
394 \{                      return tBEGIN;
395 \}                      return tEND;
396
397 [0-9]+[lL]?             { yylval.num = strtoul(yytext,  0, 10); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
398 0[xX][0-9A-Fa-f]+[lL]?  { yylval.num = strtoul(yytext,  0, 16); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
399 0[oO][0-7]+[lL]?        { yylval.num = strtoul(yytext+2, 0, 8); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
400
401         /*
402          * The next two rules scan identifiers and filenames.
403          * This is achieved by using the priority ruling
404          * of the scanner where a '.' is valid in a filename
405          * and *only* in a filename. In this case, the second
406          * rule will be reduced because it is longer.
407          */
408 [A-Za-z_0-9]+           {
409                                 struct keyword *tok = iskeyword(yytext);
410
411                                 if(tok)
412                                 {
413                                         if(wanted_id && !tok->alwayskw)
414                                         {
415                                                 yylval.str = make_string(yytext);
416                                                 return tIDENT;
417                                         }
418                                         else
419                                                 return tok->token;
420                                 }
421                                 else
422                                 {
423                                         yylval.str = make_string(yytext);
424                                         return tIDENT;
425                                 }
426                         }
427 [A-Za-z_0-9./\\]+               yylval.str = make_string(yytext); return tFILENAME;
428
429         /*
430          * Wide string scanning
431          */
432 L\"                     {
433                                 yy_push_state(yylstr);
434                                 wbufidx = 0;
435                                 if(!win32)
436                                         yywarning("16bit resource contains unicode strings\n");
437                         }
438 <yylstr>\"{ws}+ |
439 <yylstr>\"              {
440                                 yy_pop_state();
441                                 yylval.str = get_buffered_wstring();
442                                 return tSTRING;
443                         }
444 <yylstr>\\[0-7]{1,6}    { /* octal escape sequence */
445                                 unsigned int result;
446                                 result = strtoul(yytext+1, 0, 8);
447                                 if ( result > 0xffff )
448                                         yyerror("Character constant out of range");
449                                 addwchar((WCHAR)result);
450                         }
451 <yylstr>\\x[0-9a-fA-F]{4} {  /* hex escape sequence */
452                                 unsigned int result;
453                                 result = strtoul(yytext+2, 0, 16);
454                                 addwchar((WCHAR)result);
455                         }
456 <yylstr>\\x[0-9a-fA-F]{1,3} {  yyerror("Invalid hex escape sequence '%s'", yytext); }
457
458 <yylstr>\\[0-9]+        yyerror("Bad escape sequence");
459 <yylstr>\\\n{ws}*       line_number++; char_number = 1; /* backslash at EOL continues string after leading whitespace on next line */
460 <yylstr>\\a             addwchar('\a');
461 <yylstr>\\b             addwchar('\b');
462 <yylstr>\\f             addwchar('\f');
463 <yylstr>\\n             addwchar('\n');
464 <yylstr>\\r             addwchar('\r');
465 <yylstr>\\t             addwchar('\t');
466 <yylstr>\\v             addwchar('\v');
467 <yylstr>\\.             addwchar(yytext[1]);
468 <yylstr>\\\r\n          addwchar(yytext[2]); line_number++; char_number = 1;
469 <yylstr>\"\"            addwchar('\"');         /* "bla""bla"  -> "bla\"bla" */
470 <yylstr>\\\"\"          addwchar('\"');         /* "bla\""bla" -> "bla\"bla" */
471 <yylstr>\"{ws}+\"       ;                       /* "bla" "bla" -> "blabla" */
472 <yylstr>[^\\\n\"]+      {
473                                 char *yptr = yytext;
474                                 while(*yptr)    /* FIXME: codepage translation */
475                                         addwchar(*yptr++ & 0xff);
476                         }
477 <yylstr>\n              yyerror("Unterminated string");
478
479         /*
480          * Normal string scanning
481          */
482 \"                      yy_push_state(yystr); cbufidx = 0;
483 <yystr>\"{ws}+  |
484 <yystr>\"               {
485                                 yy_pop_state();
486                                 yylval.str = get_buffered_cstring();
487                                 return tSTRING;
488                         }
489 <yystr>\\[0-7]{1,3}     { /* octal escape sequence */
490                                 int result;
491                                 result = strtol(yytext+1, 0, 8);
492                                 if ( result > 0xff )
493                                         yyerror("Character constant out of range");
494                                 addcchar((char)result);
495                         }
496 <yystr>\\x[0-9a-fA-F]{2} {  /* hex escape sequence */
497                                 int result;
498                                 result = strtol(yytext+2, 0, 16);
499                                 addcchar((char)result);
500                         }
501 <yystr>\\x[0-9a-fA-F]   {  yyerror("Invalid hex escape sequence '%s'", yytext); }
502
503 <yystr>\\[0-9]+         yyerror("Bad escape sequence");
504 <yystr>\\\n{ws}*        line_number++; char_number = 1; /* backslash at EOL continues string after leading whitespace on next line */
505 <yystr>\\a              addcchar('\a');
506 <yystr>\\b              addcchar('\b');
507 <yystr>\\f              addcchar('\f');
508 <yystr>\\n              addcchar('\n');
509 <yystr>\\r              addcchar('\r');
510 <yystr>\\t              addcchar('\t');
511 <yystr>\\v              addcchar('\v');
512 <yystr>\\.              addcchar(yytext[1]);
513 <yystr>\\\r\n           addcchar(yytext[2]); line_number++; char_number = 1;
514 <yystr>[^\\\n\"]+       {
515                                 char *yptr = yytext;
516                                 while(*yptr)
517                                         addcchar(*yptr++);
518                         }
519 <yystr>\"\"             addcchar('\"');         /* "bla""bla"   -> "bla\"bla" */
520 <yystr>\\\"\"           addcchar('\"');         /* "bla\""bla"  -> "bla\"bla" */
521 <yystr>\"{ws}+\"        ;                       /* "bla" "bla"  -> "blabla" */
522 <yystr>\n               yyerror("Unterminated string");
523
524         /*
525          * Raw data scanning
526          */
527 \'                      yy_push_state(yyrcd); cbufidx = 0;
528 <yyrcd>\'               {
529                                 yy_pop_state();
530                                 yylval.raw = new_raw_data();
531                                 yylval.raw->size = cbufidx;
532                                 yylval.raw->data = xmalloc(yylval.raw->size);
533                                 memcpy(yylval.raw->data, cbuffer, yylval.raw->size);
534                                 return tRAWDATA;
535                         }
536 <yyrcd>[0-9a-fA-F]{2}   {
537                                 int result;
538                                 result = strtol(yytext, 0, 16);
539                                 addcchar((char)result);
540                         }
541 <yyrcd>{ws}+            ;       /* Ignore space */
542 <yyrcd>\n               line_number++; char_number = 1;
543 <yyrcd>.                yyerror("Malformed data-line");
544
545         /*
546          * Comment stripping
547          * Should never occur after preprocessing
548          */
549 <INITIAL,pp_stripp,pp_strips>"/*"       {
550                                 yy_push_state(comment);
551                                 save_wanted_id = wanted_id;
552                                 if(!no_preprocess)
553                                         yywarning("Found comments after preprocessing, please report");
554                         }
555 <comment>[^*\n]*        ;
556 <comment>"*"+[^*/\n]*   ;
557 <comment>\n             line_number++; char_number = 1;
558 <comment>"*"+"/"        yy_pop_state(); want_id = save_wanted_id;
559
560 ;[^\n]*                 want_id = wanted_id; /* not really comment, but left-over c-junk */
561 "//"[^\n]*              want_id = wanted_id; if(!no_preprocess) yywarning("Found comments after preprocessing, please report");
562
563 \n                      {
564                                 want_id = wanted_id;
565                                 line_number++;
566                                 char_number = 1;
567                                 if(want_nl)
568                                 {
569                                         want_nl = 0;
570                                         return tNL;
571                                 }
572                         }
573 {ws}+                   want_id = wanted_id;    /* Eat whitespace */
574
575 <INITIAL>.              return yytext[0];
576
577 <<EOF>>                 {
578                                 if(YY_START == pp_strips || YY_START == pp_stripe || YY_START == pp_stripp || YY_START == pp_stripp_final)
579                                         yyerror("Unexpected end of file during c-junk scanning (started at %d)", cjunk_tagline);
580                                 else
581                                         yyterminate();
582                         }
583
584 <*>.|\n                 {
585                                 /* Catch all rule to find any unmatched text */
586                                 if(*yytext == '\n')
587                                 {
588                                         line_number++;
589                                         char_number = 1;
590                                 }
591                                 yywarning("Unmatched text '%c' (0x%02x) YY_START=%d stripslevel=%d",
592                                         isprint(*yytext & 0xff) ? *yytext : '.', *yytext, YY_START,stripslevel);
593                         }
594
595 %%
596
597 #ifndef yywrap
598 int yywrap(void)
599 {
600 #if 0
601         if(bufferstackidx > 0)
602         {
603                 return 0;
604         }
605 #endif
606         return 1;
607 }
608 #endif
609
610 /* These dup functions copy the enclosed '\0' from
611  * the resource string.
612  */
613 static void addcchar(char c)
614 {
615         if(cbufidx >= cbufalloc)
616         {
617                 cbufalloc += 1024;
618                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
619                 if(cbufalloc > 65536)
620                         yywarning("Reallocating string buffer larger than 64kB");
621         }
622         cbuffer[cbufidx++] = c;
623 }
624
625 static void addwchar(WCHAR s)
626 {
627         if(wbufidx >= wbufalloc)
628         {
629                 wbufalloc += 1024;
630                 wbuffer = xrealloc(wbuffer, wbufalloc * sizeof(wbuffer[0]));
631                 if(wbufalloc > 65536)
632                         yywarning("Reallocating wide string buffer larger than 64kB");
633         }
634         wbuffer[wbufidx++] = s;
635 }
636
637 static string_t *get_buffered_cstring(void)
638 {
639     string_t *str = new_string();
640
641     str->size = cbufidx;
642     str->type = str_char;
643     str->str.cstr = (char *)xmalloc(cbufidx+1);
644     memcpy(str->str.cstr, cbuffer, cbufidx);
645     str->str.cstr[cbufidx] = '\0';
646
647     if (!current_codepage || current_codepage == -1 || !win32)  /* store as ANSI string */
648     {
649         if (!current_codepage) yyerror("Codepage set to Unicode only, cannot use ASCII string here");
650         return str;
651     }
652     else  /* convert to Unicode before storing */
653     {
654         string_t *str_w = convert_string( str, str_unicode, current_codepage );
655         if (!check_unicode_conversion( str, str_w, current_codepage ))
656             yyerror("String %s does not convert identically to Unicode and back in codepage %d. "
657                     "Try using a Unicode string instead.", str->str.cstr, current_codepage );
658         free_string( str );
659         return str_w;
660     }
661 }
662
663 static string_t *get_buffered_wstring(void)
664 {
665         string_t *str = new_string();
666         str->size = wbufidx;
667         str->type = str_unicode;
668         str->str.wstr = xmalloc((wbufidx+1)*sizeof(WCHAR));
669         memcpy(str->str.wstr, wbuffer, wbufidx*sizeof(WCHAR));
670         str->str.wstr[wbufidx] = 0;
671         return str;
672 }
673
674 static string_t *make_string(char *s)
675 {
676         string_t *str = new_string();
677         str->size = strlen(s);
678         str->type = str_char;
679         str->str.cstr = (char *)xmalloc(str->size+1);
680         memcpy(str->str.cstr, s, str->size+1);
681         return str;
682 }