1 #ifndef JSON_SPIRIT_READER_TEMPLATE
2 #define JSON_SPIRIT_READER_TEMPLATE
9 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
18 #include <boost/bind.hpp>
19 #include <boost/function.hpp>
20 #include <boost/version.hpp>
22 #if BOOST_VERSION >= 103800
23 #include <boost/spirit/include/classic_core.hpp>
24 #include <boost/spirit/include/classic_confix.hpp>
25 #include <boost/spirit/include/classic_escape_char.hpp>
26 #include <boost/spirit/include/classic_multi_pass.hpp>
27 #include <boost/spirit/include/classic_position_iterator.hpp>
28 #define spirit_namespace boost::spirit::classic
30 #include <boost/spirit/core.hpp>
31 #include <boost/spirit/utility/confix.hpp>
32 #include <boost/spirit/utility/escape_char.hpp>
33 #include <boost/spirit/iterator/multi_pass.hpp>
34 #include <boost/spirit/iterator/position_iterator.hpp>
35 #define spirit_namespace boost::spirit
40 const spirit_namespace::int_parser < boost::int64_t >
int64_p = spirit_namespace::int_parser < boost::int64_t >();
41 const spirit_namespace::uint_parser< boost::uint64_t >
uint64_p = spirit_namespace::uint_parser< boost::uint64_t >();
43 template<
class Iter_type >
44 bool is_eq( Iter_type first, Iter_type last,
const char* c_str )
46 for( Iter_type i = first; i != last; ++i, ++c_str )
48 if( *c_str == 0 )
return false;
50 if( *i != *c_str )
return false;
56 template<
class Char_type >
59 if( ( c >=
'0' ) && ( c <=
'9' ) )
return c -
'0';
60 if( ( c >=
'a' ) && ( c <=
'f' ) )
return c -
'a' + 10;
61 if( ( c >=
'A' ) && ( c <=
'F' ) )
return c -
'A' + 10;
65 template<
class Char_type,
class Iter_type >
68 const Char_type c1( *( ++begin ) );
69 const Char_type c2( *( ++begin ) );
74 template<
class Char_type,
class Iter_type >
77 const Char_type c1( *( ++begin ) );
78 const Char_type c2( *( ++begin ) );
79 const Char_type c3( *( ++begin ) );
80 const Char_type c4( *( ++begin ) );
88 template<
class String_type >
90 typename String_type::const_iterator& begin,
91 typename String_type::const_iterator end )
93 typedef typename String_type::value_type Char_type;
95 const Char_type c2( *begin );
99 case 't': s +=
'\t';
break;
100 case 'b': s +=
'\b';
break;
101 case 'f': s +=
'\f';
break;
102 case 'n': s +=
'\n';
break;
103 case 'r': s +=
'\r';
break;
104 case '\\': s +=
'\\';
break;
105 case '/': s +=
'/';
break;
106 case '"': s +=
'"';
break;
109 if( end - begin >= 3 )
111 s += hex_str_to_char< Char_type >( begin );
117 if( end - begin >= 5 )
119 s += unicode_str_to_char< Char_type >( begin );
126 template<
class String_type >
128 typename String_type::const_iterator end )
130 typedef typename String_type::const_iterator Iter_type;
132 if( end - begin < 2 )
return String_type( begin, end );
136 result.reserve( end - begin );
138 const Iter_type end_minus_1( end - 1 );
140 Iter_type substr_start = begin;
143 for( ; i < end_minus_1; ++i )
147 result.append( substr_start, i );
153 substr_start = i + 1;
157 result.append( substr_start, end );
162 template<
class String_type >
163 String_type
get_str_(
typename String_type::const_iterator begin,
164 typename String_type::const_iterator end )
168 typedef typename String_type::const_iterator Iter_type;
170 if ( ( *begin ==
'"' ) && ( *(end-1) ==
'"' ) )
176 Iter_type str_without_quotes( begin );
177 Iter_type end_without_quotes( end );
179 return substitute_esc_chars< String_type >( str_without_quotes, end_without_quotes );
182 inline std::string
get_str( std::string::const_iterator begin, std::string::const_iterator end )
184 return get_str_< std::string >( begin, end );
187 inline std::wstring
get_str( std::wstring::const_iterator begin, std::wstring::const_iterator end )
189 return get_str_< std::wstring >( begin, end );
192 template<
class String_type,
class Iter_type >
193 String_type
get_str( Iter_type begin, Iter_type end )
195 const String_type tmp( begin, end );
197 return get_str( tmp.begin(), tmp.end() );
205 template<
class Value_type,
class Iter_type >
226 begin_compound< Object_type >();
240 begin_compound< Array_type >();
254 name_ = get_str< String_type >( begin, end );
257 void new_str( Iter_type begin, Iter_type end )
264 assert(
is_eq( begin, end,
"true" ) );
271 assert(
is_eq( begin, end,
"false" ) );
278 assert(
is_eq( begin, end,
"null" ) );
312 template<
class Array_or_obj >
323 Array_or_obj new_array_or_obj;
365 template<
typename Iter_type >
366 void throw_error( spirit_namespace::position_iterator< Iter_type > i,
const std::string& reason )
368 throw Error_position( i.get_position().line, i.get_position().column, reason );
371 template<
typename Iter_type >
379 template<
class Value_type,
class Iter_type >
380 class Json_grammer :
public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >
421 template<
typename ScannerT >
430 typedef typename Value_type::String_type::value_type Char_type;
435 typedef boost::function< void( Char_type ) > Char_action;
436 typedef boost::function< void( Iter_type, Iter_type ) > Str_action;
437 typedef boost::function< void( double ) > Real_action;
438 typedef boost::function< void( boost::int64_t ) > Int_action;
439 typedef boost::function< void( boost::uint64_t ) > Uint64_action;
465 | str_p(
"true" ) [ new_true ]
466 | str_p(
"false" )[ new_false ]
467 | str_p(
"null" ) [ new_null ]
471 = ch_p(
'{')[ begin_obj ]
477 =
pair_ >> *( ( ch_p(
',') | *space_p ) >>
pair_ )
487 = ch_p(
'[')[ begin_array ]
510 >> *( alnum_p | ch_p(
'_') )
514 = strict_real_p[ new_real ]
522 const spirit_namespace::rule< ScannerT >&
start()
const {
return json_; }
532 template<
class Iter_type,
class Value_type >
535 typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;
537 const Posn_iter_t posn_begin( begin, end );
538 const Posn_iter_t posn_end( end, end );
543 template<
class Istream_type >
548 typedef spirit_namespace::multi_pass< istream_iter >
Mp_iter;
552 is.unsetf( std::ios::skipws );
554 begin_ = spirit_namespace::make_multi_pass(
istream_iter( is ) );
555 end_ = spirit_namespace::make_multi_pass(
istream_iter() );
570 template<
class Iter_type,
class Value_type >
575 const spirit_namespace::parse_info< Iter_type > info =
576 spirit_namespace::parse( begin, end,
578 spirit_namespace::space_p );
597 template<
class Iter_type,
class Value_type >
616 template<
class String_type,
class Value_type >
619 typename String_type::const_iterator begin = s.begin();
628 template<
class String_type,
class Value_type >
638 template<
class Istream_type,
class Value_type >
650 template<
class Istream_type,
class Value_type >
Semantic_actions< Value_type, Iter_type > Semantic_actions_t
const spirit_namespace::rule< ScannerT > & start() const
Json_grammer & operator=(const Json_grammer &)
void begin_array(Char_type c)
const spirit_namespace::uint_parser< boost::uint64_t > uint64_p
std::istream_iterator< Char_type, Char_type > istream_iter
Char_type unicode_str_to_char(Iter_type &begin)
Config_type::Object_type Object_type
Multi_pass_iters(Istream_type &is)
bool read_stream(Istream_type &is, Value_type &value)
Config_type::Array_type Array_type
void new_int(boost::int64_t i)
Json_grammer(Semantic_actions_t &semantic_actions)
void throw_error(spirit_namespace::position_iterator< Iter_type > i, const std::string &reason)
const spirit_namespace::int_parser< boost::int64_t > int64_p
void end_obj(Char_type c)
Config_type::String_type String_type
String_type get_str_(typename String_type::const_iterator begin, typename String_type::const_iterator end)
Istream_type::char_type Char_type
spirit_namespace::rule< ScannerT > json_
bool read_string(const String_type &s, Value_type &value)
void new_name(Iter_type begin, Iter_type end)
Value_type * add_to_current(const Value_type &value)
definition(const Json_grammer &self)
static void throw_not_array(Iter_type begin, Iter_type end)
spirit_namespace::rule< ScannerT > pair_
void end_array(Char_type c)
spirit_namespace::rule< ScannerT > members_
Value_type::Config_type Config_type
spirit_namespace::rule< ScannerT > key_name_
Value_type * add_first(const Value_type &value)
Semantic_actions_t & actions_
void new_false(Iter_type begin, Iter_type end)
String_type::value_type Char_type
spirit_namespace::rule< ScannerT > number_
spirit_namespace::multi_pass< istream_iter > Mp_iter
void begin_obj(Char_type c)
void new_true(Iter_type begin, Iter_type end)
bool is_eq(Iter_type first, Iter_type last, const char *c_str)
Semantic_actions & operator=(const Semantic_actions &)
static void throw_not_object(Iter_type begin, Iter_type end)
std::vector< Value_type * > stack_
void new_str(Iter_type begin, Iter_type end)
spirit_namespace::rule< ScannerT > elements_
void new_uint64(boost::uint64_t ui)
spirit_namespace::rule< ScannerT > string_
Char_type hex_to_num(const Char_type c)
spirit_namespace::rule< ScannerT > value_
void append_esc_char_and_incr_iter(String_type &s, typename String_type::const_iterator &begin, typename String_type::const_iterator end)
spirit_namespace::rule< ScannerT > object_
static void throw_not_pair(Iter_type begin, Iter_type end)
String_type substitute_esc_chars(typename String_type::const_iterator begin, typename String_type::const_iterator end)
Semantic_actions(Value_type &value)
void read_stream_or_throw(Istream_type &is, Value_type &value)
static void throw_not_string(Iter_type begin, Iter_type end)
void read_string_or_throw(const String_type &s, Value_type &value)
spirit_namespace::rule< ScannerT > array_
void new_null(Iter_type begin, Iter_type end)
Char_type hex_str_to_char(Iter_type &begin)
static void throw_not_value(Iter_type begin, Iter_type end)
std::string get_str(std::string::const_iterator begin, std::string::const_iterator end)
Iter_type read_range_or_throw(Iter_type begin, Iter_type end, Value_type &value)
bool read_range(Iter_type &begin, Iter_type end, Value_type &value)
static void throw_not_colon(Iter_type begin, Iter_type end)
void add_posn_iter_and_read_range_or_throw(Iter_type begin, Iter_type end, Value_type &value)