Search code examples
c++boost-spiritboost-spirit-x3

How to declare context type whe using multiple 'with' directives?


I have known that it is possible to get information like position_cache and error_handler at the same time by using multiple with directives refer to this doc: https://www.boost.org/doc/libs/1_75_0/libs/spirit/doc/x3/html/spirit_x3/tutorials/annotation.html

It can be configured like this when initializing the parser

with<tag1>(data1)
[
    with<tag2>(data2)[p]
]

But I'm still not sure how to manipulate the parser's context configuration when using tag1 and tag2 simultaneously.

In the boost-spirit official example. I can only find code that demonstrate how to config context type when using just single with directive. like the code below

    typedef x3::context<
        error_handler_tag
      , std::reference_wrapper<error_handler_type>
      , phrase_context_type>
    context_type;

So I want to know how to declare this context type for a parser that can with two different tag?


Solution

  • Note how context chains to the underlying context (phrase_context_type).

    You can rinse-repeat:

    using iterator_type       = std::string::const_iterator;
    using phrase_context_type = x3::phrase_parse_context<x3::ascii::space_type>::type;
    using error_handler_type  = error_handler<iterator_type>;
    
    using error_context_type =
        x3::context<error_handler_tag,
                    std::reference_wrapper<error_handler_type>,
                    phrase_context_type>;
    
    using context_type =
        x3::context<custom_with_tag,
                    CustomType,
                    error_context_type>;