I'm trying to setup a basic parser for some game data that uses a familiar and very simple "C-style" format. Basically, naming braced "structs" then putting parameters and nested "structs" inside. It would parse something like this:
Name0
{
Name1
{
Param0 *= 2
Param2 = "lol"
}
Param0 = 1
Param1 = "test"
Name2 { }
}
Name3 {
Param0 = "test"
}
However, it is failing on even the simple input test of "Test {}", much less getting to something as advanced as my above example. The structs are setup to use fusion, and that seems straight-forward enough that I doubt it is the issue. I'm not using a couple rules currently, and most of my rules are untested because it fails when it tries the first category
rule in root
. This is the error I'm getting when I input "Test {}":
Error! Expecting <sequence>"{"<node> here: ""
Here's the Parser class:
template<typename Iterator>
struct Parser : qi::grammar<Iterator, std::vector<Category>(), ascii::space_type>
{
qi::rule<Iterator, std::vector<Category>(), ascii::space_type> root;
qi::rule<Iterator, Category(), ascii::space_type> category;
qi::rule<Iterator, Param(), ascii::space_type> param;
qi::rule<Iterator, Node(), ascii::space_type> node;
qi::rule<Iterator, Value(), ascii::space_type> value;
qi::rule<Iterator, char()> escape;
qi::rule<Iterator, std::string()> quotedstring;
qi::rule<Iterator, std::string()> normalstring;
qi::rule<Iterator> comment;
qi::rule<Iterator> commentblock;
Parser() : Parser::base_type(root, "root")
{
using namespace qi;
using ascii::char_;
using phoenix::construct;
using phoenix::val;
escape %= '\\' > char_("\\\"");
quotedstring %= '"' >> *((char_ - '"') | escape) > '"';
normalstring %= *(char_ - qi::eol);
comment = "//" >> *(char_ - qi::eol);
commentblock = "/*" >> *(char_ - "*/") > "*/";
node %= category | param; //comment? comment block? holding off for now
value %= normalstring | float_;
param %=
lexeme[+(char_ - operators)]
> operators
> value
> qi::eol;
category %=
lexeme[+(char_ - '{')] //won't this grab all whitespace around the tag too?
> '{'
>> *node
> '}';
root %= *category;
root.name("root");
category.name("category");
param.name("param");
node.name("node");
value.name("value");
escape.name("escape");
quotedstring.name("quotedstring");
normalstring.name("normalstring");
comment.name("comment");
commentblock.name("commentblock");
debug(root);
debug(category);
debug(param);
debug(node);
debug(value);
debug(escape);
debug(quotedstring);
debug(normalstring);
debug(comment);
debug(commentblock);
on_error<fail>
(
root,
std::cout
<< val("Error! Expecting ")
<< _4
<< val(" here: \"")
<< construct<std::string>(_3, _2)
<< val("\"")
<< std::endl
);
}
};
And an unrelated thing, is it possible to use C++11 lambdas in on_success
and on_error
calls? I looked into the on_error
function, and it seems to have its parameters templated to the rule type, meaning a lambda would have to be defined for every single rule type (basically every rule). Is that correct? It's too bad, those phoenix lambdas are so opaque, I have no idea how to even pull out a line number and put it in a struct.
edit:
Here's the operators
table:
struct Operators : qi::symbols<char, Operator>
{
Operators()
{
add
("=", Operator::equal)
("+=", Operator::plusequal)
("-=", Operator::minusequal)
("*=", Operator::timesequal)
("/=", Operator::divideequal)
;
}
} operators;
operators
is not given.
I guess your node
rule eats the closing }
so the catagory
rule cannot succeed.