]>
Commit | Line | Data |
---|---|---|
087b9fdf | 1 | # Checking the Bison scanner. -*- Autotest -*- |
378f4bd8 | 2 | # Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. |
9b2d0677 AD |
3 | |
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 2, or (at your option) | |
7 | # any later version. | |
8 | ||
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. | |
13 | ||
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software | |
0fb669f9 PE |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | # 02110-1301, USA. | |
9b2d0677 AD |
18 | |
19 | AT_BANNER([[Input Processing.]]) | |
20 | ||
21 | # Mostly test that we are robust to mistakes. | |
22 | ||
6c35d22c | 23 | |
9b2d0677 AD |
24 | ## ------------ ## |
25 | ## Invalid $n. ## | |
26 | ## ------------ ## | |
27 | ||
d46c3fc6 | 28 | AT_SETUP([Invalid dollar-n]) |
9b2d0677 AD |
29 | |
30 | AT_DATA([input.y], | |
31 | [[%% | |
bfcf1f3a | 32 | exp: { $$ = $1 ; }; |
9b2d0677 AD |
33 | ]]) |
34 | ||
35 | AT_CHECK([bison input.y], [1], [], | |
b06af56f | 36 | [[input.y:2.13-14: integer out of range: `$1' |
9b2d0677 AD |
37 | ]]) |
38 | ||
39 | AT_CLEANUP | |
40 | ||
41 | ||
42 | ## ------------ ## | |
43 | ## Invalid @n. ## | |
44 | ## ------------ ## | |
45 | ||
9b2d0677 AD |
46 | AT_SETUP([Invalid @n]) |
47 | ||
48 | AT_DATA([input.y], | |
49 | [[%% | |
bfcf1f3a | 50 | exp: { @$ = @1 ; }; |
9b2d0677 AD |
51 | ]]) |
52 | ||
53 | AT_CHECK([bison input.y], [1], [], | |
b06af56f | 54 | [[input.y:2.13-14: integer out of range: `@1' |
9b2d0677 AD |
55 | ]]) |
56 | ||
57 | AT_CLEANUP | |
9af3fbce AD |
58 | |
59 | ||
60 | ## -------------- ## | |
e776192e | 61 | ## Type Clashes. ## |
9af3fbce AD |
62 | ## -------------- ## |
63 | ||
e776192e | 64 | AT_SETUP([Type Clashes]) |
9af3fbce AD |
65 | |
66 | AT_DATA([input.y], | |
67 | [[%token foo | |
68 | %type <bar> exp | |
69 | %% | |
70 | exp: foo {} foo | |
71 | | foo | |
72 | | /* Empty. */ | |
73 | ; | |
74 | ]]) | |
75 | ||
38e71ff8 | 76 | AT_CHECK([bison input.y], [], [], |
b06af56f PE |
77 | [[input.y:4.6-15: warning: type clash on default action: <bar> != <> |
78 | input.y:5.6-8: warning: type clash on default action: <bar> != <> | |
79 | input.y:6.5: warning: empty rule for typed nonterminal, and no action | |
9af3fbce AD |
80 | ]]) |
81 | ||
82 | AT_CLEANUP | |
5a08f1ce AD |
83 | |
84 | ||
378f4bd8 AD |
85 | ## --------------- ## |
86 | ## Unused values. ## | |
87 | ## --------------- ## | |
88 | ||
84866159 AD |
89 | m4_define([AT_CHECK_UNUSED_VALUES], |
90 | [AT_SETUP([Unused values]) | |
378f4bd8 AD |
91 | |
92 | AT_DATA([input.y], | |
93 | [[%token <integer> INT | |
94 | %type <integer> exp | |
95 | %% | |
96 | exp: | |
84866159 AD |
97 | $1 |
98 | | INT | |
378f4bd8 AD |
99 | ; |
100 | ]]) | |
101 | ||
102 | AT_CHECK([bison input.y], [], [], | |
84866159 AD |
103 | [[$2]]) |
104 | ||
105 | AT_CLEANUP | |
106 | ]) | |
107 | ||
108 | AT_CHECK_UNUSED_VALUES([INT { } INT { } INT { }], | |
109 | [input.y:5.3-25: warning: unset value: $$ | |
378f4bd8 AD |
110 | input.y:5.3-25: warning: unused value: $1 |
111 | input.y:5.3-25: warning: unused value: $3 | |
112 | input.y:5.3-25: warning: unused value: $5 | |
84866159 AD |
113 | ]) |
114 | ||
115 | AT_CHECK_UNUSED_VALUES([INT { $1 } INT { } INT { }], | |
116 | [input.y:5.3-28: warning: unset value: $$ | |
117 | input.y:5.3-28: warning: unused value: $3 | |
118 | input.y:5.3-28: warning: unused value: $5 | |
119 | ]) | |
120 | ||
121 | AT_CHECK_UNUSED_VALUES([INT { } INT { $1 } INT { }], | |
122 | [input.y:5.3-28: warning: unset value: $$ | |
123 | input.y:5.3-28: warning: unused value: $3 | |
124 | input.y:5.3-28: warning: unused value: $5 | |
125 | ]) | |
126 | ||
127 | AT_CHECK_UNUSED_VALUES([INT { } INT { } INT { $1 }], | |
128 | [input.y:5.3-29: warning: unset value: $$ | |
129 | input.y:5.3-29: warning: unused value: $3 | |
130 | input.y:5.3-29: warning: unused value: $5 | |
131 | ]) | |
132 | ||
133 | AT_CHECK_UNUSED_VALUES([INT { } INT { } INT { $$ = $1 + $3 + $5; }]) | |
134 | ||
135 | # Checking mid-rule values. | |
136 | AT_CHECK_UNUSED_VALUES([INT { $$ } INT { $$ } INT { }], | |
137 | [input.y:5.3-31: warning: unset value: $$ | |
138 | input.y:5.3-31: warning: unused value: $1 | |
139 | input.y:5.3-31: warning: unused value: $2 | |
140 | input.y:5.3-31: warning: unused value: $3 | |
141 | input.y:5.3-31: warning: unused value: $4 | |
142 | input.y:5.3-31: warning: unused value: $5 | |
143 | ]) | |
144 | ||
145 | AT_CHECK_UNUSED_VALUES([INT { $$ } INT { $$ = $2 } INT { }], | |
146 | [input.y:5.3-36: warning: unset value: $$ | |
147 | input.y:5.3-36: warning: unused value: $1 | |
148 | input.y:5.3-36: warning: unused value: $3 | |
149 | input.y:5.3-36: warning: unused value: $4 | |
150 | input.y:5.3-36: warning: unused value: $5 | |
151 | ]) | |
152 | ||
153 | # AT_CHECK_UNUSED_VALUES([INT { $$ } { $$ = $2 } { }], | |
154 | # [input.y:5.3-36: warning: unset value: $$ | |
155 | # input.y:5.3-36: warning: unused value: $1 | |
156 | # input.y:5.3-36: warning: unused value: $3 | |
157 | # input.y:5.3-36: warning: unused value: $4 | |
158 | # input.y:5.3-36: warning: unused value: $5 | |
159 | # ]) | |
160 | ||
161 | AT_CHECK_UNUSED_VALUES([INT { $$ = $1 } INT { $$ = $2 + $3 } INT { $$ = $4 + $5 }]) | |
378f4bd8 | 162 | |
378f4bd8 AD |
163 | |
164 | ||
df09ef2e AD |
165 | ## ---------------------- ## |
166 | ## Incompatible Aliases. ## | |
167 | ## ---------------------- ## | |
168 | ||
169 | AT_SETUP([Incompatible Aliases]) | |
170 | ||
171 | AT_DATA([input.y], | |
172 | [[%token foo "foo" | |
173 | ||
174 | %type <bar> foo | |
175 | %printer {bar} foo | |
176 | %destructor {bar} foo | |
177 | %left foo | |
178 | ||
179 | %type <baz> "foo" | |
180 | %printer {baz} "foo" | |
181 | %destructor {baz} "foo" | |
182 | %left "foo" | |
183 | ||
184 | %% | |
185 | exp: foo; | |
186 | ]]) | |
187 | ||
188 | AT_CHECK([bison input.y], [1], [], | |
189 | [[input.y:8.7-11: %type redeclaration for foo | |
190 | input.y:3.7-11: first declaration | |
191 | input.y:10.13-17: %destructor redeclaration for foo | |
192 | input.y:5.13-17: first declaration | |
193 | input.y:9.19-23: %printer redeclaration for foo | |
194 | input.y:10.13-17: first declaration | |
195 | input.y:11.1-5: %left redeclaration for foo | |
196 | input.y:6.1-5: first declaration | |
197 | ]]) | |
198 | ||
199 | AT_CLEANUP | |
200 | ||
201 | ||
5a08f1ce AD |
202 | |
203 | ## ----------------------- ## | |
204 | ## Torturing the Scanner. ## | |
205 | ## ----------------------- ## | |
206 | ||
207 | # Be sure to compile and run, so that the C compiler checks what | |
208 | # we do. | |
209 | ||
210 | AT_SETUP([Torturing the Scanner]) | |
211 | ||
0baf7c50 PE |
212 | |
213 | AT_DATA([input.y], []) | |
214 | AT_CHECK([bison input.y], [1], [], | |
ba20a264 | 215 | [[input.y:1.1: syntax error, unexpected end of file |
0baf7c50 PE |
216 | ]]) |
217 | ||
218 | ||
9501dc6e | 219 | AT_DATA_GRAMMAR([input.y], |
5a08f1ce AD |
220 | [[%{ |
221 | /* This is seen in GCC: a %{ and %} in middle of a comment. */ | |
222 | const char *foo = "So %{ and %} can be here too."; | |
223 | ||
dda7a53e PE |
224 | #if 0 |
225 | /* These examples test Bison while not stressing C compilers too much. | |
226 | Many C compilers mishandle backslash-newlines, so this part of the | |
227 | test is inside "#if 0". The comment and string are written so that | |
228 | the "#endif" will be seen regardless of the C compiler bugs that we | |
229 | know about, namely: | |
230 | ||
231 | HP C (as of late 2002) mishandles *\[newline]\[newline]/ within a | |
232 | comment. | |
233 | ||
234 | The Apple Darwin compiler (as of late 2002) mishandles | |
235 | \\[newline]' within a character constant. | |
236 | ||
237 | */ | |
238 | ||
206fe6a5 | 239 | /\ |
dda7a53e PE |
240 | * A comment with backslash-newlines in it. %} *\ |
241 | \ | |
206fe6a5 | 242 | / |
dda7a53e | 243 | /* { Close the above comment, if the C compiler mishandled it. */ |
206fe6a5 PE |
244 | |
245 | char str[] = "\\ | |
246 | " A string with backslash-newlines in it %{ %} \\ | |
dda7a53e | 247 | \ |
206fe6a5 PE |
248 | ""; |
249 | ||
dda7a53e | 250 | char apostrophe = '\''; |
206fe6a5 PE |
251 | #endif |
252 | ||
5a08f1ce AD |
253 | #include <stdio.h> |
254 | %} | |
255 | /* %{ and %} can be here too. */ | |
256 | ||
257 | %{ | |
258 | /* Exercise pre-prologue dependency to %union. */ | |
051ade83 | 259 | typedef int value; |
5a08f1ce AD |
260 | %} |
261 | ||
262 | /* Exercise M4 quoting: '@:>@@:>@', 0. */ | |
263 | ||
264 | /* Also exercise %union. */ | |
265 | %union | |
266 | { | |
051ade83 | 267 | value ival; /* A comment to exercise an old bug. */ |
5a08f1ce AD |
268 | }; |
269 | ||
270 | ||
271 | /* Exercise post-prologue dependency to %union. */ | |
272 | %{ | |
051ade83 | 273 | static YYSTYPE value_as_yystype (value val); |
5a08f1ce AD |
274 | |
275 | /* Exercise quotes in declarations. */ | |
276 | char quote[] = "@:>@@:>@,"; | |
277 | %} | |
278 | ||
279 | %{ | |
280 | static void yyerror (const char *s); | |
281 | static int yylex (void); | |
282 | %} | |
283 | ||
206fe6a5 | 284 | %type <ival> '@<:@' |
5a08f1ce AD |
285 | |
286 | /* Exercise quotes in strings. */ | |
3dc4c5fa | 287 | %token FAKE "fake @<:@@:>@ \a\b\f\n\r\t\v\"\'\?\\\u005B\U0000005c ??!??'??(??)??-??/??<??=??> \x1\1" |
5a08f1ce AD |
288 | |
289 | %% | |
206fe6a5 | 290 | /* Exercise M4 quoting: '@:>@@:>@', @<:@, 1. */ |
087b9fdf | 291 | exp: '@<:@' '\1' two '$' '@' '{' oline output.or.oline.opt |
5a08f1ce AD |
292 | { |
293 | /* Exercise quotes in braces. */ | |
294 | char tmp[] = "@<:@%c@:>@,\n"; | |
295 | printf (tmp, $1); | |
296 | } | |
297 | ; | |
5b7f88c7 PE |
298 | |
299 | two: '\x000000000000000000000000000000000000000000000000000000000000000000002'; | |
300 | oline: '@' 'o' 'l' 'i' 'n' 'e' '@' '_' '_' 'o' 'l' 'i' 'n' 'e' '_' '_'; | |
087b9fdf | 301 | output.or.oline.opt: ;|oline;;|output;;; |
5b7f88c7 | 302 | output: '#' 'o' 'u' 't' 'p' 'u' 't' ' '; |
5a08f1ce | 303 | %% |
206fe6a5 | 304 | /* Exercise M4 quoting: '@:>@@:>@', @<:@, 2. */ |
5a08f1ce AD |
305 | |
306 | static YYSTYPE | |
051ade83 | 307 | value_as_yystype (value val) |
5a08f1ce AD |
308 | { |
309 | YYSTYPE res; | |
310 | res.ival = val; | |
311 | return res; | |
312 | } | |
313 | ||
314 | static int | |
315 | yylex (void) | |
316 | { | |
5b7f88c7 PE |
317 | static const char *input = "@<:@\1\2$@{@oline@__@&t@oline__\ |
318 | #output "; /* " | |
319 | */ | |
051ade83 | 320 | yylval = value_as_yystype (*input); |
5a08f1ce AD |
321 | return *input++; |
322 | } | |
323 | ||
324 | static void | |
325 | yyerror (const char *msg) | |
326 | { | |
327 | fprintf (stderr, "%s\n", msg); | |
328 | } | |
329 | ]]) | |
330 | ||
9501dc6e AD |
331 | # Pacify Emacs'font-lock-mode: " |
332 | ||
5a08f1ce | 333 | AT_DATA([main.c], |
051ade83 | 334 | [[typedef int value; |
5a08f1ce AD |
335 | #include "input.h" |
336 | ||
337 | int yyparse (void); | |
338 | ||
339 | int | |
340 | main (void) | |
341 | { | |
342 | return yyparse (); | |
343 | } | |
344 | ]]) | |
345 | ||
b56471a6 | 346 | AT_CHECK([bison -d -v -o input.c input.y]) |
efc6bf1b PE |
347 | AT_COMPILE([input.o], [-c input.c]) |
348 | AT_COMPILE([main.o], [-c main.c]) | |
349 | AT_COMPILE([input], [input.o main.o]) | |
1154cced | 350 | AT_PARSER_CHECK([./input], 0, |
206fe6a5 | 351 | [[[@<:@], |
5a08f1ce AD |
352 | ]]) |
353 | ||
354 | AT_CLEANUP | |
e59adf8f PE |
355 | |
356 | ||
357 | ## ---------------------- ## | |
358 | ## Typed symbol aliases. ## | |
359 | ## ---------------------- ## | |
360 | ||
361 | AT_SETUP([Typed symbol aliases]) | |
362 | ||
363 | # Bison 2.0 broke typed symbol aliases - ensure they work. | |
364 | ||
365 | AT_DATA_GRAMMAR([input.y], | |
366 | [[%union | |
367 | { | |
368 | int val; | |
369 | }; | |
370 | %token <val> MY_TOKEN "MY TOKEN" | |
371 | %type <val> exp | |
372 | %% | |
373 | exp: "MY TOKEN"; | |
374 | %% | |
375 | ]]) | |
376 | ||
377 | AT_CHECK([bison -o input.c input.y]) | |
378 | ||
379 | AT_CLEANUP | |
b50d2359 AD |
380 | |
381 | ||
382 | ## --------- ## | |
383 | ## Require. ## | |
384 | ## --------- ## | |
385 | ||
386 | m4_define([AT_CHECK_REQUIRE], | |
387 | [AT_SETUP([Require $1]) | |
388 | AT_DATA_GRAMMAR([input.y], | |
389 | [[%require "$1"; | |
390 | %% | |
391 | empty_file:; | |
392 | ]]) | |
393 | AT_CHECK([bison -o input.c input.y], $2, [], ignore) | |
394 | AT_CLEANUP | |
395 | ]) | |
396 | ||
397 | AT_CHECK_REQUIRE(1.0, 0) | |
398 | AT_CHECK_REQUIRE(AT_PACKAGE_VERSION, 0) | |
399 | ## FIXME: Some day augment this version number. | |
9b8a5ce0 | 400 | AT_CHECK_REQUIRE(100.0, 63) |