-
+ 374B75FCF0AC2B6E20D8191BD65B0961566E907D7C5FC6B5543B99B24881343173CEE48D06CC8171778DB05B14CFB9D4D7712430D59718E780420075D7E264A1
bitcoin/src/json/json_spirit_reader_template.h
(0 . 0)(1 . 643)
7541 // /****************************\
7542 // * EXPERIMENTAL BRANCH. *
7543 // * FOR LABORATORY USE ONLY. *
7544 // ********************************
7545 // ************
7546 // **************
7547 // ****************
7548 // **** **** ****
7549 // *** *** ***
7550 // *** *** ***
7551 // *** * * **
7552 // ******** ********
7553 // ******* ******
7554 // *** **
7555 // * ******* **
7556 // ** * * * * *
7557 // ** * * ***
7558 // **** * * * * ****
7559 // **** *** * * ** ***
7560 // **** ********* ******
7561 // ******* ***** *******
7562 // ********* ****** **
7563 // ** ****** ******
7564 // ** ******* **
7565 // ** ******* ***
7566 // **** ******** ************
7567 // ************ ************
7568 // ******** *******
7569 // ****** ****
7570 // *** ***
7571 // ********************************
7572 #ifndef JSON_SPIRIT_READER_TEMPLATE
7573 #define JSON_SPIRIT_READER_TEMPLATE
7574
7575 // Copyright John W. Wilkinson 2007 - 2009.
7576 // Distributed under the MIT License, see accompanying file LICENSE.txt
7577
7578 // json spirit version 4.03
7579
7580 #include "json_spirit_value.h"
7581 #include "json_spirit_error_position.h"
7582
7583 //#define BOOST_SPIRIT_THREADSAFE // uncomment for multithreaded use, requires linking to boost.thread
7584
7585 #include <boost/bind.hpp>
7586 #include <boost/function.hpp>
7587 #include <boost/version.hpp>
7588
7589 #if BOOST_VERSION >= 103800
7590 #include <boost/spirit/include/classic_core.hpp>
7591 #include <boost/spirit/include/classic_confix.hpp>
7592 #include <boost/spirit/include/classic_escape_char.hpp>
7593 #include <boost/spirit/include/classic_multi_pass.hpp>
7594 #include <boost/spirit/include/classic_position_iterator.hpp>
7595 #define spirit_namespace boost::spirit::classic
7596 #else
7597 #include <boost/spirit/core.hpp>
7598 #include <boost/spirit/utility/confix.hpp>
7599 #include <boost/spirit/utility/escape_char.hpp>
7600 #include <boost/spirit/iterator/multi_pass.hpp>
7601 #include <boost/spirit/iterator/position_iterator.hpp>
7602 #define spirit_namespace boost::spirit
7603 #endif
7604
7605 namespace json_spirit
7606 {
7607 const spirit_namespace::int_parser < boost::int64_t > int64_p = spirit_namespace::int_parser < boost::int64_t >();
7608 const spirit_namespace::uint_parser< boost::uint64_t > uint64_p = spirit_namespace::uint_parser< boost::uint64_t >();
7609
7610 template< class Iter_type >
7611 bool is_eq( Iter_type first, Iter_type last, const char* c_str )
7612 {
7613 for( Iter_type i = first; i != last; ++i, ++c_str )
7614 {
7615 if( *c_str == 0 ) return false;
7616
7617 if( *i != *c_str ) return false;
7618 }
7619
7620 return true;
7621 }
7622
7623 template< class Char_type >
7624 Char_type hex_to_num( const Char_type c )
7625 {
7626 if( ( c >= '0' ) && ( c <= '9' ) ) return c - '0';
7627 if( ( c >= 'a' ) && ( c <= 'f' ) ) return c - 'a' + 10;
7628 if( ( c >= 'A' ) && ( c <= 'F' ) ) return c - 'A' + 10;
7629 return 0;
7630 }
7631
7632 template< class Char_type, class Iter_type >
7633 Char_type hex_str_to_char( Iter_type& begin )
7634 {
7635 const Char_type c1( *( ++begin ) );
7636 const Char_type c2( *( ++begin ) );
7637
7638 return ( hex_to_num( c1 ) << 4 ) + hex_to_num( c2 );
7639 }
7640
7641 template< class Char_type, class Iter_type >
7642 Char_type unicode_str_to_char( Iter_type& begin )
7643 {
7644 const Char_type c1( *( ++begin ) );
7645 const Char_type c2( *( ++begin ) );
7646 const Char_type c3( *( ++begin ) );
7647 const Char_type c4( *( ++begin ) );
7648
7649 return ( hex_to_num( c1 ) << 12 ) +
7650 ( hex_to_num( c2 ) << 8 ) +
7651 ( hex_to_num( c3 ) << 4 ) +
7652 hex_to_num( c4 );
7653 }
7654
7655 template< class String_type >
7656 void append_esc_char_and_incr_iter( String_type& s,
7657 typename String_type::const_iterator& begin,
7658 typename String_type::const_iterator end )
7659 {
7660 typedef typename String_type::value_type Char_type;
7661
7662 const Char_type c2( *begin );
7663
7664 switch( c2 )
7665 {
7666 case 't': s += '\t'; break;
7667 case 'b': s += '\b'; break;
7668 case 'f': s += '\f'; break;
7669 case 'n': s += '\n'; break;
7670 case 'r': s += '\r'; break;
7671 case '\\': s += '\\'; break;
7672 case '/': s += '/'; break;
7673 case '"': s += '"'; break;
7674 case 'x':
7675 {
7676 if( end - begin >= 3 ) // expecting "xHH..."
7677 {
7678 s += hex_str_to_char< Char_type >( begin );
7679 }
7680 break;
7681 }
7682 case 'u':
7683 {
7684 if( end - begin >= 5 ) // expecting "uHHHH..."
7685 {
7686 s += unicode_str_to_char< Char_type >( begin );
7687 }
7688 break;
7689 }
7690 }
7691 }
7692
7693 template< class String_type >
7694 String_type substitute_esc_chars( typename String_type::const_iterator begin,
7695 typename String_type::const_iterator end )
7696 {
7697 typedef typename String_type::const_iterator Iter_type;
7698
7699 if( end - begin < 2 ) return String_type( begin, end );
7700
7701 String_type result;
7702
7703 result.reserve( end - begin );
7704
7705 const Iter_type end_minus_1( end - 1 );
7706
7707 Iter_type substr_start = begin;
7708 Iter_type i = begin;
7709
7710 for( ; i < end_minus_1; ++i )
7711 {
7712 if( *i == '\\' )
7713 {
7714 result.append( substr_start, i );
7715
7716 ++i; // skip the '\'
7717
7718 append_esc_char_and_incr_iter( result, i, end );
7719
7720 substr_start = i + 1;
7721 }
7722 }
7723
7724 result.append( substr_start, end );
7725
7726 return result;
7727 }
7728
7729 template< class String_type >
7730 String_type get_str_( typename String_type::const_iterator begin,
7731 typename String_type::const_iterator end )
7732 {
7733 assert( end - begin >= 2 );
7734
7735 typedef typename String_type::const_iterator Iter_type;
7736
7737 Iter_type str_without_quotes( ++begin );
7738 Iter_type end_without_quotes( --end );
7739
7740 return substitute_esc_chars< String_type >( str_without_quotes, end_without_quotes );
7741 }
7742
7743 inline std::string get_str( std::string::const_iterator begin, std::string::const_iterator end )
7744 {
7745 return get_str_< std::string >( begin, end );
7746 }
7747
7748 inline std::wstring get_str( std::wstring::const_iterator begin, std::wstring::const_iterator end )
7749 {
7750 return get_str_< std::wstring >( begin, end );
7751 }
7752
7753 template< class String_type, class Iter_type >
7754 String_type get_str( Iter_type begin, Iter_type end )
7755 {
7756 const String_type tmp( begin, end ); // convert multipass iterators to string iterators
7757
7758 return get_str( tmp.begin(), tmp.end() );
7759 }
7760
7761 // this class's methods get called by the spirit parse resulting
7762 // in the creation of a JSON object or array
7763 //
7764 // NB Iter_type could be a std::string iterator, wstring iterator, a position iterator or a multipass iterator
7765 //
7766 template< class Value_type, class Iter_type >
7767 class Semantic_actions
7768 {
7769 public:
7770
7771 typedef typename Value_type::Config_type Config_type;
7772 typedef typename Config_type::String_type String_type;
7773 typedef typename Config_type::Object_type Object_type;
7774 typedef typename Config_type::Array_type Array_type;
7775 typedef typename String_type::value_type Char_type;
7776
7777 Semantic_actions( Value_type& value )
7778 : value_( value )
7779 , current_p_( 0 )
7780 {
7781 }
7782
7783 void begin_obj( Char_type c )
7784 {
7785 assert( c == '{' );
7786
7787 begin_compound< Object_type >();
7788 }
7789
7790 void end_obj( Char_type c )
7791 {
7792 assert( c == '}' );
7793
7794 end_compound();
7795 }
7796
7797 void begin_array( Char_type c )
7798 {
7799 assert( c == '[' );
7800
7801 begin_compound< Array_type >();
7802 }
7803
7804 void end_array( Char_type c )
7805 {
7806 assert( c == ']' );
7807
7808 end_compound();
7809 }
7810
7811 void new_name( Iter_type begin, Iter_type end )
7812 {
7813 assert( current_p_->type() == obj_type );
7814
7815 name_ = get_str< String_type >( begin, end );
7816 }
7817
7818 void new_str( Iter_type begin, Iter_type end )
7819 {
7820 add_to_current( get_str< String_type >( begin, end ) );
7821 }
7822
7823 void new_true( Iter_type begin, Iter_type end )
7824 {
7825 assert( is_eq( begin, end, "true" ) );
7826
7827 add_to_current( true );
7828 }
7829
7830 void new_false( Iter_type begin, Iter_type end )
7831 {
7832 assert( is_eq( begin, end, "false" ) );
7833
7834 add_to_current( false );
7835 }
7836
7837 void new_null( Iter_type begin, Iter_type end )
7838 {
7839 assert( is_eq( begin, end, "null" ) );
7840
7841 add_to_current( Value_type() );
7842 }
7843
7844 void new_int( boost::int64_t i )
7845 {
7846 add_to_current( i );
7847 }
7848
7849 void new_uint64( boost::uint64_t ui )
7850 {
7851 add_to_current( ui );
7852 }
7853
7854 void new_real( double d )
7855 {
7856 add_to_current( d );
7857 }
7858
7859 private:
7860
7861 Semantic_actions& operator=( const Semantic_actions& );
7862 // to prevent "assignment operator could not be generated" warning
7863
7864 Value_type* add_first( const Value_type& value )
7865 {
7866 assert( current_p_ == 0 );
7867
7868 value_ = value;
7869 current_p_ = &value_;
7870 return current_p_;
7871 }
7872
7873 template< class Array_or_obj >
7874 void begin_compound()
7875 {
7876 if( current_p_ == 0 )
7877 {
7878 add_first( Array_or_obj() );
7879 }
7880 else
7881 {
7882 stack_.push_back( current_p_ );
7883
7884 Array_or_obj new_array_or_obj; // avoid copy by building new array or object in place
7885
7886 current_p_ = add_to_current( new_array_or_obj );
7887 }
7888 }
7889
7890 void end_compound()
7891 {
7892 if( current_p_ != &value_ )
7893 {
7894 current_p_ = stack_.back();
7895
7896 stack_.pop_back();
7897 }
7898 }
7899
7900 Value_type* add_to_current( const Value_type& value )
7901 {
7902 if( current_p_ == 0 )
7903 {
7904 return add_first( value );
7905 }
7906 else if( current_p_->type() == array_type )
7907 {
7908 current_p_->get_array().push_back( value );
7909
7910 return ¤t_p_->get_array().back();
7911 }
7912
7913 assert( current_p_->type() == obj_type );
7914
7915 return &Config_type::add( current_p_->get_obj(), name_, value );
7916 }
7917
7918 Value_type& value_; // this is the object or array that is being created
7919 Value_type* current_p_; // the child object or array that is currently being constructed
7920
7921 std::vector< Value_type* > stack_; // previous child objects and arrays
7922
7923 String_type name_; // of current name/value pair
7924 };
7925
7926 template< typename Iter_type >
7927 void throw_error( spirit_namespace::position_iterator< Iter_type > i, const std::string& reason )
7928 {
7929 throw Error_position( i.get_position().line, i.get_position().column, reason );
7930 }
7931
7932 template< typename Iter_type >
7933 void throw_error( Iter_type i, const std::string& reason )
7934 {
7935 throw reason;
7936 }
7937
7938 // the spirit grammer
7939 //
7940 template< class Value_type, class Iter_type >
7941 class Json_grammer : public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >
7942 {
7943 public:
7944
7945 typedef Semantic_actions< Value_type, Iter_type > Semantic_actions_t;
7946
7947 Json_grammer( Semantic_actions_t& semantic_actions )
7948 : actions_( semantic_actions )
7949 {
7950 }
7951
7952 static void throw_not_value( Iter_type begin, Iter_type end )
7953 {
7954 throw_error( begin, "not a value" );
7955 }
7956
7957 static void throw_not_array( Iter_type begin, Iter_type end )
7958 {
7959 throw_error( begin, "not an array" );
7960 }
7961
7962 static void throw_not_object( Iter_type begin, Iter_type end )
7963 {
7964 throw_error( begin, "not an object" );
7965 }
7966
7967 static void throw_not_pair( Iter_type begin, Iter_type end )
7968 {
7969 throw_error( begin, "not a pair" );
7970 }
7971
7972 static void throw_not_colon( Iter_type begin, Iter_type end )
7973 {
7974 throw_error( begin, "no colon in pair" );
7975 }
7976
7977 static void throw_not_string( Iter_type begin, Iter_type end )
7978 {
7979 throw_error( begin, "not a string" );
7980 }
7981
7982 template< typename ScannerT >
7983 class definition
7984 {
7985 public:
7986
7987 definition( const Json_grammer& self )
7988 {
7989 using namespace spirit_namespace;
7990
7991 typedef typename Value_type::String_type::value_type Char_type;
7992
7993 // first we convert the semantic action class methods to functors with the
7994 // parameter signature expected by spirit
7995
7996 typedef boost::function< void( Char_type ) > Char_action;
7997 typedef boost::function< void( Iter_type, Iter_type ) > Str_action;
7998 typedef boost::function< void( double ) > Real_action;
7999 typedef boost::function< void( boost::int64_t ) > Int_action;
8000 typedef boost::function< void( boost::uint64_t ) > Uint64_action;
8001
8002 Char_action begin_obj ( boost::bind( &Semantic_actions_t::begin_obj, &self.actions_, _1 ) );
8003 Char_action end_obj ( boost::bind( &Semantic_actions_t::end_obj, &self.actions_, _1 ) );
8004 Char_action begin_array( boost::bind( &Semantic_actions_t::begin_array, &self.actions_, _1 ) );
8005 Char_action end_array ( boost::bind( &Semantic_actions_t::end_array, &self.actions_, _1 ) );
8006 Str_action new_name ( boost::bind( &Semantic_actions_t::new_name, &self.actions_, _1, _2 ) );
8007 Str_action new_str ( boost::bind( &Semantic_actions_t::new_str, &self.actions_, _1, _2 ) );
8008 Str_action new_true ( boost::bind( &Semantic_actions_t::new_true, &self.actions_, _1, _2 ) );
8009 Str_action new_false ( boost::bind( &Semantic_actions_t::new_false, &self.actions_, _1, _2 ) );
8010 Str_action new_null ( boost::bind( &Semantic_actions_t::new_null, &self.actions_, _1, _2 ) );
8011 Real_action new_real ( boost::bind( &Semantic_actions_t::new_real, &self.actions_, _1 ) );
8012 Int_action new_int ( boost::bind( &Semantic_actions_t::new_int, &self.actions_, _1 ) );
8013 Uint64_action new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64, &self.actions_, _1 ) );
8014
8015 // actual grammer
8016
8017 json_
8018 = value_ | eps_p[ &throw_not_value ]
8019 ;
8020
8021 value_
8022 = string_[ new_str ]
8023 | number_
8024 | object_
8025 | array_
8026 | str_p( "true" ) [ new_true ]
8027 | str_p( "false" )[ new_false ]
8028 | str_p( "null" ) [ new_null ]
8029 ;
8030
8031 object_
8032 = ch_p('{')[ begin_obj ]
8033 >> !members_
8034 >> ( ch_p('}')[ end_obj ] | eps_p[ &throw_not_object ] )
8035 ;
8036
8037 members_
8038 = pair_ >> *( ',' >> pair_ )
8039 ;
8040
8041 pair_
8042 = string_[ new_name ]
8043 >> ( ':' | eps_p[ &throw_not_colon ] )
8044 >> ( value_ | eps_p[ &throw_not_value ] )
8045 ;
8046
8047 array_
8048 = ch_p('[')[ begin_array ]
8049 >> !elements_
8050 >> ( ch_p(']')[ end_array ] | eps_p[ &throw_not_array ] )
8051 ;
8052
8053 elements_
8054 = value_ >> *( ',' >> value_ )
8055 ;
8056
8057 string_
8058 = lexeme_d // this causes white space inside a string to be retained
8059 [
8060 confix_p
8061 (
8062 '"',
8063 *lex_escape_ch_p,
8064 '"'
8065 )
8066 ]
8067 ;
8068
8069 number_
8070 = strict_real_p[ new_real ]
8071 | int64_p [ new_int ]
8072 | uint64_p [ new_uint64 ]
8073 ;
8074 }
8075
8076 spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_, value_, string_, number_;
8077
8078 const spirit_namespace::rule< ScannerT >& start() const { return json_; }
8079 };
8080
8081 private:
8082
8083 Json_grammer& operator=( const Json_grammer& ); // to prevent "assignment operator could not be generated" warning
8084
8085 Semantic_actions_t& actions_;
8086 };
8087
8088 template< class Iter_type, class Value_type >
8089 Iter_type read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
8090 {
8091 Semantic_actions< Value_type, Iter_type > semantic_actions( value );
8092
8093 const spirit_namespace::parse_info< Iter_type > info =
8094 spirit_namespace::parse( begin, end,
8095 Json_grammer< Value_type, Iter_type >( semantic_actions ),
8096 spirit_namespace::space_p );
8097
8098 if( !info.hit )
8099 {
8100 assert( false ); // in theory exception should already have been thrown
8101 throw_error( info.stop, "error" );
8102 }
8103
8104 return info.stop;
8105 }
8106
8107 template< class Iter_type, class Value_type >
8108 void add_posn_iter_and_read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
8109 {
8110 typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;
8111
8112 const Posn_iter_t posn_begin( begin, end );
8113 const Posn_iter_t posn_end( end, end );
8114
8115 read_range_or_throw( posn_begin, posn_end, value );
8116 }
8117
8118 template< class Iter_type, class Value_type >
8119 bool read_range( Iter_type& begin, Iter_type end, Value_type& value )
8120 {
8121 try
8122 {
8123 begin = read_range_or_throw( begin, end, value );
8124
8125 return true;
8126 }
8127 catch( ... )
8128 {
8129 return false;
8130 }
8131 }
8132
8133 template< class String_type, class Value_type >
8134 void read_string_or_throw( const String_type& s, Value_type& value )
8135 {
8136 add_posn_iter_and_read_range_or_throw( s.begin(), s.end(), value );
8137 }
8138
8139 template< class String_type, class Value_type >
8140 bool read_string( const String_type& s, Value_type& value )
8141 {
8142 typename String_type::const_iterator begin = s.begin();
8143
8144 return read_range( begin, s.end(), value );
8145 }
8146
8147 template< class Istream_type >
8148 struct Multi_pass_iters
8149 {
8150 typedef typename Istream_type::char_type Char_type;
8151 typedef std::istream_iterator< Char_type, Char_type > istream_iter;
8152 typedef spirit_namespace::multi_pass< istream_iter > Mp_iter;
8153
8154 Multi_pass_iters( Istream_type& is )
8155 {
8156 is.unsetf( std::ios::skipws );
8157
8158 begin_ = spirit_namespace::make_multi_pass( istream_iter( is ) );
8159 end_ = spirit_namespace::make_multi_pass( istream_iter() );
8160 }
8161
8162 Mp_iter begin_;
8163 Mp_iter end_;
8164 };
8165
8166 template< class Istream_type, class Value_type >
8167 bool read_stream( Istream_type& is, Value_type& value )
8168 {
8169 Multi_pass_iters< Istream_type > mp_iters( is );
8170
8171 return read_range( mp_iters.begin_, mp_iters.end_, value );
8172 }
8173
8174 template< class Istream_type, class Value_type >
8175 void read_stream_or_throw( Istream_type& is, Value_type& value )
8176 {
8177 const Multi_pass_iters< Istream_type > mp_iters( is );
8178
8179 add_posn_iter_and_read_range_or_throw( mp_iters.begin_, mp_iters.end_, value );
8180 }
8181 }
8182
8183 #endif