Search code examples
boostboost-spiritboost-spirit-x3

Synthesize to smart pointers with Boost Spirit X3


I need to parse a complex AST, and it would be impossible to allocate this AST on heap memory, and the AST nodes must support polymorphism. One solution would be to allocate the AST nodes using smart pointers.

To simplify the question, how would I synthesize the following struct (std::unique_ptr<GiantIntegerStruct> giantIntegerStruct), with Boost Spirit X3 for example?

struct GiantIntegerStruct {
    std::vector<unique_ptr<int>> manyInts; 
}

My tentative solution, is to use semantic actions. Is there an alternative?


Solution

  • You can do semantic actions, or you can define traits for you custom types. However, see here Semantic actions runs multiple times in boost::spirit parsing (especially the two links there) - basically, consider not doing that.

    I need to parse a complex AST, and it would be impossible to allocate this AST on heap memory

    This somewhat confusing statement leads me to the logical conclusion that you merely need to allocate from a shared memory segment instead.

    In the good old spirit of Rule Of Zero you could make a value-wrapper that does the allocation using whatever method you prefer and still enjoy automatic attribute propagation with "value semantics" (which will server as mere "handles" for the actual object in shared memory).

    If you need any help getting this set up, feel free to post a new question.