From ad60e80f3d13e8c4306a22a9d15863f73c576a27 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 5 Jul 2012 12:33:37 +0200 Subject: [PATCH] api.prefix: strengthen the tests and fix push-parsers. * tests/calc.at: Check api.prefix in addition to %name-prefix. * tests/headers.at: Check push parsers and pure interface. * tests/local.at: Use YYLTYPE renamed. * data/yacc.c (b4_declare_yyparse_push_): Handle api.prefix. * doc/bison.texi: Style changes. --- data/yacc.c | 19 +++++++---- doc/bison.texi | 6 ++-- tests/calc.at | 37 ++++++++++++--------- tests/headers.at | 86 +++++++++++++++++++++++++++--------------------- tests/local.at | 2 +- 5 files changed, 86 insertions(+), 64 deletions(-) diff --git a/data/yacc.c b/data/yacc.c index 2e46a1e2..5e77f844 100644 --- a/data/yacc.c +++ b/data/yacc.c @@ -230,22 +230,27 @@ m4_define([b4_declare_parser_state_variables], [b4_pure_if([[ # Declaration of yyparse (and dependencies) when using the push parser # (including in pull mode). m4_define([b4_declare_yyparse_push_], -[[typedef struct ]b4_prefix[pstate ]b4_prefix[pstate; +[[#ifndef YYPUSH_MORE_DEFINED +# define YYPUSH_MORE_DEFINED enum { YYPUSH_MORE = 4 }; +#endif + +typedef struct ]b4_prefix[pstate ]b4_prefix[pstate; + ]b4_pull_if([b4_c_function_decl([b4_prefix[parse]], [[int]], b4_parse_param) ])b4_c_function_decl([b4_prefix[push_parse]], [[int]], - [[b4_prefix[pstate *yyps]], [[yyps]]]b4_pure_if([, - [[[int yypushed_char]], [[yypushed_char]]], - [[[YYSTYPE const *yypushed_val]], [[yypushed_val]]]b4_locations_if([, - [[[YYLTYPE const *yypushed_loc]], [[yypushed_loc]]]])])m4_ifset([b4_parse_param], [, + [[b4_prefix[pstate *ps]], [[ps]]]b4_pure_if([, + [[[int pushed_char]], [[pushed_char]]], + [[b4_api_PREFIX[STYPE const *pushed_val]], [[pushed_val]]]b4_locations_if([, + [[b4_api_PREFIX[LTYPE const *pushed_loc]], [[pushed_loc]]]])])m4_ifset([b4_parse_param], [, b4_parse_param])) b4_pull_if([b4_c_function_decl([b4_prefix[pull_parse]], [[int]], - [[b4_prefix[pstate *yyps]], [[yyps]]]m4_ifset([b4_parse_param], [, + [[b4_prefix[pstate *ps]], [[ps]]]m4_ifset([b4_parse_param], [, b4_parse_param]))]) b4_c_function_decl([b4_prefix[pstate_new]], [b4_prefix[pstate *]], [[[void]], []]) b4_c_function_decl([b4_prefix[pstate_delete]], [[void]], - [[b4_prefix[pstate *yyps]], [[yyps]]])dnl + [[b4_prefix[pstate *ps]], [[ps]]])dnl ]) # b4_declare_yyparse_ diff --git a/doc/bison.texi b/doc/bison.texi index a53b52fc..15ee270e 100644 --- a/doc/bison.texi +++ b/doc/bison.texi @@ -5790,9 +5790,9 @@ function is available if either the @code{%define api.push-pull push} or @xref{Push Decl, ,A Push Parser}. @deftypefun int yypush_parse (yypstate *yyps) -The value returned by @code{yypush_parse} is the same as for yyparse with the -following exception. @code{yypush_parse} will return YYPUSH_MORE if more input -is required to finish parsing the grammar. +The value returned by @code{yypush_parse} is the same as for yyparse with +the following exception: it returns @code{YYPUSH_MORE} if more input is +required to finish parsing the grammar. @end deftypefun @node Pull Parser Function diff --git a/tests/calc.at b/tests/calc.at index 58e7e895..e4cb07f1 100644 --- a/tests/calc.at +++ b/tests/calc.at @@ -59,7 +59,7 @@ int ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([semantic_value *result, int *count]))[ { ]AT_NAME_PREFIX[::parser parser]AT_PARAM_IF([ (result, count)])[; -#if YYDEBUG +#if ]AT_API_PREFIX[DEBUG parser.set_debug_level (1); #endif return parser.parse (); @@ -113,7 +113,7 @@ static int get_char (]AT_YYLEX_FORMALS[); static void unget_char (]AT_YYLEX_PRE_FORMALS[ int c); ]AT_LOCATION_IF([ -static YYLTYPE last_yylloc; +static AT_YYLTYPE last_yylloc; ])[ static int get_char (]AT_YYLEX_FORMALS[) @@ -264,15 +264,11 @@ AT_SKEL_CC_IF( %code provides { -#include -/* The input. */ -extern FILE *input; -extern semantic_value global_result; -extern int global_count;]AT_SKEL_CC_IF([[ -#ifndef YYLTYPE -# define YYLTYPE ]AT_NAME_PREFIX[::parser::location_type -#endif -]])[ + #include + /* The input. */ + extern FILE *input; + extern semantic_value global_result; + extern int global_count; } %code @@ -285,7 +281,7 @@ FILE *input; static int power (int base, int exponent); ]AT_SKEL_CC_IF(, -[static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ]) +[static void yyerror (AT_YYERROR_ARG_LOC_IF([AT_YYLTYPE *llocp, ]) AT_PARAM_IF([semantic_value *result, int *count, ]) const char *s );])[ @@ -373,7 +369,7 @@ power (int base, int exponent) AT_YYERROR_DEFINE], [/* A C error reporting function. */ static void -yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ]) +yyerror (AT_YYERROR_ARG_LOC_IF([AT_YYLTYPE *llocp, ]) AT_PARAM_IF([semantic_value *result, int *count, ]) const char *s) { @@ -632,14 +628,16 @@ AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure %locations]) AT_CHECK_CALC_LALR([%error-verbose %locations]) AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix "calc" %verbose %yacc]) +AT_CHECK_CALC_LALR([%error-verbose %locations %defines %define api.prefix "calc" %verbose %yacc]) AT_CHECK_CALC_LALR([%debug]) AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) +AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc]) AT_CHECK_CALC_LALR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) -AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) +AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure %error-verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc]) -AT_CHECK_CALC_LALR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) +AT_CHECK_CALC_LALR([%define api.pure %error-verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) # ----------------------- # @@ -661,6 +659,7 @@ AT_CHECK_CALC_GLR() AT_CHECK_CALC_GLR([%defines]) AT_CHECK_CALC_GLR([%locations]) AT_CHECK_CALC_GLR([%name-prefix "calc"]) +AT_CHECK_CALC_GLR([%define api.prefix "calc"]) AT_CHECK_CALC_GLR([%verbose]) AT_CHECK_CALC_GLR([%yacc]) AT_CHECK_CALC_GLR([%error-verbose]) @@ -672,10 +671,12 @@ AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix "calc" %verbo AT_CHECK_CALC_GLR([%debug]) AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) +AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc]) AT_CHECK_CALC_GLR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) AT_CHECK_CALC_GLR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) +AT_CHECK_CALC_GLR([%define api.pure %error-verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) # ----------------------------- # @@ -697,11 +698,13 @@ m4_define([AT_CHECK_CALC_LALR1_CC], AT_CHECK_CALC_LALR1_CC([]) AT_CHECK_CALC_LALR1_CC([%define location_type Span]) AT_CHECK_CALC_LALR1_CC([%error-verbose %name-prefix "calc" %verbose %yacc]) +AT_CHECK_CALC_LALR1_CC([%error-verbose %define api.prefix "calc" %verbose %yacc]) AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %name-prefix "calc" %verbose %yacc]) -AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc]) +AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %define api.prefix "calc" %verbose %yacc]) AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) +AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) @@ -724,6 +727,7 @@ m4_define([AT_CHECK_CALC_GLR_CC], AT_CHECK_CALC_GLR_CC([]) AT_CHECK_CALC_GLR_CC([%define location_type Span]) AT_CHECK_CALC_GLR_CC([%error-verbose %name-prefix "calc" %verbose %yacc]) +AT_CHECK_CALC_GLR_CC([%error-verbose %define api.prefix "calc" %verbose %yacc]) AT_CHECK_CALC_GLR_CC([%debug]) AT_CHECK_CALC_GLR_CC([%error-verbose %debug %name-prefix "calc" %verbose %yacc]) @@ -731,3 +735,4 @@ AT_CHECK_CALC_GLR_CC([%error-verbose %debug %name-prefix "calc" %verbose %yacc]) AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc]) AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) +AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) diff --git a/tests/headers.at b/tests/headers.at index 70891292..07f1d1bc 100644 --- a/tests/headers.at +++ b/tests/headers.at @@ -124,15 +124,16 @@ AT_CLEANUP AT_SETUP([Several parsers]) -# AT_DATA_GRAMMAR_SEVERAL([PREFIX], [DIRECTIVES]) -# ----------------------------------------------- +# AT_TEST([PREFIX], [DIRECTIVES]) +# ------------------------------- # Generate and compile to *.o. Make sure there is no YY* nor yy* in # the header (but YYDEBUG and YYPARSE_PARAM). -m4_define([AT_DATA_GRAMMAR_SEVERAL], +m4_pushdef([AT_TEST], [AT_BISON_OPTION_PUSHDEFS([%define api.prefix "$1_" $2]) AT_DATA_GRAMMAR([$1.AT_SKEL_CC_IF([yy], [y])], [[%define api.prefix "$1_" $2 +%error-verbose %union { int integer; @@ -150,6 +151,8 @@ exp: | 'x' '4' { printf ("x4\n"); } | 'x' '5' { printf ("x5\n"); } | 'x' '6' { printf ("x6\n"); } +| 'x' '7' { printf ("x7\n"); } +| 'x' '8' { printf ("x8\n"); } ; %% @@ -162,15 +165,20 @@ AT_BISON_CHECK([-d -o AT_SKEL_CC_IF([$1.cc $1.yy], [$1.c $1.y])]) AT_SKEL_CC_IF([], [AT_CHECK([$EGREP yy $1.h], [1])]) -# Ignore comments. Ignore YYPARSE_PARAM. YYDEBUG (not renamed) can be -# read, but not changed. +# Ignore comments. Ignore YYPARSE_PARAM (obsolete) and +# YYPUSH_MORE(_DEFINED)? (whose definition is constant). +# +# YYDEBUG (not renamed) can be read, but not changed. AT_CHECK([[sed -ne 's,/\*[^*]*\*/,,g;s,//.*,,' \ -e '/YY/p' ]$1.AT_SKEL_CC_IF([hh], [h])[ | - $EGREP -wv '(YYPARSE_PARAM|defined YYDEBUG|if YYDEBUG)']], + $EGREP -wv 'YY(PARSE_PARAM|PUSH_MORE(_DEFINED)?)|(defined|if) YYDEBUG']], [1]) AT_LANG_COMPILE([$1.o]) + +AT_CHECK([[echo "$1" >>expout]]) + AT_BISON_OPTION_POPDEFS -]) +])# AT_TEST AT_DATA([main.cc], [AT_DATA_SOURCE_PROLOGUE @@ -180,47 +188,51 @@ AT_DATA([main.cc], #include "x2.h" #include "x3.h" #include "x4.h" + #include "x6.h" + #include "x7.h" + #include "x8.h" } #include "x5.hh" //#include "x6.hh" -#define ECHO(S) std::cerr << #S": " << S << std::endl; +#define RUN(S) \ + do { \ + int res = S; \ + if (res) \ + std::cerr << #S": " << res << std::endl; \ + } while (false) int main (void) { - ECHO(x1_parse()); - ECHO(x2_parse()); - ECHO(x3_parse()); - ECHO(x4_parse()); + RUN(x1_parse()); + RUN(x2_parse()); + RUN(x3_parse()); + RUN(x4_parse()); x5_::parser p5; - ECHO(p5.parse()); + RUN(p5.parse()); + RUN(x6_parse()); + RUN(x7_parse()); + RUN(x8_parse()); // x6_::parser p6; -// ECHO(p6.parse()); +// RUN(p6.parse()); return 0; } -]]) - -AT_DATA_GRAMMAR_SEVERAL([x1], []) -AT_DATA_GRAMMAR_SEVERAL([x2], [%locations %debug]) -AT_DATA_GRAMMAR_SEVERAL([x3], [%glr-parser]) -AT_DATA_GRAMMAR_SEVERAL([x4], [%locations %debug %glr-parser]) -AT_DATA_GRAMMAR_SEVERAL([x5], [%locations %debug %language "c++"]) -#AT_DATA_GRAMMAR_SEVERAL([x5], [%locations %language "c++" %glr-parser]) - -AT_COMPILE_CXX([parser], [x1.o x2.o x3.o x4.o x5.o main.cc]) -AT_CHECK([./parser], [0], -[[x1 -x2 -x3 -x4 -x5 -]], -[[x1_parse(): 0 -x2_parse(): 0 -x3_parse(): 0 -x4_parse(): 0 -p5.parse(): 0 -]]) +]])# main.cc + +AT_TEST([x1], []) +AT_TEST([x2], [%locations %debug]) +AT_TEST([x3], [%glr-parser]) +AT_TEST([x4], [%locations %debug %glr-parser]) +AT_TEST([x5], [%locations %debug %language "c++"]) +AT_TEST([x6], [%define api.pure]) +AT_TEST([x7], [%define api.push-pull both]) +AT_TEST([x8], [%define api.pure %define api.push-pull both]) +#AT_TEST([x5], [%locations %language "c++" %glr-parser]) + +AT_COMPILE_CXX([parser], [[x[1-8].o main.cc]]) +AT_CHECK([./parser], [0], [[expout]]) + +m4_popdef([AT_TEST]) AT_CLEANUP diff --git a/tests/local.at b/tests/local.at index 7a981c6d..e02bce0c 100644 --- a/tests/local.at +++ b/tests/local.at @@ -339,7 +339,7 @@ static # Must be called inside a AT_BISON_OPTION_PUSHDEFS/POPDEFS pair. m4_define([AT_YYERROR_FORMALS], [m4_case(AT_LANG, -[c], [AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])[const char *msg]])[]dnl +[c], [AT_YYERROR_ARG_LOC_IF([AT_YYLTYPE *llocp, ])[const char *msg]])[]dnl ]) m4_define([AT_YYERROR_PROTOTYPE], -- 2.45.2