Jump to content

  • Log In with Google      Sign In   
  • Create Account

boost spirit error handling


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 rozz666   Members   -  Reputation: 633

Like
0Likes
Like

Posted 15 July 2010 - 07:54 PM

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?

Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS