1 # Checking Push Parsing. -*- Autotest -*-
3 # Copyright (C) 2007, 2009-2015, 2018-2021 Free Software Foundation,
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]])
28 AT_BISON_OPTION_PUSHDEFS([%define api.push-pull push])
29 AT_DATA_GRAMMAR([[input.y]],
39 %define api.push-pull push
54 /* Make sure we don't try to free ps->yyss in this case. */
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. */
61 assert (yypush_parse (ps, 'a', YY_NULLPTR) == YYPUSH_MORE);
65 assert (yypush_parse (ps, 'a', YY_NULLPTR) == YYPUSH_MORE);
66 assert (yypush_parse (ps, 'b', YY_NULLPTR) == YYPUSH_MORE);
72 AT_BISON_OPTION_POPDEFS
74 AT_BISON_CHECK([[-o input.c input.y]])
76 AT_PARSER_CHECK([[input]])
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]],
94 ]m4_if([$1], [[both]], [AT_YYLEX_DECLARE([])])[
97 %define api.push-pull ]$1[
105 ]m4_if([$1], [[both]], [AT_YYLEX_DEFINE])[
111 for (i = 0; i < 2; ++i)
113 yypstate *ps = yypstate_new ();
115 assert (yypstate_new () == YY_NULLPTR);
116 ]m4_if([$1], [[both]], [[assert (yyparse () == 2)]])[;
118 assert (yypush_parse (ps) == 0);
119 assert (yypstate_new () == YY_NULLPTR);
120 ]m4_if([$1], [[both]], [[assert (yyparse () == 2)]])[;
121 yypstate_delete (ps);
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])
141 ## ----------------------- ##
142 ## Unsupported Skeletons. ##
143 ## ----------------------- ##
145 AT_SETUP([[Unsupported Skeletons]])
147 AT_BISON_OPTION_PUSHDEFS([%define api.push-pull push])
150 %define api.push-pull push
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
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]],
180 static char *string_concat (char *a, char *b);
185 %define api.pure full
186 %define api.push-pull push
192 %destructor { free ($$); } <sval>
193 %printer { fprintf (yyo, "%s", $$); } <sval>
203 : text EOL { printf ("text: %s\n", $1); free ($1); YYACCEPT; };
207 | text RAW { $$ = string_concat ($1, $2); }
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));
220 strcpy (res + la, b);
227 push (yypstate *ps, yytoken_kind_t kind, const char *str)
230 lval.sval = str ? strdup (str) : YY_NULLPTR;
231 switch (yypush_parse (ps, kind, &lval))
236 // parsing incomplete, but valid; parser not reset
239 // YYABORT or syntax invalid; parser is reset
240 fprintf (stderr, "invalid input, but no error was thrown\n");
243 // memory exhaustion; parser is reset
244 fprintf (stderr, "memory exhaustion during yypush_parse\n");
253 yydebug = !!getenv ("YYDEBUG");
254 yypstate *ps = yypstate_new ();
256 #define PUSH(Kind, Val) \
258 if (push (ps, Kind, Val)) \
264 PUSH (EOL, YY_NULLPTR);
268 PUSH (EOL, YY_NULLPTR);
270 yypstate_delete (ps);
276 AT_FULL_COMPILE([input])
277 AT_CHECK([./input], 0,
282 AT_BISON_OPTION_POPDEFS