5

What is the logic under using boost::spirit::x3::position_tagged as base class for some AST nodes (how to choose which should be tagged, e.g. for C-like language?) and other constructions, used in rule ID definition, like:

struct error_handler_tag;

struct error_handler_base
{

    template< typename Iterator, typename Exception, typename Context >
    x3::error_handler_result
    on_error(Iterator & /*first*/, Iterator const & /*last*/,
             Exception const & x, Context const & context)
    {
        std::string message_ = "Error! Expecting: " + x.which() + " here:";
        auto & error_handler = x3::get< error_handler_tag >(context).get();
        error_handler(x.where(), message_);
        return x3::error_handler_result::fail;
    }

};

struct annotation_base
{

    template< typename T, typename Iterator, typename Context >
    void
    on_success(Iterator const & first, Iterator const & last,
               T & ast, Context const & context)
    {
        auto & error_handler = x3::get< error_handler_tag >(context).get();
        error_handler.tag(ast, first, last);
    }

};
// ...
error_handler_type error_handler(beg, end, std::cerr);
auto const parser_ = x3::with< error_handler_tag >(std::ref(error_handler))[grammar];
// ...

?

In case of erroneous input (grammar not match) this part of code does nothing (even in case of simpliest grammar, which should recognize an identifiers) - not printing an error mesage.

Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
  • I've a similiar problem with my own code which is based on [rexpr tutorial](https://github.com/boostorg/spirit/blob/master/example/x3/rexpr/rexpr_full) and [x3_fun](https://github.com/cierelabs/x3_fun) but I am not entirely sure where the failure is coming from, i.e. if it's my own error. Did you check with the mailing list? I did not compile both tutorial projects and thus did not check if they're correct. – Christoph Oct 05 '15 at 17:21
  • @Christoph Can't do it instantly but in near future I can. – Tomilov Anatoliy Oct 05 '15 at 17:34
  • Awesome :) The `on_success` methods defined in `x3::annotate_on_success` [annotate_on_success.cpp](https://github.com/boostorg/spirit/blob/master/include/boost/spirit/home/x3/support/utility/annotate_on_success.hpp) and inherited in `struct rexpr_class : x3::annotate_on_success, error_handler_base {};` [rexpr_def.hpp](https://github.com/boostorg/spirit/blob/master/example/x3/rexpr/rexpr_full/rexpr/rexpr_def.hpp) are called successfully but the `on_error` handler defined as in the user's – Christoph Oct 05 '15 at 18:11
  • [error_handler.hpp](https://github.com/boostorg/spirit/blob/master/example/x3/rexpr/rexpr_full/rexpr/error_handler.hpp) are not (at least in my project), even if I move all it's declarations and definitions directly into my equivalent of `rexpr_class`. Tomorrow or the day after tomorrow I can try to check for occurrences of `on_error` and `on_success` for now I could not find them in the `x3` directories, only for `on_error` there is one with a differing signature. – Christoph Oct 05 '15 at 18:12
  • 1
    Short update: The `on_error` handler defined in [regexpr_full](https://github.com/boostorg/spirit/blob/master/example/x3/rexpr/rexpr_full/rexpr/error_handler.hpp) is called correctly. – Christoph Oct 06 '15 at 13:20

1 Answers1

-1

A gramatically correct parse does not mean the AST can be succesfully evaluated as well. Think of a grammar defining some evaluator that can evaluate math expressions like "3+4/(5-5)". It will parse just fine, but during the AST evaluation you may want to raise an error on the "5-5" as an argument to a division. For that, it is handy to have the position of the element you are complaining about.

André
  • 570
  • 3
  • 7