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 > >
387 : actions_( semantic_actions )
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;
441 Char_action
begin_obj ( boost::bind( &Semantic_actions_t::begin_obj, &
self.actions_, _1 ) );
442 Char_action
end_obj ( boost::bind( &Semantic_actions_t::end_obj, &
self.actions_, _1 ) );
443 Char_action
begin_array( boost::bind( &Semantic_actions_t::begin_array, &
self.actions_, _1 ) );
444 Char_action
end_array ( boost::bind( &Semantic_actions_t::end_array, &
self.actions_, _1 ) );
445 Str_action
new_name ( boost::bind( &Semantic_actions_t::new_name, &
self.actions_, _1, _2 ) );
446 Str_action
new_str ( boost::bind( &Semantic_actions_t::new_str, &
self.actions_, _1, _2 ) );
447 Str_action
new_true ( boost::bind( &Semantic_actions_t::new_true, &
self.actions_, _1, _2 ) );
448 Str_action
new_false ( boost::bind( &Semantic_actions_t::new_false, &
self.actions_, _1, _2 ) );
449 Str_action
new_null ( boost::bind( &Semantic_actions_t::new_null, &
self.actions_, _1, _2 ) );
450 Real_action
new_real ( boost::bind( &Semantic_actions_t::new_real, &
self.actions_, _1 ) );
451 Int_action
new_int ( boost::bind( &Semantic_actions_t::new_int, &
self.actions_, _1 ) );
452 Uint64_action
new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64, &
self.actions_, _1 ) );
457 =
value_ | eps_p[ &throw_not_value ]
473 >> ( ch_p(
'}')[
end_obj ] | eps_p[ &throw_not_object ] )
477 = pair_ >> *( ( ch_p(
',') | *space_p ) >> pair_ )
482 >> ( ch_p(
':') | ch_p(
'=') | eps_p[ &throw_not_colon ] )
483 >> (
value_ | eps_p[ &throw_not_value ] )
489 >> ( ch_p(
']')[
end_array ] | eps_p[ &throw_not_array ] )
510 >> *( alnum_p | ch_p(
'_') )
520 spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_,
value_, string_, key_name_, number_;
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
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)
static constexpr bool value
Istream_type::char_type Char_type
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)
void end_array(Char_type c)
Value_type::Config_type Config_type
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::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)
void new_uint64(boost::uint64_t ui)
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)
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)
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)