]>
Commit | Line | Data |
---|---|---|
abbffdf9 UD |
1 | %{ |
2 | /* Expression parsing for plural form selection. | |
eda6c725 | 3 | Copyright (C) 2000, 2001 Free Software Foundation, Inc. |
41bdb6e2 | 4 | This file is part of the GNU C Library. |
abbffdf9 UD |
5 | Written by Ulrich Drepper <drepper@cygnus.com>, 2000. |
6 | ||
7 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
8 | modify it under the terms of the GNU Lesser General Public |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
abbffdf9 UD |
11 | |
12 | The GNU C Library 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 GNU | |
41bdb6e2 | 15 | Lesser General Public License for more details. |
abbffdf9 | 16 | |
41bdb6e2 AJ |
17 | You should have received a copy of the GNU Lesser General Public |
18 | License along with the GNU C Library; if not, write to the Free | |
19 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
20 | 02111-1307 USA. */ | |
abbffdf9 | 21 | |
61402fd6 UD |
22 | #ifdef HAVE_CONFIG_H |
23 | # include <config.h> | |
24 | #endif | |
25 | ||
abbffdf9 | 26 | #include <stdlib.h> |
abbffdf9 UD |
27 | #include "gettextP.h" |
28 | ||
4a4d50f3 UD |
29 | /* Names for the libintl functions are a problem. They must not clash |
30 | with existing names and they should follow ANSI C. But this source | |
31 | code is also used in GNU C Library where the names have a __ | |
32 | prefix. So we have to make a difference here. */ | |
33 | #ifdef _LIBC | |
34 | # define FREE_EXPRESSION __gettext_free_exp | |
35 | #else | |
36 | # define FREE_EXPRESSION gettext_free_exp__ | |
37 | # define __gettextparse gettextparse__ | |
38 | #endif | |
39 | ||
abbffdf9 UD |
40 | #define YYLEX_PARAM &((struct parse_args *) arg)->cp |
41 | #define YYPARSE_PARAM arg | |
42 | %} | |
43 | %pure_parser | |
44 | %expect 10 | |
45 | ||
46 | %union { | |
47 | unsigned long int num; | |
4a4d50f3 | 48 | enum operator op; |
abbffdf9 UD |
49 | struct expression *exp; |
50 | } | |
51 | ||
52 | %{ | |
53 | /* Prototypes for local functions. */ | |
4a4d50f3 UD |
54 | static struct expression *new_exp PARAMS ((int nargs, enum operator op, |
55 | struct expression * const *args)); | |
56 | static inline struct expression *new_exp_0 PARAMS ((enum operator op)); | |
57 | static inline struct expression *new_exp_1 PARAMS ((enum operator op, | |
58 | struct expression *right)); | |
59 | static struct expression *new_exp_2 PARAMS ((enum operator op, | |
60 | struct expression *left, | |
61 | struct expression *right)); | |
62 | static inline struct expression *new_exp_3 PARAMS ((enum operator op, | |
63 | struct expression *bexp, | |
64 | struct expression *tbranch, | |
65 | struct expression *fbranch)); | |
66 | static int yylex PARAMS ((YYSTYPE *lval, const char **pexp)); | |
67 | static void yyerror PARAMS ((const char *str)); | |
68 | ||
69 | /* Allocation of expressions. */ | |
70 | ||
71 | static struct expression * | |
72 | new_exp (nargs, op, args) | |
73 | int nargs; | |
74 | enum operator op; | |
75 | struct expression * const *args; | |
76 | { | |
77 | int i; | |
78 | struct expression *newp; | |
79 | ||
80 | /* If any of the argument could not be malloc'ed, just return NULL. */ | |
81 | for (i = nargs - 1; i >= 0; i--) | |
82 | if (args[i] == NULL) | |
83 | goto fail; | |
84 | ||
85 | /* Allocate a new expression. */ | |
86 | newp = (struct expression *) malloc (sizeof (*newp)); | |
87 | if (newp != NULL) | |
88 | { | |
89 | newp->nargs = nargs; | |
90 | newp->operation = op; | |
91 | for (i = nargs - 1; i >= 0; i--) | |
92 | newp->val.args[i] = args[i]; | |
93 | return newp; | |
94 | } | |
95 | ||
96 | fail: | |
97 | for (i = nargs - 1; i >= 0; i--) | |
98 | FREE_EXPRESSION (args[i]); | |
99 | ||
100 | return NULL; | |
101 | } | |
102 | ||
103 | static inline struct expression * | |
104 | new_exp_0 (op) | |
105 | enum operator op; | |
106 | { | |
107 | return new_exp (0, op, NULL); | |
108 | } | |
109 | ||
110 | static inline struct expression * | |
111 | new_exp_1 (op, right) | |
112 | enum operator op; | |
113 | struct expression *right; | |
114 | { | |
115 | struct expression *args[1]; | |
116 | ||
117 | args[0] = right; | |
118 | return new_exp (1, op, args); | |
119 | } | |
120 | ||
121 | static struct expression * | |
122 | new_exp_2 (op, left, right) | |
123 | enum operator op; | |
124 | struct expression *left; | |
125 | struct expression *right; | |
126 | { | |
127 | struct expression *args[2]; | |
128 | ||
129 | args[0] = left; | |
130 | args[1] = right; | |
131 | return new_exp (2, op, args); | |
132 | } | |
133 | ||
134 | static inline struct expression * | |
135 | new_exp_3 (op, bexp, tbranch, fbranch) | |
136 | enum operator op; | |
137 | struct expression *bexp; | |
138 | struct expression *tbranch; | |
139 | struct expression *fbranch; | |
140 | { | |
141 | struct expression *args[3]; | |
142 | ||
143 | args[0] = bexp; | |
144 | args[1] = tbranch; | |
145 | args[2] = fbranch; | |
146 | return new_exp (3, op, args); | |
147 | } | |
148 | ||
abbffdf9 UD |
149 | %} |
150 | ||
4a4d50f3 UD |
151 | /* This declares that all operators have the same associativity and the |
152 | precedence order as in C. See [Harbison, Steele: C, A Reference Manual]. | |
153 | There is no unary minus and no bitwise operators. | |
154 | Operators with the same syntactic behaviour have been merged into a single | |
155 | token, to save space in the array generated by bison. */ | |
156 | %right '?' /* ? */ | |
157 | %left '|' /* || */ | |
158 | %left '&' /* && */ | |
159 | %left EQUOP2 /* == != */ | |
160 | %left CMPOP2 /* < > <= >= */ | |
161 | %left ADDOP2 /* + - */ | |
162 | %left MULOP2 /* * / % */ | |
163 | %right '!' /* ! */ | |
164 | ||
165 | %token <op> EQUOP2 CMPOP2 ADDOP2 MULOP2 | |
abbffdf9 UD |
166 | %token <num> NUMBER |
167 | %type <exp> exp | |
168 | ||
169 | %% | |
170 | ||
171 | start: exp | |
172 | { | |
4a4d50f3 UD |
173 | if ($1 == NULL) |
174 | YYABORT; | |
abbffdf9 UD |
175 | ((struct parse_args *) arg)->res = $1; |
176 | } | |
177 | ; | |
178 | ||
179 | exp: exp '?' exp ':' exp | |
180 | { | |
4a4d50f3 | 181 | $$ = new_exp_3 (qmop, $1, $3, $5); |
abbffdf9 UD |
182 | } |
183 | | exp '|' exp | |
184 | { | |
4a4d50f3 | 185 | $$ = new_exp_2 (lor, $1, $3); |
abbffdf9 UD |
186 | } |
187 | | exp '&' exp | |
188 | { | |
4a4d50f3 | 189 | $$ = new_exp_2 (land, $1, $3); |
abbffdf9 | 190 | } |
4a4d50f3 | 191 | | exp EQUOP2 exp |
abbffdf9 | 192 | { |
4a4d50f3 | 193 | $$ = new_exp_2 ($2, $1, $3); |
abbffdf9 | 194 | } |
4a4d50f3 | 195 | | exp CMPOP2 exp |
abbffdf9 | 196 | { |
4a4d50f3 | 197 | $$ = new_exp_2 ($2, $1, $3); |
abbffdf9 | 198 | } |
4a4d50f3 | 199 | | exp ADDOP2 exp |
abbffdf9 | 200 | { |
4a4d50f3 | 201 | $$ = new_exp_2 ($2, $1, $3); |
abbffdf9 | 202 | } |
4a4d50f3 | 203 | | exp MULOP2 exp |
abbffdf9 | 204 | { |
4a4d50f3 | 205 | $$ = new_exp_2 ($2, $1, $3); |
abbffdf9 | 206 | } |
4a4d50f3 | 207 | | '!' exp |
abbffdf9 | 208 | { |
4a4d50f3 | 209 | $$ = new_exp_1 (lnot, $2); |
abbffdf9 UD |
210 | } |
211 | | 'n' | |
212 | { | |
4a4d50f3 | 213 | $$ = new_exp_0 (var); |
abbffdf9 UD |
214 | } |
215 | | NUMBER | |
216 | { | |
4a4d50f3 UD |
217 | if (($$ = new_exp_0 (num)) != NULL) |
218 | $$->val.num = $1; | |
abbffdf9 UD |
219 | } |
220 | | '(' exp ')' | |
221 | { | |
4a4d50f3 | 222 | $$ = $2; |
abbffdf9 UD |
223 | } |
224 | ; | |
225 | ||
226 | %% | |
227 | ||
abbffdf9 UD |
228 | void |
229 | internal_function | |
4a4d50f3 UD |
230 | FREE_EXPRESSION (exp) |
231 | struct expression *exp; | |
abbffdf9 UD |
232 | { |
233 | if (exp == NULL) | |
234 | return; | |
235 | ||
236 | /* Handle the recursive case. */ | |
4a4d50f3 | 237 | switch (exp->nargs) |
abbffdf9 | 238 | { |
4a4d50f3 UD |
239 | case 3: |
240 | FREE_EXPRESSION (exp->val.args[2]); | |
241 | /* FALLTHROUGH */ | |
242 | case 2: | |
243 | FREE_EXPRESSION (exp->val.args[1]); | |
244 | /* FALLTHROUGH */ | |
245 | case 1: | |
246 | FREE_EXPRESSION (exp->val.args[0]); | |
abbffdf9 | 247 | /* FALLTHROUGH */ |
abbffdf9 UD |
248 | default: |
249 | break; | |
250 | } | |
251 | ||
252 | free (exp); | |
253 | } | |
254 | ||
255 | ||
256 | static int | |
4a4d50f3 UD |
257 | yylex (lval, pexp) |
258 | YYSTYPE *lval; | |
259 | const char **pexp; | |
abbffdf9 UD |
260 | { |
261 | const char *exp = *pexp; | |
262 | int result; | |
263 | ||
264 | while (1) | |
265 | { | |
2f599545 UD |
266 | if (exp[0] == '\0') |
267 | { | |
268 | *pexp = exp; | |
269 | return YYEOF; | |
270 | } | |
271 | ||
272 | if (exp[0] != ' ' && exp[0] != '\t') | |
abbffdf9 UD |
273 | break; |
274 | ||
275 | ++exp; | |
276 | } | |
277 | ||
278 | result = *exp++; | |
279 | switch (result) | |
280 | { | |
61402fd6 UD |
281 | case '0': case '1': case '2': case '3': case '4': |
282 | case '5': case '6': case '7': case '8': case '9': | |
abbffdf9 | 283 | { |
eda6c725 | 284 | unsigned long int n = result - '0'; |
abbffdf9 UD |
285 | while (exp[0] >= '0' && exp[0] <= '9') |
286 | { | |
287 | n *= 10; | |
288 | n += exp[0] - '0'; | |
289 | ++exp; | |
290 | } | |
291 | lval->num = n; | |
292 | result = NUMBER; | |
293 | } | |
294 | break; | |
295 | ||
296 | case '=': | |
abbffdf9 | 297 | if (exp[0] == '=') |
4a4d50f3 UD |
298 | { |
299 | ++exp; | |
300 | lval->op = equal; | |
301 | result = EQUOP2; | |
302 | } | |
abbffdf9 UD |
303 | else |
304 | result = YYERRCODE; | |
305 | break; | |
306 | ||
4a4d50f3 UD |
307 | case '!': |
308 | if (exp[0] == '=') | |
309 | { | |
310 | ++exp; | |
311 | lval->op = not_equal; | |
312 | result = EQUOP2; | |
313 | } | |
314 | break; | |
315 | ||
abbffdf9 UD |
316 | case '&': |
317 | case '|': | |
318 | if (exp[0] == result) | |
319 | ++exp; | |
320 | else | |
321 | result = YYERRCODE; | |
322 | break; | |
323 | ||
4a4d50f3 UD |
324 | case '<': |
325 | if (exp[0] == '=') | |
326 | { | |
327 | ++exp; | |
328 | lval->op = less_or_equal; | |
329 | } | |
330 | else | |
331 | lval->op = less_than; | |
332 | result = CMPOP2; | |
333 | break; | |
334 | ||
335 | case '>': | |
336 | if (exp[0] == '=') | |
337 | { | |
338 | ++exp; | |
339 | lval->op = greater_or_equal; | |
340 | } | |
341 | else | |
342 | lval->op = greater_than; | |
343 | result = CMPOP2; | |
344 | break; | |
345 | ||
abbffdf9 | 346 | case '*': |
4a4d50f3 UD |
347 | lval->op = mult; |
348 | result = MULOP2; | |
349 | break; | |
350 | ||
abbffdf9 | 351 | case '/': |
4a4d50f3 UD |
352 | lval->op = divide; |
353 | result = MULOP2; | |
354 | break; | |
355 | ||
abbffdf9 | 356 | case '%': |
4a4d50f3 UD |
357 | lval->op = module; |
358 | result = MULOP2; | |
359 | break; | |
360 | ||
abbffdf9 | 361 | case '+': |
4a4d50f3 UD |
362 | lval->op = plus; |
363 | result = ADDOP2; | |
364 | break; | |
365 | ||
abbffdf9 | 366 | case '-': |
4a4d50f3 UD |
367 | lval->op = minus; |
368 | result = ADDOP2; | |
369 | break; | |
370 | ||
371 | case 'n': | |
abbffdf9 UD |
372 | case '?': |
373 | case ':': | |
374 | case '(': | |
375 | case ')': | |
376 | /* Nothing, just return the character. */ | |
377 | break; | |
378 | ||
2f599545 | 379 | case ';': |
abbffdf9 UD |
380 | case '\n': |
381 | case '\0': | |
382 | /* Be safe and let the user call this function again. */ | |
383 | --exp; | |
384 | result = YYEOF; | |
385 | break; | |
386 | ||
387 | default: | |
388 | result = YYERRCODE; | |
389 | #if YYDEBUG != 0 | |
390 | --exp; | |
391 | #endif | |
392 | break; | |
393 | } | |
394 | ||
395 | *pexp = exp; | |
396 | ||
397 | return result; | |
398 | } | |
399 | ||
400 | ||
401 | static void | |
4a4d50f3 UD |
402 | yyerror (str) |
403 | const char *str; | |
abbffdf9 UD |
404 | { |
405 | /* Do nothing. We don't print error messages here. */ | |
406 | } |