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.
This commit is contained in:
Akim Demaille
2012-07-05 12:33:37 +02:00
parent 2a824fae30
commit ad60e80f3d
5 changed files with 84 additions and 62 deletions

View File

@@ -230,22 +230,27 @@ m4_define([b4_declare_parser_state_variables], [b4_pure_if([[
# Declaration of yyparse (and dependencies) when using the push parser # Declaration of yyparse (and dependencies) when using the push parser
# (including in pull mode). # (including in pull mode).
m4_define([b4_declare_yyparse_push_], 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 }; 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_pull_if([b4_c_function_decl([b4_prefix[parse]], [[int]], b4_parse_param)
])b4_c_function_decl([b4_prefix[push_parse]], [[int]], ])b4_c_function_decl([b4_prefix[push_parse]], [[int]],
[[b4_prefix[pstate *yyps]], [[yyps]]]b4_pure_if([, [[b4_prefix[pstate *ps]], [[ps]]]b4_pure_if([,
[[[int yypushed_char]], [[yypushed_char]]], [[[int pushed_char]], [[pushed_char]]],
[[[YYSTYPE const *yypushed_val]], [[yypushed_val]]]b4_locations_if([, [[b4_api_PREFIX[STYPE const *pushed_val]], [[pushed_val]]]b4_locations_if([,
[[[YYLTYPE const *yypushed_loc]], [[yypushed_loc]]]])])m4_ifset([b4_parse_param], [, [[b4_api_PREFIX[LTYPE const *pushed_loc]], [[pushed_loc]]]])])m4_ifset([b4_parse_param], [,
b4_parse_param])) b4_parse_param]))
b4_pull_if([b4_c_function_decl([b4_prefix[pull_parse]], [[int]], 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_parse_param]))])
b4_c_function_decl([b4_prefix[pstate_new]], [b4_prefix[pstate *]], b4_c_function_decl([b4_prefix[pstate_new]], [b4_prefix[pstate *]],
[[[void]], []]) [[[void]], []])
b4_c_function_decl([b4_prefix[pstate_delete]], [[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_ # b4_declare_yyparse_

View File

@@ -5790,9 +5790,9 @@ function is available if either the @code{%define api.push-pull push} or
@xref{Push Decl, ,A Push Parser}. @xref{Push Decl, ,A Push Parser}.
@deftypefun int yypush_parse (yypstate *yyps) @deftypefun int yypush_parse (yypstate *yyps)
The value returned by @code{yypush_parse} is the same as for yyparse with the The value returned by @code{yypush_parse} is the same as for yyparse with
following exception. @code{yypush_parse} will return YYPUSH_MORE if more input the following exception: it returns @code{YYPUSH_MORE} if more input is
is required to finish parsing the grammar. required to finish parsing the grammar.
@end deftypefun @end deftypefun
@node Pull Parser Function @node Pull Parser Function

View File

@@ -59,7 +59,7 @@ int
]AT_NAME_PREFIX[parse (]AT_PARAM_IF([semantic_value *result, int *count]))[ ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([semantic_value *result, int *count]))[
{ {
]AT_NAME_PREFIX[::parser parser]AT_PARAM_IF([ (result, count)])[; ]AT_NAME_PREFIX[::parser parser]AT_PARAM_IF([ (result, count)])[;
#if YYDEBUG #if ]AT_API_PREFIX[DEBUG
parser.set_debug_level (1); parser.set_debug_level (1);
#endif #endif
return parser.parse (); return parser.parse ();
@@ -113,7 +113,7 @@ static int get_char (]AT_YYLEX_FORMALS[);
static void unget_char (]AT_YYLEX_PRE_FORMALS[ int c); static void unget_char (]AT_YYLEX_PRE_FORMALS[ int c);
]AT_LOCATION_IF([ ]AT_LOCATION_IF([
static YYLTYPE last_yylloc; static AT_YYLTYPE last_yylloc;
])[ ])[
static int static int
get_char (]AT_YYLEX_FORMALS[) get_char (]AT_YYLEX_FORMALS[)
@@ -264,15 +264,11 @@ AT_SKEL_CC_IF(
%code provides %code provides
{ {
#include <stdio.h> #include <stdio.h>
/* The input. */ /* The input. */
extern FILE *input; extern FILE *input;
extern semantic_value global_result; extern semantic_value global_result;
extern int global_count;]AT_SKEL_CC_IF([[ extern int global_count;
#ifndef YYLTYPE
# define YYLTYPE ]AT_NAME_PREFIX[::parser::location_type
#endif
]])[
} }
%code %code
@@ -285,7 +281,7 @@ FILE *input;
static int power (int base, int exponent); static int power (int base, int exponent);
]AT_SKEL_CC_IF(, ]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, ]) AT_PARAM_IF([semantic_value *result, int *count, ])
const char *s const char *s
);])[ );])[
@@ -373,7 +369,7 @@ power (int base, int exponent)
AT_YYERROR_DEFINE], AT_YYERROR_DEFINE],
[/* A C error reporting function. */ [/* A C error reporting function. */
static void 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, ]) AT_PARAM_IF([semantic_value *result, int *count, ])
const char *s) 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])
AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix "calc" %verbose %yacc]) 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([%debug])
AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) 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.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([%defines])
AT_CHECK_CALC_GLR([%locations]) AT_CHECK_CALC_GLR([%locations])
AT_CHECK_CALC_GLR([%name-prefix "calc"]) AT_CHECK_CALC_GLR([%name-prefix "calc"])
AT_CHECK_CALC_GLR([%define api.prefix "calc"])
AT_CHECK_CALC_GLR([%verbose]) AT_CHECK_CALC_GLR([%verbose])
AT_CHECK_CALC_GLR([%yacc]) AT_CHECK_CALC_GLR([%yacc])
AT_CHECK_CALC_GLR([%error-verbose]) 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([%debug])
AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) 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])
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 %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([])
AT_CHECK_CALC_LALR1_CC([%define location_type Span]) 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 %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([%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 %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([])
AT_CHECK_CALC_GLR_CC([%define location_type Span]) 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 %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([%debug])
AT_CHECK_CALC_GLR_CC([%error-verbose %debug %name-prefix "calc" %verbose %yacc]) 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])
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 %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}])

View File

@@ -124,15 +124,16 @@ AT_CLEANUP
AT_SETUP([Several parsers]) 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 # Generate and compile to *.o. Make sure there is no YY* nor yy* in
# the header (but YYDEBUG and YYPARSE_PARAM). # 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_BISON_OPTION_PUSHDEFS([%define api.prefix "$1_" $2])
AT_DATA_GRAMMAR([$1.AT_SKEL_CC_IF([yy], [y])], AT_DATA_GRAMMAR([$1.AT_SKEL_CC_IF([yy], [y])],
[[%define api.prefix "$1_" [[%define api.prefix "$1_"
$2 $2
%error-verbose
%union %union
{ {
int integer; int integer;
@@ -150,6 +151,8 @@ exp:
| 'x' '4' { printf ("x4\n"); } | 'x' '4' { printf ("x4\n"); }
| 'x' '5' { printf ("x5\n"); } | 'x' '5' { printf ("x5\n"); }
| 'x' '6' { printf ("x6\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_SKEL_CC_IF([],
[AT_CHECK([$EGREP yy $1.h], [1])]) [AT_CHECK([$EGREP yy $1.h], [1])])
# Ignore comments. Ignore YYPARSE_PARAM. YYDEBUG (not renamed) can be # Ignore comments. Ignore YYPARSE_PARAM (obsolete) and
# read, but not changed. # YYPUSH_MORE(_DEFINED)? (whose definition is constant).
#
# YYDEBUG (not renamed) can be read, but not changed.
AT_CHECK([[sed -ne 's,/\*[^*]*\*/,,g;s,//.*,,' \ AT_CHECK([[sed -ne 's,/\*[^*]*\*/,,g;s,//.*,,' \
-e '/YY/p' ]$1.AT_SKEL_CC_IF([hh], [h])[ | -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]) [1])
AT_LANG_COMPILE([$1.o]) AT_LANG_COMPILE([$1.o])
AT_CHECK([[echo "$1" >>expout]])
AT_BISON_OPTION_POPDEFS AT_BISON_OPTION_POPDEFS
]) ])# AT_TEST
AT_DATA([main.cc], AT_DATA([main.cc],
[AT_DATA_SOURCE_PROLOGUE [AT_DATA_SOURCE_PROLOGUE
@@ -180,47 +188,51 @@ AT_DATA([main.cc],
#include "x2.h" #include "x2.h"
#include "x3.h" #include "x3.h"
#include "x4.h" #include "x4.h"
#include "x6.h"
#include "x7.h"
#include "x8.h"
} }
#include "x5.hh" #include "x5.hh"
//#include "x6.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 int
main (void) main (void)
{ {
ECHO(x1_parse()); RUN(x1_parse());
ECHO(x2_parse()); RUN(x2_parse());
ECHO(x3_parse()); RUN(x3_parse());
ECHO(x4_parse()); RUN(x4_parse());
x5_::parser p5; x5_::parser p5;
ECHO(p5.parse()); RUN(p5.parse());
RUN(x6_parse());
RUN(x7_parse());
RUN(x8_parse());
// x6_::parser p6; // x6_::parser p6;
// ECHO(p6.parse()); // RUN(p6.parse());
return 0; return 0;
} }
]]) ]])# main.cc
AT_DATA_GRAMMAR_SEVERAL([x1], []) AT_TEST([x1], [])
AT_DATA_GRAMMAR_SEVERAL([x2], [%locations %debug]) AT_TEST([x2], [%locations %debug])
AT_DATA_GRAMMAR_SEVERAL([x3], [%glr-parser]) AT_TEST([x3], [%glr-parser])
AT_DATA_GRAMMAR_SEVERAL([x4], [%locations %debug %glr-parser]) AT_TEST([x4], [%locations %debug %glr-parser])
AT_DATA_GRAMMAR_SEVERAL([x5], [%locations %debug %language "c++"]) AT_TEST([x5], [%locations %debug %language "c++"])
#AT_DATA_GRAMMAR_SEVERAL([x5], [%locations %language "c++" %glr-parser]) 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], [x1.o x2.o x3.o x4.o x5.o main.cc]) AT_COMPILE_CXX([parser], [[x[1-8].o main.cc]])
AT_CHECK([./parser], [0], AT_CHECK([./parser], [0], [[expout]])
[[x1
x2 m4_popdef([AT_TEST])
x3
x4
x5
]],
[[x1_parse(): 0
x2_parse(): 0
x3_parse(): 0
x4_parse(): 0
p5.parse(): 0
]])
AT_CLEANUP AT_CLEANUP

View File

@@ -339,7 +339,7 @@ static
# Must be called inside a AT_BISON_OPTION_PUSHDEFS/POPDEFS pair. # Must be called inside a AT_BISON_OPTION_PUSHDEFS/POPDEFS pair.
m4_define([AT_YYERROR_FORMALS], m4_define([AT_YYERROR_FORMALS],
[m4_case(AT_LANG, [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], m4_define([AT_YYERROR_PROTOTYPE],