0

A newbie for x3... The code is adapted from the roman.cpp in the x3 tutorial. Suppose I have a symbol table like below:

struct car_models_ : x3::symbols<char, unsigned>
{
    car_models_()
    {
        add
            ("sedan", 1)
            ("compact", 2)
            ("suv", 3)
        ;
    }
} car_models;

Then parse,

char const *first = "Model: sedan";
char const *last = first + std::strlen(first);
parse(first, last, "Model: " >> car_models[action()]);

If there is new model not listed in the symbol table, what would be the right way to handle it? Is there a way to add a wildcard as the last entry in the symbol table, and then somehow pass an unknown model to action (e.g., number "0" in this case)?

user180574
  • 5,681
  • 13
  • 53
  • 94

1 Answers1

0

There is no way to do it inside the symbol table itself. One possibility is:

auto ext_car_models = car_models | (x3::omit[*x3::lower] >> attr(0))

Then to parse:

parse(first, last, "Model: " >> ext_car_models[action()]);

Ignoring the attribute for a moment, your symbol table is effectively syntactic sugar for:

x3::string("sedan") | "compact" | "suv"

So, handling an unknown string in that position would need to be handled the same way. You will need to define a parser that defines what a model string looks like. Possibly *x3::lower

mhhollomon
  • 965
  • 7
  • 15