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 >
227 begin_compound< Object_type >();
243 begin_compound< Array_type >();
258 name_ = get_str< String_type >( begin, end );
261 void new_str( Iter_type begin, Iter_type end )
268 assert(
is_eq( begin, end,
"true" ) );
277 assert(
is_eq( begin, end,
"false" ) );
286 assert(
is_eq( begin, end,
"null" ) );
322 template<
class Array_or_obj >
333 Array_or_obj new_array_or_obj;
375 template<
typename Iter_type >
376 void throw_error( spirit_namespace::position_iterator< Iter_type > i,
const std::string& reason )
378 throw Error_position( i.get_position().line, i.get_position().column, reason );
381 template<
typename Iter_type >
390 template<
class Value_type,
class Iter_type >
391 class Json_grammer :
public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >
438 template<
typename ScannerT >
447 typedef typename Value_type::String_type::value_type Char_type;
452 typedef boost::function< void( Char_type ) > Char_action;
453 typedef boost::function< void( Iter_type, Iter_type ) > Str_action;
454 typedef boost::function< void(
double ) > Real_action;
455 typedef boost::function< void( boost::int64_t ) > Int_action;
456 typedef boost::function< void( boost::uint64_t ) > Uint64_action;
482 | str_p(
"true" ) [ new_true ]
483 | str_p(
"false" )[ new_false ]
484 | str_p(
"null" ) [ new_null ]
488 = ch_p(
'{')[ begin_obj ]
494 =
pair_ >> *( ( ch_p(
',') | *space_p ) >>
pair_ )
504 = ch_p(
'[')[ begin_array ]
527 >> *( alnum_p | ch_p(
'_') )
531 = strict_real_p[ new_real ]
539 const spirit_namespace::rule< ScannerT >&
start()
const {
return json_; }
549 template<
class Iter_type,
class Value_type >
552 typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;
554 const Posn_iter_t posn_begin( begin, end );
555 const Posn_iter_t posn_end( end, end );
560 template<
class Istream_type >
565 typedef spirit_namespace::multi_pass< istream_iter >
Mp_iter;
569 is.unsetf( std::ios::skipws );
571 begin_ = spirit_namespace::make_multi_pass(
istream_iter( is ) );
572 end_ = spirit_namespace::make_multi_pass(
istream_iter() );
587 template<
class Iter_type,
class Value_type >
592 const spirit_namespace::parse_info< Iter_type > info =
593 spirit_namespace::parse( begin, end,
595 spirit_namespace::space_p );
614 template<
class Iter_type,
class Value_type >
633 template<
class String_type,
class Value_type >
636 typename String_type::const_iterator begin = s.begin();
645 template<
class String_type,
class Value_type >
655 template<
class Istream_type,
class Value_type >
667 template<
class Istream_type,
class Value_type >
definition(const Json_grammer &self)
spirit_namespace::rule< ScannerT > array_
spirit_namespace::rule< ScannerT > string_
spirit_namespace::rule< ScannerT > json_
spirit_namespace::rule< ScannerT > key_name_
spirit_namespace::rule< ScannerT > elements_
spirit_namespace::rule< ScannerT > object_
spirit_namespace::rule< ScannerT > members_
spirit_namespace::rule< ScannerT > number_
const spirit_namespace::rule< ScannerT > & start() const
spirit_namespace::rule< ScannerT > pair_
spirit_namespace::rule< ScannerT > value_
static void throw_not_value(Iter_type begin, Iter_type end)
Json_grammer(Semantic_actions_t &semantic_actions)
static void throw_not_string(Iter_type begin, Iter_type end)
Semantic_actions_t & actions_
Json_grammer & operator=(const Json_grammer &)
static void throw_not_pair(Iter_type begin, Iter_type end)
static void throw_not_colon(Iter_type begin, Iter_type end)
static void throw_not_array(Iter_type begin, Iter_type end)
Semantic_actions< Value_type, Iter_type > Semantic_actions_t
static void throw_not_object(Iter_type begin, Iter_type end)
void begin_obj(Char_type c)
void new_name(Iter_type begin, Iter_type end)
void new_false(Iter_type begin, Iter_type end)
String_type::value_type Char_type
Config_type::Object_type Object_type
std::vector< Value_type * > stack_
Value_type * add_first(const Value_type &value)
void end_array(Char_type c)
Config_type::String_type String_type
void new_uint64(boost::uint64_t ui)
void new_int(boost::int64_t i)
void new_true(Iter_type begin, Iter_type end)
Value_type * add_to_current(const Value_type &value)
Semantic_actions(Value_type &value)
Semantic_actions & operator=(const Semantic_actions &)
void end_obj(Char_type c)
void new_str(Iter_type begin, Iter_type end)
Value_type::Config_type Config_type
void begin_array(Char_type c)
Config_type::Array_type Array_type
void new_null(Iter_type begin, Iter_type end)
static constexpr bool value
void append_esc_char_and_incr_iter(String_type &s, typename String_type::const_iterator &begin, typename String_type::const_iterator end)
const spirit_namespace::uint_parser< boost::uint64_t > uint64_p
String_type get_str_(typename String_type::const_iterator begin, typename String_type::const_iterator end)
Char_type unicode_str_to_char(Iter_type &begin)
void add_posn_iter_and_read_range_or_throw(Iter_type begin, Iter_type end, Value_type &value)
String_type substitute_esc_chars(typename String_type::const_iterator begin, typename String_type::const_iterator end)
Iter_type read_range_or_throw(Iter_type begin, Iter_type end, Value_type &value)
bool is_eq(Iter_type first, Iter_type last, const char *c_str)
void throw_error(spirit_namespace::position_iterator< Iter_type > i, const std::string &reason)
Char_type hex_str_to_char(Iter_type &begin)
void read_string_or_throw(const String_type &s, Value_type &value)
bool read_range(Iter_type &begin, Iter_type end, Value_type &value)
Char_type hex_to_num(const Char_type c)
bool read_stream(Istream_type &is, Value_type &value)
void read_stream_or_throw(Istream_type &is, Value_type &value)
std::string get_str(std::string::const_iterator begin, std::string::const_iterator end)
bool read_string(const String_type &s, Value_type &value)
const spirit_namespace::int_parser< boost::int64_t > int64_p
std::istream_iterator< Char_type, Char_type > istream_iter
spirit_namespace::multi_pass< istream_iter > Mp_iter
Multi_pass_iters(Istream_type &is)
Istream_type::char_type Char_type