1 # Value type. -*- Autotest -*-
3 # Copyright (C) 2013 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"
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 ## ---------------- ##
42 ## ---------------- ##
44 # AT_TEST($1: BISON-DIRECTIVES,
45 # $2: MORE-BISON-DIRECTIVES,
47 # $4: INPUT, $5: SCANNER-ACTION,
49 # --------------------------------------
50 # Compile the grammar and check the expected result.
51 # BISON-DIRECTIVES are passed to AT_SETUP, contrary to MORE-BISON-DIRECTIVES.
55 AT_KEYWORDS([api.value.type])
56 AT_BISON_OPTION_PUSHDEFS([%debug $1 $2])
57 AT_DATA_GRAMMAR([test.y],
77 ]AT_YYLEX_DEFINE([$4], [$5])[
81 AT_FULL_COMPILE([[test]])
82 AT_PARSER_CHECK([./test], 0, [$6
84 AT_BISON_OPTION_POPDEFS
88 m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc]],
90 AT_TEST([%skeleton "]b4_skel["
91 %define api.value.type double],
93 ['1' '2' { printf ("%2.1f\n", $1 + $2); }],
95 [AT_VAL = (res - '0') / 10.0],
98 # A user defined struct.
99 AT_TEST([%skeleton "]b4_skel["
100 %define api.value.type "struct foo"],
101 [%code requires { struct foo { float fval; int ival; }; }],
103 { printf ("%d %2.1f\n", $1.ival + $2.ival, $1.fval + $2.fval); }],
105 [AT_VAL.ival = (res - '0') * 10;
106 AT_VAL.fval = (res - '0') / 10.f],
109 # A user defined struct that uses pointers.
110 AT_TEST([%skeleton "]b4_skel["
111 %define api.value.type "struct bar"],
123 %token <up->ival> '1' '2'
124 %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
125 [[fprintf (yyo, "%d", $$)]])[; } <up->ival>
129 printf ("%d %d\n", $1, $<up->ival>2);
134 [AT_VAL.up = (struct u *) malloc (sizeof *AT_VAL.up);
136 AT_VAL.up->ival = res - '0';],
139 # A user defined union.
140 AT_TEST([%skeleton "]b4_skel["
141 %define api.value.type "union foo"],
142 [%code requires { union foo { float fval; int ival; }; }],
143 ['1' '2' { printf ("%d %2.1f\n", $1.ival, $2.fval); }],
152 AT_TEST([%skeleton "]b4_skel["
153 %union { float fval; int ival; };],
156 ['1' '2' { printf ("%d %2.1f\n", $1, $2); }],
164 # A Bison-defined union.
165 # The tokens names are not available directly in C++, we use their
166 # user number to keep it simple between C and C++.
167 AT_TEST([%skeleton "]b4_skel["
168 %define api.value.type union],
169 [%token <int> ONE 101;
170 %token <float> TWO 102 THREE 103;
171 %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
172 [[fprintf (yyo, "%d", $$)]])[; } <int>
173 %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
174 [[fprintf (yyo, "%f", $$)]])[; } <float>
176 [ONE TWO THREE { printf ("%d %2.1f %2.1f\n", $1, $2, $3); }],
177 [{ 101, 102, 103, EOF }],
183 AT_VAL.THREE = 3.3f],
186 # A Bison-defined variant, for lalr1.cc only.
187 m4_if(b4_skel, [lalr1.cc], [
188 AT_TEST([%skeleton "]b4_skel["
189 %define api.value.type variant],
191 %token <std::string> '2';],
192 ['1' '2' { std::cout << $1 << ", " << $2 << std::endl; }],
197 AT_VAL.build<std::string>("two");],