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