Skip to content

Commit

Permalink
c++: add support for parse.error=custom
Browse files Browse the repository at this point in the history
* data/skeletons/lalr1.cc: added support here
* tests/calc.at: added test cases
* tests/local.at: added yyreport_syntax_error implementation
   for C++ test cases
  • Loading branch information
vogelsgesang committed Feb 27, 2020
1 parent 02fda68 commit 4d5db32
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
14 changes: 11 additions & 3 deletions data/skeletons/lalr1.cc
Expand Up @@ -279,6 +279,10 @@ m4_define([b4_shared_declarations],
/// Generate an error message.
/// \param yyctx the context in which the error occurred.
virtual std::string yysyntax_error_ (const context& yyctx) const;
]])b4_parse_error_bmatch([custom], [[
/// Report a syntax error
/// \param yyctx the context in which the error occurred.
void yyreport_syntax_error (const context& yyctx) const;
]])[
/// Compute post-reduction state.
/// \param yystate the current state
Expand Down Expand Up @@ -1071,11 +1075,15 @@ b4_dollar_popdef])[]dnl
{
++yynerrs_;]b4_parse_error_case(
[simple], [[
std::string msg = YY_("syntax error");]],
std::string msg = YY_("syntax error");
error (]b4_join(b4_locations_if([yyla.location]), [[YY_MOVE (msg)]])[);]],
[custom], [[
context yyctx (*this, yyla);
yyreport_syntax_error (yyctx);]],
[[
context yyctx (*this, yyla);
std::string msg = yysyntax_error_ (yyctx);]])[
error (]b4_join(b4_locations_if([yyla.location]), [[YY_MOVE (msg)]])[);
std::string msg = yysyntax_error_ (yyctx);
error (]b4_join(b4_locations_if([yyla.location]), [[YY_MOVE (msg)]])[);]])[
}

]b4_locations_if([[
Expand Down
3 changes: 3 additions & 0 deletions tests/calc.at
Expand Up @@ -1138,6 +1138,9 @@ AT_CHECK_CALC_LALR1_CC([%no-lines %defines %locations %define api.location.file
AT_CHECK_CALC_LALR1_CC([%locations %define parse.lac full %define parse.error verbose])
AT_CHECK_CALC_LALR1_CC([%locations %define parse.lac full %define parse.error detailed])

AT_CHECK_CALC_LALR1_CC([%define parse.error custom])
AT_CHECK_CALC_LALR1_CC([%define parse.error custom %locations %define api.prefix {calc} %parse-param {semantic_value *result}{int *count}{int *nerrs}])
AT_CHECK_CALC_LALR1_CC([%define parse.error custom %locations %define api.prefix {calc} %parse-param {semantic_value *result}{int *count}{int *nerrs} %define parse.lac full])

# -------------------- #
# GLR C++ Calculator. #
Expand Down
29 changes: 27 additions & 2 deletions tests/local.at
Expand Up @@ -733,10 +733,35 @@ void
++global_nerrs;
++*nerrs;]])[
std::cerr << ]AT_LOCATION_IF([l << ": " << ])[m << '\n';
}]])
}]AT_ERROR_CUSTOM_IF([[
void
]AT_NAMESPACE[::parser::yyreport_syntax_error (const context& ctx) const
{
/* Arguments of yyformat: reported tokens (one for the "unexpected",
one per "expected"). */
int arg[yyntokens_];
int n = ctx.yysyntax_error_arguments (arg, yyntokens_);]AT_PARAM_IF([m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
[[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [
YYUSE (\1);])])[]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs],[[
++global_nerrs;
++*nerrs;]])[
if (n)
{]AT_LOCATION_IF([[
std::cerr << ctx.get_location () << ": ";]])[
std::cerr << "syntax error on token [" << yysymbol_name (arg[0]) << ']';
if (1 < n)
{
std::cerr << " (expected:";
for (int i = 1; i < n; ++i)
std::cerr << " [" << yysymbol_name (arg[i]) << ']';
std::cerr << ')';
}
std::cerr << '\n';
}
}]])])


# AT_YYERROR_DEFINE(c++)([INPUT], [ACTION])
# AT_YYLEX_DEFINE(c++)([INPUT], [ACTION])
# -----------------------------------------
# Same as in C.
m4_copy([AT_YYLEX_DEFINE(c)], [AT_YYLEX_DEFINE(c++)])
Expand Down

0 comments on commit 4d5db32

Please sign in to comment.