1 #ifndef JSON_SPIRIT_READER_TEMPLATE 2 #define JSON_SPIRIT_READER_TEMPLATE 14 #include <boost/bind.hpp> 15 #include <boost/function.hpp> 16 #include <boost/version.hpp> 18 #if BOOST_VERSION >= 103800 19 #include <boost/spirit/include/classic_core.hpp> 20 #include <boost/spirit/include/classic_confix.hpp> 21 #include <boost/spirit/include/classic_escape_char.hpp> 22 #include <boost/spirit/include/classic_multi_pass.hpp> 23 #include <boost/spirit/include/classic_position_iterator.hpp> 24 #define spirit_namespace boost::spirit::classic 26 #include <boost/spirit/core.hpp> 27 #include <boost/spirit/utility/confix.hpp> 28 #include <boost/spirit/utility/escape_char.hpp> 29 #include <boost/spirit/iterator/multi_pass.hpp> 30 #include <boost/spirit/iterator/position_iterator.hpp> 31 #define spirit_namespace boost::spirit 36 const spirit_namespace::int_parser < boost::int64_t >
int64_p = spirit_namespace::int_parser < boost::int64_t >();
37 const spirit_namespace::uint_parser< boost::uint64_t >
uint64_p = spirit_namespace::uint_parser< boost::uint64_t >();
39 template<
class Iter_type >
40 bool is_eq( Iter_type first, Iter_type last,
const char* c_str )
42 for( Iter_type i = first; i != last; ++i, ++c_str )
44 if( *c_str == 0 )
return false;
46 if( *i != *c_str )
return false;
52 template<
class Char_type >
55 if( ( c >=
'0' ) && ( c <=
'9' ) )
return c -
'0';
56 if( ( c >=
'a' ) && ( c <=
'f' ) )
return c -
'a' + 10;
57 if( ( c >=
'A' ) && ( c <=
'F' ) )
return c -
'A' + 10;
61 template<
class Char_type,
class Iter_type >
64 const Char_type c1( *( ++begin ) );
65 const Char_type c2( *( ++begin ) );
70 template<
class Char_type,
class Iter_type >
73 const Char_type c1( *( ++begin ) );
74 const Char_type c2( *( ++begin ) );
75 const Char_type c3( *( ++begin ) );
76 const Char_type c4( *( ++begin ) );
84 template<
class String_type >
86 typename String_type::const_iterator& begin,
87 typename String_type::const_iterator end )
89 typedef typename String_type::value_type Char_type;
91 const Char_type c2( *begin );
95 case 't': s +=
'\t';
break;
96 case 'b': s +=
'\b';
break;
97 case 'f': s +=
'\f';
break;
98 case 'n': s +=
'\n';
break;
99 case 'r': s +=
'\r';
break;
100 case '\\': s +=
'\\';
break;
101 case '/': s +=
'/';
break;
102 case '"': s +=
'"';
break;
105 if( end - begin >= 3 )
107 s += hex_str_to_char< Char_type >( begin );
113 if( end - begin >= 5 )
115 s += unicode_str_to_char< Char_type >( begin );
122 template<
class String_type >
124 typename String_type::const_iterator end )
126 typedef typename String_type::const_iterator Iter_type;
128 if( end - begin < 2 )
return String_type( begin, end );
132 result.reserve( end - begin );
134 const Iter_type end_minus_1( end - 1 );
136 Iter_type substr_start = begin;
139 for( ; i < end_minus_1; ++i )
143 result.append( substr_start, i );
149 substr_start = i + 1;
153 result.append( substr_start, end );
158 template<
class String_type >
159 String_type
get_str_(
typename String_type::const_iterator begin,
160 typename String_type::const_iterator end )
162 assert( end - begin >= 2 );
164 typedef typename String_type::const_iterator Iter_type;
166 Iter_type str_without_quotes( ++begin );
167 Iter_type end_without_quotes( --end );
169 return substitute_esc_chars< String_type >( str_without_quotes, end_without_quotes );
172 inline std::string
get_str( std::string::const_iterator begin, std::string::const_iterator end )
174 return get_str_< std::string >( begin, end );
177 inline std::wstring
get_str( std::wstring::const_iterator begin, std::wstring::const_iterator end )
179 return get_str_< std::wstring >( begin, end );
182 template<
class String_type,
class Iter_type >
183 String_type
get_str( Iter_type begin, Iter_type end )
185 const String_type tmp( begin, end );
187 return get_str( tmp.begin(), tmp.end() );
195 template<
class Value_type,
class Iter_type >
216 begin_compound< Object_type >();
230 begin_compound< Array_type >();
244 name_ = get_str< String_type >( begin, end );
247 void new_str( Iter_type begin, Iter_type end )
302 template<
class Array_or_obj >
313 Array_or_obj new_array_or_obj;
355 template<
typename Iter_type >
356 void throw_error( spirit_namespace::position_iterator< Iter_type > i,
const std::string& reason )
358 throw Error_position( i.get_position().line, i.get_position().column, reason );
361 template<
typename Iter_type >
369 template<
class Value_type,
class Iter_type >
370 class Json_grammer :
public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >
377 : actions_( semantic_actions )
411 template<
typename ScannerT >
420 typedef typename Value_type::String_type::value_type
Char_type;
425 typedef boost::function< void( Char_type ) > Char_action;
426 typedef boost::function< void( Iter_type, Iter_type ) > Str_action;
427 typedef boost::function< void( double ) > Real_action;
428 typedef boost::function< void( boost::int64_t ) > Int_action;
429 typedef boost::function< void( boost::uint64_t ) > Uint64_action;
431 Char_action
begin_obj ( boost::bind( &Semantic_actions_t::begin_obj, &
self.actions_, _1 ) );
432 Char_action
end_obj ( boost::bind( &Semantic_actions_t::end_obj, &
self.actions_, _1 ) );
433 Char_action
begin_array( boost::bind( &Semantic_actions_t::begin_array, &
self.actions_, _1 ) );
434 Char_action
end_array ( boost::bind( &Semantic_actions_t::end_array, &
self.actions_, _1 ) );
435 Str_action
new_name ( boost::bind( &Semantic_actions_t::new_name, &
self.actions_, _1, _2 ) );
436 Str_action
new_str ( boost::bind( &Semantic_actions_t::new_str, &
self.actions_, _1, _2 ) );
437 Str_action
new_true ( boost::bind( &Semantic_actions_t::new_true, &
self.actions_, _1, _2 ) );
438 Str_action
new_false ( boost::bind( &Semantic_actions_t::new_false, &
self.actions_, _1, _2 ) );
439 Str_action
new_null ( boost::bind( &Semantic_actions_t::new_null, &
self.actions_, _1, _2 ) );
440 Real_action
new_real ( boost::bind( &Semantic_actions_t::new_real, &
self.actions_, _1 ) );
441 Int_action
new_int ( boost::bind( &Semantic_actions_t::new_int, &
self.actions_, _1 ) );
442 Uint64_action
new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64, &
self.actions_, _1 ) );
447 =
value_ | eps_p[ &throw_not_value ]
463 >> ( ch_p(
'}')[
end_obj ] | eps_p[ &throw_not_object ] )
467 = pair_ >> *(
',' >> pair_ )
472 >> (
':' | eps_p[ &throw_not_colon ] )
473 >> (
value_ | eps_p[ &throw_not_value ] )
479 >> ( ch_p(
']')[
end_array ] | eps_p[ &throw_not_array ] )
505 spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_,
value_, string_, number_;
507 const spirit_namespace::rule< ScannerT >&
start()
const {
return json_; }
517 template<
class Iter_type,
class Value_type >
522 const spirit_namespace::parse_info< Iter_type > info =
523 spirit_namespace::parse( begin, end,
525 spirit_namespace::space_p );
536 template<
class Iter_type,
class Value_type >
539 typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;
541 const Posn_iter_t posn_begin( begin, end );
542 const Posn_iter_t posn_end( end, end );
547 template<
class Iter_type,
class Value_type >
562 template<
class String_type,
class Value_type >
568 template<
class String_type,
class Value_type >
571 typename String_type::const_iterator begin = s.begin();
576 template<
class Istream_type >
581 typedef spirit_namespace::multi_pass< istream_iter >
Mp_iter;
585 is.unsetf( std::ios::skipws );
587 begin_ = spirit_namespace::make_multi_pass( istream_iter( is ) );
588 end_ = spirit_namespace::make_multi_pass( istream_iter() );
595 template<
class Istream_type,
class Value_type >
603 template<
class Istream_type,
class Value_type >
void new_str(Iter_type begin, Iter_type end)
spirit_namespace::rule< ScannerT > value_
void new_name(Iter_type begin, Iter_type end)
void end_array(Char_type c)
void begin_obj(Char_type c)
static void throw_not_colon(Iter_type begin, Iter_type end)
const spirit_namespace::uint_parser< boost::uint64_t > uint64_p
Istream_type::char_type Char_type
const spirit_namespace::rule< ScannerT > & start() const
Char_type unicode_str_to_char(Iter_type &begin)
void new_false(Iter_type begin, Iter_type end)
assert(len-trim+(2 *lenIndices)<=WIDTH)
void new_null(Iter_type begin, Iter_type end)
bool read_stream(Istream_type &is, Value_type &value)
void throw_error(spirit_namespace::position_iterator< Iter_type > i, const std::string &reason)
const spirit_namespace::int_parser< boost::int64_t > int64_p
static void throw_not_string(Iter_type begin, Iter_type end)
definition(const Json_grammer &self)
Config_type::Object_type Object_type
String_type get_str_(typename String_type::const_iterator begin, typename String_type::const_iterator end)
void new_int(boost::int64_t i)
static void throw_not_pair(Iter_type begin, Iter_type end)
static void throw_not_array(Iter_type begin, Iter_type end)
Value_type::Config_type Config_type
String_type::value_type Char_type
bool read_string(const String_type &s, Value_type &value)
static void throw_not_value(Iter_type begin, Iter_type end)
spirit_namespace::multi_pass< istream_iter > Mp_iter
Semantic_actions< Value_type, Iter_type > Semantic_actions_t
Semantic_actions_t & actions_
Semantic_actions & operator=(const Semantic_actions &)
void end_obj(Char_type c)
bool is_eq(Iter_type first, Iter_type last, const char *c_str)
std::vector< Value_type * > stack_
void begin_array(Char_type c)
void new_true(Iter_type begin, Iter_type end)
Config_type::String_type String_type
std::istream_iterator< Char_type, Char_type > istream_iter
Char_type hex_to_num(const Char_type c)
void append_esc_char_and_incr_iter(String_type &s, typename String_type::const_iterator &begin, typename String_type::const_iterator end)
Config_type::Array_type Array_type
void new_uint64(boost::uint64_t ui)
Json_grammer(Semantic_actions_t &semantic_actions)
String_type substitute_esc_chars(typename String_type::const_iterator begin, typename String_type::const_iterator end)
void read_stream_or_throw(Istream_type &is, Value_type &value)
void read_string_or_throw(const String_type &s, Value_type &value)
Value_type * add_first(const Value_type &value)
Value_type * add_to_current(const Value_type &value)
Char_type hex_str_to_char(Iter_type &begin)
std::string get_str(std::string::const_iterator begin, std::string::const_iterator end)
Semantic_actions(Value_type &value)
static void throw_not_object(Iter_type begin, Iter_type 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)
Multi_pass_iters(Istream_type &is)
void add_posn_iter_and_read_range_or_throw(Iter_type begin, Iter_type end, Value_type &value)