mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-10 21:03:04 +00:00
%union: fix the support for named %union
Bison supports a union tag, for obscure reasons. But it does a poor job at it, especially since Bison 3.0. Reported by Stephen Cameron and Tobias Frost. It did not ensure that the name was not given several times. An easy way to do this is to make the %union tag be handled as a %define variable, as they cannot be defined several times. Since Bison 3.0, the synclines were wrongly placed, resulting in invalid code. Addressing this issue, because of the way the union tag was stored (as a code muscle), would have been tedious. Unless we rather define the %union tag as a %percent variable, whose synclines are easier to manipulate. So replace the b4_union_name muscle by the api.value.union.name %define variable, document, and check. * data/bison.m4: Make sure that api.value.union.name has a keyword value. * data/c++.m4: Make sure that api.value.union.name is not defined. * data/c.m4 (b4_union_name): No longer use it, use api.value.union.name. * doc/bison.texi (%define Summary): Document it. * src/parse-gram.y (union_name): No longer define b4_uion_name, but api.value.union.name. * tests/input.at (Redefined %union name): New. * tests/synclines.at (%union name syncline): New. * tests/types.at: Check named %unions.
This commit is contained in:
@@ -1904,6 +1904,51 @@ m4_popdef([AT_TEST])
|
||||
AT_CLEANUP
|
||||
|
||||
|
||||
## ----------------------- ##
|
||||
## Redefined %union name. ##
|
||||
## ----------------------- ##
|
||||
|
||||
AT_SETUP([[Redefined %union name]])
|
||||
|
||||
# AT_TEST(DIRECTIVES, ERROR)
|
||||
# --------------------------
|
||||
m4_pushdef([AT_TEST],
|
||||
[AT_DATA([[input.y]],
|
||||
[$1
|
||||
%%
|
||||
exp: %empty;
|
||||
])
|
||||
|
||||
AT_BISON_CHECK([[input.y]], [[1]], [[]],
|
||||
[$2])
|
||||
])
|
||||
|
||||
AT_TEST([[%union foo {};
|
||||
%union {};
|
||||
%union foo {};
|
||||
%define api.value.union.name foo]],
|
||||
[[input.y:3.8-10: error: %define variable 'api.value.union.name' redefined
|
||||
input.y:1.8-10: previous definition
|
||||
input.y:4.9-28: error: %define variable 'api.value.union.name' redefined
|
||||
input.y:3.8-10: previous definition
|
||||
]])
|
||||
|
||||
AT_TEST([[%define api.value.union.name {foo}]],
|
||||
[[input.y:1.9-28: error: %define variable 'api.value.union.name' requires keyword values
|
||||
input.y:1.9-28: error: %define variable 'api.value.union.name' is not used
|
||||
]])
|
||||
|
||||
AT_TEST([[%define api.value.union.name "foo"]],
|
||||
[[input.y:1.9-28: error: %define variable 'api.value.union.name' requires keyword values
|
||||
input.y:1.9-28: error: %define variable 'api.value.union.name' is not used
|
||||
]])
|
||||
|
||||
m4_popdef([AT_TEST])
|
||||
AT_CLEANUP
|
||||
|
||||
|
||||
|
||||
|
||||
## -------------- ##
|
||||
## Stray $ or @. ##
|
||||
## -------------- ##
|
||||
|
||||
@@ -182,6 +182,35 @@ exp: '0';
|
||||
])
|
||||
|
||||
|
||||
## ---------------------- ##
|
||||
## %union name syncline. ##
|
||||
## ---------------------- ##
|
||||
|
||||
# Check that invalid union names are properly reported in the
|
||||
# source file.
|
||||
AT_SETUP([%union name syncline])
|
||||
AT_BISON_OPTION_PUSHDEFS
|
||||
AT_DATA([[input.y]],
|
||||
[[%union break
|
||||
{
|
||||
char dummy;
|
||||
}
|
||||
%{
|
||||
]AT_YYERROR_DECLARE_EXTERN[
|
||||
]AT_YYLEX_DECLARE_EXTERN[
|
||||
%}
|
||||
%%
|
||||
exp: '0';
|
||||
%%
|
||||
]])
|
||||
|
||||
AT_BISON_CHECK([-o input.c input.y])
|
||||
AT_SYNCLINES_COMPILE([input.c])
|
||||
AT_CHECK([[grep '^input.y:1' stdout]], 0, [ignore])
|
||||
AT_BISON_OPTION_POPDEFS
|
||||
AT_CLEANUP
|
||||
|
||||
|
||||
## ----------------------- ##
|
||||
## Postprologue syncline. ##
|
||||
## ----------------------- ##
|
||||
|
||||
@@ -197,18 +197,23 @@ m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc]],
|
||||
AT_VAL.fval = .2f],
|
||||
[10 0.2])
|
||||
|
||||
# A %union.
|
||||
AT_TEST([%skeleton "]b4_skel["
|
||||
%union { float fval; int ival; };],
|
||||
[%token <ival> '1';
|
||||
%token <fval> '2';],
|
||||
['1' '2' { printf ("%d %2.1f\n", $1, $2); }],
|
||||
["12"],
|
||||
[if (res == '1')
|
||||
AT_VAL.ival = 10;
|
||||
else
|
||||
AT_VAL.fval = 0.2f],
|
||||
[10 0.2])
|
||||
# A %union and a named %union. In C++ named %union is an error.
|
||||
m4_foreach([b4_union],
|
||||
[m4_bmatch(b4_skel, [\.cc$],
|
||||
[[%union]],
|
||||
[[%union], [%union foo],
|
||||
[%define api.value.union.name foo; %union]])],
|
||||
[AT_TEST([%skeleton "]b4_skel["
|
||||
]b4_union[ { float fval; int ival; };],
|
||||
[%token <ival> '1';
|
||||
%token <fval> '2';],
|
||||
['1' '2' { printf ("%d %2.1f\n", $1, $2); }],
|
||||
["12"],
|
||||
[if (res == '1')
|
||||
AT_VAL.ival = 10;
|
||||
else
|
||||
AT_VAL.fval = 0.2f],
|
||||
[10 0.2])])
|
||||
|
||||
# A Bison-defined union.
|
||||
# The tokens names are not available directly in C++, we use their
|
||||
@@ -249,3 +254,22 @@ m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc]],
|
||||
|
||||
m4_popdef([AT_TEST])
|
||||
m4_popdef([_AT_TEST])
|
||||
|
||||
|
||||
## ------------------- ##
|
||||
## C++: Named %union. ##
|
||||
## ------------------- ##
|
||||
|
||||
m4_foreach([b4_skel], [[lalr1.cc], [glr.cc]],
|
||||
[AT_SETUP([b4_skel: Named %union])
|
||||
AT_DATA([input.y],
|
||||
[%skeleton "]b4_skel["
|
||||
%union foo { float fval; int ival; };
|
||||
%%
|
||||
exp: %empty;
|
||||
])
|
||||
AT_BISON_CHECK([input.y], 1, [],
|
||||
[[input.y:2.8-10: error: named %union is invalid in C++
|
||||
]])
|
||||
AT_CLEANUP
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user