]> git.saurik.com Git - bison.git/blame - tests/java.at
tests: reorder.
[bison.git] / tests / java.at
CommitLineData
0049ec86 1# Java tests for simple calculator. -*- Autotest -*-
8405b70c 2
c932d613 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
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])
8405b70c
PB
27# ----------------------------------------------------------------------
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.
32m4_define([_AT_DATA_JAVA_CALC_Y],
33[m4_if([$1$2$3], $[1]$[2]$[3], [],
34 [m4_fatal([$0: Invalid arguments: $@])])dnl
35AT_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%%
64input:
65 line
66| input line
67;
68
69line:
70 '\n'
71| exp '\n'
72| error '\n'
73;
74
75exp:
76 NUM { $$ = $1; }
77| exp '=' exp
78 {
79 if ($1.intValue () != $3.intValue ())
77768165 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
101class 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
115AT_LOCATION_IF([[
77768165 116 Position yypos = new Position (1, 0);
8405b70c 117
01b477c6 118 public Position getStartPos() {
77768165 119 return yypos;
8405b70c
PB
120 }
121
01b477c6 122 public Position getEndPos() {
77768165 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
77768165 131 System.err.println (l + ": " + s);
01b477c6
PB
132 }
133]], [[
134 public void yyerror (String s)
135 {
8defe11b 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 ();
77768165
JD
148 ]AT_LOCATION_IF([[yypos = new Position (yypos.lineno (),
149 yypos.token () + 1);]])[
01b477c6
PB
150 if (ttype == st.TT_EOF)
151 return Calc.EOF;
152
153 else if (ttype == st.TT_EOL)
154 {
77768165 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);
162 return Calc.NUM;
163 }
164
165 else
166 return st.ttype;
167 }
168
169
170]AT_LEXPARAM_IF([[
171};
172%%]], [[
173}]])
174
175[
176class Position {
177 public int line;
77768165 178 public int token;
01b477c6
PB
179
180 public Position ()
181 {
182 line = 0;
77768165 183 token = 0;
01b477c6
PB
184 }
185
77768165 186 public Position (int l, int t)
01b477c6
PB
187 {
188 line = l;
77768165 189 token = t;
01b477c6
PB
190 }
191
192 public boolean equals (Position l)
193 {
77768165 194 return l.line == line && l.token == token;
01b477c6
PB
195 }
196
197 public String toString ()
198 {
77768165 199 return Integer.toString (line) + "." + Integer.toString(token);
01b477c6
PB
200 }
201
202 public int lineno ()
203 {
204 return line;
205 }
77768165
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])
8405b70c
PB
218# -------------------------------------------------
219# Produce `calc.y'.
220m4_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.
235m4_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]])
241AT_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.
246AT_DATA([[expout]],
247[$3
248])
249# 2. If locations are not used, remove them.
250AT_YYERROR_SEES_LOC_IF([],
251[[sed 's/^[-0-9.]*: //' expout >at-expout
252mv at-expout expout]])
253# 3. If error-verbose is not used, strip the`, unexpected....' part.
254m4_bmatch([$1], [%error-verbose], [],
255[[sed 's/syntax error, .*$/syntax error/' expout >at-expout
256mv at-expout expout]])
257# 4. Check
258AT_CHECK([cat stderr], 0, [expout])
259])
260
01b477c6 261# _AT_CHECK_JAVA_CALC([BISON-DIRECTIVES], [BISON-CODE])
8405b70c
PB
262# -----------------------------------------------------------------------
263# Start a testing chunk which compiles `calc' grammar with
264# BISON-DIRECTIVES, and performs several tests over the parser.
265m4_define([_AT_CHECK_JAVA_CALC],
266[# We use integers to avoid dependencies upon the precision of doubles.
267AT_SETUP([Calculator $1])
268
269AT_BISON_OPTION_PUSHDEFS([$1])
270
271AT_DATA_JAVA_CALC_Y([$1
272%code {
273$2
01b477c6 274}])
8405b70c 275
da730230 276AT_BISON_CHECK([-o Calc.java Calc.y])
8405b70c
PB
277AT_JAVA_COMPILE([Calc.java])
278
279# Test the priorities.
280AT_DATA([[input]],
281[[1 + 2 * 3 = 7
2821 + 2 * -3 = -5
283
284-1^2 = -1
285(-1)^2 = 1
286
287---1 = -1
288
2891 - 2 - 3 = -4
2901 - (2 - 3) = 2
291
2922^2^3 = 256
293(2^2)^3 = 64
294]])
295AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])
296
297
298# Some syntax errors.
299_AT_CHECK_JAVA_CALC_ERROR([$1], [0 0],
77768165 300 [1.2: syntax error, unexpected number])
8405b70c 301_AT_CHECK_JAVA_CALC_ERROR([$1], [1//2],
77768165 302 [1.3: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
8405b70c 303_AT_CHECK_JAVA_CALC_ERROR([$1], [error],
77768165 304 [1.1: syntax error, unexpected $undefined])
8405b70c 305_AT_CHECK_JAVA_CALC_ERROR([$1], [1 = 2 = 3],
77768165 306 [1.4: syntax error, unexpected '='])
8405b70c
PB
307_AT_CHECK_JAVA_CALC_ERROR([$1], [
308+1],
77768165 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],
77768165 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#
8defe11b 330_AT_CHECK_JAVA_CALC_ERROR([$1],
8405b70c 331 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
77768165
JD
332[1.2: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
3331.11: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
3341.14: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
3351.24: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
3361.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],
77768165
JD
341[1.7: syntax error, unexpected number
3421.1-1.10: calc: error: 2222 != 1])
8405b70c 343_AT_CHECK_JAVA_CALC_ERROR([$1], [(- *) + (0 0) = 1],
77768165
JD
344[1.3: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
3451.8: syntax error, unexpected number
3461.1-1.11: calc: error: 2222 != 1])
8405b70c
PB
347AT_BISON_OPTION_POPDEFS
348
349AT_CLEANUP
350])# _AT_CHECK_JAVA_CALC
351
352
01b477c6 353# AT_CHECK_JAVA_CALC([BISON-DIRECTIVES])
8405b70c
PB
354# --------------------------------------------------------
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.
358m4_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 370AT_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
379AT_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
392AT_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.
399m4_define([AT_CHECK_JAVA_MINIMAL],
400[
401AT_DATA([[YYParser.y]], [
402%language "Java"
403%locations
404%debug
405%error-verbose
406%token-table
407$1
408%%
409start: "end" {$2};
410%%
411class m4_default([$3], [Position]) {}
412])
413AT_BISON_CHECK([[YYParser.y]])
8defe11b 414AT_CHECK([[grep '[mb]4_' YYParser.y]], [1], [ignore])
e254a580
DJ
415AT_JAVA_COMPILE([[YYParser.java]])
416])
417
418
419# AT_CHECK_JAVA_MINIMAL_W_LEXER([1:DIRECTIVES], [2:LEX_THROWS],
e0e2b933
AD
420# [3:YYLEX_ACTION], [4:LEXER_BODY], [5:PARSER_ACTION], [6:STYPE],
421# [7:POSITION_TYPE], [8:LOCATION_TYPE])
e254a580
DJ
422# ---------------------------------------------------------------------
423# Check that a mininal parser with DIRECTIVES and a "%code lexer".
424# YYLEX is the body of yylex () which throws LEX_THROW.
425# compiles in Java.
426m4_define([AT_CHECK_JAVA_MINIMAL_W_LEXER],
427[AT_CHECK_JAVA_MINIMAL([$1
428
429%code lexer
430{
431 m4_default([$6], [Object]) yylval;
432 public m4_default([$6], [Object]) getLVal() { return yylval; }
433
434 public m4_default([$7], [Position]) getStartPos() { return null; }
435 public m4_default([$7], [Position]) getEndPos() { return null; }
436
437 public void yyerror (m4_default([$8], [Location]) loc, String s)
438 {
439 System.err.println (loc + ": " + s);
440 }
441
442 public int yylex ()$2
443 {
444 $3
445 }
8defe11b 446
e254a580
DJ
447 $4
448}], [$5], [$7])])
449
450
451# AT_CHECK_JAVA_GREP([LINE], [COUNT=1])
452# -------------------------------------
453# Check that YYParser.java contains exactly COUNT lines matching ^LINE$
454# with grep.
455m4_define([AT_CHECK_JAVA_GREP],
456 [AT_CHECK([grep -c '^$1$' YYParser.java], [], [m4_default([$2], [1])
457])
458])
459
460
461# ----------------------------------- #
462# Java parser class and package names #
463# ----------------------------------- #
464
465AT_SETUP([Java parser class and package names])
466
467AT_CHECK_JAVA_MINIMAL([])
468AT_CHECK_JAVA_GREP([[class YYParser]])
469
470AT_CHECK_JAVA_MINIMAL([[%name-prefix "Prefix"]])
471AT_CHECK_JAVA_GREP([[class PrefixParser]])
472
473AT_CHECK_JAVA_MINIMAL([[%define parser_class_name "ParserClassName"]])
474AT_CHECK_JAVA_GREP([[class ParserClassName]])
475
476AT_CHECK_JAVA_MINIMAL([[%define package "user_java_package"]])
477AT_CHECK_JAVA_GREP([[package user_java_package;]])
478
479AT_CLEANUP
480
481
482# --------------------------- #
483# Java parser class modifiers #
484# --------------------------- #
485
486AT_SETUP([Java parser class modifiers])
487
488AT_CHECK_JAVA_MINIMAL([[%define abstract]])
489AT_CHECK_JAVA_GREP([[abstract class YYParser]])
490
491AT_CHECK_JAVA_MINIMAL([[%define final]])
492AT_CHECK_JAVA_GREP([[final class YYParser]])
493
494AT_CHECK_JAVA_MINIMAL([[%define strictfp]])
495AT_CHECK_JAVA_GREP([[strictfp class YYParser]])
496
497AT_CHECK_JAVA_MINIMAL([[
498%define abstract
499%define strictfp]])
500AT_CHECK_JAVA_GREP([[abstract strictfp class YYParser]])
501
502AT_CHECK_JAVA_MINIMAL([[
503%define final
504%define strictfp]])
505AT_CHECK_JAVA_GREP([[final strictfp class YYParser]])
506
507AT_CHECK_JAVA_MINIMAL([[%define public]])
508AT_CHECK_JAVA_GREP([[public class YYParser]])
509
510AT_CHECK_JAVA_MINIMAL([[
511%define public
512%define abstract]])
513AT_CHECK_JAVA_GREP([[public abstract class YYParser]])
514
515AT_CHECK_JAVA_MINIMAL([[
516%define public
517%define final]])
518AT_CHECK_JAVA_GREP([[public final class YYParser]])
519
520AT_CHECK_JAVA_MINIMAL([[
521%define public
522%define strictfp]])
523AT_CHECK_JAVA_GREP([[public strictfp class YYParser]])
524
525AT_CHECK_JAVA_MINIMAL([[
526%define public
527%define abstract
528%define strictfp]])
529AT_CHECK_JAVA_GREP([[public abstract strictfp class YYParser]])
530
531AT_CHECK_JAVA_MINIMAL([[
532%define public
533%define final
534%define strictfp]])
535AT_CHECK_JAVA_GREP([[public final strictfp class YYParser]])
536
537AT_CLEANUP
538
539
540# ---------------------------------------- #
541# Java parser class extends and implements #
542# ---------------------------------------- #
543
544AT_SETUP([Java parser class extends and implements])
545
546AT_CHECK_JAVA_MINIMAL([[%define extends "Thread"]])
547AT_CHECK_JAVA_GREP([[class YYParser extends Thread]])
548
549AT_CHECK_JAVA_MINIMAL([[%define implements "Cloneable"]])
550AT_CHECK_JAVA_GREP([[class YYParser implements Cloneable]])
551
552AT_CHECK_JAVA_MINIMAL([[
553%define extends "Thread"
554%define implements "Cloneable"]])
555AT_CHECK_JAVA_GREP([[class YYParser extends Thread implements Cloneable]])
556
557AT_CLEANUP
558
559
560# -------------------------------- #
561# Java %parse-param and %lex-param #
562# -------------------------------- #
563
564AT_SETUP([Java %parse-param and %lex-param])
565
566AT_CHECK_JAVA_MINIMAL([])
567AT_CHECK_JAVA_GREP([[ *public YYParser (Lexer yylexer) {]])
568
569AT_CHECK_JAVA_MINIMAL([[%parse-param {int parse_param1}]])
570AT_CHECK_JAVA_GREP([[ *protected final int parse_param1;]])
571AT_CHECK_JAVA_GREP([[ *public YYParser (Lexer yylexer, *int parse_param1) {]])
e0e2b933 572AT_CHECK_JAVA_GREP([[[ ]*this.parse_param1 = parse_param1;]])
e254a580
DJ
573
574AT_CHECK_JAVA_MINIMAL([[
575%parse-param {int parse_param1}
576%parse-param {long parse_param2}]])
577AT_CHECK_JAVA_GREP([[ *protected final int parse_param1;]])
578AT_CHECK_JAVA_GREP([[ *protected final long parse_param2;]])
579AT_CHECK_JAVA_GREP([[ *public YYParser (Lexer yylexer, *int parse_param1, *long parse_param2) {]])
e0e2b933
AD
580AT_CHECK_JAVA_GREP([[[ ]*this.parse_param1 = parse_param1;]])
581AT_CHECK_JAVA_GREP([[[ ]*this.parse_param2 = parse_param2;]])
e254a580
DJ
582
583AT_CHECK_JAVA_MINIMAL_W_LEXER([], [], [[return EOF;]])
584AT_CHECK_JAVA_GREP([[ *public YYParser () {]])
585AT_CHECK_JAVA_GREP([[ *protected YYParser (Lexer yylexer) {]])
586
587AT_CHECK_JAVA_MINIMAL_W_LEXER([[%parse-param {int parse_param1}]],
588 [], [[return EOF;]])
589AT_CHECK_JAVA_GREP([[ *protected final int parse_param1;]])
590AT_CHECK_JAVA_GREP([[ *public YYParser (int parse_param1) {]])
591AT_CHECK_JAVA_GREP([[ *protected YYParser (Lexer yylexer, *int parse_param1) {]])
e0e2b933 592AT_CHECK_JAVA_GREP([[[ ]*this.parse_param1 = parse_param1;]], [2])
e254a580
DJ
593
594AT_CHECK_JAVA_MINIMAL_W_LEXER([[
595%parse-param {int parse_param1}
596%parse-param {long parse_param2}]],
597 [], [[return EOF;]])
598AT_CHECK_JAVA_GREP([[ *protected final int parse_param1;]])
599AT_CHECK_JAVA_GREP([[ *protected final long parse_param2;]])
600AT_CHECK_JAVA_GREP([[ *public YYParser (int parse_param1, *long parse_param2) {]])
601AT_CHECK_JAVA_GREP([[ *protected YYParser (Lexer yylexer, *int parse_param1, *long parse_param2) {]])
e0e2b933
AD
602AT_CHECK_JAVA_GREP([[[ ]*this.parse_param1 = parse_param1;]], [2])
603AT_CHECK_JAVA_GREP([[[ ]*this.parse_param2 = parse_param2;]], [2])
e254a580
DJ
604
605AT_CHECK_JAVA_MINIMAL_W_LEXER([[%lex-param {char lex_param1}]],
606 [], [[return EOF;]], [[YYLexer (char lex_param1) {}]])
607AT_CHECK_JAVA_GREP([[ *public YYParser (char lex_param1) {]])
608AT_CHECK_JAVA_GREP([[.* = new YYLexer *(lex_param1);]])
609
610AT_CHECK_JAVA_MINIMAL_W_LEXER([[
611%lex-param {char lex_param1}
612%lex-param {short lex_param2}]],
613 [], [[return EOF;]], [[YYLexer (char lex_param1, short lex_param2) {}]])
614AT_CHECK_JAVA_GREP([[ *public YYParser (char lex_param1, *short lex_param2) {]])
615AT_CHECK_JAVA_GREP([[.* = new YYLexer *(lex_param1, *lex_param2);]])
616
617AT_CHECK_JAVA_MINIMAL_W_LEXER([[
618%parse-param {int parse_param1}
619%parse-param {long parse_param2}
620%lex-param {char lex_param1}
621%lex-param {short lex_param2}]],
622 [], [[return EOF;]], [[YYLexer (char lex_param1, short lex_param2) {}]])
623AT_CHECK_JAVA_GREP([[ *protected final int parse_param1;]])
624AT_CHECK_JAVA_GREP([[ *protected final long parse_param2;]])
625AT_CHECK_JAVA_GREP([[ *public YYParser (char lex_param1, *short lex_param2, *int parse_param1, *long parse_param2) {]])
626AT_CHECK_JAVA_GREP([[.* = new YYLexer *(lex_param1, *lex_param2);]])
627AT_CHECK_JAVA_GREP([[ *protected YYParser (Lexer yylexer, *int parse_param1, *long parse_param2) {]])
e0e2b933
AD
628AT_CHECK_JAVA_GREP([[[ ]*this.parse_param1 = parse_param1;]], [2])
629AT_CHECK_JAVA_GREP([[[ ]*this.parse_param2 = parse_param2;]], [2])
e254a580
DJ
630
631AT_CLEANUP
632
633
634# ------------------------- #
635# Java throw specifications #
636# ------------------------- #
637
638AT_SETUP([Java throws specifications])
639
640# %define throws - 0 1 2
641# %define lex-throws - 0 1 2
642# %code lexer 0 1
643
644m4_define([AT_JT_lex_throws_define], [m4_case(AT_JT_lex_throws,
645 -1, [],
646 0, [[%define lex_throws ""]],
647 1, [[%define lex_throws "InterruptedException"]],
648 2, [[%define lex_throws "InterruptedException, IllegalAccessException"]])])
649
650m4_define([AT_JT_yylex_throws], [m4_case(AT_JT_lex_throws,
651 -1, [[ throws java.io.IOException]],
652 0, [],
653 1, [[ throws InterruptedException]],
654 2, [[ throws InterruptedException, IllegalAccessException]])])
655
656m4_define([AT_JT_yylex_action], [m4_case(AT_JT_lex_throws,
657 -1, [[throw new java.io.IOException();]],
658 0, [[return EOF;]],
659 1, [[throw new InterruptedException();]],
660 2, [[throw new IllegalAccessException();]])])
661
662
663m4_define([AT_JT_throws_define], [m4_case(AT_JT_throws,
664 -1, [],
665 0, [[%define throws ""]],
666 1, [[%define throws "ClassNotFoundException"]],
667 2, [[%define throws "ClassNotFoundException, InstantiationException"]])])
668
669m4_define([AT_JT_yyaction_throws], [m4_case(AT_JT_throws,
670 -1, [],
671 0, [],
672 1, [[ throws ClassNotFoundException]],
673 2, [[ throws ClassNotFoundException, InstantiationException]])])
674
675m4_define([AT_JT_parse_throws_2], [m4_case(AT_JT_throws,
676 -1, [],
677 0, [],
678 1, [[, ClassNotFoundException]],
679 2, [[, ClassNotFoundException, InstantiationException]])])
680
681m4_define([AT_JT_parse_throws],
682 [m4_if(m4_quote(AT_JT_yylex_throws), [],
683 [AT_JT_yyaction_throws],
684 [AT_JT_yylex_throws[]AT_JT_parse_throws_2])])
685
686m4_define([AT_JT_initial_action], [m4_case(AT_JT_throws,
687 -1, [],
688 0, [],
689 1, [[%initial-action {if (true) throw new ClassNotFoundException();}]],
690 2, [[%initial-action {if (true) throw new InstantiationException();}]])])
691
692m4_define([AT_JT_parse_action], [m4_case(AT_JT_throws,
693 -1, [],
694 0, [],
695 1, [[throw new ClassNotFoundException();]],
696 2, [[throw new ClassNotFoundException();]])])
697
698m4_for([AT_JT_lexer], 0, 1, 1,
699 [m4_for([AT_JT_lex_throws], -1, 2, 1,
700 [m4_for([AT_JT_throws], -1, 2, 1,
701 [m4_if(AT_JT_lexer, 0,
702 [AT_CHECK_JAVA_MINIMAL([
703AT_JT_throws_define
704AT_JT_lex_throws_define
705AT_JT_initial_action],
706[AT_JT_parse_action])],
707 [AT_CHECK_JAVA_MINIMAL_W_LEXER([
708AT_JT_throws_define
709AT_JT_lex_throws_define
710AT_JT_initial_action],
711[AT_JT_yylex_throws],
712[AT_JT_yylex_action],
713[],
714[AT_JT_parse_action])])
715AT_CHECK_JAVA_GREP([[ *int yylex ()]AT_JT_yylex_throws *[;]])
716AT_CHECK_JAVA_GREP([[ *private int yyaction ([^)]*)]AT_JT_yyaction_throws[ *]])
717AT_CHECK_JAVA_GREP([[ *public boolean parse ()]AT_JT_parse_throws[ *]])
718])])])
719
720AT_CLEANUP
721
722
723# --------------------------------------------- #
724# Java stype, position_class and location_class #
725# --------------------------------------------- #
726
727AT_SETUP([Java stype, position_class and location_class])
728
729AT_CHECK_JAVA_MINIMAL([[
730%define stype "java.awt.Color"
731%type<java.awt.Color> start;
732%define location_type "MyLoc"
733%define position_type "MyPos"
734%code { class MyPos {} }]], [[$$ = $<java.awt.Color>1;]], [[MyPos]])
8defe11b
AD
735AT_CHECK([[grep 'java.awt.Color' YYParser.java]], [0], [ignore])
736AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Position']], [1], [ignore])
737AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Location']], [1], [ignore])
e254a580
DJ
738
739AT_CHECK_JAVA_MINIMAL_W_LEXER([[
740%define stype "java.awt.Color"
741%type<java.awt.Color> start;
742%define location_type "MyLoc"
743%define position_type "MyPos"
744%code { class MyPos {} }]], [], [[return EOF;]], [],
745[[$$ = $<java.awt.Color>1;]],
746[[java.awt.Color]], [[MyPos]], [[MyLoc]])
8defe11b
AD
747AT_CHECK([[grep 'java.awt.Color' YYParser.java]], [0], [ignore])
748AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Position']], [1], [ignore])
749AT_CHECK([[$EGREP -v ' */?\*' YYParser.java | grep 'Location']], [1], [ignore])
e254a580
DJ
750
751AT_CLEANUP
e34e97bc
TL
752
753
754# ----------------------------------------------- #
755# Java syntax error handling without error token. #
756# ----------------------------------------------- #
757
758AT_SETUP([Java syntax error handling without error token])
759
760AT_DATA([[YYParser.y]], [[%language "Java"
761
762%lex-param { String s }
763
764%code imports {
765 import java.io.IOException;
766}
767
768%code lexer {
769 String Input;
770 int Position;
771
772 public YYLexer (String s)
773 {
774 Input = s;
775 Position = 0;
776 }
777
778 public void yyerror (String s)
779 {
780 System.err.println (s);
781 }
782
783 public Object getLVal ()
784 {
785 return null;
786 }
787
788 public int yylex () throws IOException
789 {
790 if (Position >= Input.length ())
791 return EOF;
792 else
793 return Input.charAt (Position++);
794 }
795}
796
797%code {
798 public static void main (String args []) throws IOException
799 {
800 YYParser p = new YYParser (args [0]);
801 p.parse ();
802 }
803}
804%%
805input:
806 'a' 'a'
807;
808]])
809AT_BISON_CHECK([[YYParser.y]])
810AT_JAVA_COMPILE([[YYParser.java]])
811AT_JAVA_PARSER_CHECK([[YYParser aa]], [[0]], [[]], [[]])
812AT_JAVA_PARSER_CHECK([[YYParser ab]], [[0]], [[]], [[syntax error
813]])
814AT_JAVA_PARSER_CHECK([[YYParser ba]], [[0]], [[]], [[syntax error
815]])
816
817AT_CLEANUP