]> git.saurik.com Git - bison.git/blob - tests/types.at
api.value.type: use keyword/brace values
[bison.git] / tests / types.at
1 # Value type. -*- Autotest -*-
2
3 # Copyright (C) 2013 Free Software Foundation, Inc.
4
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.
9 #
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.
14 #
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/>.
17
18 AT_BANNER([[Value type tests.]])
19
20
21 ## ----------------------------------- ##
22 ## %union vs. %define api.value.type. ##
23 ## ----------------------------------- ##
24
25 AT_SETUP([[%union vs. %define api.value.type]])
26
27 AT_DATA([[input.y]],
28 [[%union { int ival; }
29 %define api.value.type union-directive
30 %%
31 exp: %empty;
32 ]])
33
34 AT_BISON_CHECK([[input.y]], [[1]], [[]],
35 [[input.y:2.9-22: error: '%union' and '%define api.value.type' cannot be used together
36 ]])
37
38 AT_CLEANUP
39
40 ## ---------------------------------------- ##
41 ## %yacc vs. %define api.value.type union. ##
42 ## ---------------------------------------- ##
43
44 AT_SETUP([[%yacc vs. %define api.value.type union]])
45
46 AT_DATA([[input.y]],
47 [[%yacc
48 %define api.value.type union
49 %%
50 exp: %empty;
51 ]])
52
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
55 ]])
56
57 AT_CLEANUP
58
59
60 ## ---------------- ##
61 ## api.value.type. ##
62 ## ---------------- ##
63
64 # AT_TEST($1: BISON-DIRECTIVES,
65 # $2: MORE-BISON-DIRECTIVES,
66 # $3: PARSER-ACTION,
67 # $4: INPUT, $5: SCANNER-ACTION,
68 # $6: RESULT)
69 # --------------------------------------
70 # Compile the grammar and check the expected result.
71 # BISON-DIRECTIVES are passed to AT_SETUP, contrary to MORE-BISON-DIRECTIVES.
72 m4_pushdef([AT_TEST],
73 [
74 AT_SETUP([$1])
75 AT_KEYWORDS([api.value.type])
76 AT_BISON_OPTION_PUSHDEFS([%debug $1 $2])
77 AT_DATA_GRAMMAR([test.y],
78 [[%debug
79
80 %code
81 {
82 # include <stdio.h>
83 # include <stdlib.h>
84 ]AT_YYERROR_DECLARE[
85 ]AT_YYLEX_DECLARE[
86 }
87
88 ]$1[
89 ]$2[
90
91 %%
92
93 start: $3;
94
95 %%
96 ]AT_YYERROR_DEFINE[
97 ]AT_YYLEX_DEFINE([$4], [$5])[
98 ]AT_MAIN_DEFINE[
99 ]])
100
101 AT_FULL_COMPILE([[test]])
102 AT_PARSER_CHECK([./test], 0, [$6
103 ], [stderr])
104 AT_BISON_OPTION_POPDEFS
105 AT_CLEANUP
106 ])
107
108 m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc]],
109 [# A built-in type.
110 AT_TEST([%skeleton "]b4_skel["
111 %define api.value.type {double}],
112 [],
113 ['1' '2' { printf ("%2.1f\n", $1 + $2); }],
114 ["12"],
115 [AT_VAL = (res - '0') / 10.0],
116 [0.3])
117
118 # A typedef which looks like a Bison keyword, but it's using braces.
119 AT_TEST([%skeleton "]b4_skel["
120 %define api.value.type {variant}],
121 [%code requires { typedef double variant; }],
122 ['1' '2' { printf ("%2.1f\n", $1 + $2); }],
123 ["12"],
124 [AT_VAL = (res - '0') / 10.0],
125 [0.3])
126
127 # A user defined struct.
128 AT_TEST([%skeleton "]b4_skel["
129 %define api.value.type {struct foo}],
130 [%code requires { struct foo { float fval; int ival; }; }],
131 ['1' '2'
132 { printf ("%d %2.1f\n", $1.ival + $2.ival, $1.fval + $2.fval); }],
133 ["12"],
134 [AT_VAL.ival = (res - '0') * 10;
135 AT_VAL.fval = (res - '0') / 10.f],
136 [30 0.3])
137
138 # A user defined struct that uses pointers.
139 AT_TEST([%skeleton "]b4_skel["
140 %define api.value.type {struct bar}],
141 [%code requires
142 {
143 struct u
144 {
145 int ival;
146 };
147 struct bar
148 {
149 struct u *up;
150 };
151 }
152 %token <up->ival> '1' '2'
153 %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
154 [[fprintf (yyo, "%d", $$)]])[; } <up->ival>
155 ],
156 ['1' '2'
157 {
158 printf ("%d %d\n", $1, $<up->ival>2);
159 free ($<up>1);
160 free ($<up>2);
161 }],
162 ["12"],
163 [AT_VAL.up = (struct u *) malloc (sizeof *AT_VAL.up);
164 assert (AT_VAL.up);
165 AT_VAL.up->ival = res - '0';],
166 [1 2])
167
168 # A user defined union.
169 AT_TEST([%skeleton "]b4_skel["
170 %define api.value.type {union foo}],
171 [%code requires { union foo { float fval; int ival; }; }],
172 ['1' '2' { printf ("%d %2.1f\n", $1.ival, $2.fval); }],
173 ["12"],
174 [if (res == '1')
175 AT_VAL.ival = 10;
176 else
177 AT_VAL.fval = .2f],
178 [10 0.2])
179
180 # A %union.
181 AT_TEST([%skeleton "]b4_skel["
182 %union { float fval; int ival; };],
183 [%token <ival> '1';
184 %token <fval> '2';],
185 ['1' '2' { printf ("%d %2.1f\n", $1, $2); }],
186 ["12"],
187 [if (res == '1')
188 AT_VAL.ival = 10;
189 else
190 AT_VAL.fval = 0.2f],
191 [10 0.2])
192
193 # A Bison-defined union.
194 # The tokens names are not available directly in C++, we use their
195 # user number to keep it simple between C and C++.
196 AT_TEST([%skeleton "]b4_skel["
197 %define api.value.type union],
198 [%token <int> ONE 101;
199 %token <float> TWO 102 THREE 103;
200 %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
201 [[fprintf (yyo, "%d", $$)]])[; } <int>
202 %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
203 [[fprintf (yyo, "%f", $$)]])[; } <float>
204 ],
205 [ONE TWO THREE { printf ("%d %2.1f %2.1f\n", $1, $2, $3); }],
206 [{ 101, 102, 103, EOF }],
207 [if (res == 101)
208 AT_VAL.ONE = 10;
209 else if (res == 102)
210 AT_VAL.TWO = .2f;
211 else if (res == 103)
212 AT_VAL.THREE = 3.3f],
213 [10 0.2 3.3])
214
215 # A Bison-defined variant, for lalr1.cc only.
216 m4_if(b4_skel, [lalr1.cc], [
217 AT_TEST([%skeleton "]b4_skel["
218 %define api.value.type variant],
219 [%token <int> '1';
220 %token <std::string> '2';],
221 ['1' '2' { std::cout << $1 << ", " << $2 << std::endl; }],
222 ["12"],
223 [if (res == '1')
224 AT_VAL.build(10);
225 else
226 AT_VAL.build<std::string>("two");],
227 [10, two])])
228 ])
229
230 m4_popdef([AT_TEST])