Boost Spirit x3: parse into structs

Viewed 1062

From the Boost Spirit X3 tutorial:

First, let's create a struct representing an employee:

namespace client { namespace ast
{
   struct employee
   {
       int age;
       std::string surname;
       std::string forename;
       double salary;
   };
}}

Then, we need to tell Boost.Fusion about our employee struct to make it a first-class fusion citizen that the grammar can utilize.

BOOST_FUSION_ADAPT_STRUCT(
    client::ast::employee,
    (int, age)
    (std::string, surname)
    (std::string, forename)
    (double, salary)
)`

[...] In fusion's view, a struct is just a form of a tuple. You can adapt any struct to be a fully conforming fusion tuple. [...] Applying our collapsing rules above, the RHS has an attribute of: fusion::vector<int, std::string, std::string, double> The struct employee IS compatible with fusion::vector. So, the RHS of start uses start's attribute (a struct employee) in-situ when it does its work.

If I well understood, this logic heavily relies on the order of the attributes.

Now, I am in a situation where I need to parse something like

Layer "L1" {
    number = 23
    color = green
    visible = true
}

into a struct

struct LayerInfo
{
    std::string layerName;
    int layerNumber;
    std::string color;
    bool visible;
}

The problem is, the order of the layer properties can change, which is in opposition with the logic seen above.

Which is the correct way to parse into a struct like this? Do I need necessarily need to use semantic actions?

1 Answers
Related