news: update
[bison.git] / tests / push.at
blob0cfb346e7ae679c45a71e6413b661425bc61a301
1 # Checking Push Parsing.                            -*- Autotest -*-
3 # Copyright (C) 2007, 2009-2015, 2018-2021 Free Software Foundation,
4 # Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 AT_BANNER([[Push Parsing Tests]])
21 ## -------------------------------- ##
22 ## Memory Leak for Early Deletion.  ##
23 ## -------------------------------- ##
25 AT_SETUP([[Memory Leak for Early Deletion]])
27 # Requires Valgrind.
28 AT_BISON_OPTION_PUSHDEFS([%define api.push-pull push])
29 AT_DATA_GRAMMAR([[input.y]],
32   #include <assert.h>
33   #include <stdio.h>
34   #define YYINITDEPTH 1
35 ]AT_YYERROR_DECLARE[
38 %define api.pure
39 %define api.push-pull push
43 start: 'a' 'b' 'c' ;
47 ]AT_YYERROR_DEFINE[
49 int
50 main (void)
52   yypstate *ps;
54   /* Make sure we don't try to free ps->yyss in this case.  */
55   ps = yypstate_new ();
56   yypstate_delete (ps);
58   /* yypstate_delete used to leak ps->yyss if the stack was reallocated but the
59      parse did not return on success, syntax error, or memory exhaustion.  */
60   ps = yypstate_new ();
61   assert (yypush_parse (ps, 'a', YY_NULLPTR) == YYPUSH_MORE);
62   yypstate_delete (ps);
64   ps = yypstate_new ();
65   assert (yypush_parse (ps, 'a', YY_NULLPTR) == YYPUSH_MORE);
66   assert (yypush_parse (ps, 'b', YY_NULLPTR) == YYPUSH_MORE);
67   yypstate_delete (ps);
69   return 0;
71 ]])
72 AT_BISON_OPTION_POPDEFS
74 AT_BISON_CHECK([[-o input.c input.y]])
75 AT_COMPILE([[input]])
76 AT_PARSER_CHECK([[input]])
78 AT_CLEANUP
80 ## --------------------------- ##
81 ## Multiple impure instances.  ##
82 ## --------------------------- ##
84 AT_SETUP([[Multiple impure instances]])
86 m4_pushdef([AT_MULTIPLE_IMPURE_INSTANCES_CHECK], [
87 AT_BISON_OPTION_PUSHDEFS([%define api.push-pull $1])
88 AT_DATA_GRAMMAR([[input.y]],
91   #include <assert.h>
92   #include <stdio.h>
93 ]AT_YYERROR_DECLARE[
94 ]m4_if([$1], [[both]], [AT_YYLEX_DECLARE([])])[
97 %define api.push-pull ]$1[
101 start: ;
104 ]AT_YYERROR_DEFINE[
105 ]m4_if([$1], [[both]], [AT_YYLEX_DEFINE])[
108 main (void)
110   int i;
111   for (i = 0; i < 2; ++i)
112     {
113       yypstate *ps = yypstate_new ();
114       assert (ps);
115       assert (yypstate_new () == YY_NULLPTR);
116       ]m4_if([$1], [[both]], [[assert (yyparse () == 2)]])[;
117       yychar = 0;
118       assert (yypush_parse (ps) == 0);
119       assert (yypstate_new () == YY_NULLPTR);
120       ]m4_if([$1], [[both]], [[assert (yyparse () == 2)]])[;
121       yypstate_delete (ps);
122     }
124   return 0;
128 AT_BISON_CHECK([[-o input.c input.y]])
129 AT_COMPILE([[input]])
130 AT_PARSER_CHECK([[input]])
131 AT_BISON_OPTION_POPDEFS
134 AT_MULTIPLE_IMPURE_INSTANCES_CHECK([[both]])
135 AT_MULTIPLE_IMPURE_INSTANCES_CHECK([[push]])
137 m4_popdef([AT_MULTIPLE_IMPURE_INSTANCES_CHECK])
139 AT_CLEANUP
141 ## ----------------------- ##
142 ## Unsupported Skeletons.  ##
143 ## ----------------------- ##
145 AT_SETUP([[Unsupported Skeletons]])
147 AT_BISON_OPTION_PUSHDEFS([%define api.push-pull push])
148 AT_DATA([[input.y]],
149 [[%glr-parser
150 %define api.push-pull push
152 start: ;
154 AT_BISON_OPTION_POPDEFS
156 AT_BISON_CHECK([[input.y]], [[1]], [],
157 [[input.y:2.1-26: error: %define variable 'api.push-pull' is not used
160 AT_CLEANUP
163 ## -------------- ##
164 ## Pstate reuse.  ##
165 ## -------------- ##
167 AT_SETUP([[Pstate reuse]])
169 # Make sure that when a single pstate is used for multiple successive
170 # parses, no state from a previous run leaks into the following one.
172 # See https://lists.gnu.org/r/bug-bison/2021-03/msg00000.html.
174 AT_BISON_OPTION_PUSHDEFS([%define api.push-pull push])
175 AT_DATA_GRAMMAR([[input.y]],
176 [[%code top {
177   #include <stdlib.h>
178   #include <string.h>
180   static char *string_concat (char *a, char *b);
181   ]AT_YYERROR_DECLARE[
184 %define parse.trace
185 %define api.pure full
186 %define api.push-pull push
187 %expect 0
189 %union {
190   char *sval;
192 %destructor { free ($$); } <sval>
193 %printer { fprintf (yyo, "%s", $$); } <sval>
195 %token <sval> RAW
196 %token EOL
198 %type <sval> text
202 line
203   : text EOL  { printf ("text: %s\n", $1); free ($1); YYACCEPT; };
205 text
206   : RAW       { $$ = $1; }
207   | text RAW  { $$ = string_concat ($1, $2); }
208   ;
211 ]AT_YYERROR_DEFINE[
213 static char *
214 string_concat (char *a, char *b)
216   size_t la = strlen (a);
217   size_t lb = strlen (b);
218   char *res = YY_CAST (char *, malloc (la + lb + 1));
219   strcpy (res, a);
220   strcpy (res + la, b);
221   free (a);
222   free (b);
223   return res;
226 static int
227 push (yypstate *ps, yytoken_kind_t kind, const char *str)
229   YYSTYPE lval;
230   lval.sval = str ? strdup (str) :  YY_NULLPTR;
231   switch (yypush_parse (ps, kind, &lval))
232     {
233     case 0:
234       return 0;
235     case YYPUSH_MORE:
236       // parsing incomplete, but valid; parser not reset
237       return 0;
238     case 1:
239       // YYABORT or syntax invalid; parser is reset
240       fprintf (stderr, "invalid input, but no error was thrown\n");
241       return 1;
242     case 2:
243       // memory exhaustion; parser is reset
244       fprintf (stderr, "memory exhaustion during yypush_parse\n");
245       return 1;
246     }
247   return 1;
251 main (void)
253   yydebug = !!getenv ("YYDEBUG");
254   yypstate *ps = yypstate_new ();
256 #define PUSH(Kind, Val)                         \
257   do {                                          \
258     if (push (ps, Kind, Val))                   \
259       return 1;                                 \
260   } while (0)
262   PUSH (RAW, "te");
263   PUSH (RAW, "xt");
264   PUSH (EOL,  YY_NULLPTR);
266   PUSH (RAW, "te");
267   PUSH (RAW, "xt");
268   PUSH (EOL,  YY_NULLPTR);
270   yypstate_delete (ps);
272   return 0;
276 AT_FULL_COMPILE([input])
277 AT_CHECK([./input], 0,
278 [[text: text
279 text: text
282 AT_BISON_OPTION_POPDEFS
283 AT_CLEANUP