1 %{ /* rclex.l -- lexer for Windows rc files parser */
2 /* Copyright 1997, 1998, 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 /* This is a lex input file which generates a lexer used by the
23 Windows rc file parser. It basically just recognized a bunch of
28 #include "libiberty.h"
29 #include "safe-ctype.h"
35 /* Whether we are in rcdata mode, in which we returns the lengths of
38 static int rcdata_mode;
40 /* Whether we are supressing lines from cpp (including windows.h or
41 headers from your C sources may bring in externs and typedefs).
42 When active, we return IGNORED_TOKEN, which lets us ignore these
43 outside of resource constructs. Thus, it isn't required to protect
44 all the non-preprocessor lines in your header files with #ifdef
45 RC_INVOKED. It also means your RC file can't include other RC
46 files if they're named "*.h". Sorry. Name them *.rch or whatever. */
48 static int suppress_cpp_data;
50 #define MAYBE_RETURN(x) return suppress_cpp_data ? IGNORED_TOKEN : (x)
52 /* The first filename we detect in the cpp output. We use this to
53 tell included files from the original file. */
55 static char *initial_fn;
57 /* List of allocated strings. */
61 struct alloc_string *next;
65 static struct alloc_string *strings;
67 /* Local functions. */
69 static void cpp_line (const char *);
70 static char *handle_quotes (const char *, unsigned long *);
71 static char *get_string (int);
77 "BEGIN" { MAYBE_RETURN (BEG); }
78 "{" { MAYBE_RETURN (BEG); }
79 "END" { MAYBE_RETURN (END); }
80 "}" { MAYBE_RETURN (END); }
81 "ACCELERATORS" { MAYBE_RETURN (ACCELERATORS); }
82 "VIRTKEY" { MAYBE_RETURN (VIRTKEY); }
83 "ASCII" { MAYBE_RETURN (ASCII); }
84 "NOINVERT" { MAYBE_RETURN (NOINVERT); }
85 "SHIFT" { MAYBE_RETURN (SHIFT); }
86 "CONTROL" { MAYBE_RETURN (CONTROL); }
87 "ALT" { MAYBE_RETURN (ALT); }
88 "BITMAP" { MAYBE_RETURN (BITMAP); }
89 "CURSOR" { MAYBE_RETURN (CURSOR); }
90 "DIALOG" { MAYBE_RETURN (DIALOG); }
91 "DIALOGEX" { MAYBE_RETURN (DIALOGEX); }
92 "EXSTYLE" { MAYBE_RETURN (EXSTYLE); }
93 "CAPTION" { MAYBE_RETURN (CAPTION); }
94 "CLASS" { MAYBE_RETURN (CLASS); }
95 "STYLE" { MAYBE_RETURN (STYLE); }
96 "AUTO3STATE" { MAYBE_RETURN (AUTO3STATE); }
97 "AUTOCHECKBOX" { MAYBE_RETURN (AUTOCHECKBOX); }
98 "AUTORADIOBUTTON" { MAYBE_RETURN (AUTORADIOBUTTON); }
99 "CHECKBOX" { MAYBE_RETURN (CHECKBOX); }
100 "COMBOBOX" { MAYBE_RETURN (COMBOBOX); }
101 "CTEXT" { MAYBE_RETURN (CTEXT); }
102 "DEFPUSHBUTTON" { MAYBE_RETURN (DEFPUSHBUTTON); }
103 "EDITTEXT" { MAYBE_RETURN (EDITTEXT); }
104 "GROUPBOX" { MAYBE_RETURN (GROUPBOX); }
105 "LISTBOX" { MAYBE_RETURN (LISTBOX); }
106 "LTEXT" { MAYBE_RETURN (LTEXT); }
107 "PUSHBOX" { MAYBE_RETURN (PUSHBOX); }
108 "PUSHBUTTON" { MAYBE_RETURN (PUSHBUTTON); }
109 "RADIOBUTTON" { MAYBE_RETURN (RADIOBUTTON); }
110 "RTEXT" { MAYBE_RETURN (RTEXT); }
111 "SCROLLBAR" { MAYBE_RETURN (SCROLLBAR); }
112 "STATE3" { MAYBE_RETURN (STATE3); }
113 "USERBUTTON" { MAYBE_RETURN (USERBUTTON); }
114 "BEDIT" { MAYBE_RETURN (BEDIT); }
115 "HEDIT" { MAYBE_RETURN (HEDIT); }
116 "IEDIT" { MAYBE_RETURN (IEDIT); }
117 "FONT" { MAYBE_RETURN (FONT); }
118 "ICON" { MAYBE_RETURN (ICON); }
119 "LANGUAGE" { MAYBE_RETURN (LANGUAGE); }
120 "CHARACTERISTICS" { MAYBE_RETURN (CHARACTERISTICS); }
121 "VERSION" { MAYBE_RETURN (VERSIONK); }
122 "MENU" { MAYBE_RETURN (MENU); }
123 "MENUEX" { MAYBE_RETURN (MENUEX); }
124 "MENUITEM" { MAYBE_RETURN (MENUITEM); }
125 "SEPARATOR" { MAYBE_RETURN (SEPARATOR); }
126 "POPUP" { MAYBE_RETURN (POPUP); }
127 "CHECKED" { MAYBE_RETURN (CHECKED); }
128 "GRAYED" { MAYBE_RETURN (GRAYED); }
129 "HELP" { MAYBE_RETURN (HELP); }
130 "INACTIVE" { MAYBE_RETURN (INACTIVE); }
131 "MENUBARBREAK" { MAYBE_RETURN (MENUBARBREAK); }
132 "MENUBREAK" { MAYBE_RETURN (MENUBREAK); }
133 "MESSAGETABLE" { MAYBE_RETURN (MESSAGETABLE); }
134 "RCDATA" { MAYBE_RETURN (RCDATA); }
135 "STRINGTABLE" { MAYBE_RETURN (STRINGTABLE); }
136 "VERSIONINFO" { MAYBE_RETURN (VERSIONINFO); }
137 "FILEVERSION" { MAYBE_RETURN (FILEVERSION); }
138 "PRODUCTVERSION" { MAYBE_RETURN (PRODUCTVERSION); }
139 "FILEFLAGSMASK" { MAYBE_RETURN (FILEFLAGSMASK); }
140 "FILEFLAGS" { MAYBE_RETURN (FILEFLAGS); }
141 "FILEOS" { MAYBE_RETURN (FILEOS); }
142 "FILETYPE" { MAYBE_RETURN (FILETYPE); }
143 "FILESUBTYPE" { MAYBE_RETURN (FILESUBTYPE); }
144 "VALUE" { MAYBE_RETURN (VALUE); }
145 "MOVEABLE" { MAYBE_RETURN (MOVEABLE); }
146 "FIXED" { MAYBE_RETURN (FIXED); }
147 "PURE" { MAYBE_RETURN (PURE); }
148 "IMPURE" { MAYBE_RETURN (IMPURE); }
149 "PRELOAD" { MAYBE_RETURN (PRELOAD); }
150 "LOADONCALL" { MAYBE_RETURN (LOADONCALL); }
151 "DISCARDABLE" { MAYBE_RETURN (DISCARDABLE); }
152 "NOT" { MAYBE_RETURN (NOT); }
154 "BLOCK"[ \t\n]*"\""[^\#\n]*"\"" {
157 /* This is a hack to let us parse version
158 information easily. */
160 s = strchr (yytext, '"');
162 send = strchr (s, '"');
163 if (strncmp (s, "StringFileInfo",
164 sizeof "StringFileInfo" - 1) == 0
165 && s + sizeof "StringFileInfo" - 1 == send)
166 MAYBE_RETURN (BLOCKSTRINGFILEINFO);
167 else if (strncmp (s, "VarFileInfo",
168 sizeof "VarFileInfo" - 1) == 0
169 && s + sizeof "VarFileInfo" - 1 == send)
170 MAYBE_RETURN (BLOCKVARFILEINFO);
175 r = get_string (send - s + 1);
176 strncpy (r, s, send - s);
179 MAYBE_RETURN (BLOCK);
187 [0-9][x0-9A-Fa-f]*L {
188 yylval.i.val = strtoul (yytext, 0, 0);
190 MAYBE_RETURN (NUMBER);
194 yylval.i.val = strtoul (yytext, 0, 0);
196 MAYBE_RETURN (NUMBER);
199 ("\""[^\"\n]*"\""[ \t\n]*)+ {
201 unsigned long length;
203 s = handle_quotes (yytext, &length);
207 MAYBE_RETURN (QUOTEDSTRING);
211 yylval.ss.length = length;
213 MAYBE_RETURN (SIZEDSTRING);
217 [A-Za-z][^ ,\t\r\n]* {
220 /* I rejected comma in a string in order to
221 handle VIRTKEY, CONTROL in an accelerator
222 resource. This means that an unquoted
223 file name can not contain a comma. I
224 don't know what rc permits. */
226 s = get_string (strlen (yytext) + 1);
229 MAYBE_RETURN (STRING);
232 [\n] { ++rc_lineno; }
233 [ \t\r]+ { /* ignore whitespace */ }
234 . { MAYBE_RETURN (*yytext); }
238 /* This is needed for some versions of lex. */
245 /* Handle a C preprocessor line. */
248 cpp_line (const char *s)
257 line = strtol (s, &send, 0);
258 if (*send != '\0' && ! ISSPACE (*send))
261 /* Subtract 1 because we are about to count the newline. */
262 rc_lineno = line - 1;
272 send = strchr (s, '"');
276 fn = (char *) xmalloc (send - s + 1);
277 strncpy (fn, s, send - s);
285 initial_fn = xmalloc (strlen (fn) + 1);
286 strcpy (initial_fn, fn);
289 /* Allow the initial file, regardless of name. Suppress all other
290 files if they end in ".h" (this allows included "*.rc"). */
291 if (strcmp (initial_fn, fn) == 0
292 || strcmp (fn + strlen (fn) - 2, ".h") != 0)
293 suppress_cpp_data = 0;
295 suppress_cpp_data = 1;
298 /* Handle a quoted string. The quotes are stripped. A pair of quotes
299 in a string are turned into a single quote. Adjacent strings are
300 merged separated by whitespace are merged, as in C. */
303 handle_quotes (const char *input, unsigned long *len)
309 ret = get_string (strlen (input) + 1);
323 rcparse_warning ("backslash at end of string");
327 rcparse_warning ("use \"\" to put \" in a string");
331 *s++ = ESCAPE_B; /* Strange, but true... */
369 case '0': case '1': case '2': case '3':
370 case '4': case '5': case '6': case '7':
373 if (*t >= '0' && *t <= '7')
375 ch = (ch << 3) | (*t - '0');
377 if (*t >= '0' && *t <= '7')
379 ch = (ch << 3) | (*t - '0');
391 if (*t >= '0' && *t <= '9')
392 ch = (ch << 4) | (*t - '0');
393 else if (*t >= 'a' && *t <= 'f')
394 ch = (ch << 4) | (*t - 'a' + 10);
395 else if (*t >= 'A' && *t <= 'F')
396 ch = (ch << 4) | (*t - 'A' + 10);
405 rcparse_warning ("unrecognized escape sequence");
413 else if (t[1] == '\0')
415 else if (t[1] == '"')
423 assert (ISSPACE (*t));
444 /* Allocate a string of a given length. */
449 struct alloc_string *as;
451 as = (struct alloc_string *) xmalloc (sizeof *as);
452 as->s = xmalloc (len);
460 /* Discard all the strings we have allocated. The parser calls this
461 when it no longer needs them. */
464 rcparse_discard_strings (void)
466 struct alloc_string *as;
471 struct alloc_string *n;
482 /* Enter rcdata mode. */
485 rcparse_rcdata (void)
490 /* Go back to normal mode from rcdata mode. */
493 rcparse_normal (void)