Files
bison/tests/types.at
Akim Demaille fc51acddb4 package: bump to 2014
* AUTHORS, ChangeLog-2012, Makefile.am, NEWS, PACKAGING, README,
* README-alpha, README-hacking, THANKS, TODO, bootstrap.conf,
* build-aux/darwin11.4.0.valgrind, build-aux/local.mk,
* build-aux/update-b4-copyright,
* build-aux/update-package-copyright-year, cfg.mk, configure.ac,
* data/README, data/bison.m4, data/c++-skel.m4, data/c++.m4,
* data/c-like.m4, data/c-skel.m4, data/c.m4, data/glr.c, data/glr.cc,
* data/java-skel.m4, data/java.m4, data/lalr1.cc, data/lalr1.java,
* data/local.mk, data/location.cc, data/stack.hh, data/variant.hh,
* data/xslt/bison.xsl, data/xslt/xml2dot.xsl, data/xslt/xml2text.xsl,
* data/xslt/xml2xhtml.xsl, data/yacc.c, djgpp/Makefile.maint,
* djgpp/README.in, djgpp/config.bat, djgpp/config.sed,
* djgpp/config.site, djgpp/config_h.sed, djgpp/djunpack.bat,
* djgpp/local.mk, djgpp/subpipe.c, djgpp/subpipe.h,
* djgpp/testsuite.sed, doc/bison.texi, doc/local.mk, doc/refcard.tex,
* etc/README, etc/bench.pl.in, etc/local.mk,
* examples/calc++/calc++.test, examples/calc++/local.mk,
* examples/extexi, examples/local.mk, examples/mfcalc/local.mk,
* examples/mfcalc/mfcalc.test, examples/rpcalc/local.mk,
* examples/rpcalc/rpcalc.test, examples/test, examples/variant.yy,
* lib/abitset.c, lib/abitset.h, lib/bbitset.h, lib/bitset.c,
* lib/bitset.h, lib/bitset_stats.c, lib/bitset_stats.h,
* lib/bitsetv-print.c, lib/bitsetv-print.h, lib/bitsetv.c,
* lib/bitsetv.h, lib/ebitset.c, lib/ebitset.h, lib/get-errno.c,
* lib/get-errno.h, lib/lbitset.c, lib/lbitset.h, lib/libiberty.h,
* lib/local.mk, lib/main.c, lib/timevar.c, lib/timevar.def,
* lib/timevar.h, lib/vbitset.c, lib/vbitset.h, lib/yyerror.c,
* m4/bison-i18n.m4, m4/c-working.m4, m4/cxx.m4, m4/flex.m4,
* m4/timevar.m4, src/AnnotationList.c, src/AnnotationList.h,
* src/InadequacyList.c, src/InadequacyList.h, src/LR0.c, src/LR0.h,
* src/Sbitset.c, src/Sbitset.h, src/assoc.c, src/assoc.h,
* src/closure.c, src/closure.h, src/complain.c, src/complain.h,
* src/conflicts.c, src/conflicts.h, src/derives.c, src/derives.h,
* src/files.c, src/files.h, src/flex-scanner.h, src/getargs.c,
* src/getargs.h, src/gram.c, src/gram.h, src/graphviz.c,
* src/graphviz.h, src/ielr.c, src/ielr.h, src/lalr.c, src/lalr.h,
* src/local.mk, src/location.c, src/location.h, src/main.c,
* src/muscle-tab.c, src/muscle-tab.h, src/named-ref.c,
* src/named-ref.h, src/nullable.c, src/nullable.h, src/output.c,
* src/output.h, src/parse-gram.c, src/parse-gram.y, src/print-xml.c,
* src/print-xml.h, src/print.c, src/print.h, src/print_graph.c,
* src/print_graph.h, src/reader.c, src/reader.h, src/reduce.c,
* src/reduce.h, src/relation.c, src/relation.h, src/scan-code.h,
* src/scan-code.l, src/scan-gram.h, src/scan-gram.l, src/scan-skel.h,
* src/scan-skel.l, src/state.c, src/state.h, src/symlist.c,
* src/symlist.h, src/symtab.c, src/symtab.h, src/system.h,
* src/tables.c, src/tables.h, src/uniqstr.c, src/uniqstr.h,
* tests/actions.at, tests/atlocal.in, tests/bison.in, tests/c++.at,
* tests/calc.at, tests/conflicts.at, tests/cxx-type.at,
* tests/existing.at, tests/glr-regression.at, tests/headers.at,
* tests/input.at, tests/java.at, tests/javapush.at, tests/local.at,
* tests/local.mk, tests/named-refs.at, tests/output.at, tests/push.at,
* tests/reduce.at, tests/regression.at, tests/sets.at,
* tests/skeletons.at, tests/synclines.at, tests/testsuite.at,
* tests/torture.at, tests/types.at:
here.
2014-02-03 15:27:02 +01:00

231 lines
6.4 KiB
Plaintext

# Value type. -*- Autotest -*-
# Copyright (C) 2013-2014 Free Software Foundation, Inc.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
AT_BANNER([[Value type tests.]])
## ----------------------------------- ##
## %union vs. %define api.value.type. ##
## ----------------------------------- ##
AT_SETUP([[%union vs. %define api.value.type]])
AT_DATA([[input.y]],
[[%union { int ival; }
%define api.value.type union-directive
%%
exp: %empty;
]])
AT_BISON_CHECK([[input.y]], [[1]], [[]],
[[input.y:2.9-22: error: '%union' and '%define api.value.type' cannot be used together
]])
AT_CLEANUP
## ---------------------------------------- ##
## %yacc vs. %define api.value.type union. ##
## ---------------------------------------- ##
AT_SETUP([[%yacc vs. %define api.value.type union]])
AT_DATA([[input.y]],
[[%yacc
%define api.value.type union
%%
exp: %empty;
]])
AT_BISON_CHECK([[input.y]], [[1]], [[]],
[[input.y:2.9-22: error: '%yacc' and '%define api.value.type "union"' cannot be used together
]])
AT_CLEANUP
## ---------------- ##
## api.value.type. ##
## ---------------- ##
# AT_TEST($1: BISON-DIRECTIVES,
# $2: MORE-BISON-DIRECTIVES,
# $3: PARSER-ACTION,
# $4: INPUT, $5: SCANNER-ACTION,
# $6: RESULT)
# --------------------------------------
# Compile the grammar and check the expected result.
# BISON-DIRECTIVES are passed to AT_SETUP, contrary to MORE-BISON-DIRECTIVES.
m4_pushdef([AT_TEST],
[
AT_SETUP([$1])
AT_KEYWORDS([api.value.type])
AT_BISON_OPTION_PUSHDEFS([%debug $1 $2])
AT_DATA_GRAMMAR([test.y],
[[%debug
%code
{
# include <stdio.h>
# include <stdlib.h>
]AT_YYERROR_DECLARE[
]AT_YYLEX_DECLARE[
}
]$1[
]$2[
%%
start: $3;
%%
]AT_YYERROR_DEFINE[
]AT_YYLEX_DEFINE([$4], [$5])[
]AT_MAIN_DEFINE[
]])
AT_FULL_COMPILE([[test]])
AT_PARSER_CHECK([./test], 0, [$6
], [stderr])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP
])
m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc]],
[# A built-in type.
AT_TEST([%skeleton "]b4_skel["
%define api.value.type {double}],
[],
['1' '2' { printf ("%2.1f\n", $1 + $2); }],
["12"],
[AT_VAL = (res - '0') / 10.0],
[0.3])
# A typedef which looks like a Bison keyword, but it's using braces.
AT_TEST([%skeleton "]b4_skel["
%define api.value.type {variant}],
[%code requires { typedef double variant; }],
['1' '2' { printf ("%2.1f\n", $1 + $2); }],
["12"],
[AT_VAL = (res - '0') / 10.0],
[0.3])
# A user defined struct.
AT_TEST([%skeleton "]b4_skel["
%define api.value.type {struct foo}],
[%code requires { struct foo { float fval; int ival; }; }],
['1' '2'
{ printf ("%d %2.1f\n", $1.ival + $2.ival, $1.fval + $2.fval); }],
["12"],
[AT_VAL.ival = (res - '0') * 10;
AT_VAL.fval = (res - '0') / 10.f],
[30 0.3])
# A user defined struct that uses pointers.
AT_TEST([%skeleton "]b4_skel["
%define api.value.type {struct bar}],
[%code requires
{
struct u
{
int ival;
};
struct bar
{
struct u *up;
};
}
%token <up->ival> '1' '2'
%printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
[[fprintf (yyo, "%d", $$)]])[; } <up->ival>
],
['1' '2'
{
printf ("%d %d\n", $1, $<up->ival>2);
free ($<up>1);
free ($<up>2);
}],
["12"],
[AT_VAL.up = (struct u *) malloc (sizeof *AT_VAL.up);
assert (AT_VAL.up);
AT_VAL.up->ival = res - '0';],
[1 2])
# A user defined union.
AT_TEST([%skeleton "]b4_skel["
%define api.value.type {union foo}],
[%code requires { union foo { float fval; int ival; }; }],
['1' '2' { printf ("%d %2.1f\n", $1.ival, $2.fval); }],
["12"],
[if (res == '1')
AT_VAL.ival = 10;
else
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 Bison-defined union.
# The tokens names are not available directly in C++, we use their
# user number to keep it simple between C and C++.
AT_TEST([%skeleton "]b4_skel["
%define api.value.type union],
[%token <int> ONE 101;
%token <float> TWO 102 THREE 103;
%printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
[[fprintf (yyo, "%d", $$)]])[; } <int>
%printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
[[fprintf (yyo, "%f", $$)]])[; } <float>
],
[ONE TWO THREE { printf ("%d %2.1f %2.1f\n", $1, $2, $3); }],
[{ 101, 102, 103, EOF }],
[if (res == 101)
AT_VAL.ONE = 10;
else if (res == 102)
AT_VAL.TWO = .2f;
else if (res == 103)
AT_VAL.THREE = 3.3f],
[10 0.2 3.3])
# A Bison-defined variant, for lalr1.cc only.
m4_if(b4_skel, [lalr1.cc], [
AT_TEST([%skeleton "]b4_skel["
%define api.value.type variant],
[%token <int> '1';
%token <std::string> '2';],
['1' '2' { std::cout << $1 << ", " << $2 << std::endl; }],
["12"],
[if (res == '1')
AT_VAL.build(10);
else
AT_VAL.build<std::string>("two");],
[10, two])])
])
m4_popdef([AT_TEST])