]>
Commit | Line | Data |
---|---|---|
1 | # Checking the Bison scanner. -*- Autotest -*- | |
2 | # Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. | |
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 | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
17 | # 02110-1301, USA. | |
18 | ||
19 | AT_BANNER([[Input Processing.]]) | |
20 | ||
21 | # Mostly test that we are robust to mistakes. | |
22 | ||
23 | ||
24 | ## ------------ ## | |
25 | ## Invalid $n. ## | |
26 | ## ------------ ## | |
27 | ||
28 | AT_SETUP([Invalid dollar-n]) | |
29 | ||
30 | AT_DATA([input.y], | |
31 | [[%% | |
32 | exp: { $$ = $1 ; }; | |
33 | ]]) | |
34 | ||
35 | AT_CHECK([bison input.y], [1], [], | |
36 | [[input.y:2.13-14: integer out of range: `$1' | |
37 | ]]) | |
38 | ||
39 | AT_CLEANUP | |
40 | ||
41 | ||
42 | ## ------------ ## | |
43 | ## Invalid @n. ## | |
44 | ## ------------ ## | |
45 | ||
46 | AT_SETUP([Invalid @n]) | |
47 | ||
48 | AT_DATA([input.y], | |
49 | [[%% | |
50 | exp: { @$ = @1 ; }; | |
51 | ]]) | |
52 | ||
53 | AT_CHECK([bison input.y], [1], [], | |
54 | [[input.y:2.13-14: integer out of range: `@1' | |
55 | ]]) | |
56 | ||
57 | AT_CLEANUP | |
58 | ||
59 | ||
60 | ## -------------- ## | |
61 | ## Type Clashes. ## | |
62 | ## -------------- ## | |
63 | ||
64 | AT_SETUP([Type Clashes]) | |
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 | ||
76 | AT_CHECK([bison input.y], [], [], | |
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 | |
80 | ]]) | |
81 | ||
82 | AT_CLEANUP | |
83 | ||
84 | ||
85 | ## --------------- ## | |
86 | ## Unused values. ## | |
87 | ## --------------- ## | |
88 | ||
89 | m4_define([AT_CHECK_UNUSED_VALUES], | |
90 | [AT_SETUP([Unused values]) | |
91 | ||
92 | AT_DATA([input.y], | |
93 | [[%token <integer> INT | |
94 | %type <integer> exp | |
95 | %% | |
96 | exp: | |
97 | $1 | |
98 | | INT | |
99 | ; | |
100 | ]]) | |
101 | ||
102 | AT_CHECK([bison input.y], [], [], | |
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: $$ | |
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 | |
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 }]) | |
162 | ||
163 | ||
164 | ||
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 | ||
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 | ||
212 | ||
213 | AT_DATA([input.y], []) | |
214 | AT_CHECK([bison input.y], [1], [], | |
215 | [[input.y:1.1: syntax error, unexpected end of file | |
216 | ]]) | |
217 | ||
218 | ||
219 | AT_DATA_GRAMMAR([input.y], | |
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 | ||
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 | ||
239 | /\ | |
240 | * A comment with backslash-newlines in it. %} *\ | |
241 | \ | |
242 | / | |
243 | /* { Close the above comment, if the C compiler mishandled it. */ | |
244 | ||
245 | char str[] = "\\ | |
246 | " A string with backslash-newlines in it %{ %} \\ | |
247 | \ | |
248 | ""; | |
249 | ||
250 | char apostrophe = '\''; | |
251 | #endif | |
252 | ||
253 | #include <stdio.h> | |
254 | %} | |
255 | /* %{ and %} can be here too. */ | |
256 | ||
257 | %{ | |
258 | /* Exercise pre-prologue dependency to %union. */ | |
259 | typedef int value; | |
260 | %} | |
261 | ||
262 | /* Exercise M4 quoting: '@:>@@:>@', 0. */ | |
263 | ||
264 | /* Also exercise %union. */ | |
265 | %union | |
266 | { | |
267 | value ival; /* A comment to exercise an old bug. */ | |
268 | }; | |
269 | ||
270 | ||
271 | /* Exercise post-prologue dependency to %union. */ | |
272 | %{ | |
273 | static YYSTYPE value_as_yystype (value val); | |
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 | ||
284 | %type <ival> '@<:@' | |
285 | ||
286 | /* Exercise quotes in strings. */ | |
287 | %token FAKE "fake @<:@@:>@ \a\b\f\n\r\t\v\"\'\?\\\u005B\U0000005c ??!??'??(??)??-??/??<??=??> \x1\1" | |
288 | ||
289 | %% | |
290 | /* Exercise M4 quoting: '@:>@@:>@', @<:@, 1. */ | |
291 | exp: '@<:@' '\1' two '$' '@' '{' oline output.or.oline.opt | |
292 | { | |
293 | /* Exercise quotes in braces. */ | |
294 | char tmp[] = "@<:@%c@:>@,\n"; | |
295 | printf (tmp, $1); | |
296 | } | |
297 | ; | |
298 | ||
299 | two: '\x000000000000000000000000000000000000000000000000000000000000000000002'; | |
300 | oline: '@' 'o' 'l' 'i' 'n' 'e' '@' '_' '_' 'o' 'l' 'i' 'n' 'e' '_' '_'; | |
301 | output.or.oline.opt: ;|oline;;|output;;; | |
302 | output: '#' 'o' 'u' 't' 'p' 'u' 't' ' '; | |
303 | %% | |
304 | /* Exercise M4 quoting: '@:>@@:>@', @<:@, 2. */ | |
305 | ||
306 | static YYSTYPE | |
307 | value_as_yystype (value val) | |
308 | { | |
309 | YYSTYPE res; | |
310 | res.ival = val; | |
311 | return res; | |
312 | } | |
313 | ||
314 | static int | |
315 | yylex (void) | |
316 | { | |
317 | static const char *input = "@<:@\1\2$@{@oline@__@&t@oline__\ | |
318 | #output "; /* " | |
319 | */ | |
320 | yylval = value_as_yystype (*input); | |
321 | return *input++; | |
322 | } | |
323 | ||
324 | static void | |
325 | yyerror (const char *msg) | |
326 | { | |
327 | fprintf (stderr, "%s\n", msg); | |
328 | } | |
329 | ]]) | |
330 | ||
331 | # Pacify Emacs'font-lock-mode: " | |
332 | ||
333 | AT_DATA([main.c], | |
334 | [[typedef int value; | |
335 | #include "input.h" | |
336 | ||
337 | int yyparse (void); | |
338 | ||
339 | int | |
340 | main (void) | |
341 | { | |
342 | return yyparse (); | |
343 | } | |
344 | ]]) | |
345 | ||
346 | AT_CHECK([bison -d -v -o input.c input.y]) | |
347 | AT_COMPILE([input.o], [-c input.c]) | |
348 | AT_COMPILE([main.o], [-c main.c]) | |
349 | AT_COMPILE([input], [input.o main.o]) | |
350 | AT_PARSER_CHECK([./input], 0, | |
351 | [[[@<:@], | |
352 | ]]) | |
353 | ||
354 | AT_CLEANUP | |
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 | |
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. | |
400 | AT_CHECK_REQUIRE(100.0, 63) |