Bugfix: GetFullPathName sets lpFilePart only when the last element
[wine/multimedia.git] / tools / wrc / parser.l
blobbc11b4e755879bf1aa54ec7b0f5f859277fff5ff
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 <stdio.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <ctype.h>
102 #include <config.h>
103 #include "wrc.h"
104 #include "utils.h"
105 #include "preproc.h"
106 #include "parser.h"
107 #include "newstruc.h"
109 #include "y.tab.h"
111 #define YY_USE_PROTOS
112 #define YY_NO_UNPUT
114 /* Always update the current character position within a line */
115 #define YY_USER_ACTION  char_number+=yyleng;
117 raw_data_t *new_raw_data(void);
119 void addcchar(char c);
120 void addwchar(short s);
121 string_t *get_buffered_cstring(void);
122 string_t *get_buffered_wstring(void);
123 string_t *make_string(char *s);
124 string_t *make_filename(char *s, int len);
126 int line_number = 1;            /* The current line */
127 int char_number = 1;            /* The current char pos within the line */
128 static char cbuffer[1024];      /* Buffers for string collection */
129 static int cbufidx;
130 static short wbuffer[1024];
131 static int wbufidx;
132 static int want_nl = 0;         /* Set when newline needs to go to parser */
133 static int want_ident = 0;      /* Set is #ifdef, #ifndef or defined is seen */
134 static int stripslevel = 0;     /* Count {} during pp_strips mode */
135 static int stripplevel = 0;     /* Count () during pp_strips mode */
136 static char *substtext = NULL;  /* Holds the substition text while getting a define */
137 static int cjunk_tagline;       /* Where did we start stripping (helps error tracking) */
139 #ifdef YY_USE_STACK
140 void push_to(int start) { yy_push_state(start); }
141 void pop_start(void)    { yy_pop_state(start); }
142 #else
143 #define MAXSTARTSTACK   32
144 static int startstack[MAXSTARTSTACK];
145 static int startstackidx = 0;
147 void push_to(int start)
149         if(yydebug)
150                 printf("push_to(%d): %d -> %d\n", line_number, YY_START, start);
151         if(startstackidx >= MAXSTARTSTACK-1)
152                 internal_error(__FILE__, __LINE__, "Start condition stack overflow");
153         startstack[startstackidx++] = YY_START;
154         BEGIN(start);
157 void pop_start(void)
159         if(yydebug)
160                 printf("pop_start(%d): %d <- %d\n", line_number, startstack[startstackidx-1], YY_START);
161         if(startstackidx <= 0)
162                 internal_error(__FILE__, __LINE__, "Start condition stack underflow");
163         --startstackidx;
164         BEGIN(startstack[startstackidx]);
166 #endif
169 struct bufferstackentry {
170         YY_BUFFER_STATE bufferstate;    /* Buffer to switch back to */
171         struct pp_entry *define;        /* Points to expanding define
172                                            or NULL if handling includes
173                                          */
174         int line_number;                /* Line that we were handling */
175         int char_number;                /* The current position */
176         char *filename;                 /* Filename that we were handling */
179 #define MAXBUFFERSTACK 128
180 static struct bufferstackentry bufferstack[MAXBUFFERSTACK];
181 static int bufferstackidx = 0;
183 void push_buffer(YY_BUFFER_STATE buf, struct pp_entry *ppp, char *filename)
185         if(yydebug)
186                 printf("push_buffer: %p %p %p\n", buf, ppp, filename);
187         if(bufferstackidx >= MAXBUFFERSTACK-1)
188                 internal_error(__FILE__, __LINE__, "Buffer stack overflow");
189         memset(&bufferstack[bufferstackidx], 0, sizeof(bufferstack[0]));
190         bufferstack[bufferstackidx].bufferstate = buf;
191         bufferstack[bufferstackidx].define = ppp;
192         if(ppp)
193                 ppp->expanding = 1;
194         else if(filename)
195         {
196                 /* These will track the yyerror to the correct file and line */
197                 bufferstack[bufferstackidx].line_number = line_number;
198                 bufferstack[bufferstackidx].char_number = char_number;
199                 line_number = 1;
200                 char_number = 1;
201                 bufferstack[bufferstackidx].filename = input_name;
202                 input_name = filename;
203         }
204         else
205                 internal_error(__FILE__, __LINE__, "Pushing buffer without knowing where to go to");
206         bufferstackidx++;
209 YY_BUFFER_STATE pop_buffer(void)
211         if(bufferstackidx <= 0)
212                 return (YY_BUFFER_STATE)0;
213         bufferstackidx--;
214         if(bufferstack[bufferstackidx].define)
215                 bufferstack[bufferstackidx].define->expanding = 0;
216         else
217         {
218                 line_number = bufferstack[bufferstackidx].line_number;
219                 char_number = bufferstack[bufferstackidx].char_number;
220                 input_name = bufferstack[bufferstackidx].filename;
221                 fclose(yyin);
222         }
223         if(yydebug)
224                 printf("pop_buffer: %p %p (%d, %d) %p\n",
225                         bufferstack[bufferstackidx].bufferstate,
226                         bufferstack[bufferstackidx].define,
227                         bufferstack[bufferstackidx].line_number,
228                         bufferstack[bufferstackidx].char_number,
229                         bufferstack[bufferstackidx].filename);
230         yy_switch_to_buffer(bufferstack[bufferstackidx].bufferstate);
231         return bufferstack[bufferstackidx].bufferstate;
234 void do_include(char *name, int namelen)
236         char *cpy = (char *)xmalloc(namelen);
237         strcpy(cpy, name+1);    /* strip leading " or < */
238         cpy[namelen-2] = '\0';  /* strip trailing " or > */
239         if((yyin = open_include(cpy, name[0] == '"')) == NULL)
240                 yyerror("Unable to open include file %s", cpy);
241         push_buffer(YY_CURRENT_BUFFER, NULL, cpy);
242         yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
246 struct keyword {
247         char    *keyword;
248         int     token;
249         int     isextension;
250         int     needcase;
251         int     alwayskeyword;
254 static struct keyword keywords[] = {
255         { "ACCELERATORS", ACCELERATORS, 0, 0, 0},
256         { "ALT", ALT, 0, 0, 0},
257         { "ASCII", ASCII, 0, 0, 0},
258         { "AUTO3STATE", AUTO3STATE, 1, 0, 0},
259         { "AUTOCHECKBOX", AUTOCHECKBOX, 1, 0, 0},
260         { "AUTORADIOBUTTON", AUTORADIOBUTTON, 1, 0, 0},
261         { "BEGIN", tBEGIN, 0, 0, 1},
262         { "BITMAP", tBITMAP, 0, 0, 0},
263         { "BLOCK", BLOCK, 0, 0, 1},
264         { "CAPTION", CAPTION, 0, 0, 0},
265         { "CHARACTERISTICS", CHARACTERISTICS, 1, 0, 0},
266         { "CHECKBOX", CHECKBOX, 0, 0, 0},
267         { "CHECKED", CHECKED, 0, 0, 0},
268         { "CLASS", CLASS, 0, 0, 0},
269         { "COMBOBOX", COMBOBOX, 0, 0, 0},
270         { "CONTROL", CONTROL, 0, 0, 0},
271         { "CTEXT", CTEXT, 0, 0, 0},
272         { "CURSOR", CURSOR, 0, 0, 0},
273         { "defined", tDEFINED, 0, 1, 1},
274         { "DEFPUSHBUTTON", DEFPUSHBUTTON, 0, 0, 1},
275         { "DIALOG", DIALOG, 0, 0, 0},
276         { "DIALOGEX", DIALOGEX, 1, 0, 0},
277         { "DISCARDABLE", DISCARDABLE, 0, 0, 0},
278         { "EDITTEXT", EDITTEXT, 0, 0, 0},
279         { "END", tEND, 0, 0, 1},
280         { "EXSTYLE", EXSTYLE, 0, 0, 0},
281         { "extern", tEXTERN, 0, 1, 1},
282         { "FILEFLAGS", FILEFLAGS, 0, 0, 0},
283         { "FILEFLAGSMASK", FILEFLAGSMASK, 0, 0, 0},
284         { "FILEOS", FILEOS, 0, 0, 0},
285         { "FILESUBTYPE", FILESUBTYPE, 0, 0, 0},
286         { "FILETYPE", FILETYPE, 0, 0, 0},
287         { "FILEVERSION", FILEVERSION, 0, 0, 0},
288         { "FIXED", tFIXED, 0, 0, 0},
289         { "FONT", FONT, 0, 0, 0},
290         { "GRAYED", GRAYED, 0, 0, 0},
291         { "GROUPBOX", GROUPBOX, 0, 0, 0},
292         { "HELP", HELP, 0, 0, 0},
293         { "ICON", ICON, 0, 0, 0},
294         { "IMPURE", IMPURE, 0, 0, 0},
295         { "INACTIVE", INACTIVE, 0, 0, 0},
296         { "LANGUAGE", LANGUAGE, 1, 0, 1},
297         { "LISTBOX", LISTBOX, 0, 0, 0},
298         { "LOADONCALL", LOADONCALL, 0, 0, 0},
299         { "LTEXT", LTEXT, 0, 0, 0},
300         { "MENU", MENU, 0, 0, 0},
301         { "MENUBARBREAK", MENUBARBREAK, 0, 0, 0},
302         { "MENUBREAK", MENUBREAK, 0, 0, 0},
303         { "MENUEX", MENUEX, 1, 0, 0},
304         { "MENUITEM", MENUITEM, 0, 0, 0},
305         { "MESSAGETABLE", MESSAGETABLE, 1, 0, 0},
306         { "MOVEABLE", MOVEABLE, 0, 0, 0},
307         { "NOINVERT", NOINVERT, 0, 0, 0},
308         { "NOT", NOT, 0, 0, 0},
309         { "POPUP", POPUP, 0, 0, 0},
310         { "PRELOAD", PRELOAD, 0, 0, 0},
311         { "PRODUCTVERSION", PRODUCTVERSION, 0, 0, 0},
312         { "PURE", tPURE, 0, 0, 0},
313         { "PUSHBUTTON", PUSHBUTTON, 0, 0, 0},
314         { "RADIOBUTTON", RADIOBUTTON, 0, 0, 0},
315         { "RCDATA", RCDATA, 0, 0, 0},
316         { "RTEXT", RTEXT, 0, 0, 0},
317         { "SCROLLBAR", SCROLLBAR, 0, 0, 0},
318         { "SEPARATOR", SEPARATOR, 0, 0, 0},
319         { "SHIFT", SHIFT, 0, 0, 0},
320         { "STATE3", STATE3, 1, 0, 0},
321         { "STRING", tSTRING, 0, 0, 0},
322         { "STRINGTABLE", STRINGTABLE, 0, 0, 1},
323         { "STYLE", STYLE, 0, 0, 0},
324         { "typedef", tTYPEDEF, 0, 1, 1},
325         { "VALUE", VALUE, 0, 0, 0},
326         { "VERSION", VERSION, 1, 0, 0},
327         { "VERSIONINFO", VERSIONINFO, 0, 0, 0},
328         { "VIRTKEY", VIRTKEY, 0, 0, 0}
331 #define NKEYWORDS       (sizeof(keywords)/sizeof(keywords[0]))
332 #define KWP(p)          ((struct keyword *)(p))
333 int kw_cmp_func(const void *s1, const void *s2)
335         int ret;
336         ret = stricmp(KWP(s1)->keyword, KWP(s2)->keyword);
337         if(!ret && (KWP(s1)->needcase || KWP(s2)->needcase))
338                 return strcmp(KWP(s1)->keyword, KWP(s2)->keyword);
339         else
340                 return ret;
343 #define KW_BSEARCH
344 #define DO_SORT
345 struct keyword *iskeyword(char *kw)
347         struct keyword *kwp;
348         struct keyword key;
349         key.keyword = kw;
350         key.needcase = 0;
351 #ifdef DO_SORT
352         {
353                 /* Make sure that it is sorted for bsearsh */
354                 static int sorted = 0;
355                 if(!sorted)
356                 {
357                         qsort(keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
358                         sorted = 1;
359                 }
360         }
361 #endif
362 #ifdef KW_BSEARCH
363         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
364 #else
365         {
366                 int i;
367                 for(i = 0; i < NKEYWORDS; i++)
368                 {
369                         if(!kw_cmp_func(&key, &keywords[i]))
370                                 break;
371                 }
372                 if(i < NKEYWORDS)
373                         kwp = &keywords[i];
374                 else
375                         kwp = NULL;
376         }
377 #endif
379 #ifdef LEX_DEBUG
380         if(kwp && !strcmp(kwp->keyword, "LANGUAGE"))
381                 printf("Got Language\n");
382 #endif
383         if(kwp == NULL || (kwp->isextension && !extensions))
384                 return NULL;
385         else
386                 return kwp;
389 void add_to_substtext(char *text, int len)
391         if(!substtext)
392         {
393                 substtext = xstrdup(text);
394         }
395         else
396         {
397                 substtext = (char *)xrealloc(substtext, strlen(substtext)+len+1);
398                 strcat(substtext, text);
399         }
405         /* #include handling */
406 ^{ws}*#{ws}*include{ws}*        push_to(pp_incl);
407 <pp_incl>\<[^\n\>]+\>           do_include(yytext, yyleng); pop_start();
408 <pp_incl>\"[^\n\>]+\"           do_include(yytext, yyleng); pop_start();
409 <pp_incl>.              yyerror("Malformed #include");
411         /* #define handling */
412 ^{ws}*#{ws}*define{ws}* push_to(pp_def);
413 <pp_def>{cident}        {
414                                 set_define(yytext);
415                                 push_to(pp_def_s);
416                         }
417 <pp_def>{cident}\(      push_to(pp_ignore); /* Ignore function-like defines for now*/
418 <pp_def>.               yyerror("Malformed #define");
420 <pp_ignore,pp_def_s>[^\/\\\n]*  {
421                         if(YY_START == pp_def_s)
422                                 add_to_substtext(yytext, yyleng);
423                 }
424 <pp_ignore,pp_def_s>\/[^\/\*][^\/\\\n]* { /* Comment is handled in normal handling */
425                         if(YY_START == pp_def_s)
426                                 add_to_substtext(yytext, yyleng);
427                 }
428 <pp_ignore,pp_def_s>\\{ws}*\n   line_number++;  char_number = 1; /* Line continuation */
429 <pp_ignore,pp_def_s>\n          {
430                         if(YY_START == pp_def_s)
431                         {
432                                 add_define(substtext ? substtext : "");
433                                 free(substtext);
434                                 substtext = NULL;
435                         }
436                         line_number++;
437                         char_number = 1;
438                         pop_start();
439                         pop_start();
440                 }
442         /* #undef handling */
443 ^{ws}*#{ws}*undef{ws}*  push_to(pp_undef);
444 <pp_undef>{cident}      {
445                                 del_define(yytext);
446                                 pop_start();
447                                 /*push_to(pp_ignore);*/
448                         }
450         /* Conditional handling */
451 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*if{ws}*       {
452                         if(YY_START == pp_false)
453                         {
454                                 if(yydebug)
455                                         printf("(%d)#if ignored\n", line_number);
456                                 push_if(0, 0, 1);
457                                 push_to(pp_ignore_eol);
458                         }
459                         else
460                         {
461                                 push_to(INITIAL);
462                                 want_nl = 1;
463                                 return tIF;
464                         }
465                 }
466 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*ifdef{ws}*    {
467                         if(YY_START == pp_false)
468                         {
469                                 if(yydebug)
470                                         printf("(%d)#ifdef ignored\n", line_number);
471                                 push_if(0, 0, 1);
472                                 push_to(pp_ignore_eol);
473                         }
474                         else
475                         {
476                                 push_to(INITIAL);
477                                 want_nl = 1;
478                                 want_ident = 1;
479                                 return tIFDEF;
480                         }
481                 }
482 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*ifndef{ws}*   {
483                         if(YY_START == pp_false)
484                         {
485                                 if(yydebug)
486                                         printf("(%d)#ifndef ignored\n", line_number);
487                                 push_if(0, 0, 1);
488                                 push_to(pp_ignore_eol);
489                         }
490                         else
491                         {
492                                 push_to(INITIAL);
493                                 want_nl = 1;
494                                 want_ident = 1;
495                                 return tIFNDEF;
496                         }
497                 }
498 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*elif{ws}*     {
499                         if(!isnevertrue_if())
500                         {
501                                 push_to(INITIAL);
502                                 want_nl = 1;
503                                 return tELIF;
504                         }
505                         else if(YY_START == pp_false)
506                                 push_to(pp_ignore_eol);
507                         if(yydebug)
508                                 printf("(%d)#elif ignored\n", line_number);
509                 }
510 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*else{ws}*     {
511                         if(!isnevertrue_if())
512                         {
513                                 push_to(INITIAL);
514                                 want_nl = 1;
515                                 return tELSE;
516                         }
517                         if(yydebug)
518                                 printf("(%d)#else ignored\n", line_number);
519                 }
520 <INITIAL,pp_strips,pp_stripp,pp_false>^{ws}*#{ws}*endif{ws}*    {
521                         if(!isnevertrue_if())
522                         {
523                                 want_nl = 1;
524                                 return tENDIF;
525                         }
526                         else
527                         {
528                                 if(yydebug)
529                                         printf("(%d)#endif ignored\n", line_number);
530                                 pop_if();
531                         }
532                 }
534         /* The error directive */
535 ^{ws}*#{ws}*error{ws}*  push_to(pp_error);
536 <pp_error>[^\n]*        yyerror("Error directive: %s", yytext);
538         /* preprocessor junk */
539 ^{ws}*#{ws}*pragma[^\n]*        ;       /* Ignore #pragma */
540 ^{ws}*#{ws}*line[^\n]*          ;       /* Ignore #line */
541  /* We'll get an error on malformed #xxx statements
542   * by not recognising '#' at all. This helps tracking
543   * preprocessor errors.
544   */
545  /*^{ws}*#{ws}*                 ;        Ignore # */
547 <pp_strips>\{           stripslevel++;
548 <pp_strips>\}           stripslevel--;
549 <pp_strips>;            if(!stripslevel) pop_start();
550 <pp_strips>[^\{\};\n#]* ; /* Ignore rest */
552 <pp_stripp>\(           stripplevel++;
553 <pp_stripp>\)           {
554                                 stripplevel--;
555                                 if(!stripplevel)
556                                 {
557                                         pop_start();
558                                         push_to(pp_stripp_final);
559                                 }
560                         }
561 <pp_stripp>[^\(\);\n#]* ; /* Ignore rest */
563 <pp_stripp_final>{ws}*  ; /* Ignore */
564 <pp_stripp_final>;      pop_start(); /* Kill the semicolon */
565 <pp_stripp_final>\n     line_number++; char_number = 1; pop_start();
566 <pp_stripp_final>.      yyless(0); pop_start();
568 <pp_false>.             ;       /* Ignore everything except #xxx during false #if state */
570 <pp_ignore_eol>[^\n]*   pop_start();
572  /* These are special cases due to filename scanning */
573 <yywf>[Dd][Ii][Ss][Cc][Aa][Rr][Dd][Aa][Bb][Ll][Ee]      return DISCARDABLE;
574 <yywf>[Ff][Ii][Xx][Ee][Dd]                              return tFIXED;
575 <yywf>[Ii][Mm][Pp][Uu][Rr][Ee]                          return IMPURE;
576 <yywf>[Mm][Oo][Vv][Ee][Aa][Bb][Ll][Ee]                  return MOVEABLE;
577 <yywf>[Ll][Oo][Aa][Dd][Oo][Nn][Cc][Aa][Ll][Ll]          return LOADONCALL;
578 <yywf>[Pp][Rr][Ee][Ll][Oo][Aa][Dd]                      return PRELOAD;
579 <yywf>[Pp][Uu][Rr][Ee]                                  return tPURE;
581 \{                      return tBEGIN;
582 \}                      return tEND;
584 [0-9]+[lL]?             { yylval.num = atoi(yytext); return NUMBER; }
585 0[xX][0-9A-Fa-f]+[lL]?  { yylval.num = strtoul(yytext,0,16); return NUMBER; }
586 0[oO][0-7]+             { yylval.num = strtoul(yytext+2,0,8); return NUMBER; }
587 [A-Za-z_0-9]+           {
588                                 struct keyword *token;
589                                 struct pp_entry *ppp;
591                                 want_rscname = 0;
592                                 
593                                 if(want_ident)
594                                 {
595                                         /* Prevent preprocessor subst */
596                                         want_ident = 0;
597                                         yylval.str = make_string(yytext);
598                                 #ifdef LEX_DEBUG
599                                         printf("want IDENT (%s, %d, %d): <%s>\n", input_name, line_number, char_number, yytext);
600                                 #endif
601                                         return IDENT;
602                                 }
603                                 else if((ppp = pp_lookup(yytext)) != NULL)
604                                 {
605                                         /* Do preprocessor substitution,
606                                          * but expand only if macro is not
607                                          * already expanding.
608                                          */
609                                         if(!ppp->expanding)
610                                         {
611                                 #ifdef LEX_DEBUG
612                                                 printf("expand IDENT (%s, %d, %d): <%s>\n", input_name, line_number, char_number, yytext);
613                                 #endif
614                                                 push_buffer(YY_CURRENT_BUFFER, ppp, NULL);
615                                                 yy_scan_string(ppp->subst);
616                                         }
617                                 }
618                                 else if((token = iskeyword(yytext)) != NULL
619                                   && !(!token->alwayskeyword && want_rscname))
620                                 {
621                                         switch(token->token)
622                                         {
623                                         case tDEFINED:
624                                                 want_ident = 1;
625                                                 break;
626                                         /*case RCDATA:*/
627                                         case CURSOR:
628                                         case tBITMAP:
629                                         case MESSAGETABLE:
630                                                 push_to(yywf);
631                                                 break;
632                                         case FONT:
633                                         case ICON:
634                                                 if(!indialog)
635                                                         push_to(yywf);
636                                                 break;
637                                         case DIALOG:
638                                         case DIALOGEX:
639                                                 indialog = 1;
640                                                 break;
641                                         }
642                                         return token->token;
643                                 }
644                                 else
645                                 {
646                                         yylval.str = make_string(yytext);
647                                 #ifdef LEX_DEBUG
648                                         printf("%s IDENT (%s, %d, %d): <%s>\n",
649                                                 want_rscname ? "rscname" : "just",
650                                                 input_name,
651                                                 line_number,
652                                                 char_number,
653                                                 yytext);
654                                 #endif
655                                         return IDENT;
656                                 }
657                         }
658 \|\|                    return LOGOR;
659 \&\&                    return LOGAND;
660 \=\=                    return EQ;
661 \!\=                    return NE;
662 \<\=                    return LTE;
663 \>\=                    return GTE;
665 <yywf>[^ \f\t\r\n\"]*   { pop_start(); yylval.str = make_filename(yytext, yyleng); return FILENAME; }
666 <yywf>\"                push_to(yywf_s);
667 <yywf_s>[^\"\n]*\"      { pop_start(); pop_start(); yylval.str = make_filename(yytext, yyleng-1); return FILENAME; }
668 <yywf_s>\n              yyerror("Newline in filename");
670 L\"                     {
671                                 push_to(yylstr);
672                                 wbufidx = 0;
673                                 if(!win32)
674                                         yywarning("16bit resource contains unicode strings\n");
675                         }
676 <yylstr>\"              {
677                                 pop_start();
678                                 yylval.str = get_buffered_wstring();
679                                 return tSTRING;
680                         }
681 <yylstr>\n              yyerror("Unterminated string");
682 <yylstr>\\[0-7]{1,6}    { /* octal escape sequence */
683                                 int result;
684                                 result = strtol(yytext+1, 0, 8);
685                                 if ( result > 0xffff )
686                                         yyerror("Character constant out of range");
687                                 addwchar((short)result);
688                         }
689 <yylstr>\\x[0-9a-fA-F]{4} {  /* hex escape sequence */
690                                 int result;
691                                 result = strtol(yytext+2, 0, 16);
692                                 addwchar((short)result);
693                         }
694 <yylstr>\\[0-9]+        yyerror("Bad escape secuence");
695 <yylstr>\\a             addwchar('\a');
696 <yylstr>\\b             addwchar('\b');
697 <yylstr>\\f             addwchar('\f');
698 <yylstr>\\n             addwchar('\n');
699 <yylstr>\\r             addwchar('\r');
700 <yylstr>\\t             addwchar('\t');
701 <yylstr>\\v             addwchar('\v');
702 <yylstr>\\(.|\n)        addwchar(yytext[1]);
703 <yylstr>[^\\\n\"]+      {
704                                 char *yptr = yytext;
705                                 while(*yptr)    /* FIXME: codepage translation */
706                                         addwchar(*yptr++ & 0xff);
707                         }
709 \"                      {
710                                 push_to(yystr);
711                                 cbufidx = 0;
712                         }
713 <yystr>\"               {
714                                 pop_start();
715                                 yylval.str = get_buffered_cstring();
716                                 return tSTRING;
717                         }
718 <yystr>\n               yyerror("Unterminated string");
719 <yystr>\\[0-7]{1,3}     { /* octal escape sequence */
720                                 int result;
721                                 result = strtol(yytext+1, 0, 8);
722                                 if ( result > 0xff )
723                                         yyerror("Character constant out of range");
724                                 addcchar((char)result);
725                         }
726 <yystr>\\x[0-9a-fA-F]{2} {  /* hex escape sequence */
727                                 int result;
728                                 result = strtol(yytext+2, 0, 16);
729                                 addcchar((char)result);
730                         }
731 <yystr>\\[0-9]+         yyerror("Bad escape secuence");
732 <yystr>\\a              addcchar('\a');
733 <yystr>\\b              addcchar('\b');
734 <yystr>\\f              addcchar('\f');
735 <yystr>\\n              addcchar('\n');
736 <yystr>\\r              addcchar('\r');
737 <yystr>\\t              addcchar('\t');
738 <yystr>\\v              addcchar('\v');
739 <yystr>\\(.|\n)         addcchar(yytext[1]);
740 <yystr>[^\\\n\"]+       {
741                                 char *yptr = yytext;
742                                 while(*yptr)
743                                         addcchar(*yptr++);
744                         }
748 \'                      {
749                                 push_to(yyrcd);
750                                 cbufidx = 0;
751                         }
752 <yyrcd>\'               {
753                                 pop_start();
754                                 yylval.raw = new_raw_data();
755                                 yylval.raw->size = cbufidx;
756                                 yylval.raw->data = xmalloc(yylval.raw->size);
757                                 memcpy(yylval.raw->data, cbuffer, yylval.raw->size);
758                                 return RAWDATA;
759                         }
760 <yyrcd>[0-9a-fA-F]{2}   {
761                                 int result;
762                                 result = strtol(yytext, 0, 16);
763                                 addcchar((char)result);
764                         }
765 <yyrcd>{ws}+            ;       /* Ignore space */
766 <yyrcd>.                yyerror("Malformed data-line");
768 <INITIAL,pp_ignore,pp_def_s>"/*"        push_to(comment);       /* Eat comment */
769 <comment>[^*\n]*        ;
770 <comment>"*"+[^*/\n]*   ;
771 <comment>\n             line_number++; char_number = 1;
772 <comment>"*"+"/"        pop_start();
774 ;[^\n]*                 ; /* Eat comment */
775 <INITIAL,pp_ignore,pp_def_s>"//"[^\n]*          ; /* Eat comment */
777 <INITIAL,yywf,pp_false,pp_strips,pp_stripp>\n   {
778                                 if(YY_START == yywf)
779                                         pop_start();
780                                 line_number++;
781                                 char_number = 1;
782                                 if(want_nl)
783                                 {
784                                         want_nl = 0;
785                                         return tNL;
786                                 }
787                         }
788 <INITIAL,yywf>{ws}+     ;       /* Eat whitespace */
790 <INITIAL>.              return yytext[0];
791 <<EOF>>                 {
792                                 YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
793                                 if(!pop_buffer())
794                                 {
795                                         if(YY_START == pp_strips || YY_START == pp_stripp || YY_START == pp_stripp_final)
796                                                 yyerror("Unexpected end of file during c-junk scanning (started at %d)", cjunk_tagline);
797                                         else
798                                                 yyterminate();
799                                 }
800                                 yy_delete_buffer(b);
801                         }
804 #ifndef yywrap
805 int yywrap(void)
807 //      if(bufferstackidx > 0)
808 //      {
809 //              return 0;
810 //      }
811         return 1;
813 #endif
815 /* These dup functions copy the enclosed '\0' from
816  * the resource string.
817  */
818 void addcchar(char c)
820         if(cbufidx >= sizeof(cbuffer))
821                 internal_error(__FILE__, __LINE__, "Character buffer overflow");
822         cbuffer[cbufidx++] = c;
825 void addwchar(short s)
827         if(wbufidx >= sizeof(wbuffer))
828                 internal_error(__FILE__, __LINE__, "Wide character buffer overflow");
829         wbuffer[wbufidx++] = (short)(s & 0xff);
832 string_t *get_buffered_cstring(void)
834         string_t *str = new_string();
835         str->size = cbufidx;
836         str->type = str_char;
837         str->str.cstr = (char *)xmalloc(cbufidx+1);
838         memcpy(str->str.cstr, cbuffer, cbufidx);
839         str->str.cstr[cbufidx] = '\0';
840 /*      printf("got cstring \"%s\"\n", str->str.cstr); */
841         return str;
844 string_t *get_buffered_wstring(void)
846         string_t *str = new_string();
847         str->size = wbufidx;
848         str->type = str_unicode;
849         str->str.wstr = (short *)xmalloc(2*(wbufidx+1));
850         memcpy(str->str.wstr, wbuffer, wbufidx);
851         str->str.wstr[wbufidx] = 0;
852         return str;
855 string_t *make_string(char *s)
857         string_t *str = new_string();
858         str->size = strlen(s);
859         str->type = str_char;
860         str->str.cstr = (char *)xmalloc(str->size+1);
861         memcpy(str->str.cstr, s, str->size+1);
862         return str;
865 string_t *make_filename(char *s, int len)
867         char *cptr;
868         string_t *str = new_string();
870         str->size = len;
871         str->type = str_char;
872         str->str.cstr = (char *)xmalloc(str->size+1);
873         memcpy(str->str.cstr, s, str->size);
874         str->str.cstr[str->size] = '\0';
876         /* Remove escaped backslash and convert to forward */
877         cptr = str->str.cstr;
878         for(cptr = str->str.cstr; (cptr = strchr(cptr, '\\')) != NULL; cptr++)
879         {
880                 if(cptr[1] == '\\')
881                 {
882                         memmove(cptr, cptr+1, strlen(cptr));
883                         str->size--;
884                 }
885                 *cptr = '/';
886         }
888         /* Convert to lower case. Seems to be reasonable to do */
889         for(cptr = str->str.cstr; !leave_case && *cptr; cptr++)
890         {
891                 *cptr = tolower(*cptr);
892         }
893         return str;
896 /* Called from the parser to signal filename request */
897 void set_yywf(void)
899         push_to(yywf);
902 /* Called from the parser to signal preprocessor if case */
903 void set_pp_ignore(int state)
905         if(state)
906                 push_to(pp_false);
907         else
908                 pop_start();
911 /* Called from the parser to kill c-junk */
912 void strip_til_semicolon(void)
914         cjunk_tagline = line_number;
915         push_to(pp_strips);
918 void strip_til_parenthesis(void)
920         cjunk_tagline = line_number;
921         stripplevel = 1;        /* One scanned already */
922         push_to(pp_stripp);