]>
Commit | Line | Data |
---|---|---|
0049ec86 | 1 | # Java tests for simple calculator. -*- Autotest -*- |
8405b70c | 2 | |
34136e65 | 3 | # Copyright (C) 2007-2012 Free Software Foundation, Inc. |
8405b70c | 4 | |
f16b0819 | 5 | # This program is free software: you can redistribute it and/or modify |
8405b70c | 6 | # it under the terms of the GNU General Public License as published by |
f16b0819 PE |
7 | # the Free Software Foundation, either version 3 of the License, or |
8 | # (at your option) any later version. | |
9 | # | |
8405b70c PB |
10 | # This program is distributed in the hope that it will be useful, |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
f16b0819 | 14 | # |
8405b70c | 15 | # You should have received a copy of the GNU General Public License |
f16b0819 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
8405b70c PB |
17 | |
18 | AT_BANNER([[Java Calculator.]]) | |
19 | ||
20 | ||
21 | # ------------------------- # | |
22 | # Helping Autotest macros. # | |
23 | # ------------------------- # | |
24 | ||
25 | ||
01b477c6 | 26 | # _AT_DATA_JAVA_CALC_Y($1, $2, $3, [BISON-DIRECTIVES]) |
417e31d2 | 27 | # ---------------------------------------------------- |
8405b70c PB |
28 | # Produce `calc.y'. Don't call this macro directly, because it contains |
29 | # some occurrences of `$1' etc. which will be interpreted by m4. So | |
30 | # you should call it with $1, $2, and $3 as arguments, which is what | |
31 | # AT_DATA_JAVA_CALC_Y does. | |
32 | m4_define([_AT_DATA_JAVA_CALC_Y], | |
33 | [m4_if([$1$2$3], $[1]$[2]$[3], [], | |
34 | [m4_fatal([$0: Invalid arguments: $@])])dnl | |
35 | AT_DATA([Calc.y], | |
36 | [[/* Infix notation calculator--calc */ | |
37 | %language "Java" | |
01b477c6 | 38 | %name-prefix "Calc" |
8405b70c PB |
39 | %define parser_class_name "Calc" |
40 | %define public | |
41 | ||
01b477c6 PB |
42 | ]$4[ |
43 | ||
8405b70c PB |
44 | %code imports { |
45 | import java.io.StreamTokenizer; | |
46 | import java.io.InputStream; | |
47 | import java.io.InputStreamReader; | |
48 | import java.io.Reader; | |
49 | import java.io.IOException; | |
50 | } | |
51 | ||
52 | /* Bison Declarations */ | |
53 | %token <Integer> NUM "number" | |
54 | %type <Integer> exp | |
55 | ||
56 | %nonassoc '=' /* comparison */ | |
57 | %left '-' '+' | |
58 | %left '*' '/' | |
59 | %left NEG /* negation--unary minus */ | |
60 | %right '^' /* exponentiation */ | |
61 | ||
62 | /* Grammar follows */ | |
63 | %% | |
64 | input: | |
65 | line | |
66 | | input line | |
67 | ; | |
68 | ||
69 | line: | |
70 | '\n' | |
71 | | exp '\n' | |
72 | | error '\n' | |
73 | ; | |
74 | ||
75 | exp: | |
76 | NUM { $$ = $1; } | |
77 | | exp '=' exp | |
78 | { | |
79 | if ($1.intValue () != $3.intValue ()) | |
6771a463 | 80 | yyerror (]AT_LOCATION_IF([[@$,]])[ "calc: error: " + $1 + " != " + $3); |
8405b70c PB |
81 | } |
82 | | exp '+' exp { $$ = new Integer ($1.intValue () + $3.intValue ()); } | |
83 | | exp '-' exp { $$ = new Integer ($1.intValue () - $3.intValue ()); } | |
84 | | exp '*' exp { $$ = new Integer ($1.intValue () * $3.intValue ()); } | |
85 | | exp '/' exp { $$ = new Integer ($1.intValue () / $3.intValue ()); } | |
86 | | '-' exp %prec NEG { $$ = new Integer (-$2.intValue ()); } | |
87 | | exp '^' exp { $$ = new Integer ((int) | |
88 | Math.pow ($1.intValue (), | |
89 | $3.intValue ())); } | |
90 | | '(' exp ')' { $$ = $2; } | |
91 | | '(' error ')' { $$ = new Integer (1111); } | |
92 | | '!' { $$ = new Integer (0); return YYERROR; } | |
93 | | '-' error { $$ = new Integer (0); return YYERROR; } | |
94 | ; | |
01b477c6 PB |
95 | |
96 | ]AT_LEXPARAM_IF([[ | |
97 | %code lexer { | |
98 | ]], | |
99 | [[ | |
8405b70c | 100 | %% |
01b477c6 PB |
101 | class CalcLexer implements Calc.Lexer { |
102 | ]])[ | |
103 | StreamTokenizer st; | |
104 | ||
105 | public ]AT_LEXPARAM_IF([[YYLexer]], [[CalcLexer]]) (InputStream is) | |
106 | { | |
107 | st = new StreamTokenizer (new InputStreamReader (is)); | |
108 | st.resetSyntax (); | |
109 | st.eolIsSignificant (true); | |
110 | st.whitespaceChars (9, 9); | |
111 | st.whitespaceChars (32, 32); | |
112 | st.wordChars (48, 57); | |
113 | } | |
114 | ||
115 | AT_LOCATION_IF([[ | |
6771a463 | 116 | Position yypos = new Position (1, 0); |
8405b70c | 117 | |
01b477c6 | 118 | public Position getStartPos() { |
6771a463 | 119 | return yypos; |
8405b70c PB |
120 | } |
121 | ||
01b477c6 | 122 | public Position getEndPos() { |
6771a463 | 123 | return yypos; |
01b477c6 PB |
124 | } |
125 | ||
126 | public void yyerror (Calc.Location l, String s) | |
127 | { | |
128 | if (l == null) | |
129 | System.err.println (s); | |
130 | else | |
6771a463 | 131 | System.err.println (l + ": " + s); |
01b477c6 PB |
132 | } |
133 | ]], [[ | |
134 | public void yyerror (String s) | |
135 | { | |
11707b2b | 136 | System.err.println (s); |
01b477c6 PB |
137 | } |
138 | ]])[ | |
139 | ||
140 | Integer yylval; | |
141 | ||
142 | public Object getLVal() { | |
143 | return yylval; | |
144 | } | |
145 | ||
146 | public int yylex () throws IOException { | |
147 | int ttype = st.nextToken (); | |
6771a463 JD |
148 | ]AT_LOCATION_IF([[yypos = new Position (yypos.lineno (), |
149 | yypos.token () + 1);]])[ | |
01b477c6 | 150 | if (ttype == st.TT_EOF) |
1979121c | 151 | return EOF; |
01b477c6 PB |
152 | |
153 | else if (ttype == st.TT_EOL) | |
154 | { | |
6771a463 | 155 | ]AT_LOCATION_IF([[yypos = new Position (yypos.lineno () + 1, 0);]])[ |
01b477c6 PB |
156 | return (int) '\n'; |
157 | } | |
158 | ||
159 | else if (ttype == st.TT_WORD) | |
160 | { | |
161 | yylval = new Integer (st.sval); | |
1979121c | 162 | return NUM; |
01b477c6 PB |
163 | } |
164 | ||
165 | else | |
166 | return st.ttype; | |
167 | } | |
168 | ||
169 | ||
170 | ]AT_LEXPARAM_IF([[ | |
171 | }; | |
172 | %%]], [[ | |
173 | }]]) | |
174 | ||
175 | [ | |
176 | class Position { | |
177 | public int line; | |
6771a463 | 178 | public int token; |
01b477c6 PB |
179 | |
180 | public Position () | |
181 | { | |
182 | line = 0; | |
6771a463 | 183 | token = 0; |
01b477c6 PB |
184 | } |
185 | ||
6771a463 | 186 | public Position (int l, int t) |
01b477c6 PB |
187 | { |
188 | line = l; | |
6771a463 | 189 | token = t; |
01b477c6 PB |
190 | } |
191 | ||
192 | public boolean equals (Position l) | |
193 | { | |
6771a463 | 194 | return l.line == line && l.token == token; |
01b477c6 PB |
195 | } |
196 | ||
197 | public String toString () | |
198 | { | |
6771a463 | 199 | return Integer.toString (line) + "." + Integer.toString(token); |
01b477c6 PB |
200 | } |
201 | ||
202 | public int lineno () | |
203 | { | |
204 | return line; | |
205 | } | |
6771a463 JD |
206 | |
207 | public int token () | |
208 | { | |
209 | return token; | |
210 | } | |
01b477c6 PB |
211 | } |
212 | ||
213 | ]]) | |
8405b70c PB |
214 | ])# _AT_DATA_JAVA_CALC_Y |
215 | ||
216 | ||
01b477c6 | 217 | # AT_DATA_CALC_Y([BISON-OPTIONS]) |
417e31d2 | 218 | # ------------------------------- |
8405b70c PB |
219 | # Produce `calc.y'. |
220 | m4_define([AT_DATA_JAVA_CALC_Y], | |
01b477c6 | 221 | [_AT_DATA_JAVA_CALC_Y($[1], $[2], $[3], [$1]) |
8405b70c PB |
222 | ]) |
223 | ||
224 | ||
8405b70c PB |
225 | # _AT_CHECK_JAVA_CALC_ERROR(BISON-OPTIONS, INPUT, |
226 | # [VERBOSE-AND-LOCATED-ERROR-MESSAGE]) | |
227 | # --------------------------------------------------------- | |
228 | # Run `calc' on INPUT, and expect a `syntax error' message. | |
229 | # | |
230 | # If INPUT starts with a slash, it is used as absolute input file name, | |
231 | # otherwise as contents. | |
232 | # | |
233 | # The VERBOSE-AND-LOCATED-ERROR-MESSAGE is stripped of locations | |
234 | # and expected tokens if necessary, and compared with the output. | |
235 | m4_define([_AT_CHECK_JAVA_CALC_ERROR], | |
236 | [m4_bmatch([$2], [^/], | |
237 | [AT_JAVA_PARSER_CHECK([Calc < $2], 0, [], [stderr])], | |
238 | [AT_DATA([[input]], | |
239 | [[$2 | |
240 | ]]) | |
241 | AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])]) | |
242 | ||
243 | # Normalize the observed and expected error messages, depending upon the | |
244 | # options. | |
245 | # 1. Create the reference error message. | |
246 | AT_DATA([[expout]], | |
247 | [$3 | |
248 | ]) | |
249 | # 2. If locations are not used, remove them. | |
250 | AT_YYERROR_SEES_LOC_IF([], | |
251 | [[sed 's/^[-0-9.]*: //' expout >at-expout | |
252 | mv at-expout expout]]) | |
253 | # 3. If error-verbose is not used, strip the`, unexpected....' part. | |
254 | m4_bmatch([$1], [%error-verbose], [], | |
255 | [[sed 's/syntax error, .*$/syntax error/' expout >at-expout | |
256 | mv at-expout expout]]) | |
257 | # 4. Check | |
258 | AT_CHECK([cat stderr], 0, [expout]) | |
259 | ]) | |
260 | ||
01b477c6 | 261 | # _AT_CHECK_JAVA_CALC([BISON-DIRECTIVES], [BISON-CODE]) |
417e31d2 | 262 | # ----------------------------------------------------- |
8405b70c PB |
263 | # Start a testing chunk which compiles `calc' grammar with |
264 | # BISON-DIRECTIVES, and performs several tests over the parser. | |
265 | m4_define([_AT_CHECK_JAVA_CALC], | |
266 | [# We use integers to avoid dependencies upon the precision of doubles. | |
267 | AT_SETUP([Calculator $1]) | |
268 | ||
269 | AT_BISON_OPTION_PUSHDEFS([$1]) | |
270 | ||
271 | AT_DATA_JAVA_CALC_Y([$1 | |
272 | %code { | |
273 | $2 | |
01b477c6 | 274 | }]) |
8405b70c | 275 | |
da730230 | 276 | AT_BISON_CHECK([-o Calc.java Calc.y]) |
8405b70c PB |
277 | AT_JAVA_COMPILE([Calc.java]) |
278 | ||
279 | # Test the priorities. | |
280 | AT_DATA([[input]], | |
281 | [[1 + 2 * 3 = 7 | |
282 | 1 + 2 * -3 = -5 | |
283 | ||
284 | -1^2 = -1 | |
285 | (-1)^2 = 1 | |
286 | ||
287 | ---1 = -1 | |
288 | ||
289 | 1 - 2 - 3 = -4 | |
290 | 1 - (2 - 3) = 2 | |
291 | ||
292 | 2^2^3 = 256 | |
293 | (2^2)^3 = 64 | |
294 | ]]) | |
295 | AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr]) | |
296 | ||
297 | ||
298 | # Some syntax errors. | |
299 | _AT_CHECK_JAVA_CALC_ERROR([$1], [0 0], | |
6771a463 | 300 | [1.2: syntax error, unexpected number]) |
8405b70c | 301 | _AT_CHECK_JAVA_CALC_ERROR([$1], [1//2], |
6771a463 | 302 | [1.3: syntax error, unexpected '/', expecting number or '-' or '(' or '!']) |
8405b70c | 303 | _AT_CHECK_JAVA_CALC_ERROR([$1], [error], |
6771a463 | 304 | [1.1: syntax error, unexpected $undefined]) |
8405b70c | 305 | _AT_CHECK_JAVA_CALC_ERROR([$1], [1 = 2 = 3], |
6771a463 | 306 | [1.4: syntax error, unexpected '=']) |
8405b70c PB |
307 | _AT_CHECK_JAVA_CALC_ERROR([$1], [ |
308 | +1], | |
6771a463 | 309 | [2.1: syntax error, unexpected '+']) |
8405b70c PB |
310 | # Exercise error messages with EOF: work on an empty file. |
311 | _AT_CHECK_JAVA_CALC_ERROR([$1], [/dev/null], | |
6771a463 | 312 | [1.1: syntax error, unexpected end of input]) |
8405b70c PB |
313 | |
314 | # Exercise the error token: without it, we die at the first error, | |
315 | # hence be sure to | |
316 | # | |
317 | # - have several errors which exercise different shift/discardings | |
318 | # - (): nothing to pop, nothing to discard | |
319 | # - (1 + 1 + 1 +): a lot to pop, nothing to discard | |
320 | # - (* * *): nothing to pop, a lot to discard | |
321 | # - (1 + 2 * *): some to pop and discard | |
322 | # | |
323 | # - test the action associated to `error' | |
324 | # | |
325 | # - check the lookahead that triggers an error is not discarded | |
326 | # when we enter error recovery. Below, the lookahead causing the | |
327 | # first error is ")", which is needed to recover from the error and | |
328 | # produce the "0" that triggers the "0 != 1" error. | |
329 | # | |
11707b2b | 330 | _AT_CHECK_JAVA_CALC_ERROR([$1], |
8405b70c | 331 | [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1], |
6771a463 JD |
332 | [1.2: syntax error, unexpected ')', expecting number or '-' or '(' or '!' |
333 | 1.11: syntax error, unexpected ')', expecting number or '-' or '(' or '!' | |
334 | 1.14: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
335 | 1.24: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
336 | 1.1-1.27: calc: error: 4444 != 1]) | |
8405b70c PB |
337 | |
338 | # The same, but this time exercising explicitly triggered syntax errors. | |
339 | # POSIX says the lookahead causing the error should not be discarded. | |
340 | _AT_CHECK_JAVA_CALC_ERROR([$1], [(!) + (0 0) = 1], | |
6771a463 JD |
341 | [1.7: syntax error, unexpected number |
342 | 1.1-1.10: calc: error: 2222 != 1]) | |
8405b70c | 343 | _AT_CHECK_JAVA_CALC_ERROR([$1], [(- *) + (0 0) = 1], |
6771a463 JD |
344 | [1.3: syntax error, unexpected '*', expecting number or '-' or '(' or '!' |
345 | 1.8: syntax error, unexpected number | |
346 | 1.1-1.11: calc: error: 2222 != 1]) | |
8405b70c PB |
347 | AT_BISON_OPTION_POPDEFS |
348 | ||
349 | AT_CLEANUP | |
350 | ])# _AT_CHECK_JAVA_CALC | |
351 | ||
352 | ||
01b477c6 | 353 | # AT_CHECK_JAVA_CALC([BISON-DIRECTIVES]) |
417e31d2 | 354 | # -------------------------------------- |
8405b70c PB |
355 | # Start a testing chunk which compiles `calc' grammar with |
356 | # BISON-DIRECTIVES, and performs several tests over the parser. | |
357 | # Run the test with and without %error-verbose. | |
358 | m4_define([AT_CHECK_JAVA_CALC], | |
01b477c6 PB |
359 | [_AT_CHECK_JAVA_CALC([$1], [$2]) |
360 | _AT_CHECK_JAVA_CALC([%error-verbose $1], [$2]) | |
361 | _AT_CHECK_JAVA_CALC([%locations $1], [$2]) | |
362 | _AT_CHECK_JAVA_CALC([%error-verbose %locations $1], [$2]) | |
8405b70c PB |
363 | ])# AT_CHECK_JAVA_CALC |
364 | ||
365 | ||
366 | # ------------------------ # | |
367 | # Simple LALR Calculator. # | |
368 | # ------------------------ # | |
369 | ||
01b477c6 | 370 | AT_CHECK_JAVA_CALC([], [[ |
8405b70c PB |
371 | public static void main (String args[]) throws IOException |
372 | { | |
373 | CalcLexer l = new CalcLexer (System.in); | |
374 | Calc p = new Calc (l); | |
375 | p.parse (); | |
376 | } | |
01b477c6 | 377 | ]]) |
8405b70c | 378 | |
01b477c6 PB |
379 | AT_CHECK_JAVA_CALC([%lex-param { InputStream is } ], [[ |
380 | public static void main (String args[]) throws IOException | |
8405b70c | 381 | { |
01b477c6 | 382 | new Calc (System.in).parse (); |
8405b70c | 383 | } |
8405b70c | 384 | ]]) |
e254a580 DJ |
385 | |
386 | ||
387 | ||
388 | # -----------------# | |
389 | # Java Directives. # | |
390 | # -----------------# | |
391 | ||
392 | AT_BANNER([Java Parameters.]) | |
393 | ||
394 | ||
395 | # AT_CHECK_JAVA_MINIMAL([DIRECTIVES], [PARSER_ACTION], [POSITION_CLASS]) | |
396 | # ---------------------------------------------------------------------- | |
397 | # Check that a mininal parser with DIRECTIVES compiles in Java. | |
398 | # Put the Java code in YYParser.java. | |
399 | m4_define([AT_CHECK_JAVA_MINIMAL], | |
400 | [ | |
401 | AT_DATA([[YYParser.y]], [ | |
402 | %language "Java" | |
403 | %locations | |
404 | %debug | |
405 | %error-verbose | |
406 | %token-table | |
417e31d2 | 407 | %token END "end" |
e254a580 DJ |
408 | $1 |
409 | %% | |
417e31d2 | 410 | start: END {$2}; |
e254a580 DJ |
411 | %% |
412 | class m4_default([$3], [Position]) {} | |
413 | ]) | |
414 | AT_BISON_CHECK([[YYParser.y]]) | |
74553c98 | 415 | AT_CHECK([[grep '[mb]4_' YYParser.y]], [1], [ignore]) |
e254a580 DJ |
416 | AT_JAVA_COMPILE([[YYParser.java]]) |
417 | ]) | |
418 | ||
419 | ||
420 | # AT_CHECK_JAVA_MINIMAL_W_LEXER([1:DIRECTIVES], [2:LEX_THROWS], | |
e9690142 JD |
421 | # [3:YYLEX_ACTION], [4:LEXER_BODY], [5:PARSER_ACTION], [6:STYPE], |
422 | # [7:POSITION_TYPE], [8:LOCATION_TYPE]) | |
e254a580 DJ |
423 | # --------------------------------------------------------------------- |
424 | # Check that a mininal parser with DIRECTIVES and a "%code lexer". | |
425 | # YYLEX is the body of yylex () which throws LEX_THROW. | |
426 | # compiles in Java. | |
427 | m4_define([AT_CHECK_JAVA_MINIMAL_W_LEXER], | |
428 | [AT_CHECK_JAVA_MINIMAL([$1 | |
429 | ||
430 | %code lexer | |
431 | { | |
432 | m4_default([$6], [Object]) yylval; | |
433 | public m4_default([$6], [Object]) getLVal() { return yylval; } | |
434 | ||
435 | public m4_default([$7], [Position]) getStartPos() { return null; } | |
436 | public m4_default([$7], [Position]) getEndPos() { return null; } | |
437 | ||
438 | public void yyerror (m4_default([$8], [Location]) loc, String s) | |
439 | { | |
440 | System.err.println (loc + ": " + s); | |
441 | } | |
442 | ||
443 | public int yylex ()$2 | |
444 | { | |
445 | $3 | |
446 | } | |
11707b2b | 447 | |
e254a580 DJ |
448 | $4 |
449 | }], [$5], [$7])]) | |
450 | ||
451 | ||
452 | # AT_CHECK_JAVA_GREP([LINE], [COUNT=1]) | |
453 | # ------------------------------------- | |
454 | # Check that YYParser.java contains exactly COUNT lines matching ^LINE$ | |
455 | # with grep. | |
456 | m4_define([AT_CHECK_JAVA_GREP], | |
e9690142 | 457 | [AT_CHECK([grep -c '^$1$' YYParser.java], [], [m4_default([$2], [1]) |
e254a580 DJ |
458 | ]) |
459 | ]) | |
460 | ||
461 | ||
417e31d2 AD |
462 | # ------------------------------------- # |
463 | # Java parser class and package names. # | |
464 | # ------------------------------------- # | |
e254a580 DJ |
465 | |
466 | AT_SETUP([Java parser class and package names]) | |
467 | ||
468 | AT_CHECK_JAVA_MINIMAL([]) | |
469 | AT_CHECK_JAVA_GREP([[class YYParser]]) | |
470 | ||
471 | AT_CHECK_JAVA_MINIMAL([[%name-prefix "Prefix"]]) | |
472 | AT_CHECK_JAVA_GREP([[class PrefixParser]]) | |
473 | ||
4c6622c2 | 474 | AT_CHECK_JAVA_MINIMAL([[%define api.tokens.prefix "TOK_"]]) |
417e31d2 AD |
475 | AT_CHECK_JAVA_GREP([[.*TOK_END.*]]) |
476 | ||
e254a580 DJ |
477 | AT_CHECK_JAVA_MINIMAL([[%define parser_class_name "ParserClassName"]]) |
478 | AT_CHECK_JAVA_GREP([[class ParserClassName]]) | |
479 | ||
480 | AT_CHECK_JAVA_MINIMAL([[%define package "user_java_package"]]) | |
481 | AT_CHECK_JAVA_GREP([[package user_java_package;]]) | |
482 | ||
483 | AT_CLEANUP | |
484 | ||
485 | ||
417e31d2 AD |
486 | # ----------------------------- # |
487 | # Java parser class modifiers. # | |
488 | # ----------------------------- # | |
e254a580 DJ |
489 | |
490 | AT_SETUP([Java parser class modifiers]) | |
491 | ||
492 | AT_CHECK_JAVA_MINIMAL([[%define abstract]]) | |
493 | AT_CHECK_JAVA_GREP([[abstract class YYParser]]) | |
494 | ||
495 | AT_CHECK_JAVA_MINIMAL([[%define final]]) | |
496 | AT_CHECK_JAVA_GREP([[final class YYParser]]) | |
497 | ||
498 | AT_CHECK_JAVA_MINIMAL([[%define strictfp]]) | |
499 | AT_CHECK_JAVA_GREP([[strictfp class YYParser]]) | |
500 | ||
501 | AT_CHECK_JAVA_MINIMAL([[ | |
502 | %define abstract | |
503 | %define strictfp]]) | |
504 | AT_CHECK_JAVA_GREP([[abstract strictfp class YYParser]]) | |
505 | ||
506 | AT_CHECK_JAVA_MINIMAL([[ | |
507 | %define final | |
508 | %define strictfp]]) | |
509 | AT_CHECK_JAVA_GREP([[final strictfp class YYParser]]) | |
510 | ||
511 | AT_CHECK_JAVA_MINIMAL([[%define public]]) | |
512 | AT_CHECK_JAVA_GREP([[public class YYParser]]) | |
513 | ||
514 | AT_CHECK_JAVA_MINIMAL([[ | |
515 | %define public | |
516 | %define abstract]]) | |
517 | AT_CHECK_JAVA_GREP([[public abstract class YYParser]]) | |
518 | ||
519 | AT_CHECK_JAVA_MINIMAL([[ | |
520 | %define public | |
521 | %define final]]) | |
522 | AT_CHECK_JAVA_GREP([[public final class YYParser]]) | |
523 | ||
524 | AT_CHECK_JAVA_MINIMAL([[ | |
525 | %define public | |
526 | %define strictfp]]) | |
527 | AT_CHECK_JAVA_GREP([[public strictfp class YYParser]]) | |
528 | ||
529 | AT_CHECK_JAVA_MINIMAL([[ | |
530 | %define public | |
531 | %define abstract | |
532 | %define strictfp]]) | |
533 | AT_CHECK_JAVA_GREP([[public abstract strictfp class YYParser]]) | |
534 | ||
535 | AT_CHECK_JAVA_MINIMAL([[ | |
536 | %define public | |
537 | %define final | |
538 | %define strictfp]]) | |
539 | AT_CHECK_JAVA_GREP([[public final strictfp class YYParser]]) | |
540 | ||
1979121c DJ |
541 | # FIXME: Can't do a Java compile because javacomp.sh is configured for 1.3 |
542 | AT_CHECK_JAVA_MINIMAL([[ | |
543 | %define annotations "/*@Deprecated @SupressWarnings(\"unchecked\") @SupressWarnings({\"unchecked\", \"deprecation\"}) @SupressWarnings(value={\"unchecked\", \"deprecation\"})*/" | |
544 | %define public]]) | |
545 | AT_CHECK_JAVA_GREP([[/\*@Deprecated @SupressWarnings("unchecked") @SupressWarnings({"unchecked", "deprecation"}) @SupressWarnings(value={"unchecked", "deprecation"})\*/ public class YYParser]]) | |
546 | ||
e254a580 DJ |
547 | AT_CLEANUP |
548 | ||
549 | ||
550 | # ---------------------------------------- # | |
551 | # Java parser class extends and implements # | |
552 | # ---------------------------------------- # | |
553 | ||
554 | AT_SETUP([Java parser class extends and implements]) | |
555 | ||
556 | AT_CHECK_JAVA_MINIMAL([[%define extends "Thread"]]) | |
557 | AT_CHECK_JAVA_GREP([[class YYParser extends Thread]]) | |
558 | ||
559 | AT_CHECK_JAVA_MINIMAL([[%define implements "Cloneable"]]) | |
560 | AT_CHECK_JAVA_GREP([[class YYParser implements Cloneable]]) | |
561 | ||
562 | AT_CHECK_JAVA_MINIMAL([[ | |
563 | %define extends "Thread" | |
564 | %define implements "Cloneable"]]) | |
565 | AT_CHECK_JAVA_GREP([[class YYParser extends Thread implements Cloneable]]) | |
566 | ||
567 | AT_CLEANUP | |
568 | ||
569 | ||
570 | # -------------------------------- # | |
571 | # Java %parse-param and %lex-param # | |
572 | # -------------------------------- # | |
573 | ||
574 | AT_SETUP([Java %parse-param and %lex-param]) | |
575 | ||
576 | AT_CHECK_JAVA_MINIMAL([]) | |
1979121c | 577 | AT_CHECK_JAVA_GREP([[ *public YYParser (Lexer yylexer) *]]) |
e254a580 DJ |
578 | |
579 | AT_CHECK_JAVA_MINIMAL([[%parse-param {int parse_param1}]]) | |
580 | AT_CHECK_JAVA_GREP([[ *protected final int parse_param1;]]) | |
1979121c | 581 | AT_CHECK_JAVA_GREP([[ *public YYParser (Lexer yylexer, *int parse_param1) *]]) |
11707b2b | 582 | AT_CHECK_JAVA_GREP([[ *this.parse_param1 = parse_param1;]]) |
e254a580 DJ |
583 | |
584 | AT_CHECK_JAVA_MINIMAL([[ | |
585 | %parse-param {int parse_param1} | |
586 | %parse-param {long parse_param2}]]) | |
587 | AT_CHECK_JAVA_GREP([[ *protected final int parse_param1;]]) | |
588 | AT_CHECK_JAVA_GREP([[ *protected final long parse_param2;]]) | |
1979121c | 589 | AT_CHECK_JAVA_GREP([[ *public YYParser (Lexer yylexer, *int parse_param1, *long parse_param2) *]]) |
11707b2b AD |
590 | AT_CHECK_JAVA_GREP([[ *this.parse_param1 = parse_param1;]]) |
591 | AT_CHECK_JAVA_GREP([[ *this.parse_param2 = parse_param2;]]) | |
e254a580 DJ |
592 | |
593 | AT_CHECK_JAVA_MINIMAL_W_LEXER([], [], [[return EOF;]]) | |
1979121c DJ |
594 | AT_CHECK_JAVA_GREP([[ *public YYParser () *]]) |
595 | AT_CHECK_JAVA_GREP([[ *protected YYParser (Lexer yylexer) *]]) | |
e254a580 DJ |
596 | |
597 | AT_CHECK_JAVA_MINIMAL_W_LEXER([[%parse-param {int parse_param1}]], | |
1979121c | 598 | [], [[return EOF;]]) |
e254a580 | 599 | AT_CHECK_JAVA_GREP([[ *protected final int parse_param1;]]) |
1979121c DJ |
600 | AT_CHECK_JAVA_GREP([[ *public YYParser (int parse_param1) *]]) |
601 | AT_CHECK_JAVA_GREP([[ *protected YYParser (Lexer yylexer, *int parse_param1) *]]) | |
11707b2b | 602 | AT_CHECK_JAVA_GREP([[ *this.parse_param1 = parse_param1;]], [2]) |
e254a580 DJ |
603 | |
604 | AT_CHECK_JAVA_MINIMAL_W_LEXER([[ | |
605 | %parse-param {int parse_param1} | |
606 | %parse-param {long parse_param2}]], | |
1979121c | 607 | [], [[return EOF;]]) |
e254a580 DJ |
608 | AT_CHECK_JAVA_GREP([[ *protected final int parse_param1;]]) |
609 | AT_CHECK_JAVA_GREP([[ *protected final long parse_param2;]]) | |
1979121c DJ |
610 | AT_CHECK_JAVA_GREP([[ *public YYParser (int parse_param1, *long parse_param2) *]]) |
611 | AT_CHECK_JAVA_GREP([[ *protected YYParser (Lexer yylexer, *int parse_param1, *long parse_param2) *]]) | |
11707b2b AD |
612 | AT_CHECK_JAVA_GREP([[ *this.parse_param1 = parse_param1;]], [2]) |
613 | AT_CHECK_JAVA_GREP([[ *this.parse_param2 = parse_param2;]], [2]) | |
e254a580 DJ |
614 | |
615 | AT_CHECK_JAVA_MINIMAL_W_LEXER([[%lex-param {char lex_param1}]], | |
1979121c DJ |
616 | [], [[return EOF;]], [[YYLexer (char lex_param1) {}]]) |
617 | AT_CHECK_JAVA_GREP([[ *public YYParser (char lex_param1) *]]) | |
e254a580 DJ |
618 | AT_CHECK_JAVA_GREP([[.* = new YYLexer *(lex_param1);]]) |
619 | ||
620 | AT_CHECK_JAVA_MINIMAL_W_LEXER([[ | |
621 | %lex-param {char lex_param1} | |
622 | %lex-param {short lex_param2}]], | |
1979121c DJ |
623 | [], [[return EOF;]], [[YYLexer (char lex_param1, short lex_param2) {}]]) |
624 | AT_CHECK_JAVA_GREP([[ *public YYParser (char lex_param1, *short lex_param2) *]]) | |
e254a580 DJ |
625 | AT_CHECK_JAVA_GREP([[.* = new YYLexer *(lex_param1, *lex_param2);]]) |
626 | ||
627 | AT_CHECK_JAVA_MINIMAL_W_LEXER([[ | |
628 | %parse-param {int parse_param1} | |
629 | %parse-param {long parse_param2} | |
630 | %lex-param {char lex_param1} | |
631 | %lex-param {short lex_param2}]], | |
1979121c | 632 | [], [[return EOF;]], [[YYLexer (char lex_param1, short lex_param2) {}]]) |
e254a580 DJ |
633 | AT_CHECK_JAVA_GREP([[ *protected final int parse_param1;]]) |
634 | AT_CHECK_JAVA_GREP([[ *protected final long parse_param2;]]) | |
1979121c | 635 | AT_CHECK_JAVA_GREP([[ *public YYParser (char lex_param1, *short lex_param2, *int parse_param1, *long parse_param2) *]]) |
e254a580 | 636 | AT_CHECK_JAVA_GREP([[.* = new YYLexer *(lex_param1, *lex_param2);]]) |
1979121c | 637 | AT_CHECK_JAVA_GREP([[ *protected YYParser (Lexer yylexer, *int parse_param1, *long parse_param2) *]]) |
11707b2b AD |
638 | AT_CHECK_JAVA_GREP([[ *this.parse_param1 = parse_param1;]], [2]) |
639 | AT_CHECK_JAVA_GREP([[ *this.parse_param2 = parse_param2;]], [2]) | |
e254a580 DJ |
640 | |
641 | AT_CLEANUP | |
642 | ||
643 | ||
644 | # ------------------------- # | |
645 | # Java throw specifications # | |
646 | # ------------------------- # | |
647 | ||
648 | AT_SETUP([Java throws specifications]) | |
649 | ||
e9690142 JD |
650 | # %define throws - 0 1 2 |
651 | # %define lex-throws - 0 1 2 | |
652 | # %code lexer 0 1 | |
e254a580 DJ |
653 | |
654 | m4_define([AT_JT_lex_throws_define], [m4_case(AT_JT_lex_throws, | |
e9690142 JD |
655 | -1, [], |
656 | 0, [[%define lex_throws ""]], | |
657 | 1, [[%define lex_throws "InterruptedException"]], | |
658 | 2, [[%define lex_throws "InterruptedException, IllegalAccessException"]])]) | |
e254a580 DJ |
659 | |
660 | m4_define([AT_JT_yylex_throws], [m4_case(AT_JT_lex_throws, | |
e9690142 JD |
661 | -1, [[ throws java.io.IOException]], |
662 | 0, [], | |
663 | 1, [[ throws InterruptedException]], | |
664 | 2, [[ throws InterruptedException, IllegalAccessException]])]) | |
e254a580 DJ |
665 | |
666 | m4_define([AT_JT_yylex_action], [m4_case(AT_JT_lex_throws, | |
e9690142 JD |
667 | -1, [[throw new java.io.IOException();]], |
668 | 0, [[return EOF;]], | |
669 | 1, [[throw new InterruptedException();]], | |
670 | 2, [[throw new IllegalAccessException();]])]) | |
e254a580 DJ |
671 | |
672 | ||
673 | m4_define([AT_JT_throws_define], [m4_case(AT_JT_throws, | |
e9690142 JD |
674 | -1, [], |
675 | 0, [[%define throws ""]], | |
676 | 1, [[%define throws "ClassNotFoundException"]], | |
677 | 2, [[%define throws "ClassNotFoundException, InstantiationException"]])]) | |
e254a580 DJ |
678 | |
679 | m4_define([AT_JT_yyaction_throws], [m4_case(AT_JT_throws, | |
e9690142 JD |
680 | -1, [], |
681 | 0, [], | |
682 | 1, [[ throws ClassNotFoundException]], | |
683 | 2, [[ throws ClassNotFoundException, InstantiationException]])]) | |
e254a580 DJ |
684 | |
685 | m4_define([AT_JT_parse_throws_2], [m4_case(AT_JT_throws, | |
e9690142 JD |
686 | -1, [], |
687 | 0, [], | |
688 | 1, [[, ClassNotFoundException]], | |
689 | 2, [[, ClassNotFoundException, InstantiationException]])]) | |
e254a580 DJ |
690 | |
691 | m4_define([AT_JT_parse_throws], | |
e9690142 JD |
692 | [m4_if(m4_quote(AT_JT_yylex_throws), [], |
693 | [AT_JT_yyaction_throws], | |
694 | [AT_JT_yylex_throws[]AT_JT_parse_throws_2])]) | |
e254a580 DJ |
695 | |
696 | m4_define([AT_JT_initial_action], [m4_case(AT_JT_throws, | |
e9690142 JD |
697 | -1, [], |
698 | 0, [], | |
699 | 1, [[%initial-action {if (true) throw new ClassNotFoundException();}]], | |
700 | 2, [[%initial-action {if (true) throw new InstantiationException();}]])]) | |
e254a580 DJ |
701 | |
702 | m4_define([AT_JT_parse_action], [m4_case(AT_JT_throws, | |
e9690142 JD |
703 | -1, [], |
704 | 0, [], | |
705 | 1, [[throw new ClassNotFoundException();]], | |
706 | 2, [[throw new ClassNotFoundException();]])]) | |
e254a580 DJ |
707 | |
708 | m4_for([AT_JT_lexer], 0, 1, 1, | |
709 | [m4_for([AT_JT_lex_throws], -1, 2, 1, | |
710 | [m4_for([AT_JT_throws], -1, 2, 1, | |
711 | [m4_if(AT_JT_lexer, 0, | |
e9690142 | 712 | [AT_CHECK_JAVA_MINIMAL([ |
e254a580 DJ |
713 | AT_JT_throws_define |
714 | AT_JT_lex_throws_define | |
715 | AT_JT_initial_action], | |
716 | [AT_JT_parse_action])], | |
e9690142 | 717 | [AT_CHECK_JAVA_MINIMAL_W_LEXER([ |
e254a580 DJ |
718 | AT_JT_throws_define |
719 | AT_JT_lex_throws_define | |
720 | AT_JT_initial_action], | |
721 | [AT_JT_yylex_throws], | |
722 | [AT_JT_yylex_action], | |
723 | [], | |
724 | [AT_JT_parse_action])]) | |
725 | AT_CHECK_JAVA_GREP([[ *int yylex ()]AT_JT_yylex_throws *[;]]) | |
726 | AT_CHECK_JAVA_GREP([[ *private int yyaction ([^)]*)]AT_JT_yyaction_throws[ *]]) | |
727 | AT_CHECK_JAVA_GREP([[ *public boolean parse ()]AT_JT_parse_throws[ *]]) | |
728 | ])])]) | |
729 | ||
730 | AT_CLEANUP | |
731 | ||
732 | ||
1979121c DJ |
733 | # ------------------------------------- # |
734 | # Java constructor init and init_throws # | |
735 | # ------------------------------------- # | |
736 | ||
737 | AT_SETUP([Java constructor init and init_throws]) | |
738 | ||
739 | AT_CHECK_JAVA_MINIMAL([[ | |
740 | %define extends "Thread" | |
741 | %code init { super("Test Thread"); if (true) throw new InterruptedException(); } | |
742 | %define init_throws "InterruptedException" | |
743 | %lex-param {int lex_param}]]) | |
744 | AT_CHECK([[grep -q 'super("Test Thread"); if (true) throw new InterruptedException();' YYParser.java]]) | |
745 | ||
746 | AT_CHECK_JAVA_MINIMAL_W_LEXER([[ | |
747 | %define extends "Thread" | |
748 | %code init { super("Test Thread"); if (true) throw new InterruptedException(); } | |
749 | %define init_throws "InterruptedException"]], [], [[return EOF;]]) | |
750 | AT_CHECK([[grep -q 'super("Test Thread"); if (true) throw new InterruptedException();' YYParser.java]]) | |
751 | ||
752 | AT_CLEANUP | |
753 | ||
754 | ||
e254a580 DJ |
755 | # --------------------------------------------- # |
756 | # Java stype, position_class and location_class # | |
757 | # --------------------------------------------- # | |
758 | ||
759 | AT_SETUP([Java stype, position_class and location_class]) | |
760 | ||
761 | AT_CHECK_JAVA_MINIMAL([[ | |
762 | %define stype "java.awt.Color" | |
763 | %type<java.awt.Color> start; | |
764 | %define location_type "MyLoc" | |
765 | %define position_type "MyPos" | |
766 | %code { class MyPos {} }]], [[$$ = $<java.awt.Color>1;]], [[MyPos]]) | |
74553c98 AD |
767 | AT_CHECK([[grep 'java.awt.Color' YYParser.java]], [0], [ignore]) |
768 | AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Position']], [1], [ignore]) | |
769 | AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Location']], [1], [ignore]) | |
e254a580 DJ |
770 | |
771 | AT_CHECK_JAVA_MINIMAL_W_LEXER([[ | |
772 | %define stype "java.awt.Color" | |
773 | %type<java.awt.Color> start; | |
774 | %define location_type "MyLoc" | |
775 | %define position_type "MyPos" | |
776 | %code { class MyPos {} }]], [], [[return EOF;]], [], | |
777 | [[$$ = $<java.awt.Color>1;]], | |
778 | [[java.awt.Color]], [[MyPos]], [[MyLoc]]) | |
74553c98 AD |
779 | AT_CHECK([[grep 'java.awt.Color' YYParser.java]], [0], [ignore]) |
780 | AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Position']], [1], [ignore]) | |
781 | AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Location']], [1], [ignore]) | |
e254a580 DJ |
782 | |
783 | AT_CLEANUP | |
e34e97bc TL |
784 | |
785 | ||
786 | # ----------------------------------------------- # | |
787 | # Java syntax error handling without error token. # | |
788 | # ----------------------------------------------- # | |
789 | ||
790 | AT_SETUP([Java syntax error handling without error token]) | |
791 | ||
792 | AT_DATA([[YYParser.y]], [[%language "Java" | |
793 | ||
794 | %lex-param { String s } | |
795 | ||
796 | %code imports { | |
797 | import java.io.IOException; | |
798 | } | |
799 | ||
800 | %code lexer { | |
801 | String Input; | |
802 | int Position; | |
803 | ||
804 | public YYLexer (String s) | |
805 | { | |
806 | Input = s; | |
807 | Position = 0; | |
808 | } | |
809 | ||
810 | public void yyerror (String s) | |
811 | { | |
812 | System.err.println (s); | |
813 | } | |
814 | ||
815 | public Object getLVal () | |
816 | { | |
817 | return null; | |
818 | } | |
819 | ||
820 | public int yylex () throws IOException | |
821 | { | |
822 | if (Position >= Input.length ()) | |
823 | return EOF; | |
824 | else | |
825 | return Input.charAt (Position++); | |
826 | } | |
827 | } | |
828 | ||
829 | %code { | |
830 | public static void main (String args []) throws IOException | |
831 | { | |
832 | YYParser p = new YYParser (args [0]); | |
833 | p.parse (); | |
834 | } | |
835 | } | |
836 | %% | |
837 | input: | |
838 | 'a' 'a' | |
839 | ; | |
840 | ]]) | |
841 | AT_BISON_CHECK([[YYParser.y]]) | |
842 | AT_JAVA_COMPILE([[YYParser.java]]) | |
843 | AT_JAVA_PARSER_CHECK([[YYParser aa]], [[0]], [[]], [[]]) | |
844 | AT_JAVA_PARSER_CHECK([[YYParser ab]], [[0]], [[]], [[syntax error | |
845 | ]]) | |
846 | AT_JAVA_PARSER_CHECK([[YYParser ba]], [[0]], [[]], [[syntax error | |
847 | ]]) | |
848 | ||
849 | AT_CLEANUP |