]>
Commit | Line | Data |
---|---|---|
6574576c AD |
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" | |
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 | ## api.value.type. ## | |
42 | ## ---------------- ## | |
43 | ||
44 | # AT_TEST($1: BISON-DIRECTIVES, | |
45 | # $2: MORE-BISON-DIRECTIVES, | |
46 | # $3: PARSER-ACTION, | |
47 | # $4: INPUT, $5: SCANNER-ACTION, | |
48 | # $6: RESULT) | |
49 | # -------------------------------------- | |
50 | # Compile the grammar and check the expected result. | |
51 | # BISON-DIRECTIVES are passed to AT_SETUP, contrary to MORE-BISON-DIRECTIVES. | |
52 | m4_pushdef([AT_TEST], | |
53 | [ | |
54 | AT_SETUP([$1]) | |
55 | AT_KEYWORDS([api.value.type]) | |
56 | AT_BISON_OPTION_PUSHDEFS([$1 $2]) | |
57 | AT_DATA_GRAMMAR([test.y], | |
58 | [[%debug | |
59 | ||
60 | %code | |
61 | { | |
62 | # include <stdio.h> | |
63 | # include <stdlib.h> | |
64 | ]AT_YYERROR_DECLARE[ | |
65 | ]AT_YYLEX_DECLARE[ | |
66 | } | |
67 | ||
68 | ]$1[ | |
69 | ]$2[ | |
70 | ||
71 | %% | |
72 | ||
73 | start: $3; | |
74 | ||
75 | %% | |
76 | ]AT_YYERROR_DEFINE[ | |
77 | ]AT_YYLEX_DEFINE([$4], [$5])[ | |
78 | ]AT_MAIN_DEFINE[ | |
79 | ]]) | |
80 | ||
81 | AT_FULL_COMPILE([[test]]) | |
82 | AT_PARSER_CHECK([./test], 0, [$6 | |
83 | ], [stderr]) | |
84 | AT_BISON_OPTION_POPDEFS | |
85 | AT_CLEANUP | |
86 | ]) | |
87 | ||
88 | m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc]], | |
89 | [# A built-in type. | |
90 | AT_TEST([%skeleton "]b4_skel[" | |
91 | %define api.value.type double], | |
92 | [], | |
93 | ['1' '2' { printf ("%2.1f\n", $1 + $2); }], | |
94 | ["12"], | |
95 | [AT_VAL = (res - '0') / 10.0], | |
96 | [0.3]) | |
97 | ||
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; }; }], | |
102 | ['1' '2' | |
103 | { printf ("%d %2.1f\n", $1.ival + $2.ival, $1.fval + $2.fval); }], | |
104 | ["12"], | |
105 | [AT_VAL.ival = (res - '0') * 10; | |
106 | AT_VAL.fval = (res - '0') / 10.f], | |
107 | [30 0.3]) | |
108 | ||
109 | # A user defined union. | |
110 | AT_TEST([%skeleton "]b4_skel[" | |
111 | %define api.value.type "union foo"], | |
112 | [%code requires { union foo { float fval; int ival; }; }], | |
113 | ['1' '2' { printf ("%d %2.1f\n", $1.ival, $2.fval); }], | |
114 | ["12"], | |
115 | [if (res == '1') | |
116 | AT_VAL.ival = 10; | |
117 | else | |
118 | AT_VAL.fval = .2f], | |
119 | [10 0.2]) | |
120 | ||
121 | # A %union. | |
122 | AT_TEST([%skeleton "]b4_skel[" | |
123 | %union { float fval; int ival; };], | |
124 | [%token <ival> '1'; | |
125 | %token <fval> '2';], | |
126 | ['1' '2' { printf ("%d %2.1f\n", $1, $2); }], | |
127 | ["12"], | |
128 | [if (res == '1') | |
129 | AT_VAL.ival = 10; | |
130 | else | |
131 | AT_VAL.fval = 0.2f], | |
132 | [10 0.2]) | |
133 | ||
134 | # A Bison-defined union. | |
135 | # The tokens names are not available directly in C++, we use their | |
136 | # user number to keep it simple between C and C++. | |
137 | AT_TEST([%skeleton "]b4_skel[" | |
138 | %define api.value.type union], | |
139 | [%token <int> ONE 101; | |
140 | %token <float> TWO 102 THREE 103; | |
141 | %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]], | |
142 | [[fprintf (yyo, "%d", $$)]])[; } <int> | |
143 | %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]], | |
144 | [[fprintf (yyo, "%f", $$)]])[; } <float> | |
145 | ], | |
146 | [ONE TWO THREE { printf ("%d %2.1f %2.1f\n", $1, $2, $3); }], | |
147 | [{ 101, 102, 103, EOF }], | |
148 | [if (res == 101) | |
149 | AT_VAL.ONE = 10; | |
150 | else if (res == 102) | |
151 | AT_VAL.TWO = .2f; | |
152 | else if (res == 103) | |
153 | AT_VAL.THREE = 3.3f], | |
154 | [10 0.2 3.3]) | |
155 | ||
156 | # A Bison-defined variant, for lalr1.cc only. | |
157 | m4_if(b4_skel, [lalr1.cc], [ | |
158 | AT_TEST([%skeleton "]b4_skel[" | |
159 | %define api.value.type variant], | |
160 | [%token <int> '1'; | |
161 | %token <std::string> '2';], | |
162 | ['1' '2' { std::cout << $1 << ", " << $2 << std::endl; }], | |
163 | ["12"], | |
164 | [if (res == '1') | |
165 | AT_VAL.build(10); | |
166 | else | |
167 | AT_VAL.build<std::string>("two");], | |
168 | [10, two])]) | |
169 | ]) | |
170 | ||
171 | m4_popdef([AT_TEST]) |