1

I'm trying to figure out the way of parsing the following texts

function() {body ...}
function(args_) {body...}

Should i use the same struct for both variants or it could be done with only one struct

struct function
{
    std::string name_;
    std::vector<std::string> args_;
    statement_list body_;
};

The way i parse it for now (how to skip arguments if there is no arguments):

auto const identifier_def = raw[lexeme[(alpha | '_') >> *(alnum | '_')]];
auto const function_def  =
        lexeme["function" >> !(alnum | '_')] >> identifier
     >> '(' >> ((identifier % ',') )>> ')'
     >> '{' >> statement  >> '}'
        ;

And i could parse the variant with arguments, but not the one with no arguments!

I'm trying to use something like OR operator, but there was no success.

Thanks!

Nikita Kniazev
  • 3,728
  • 2
  • 16
  • 30

1 Answers1

2

As a quick hint, usually this works:

 >> '(' >> -(identifier % ',') >> ')'

Depending on the specific types (especially the declaration of identifier) you might have a tweak like this:

 >> '(' >> (-identifier % ',') >> ')'

An idea for forcing it:

 x3::rule<struct arglist_, std::vector<std::string> > { "arglist" }
         = '(' >> (
                       identifier % ','
                     | x3::attr(std::vector<std::string> ())
                   )
         >> ')';
sehe
  • 374,641
  • 47
  • 450
  • 633