c++boostboost-spiritboost-spirit-qiboost-spirit-lex

boost spirit: how to match any lexer token in qi?


I would like to match a C++ function declaration with default argument values, but ignoring these values. For example:

int myFunction(int a, int b = 5 + 4);

Here is (a part of) the lexer:

struct Lexer : boost::spirit::lex::lexer<lexer_type>
{
    Lexer()
    {
        identifier = "[A-Za-z_][A-Za-z0-9_]*";
        numLiteral = "([0-9]+)|(0x[0-9a-fA-F]+)";

        this->self.add
            ("int")
            ('+')
            ('=')
            ('(')
            (')')
            (';')
            (',')
            (identifier)
            (numLiteral);
    }
};

I would like to write a couple of parser rules like:

function = qi::lit("int") >> lexer.identifier >> '(' >> arglist >> ')' >> ';';
arglist = (lexer.numLiteral >> lexer.identifier >> -(lit('=') >> rvalue )) % ',';
rvalue = +(qi::token() - ',' - ')');

I've seen here that "The parser primitives qi::token and qi::tokenid can now be used without any argument. In this case they will match any token.". Which is what I want (and what I wrote), but unfortunately it does not compile. qi::token() really needs at leat one argument. Did I miss something?


Solution

  • Ok, since this was apparently enough to answer it:

    Which is what I want (and what I wrote), but unfortunately it does not compile. qi::token() really needs at leat one argument. Did I miss something?

    Probably you didn't miss enough: did you leave the ()? Because in the EDSL it's convention to drop the parentheses for the parameter-less version (cf. qi::string vs. qi::string("value"))