]>
Commit | Line | Data |
---|---|---|
abbffdf9 UD |
1 | %{ |
2 | /* Expression parsing for plural form selection. | |
3 | Copyright (C) 2000 Free Software Foundation, Inc. | |
4 | Written by Ulrich Drepper <drepper@cygnus.com>, 2000. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Library General Public License as | |
8 | published by the Free Software Foundation; either version 2 of the | |
9 | License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Library General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Library General Public | |
17 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include <stdarg.h> | |
22 | #include <stdlib.h> | |
23 | #include "gettext.h" | |
24 | #include "gettextP.h" | |
25 | ||
26 | #define YYLEX_PARAM &((struct parse_args *) arg)->cp | |
27 | #define YYPARSE_PARAM arg | |
28 | %} | |
29 | %pure_parser | |
30 | %expect 10 | |
31 | ||
32 | %union { | |
33 | unsigned long int num; | |
34 | struct expression *exp; | |
35 | } | |
36 | ||
37 | %{ | |
38 | /* Prototypes for local functions. */ | |
39 | static struct expression *new_exp (enum operator op, ...); | |
40 | static int yylex (YYSTYPE *lval, const char **pexp); | |
41 | static void yyerror (const char *str); | |
42 | %} | |
43 | ||
44 | %left '?' | |
45 | %left '|' | |
46 | %left '&' | |
47 | %left '=', '!' | |
48 | %left '+', '-' | |
49 | %left '*', '/', '%' | |
50 | %token <num> NUMBER | |
51 | %type <exp> exp | |
52 | ||
53 | %% | |
54 | ||
55 | start: exp | |
56 | { | |
57 | ((struct parse_args *) arg)->res = $1; | |
58 | } | |
59 | ; | |
60 | ||
61 | exp: exp '?' exp ':' exp | |
62 | { | |
63 | if (($$ = new_exp (qmop, $1, $3, $5, NULL)) == NULL) | |
64 | YYABORT | |
65 | } | |
66 | | exp '|' exp | |
67 | { | |
68 | if (($$ = new_exp (lor, $1, $3, NULL)) == NULL) | |
69 | YYABORT | |
70 | } | |
71 | | exp '&' exp | |
72 | { | |
73 | if (($$ = new_exp (land, $1, $3, NULL)) == NULL) | |
74 | YYABORT | |
75 | } | |
76 | | exp '=' exp | |
77 | { | |
78 | if (($$ = new_exp (equal, $1, $3, NULL)) == NULL) | |
79 | YYABORT | |
80 | } | |
81 | | exp '!' exp | |
82 | { | |
83 | if (($$ = new_exp (not_equal, $1, $3, NULL)) == NULL) | |
84 | YYABORT | |
85 | } | |
86 | | exp '+' exp | |
87 | { | |
88 | if (($$ = new_exp (plus, $1, $3, NULL)) == NULL) | |
89 | YYABORT | |
90 | } | |
91 | | exp '-' exp | |
92 | { | |
93 | if (($$ = new_exp (minus, $1, $3, NULL)) == NULL) | |
94 | YYABORT | |
95 | } | |
96 | | exp '*' exp | |
97 | { | |
98 | if (($$ = new_exp (mult, $1, $3, NULL)) == NULL) | |
99 | YYABORT | |
100 | } | |
101 | | exp '/' exp | |
102 | { | |
103 | if (($$ = new_exp (divide, $1, $3, NULL)) == NULL) | |
104 | YYABORT | |
105 | } | |
106 | | exp '%' exp | |
107 | { | |
108 | if (($$ = new_exp (module, $1, $3, NULL)) == NULL) | |
109 | YYABORT | |
110 | } | |
111 | | 'n' | |
112 | { | |
113 | if (($$ = new_exp (var, NULL)) == NULL) | |
114 | YYABORT | |
115 | } | |
116 | | NUMBER | |
117 | { | |
118 | if (($$ = new_exp (num, NULL)) == NULL) | |
119 | YYABORT; | |
120 | $$->val.num = $1 | |
121 | } | |
122 | | '(' exp ')' | |
123 | { | |
124 | $$ = $2 | |
125 | } | |
126 | ; | |
127 | ||
128 | %% | |
129 | ||
130 | static struct expression * | |
131 | new_exp (enum operator op, ...) | |
132 | { | |
133 | struct expression *newp = (struct expression *) malloc (sizeof (*newp)); | |
134 | va_list va; | |
135 | struct expression *next; | |
136 | ||
137 | va_start (va, op); | |
138 | ||
139 | if (newp == NULL) | |
140 | while ((next = va_arg (va, struct expression *)) != NULL) | |
141 | __gettext_free_exp (next); | |
142 | else | |
143 | { | |
144 | newp->operation = op; | |
145 | next = va_arg (va, struct expression *); | |
146 | if (next != NULL) | |
147 | { | |
148 | newp->val.args3.bexp = next; | |
149 | next = va_arg (va, struct expression *); | |
150 | if (next != NULL) | |
151 | { | |
152 | newp->val.args3.tbranch = next; | |
153 | next = va_arg (va, struct expression *); | |
154 | if (next != NULL) | |
155 | newp->val.args3.fbranch = next; | |
156 | } | |
157 | } | |
158 | } | |
159 | ||
160 | va_end (va); | |
161 | ||
162 | return newp; | |
163 | } | |
164 | ||
165 | void | |
166 | internal_function | |
167 | __gettext_free_exp (struct expression *exp) | |
168 | { | |
169 | if (exp == NULL) | |
170 | return; | |
171 | ||
172 | /* Handle the recursive case. */ | |
173 | switch (exp->operation) | |
174 | { | |
175 | case qmop: | |
176 | __gettext_free_exp (exp->val.args3.fbranch); | |
177 | /* FALLTHROUGH */ | |
178 | ||
179 | case mult: | |
180 | case divide: | |
181 | case module: | |
182 | case plus: | |
183 | case minus: | |
184 | case equal: | |
185 | case not_equal: | |
186 | case land: | |
187 | case lor: | |
188 | __gettext_free_exp (exp->val.args2.right); | |
189 | __gettext_free_exp (exp->val.args2.left); | |
190 | break; | |
191 | ||
192 | default: | |
193 | break; | |
194 | } | |
195 | ||
196 | free (exp); | |
197 | } | |
198 | ||
199 | ||
200 | static int | |
201 | yylex (YYSTYPE *lval, const char **pexp) | |
202 | { | |
203 | const char *exp = *pexp; | |
204 | int result; | |
205 | ||
206 | while (1) | |
207 | { | |
208 | if (exp[0] == '\\' && exp[1] == '\n') | |
209 | { | |
210 | exp += 2; | |
211 | continue; | |
212 | } | |
213 | if (exp[0] != '\0' && exp[0] != ' ' && exp[0] != '\t') | |
214 | break; | |
215 | ||
216 | ++exp; | |
217 | } | |
218 | ||
219 | result = *exp++; | |
220 | switch (result) | |
221 | { | |
222 | case '0' ... '9': | |
223 | { | |
224 | unsigned long int n = exp[-1] - '0'; | |
225 | while (exp[0] >= '0' && exp[0] <= '9') | |
226 | { | |
227 | n *= 10; | |
228 | n += exp[0] - '0'; | |
229 | ++exp; | |
230 | } | |
231 | lval->num = n; | |
232 | result = NUMBER; | |
233 | } | |
234 | break; | |
235 | ||
236 | case '=': | |
237 | case '!': | |
238 | if (exp[0] == '=') | |
239 | ++exp; | |
240 | else | |
241 | result = YYERRCODE; | |
242 | break; | |
243 | ||
244 | case '&': | |
245 | case '|': | |
246 | if (exp[0] == result) | |
247 | ++exp; | |
248 | else | |
249 | result = YYERRCODE; | |
250 | break; | |
251 | ||
252 | case 'n': | |
253 | case '*': | |
254 | case '/': | |
255 | case '%': | |
256 | case '+': | |
257 | case '-': | |
258 | case '?': | |
259 | case ':': | |
260 | case '(': | |
261 | case ')': | |
262 | /* Nothing, just return the character. */ | |
263 | break; | |
264 | ||
265 | case '\n': | |
266 | case '\0': | |
267 | /* Be safe and let the user call this function again. */ | |
268 | --exp; | |
269 | result = YYEOF; | |
270 | break; | |
271 | ||
272 | default: | |
273 | result = YYERRCODE; | |
274 | #if YYDEBUG != 0 | |
275 | --exp; | |
276 | #endif | |
277 | break; | |
278 | } | |
279 | ||
280 | *pexp = exp; | |
281 | ||
282 | return result; | |
283 | } | |
284 | ||
285 | ||
286 | static void | |
287 | yyerror (const char *str) | |
288 | { | |
289 | /* Do nothing. We don't print error messages here. */ | |
290 | } |