this is my first time asking a question here. It has already taken me a lot of time and research to get this working and I can't. I really hope you can help me with it. I am a newbie using Spirit, I do not fully understand all the terms; however, even after reading tons of articles and posts I do not know what am I missing.
So, I have the following struct and class in a header file.
typedef std::string::const_iterator iterator_type;
struct GrammarRules
{
qi::rule<iterator_type, ascii::space_type> create_char;
};
class Parser
{
public:
Parser();
bool parse(std::string const& to_parse);
private:
GrammarRules rules_;
gtor::World * world_;
};
Then I have the following in the .cpp file:
Parser::Parser()
: rules_()
, world_(nullptr)
{
world_ = new gtor::World();
qi::rule<iterator_type, std::string(), ascii::space_type> qg_string;
qg_string %= qi::lexeme[ +(ascii::alnum) ];
rules_.create_char =
(
qi::lit("CreateChar")
>> '('
>> qg_string >> ','
>> qg_string >> ','
>> qi::int_
>> ')'
)
[
phx::bind(>or::World::createCharacter, world_, qi::_1, qi::_2, qi::_3)
]
;
}
...
bool Parser::parse(std::string const& to_parse)
{
iterator_type it = to_parse.begin();
iterator_type end = to_parse.end();
bool success = qi::phrase_parse(it, end, rules_.create_char, ascii::space);
/*qi::rule<iterator_type, std::string(), ascii::space_type> qg_string;
qg_string %= qi::lexeme[ +(ascii::alnum) ];
qi::rule<iterator_type, ascii::space_type> create_char1 =
(
qi::lit("CreateChar")
>> '('
>> qg_string >> ','
>> qg_string >> ','
>> qi::int_
>> ')'
)
[
phx::bind(>or::World::createCharacter, world_, qi::_1, qi::_2, qi::_3)
]
;
bool success = qi::phrase_parse(it, end, create_char1, ascii::space);*/
if (success && it == end)
return true;
return false;
}
The code which is not commented on the parse()
method doesn't work, I get an Access Violation
as soon as the parser gets to the qg_string
rule. However, the code that is commented works perfectly. It looks exactly the same to me, except for the obvious differences. Maybe I am missing something very obvious, but I am unable to find it.
It already took me a lot to find that my code worked if I used everything as local variables. And still can't find the problem.
Thank you in advance for any help. Sorry if there are any mistakes in the post (5AM).