Error reporting fix.
[wine.git] / tools / wrc / parser.l
blobf3e176e03b48ceb2bd460c2df523f53112462ee5
1 /* -*-C-*-
2  *
3  * Copyright 1994 Martin von Loewis
4  * Copyright 1998 Bertho A. Stultiens (BS)
5  *
6  * 20-Jun-1998 BS       - Changed the filename conversion. Filenames are
7  *                        case-sensitive inder *nix, but not under dos.
8  *                        default behaviour is to convert to lower case.
9  *                      - All backslashes are converted to forward and
10  *                        both single and double slash is recognized as
11  *                        MS/Borland does.
12  *                      - Fixed a bug in 'yywf' case that prevented
13  *                        double quoted names to be scanned propperly.
14  *
15  * 19-May-1998 BS       - Started to build a preprocessor.
16  *                      - Changed keyword processing completely to
17  *                        table-lookups.
18  *
19  * 20-Apr-1998 BS       - Added ';' comment stripping
20  *
21  * 17-Apr-1998 BS       - Made the win32 keywords optional when compiling in
22  *                        16bit mode
23  *
24  * 15-Apr-1998 BS       - Changed string handling to include escapes
25  *                      - Added unicode string handling (no codepage
26  *                        translation though).
27  *                      - 'Borrowed' the main idea of string scanning from
28  *                        the flex manual pages.
29  *                      - Added conditional handling of scanning depending
30  *                        on the state of the parser. This was mainly required
31  *                        to distinguish a file to load or raw data that
32  *                        follows. MS's definition of filenames is rather
33  *                        complex... It can be unquoted or double quoted. If
34  *                        double quoted, then the '\\' char is not automatically
35  *                        escaped according to Borland's rc compiler, but it
36  *                        accepts both "\\path\\file.rc" and "\path\file.rc".
37  *                        This makes life very hard! I go for the escaped
38  *                        version, as this seems to be the documented way...
39  *                      - Single quoted strings are now parsed and converted
40  *                        here.
41  *                      - Added comment stripping. The implementation is
42  *                        'borrowed' from the flex manpages.
43  *                      - Rebuild string processing so that it may contain
44  *                        escaped '\0'.
45  */
47 /* Exclusive rules when looking for a filename */
48 %x yywf
49 %x yywf_s
50 /* Exclusive string handling */
51 %x yystr
52 /* Exclusive unicode string handling */
53 %x yylstr
54 /* Exclusive rcdata single quoted data handling */
55 %x yyrcd
56 /* Exclusive comment eating... */
57 %x comment
58 /* Preprocessor exclusives */
59 %x pp_incl
60 %x pp_def
61 %x pp_undef
62 %x pp_if
63 %x pp_ifdef
64 %x pp_ifndef
65 %x pp_elif
66 %x pp_else
67 %x pp_endif
68 %x pp_error
69 /* Set when accumulating #define's expansion text */
70 %x pp_def_s
71 /* Set when processing function type defines */
72 %x pp_ignore
73 /* Set when need to strip to eol */
74 %x pp_ignore_eol
75 /* Set when handling a false #if case */
76 %x pp_false
77 /* Set when stripping c-junk */
78 %x pp_strips
79 %x pp_stripp
80 %x pp_stripp_final
82 /*%option stack*/
83 %option never-interactive
84 /*%option noyywrap */
85 /* Some shortcut definitions */
86 ws      [ \f\t\r]
87 cident  [a-zA-Z_][0-9a-zA-Z_]*
91 #if !defined(YY_FLEX_MAJOR_VERSION) || (1000 * YY_FLEX_MAJOR_VERSION + YY_FLEX_MINOR_VERSION < 2005)
92 #error Must use flex version 2.5.1 or higher (yy_scan_* routines are required).
93 #endif
95 /*#define LEX_DEBUG*/
97 #include "config.h"
99 #include <stdio.h>
100 #include <stdlib.h>
101 #include <string.h>
102 #include <ctype.h>
104 #include "wrc.h"
105 #include "utils.h"
106 #include "preproc.h"
107 #include "parser.h"
108 #include "newstruc.h"
110 #include "y.tab.h"
112 #define YY_USE_PROTOS
113 #define YY_NO_UNPUT
115 /* Always update the current character position within a line */
116 #define YY_USER_ACTION  char_number+=yyleng;
118 raw_data_t *new_raw_data(void);
120 void addcchar(char c);
121 void addwchar(short s);
122 string_t *get_buffered_cstring(void);
123 string_t *get_buffered_wstring(void);
124 string_t *make_string(char *s);
125 string_t *make_filename(char *s, int len);
127 int line_number = 1;            /* The current line */
128 int char_number = 1;            /* The current char pos within the line */
129 static char *cbuffer;           /* Buffers for string collection */
130 static int cbufidx;
131 static int cbufalloc = 0;
132 static short *wbuffer;
133 static int wbufidx;
134 static int wbufalloc = 0;
135 static int want_nl = 0;         /* Set when newline needs to go to parser */
136 static int want_ident = 0;      /* Set is #ifdef, #ifndef or defined is seen */
137 static int stripslevel = 0;     /* Count {} during pp_strips mode */
138 static int stripplevel = 0;     /* Count () during pp_strips mode */
139 static char *substtext = NULL;  /* Holds the substition text while getting a define */
140 static int cjunk_tagline;       /* Where did we start stripping (helps error tracking) */
142 #ifdef YY_USE_STACK
143 void push_to(int start) { yy_push_state(start); }
144 void pop_start(void)    { yy_pop_state(start); }
145 #else
146 #define MAXSTARTSTACK   32
147 static int startstack[MAXSTARTSTACK];
148 static int startstackidx = 0;
150 void push_to(int start)
152         if(yydebug)
153                 printf("push_to(%d): %d -> %d\n", line_number, YY_START, start);
154         if(startstackidx >= MAXSTARTSTACK-1)
155                 internal_error(__FILE__, __LINE__, "Start condition stack overflow");
156         startstack[startstackidx++] = YY_START;
157         BEGIN(start);
160 void pop_start(void)
162         if(yydebug)
163                 printf("pop_start(%d): %d <- %d\n", line_number, startstack[startstackidx-1], YY_START);
164         if(startstackidx <= 0)
165                 internal_error(__FILE__, __LINE__, "Start condition stack underflow");
166         --startstackidx;
167         BEGIN(startstack[startstackidx]);
169 #endif
172 struct bufferstackentry {
173         YY_BUFFER_STATE bufferstate;    /* Buffer to switch back to */
174         struct pp_entry *define;        /* Points to expanding define
175                                            or NULL if handling includes
176                                          */
177         int line_number;                /* Line that we were handling */
178         int char_number;                /* The current position */
179         char *filename;                 /* Filename that we were handling */
182 #define MAXBUFFERSTACK 128
183 static struct bufferstackentry bufferstack[MAXBUFFERSTACK];
184 static int bufferstackidx = 0;
186 void push_buffer(YY_BUFFER_STATE buf, struct pp_entry *ppp, char *filename)
188         if(yydebug)
189                 printf("push_buffer: %p %p %p\n", buf, ppp, filename);
190         if(bufferstackidx >= MAXBUFFERSTACK-1)
191                 internal_error(__FILE__, __LINE__, "Buffer stack overflow");
192         memset(&bufferstack[bufferstackidx], 0, sizeof(bufferstack[0]));
193         bufferstack[bufferstackidx].bufferstate = buf;
194         bufferstack[bufferstackidx].define = ppp;
195         if(ppp)
196                 ppp->expanding = 1;
197         else if(filename)
198         {
199                 /* These will track the yyerror to the correct file and line */
200                 bufferstack[bufferstackidx].line_number = line_number;
201                 bufferstack[bufferstackidx].char_number = char_number;
202                 line_number = 1;
203                 char_number = 1;
204                 bufferstack[bufferstackidx].filename = input_name;
205                 input_name = filename;
206         }
207         else
208                 internal_error(__FILE__, __LINE__, "Pushing buffer without knowing where to go to");
209         bufferstackidx++;
212 YY_BUFFER_STATE pop_buffer(void)
214         if(bufferstackidx <= 0)
215                 return (YY_BUFFER_STATE)0;
216         bufferstackidx--;
217         if(bufferstack[bufferstackidx].define)
218                 bufferstack[bufferstackidx].define->expanding = 0;
219         else
220         {
221                 line_number = bufferstack[bufferstackidx].line_number;
222                 char_number = bufferstack[bufferstackidx].char_number;
223                 input_name = bufferstack[bufferstackidx].filename;
224                 fclose(yyin);
225         }
226         if(yydebug)
227                 printf("pop_buffer: %p %p (%d, %d) %p\n",
228                         bufferstack[bufferstackidx].bufferstate,
229                         bufferstack[bufferstackidx].define,
230                         bufferstack[bufferstackidx].line_number,
231                         bufferstack[bufferstackidx].char_number,
232                         bufferstack[bufferstackidx].filename);
233         yy_switch_to_buffer(bufferstack[bufferstackidx].bufferstate);
234         return bufferstack[bufferstackidx].bufferstate;
237 void do_include(char *name, int namelen)
239         char *cpy = (char *)xmalloc(namelen);
240         strcpy(cpy, name+1);    /* strip leading " or < */
241         cpy[namelen-2] = '\0';  /* strip trailing " or > */
242         if((yyin = open_include(cpy, name[0] == '"')) == NULL)
243                 yyerror("Unable to open include file %s", cpy);
244         push_buffer(YY_CURRENT_BUFFER, NULL, cpy);
245         yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
249 struct keyword {
250         char    *keyword;
251         int     token;
252         int     isextension;
253         int     needcase;
254         int     alwayskeyword;
257 static struct keyword keywords[] = {
258         { "ACCELERATORS", ACCELERATORS, 0, 0, 0},
259         { "ALT", ALT, 0, 0, 0},
260         { "ASCII", ASCII, 0, 0, 0},
261         { "AUTO3STATE", AUTO3STATE, 1, 0, 0},
262         { "AUTOCHECKBOX", AUTOCHECKBOX, 1, 0, 0},
263         { "AUTORADIOBUTTON", AUTORADIOBUTTON, 1, 0, 0},
264         { "BEGIN", tBEGIN, 0, 0, 1},
265         { "BITMAP", tBITMAP, 0, 0, 0},
266         { "BLOCK", BLOCK, 0, 0, 1},
267         { "BUTTON", BUTTON, 1, 0, 0},
268         { "CAPTION", CAPTION, 0, 0, 0},
269         { "CHARACTERISTICS", CHARACTERISTICS, 1, 0, 0},
270         { "CHECKBOX", CHECKBOX, 0, 0, 0},
271         { "CHECKED", CHECKED, 0, 0, 0},
272         { "CLASS", CLASS, 0, 0, 0},
273         { "COMBOBOX", COMBOBOX, 0, 0, 0},
274         { "CONTROL", CONTROL, 0, 0, 0},
275         { "CTEXT", CTEXT, 0, 0, 0},
276         { "CURSOR", CURSOR, 0, 0, 0},
277         { "defined", tDEFINED, 0, 1, 1},
278         { "DEFPUSHBUTTON", DEFPUSHBUTTON, 0, 0, 1},
279         { "DIALOG", DIALOG, 0, 0, 0},
280         { "DIALOGEX", DIALOGEX, 1, 0, 0},
281         { "DISCARDABLE", DISCARDABLE, 0, 0, 0},
282         { "DLGINIT", DLGINIT, 0, 0, 0},
283         { "EDITTEXT", EDITTEXT, 0, 0, 0},
284         { "END", tEND, 0, 0, 1},
285         { "EXSTYLE", EXSTYLE, 0, 0, 0},
286         { "extern", tEXTERN, 0, 1, 1},
287         { "FILEFLAGS", FILEFLAGS, 0, 0, 0},
288         { "FILEFLAGSMASK", FILEFLAGSMASK, 0, 0, 0},
289         { "FILEOS", FILEOS, 0, 0, 0},
290         { "FILESUBTYPE", FILESUBTYPE, 0, 0, 0},
291         { "FILETYPE", FILETYPE, 0, 0, 0},
292         { "FILEVERSION", FILEVERSION, 0, 0, 0},
293         { "FIXED", tFIXED, 0, 0, 0},
294         { "FONT", FONT, 0, 0, 0},
295         { "GRAYED", GRAYED, 0, 0, 0},
296         { "GROUPBOX", GROUPBOX, 0, 0, 0},
297         { "HELP", HELP, 0, 0, 0},
298         { "ICON", ICON, 0, 0, 0},
299         { "IMPURE", IMPURE, 0, 0, 0},
300         { "INACTIVE", INACTIVE, 0, 0, 0},
301         { "LANGUAGE", LANGUAGE, 1, 0, 1},
302         { "LISTBOX", LISTBOX, 0, 0, 0},
303         { "LOADONCALL", LOADONCALL, 0, 0, 0},
304         { "LTEXT", LTEXT, 0, 0, 0},
305         { "MENU", MENU, 0, 0, 0},
306         { "MENUBARBREAK", MENUBARBREAK, 0, 0, 0},
307         { "MENUBREAK", MENUBREAK, 0, 0, 0},
308         { "MENUEX", MENUEX, 1, 0, 0},
309         { "MENUITEM", MENUITEM, 0, 0, 0},
310         { "MESSAGETABLE", MESSAGETABLE, 1, 0, 0},
311         { "MOVEABLE", MOVEABLE, 0, 0, 0},
312         { "NOINVERT", NOINVERT, 0, 0, 0},
313         { "NOT", NOT, 0, 0, 0},
314         { "POPUP", POPUP, 0, 0, 0},
315         { "PRELOAD", PRELOAD, 0, 0, 0},
316         { "PRODUCTVERSION", PRODUCTVERSION, 0, 0, 0},
317         { "PURE", tPURE, 0, 0, 0},
318         { "PUSHBUTTON", PUSHBUTTON, 0, 0, 0},
319         { "RADIOBUTTON", RADIOBUTTON, 0, 0, 0},
320         { "RCDATA", RCDATA, 0, 0, 0},
321         { "RTEXT", RTEXT, 0, 0, 0},
322         { "SCROLLBAR", SCROLLBAR, 0, 0, 0},
323         { "SEPARATOR", SEPARATOR, 0, 0, 0},
324         { "SHIFT", SHIFT, 0, 0, 0},
325         { "STATE3", STATE3, 1, 0, 0},
326         { "STRING", tSTRING, 0, 0, 0},
327         { "STRINGTABLE", STRINGTABLE, 0, 0, 1},
328         { "STYLE", STYLE, 0, 0, 0},
329         { "TOOLBAR", TOOLBAR, 1, 0, 0},
330         { "typedef", tTYPEDEF, 0, 1, 1},
331         { "VALUE", VALUE, 0, 0, 0},
332         { "VERSION", VERSION, 1, 0, 0},
333         { "VERSIONINFO", VERSIONINFO, 0, 0, 0},
334         { "VIRTKEY", VIRTKEY, 0, 0, 0}
337 #define NKEYWORDS       (sizeof(keywords)/sizeof(keywords[0]))
338 #define KWP(p)          ((struct keyword *)(p))
339 int kw_cmp_func(const void *s1, const void *s2)
341         int ret;
342         ret = strcasecmp(KWP(s1)->keyword, KWP(s2)->keyword);
343         if(!ret && (KWP(s1)->needcase || KWP(s2)->needcase))
344                 return strcmp(KWP(s1)->keyword, KWP(s2)->keyword);
345         else
346                 return ret;
349 #define KW_BSEARCH
350 #define DO_SORT
351 struct keyword *iskeyword(char *kw)
353         struct keyword *kwp;
354         struct keyword key;
355         key.keyword = kw;
356         key.needcase = 0;
357 #ifdef DO_SORT
358         {
359                 /* Make sure that it is sorted for bsearsh */
360                 static int sorted = 0;
361                 if(!sorted)
362                 {
363                         qsort(keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
364                         sorted = 1;
365                 }
366         }
367 #endif
368 #ifdef KW_BSEARCH
369         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
370 #else
371         {
372                 int i;
373                 for(i = 0; i < NKEYWORDS; i++)
374                 {
375                         if(!kw_cmp_func(&key, &keywords[i]))
376                                 break;
377                 }
378                 if(i < NKEYWORDS)
379                         kwp = &keywords[i];
380                 else
381                         kwp = NULL;
382         }
383 #endif
385 #ifdef LEX_DEBUG
386         if(kwp && !strcmp(kwp->keyword, "LANGUAGE"))
387                 printf("Got Language\n");
388 #endif
389         if(kwp == NULL || (kwp->isextension && !extensions))
390                 return NULL;
391         else
392                 return kwp;
395 void add_to_substtext(char *text, int len)
397         if(!substtext)
398         {
399                 substtext = xstrdup(text);
400         }
401         else
402         {
403                 substtext = (char *)xrealloc(substtext, strlen(substtext)+len+1);
404                 strcat(substtext, text);
405         }
411         /* #include handling */
412 ^{ws}*#{ws}*include{ws}*        push_to(pp_incl);
413 <pp_incl>\<[^\n\>]+\>           do_include(yytext, yyleng); pop_start();
414 <pp_incl>\"[^\n\>]+\"           do_include(yytext, yyleng); pop_start();
415 <pp_incl>.              yyerror("Malformed #include");
417         /* #define handling */
418 ^{ws}*#{ws}*define{ws}* push_to(pp_def);
419 <pp_def>{cident}        {
420                                 set_define(yytext);
421                                 push_to(pp_def_s);
422                         }
423 <pp_def>{cident}\(      push_to(pp_ignore); /* Ignore function-like defines for now*/
424 <pp_def>.               yyerror("Malformed #define");
426 <pp_ignore,pp_def_s>[^\/\\\n]*  {
427                         if(YY_START == pp_def_s)
428                                 add_to_substtext(yytext, yyleng);
429                 }
430 <pp_ignore,pp_def_s>\/[^\/\*][^\/\\\n]* { /* Comment is handled in normal handling */
431                         if(YY_START == pp_def_s)
432                                 add_to_substtext(yytext, yyleng);
433                 }
434 <pp_ignore,pp_def_s>\\{ws}*\n   line_number++;  char_number = 1; /* Line continuation */
435 <pp_ignore,pp_def_s>\n          {
436                         if(YY_START == pp_def_s)
437                         {
438                                 add_define(substtext ? substtext : "");
439                                 free(substtext);
440                                 substtext = NULL;
441                         }
442                         line_number++;
443                         char_number = 1;
444                         pop_start();
445                         pop_start();
446                 }
448         /* #undef handling */
449 ^{ws}*#{ws}*undef{ws}*  push_to(pp_undef);
450 <pp_undef>{cident}      {
451                                 del_define(yytext);
452                                 pop_start();
453                                 /*push_to(pp_ignore);*/
454                         }
456         /* Conditional handling */
457 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*if{ws}*       {
458                         if(YY_START == pp_false)
459                         {
460                                 if(yydebug)
461                                         printf("(%d)#if ignored\n", line_number);
462                                 push_if(0, 0, 1);
463                                 push_to(pp_ignore_eol);
464                         }
465                         else
466                         {
467                                 push_to(INITIAL);
468                                 want_nl = 1;
469                                 return tIF;
470                         }
471                 }
472 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*ifdef{ws}*    {
473                         if(YY_START == pp_false)
474                         {
475                                 if(yydebug)
476                                         printf("(%d)#ifdef ignored\n", line_number);
477                                 push_if(0, 0, 1);
478                                 push_to(pp_ignore_eol);
479                         }
480                         else
481                         {
482                                 push_to(INITIAL);
483                                 want_nl = 1;
484                                 want_ident = 1;
485                                 return tIFDEF;
486                         }
487                 }
488 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*ifndef{ws}*   {
489                         if(YY_START == pp_false)
490                         {
491                                 if(yydebug)
492                                         printf("(%d)#ifndef ignored\n", line_number);
493                                 push_if(0, 0, 1);
494                                 push_to(pp_ignore_eol);
495                         }
496                         else
497                         {
498                                 push_to(INITIAL);
499                                 want_nl = 1;
500                                 want_ident = 1;
501                                 return tIFNDEF;
502                         }
503                 }
504 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*elif{ws}*     {
505                         if(!isnevertrue_if())
506                         {
507                                 push_to(INITIAL);
508                                 want_nl = 1;
509                                 return tELIF;
510                         }
511                         else if(YY_START == pp_false)
512                                 push_to(pp_ignore_eol);
513                         if(yydebug)
514                                 printf("(%d)#elif ignored\n", line_number);
515                 }
516 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*else{ws}*     {
517                         if(!isnevertrue_if())
518                         {
519                                 push_to(INITIAL);
520                                 want_nl = 1;
521                                 return tELSE;
522                         }
523                         if(yydebug)
524                                 printf("(%d)#else ignored\n", line_number);
525                 }
526 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*endif{ws}*    {
527                         if(!isnevertrue_if())
528                         {
529                                 want_nl = 1;
530                                 return tENDIF;
531                         }
532                         else
533                         {
534                                 if(yydebug)
535                                         printf("(%d)#endif ignored\n", line_number);
536                                 pop_if();
537                         }
538                 }
540         /* The error directive */
541 ^{ws}*#{ws}*error{ws}*  push_to(pp_error);
542 <pp_error>[^\n]*        yyerror("Error directive: %s", yytext);
544         /* preprocessor junk */
545 ^{ws}*#{ws}*pragma[^\n]*        ;       /* Ignore #pragma */
546 ^{ws}*#{ws}*line[^\n]*          ;       /* Ignore #line */
547  /* We'll get an error on malformed #xxx statements
548   * by not recognising '#' at all. This helps tracking
549   * preprocessor errors.
550   */
551  /*^{ws}*#{ws}*                 ;        Ignore # */
553 <pp_strips>\{           stripslevel++;
554 <pp_strips>\}           stripslevel--;
555 <pp_strips>;            if(!stripslevel) pop_start();
556 <pp_strips>\/[^*\n]             ; /* To catch comments */
557 <pp_strips>[^\{\};\n#/]*        ; /* Ignore rest */
559 <pp_stripp>\(           stripplevel++;
560 <pp_stripp>\)           {
561                                 stripplevel--;
562                                 if(!stripplevel)
563                                 {
564                                         pop_start();
565                                         push_to(pp_stripp_final);
566                                 }
567                         }
568 <pp_stripp>\/[^*\n]             ; /* To catch comments */
569 <pp_stripp>[^\(\);\n#/]*        ; /* Ignore rest */
571 <pp_stripp_final>{ws}*  ; /* Ignore */
572 <pp_stripp_final>;      pop_start(); /* Kill the semicolon */
573 <pp_stripp_final>\n     line_number++; char_number = 1; pop_start();
574 <pp_stripp_final>.      yyless(0); pop_start();
576 <pp_false>.             ;       /* Ignore everything except #xxx during false #if state */
578 <pp_ignore_eol>[^\n]*   pop_start();
580  /* These are special cases due to filename scanning */
581 <yywf>[Dd][Ii][Ss][Cc][Aa][Rr][Dd][Aa][Bb][Ll][Ee]      return DISCARDABLE;
582 <yywf>[Ff][Ii][Xx][Ee][Dd]                              return tFIXED;
583 <yywf>[Ii][Mm][Pp][Uu][Rr][Ee]                          return IMPURE;
584 <yywf>[Mm][Oo][Vv][Ee][Aa][Bb][Ll][Ee]                  return MOVEABLE;
585 <yywf>[Ll][Oo][Aa][Dd][Oo][Nn][Cc][Aa][Ll][Ll]          return LOADONCALL;
586 <yywf>[Pp][Rr][Ee][Ll][Oo][Aa][Dd]                      return PRELOAD;
587 <yywf>[Pp][Uu][Rr][Ee]                                  return tPURE;
589 \{                      return tBEGIN;
590 \}                      return tEND;
592 [0-9]+[lL]?             { yylval.num = strtoul(yytext,  0, 10); return toupper(yytext[yyleng-1]) == 'L' ? LNUMBER : NUMBER; }
593 0[xX][0-9A-Fa-f]+[lL]?  { yylval.num = strtoul(yytext,  0, 16); return toupper(yytext[yyleng-1]) == 'L' ? LNUMBER : NUMBER; }
594 0[oO][0-7]+[lL]?        { yylval.num = strtoul(yytext+2, 0, 8); return toupper(yytext[yyleng-1]) == 'L' ? LNUMBER : NUMBER; }
595 [A-Za-z_0-9]+           {
596                                 struct keyword *token;
597                                 struct pp_entry *ppp;
599                                 want_rscname = 0;
600                                 
601                                 if(want_ident)
602                                 {
603                                         /* Prevent preprocessor subst */
604                                         want_ident = 0;
605                                         yylval.str = make_string(yytext);
606                                 #ifdef LEX_DEBUG
607                                         printf("want IDENT (%s, %d, %d): <%s>\n", input_name, line_number, char_number, yytext);
608                                 #endif
609                                         return IDENT;
610                                 }
611                                 else if((ppp = pp_lookup(yytext)) != NULL)
612                                 {
613                                         /* Do preprocessor substitution,
614                                          * but expand only if macro is not
615                                          * already expanding.
616                                          */
617                                         if(!ppp->expanding)
618                                         {
619                                 #ifdef LEX_DEBUG
620                                                 printf("expand IDENT (%s, %d, %d): <%s>\n", input_name, line_number, char_number, yytext);
621                                 #endif
622                                                 push_buffer(YY_CURRENT_BUFFER, ppp, NULL);
623                                                 yy_scan_string(ppp->subst);
624                                         }
625                                 }
626                                 else if((token = iskeyword(yytext)) != NULL
627                                   && !(!token->alwayskeyword && want_rscname))
628                                 {
629                                         switch(token->token)
630                                         {
631                                         case tDEFINED:
632                                                 want_ident = 1;
633                                                 break;
634                                         /*case RCDATA:*/
635                                         case CURSOR:
636                                         case tBITMAP:
637                                         case MESSAGETABLE:
638                                         case DLGINIT:
639                                                 push_to(yywf);
640                                                 break;
641                                         case FONT:
642                                         case ICON:
643                                                 if(!indialog)
644                                                         push_to(yywf);
645                                                 break;
646                                         case DIALOG:
647                                         case DIALOGEX:
648                                                 indialog = 1;
649                                                 break;
650                                         }
651                                         return token->token;
652                                 }
653                                 else
654                                 {
655                                         yylval.str = make_string(yytext);
656                                 #ifdef LEX_DEBUG
657                                         printf("%s IDENT (%s, %d, %d): <%s>\n",
658                                                 want_rscname ? "rscname" : "just",
659                                                 input_name,
660                                                 line_number,
661                                                 char_number,
662                                                 yytext);
663                                 #endif
664                                         return IDENT;
665                                 }
666                         }
667 \|\|                    return LOGOR;
668 \&\&                    return LOGAND;
669 \=\=                    return EQ;
670 \!\=                    return NE;
671 \<\=                    return LTE;
672 \>\=                    return GTE;
674 <yywf>[^ \f\t\r\n\"]*   { pop_start(); yylval.str = make_filename(yytext, yyleng); return FILENAME; }
675 <yywf>\"                push_to(yywf_s);
676 <yywf_s>[^\"\n]*\"      { pop_start(); pop_start(); yylval.str = make_filename(yytext, yyleng-1); return FILENAME; }
677 <yywf_s>\n              yyerror("Newline in filename");
679 L\"                     {
680                                 push_to(yylstr);
681                                 wbufidx = 0;
682                                 if(!win32)
683                                         yywarning("16bit resource contains unicode strings\n");
684                         }
685 <yylstr>\"              {
686                                 pop_start();
687                                 yylval.str = get_buffered_wstring();
688                                 return tSTRING;
689                         }
690 <yylstr>\n              yyerror("Unterminated string");
691 <yylstr>\\[0-7]{1,6}    { /* octal escape sequence */
692                                 int result;
693                                 result = strtol(yytext+1, 0, 8);
694                                 if ( result > 0xffff )
695                                         yyerror("Character constant out of range");
696                                 addwchar((short)result);
697                         }
698 <yylstr>\\x[0-9a-fA-F]{4} {  /* hex escape sequence */
699                                 int result;
700                                 result = strtol(yytext+2, 0, 16);
701                                 addwchar((short)result);
702                         }
703 <yylstr>\\[0-9]+        yyerror("Bad escape secuence");
704 <yylstr>\\a             addwchar('\a');
705 <yylstr>\\b             addwchar('\b');
706 <yylstr>\\f             addwchar('\f');
707 <yylstr>\\n             addwchar('\n');
708 <yylstr>\\r             addwchar('\r');
709 <yylstr>\\t             addwchar('\t');
710 <yylstr>\\v             addwchar('\v');
711 <yylstr>\\(.|\n)        addwchar(yytext[1]);
712 <yylstr>\"\"            addcchar('\"');         /* "bla""bla"  -> "bla\"bla" */
713 <yylstr>\\\"\"          addcchar('\"');         /* "bla\""bla" -> "bla\"bla" */
714 <yylstr>\"{ws}+\"       ;                       /* "bla" "bla" -> "blabla" */
715 <yylstr>[^\\\n\"]+      {
716                                 char *yptr = yytext;
717                                 while(*yptr)    /* FIXME: codepage translation */
718                                         addwchar(*yptr++ & 0xff);
719                         }
721 \"                      {
722                                 push_to(yystr);
723                                 cbufidx = 0;
724                         }
725 <yystr>\"               {
726                                 pop_start();
727                                 yylval.str = get_buffered_cstring();
728                                 return tSTRING;
729                         }
730 <yystr>\n               yyerror("Unterminated string");
731 <yystr>\\[0-7]{1,3}     { /* octal escape sequence */
732                                 int result;
733                                 result = strtol(yytext+1, 0, 8);
734                                 if ( result > 0xff )
735                                         yyerror("Character constant out of range");
736                                 addcchar((char)result);
737                         }
738 <yystr>\\x[0-9a-fA-F]{2} {  /* hex escape sequence */
739                                 int result;
740                                 result = strtol(yytext+2, 0, 16);
741                                 addcchar((char)result);
742                         }
743 <yystr>\\[0-9]+         yyerror("Bad escape secuence");
744 <yystr>\\a              addcchar('\a');
745 <yystr>\\b              addcchar('\b');
746 <yystr>\\f              addcchar('\f');
747 <yystr>\\n              addcchar('\n');
748 <yystr>\\r              addcchar('\r');
749 <yystr>\\t              addcchar('\t');
750 <yystr>\\v              addcchar('\v');
751 <yystr>\\(.|\n)         addcchar(yytext[1]);
752 <yystr>[^\\\n\"]+       {
753                                 char *yptr = yytext;
754                                 while(*yptr)
755                                         addcchar(*yptr++);
756                         }
757 <yystr>\"\"             addcchar('\"');         /* "bla""bla"   -> "bla\"bla" */
758 <yystr>\\\"\"           addcchar('\"');         /* "bla\""bla"  -> "bla\"bla" */
759 <yystr>\"{ws}+\"        ;                       /* "bla" "bla"  -> "blabla" */
760 <yystr>.                yywarning("Matched %c", *yytext);
763 \'                      {
764                                 push_to(yyrcd);
765                                 cbufidx = 0;
766                         }
767 <yyrcd>\'               {
768                                 pop_start();
769                                 yylval.raw = new_raw_data();
770                                 yylval.raw->size = cbufidx;
771                                 yylval.raw->data = xmalloc(yylval.raw->size);
772                                 memcpy(yylval.raw->data, cbuffer, yylval.raw->size);
773                                 return RAWDATA;
774                         }
775 <yyrcd>[0-9a-fA-F]{2}   {
776                                 int result;
777                                 result = strtol(yytext, 0, 16);
778                                 addcchar((char)result);
779                         }
780 <yyrcd>{ws}+            ;       /* Ignore space */
781 <yyrcd>\n               line_number++; char_number = 1;
782 <yyrcd>.                yyerror("Malformed data-line");
784 <INITIAL,pp_ignore,pp_def_s,pp_strips,pp_stripp>"/*"    push_to(comment);       /* Eat comment */
785 <comment>[^*\n]*        ;
786 <comment>"*"+[^*/\n]*   ;
787 <comment>\n             line_number++; char_number = 1;
788 <comment>"*"+"/"        pop_start();
790 ;[^\n]*                 ; /* Eat comment */
791 <INITIAL,pp_ignore,pp_def_s>"//"[^\n]*          ; /* Eat comment */
793 <INITIAL,yywf,pp_false,pp_strips,pp_stripp>\n   {
794                                 if(YY_START == yywf)
795                                         pop_start();
796                                 line_number++;
797                                 char_number = 1;
798                                 if(want_nl)
799                                 {
800                                         want_nl = 0;
801                                         return tNL;
802                                 }
803                         }
804 <INITIAL,yywf>{ws}+     ;       /* Eat whitespace */
806 <INITIAL>.              return yytext[0];
807 <<EOF>>                 {
808                                 YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
809                                 if(!pop_buffer())
810                                 {
811                                         if(YY_START == pp_strips || YY_START == pp_stripp || YY_START == pp_stripp_final)
812                                                 yyerror("Unexpected end of file during c-junk scanning (started at %d)", cjunk_tagline);
813                                         else
814                                                 yyterminate();
815                                 }
816                                 yy_delete_buffer(b);
817                         }
819 <*>.|\n                 {
820                                 /* Catch all rule to find any unmatched text */
821                                 if(*yytext == '\n')
822                                 {
823                                         line_number++;
824                                         char_number = 1;
825                                 }
826                                 yywarning("Unmatched text '%c' (0x%02x) YY_START=%d",
827                                         isprint(*yytext) ? *yytext : '.', *yytext, YY_START);
828                         }
832 #ifndef yywrap
833 int yywrap(void)
835 #if 0
836         if(bufferstackidx > 0)
837         {
838                 return 0;
839         }
840 #endif
841         return 1;
843 #endif
845 /* These dup functions copy the enclosed '\0' from
846  * the resource string.
847  */
848 void addcchar(char c)
850         if(cbufidx >= cbufalloc)
851         {
852                 cbufalloc += 1024;
853                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
854                 if(cbufalloc > 65536)
855                         yywarning("Reallocating string buffer larger than 64kB");
856         }
857         cbuffer[cbufidx++] = c;
860 void addwchar(short s)
862         if(wbufidx >= wbufalloc)
863         {
864                 wbufalloc += 1024;
865                 wbuffer = xrealloc(wbuffer, wbufalloc * sizeof(wbuffer[0]));
866                 if(wbufalloc > 65536)
867                         yywarning("Reallocating wide string buffer larger than 64kB");
868         }
870         /*
871          * BS 08-Aug-1999 FIXME: The '& 0xff' is probably a bug, but I have
872          * not experienced it yet and I seem to remember that this was for
873          * a reason. But, as so many things you tend to forget why.
874          * I guess that there were problems due to the sign extension of
875          * shorts WRT chars (e.g. 0x80 becomes 0xff80 instead of 0x0080).
876          * This should then be fixed in the lexer calling the function.
877          */
878         wbuffer[wbufidx++] = (short)(s & 0xff);
881 string_t *get_buffered_cstring(void)
883         string_t *str = new_string();
884         str->size = cbufidx;
885         str->type = str_char;
886         str->str.cstr = (char *)xmalloc(cbufidx+1);
887         memcpy(str->str.cstr, cbuffer, cbufidx);
888         str->str.cstr[cbufidx] = '\0';
889 /*      printf("got cstring \"%s\"\n", str->str.cstr); */
890         return str;
893 string_t *get_buffered_wstring(void)
895         string_t *str = new_string();
896         str->size = wbufidx;
897         str->type = str_unicode;
898         str->str.wstr = (short *)xmalloc(2*(wbufidx+1));
899         memcpy(str->str.wstr, wbuffer, wbufidx);
900         str->str.wstr[wbufidx] = 0;
901         return str;
904 string_t *make_string(char *s)
906         string_t *str = new_string();
907         str->size = strlen(s);
908         str->type = str_char;
909         str->str.cstr = (char *)xmalloc(str->size+1);
910         memcpy(str->str.cstr, s, str->size+1);
911         return str;
914 string_t *make_filename(char *s, int len)
916         char *cptr;
917         string_t *str = new_string();
919         str->size = len;
920         str->type = str_char;
921         str->str.cstr = (char *)xmalloc(str->size+1);
922         memcpy(str->str.cstr, s, str->size);
923         str->str.cstr[str->size] = '\0';
925         /* Remove escaped backslash and convert to forward */
926         cptr = str->str.cstr;
927         for(cptr = str->str.cstr; (cptr = strchr(cptr, '\\')) != NULL; cptr++)
928         {
929                 if(cptr[1] == '\\')
930                 {
931                         memmove(cptr, cptr+1, strlen(cptr));
932                         str->size--;
933                 }
934                 *cptr = '/';
935         }
937         /* Convert to lower case. Seems to be reasonable to do */
938         for(cptr = str->str.cstr; !leave_case && *cptr; cptr++)
939         {
940                 *cptr = tolower(*cptr);
941         }
942         return str;
945 /* Called from the parser to signal filename request */
946 void set_yywf(void)
948         push_to(yywf);
951 /* Called from the parser to signal preprocessor if case */
952 void set_pp_ignore(int state)
954         if(state)
955                 push_to(pp_false);
956         else
957                 pop_start();
960 /* Called from the parser to kill c-junk */
961 void strip_til_semicolon(void)
963         cjunk_tagline = line_number;
964         push_to(pp_strips);
967 void strip_til_parenthesis(void)
969         cjunk_tagline = line_number;
970         stripplevel = 1;        /* One scanned already */
971         push_to(pp_stripp);