1 # Checking Java Push Parsing. -*- Autotest -*-
3 # Copyright (C) 2013-2015, 2018-2021 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
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.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 # The Java push parser tests are intended primarily
19 # to verify that the sequence of states that the parser
20 # traverses is the same as a pull parser would traverse.
22 ##################################################
23 # Provide a way to generate data with and without push parsing
24 # so it is possible to capture the output for comparison
25 # (except the "trivial" tests).
26 # Use "both" rather than "push" so we can also set it to "pull" to
27 # get the "experr" data.
29 m4_define([PUSHPULLFLAG],[-Dapi.push-pull=both])
31 # AT_CHECK_JAVA_GREP(FILE, [LINE], [COUNT=1])
32 # -------------------------------------------
33 # Check that FILE contains exactly COUNT lines matching ^LINE$
34 # with grep. Unquoted so that COUNT can be a shell expression.
35 m4_define([AT_CHECK_JAVA_GREP],
36 [AT_CHECK_UNQUOTED([grep -c '^$2$' $1], [ignore], [m4_default([$3], [1])
40 ##################################################
42 AT_BANNER([[Java Push Parsing Tests]])
44 # Define a single copy of the trivial parser grammar.
45 # This is missing main(), so two versions
46 # are instantiated with different main() procedures.
47 m4_define([AT_TRIVIAL_GRAMMAR],
49 %define api.parser.class {YYParser}
50 %define parse.error verbose
65 # Define common code across to be included in
66 # class Main for the trivial parser tests.
67 m4_define([AT_TRIVIAL_COMMON],[[
68 static class YYerror implements YYParser.Lexer
70 public Object getLVal() {return null;}
71 public int yylex () throws java.io.IOException { return 0; }
72 public void yyerror (String msg) { System.err.println(msg); }
75 static YYParser parser = null;
76 static YYerror yyerror = null;
77 static int teststate = -1;
82 yyerror = new YYerror();
83 parser = new YYParser(yyerror);
84 parser.setDebugLevel(1);
88 static String[] teststatename
89 = new String[]{"YYACCEPT","YYABORT","YYERROR","UNKNOWN","YYPUSH_MORE"};
91 static void check(int teststate, int expected, String msg)
93 System.err.println("teststate="+teststatename[teststate]
94 +"; expected="+teststatename[expected]);
95 if (teststate != expected)
97 System.err.println("unexpected state: "+msg);
103 m4_define([AT_TRIVIAL_PARSER],[[
111 static public void main (String[] args)
116 teststate = parser.push_parse('a', null);
117 check(teststate,YYParser.YYPUSH_MORE,"push_parse('a', null)");
121 teststate = parser.push_parse('a', null);
122 check(teststate,YYParser.YYPUSH_MORE,"push_parse('a', null)");
123 teststate = parser.push_parse('b', null);
124 check(teststate,YYParser.YYPUSH_MORE,"push_parse('b', null)");
125 teststate = parser.push_parse('c', null);
126 check(teststate,YYParser.YYPUSH_MORE,"push_parse('c', null)");
127 teststate = parser.push_parse('\0', null);
128 check(teststate,YYParser.YYACCEPT,"push_parse('\\0', null)");
130 /* Reuse the parser instance and cause a failure */
131 teststate = parser.push_parse('b', null);
132 check(teststate,YYParser.YYABORT,"push_parse('b', null)");
140 m4_define([AT_TRIVIAL_PARSER_INITIAL_ACTION],[[
148 static public void main (String[] args)
153 teststate = parser.push_parse('a', null);
154 check(teststate,YYParser.YYPUSH_MORE,"push_parse('a', null)");
155 teststate = parser.push_parse('b', null);
156 check(teststate,YYParser.YYPUSH_MORE,"push_parse('b', null)");
157 teststate = parser.push_parse('c', null);
158 check(teststate,YYParser.YYPUSH_MORE,"push_parse('c', null)");
159 teststate = parser.push_parse('\0', null);
160 check(teststate,YYParser.YYACCEPT,"push_parse('\\0', null)");
168 ## ----------------------------------------------------- ##
169 ## Trivial Push Parser with api.push-pull verification. ##
170 ## ----------------------------------------------------- ##
172 AT_SETUP([Trivial Push Parser with api.push-pull verification])
173 AT_BISON_OPTION_PUSHDEFS
180 # Verify that the proper procedure(s) are generated for each case.
181 AT_BISON_CHECK([[-Dapi.push-pull=pull -o Main.java input.y]])
182 AT_CHECK_JAVA_GREP([[Main.java]],
183 [[.*public boolean parse().*]],
185 # If BISON_USE_PUSH_FOR_PULL is set, then we have one occurrence of
186 # this function, otherwise it should not be there.
187 AT_CHECK_JAVA_GREP([[Main.java]],
188 [[.*public int push_parse(int yylextoken, Object yylexval).*]],
189 [${BISON_USE_PUSH_FOR_PULL-0}])
191 AT_BISON_CHECK([[-Dapi.push-pull=both -o Main.java input.y]])
192 AT_CHECK_JAVA_GREP([[Main.java]],
193 [[.*public boolean parse().*]],
195 AT_CHECK_JAVA_GREP([[Main.java]],
196 [[.*public int push_parse(int yylextoken, Object yylexval).*]],
199 AT_BISON_CHECK([[-Dapi.push-pull=push -o Main.java input.y]])
200 AT_CHECK_JAVA_GREP([[Main.java]],
201 [[.*public boolean parse().*]],
203 AT_CHECK_JAVA_GREP([[Main.java]],
204 [[.*public int push_parse(int yylextoken, Object yylexval).*]],
207 AT_JAVA_COMPILE([[Main.java]])
208 AT_JAVA_PARSER_CHECK([Main], 0, [], [stderr-nolog])
209 AT_BISON_OPTION_POPDEFS
213 ## ------------------------------------------ ##
214 ## Trivial Push Parser with %initial-action. ##
215 ## ------------------------------------------ ##
217 AT_SETUP([Trivial Push Parser with %initial-action])
218 AT_BISON_OPTION_PUSHDEFS
222 System.err.println("Initial action invoked");
224 ]AT_TRIVIAL_PARSER_INITIAL_ACTION[
226 AT_BISON_OPTION_POPDEFS
227 AT_BISON_CHECK([[-Dapi.push-pull=push -o Main.java input.y]])
228 AT_CHECK_JAVA_GREP([[Main.java]],
229 [[System.err.println("Initial action invoked");]])
230 AT_JAVA_COMPILE([[Main.java]])
231 AT_JAVA_PARSER_CHECK([Main], 0, [], [stderr-nolog])
232 # Verify that initial action is called exactly once.
235 [[Initial action invoked]],