1 # Value type. -*- Autotest -*-
3 # Copyright (C) 2013-2015, 2018-2019 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 <http://www.gnu.org/licenses/>.
18 AT_BANNER([[Value type tests.]])
21 ## ----------------------------------- ##
22 ## %union vs. %define api.value.type. ##
23 ## ----------------------------------- ##
25 AT_SETUP([[%union vs. %define api.value.type]])
28 [[%union { int ival; }
29 %define api.value.type union-directive
34 AT_BISON_CHECK([[input.y]], [[1]], [[]],
35 [[input.y:2.1-38: error: '%union' and '%define api.value.type' cannot be used together
40 ## ---------------------------------------- ##
41 ## %yacc vs. %define api.value.type union. ##
42 ## ---------------------------------------- ##
44 AT_SETUP([[%yacc vs. %define api.value.type union]])
48 %define api.value.type union
53 AT_BISON_CHECK([[input.y]], [[1]], [[]],
54 [[input.y:2.1-28: error: '%yacc' and '%define api.value.type "union"' cannot be used together
60 ## ---------------- ##
62 ## ---------------- ##
64 # _AT_TEST($1: BISON-DIRECTIVES,
65 # $2: MORE-BISON-DIRECTIVES,
71 # --------------------------------------
72 # Compile the grammar and check the expected result.
73 # BISON-DIRECTIVES are passed to AT_SETUP, contrary to MORE-BISON-DIRECTIVES.
75 # Use REQUIREMENT e.g. skip the test if the compiler does not meet the
77 m4_pushdef([_AT_TEST],
80 AT_KEYWORDS([api.value.type])
82 AT_BISON_OPTION_PUSHDEFS([%debug $1 $2])
83 AT_DATA_GRAMMAR([test.y],
103 ]AT_YYLEX_DEFINE([$4], [$5])[
107 AT_LANG_FOR_EACH_STD([
109 AT_FULL_COMPILE([[test]])
110 AT_PARSER_CHECK([[test]], 0, [[$6
114 AT_BISON_OPTION_POPDEFS
118 # AT_TEST($1: BISON-DIRECTIVES,
119 # $2: MORE-BISON-DIRECTIVES,
122 # $5: SCANNER-ACTION,
125 # --------------------------------------
126 # Check with and without %defines, to avoid regressions. It turns out
127 # that in that case yacc.c calls the set-up of the %union twice,
128 # because YYSTYPE is defined once in the header, and once in the
129 # implementation file (eventually it'd be better to include the header
130 # file, but that's another story). Unfortunately running these macros
131 # a second time doubled the side-effects and resulted in a double
132 # definition of the union members.
133 m4_pushdef([AT_TEST],
134 [_AT_TEST([$1], [$2], [$3], [$4], [$5], [$6], [$7])
135 _AT_TEST([$1 %defines], [$2], [$3], [$4], [$5], [$6], [$7])
138 m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc]],
140 AT_TEST([%skeleton "]b4_skel["
141 %define api.value.type {double}],
143 ['1' '2' { printf ("%2.1f\n", $1 + $2); }],
146 AT_VAL = (res - '0') / 10.0],
149 # A typedef which looks like a Bison keyword, but it's using braces.
150 AT_TEST([%skeleton "]b4_skel["
151 %define api.value.type {variant}],
152 [%code requires { typedef double variant; }],
153 ['1' '2' { printf ("%2.1f\n", $1 + $2); }],
156 AT_VAL = (res - '0') / 10.0],
159 # A user defined struct.
160 AT_TEST([%skeleton "]b4_skel["
161 %define api.value.type {struct foo}],
162 [%code requires { struct foo { float fval; int ival; }; }],
164 { printf ("%d %2.1f\n", $1.ival + $2.ival, $1.fval + $2.fval); }],
168 AT_VAL.ival = (res - '0') * 10;
169 AT_VAL.fval = (float) (res - '0') / 10.f;
173 # A user defined struct that uses pointers.
174 AT_TEST([%skeleton "]b4_skel["
175 %define api.value.type {struct bar}],
187 %token <up->ival> '1' '2'
188 %printer { ]AT_CXX_IF([[yyo << $$]],
189 [[fprintf (yyo, "%d", $$)]])[; } <up->ival>
193 printf ("%d %d\n", $1, $<up->ival>2);
200 AT_VAL.up = (struct u *) malloc (sizeof *AT_VAL.up);
202 AT_VAL.up->ival = res - '0';
207 # A user defined union.
208 AT_TEST([%skeleton "]b4_skel["
209 %define api.value.type {union foo}],
210 [%code requires { union foo { float fval; int ival; }; }],
211 ['1' '2' { printf ("%d %2.1f\n", $1.ival, $2.fval); }],
219 # A %union and a named %union. In C++ named %union is an error.
220 m4_foreach([b4_union],
221 [m4_bmatch(b4_skel, [\.cc$],
223 [[%union], [%union foo],
224 [%define api.value.union.name foo; %union]])],
225 [AT_TEST([%skeleton "]b4_skel["
226 ]b4_union[ { float fval; int ival; };],
229 ['1' '2' { printf ("%d %2.1f\n", $1, $2); }],
237 # A Bison-defined union.
238 # The tokens names are not available directly in C++, we use their
239 # user number to keep it simple between C and C++.
240 AT_TEST([%skeleton "]b4_skel["
241 %define api.value.type union],
242 [%token <int> ONE 101;
243 %token <float> TWO 102 THREE 103;
244 %printer { ]AT_CXX_IF([[yyo << $$]],
245 [[fprintf (yyo, "%d", $$)]])[; } <int>
246 %printer { ]AT_CXX_IF([[yyo << $$]],
247 [[fprintf (yyo, "%f", $$)]])[; } <float>
249 [ONE TWO THREE { printf ("%d %2.1f %2.1f\n", $1, $2, $3); }],
250 [{ 101, 102, 103, EOF }],
256 AT_VAL.THREE = 3.3f],
259 # A Bison-defined variant, for lalr1.cc only.
260 m4_if(b4_skel, [lalr1.cc], [
261 AT_TEST([%skeleton "]b4_skel["
262 %define api.value.type variant],
264 %token <std::string> '2';],
265 ['1' '2' { std::cout << $1 << ", " << $2 << '\n'; }],
270 AT_VAL.build<std::string> ("two");],
273 # Test a regression where we passed user types (we can include
274 # commas) to a CPP macro.
275 AT_TEST([%skeleton "]b4_skel["
276 %define api.value.type variant],
277 [%token <std::pair<int, int>> '1';
278 %token <std::pair<std::string, std::string>> '2';],
281 std::cout << $1.first << ':' << $1.second << ", "
282 << $2.first << ':' << $2.second << '\n';
286 AT_VAL.build (std::make_pair (10, 11));
288 AT_VAL.build (std::pair<std::string, std::string> ("two", "deux"));],
291 # Type-based token constructors on move-only types, and types with commas.
292 AT_TEST([%skeleton "]b4_skel["
293 %define api.value.type variant
294 %define api.token.constructor],
295 [[%token <std::pair<int, int>> '1' '2';]],
298 std::cout << $1.first << ':' << $1.second << ", "
299 << $2.first << ':' << $2.second << '\n';
302 [[typedef yy::parser::symbol_type symbol;
304 return symbol (res, std::make_pair (res - '0', res - '0' + 1));
306 return symbol (res)]],
309 # Move-only types, and variadic emplace.
310 AT_TEST([%skeleton "]b4_skel["
311 %code requires { #include <memory> }
312 %define api.value.type variant],
313 [[%token <std::unique_ptr<int>> '1';
314 %token <std::pair<int, int>> '2';]],
315 ['1' '2' { std::cout << *$1 << ", "
316 << $2.first << ':' << $2.second << '\n'; }],
319 ]AT_VAL[.emplace <std::unique_ptr<int>>
320 (std::make_unique <int> (10));
322 ]AT_VAL[.emplace <std::pair<int, int>> (21, 22);]],
324 [AT_REQUIRE_CXX_STD(14, [echo "$at_std not supported"; continue])])
326 # Token constructors on move-only types, and types with commas.
327 AT_TEST([%skeleton "]b4_skel["
328 %code requires { #include <memory> }
329 %define api.value.type variant
330 %define api.token.constructor],
331 [[%token <std::unique_ptr<int>> ONE;
332 %token <std::pair<int, int>> TWO;
334 [ONE TWO { std::cout << *$1 << ", "
335 << $2.first << ':' << $2.second << '\n'; }],
338 return yy::parser::make_ONE (std::make_unique<int> (10));
340 return yy::parser::make_TWO (std::make_pair (21, 22));
342 return yy::parser::make_EOI ()]],
344 [AT_REQUIRE_CXX_STD(14, [echo "$at_std not supported"; continue])])
346 # Type-based token constructors on move-only types, and types with commas.
347 AT_TEST([%skeleton "]b4_skel["
348 %code requires { #include <memory> }
349 %define api.value.type variant
350 %define api.token.constructor],
351 [[%token <std::unique_ptr<int>> '1';
352 %token <std::pair<int, int>> '2';]],
353 ['1' '2' { std::cout << *$1 << ", "
354 << $2.first << ':' << $2.second << '\n'; }],
357 return {res, std::make_unique<int> (10)};
359 return {res, std::make_pair (21, 22)};
363 [AT_REQUIRE_CXX_STD(14, [echo "$at_std not supported"; continue])])
369 m4_popdef([_AT_TEST])
372 ## ------------------- ##
373 ## C++: Named %union. ##
374 ## ------------------- ##
376 m4_foreach([b4_skel], [[lalr1.cc], [glr.cc]],
377 [AT_SETUP([b4_skel: Named %union])
379 [%skeleton "]b4_skel["
380 %union foo { float fval; int ival; };
384 AT_BISON_CHECK([input.y], 1, [],
385 [[input.y:2.8-10: error: named %union is invalid in C++