yhirose / cpp-peglib

A single file C++ header-only PEG (Parsing Expression Grammars) library
MIT License
879 stars 112 forks source link

Need help understanding what my parser is doing :) #279

Closed lewismj closed 1 year ago

lewismj commented 1 year ago

Hi, I have a trivial parser, which is just sketched out as:

        peg::parser parser(R"(
                UCI <- Position0
                Position0 <-  Position StartPos /  Position Fen
                Position <- 'position'
                StartPos <- 'startpos'
                Fen <- < [a-zA-Z0-9] [a-zA-Z0-9-_]* >
                %whitespace <- [ \t]*
            )");

        parser["Fen"] = [&](peg::SemanticValues& vs)
        {
            // delegate to fenp.
            auto tmp = vs; // vs empty on input: position foobarbaz
        };

As you can see the Grammar is trivial, but I'm not sure why the semantic values captured by 'Fen' is empty. It is at this point that I'd expect to see the contents matched by' < [a-zA-Z0-9] [a-zA-Z0-9-_]* >' ??

yhirose commented 1 year ago

@lewismj in your situation, you have to call vs.token_to_string() to the matched string since it's a token instead of a semantic value. You can see more detail in README. Hope it helps!

lewismj commented 1 year ago

thanks