Jump to content
  • Advertisement
Sign in to follow this  
rozz666

boost spirit error handling

This topic is 2870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to provide some error handling to my parser. As in the example here, I use qi::on_error. However, the error handling function is never called:

namespace detail
{

struct errorMessageImpl
{
template <typename Logger, typename String, typename It>
struct result { typedef void type; };

template <typename Logger, typename String, typename It>
void operator()(Logger *logger, const String& filename, It it) const
{
logger->log(error::Message::missingMainFunction(Position(filename, it.get_position().line, it.get_position().column)));
}
};

boost::phoenix::function<errorMessageImpl> errorMessage;

}

template <typename Iterator>
struct Grammar : qi::grammar<Iterator, cst::Function(), ascii::space_type>
{
Grammar(const std::string& filename, error::Logger& errorLogger)
: Grammar::base_type(function, "function")
{
identifier %= inputPos >> qi::lexeme[qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z0-9_")];
returnType %= qi::lit("void");
function %= identifier > '(' > ')' > "->" > returnType > '{' > '}' > qi::eoi;

qi::on_error<qi::fail>
(
returnType,
detail::errorMessage(&errorLogger, filename, qi::labels::_3)
);
}

qi::rule<Iterator, cst::Identifier(), ascii::space_type> identifier;
qi::rule<Iterator, void(), ascii::space_type> returnType;
qi::rule<Iterator, cst::Function(), ascii::space_type> function;
};


boost::optional<Function> parseFile(InputStream& is, error::Logger& logger)
{
typedef std::vector<char> Source;
Source source;

source.assign(std::istreambuf_iterator<char>(is.stream()), std::istreambuf_iterator<char>());

typedef boost::spirit::classic::position_iterator<Source::const_iterator, boost::spirit::classic::file_position> PosIterator;

using boost::spirit::ascii::space;
PosIterator iter(source.begin(), source.end(), is.file());
PosIterator end;

typedef rask::cst::Grammar<PosIterator> Grammar;
Grammar grammar(is.file(), logger);
cst::Function function;

bool r = boost::spirit::qi::phrase_parse(iter, end, grammar, space, function);

if (r && iter == end)
{
return function;
}
else
{
logger.log(error::Message::missingMainFunction(Position(is.file())));

return boost::none;
}
}



Even if I replace returnType with function, the error handler is still not called.
Has anyone had a similar problem and know a solution?

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!