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