
2013/1/27 Szymon Gatner <szymon.gatner@gmail.com>
Hi,
I am trying to develop a JSON parser using spirit/qi so far it is not going bad but I am having serious problem with on_error and error handler. This is my code:
namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; namespace phoenix = boost::phoenix;
template <typename Iterator> struct error_handler { template <typename, typename, typename, typename> struct result { typedef void type; };
void operator()(Iterator first, Iterator last, Iterator err_pos, const boost::spirit::info& info) const { std::cout << "never called"; } };
template <class Iterator> struct JsonGrammar : public qi::grammar<Iterator, Value(), ascii::space_type> { JsonGrammar() : JsonGrammar::base_type(json, "json"), error(error_handler<Iterator>()) { using qi::double_; using qi::lexeme; using qi::char_; using qi::int_; using qi::on_error; using qi::fail; using phoenix::construct; using phoenix::val; using namespace qi::labels;
qi::on_error<qi::fail>(json, error(_1, _2, _3, _4));
string %= lexeme['"' >> +(char_ - '"') >> '"']; number = int_; member = string >> ':' >> value; array = '[' >> value % ',' >> ']'; object = '{' >> member >> *(',' >> member) >> '}'; value = array | object | number | string | qi::bool_ | "null"; json = array | object;
json.name("json");
}
phoenix::function<error_handler<Iterator> > const error; qi::rule<Iterator, std::string(), ascii::space_type> string; qi::rule<Iterator, int(), ascii::space_type> number; qi::rule<Iterator, Value(), ascii::space_type> json; qi::rule<Iterator, Array(), ascii::space_type> array; qi::rule<Iterator, Value(), ascii::space_type> value; qi::rule<Iterator, Object(), ascii::space_type> object; qi::rule<Iterator, std::pair<std::string, Value>(), ascii::space_type> member; };
now when i try to parse
std::string json = "[1, 2, 3, 4, t]";
parising result is false. but error handelr is never called. What am I doing wrong?
You're not using expectation point (i.e. ">"), so no error would be triggered.