-
+ 2030127C7941B905D7C30163B0333C87B8A517B897113CA6E1564B0FC9A0FC81EF9D973814DDFFE2A5BEA0F7C0CA0C0BDB43CC035D9587B9147BA6B73C8A6138
bitcoin/src/json/json_spirit_reader_template.h
(0 . 0)(1 . 612)
6859 #ifndef JSON_SPIRIT_READER_TEMPLATE
6860 #define JSON_SPIRIT_READER_TEMPLATE
6861
6862 // Copyright John W. Wilkinson 2007 - 2009.
6863 // Distributed under the MIT License, see accompanying file LICENSE.txt
6864
6865 // json spirit version 4.03
6866
6867 #include "json_spirit_value.h"
6868 #include "json_spirit_error_position.h"
6869
6870 //#define BOOST_SPIRIT_THREADSAFE // uncomment for multithreaded use, requires linking to boost.thread
6871
6872 #include <boost/bind.hpp>
6873 #include <boost/function.hpp>
6874 #include <boost/version.hpp>
6875
6876 #if BOOST_VERSION >= 103800
6877 #include <boost/spirit/include/classic_core.hpp>
6878 #include <boost/spirit/include/classic_confix.hpp>
6879 #include <boost/spirit/include/classic_escape_char.hpp>
6880 #include <boost/spirit/include/classic_multi_pass.hpp>
6881 #include <boost/spirit/include/classic_position_iterator.hpp>
6882 #define spirit_namespace boost::spirit::classic
6883 #else
6884 #include <boost/spirit/core.hpp>
6885 #include <boost/spirit/utility/confix.hpp>
6886 #include <boost/spirit/utility/escape_char.hpp>
6887 #include <boost/spirit/iterator/multi_pass.hpp>
6888 #include <boost/spirit/iterator/position_iterator.hpp>
6889 #define spirit_namespace boost::spirit
6890 #endif
6891
6892 namespace json_spirit
6893 {
6894 const spirit_namespace::int_parser < boost::int64_t > int64_p = spirit_namespace::int_parser < boost::int64_t >();
6895 const spirit_namespace::uint_parser< boost::uint64_t > uint64_p = spirit_namespace::uint_parser< boost::uint64_t >();
6896
6897 template< class Iter_type >
6898 bool is_eq( Iter_type first, Iter_type last, const char* c_str )
6899 {
6900 for( Iter_type i = first; i != last; ++i, ++c_str )
6901 {
6902 if( *c_str == 0 ) return false;
6903
6904 if( *i != *c_str ) return false;
6905 }
6906
6907 return true;
6908 }
6909
6910 template< class Char_type >
6911 Char_type hex_to_num( const Char_type c )
6912 {
6913 if( ( c >= '0' ) && ( c <= '9' ) ) return c - '0';
6914 if( ( c >= 'a' ) && ( c <= 'f' ) ) return c - 'a' + 10;
6915 if( ( c >= 'A' ) && ( c <= 'F' ) ) return c - 'A' + 10;
6916 return 0;
6917 }
6918
6919 template< class Char_type, class Iter_type >
6920 Char_type hex_str_to_char( Iter_type& begin )
6921 {
6922 const Char_type c1( *( ++begin ) );
6923 const Char_type c2( *( ++begin ) );
6924
6925 return ( hex_to_num( c1 ) << 4 ) + hex_to_num( c2 );
6926 }
6927
6928 template< class Char_type, class Iter_type >
6929 Char_type unicode_str_to_char( Iter_type& begin )
6930 {
6931 const Char_type c1( *( ++begin ) );
6932 const Char_type c2( *( ++begin ) );
6933 const Char_type c3( *( ++begin ) );
6934 const Char_type c4( *( ++begin ) );
6935
6936 return ( hex_to_num( c1 ) << 12 ) +
6937 ( hex_to_num( c2 ) << 8 ) +
6938 ( hex_to_num( c3 ) << 4 ) +
6939 hex_to_num( c4 );
6940 }
6941
6942 template< class String_type >
6943 void append_esc_char_and_incr_iter( String_type& s,
6944 typename String_type::const_iterator& begin,
6945 typename String_type::const_iterator end )
6946 {
6947 typedef typename String_type::value_type Char_type;
6948
6949 const Char_type c2( *begin );
6950
6951 switch( c2 )
6952 {
6953 case 't': s += '\t'; break;
6954 case 'b': s += '\b'; break;
6955 case 'f': s += '\f'; break;
6956 case 'n': s += '\n'; break;
6957 case 'r': s += '\r'; break;
6958 case '\\': s += '\\'; break;
6959 case '/': s += '/'; break;
6960 case '"': s += '"'; break;
6961 case 'x':
6962 {
6963 if( end - begin >= 3 ) // expecting "xHH..."
6964 {
6965 s += hex_str_to_char< Char_type >( begin );
6966 }
6967 break;
6968 }
6969 case 'u':
6970 {
6971 if( end - begin >= 5 ) // expecting "uHHHH..."
6972 {
6973 s += unicode_str_to_char< Char_type >( begin );
6974 }
6975 break;
6976 }
6977 }
6978 }
6979
6980 template< class String_type >
6981 String_type substitute_esc_chars( typename String_type::const_iterator begin,
6982 typename String_type::const_iterator end )
6983 {
6984 typedef typename String_type::const_iterator Iter_type;
6985
6986 if( end - begin < 2 ) return String_type( begin, end );
6987
6988 String_type result;
6989
6990 result.reserve( end - begin );
6991
6992 const Iter_type end_minus_1( end - 1 );
6993
6994 Iter_type substr_start = begin;
6995 Iter_type i = begin;
6996
6997 for( ; i < end_minus_1; ++i )
6998 {
6999 if( *i == '\\' )
7000 {
7001 result.append( substr_start, i );
7002
7003 ++i; // skip the '\'
7004
7005 append_esc_char_and_incr_iter( result, i, end );
7006
7007 substr_start = i + 1;
7008 }
7009 }
7010
7011 result.append( substr_start, end );
7012
7013 return result;
7014 }
7015
7016 template< class String_type >
7017 String_type get_str_( typename String_type::const_iterator begin,
7018 typename String_type::const_iterator end )
7019 {
7020 assert( end - begin >= 2 );
7021
7022 typedef typename String_type::const_iterator Iter_type;
7023
7024 Iter_type str_without_quotes( ++begin );
7025 Iter_type end_without_quotes( --end );
7026
7027 return substitute_esc_chars< String_type >( str_without_quotes, end_without_quotes );
7028 }
7029
7030 inline std::string get_str( std::string::const_iterator begin, std::string::const_iterator end )
7031 {
7032 return get_str_< std::string >( begin, end );
7033 }
7034
7035 inline std::wstring get_str( std::wstring::const_iterator begin, std::wstring::const_iterator end )
7036 {
7037 return get_str_< std::wstring >( begin, end );
7038 }
7039
7040 template< class String_type, class Iter_type >
7041 String_type get_str( Iter_type begin, Iter_type end )
7042 {
7043 const String_type tmp( begin, end ); // convert multipass iterators to string iterators
7044
7045 return get_str( tmp.begin(), tmp.end() );
7046 }
7047
7048 // this class's methods get called by the spirit parse resulting
7049 // in the creation of a JSON object or array
7050 //
7051 // NB Iter_type could be a std::string iterator, wstring iterator, a position iterator or a multipass iterator
7052 //
7053 template< class Value_type, class Iter_type >
7054 class Semantic_actions
7055 {
7056 public:
7057
7058 typedef typename Value_type::Config_type Config_type;
7059 typedef typename Config_type::String_type String_type;
7060 typedef typename Config_type::Object_type Object_type;
7061 typedef typename Config_type::Array_type Array_type;
7062 typedef typename String_type::value_type Char_type;
7063
7064 Semantic_actions( Value_type& value )
7065 : value_( value )
7066 , current_p_( 0 )
7067 {
7068 }
7069
7070 void begin_obj( Char_type c )
7071 {
7072 assert( c == '{' );
7073
7074 begin_compound< Object_type >();
7075 }
7076
7077 void end_obj( Char_type c )
7078 {
7079 assert( c == '}' );
7080
7081 end_compound();
7082 }
7083
7084 void begin_array( Char_type c )
7085 {
7086 assert( c == '[' );
7087
7088 begin_compound< Array_type >();
7089 }
7090
7091 void end_array( Char_type c )
7092 {
7093 assert( c == ']' );
7094
7095 end_compound();
7096 }
7097
7098 void new_name( Iter_type begin, Iter_type end )
7099 {
7100 assert( current_p_->type() == obj_type );
7101
7102 name_ = get_str< String_type >( begin, end );
7103 }
7104
7105 void new_str( Iter_type begin, Iter_type end )
7106 {
7107 add_to_current( get_str< String_type >( begin, end ) );
7108 }
7109
7110 void new_true( Iter_type begin, Iter_type end )
7111 {
7112 assert( is_eq( begin, end, "true" ) );
7113
7114 add_to_current( true );
7115 }
7116
7117 void new_false( Iter_type begin, Iter_type end )
7118 {
7119 assert( is_eq( begin, end, "false" ) );
7120
7121 add_to_current( false );
7122 }
7123
7124 void new_null( Iter_type begin, Iter_type end )
7125 {
7126 assert( is_eq( begin, end, "null" ) );
7127
7128 add_to_current( Value_type() );
7129 }
7130
7131 void new_int( boost::int64_t i )
7132 {
7133 add_to_current( i );
7134 }
7135
7136 void new_uint64( boost::uint64_t ui )
7137 {
7138 add_to_current( ui );
7139 }
7140
7141 void new_real( double d )
7142 {
7143 add_to_current( d );
7144 }
7145
7146 private:
7147
7148 Semantic_actions& operator=( const Semantic_actions& );
7149 // to prevent "assignment operator could not be generated" warning
7150
7151 Value_type* add_first( const Value_type& value )
7152 {
7153 assert( current_p_ == 0 );
7154
7155 value_ = value;
7156 current_p_ = &value_;
7157 return current_p_;
7158 }
7159
7160 template< class Array_or_obj >
7161 void begin_compound()
7162 {
7163 if( current_p_ == 0 )
7164 {
7165 add_first( Array_or_obj() );
7166 }
7167 else
7168 {
7169 stack_.push_back( current_p_ );
7170
7171 Array_or_obj new_array_or_obj; // avoid copy by building new array or object in place
7172
7173 current_p_ = add_to_current( new_array_or_obj );
7174 }
7175 }
7176
7177 void end_compound()
7178 {
7179 if( current_p_ != &value_ )
7180 {
7181 current_p_ = stack_.back();
7182
7183 stack_.pop_back();
7184 }
7185 }
7186
7187 Value_type* add_to_current( const Value_type& value )
7188 {
7189 if( current_p_ == 0 )
7190 {
7191 return add_first( value );
7192 }
7193 else if( current_p_->type() == array_type )
7194 {
7195 current_p_->get_array().push_back( value );
7196
7197 return ¤t_p_->get_array().back();
7198 }
7199
7200 assert( current_p_->type() == obj_type );
7201
7202 return &Config_type::add( current_p_->get_obj(), name_, value );
7203 }
7204
7205 Value_type& value_; // this is the object or array that is being created
7206 Value_type* current_p_; // the child object or array that is currently being constructed
7207
7208 std::vector< Value_type* > stack_; // previous child objects and arrays
7209
7210 String_type name_; // of current name/value pair
7211 };
7212
7213 template< typename Iter_type >
7214 void throw_error( spirit_namespace::position_iterator< Iter_type > i, const std::string& reason )
7215 {
7216 throw Error_position( i.get_position().line, i.get_position().column, reason );
7217 }
7218
7219 template< typename Iter_type >
7220 void throw_error( Iter_type i, const std::string& reason )
7221 {
7222 throw reason;
7223 }
7224
7225 // the spirit grammer
7226 //
7227 template< class Value_type, class Iter_type >
7228 class Json_grammer : public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >
7229 {
7230 public:
7231
7232 typedef Semantic_actions< Value_type, Iter_type > Semantic_actions_t;
7233
7234 Json_grammer( Semantic_actions_t& semantic_actions )
7235 : actions_( semantic_actions )
7236 {
7237 }
7238
7239 static void throw_not_value( Iter_type begin, Iter_type end )
7240 {
7241 throw_error( begin, "not a value" );
7242 }
7243
7244 static void throw_not_array( Iter_type begin, Iter_type end )
7245 {
7246 throw_error( begin, "not an array" );
7247 }
7248
7249 static void throw_not_object( Iter_type begin, Iter_type end )
7250 {
7251 throw_error( begin, "not an object" );
7252 }
7253
7254 static void throw_not_pair( Iter_type begin, Iter_type end )
7255 {
7256 throw_error( begin, "not a pair" );
7257 }
7258
7259 static void throw_not_colon( Iter_type begin, Iter_type end )
7260 {
7261 throw_error( begin, "no colon in pair" );
7262 }
7263
7264 static void throw_not_string( Iter_type begin, Iter_type end )
7265 {
7266 throw_error( begin, "not a string" );
7267 }
7268
7269 template< typename ScannerT >
7270 class definition
7271 {
7272 public:
7273
7274 definition( const Json_grammer& self )
7275 {
7276 using namespace spirit_namespace;
7277
7278 typedef typename Value_type::String_type::value_type Char_type;
7279
7280 // first we convert the semantic action class methods to functors with the
7281 // parameter signature expected by spirit
7282
7283 typedef boost::function< void( Char_type ) > Char_action;
7284 typedef boost::function< void( Iter_type, Iter_type ) > Str_action;
7285 typedef boost::function< void( double ) > Real_action;
7286 typedef boost::function< void( boost::int64_t ) > Int_action;
7287 typedef boost::function< void( boost::uint64_t ) > Uint64_action;
7288
7289 Char_action begin_obj ( boost::bind( &Semantic_actions_t::begin_obj, &self.actions_, _1 ) );
7290 Char_action end_obj ( boost::bind( &Semantic_actions_t::end_obj, &self.actions_, _1 ) );
7291 Char_action begin_array( boost::bind( &Semantic_actions_t::begin_array, &self.actions_, _1 ) );
7292 Char_action end_array ( boost::bind( &Semantic_actions_t::end_array, &self.actions_, _1 ) );
7293 Str_action new_name ( boost::bind( &Semantic_actions_t::new_name, &self.actions_, _1, _2 ) );
7294 Str_action new_str ( boost::bind( &Semantic_actions_t::new_str, &self.actions_, _1, _2 ) );
7295 Str_action new_true ( boost::bind( &Semantic_actions_t::new_true, &self.actions_, _1, _2 ) );
7296 Str_action new_false ( boost::bind( &Semantic_actions_t::new_false, &self.actions_, _1, _2 ) );
7297 Str_action new_null ( boost::bind( &Semantic_actions_t::new_null, &self.actions_, _1, _2 ) );
7298 Real_action new_real ( boost::bind( &Semantic_actions_t::new_real, &self.actions_, _1 ) );
7299 Int_action new_int ( boost::bind( &Semantic_actions_t::new_int, &self.actions_, _1 ) );
7300 Uint64_action new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64, &self.actions_, _1 ) );
7301
7302 // actual grammer
7303
7304 json_
7305 = value_ | eps_p[ &throw_not_value ]
7306 ;
7307
7308 value_
7309 = string_[ new_str ]
7310 | number_
7311 | object_
7312 | array_
7313 | str_p( "true" ) [ new_true ]
7314 | str_p( "false" )[ new_false ]
7315 | str_p( "null" ) [ new_null ]
7316 ;
7317
7318 object_
7319 = ch_p('{')[ begin_obj ]
7320 >> !members_
7321 >> ( ch_p('}')[ end_obj ] | eps_p[ &throw_not_object ] )
7322 ;
7323
7324 members_
7325 = pair_ >> *( ',' >> pair_ )
7326 ;
7327
7328 pair_
7329 = string_[ new_name ]
7330 >> ( ':' | eps_p[ &throw_not_colon ] )
7331 >> ( value_ | eps_p[ &throw_not_value ] )
7332 ;
7333
7334 array_
7335 = ch_p('[')[ begin_array ]
7336 >> !elements_
7337 >> ( ch_p(']')[ end_array ] | eps_p[ &throw_not_array ] )
7338 ;
7339
7340 elements_
7341 = value_ >> *( ',' >> value_ )
7342 ;
7343
7344 string_
7345 = lexeme_d // this causes white space inside a string to be retained
7346 [
7347 confix_p
7348 (
7349 '"',
7350 *lex_escape_ch_p,
7351 '"'
7352 )
7353 ]
7354 ;
7355
7356 number_
7357 = strict_real_p[ new_real ]
7358 | int64_p [ new_int ]
7359 | uint64_p [ new_uint64 ]
7360 ;
7361 }
7362
7363 spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_, value_, string_, number_;
7364
7365 const spirit_namespace::rule< ScannerT >& start() const { return json_; }
7366 };
7367
7368 private:
7369
7370 Json_grammer& operator=( const Json_grammer& ); // to prevent "assignment operator could not be generated" warning
7371
7372 Semantic_actions_t& actions_;
7373 };
7374
7375 template< class Iter_type, class Value_type >
7376 Iter_type read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
7377 {
7378 Semantic_actions< Value_type, Iter_type > semantic_actions( value );
7379
7380 const spirit_namespace::parse_info< Iter_type > info =
7381 spirit_namespace::parse( begin, end,
7382 Json_grammer< Value_type, Iter_type >( semantic_actions ),
7383 spirit_namespace::space_p );
7384
7385 if( !info.hit )
7386 {
7387 assert( false ); // in theory exception should already have been thrown
7388 throw_error( info.stop, "error" );
7389 }
7390
7391 return info.stop;
7392 }
7393
7394 template< class Iter_type, class Value_type >
7395 void add_posn_iter_and_read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
7396 {
7397 typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;
7398
7399 const Posn_iter_t posn_begin( begin, end );
7400 const Posn_iter_t posn_end( end, end );
7401
7402 read_range_or_throw( posn_begin, posn_end, value );
7403 }
7404
7405 template< class Iter_type, class Value_type >
7406 bool read_range( Iter_type& begin, Iter_type end, Value_type& value )
7407 {
7408 try
7409 {
7410 begin = read_range_or_throw( begin, end, value );
7411
7412 return true;
7413 }
7414 catch( ... )
7415 {
7416 return false;
7417 }
7418 }
7419
7420 template< class String_type, class Value_type >
7421 void read_string_or_throw( const String_type& s, Value_type& value )
7422 {
7423 add_posn_iter_and_read_range_or_throw( s.begin(), s.end(), value );
7424 }
7425
7426 template< class String_type, class Value_type >
7427 bool read_string( const String_type& s, Value_type& value )
7428 {
7429 typename String_type::const_iterator begin = s.begin();
7430
7431 return read_range( begin, s.end(), value );
7432 }
7433
7434 template< class Istream_type >
7435 struct Multi_pass_iters
7436 {
7437 typedef typename Istream_type::char_type Char_type;
7438 typedef std::istream_iterator< Char_type, Char_type > istream_iter;
7439 typedef spirit_namespace::multi_pass< istream_iter > Mp_iter;
7440
7441 Multi_pass_iters( Istream_type& is )
7442 {
7443 is.unsetf( std::ios::skipws );
7444
7445 begin_ = spirit_namespace::make_multi_pass( istream_iter( is ) );
7446 end_ = spirit_namespace::make_multi_pass( istream_iter() );
7447 }
7448
7449 Mp_iter begin_;
7450 Mp_iter end_;
7451 };
7452
7453 template< class Istream_type, class Value_type >
7454 bool read_stream( Istream_type& is, Value_type& value )
7455 {
7456 Multi_pass_iters< Istream_type > mp_iters( is );
7457
7458 return read_range( mp_iters.begin_, mp_iters.end_, value );
7459 }
7460
7461 template< class Istream_type, class Value_type >
7462 void read_stream_or_throw( Istream_type& is, Value_type& value )
7463 {
7464 const Multi_pass_iters< Istream_type > mp_iters( is );
7465
7466 add_posn_iter_and_read_range_or_throw( mp_iters.begin_, mp_iters.end_, value );
7467 }
7468 }
7469
7470 #endif