1 # Value type. -*- Autotest -*-
3 # Copyright (C) 2013-2015 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.9-22: 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.9-22: 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,
70 # --------------------------------------
71 # Compile the grammar and check the expected result.
72 # BISON-DIRECTIVES are passed to AT_SETUP, contrary to MORE-BISON-DIRECTIVES.
73 m4_pushdef([_AT_TEST],
76 AT_KEYWORDS([api.value.type])
77 AT_BISON_OPTION_PUSHDEFS([%debug $1 $2])
78 AT_DATA_GRAMMAR([test.y],
98 ]AT_YYLEX_DEFINE([$4], [$5])[
102 AT_FULL_COMPILE([[test]])
103 AT_PARSER_CHECK([./test], 0, [$6
105 AT_BISON_OPTION_POPDEFS
109 # AT_TEST($1: BISON-DIRECTIVES,
110 # $2: MORE-BISON-DIRECTIVES,
113 # $5: SCANNER-ACTION,
115 # --------------------------------------
116 # Check with and without %defines, to avoid regressions. It turns out
117 # that in that case yacc.c calls the set-up of the %union twice,
118 # because YYSTYPE is defined once in the header, and once in the
119 # implementation file (eventually it'd be better to include the header
120 # file, but that's another story). Unfortunately running these macros
121 # a second time doubled the side-effects and resulted in a double
122 # definition of the union members.
123 m4_pushdef([AT_TEST],
124 [_AT_TEST([$1], [$2], [$3], [$4], [$5], [$6])
125 _AT_TEST([$1 %defines], [$2], [$3], [$4], [$5], [$6])
128 m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc]],
130 AT_TEST([%skeleton "]b4_skel["
131 %define api.value.type {double}],
133 ['1' '2' { printf ("%2.1f\n", $1 + $2); }],
135 [AT_VAL = (res - '0') / 10.0],
138 # A typedef which looks like a Bison keyword, but it's using braces.
139 AT_TEST([%skeleton "]b4_skel["
140 %define api.value.type {variant}],
141 [%code requires { typedef double variant; }],
142 ['1' '2' { printf ("%2.1f\n", $1 + $2); }],
144 [AT_VAL = (res - '0') / 10.0],
147 # A user defined struct.
148 AT_TEST([%skeleton "]b4_skel["
149 %define api.value.type {struct foo}],
150 [%code requires { struct foo { float fval; int ival; }; }],
152 { printf ("%d %2.1f\n", $1.ival + $2.ival, $1.fval + $2.fval); }],
154 [AT_VAL.ival = (res - '0') * 10;
155 AT_VAL.fval = (res - '0') / 10.f],
158 # A user defined struct that uses pointers.
159 AT_TEST([%skeleton "]b4_skel["
160 %define api.value.type {struct bar}],
172 %token <up->ival> '1' '2'
173 %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
174 [[fprintf (yyo, "%d", $$)]])[; } <up->ival>
178 printf ("%d %d\n", $1, $<up->ival>2);
183 [AT_VAL.up = (struct u *) malloc (sizeof *AT_VAL.up);
185 AT_VAL.up->ival = res - '0';],
188 # A user defined union.
189 AT_TEST([%skeleton "]b4_skel["
190 %define api.value.type {union foo}],
191 [%code requires { union foo { float fval; int ival; }; }],
192 ['1' '2' { printf ("%d %2.1f\n", $1.ival, $2.fval); }],
200 # A %union and a named %union. In C++ named %union is an error.
201 m4_foreach([b4_union],
202 [m4_bmatch(b4_skel, [\.cc$],
204 [[%union], [%union foo],
205 [%define api.value.union.name foo; %union]])],
206 [AT_TEST([%skeleton "]b4_skel["
207 ]b4_union[ { float fval; int ival; };],
210 ['1' '2' { printf ("%d %2.1f\n", $1, $2); }],
218 # A Bison-defined union.
219 # The tokens names are not available directly in C++, we use their
220 # user number to keep it simple between C and C++.
221 AT_TEST([%skeleton "]b4_skel["
222 %define api.value.type union],
223 [%token <int> ONE 101;
224 %token <float> TWO 102 THREE 103;
225 %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
226 [[fprintf (yyo, "%d", $$)]])[; } <int>
227 %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
228 [[fprintf (yyo, "%f", $$)]])[; } <float>
230 [ONE TWO THREE { printf ("%d %2.1f %2.1f\n", $1, $2, $3); }],
231 [{ 101, 102, 103, EOF }],
237 AT_VAL.THREE = 3.3f],
240 # A Bison-defined variant, for lalr1.cc only.
241 m4_if(b4_skel, [lalr1.cc], [
242 AT_TEST([%skeleton "]b4_skel["
243 %define api.value.type variant],
245 %token <std::string> '2';],
246 ['1' '2' { std::cout << $1 << ", " << $2 << std::endl; }],
251 AT_VAL.build<std::string>("two");],
256 m4_popdef([_AT_TEST])
259 ## ------------------- ##
260 ## C++: Named %union. ##
261 ## ------------------- ##
263 m4_foreach([b4_skel], [[lalr1.cc], [glr.cc]],
264 [AT_SETUP([b4_skel: Named %union])
266 [%skeleton "]b4_skel["
267 %union foo { float fval; int ival; };
271 AT_BISON_CHECK([input.y], 1, [],
272 [[input.y:2.8-10: error: named %union is invalid in C++