// ---------------------------------------------------------------------------- // Copyright (C) 2002-2005 Marcin Kalicinski // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // For more information, see www.boost.org // ---------------------------------------------------------------------------- #ifndef BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_READ_HPP_INCLUDED #define BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_READ_HPP_INCLUDED //#define BOOST_SPIRIT_DEBUG #include #include #include #include #include #include #include #include #include namespace boost { namespace property_tree { namespace json_parser { /////////////////////////////////////////////////////////////////////// // Json parser context template struct context { typedef typename Ptree::char_type Ch; typedef std::basic_string Str; typedef typename std::vector::iterator It; Str string; Str name; Ptree root; std::vector stack; struct a_object_s { context &c; a_object_s(context &c): c(c) { } void operator()(Ch) const { if (c.stack.empty()) c.stack.push_back(&c.root); else { Ptree *parent = c.stack.back(); Ptree *child = &parent->push_back(std::make_pair(c.name, Ptree()))->second; c.stack.push_back(child); c.name.clear(); } } }; struct a_object_e { context &c; a_object_e(context &c): c(c) { } void operator()(Ch) const { BOOST_ASSERT(c.stack.size() >= 1); c.stack.pop_back(); } }; struct a_name { context &c; a_name(context &c): c(c) { } void operator()(It, It) const { c.name.swap(c.string); c.string.clear(); } }; struct a_string_val { context &c; a_string_val(context &c): c(c) { } void operator()(It, It) const { BOOST_ASSERT(c.stack.size() >= 1); c.stack.back()->push_back(std::make_pair(c.name, Ptree(c.string))); c.name.clear(); c.string.clear(); } }; struct a_literal_val { context &c; a_literal_val(context &c): c(c) { } void operator()(It b, It e) const { BOOST_ASSERT(c.stack.size() >= 1); c.stack.back()->push_back(std::make_pair(c.name, Str(b, e))); c.name.clear(); c.string.clear(); } }; struct a_char { context &c; a_char(context &c): c(c) { } void operator()(It b, It e) const { c.string += *b; } }; struct a_escape { context &c; a_escape(context &c): c(c) { } void operator()(Ch ch) const { switch (ch) { case Ch('\"'): c.string += Ch('\"'); break; case Ch('\\'): c.string += Ch('\\'); break; case Ch('0'): c.string += Ch('\0'); break; case Ch('b'): c.string += Ch('\b'); break; case Ch('f'): c.string += Ch('\f'); break; case Ch('n'): c.string += Ch('\n'); break; case Ch('r'): c.string += Ch('\r'); break; case Ch('t'): c.string += Ch('\t'); break; default: BOOST_ASSERT(0); } } }; struct a_unicode { context &c; a_unicode(context &c): c(c) { } void operator()(unsigned long u) const { u = (std::min)(u, static_cast((std::numeric_limits::max)())); c.string += Ch(u); } }; }; /////////////////////////////////////////////////////////////////////// // Json grammar template struct json_grammar: public boost::spirit::grammar > { typedef context Context; typedef typename Ptree::char_type Ch; mutable Context c; template struct definition { boost::spirit::rule root, object, member, array, item, value, string, number; boost::spirit::rule::type> character, escape; definition(const json_grammar &self) { using namespace boost::spirit; // Assertions assertion expect_object("expected object"); assertion expect_eoi("expected end of input"); assertion expect_objclose("expected ',' or '}'"); assertion expect_arrclose("expected ',' or ']'"); assertion expect_name("expected object name"); assertion expect_colon("expected ':'"); assertion expect_value("expected value"); assertion expect_escape("invalid escape sequence"); // JSON grammar rules root = expect_object(object) >> expect_eoi(end_p) ; object = ch_p('{')[typename Context::a_object_s(self.c)] >> (ch_p('}')[typename Context::a_object_e(self.c)] | (list_p(member, ch_p(',')) >> expect_objclose(ch_p('}')[typename Context::a_object_e(self.c)]) ) ) ; member = expect_name(string[typename Context::a_name(self.c)]) >> expect_colon(ch_p(':')) >> expect_value(value) ; array = ch_p('[')[typename Context::a_object_s(self.c)] >> (ch_p(']')[typename Context::a_object_e(self.c)] | (list_p(item, ch_p(',')) >> expect_arrclose(ch_p(']')[typename Context::a_object_e(self.c)]) ) ) ; item = expect_value(value) ; value = string[typename Context::a_string_val(self.c)] | (number | str_p("true") | "false" | "null")[typename Context::a_literal_val(self.c)] | object | array ; number = strict_real_p | int_p ; string = +(lexeme_d[confix_p('\"', *character, '\"')]) ; character = (anychar_p - "\\" - "\"")[typename Context::a_char(self.c)] | ch_p("\\") >> expect_escape(escape) ; escape = chset_p(detail::widen("\"\\0bfnrt").c_str())[typename Context::a_escape(self.c)] | 'u' >> uint_parser()[typename Context::a_unicode(self.c)] ; // Debug BOOST_SPIRIT_DEBUG_RULE(root); BOOST_SPIRIT_DEBUG_RULE(object); BOOST_SPIRIT_DEBUG_RULE(member); BOOST_SPIRIT_DEBUG_RULE(array); BOOST_SPIRIT_DEBUG_RULE(item); BOOST_SPIRIT_DEBUG_RULE(value); BOOST_SPIRIT_DEBUG_RULE(string); BOOST_SPIRIT_DEBUG_RULE(number); BOOST_SPIRIT_DEBUG_RULE(escape); BOOST_SPIRIT_DEBUG_RULE(character); } const boost::spirit::rule &start() const { return root; } }; }; template unsigned long count_lines(It begin, It end) { return static_cast(std::count(begin, end, Ch('\n')) + 1); } template void read_json_internal(std::basic_istream &stream, Ptree &pt, const std::string &filename) { using namespace boost::spirit; typedef typename Ptree::char_type Ch; typedef typename std::vector::iterator It; // Load data into vector std::vector v(std::istreambuf_iterator(stream.rdbuf()), std::istreambuf_iterator()); if (!stream.good()) throw json_parser_error("read error", filename, 0); // Prepare grammar json_grammar g; // Parse try { parse_info pi = parse(v.begin(), v.end(), g, space_p | comment_p("//") | comment_p("/*", "*/")); if (!pi.hit || !pi.full) throw parser_error(v.begin(), "syntax error"); } catch (parser_error &e) { throw json_parser_error(e.descriptor, filename, count_lines(v.begin(), e.where)); } // Swap grammar context root and pt pt.swap(g.c.root); } } } } #endif