1

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?

sehe
  • 374,641
  • 47
  • 450
  • 633
Alfred Li
  • 25
  • 1
  • 4
  • Found a related topic [answer](https://stackoverflow.com/a/61732124/17494985) that might be able to solve this problem – Alfred Li Oct 17 '22 at 06:48

1 Answers1

0

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>;
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you @sehe. Your answers always help me understand better how spirit works. And sorry for poor knowledge in C++ Template metaprogramming. Is this **rinse-repeat** a common concept in template class declaration? – Alfred Li Oct 18 '22 at 01:24
  • It's more of a common concept in recursive structures. Immutable Lists are commonly modeled recursively (basically like `List = tuple >` in pseudo-code). X3 contexts are like a recursive list. This idiom has been copied from pure functional programming (including Lisp's car/cdr) because template meta-programming is necessarily completely immutable. Rinse-repeat was probably not the best choice of words :) I just meant to say "you can keep doing the same" – sehe Oct 18 '22 at 02:06