aboutsummaryrefslogtreecommitdiff
path: root/binutils/mcparse.y
blob: 2cebd81d757abdf3457a921f4e263299cd831248 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
%{ /* mcparse.y -- parser for Windows mc files
  Copyright (C) 2007-2017 Free Software Foundation, Inc.

  Parser for Windows mc files
  Written by Kai Tietz, Onevision.

  This file is part of GNU Binutils.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  02110-1301, USA.  */

/* This is a parser for Windows rc files.  It is based on the parser
   by Gunther Ebert <gunther.ebert@ixos-leipzig.de>.  */

#include "sysdep.h"
#include "bfd.h"
#include "bucomm.h"
#include "libiberty.h"
#include "windmc.h"
#include "safe-ctype.h"

static rc_uint_type mc_last_id = 0;
static rc_uint_type mc_sefa_val = 0;
static unichar *mc_last_symbol = NULL;
static const mc_keyword *mc_cur_severity = NULL;
static const mc_keyword *mc_cur_facility = NULL;
static mc_node *cur_node = NULL;

%}

%union
{
  rc_uint_type ival;
  unichar *ustr;
  const mc_keyword *tok;
  mc_node *nod;
};

%start input

%token NL
%token<ustr> MCIDENT MCFILENAME MCLINE MCCOMMENT
%token<tok> MCTOKEN
%token MCENDLINE
%token MCLANGUAGENAMES MCFACILITYNAMES MCSEVERITYNAMES MCOUTPUTBASE MCMESSAGEIDTYPEDEF
%token MCLANGUAGE MCMESSAGEID MCSEVERITY MCFACILITY MCSYMBOLICNAME
%token <ival> MCNUMBER

%type<ival> id vid sefasy_def
%type<ustr> alias_name token lines comments
%type<tok> lang

%%
input:	  entities
	;

entities:
	  /* empty */
	| entities entity
	;
entity:	  global_section
	| message
	| comments
	  {
	    cur_node = mc_add_node ();
	    cur_node->user_text = $1;
	  }
	| error	{ mc_fatal ("syntax error"); }
;

global_section:
	  MCSEVERITYNAMES '=' '(' severitymaps ')'
	| MCSEVERITYNAMES '=' '(' severitymaps error { mc_fatal ("missing ')' in SeverityNames"); }
	| MCSEVERITYNAMES '=' error { mc_fatal ("missing '(' in SeverityNames"); }
	| MCSEVERITYNAMES error { mc_fatal ("missing '=' for SeverityNames"); }
	| MCLANGUAGENAMES '=' '(' langmaps ')'
	| MCLANGUAGENAMES '=' '(' langmaps error { mc_fatal ("missing ')' in LanguageNames"); }
	| MCLANGUAGENAMES '=' error { mc_fatal ("missing '(' in LanguageNames"); }
	| MCLANGUAGENAMES error { mc_fatal ("missing '=' for LanguageNames"); }
	| MCFACILITYNAMES '=' '(' facilitymaps ')'
	| MCFACILITYNAMES '=' '(' facilitymaps error { mc_fatal ("missing ')' in FacilityNames"); }
	| MCFACILITYNAMES '=' error { mc_fatal ("missing '(' in FacilityNames"); }
	| MCFACILITYNAMES error { mc_fatal ("missing '=' for FacilityNames"); }
	| MCOUTPUTBASE '=' MCNUMBER
	  {
	    if ($3 != 10 && $3 != 16)
	      mc_fatal ("OutputBase allows 10 or 16 as value");
	    mcset_out_values_are_decimal = ($3 == 10 ? 1 : 0);
	  }
	| MCMESSAGEIDTYPEDEF '=' MCIDENT
	  {
	    mcset_msg_id_typedef = $3;
	  }
	| MCMESSAGEIDTYPEDEF '=' error
	  {
	    mc_fatal ("MessageIdTypedef expects an identifier");
	  }
	| MCMESSAGEIDTYPEDEF error
	  {
	    mc_fatal ("missing '=' for MessageIdTypedef");
	  }
;

severitymaps:
	  severitymap
	| severitymaps severitymap
	| error { mc_fatal ("severity ident missing"); }
;

severitymap:
	  token '=' MCNUMBER alias_name
	  {
	    mc_add_keyword ($1, MCTOKEN, "severity", $3, $4);
	  }
	| token '=' error { mc_fatal ("severity number missing"); }
	| token error { mc_fatal ("severity missing '='"); }
;

facilitymaps:
	  facilitymap
	| facilitymaps facilitymap
	| error { mc_fatal ("missing ident in FacilityNames"); }
;

facilitymap:
	  token '=' MCNUMBER alias_name
	  {
	    mc_add_keyword ($1, MCTOKEN, "facility", $3, $4);
	  }
	| token '=' error { mc_fatal ("facility number missing"); }
	| token error { mc_fatal ("facility missing '='"); }
;

langmaps:
	  langmap
	| langmaps langmap
	| error { mc_fatal ("missing ident in LanguageNames"); }
;

langmap:
	  token '=' MCNUMBER lex_want_filename ':' MCFILENAME
	  {
	    mc_add_keyword ($1, MCTOKEN, "language", $3, $6);
	  }
	| token '=' MCNUMBER lex_want_filename ':' error { mc_fatal ("missing filename in LanguageNames"); }
	| token '=' MCNUMBER error { mc_fatal ("missing ':' in LanguageNames"); }
	| token '=' error { mc_fatal ("missing language code in LanguageNames"); }
	| token error { mc_fatal ("missing '=' for LanguageNames"); }
;

alias_name:
	  /* empty */
	  {
	    $$ = NULL;
	  }
	| ':' MCIDENT
	  {
	    $$ = $2;
	  }
	| ':' error { mc_fatal ("illegal token in identifier"); $$ = NULL; }
;

message:
	  id sefasy_def
	  {
	    cur_node = mc_add_node ();
	    cur_node->symbol = mc_last_symbol;
	    cur_node->facility = mc_cur_facility;
	    cur_node->severity = mc_cur_severity;
	    cur_node->id = ($1 & 0xffffUL);
	    cur_node->vid = ($1 & 0xffffUL) | mc_sefa_val;
	    mc_last_id = $1;
	  }
	  lang_entities
;

id:	  MCMESSAGEID '=' vid { $$ = $3; }
	| MCMESSAGEID '=' error { mc_fatal ("missing number in MessageId"); $$ = 0; }
	| MCMESSAGEID error { mc_fatal ("missing '=' for MessageId"); $$ = 0; }
;

vid:	  /* empty */
	  {
	    $$ = ++mc_last_id;
	  }
	| MCNUMBER
	  {
	    $$ = $1;
	  }
	| '+' MCNUMBER
	  {
	    $$ = mc_last_id + $2;
	  }
	| '+' error { mc_fatal ("missing number after MessageId '+'"); }
;

sefasy_def:
	  /* empty */
	  {
	    $$ = 0;
	    mc_sefa_val = (mcset_custom_bit ? 1 : 0) << 29;
	    mc_last_symbol = NULL;
	    mc_cur_severity = NULL;
	    mc_cur_facility = NULL;
	  }
	| sefasy_def severity
	  {
	    if ($1 & 1)
	      mc_warn (_("duplicate definition of Severity"));
	    $$ = $1 | 1;
	  }
	| sefasy_def facility
	  {
	    if ($1 & 2)
	      mc_warn (_("duplicate definition of Facility"));
	    $$ = $1 | 2;
	  }
	| sefasy_def symbol
	  {
	    if ($1 & 4)
	      mc_warn (_("duplicate definition of SymbolicName"));
	    $$ = $1 | 4;
	  }
;

severity: MCSEVERITY '=' MCTOKEN
	  {
	    mc_sefa_val &= ~ (0x3UL << 30);
	    mc_sefa_val |= (($3->nval & 0x3UL) << 30);
	    mc_cur_severity = $3;
	  }
;

facility: MCFACILITY '=' MCTOKEN
	  {
	    mc_sefa_val &= ~ (0xfffUL << 16);
	    mc_sefa_val |= (($3->nval & 0xfffUL) << 16);
	    mc_cur_facility = $3;
	  }
;

symbol: MCSYMBOLICNAME '=' MCIDENT
	{
	  mc_last_symbol = $3;
	}
;

lang_entities:
	  lang_entity
	| lang_entities lang_entity
;

lang_entity:
	  lang lex_want_line lines MCENDLINE
	  {
	    mc_node_lang *h;
	    h = mc_add_node_lang (cur_node, $1, cur_node->vid);
	    h->message = $3;
	    if (mcset_max_message_length != 0 && unichar_len (h->message) > mcset_max_message_length)
	      mc_warn ("message length to long");
	  }
;

lines:	  MCLINE
	  {
	    $$ = $1;
	  }
	| lines MCLINE
	  {
	    unichar *h;
	    rc_uint_type l1,l2;
	    l1 = unichar_len ($1);
	    l2 = unichar_len ($2);
	    h = (unichar *) res_alloc ((l1 + l2 + 1) * sizeof (unichar));
	    if (l1) memcpy (h, $1, l1 * sizeof (unichar));
	    if (l2) memcpy (&h[l1], $2, l2 * sizeof (unichar));
	    h[l1 + l2] = 0;
	    $$ = h;
	  }
	| error { mc_fatal ("missing end of message text"); $$ = NULL; }
	| lines error { mc_fatal ("missing end of message text"); $$ = $1; }
;

comments: MCCOMMENT { $$ = $1; }
	| comments MCCOMMENT
	  {
	    unichar *h;
	    rc_uint_type l1,l2;
	    l1 = unichar_len ($1);
	    l2 = unichar_len ($2);
	    h = (unichar *) res_alloc ((l1 + l2 + 1) * sizeof (unichar));
	    if (l1) memcpy (h, $1, l1 * sizeof (unichar));
	    if (l2) memcpy (&h[l1], $2, l2 * sizeof (unichar));
	    h[l1 + l2] = 0;
	    $$ = h;
	  }
;

lang:	  MCLANGUAGE lex_want_nl '=' MCTOKEN NL
	  {
	    $$ = $4;
	  }
	| MCLANGUAGE lex_want_nl '=' MCIDENT NL
	  {
	    $$ = NULL;
	    mc_fatal (_("undeclared language identifier"));
	  }
	| MCLANGUAGE lex_want_nl '=' token error
	  {
	    $$ = NULL;
	    mc_fatal ("missing newline after Language");
	  }
	| MCLANGUAGE lex_want_nl '=' error
	  {
	    $$ = NULL;
	    mc_fatal ("missing ident for Language");
	  }
	| MCLANGUAGE error
	  {
	    $$ = NULL;
	    mc_fatal ("missing '=' for Language");
	  }
;

token: 	MCIDENT { $$ = $1; }
	|  MCTOKEN { $$ = $1->usz; }
;

lex_want_nl:
	  /* Empty */	{ mclex_want_nl = 1; }
;

lex_want_line:
	  /* Empty */	{ mclex_want_line = 1; }
;

lex_want_filename:
	  /* Empty */	{ mclex_want_filename = 1; }
;

%%

/* Something else.  */