1 /* mclex.c -- lexer for Windows mc files parser.
2 Copyright (C) 2007-2017 Free Software Foundation, Inc.
4 Written by Kai Tietz, Onevision.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 /* This is a lexer used by the Windows rc file parser.
24 It basically just recognized a bunch of keywords. */
29 #include "libiberty.h"
30 #include "safe-ctype.h"
36 /* Exported globals. */
37 bfd_boolean mclex_want_nl
= FALSE
;
38 bfd_boolean mclex_want_line
= FALSE
;
39 bfd_boolean mclex_want_filename
= FALSE
;
42 static unichar
*input_stream
= NULL
;
43 static unichar
*input_stream_pos
= NULL
;
44 static int input_line
= 1;
45 static const char *input_filename
= NULL
;
48 mc_set_content (const unichar
*src
)
52 input_stream
= input_stream_pos
= unichar_dup (src
);
56 mc_set_inputfile (const char *name
)
58 if (! name
|| *name
== 0)
62 const char *s1
= strrchr (name
, '/');
63 const char *s2
= strrchr (name
, '\\');
67 if (s1
&& s2
&& s1
< s2
)
79 show_msg (const char *kind
, const char *msg
, va_list argp
)
81 fprintf (stderr
, "In %s at line %d: %s: ", input_filename
, input_line
, kind
);
82 vfprintf (stderr
, msg
, argp
);
83 fprintf (stderr
, ".\n");
87 mc_warn (const char *s
, ...)
91 show_msg ("warning", s
, argp
);
96 mc_fatal (const char *s
, ...)
100 show_msg ("fatal", s
, argp
);
107 yyerror (const char *s
, ...)
111 show_msg ("parser", s
, argp
);
117 get_diff (unichar
*end
, unichar
*start
)
123 ret
= unichar_dup (start
);
129 parse_digit (unichar ch
)
131 rc_uint_type base
= 10, v
= 0, c
;
136 switch (input_stream_pos
[0])
138 case 'x': case 'X': base
= 16; input_stream_pos
++; break;
139 case 'o': case 'O': base
= 8; input_stream_pos
++; break;
140 case 'b': case 'B': base
= 2; input_stream_pos
++; break;
144 v
= (rc_uint_type
) (ch
- '0');
146 while ((ch
= input_stream_pos
[0]) != 0)
148 if (ch
>= 'A' && ch
<= 'F')
149 c
= (rc_uint_type
) (ch
- 'A') + 10;
150 else if (ch
>= 'a' && ch
<= 'f')
151 c
= (rc_uint_type
) (ch
- 'a') + 10;
152 else if (ch
>= '0' && ch
<= '9')
153 c
= (rc_uint_type
) (ch
- '0');
160 if (input_stream_pos
[0] == 'U' || input_stream_pos
[0] == 'u')
162 if (input_stream_pos
[0] == 'L' || input_stream_pos
[0] == 'l')
164 if (input_stream_pos
[0] == 'L' || input_stream_pos
[0] == 'l')
169 static mc_keyword
*keyword_top
= NULL
;
172 enum_facility (int e
)
174 mc_keyword
*h
= keyword_top
;
178 while (h
&& strcmp (h
->group_name
, "facility") != 0)
190 enum_severity (int e
)
192 mc_keyword
*h
= keyword_top
;
196 while (h
&& strcmp (h
->group_name
, "severity") != 0)
208 mc_add_keyword_ascii (const char *sz
, int rid
, const char *grp
, rc_uint_type nv
, const char *sv
)
210 unichar
*usz
, *usv
= NULL
;
211 rc_uint_type usz_len
;
213 unicode_from_codepage (&usz_len
, &usz
, sz
, CP_ACP
);
215 unicode_from_codepage (&usz_len
, &usv
, sv
, CP_ACP
);
216 mc_add_keyword (usz
, rid
, grp
, nv
, usv
);
220 mc_add_keyword (unichar
*usz
, int rid
, const char *grp
, rc_uint_type nv
, unichar
*sv
)
222 mc_keyword
*p
, *c
, *n
;
223 size_t len
= unichar_len (usz
);
233 int e
= memcmp (usz
, c
->usz
, len
* sizeof (unichar
));
239 if (! strcmp (grp
, "keyword") || strcmp (c
->group_name
, grp
) != 0)
240 fatal (_("Duplicate symbol entered into keyword list."));
243 c
->sval
= (!sv
? NULL
: unichar_dup (sv
));
244 if (! strcmp (grp
, "language"))
246 const wind_language_t
*lag
= wind_find_language_by_id ((unsigned) nv
);
249 fatal ("Language ident 0x%lx is not resolvable.\n", (long) nv
);
250 memcpy (&c
->lang_info
, lag
, sizeof (*lag
));
257 n
= xmalloc (sizeof (mc_keyword
));
264 n
->sval
= (!sv
? NULL
: unichar_dup (sv
));
265 if (! strcmp (grp
, "language"))
267 const wind_language_t
*lag
= wind_find_language_by_id ((unsigned) nv
);
269 fatal ("Language ident 0x%lx is not resolvable.\n", (long) nv
);
270 memcpy (&n
->lang_info
, lag
, sizeof (*lag
));
279 mc_token (const unichar
*t
, size_t len
)
281 static int was_init
= 0;
287 mc_add_keyword_ascii ("OutputBase", MCOUTPUTBASE
, "keyword", 0, NULL
);
288 mc_add_keyword_ascii ("MessageIdTypedef", MCMESSAGEIDTYPEDEF
, "keyword", 0, NULL
);
289 mc_add_keyword_ascii ("SeverityNames", MCSEVERITYNAMES
, "keyword", 0, NULL
);
290 mc_add_keyword_ascii ("FacilityNames", MCFACILITYNAMES
, "keyword", 0, NULL
);
291 mc_add_keyword_ascii ("LanguageNames", MCLANGUAGENAMES
, "keyword", 0, NULL
);
292 mc_add_keyword_ascii ("MessageId", MCMESSAGEID
, "keyword", 0, NULL
);
293 mc_add_keyword_ascii ("Severity", MCSEVERITY
, "keyword", 0, NULL
);
294 mc_add_keyword_ascii ("Facility", MCFACILITY
, "keyword", 0, NULL
);
295 mc_add_keyword_ascii ("SymbolicName", MCSYMBOLICNAME
, "keyword", 0, NULL
);
296 mc_add_keyword_ascii ("Language", MCLANGUAGE
, "keyword", 0, NULL
);
297 mc_add_keyword_ascii ("Success", MCTOKEN
, "severity", 0, NULL
);
298 mc_add_keyword_ascii ("Informational", MCTOKEN
, "severity", 1, NULL
);
299 mc_add_keyword_ascii ("Warning", MCTOKEN
, "severity", 2, NULL
);
300 mc_add_keyword_ascii ("Error", MCTOKEN
, "severity", 3, NULL
);
301 mc_add_keyword_ascii ("System", MCTOKEN
, "facility", 0xff, NULL
);
302 mc_add_keyword_ascii ("Application", MCTOKEN
, "facility", 0xfff, NULL
);
303 mc_add_keyword_ascii ("English", MCTOKEN
, "language", 0x409, "MSG00001");
306 if (!len
|| !t
|| *t
== 0)
314 if (! memcmp (k
->usz
, t
, len
* sizeof (unichar
)))
316 if (k
->rid
== MCTOKEN
)
329 unichar
*start_token
;
332 if (! input_stream_pos
)
334 fatal ("Input stream not setuped.\n");
339 start_token
= input_stream_pos
;
340 if (input_stream_pos
[0] == '.'
341 && (input_stream_pos
[1] == '\n'
342 || (input_stream_pos
[1] == '\r' && input_stream_pos
[2] == '\n')))
344 mclex_want_line
= FALSE
;
345 while (input_stream_pos
[0] != 0 && input_stream_pos
[0] != '\n')
347 if (input_stream_pos
[0] == '\n')
351 while (input_stream_pos
[0] != 0 && input_stream_pos
[0] != '\n')
353 if (input_stream_pos
[0] == '\n')
355 yylval
.ustr
= get_diff (input_stream_pos
, start_token
);
358 while ((ch
= input_stream_pos
[0]) <= 0x20)
365 if (mclex_want_nl
&& ch
== '\n')
367 mclex_want_nl
= FALSE
;
371 start_token
= input_stream_pos
;
373 if (mclex_want_filename
)
375 mclex_want_filename
= FALSE
;
379 while ((ch
= input_stream_pos
[0]) != 0)
385 yylval
.ustr
= get_diff (input_stream_pos
, start_token
);
391 while ((ch
= input_stream_pos
[0]) != 0)
393 if (ch
<= 0x20 || ch
== ')')
397 yylval
.ustr
= get_diff (input_stream_pos
, start_token
);
405 while (input_stream_pos
[0] != '\n' && input_stream_pos
[0] != 0)
407 if (input_stream_pos
[0] == '\n')
409 yylval
.ustr
= get_diff (input_stream_pos
, start_token
);
421 case '0': case '1': case '2': case '3': case '4':
422 case '5': case '6': case '7': case '8': case '9':
423 yylval
.ival
= parse_digit (ch
);
429 while (input_stream_pos
[0] >= 0x40 || (input_stream_pos
[0] >= '0' && input_stream_pos
[0] <= '9'))
431 ret
= mc_token (start_token
, (size_t) (input_stream_pos
- start_token
));
434 yylval
.ustr
= get_diff (input_stream_pos
, start_token
);
437 yyerror ("illegal character 0x%x.", ch
);