0

What is considered good form for adding position annotations for rules that return sequences?

Assuming I have a rule in my grammar of the form:

const auto array_def = '[' >> *int_ >> ']';

that synthesizes an attribute of type std::vector< int >, I thought about adding position annotation via an encapsulation of the vector sequence into a made-up type:

struct array_t { std::vector< int > value; };

and add position annotation as per the X3 tutorials:

struct array_t : x3::position_tagged { std::vector< int > value; };

, etc. Unfortunately, this does not work as explained in Boost.Spirit X3 parser "no type named type in(...)" where I see that collapsing single element sequences has been rejected in Spirit.

What's left is perhaps to go the route of:

struct array_t : x3::position_tagged, std::vector< int > { };

which would work (actually, works) but it contradicts decades of good practice that says don't inherit std types. What is good X3 form here?

Engineerist
  • 367
  • 2
  • 13
  • 1) If you want your type to be treated as a container - implement container API for it. 2) Using `BOOST_FUSION_ADAPT_STRUCT` on a type makes the type a Fusion **sequence** (check with [`is_sequence` trait](https://www.boost.org/doc/libs/1_69_0/libs/fusion/doc/html/fusion/support/is_sequence.html)) and has an implication for Spirit. 3) There is nothing wrong with inheriting `std::vector` and your case is a perfect example of a situation when it is justified. – Nikita Kniazev Jan 04 '19 at 16:21
  • Awesome, thanks! If you create an answer I will mark it answered. – Engineerist Jan 04 '19 at 18:59

0 Answers0