[spirit/qi] Error handler not called
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
2013/1/27 Szymon Gatner
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
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
{ 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_errorqi::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
const error; qi::rule string; qi::rule number; qi::rule json; qi::rule array; qi::rule value; qi::rule object; qi::rule (), 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.
2013/1/27 TONGARI
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.
Ah, understand now. I thought expectation points were additional error handling mechanism. So without expectation points all error handing must be done "manually" based on position of first iterator after parsing right? I could not find any error handing information in the docs. Am I missing something? Thanks! Simon
On 1/27/13 9:31 PM, Szymon Gatner wrote:
2013/1/27 TONGARI
: 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.
Ah, understand now. I thought expectation points were additional error handling mechanism. So without expectation points all error handing must be done "manually" based on position of first iterator after parsing right?
I could not find any error handing information in the docs. Am I missing something?
See: http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/tutorial... Here's another article you can read: http://boost-spirit.com/home/2011/02/28/dispatching-on-expectation-point-fai... Regards, -- Joel de Guzman http://www.boostpro.com http://boost-spirit.com
See: http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/tutorial...
Here's another article you can read: http://boost-spirit.com/home/2011/02/28/dispatching-on-expectation-point-fai...
I actually read them both but didn't understand that expectation points were necessary to make on_error<> working. I have another problem though: With code sample I posted, "first" iterator is never moved while I would like to to point around "t" array element. Is there a way to get meaningful error information without expectation points? Regards, Simon
participants (3)
-
Joel de Guzman
-
Szymon Gatner
-
TONGARI