I am trying to set up a basic parser for some game data that use the familiar and very simple "C-style" format. Basically, naming the copied "structures" and then entering the parameters and nested "structures" inside. It will parse something like this:
Name0
{
Name1
{
Param0 *= 2
Param2 = "lol"
}
Param0 = 1
Param1 = "test"
Name2 { }
}
Name3 {
Param0 = "test"
}
, "Test {}", - , . , , , . , , , category root. , "Test {}":
Error! Expecting <sequence>"{"<node> here: ""
Parser:
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;
value %= normalstring | float_;
param %=
lexeme[+(char_ - operators)]
> operators
> value
> qi::eol;
category %=
lexeme[+(char_ - '{')]
> '{'
>> *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
);
}
};
, ++ 11 lambdas on_success on_error? on_error , , , , ( ). ? , - , , .
:
operators:
struct Operators : qi::symbols<char, Operator>
{
Operators()
{
add
("=", Operator::equal)
("+=", Operator::plusequal)
("-=", Operator::minusequal)
("*=", Operator::timesequal)
("/=", Operator::divideequal)
;
}
} operators;