1 #ifndef JSON_SPIRIT_READER_TEMPLATE
2 #define JSON_SPIRIT_READER_TEMPLATE
3 
4 //          Copyright John W. Wilkinson 2007 - 2014
5 // Distributed under the MIT License, see accompanying file LICENSE-OSS
6 
7 // json spirit version 4.08
8 
9 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
10 # pragma once
11 #endif
12 
13 #include "json_spirit_value.h"
14 #include "json_spirit_error_position.h"
15 
16 //#define BOOST_SPIRIT_THREADSAFE  // uncomment for multithreaded use, requires linking to boost.thread
17 
18 #include <boost/bind.hpp>
19 #include <boost/function.hpp>
20 #include <boost/version.hpp>
21 
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
29 #else
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
36 #endif
37 
38 namespace json_spirit
39 {
40     const spirit_namespace::int_parser < int64_t >  int64_p  = spirit_namespace::int_parser < int64_t  >();
41     const spirit_namespace::uint_parser< uint64_t > uint64_p = spirit_namespace::uint_parser< uint64_t >();
42 
43     template< class Iter_type >
is_eq(Iter_type first,Iter_type last,const char * c_str)44     bool is_eq( Iter_type first, Iter_type last, const char* c_str )
45     {
46         for( Iter_type i = first; i != last; ++i, ++c_str )
47         {
48             if( *c_str == 0 ) return false;
49 
50             if( *i != *c_str ) return false;
51         }
52 
53         return true;
54     }
55 
56     template< class Char_type >
hex_to_num(const Char_type c)57     Char_type hex_to_num( const Char_type c )
58     {
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;
62         return 0;
63     }
64 
65     template< class Char_type, class Iter_type >
hex_str_to_char(Iter_type & begin)66     Char_type hex_str_to_char( Iter_type& begin )
67     {
68         const Char_type c1( *( ++begin ) );
69         const Char_type c2( *( ++begin ) );
70 
71         return ( hex_to_num( c1 ) << 4 ) + hex_to_num( c2 );
72     }
73 
74     template< class Char_type, class Iter_type >
unicode_str_to_char(Iter_type & begin)75     Char_type unicode_str_to_char( Iter_type& begin )
76     {
77         const Char_type c1( *( ++begin ) );
78         const Char_type c2( *( ++begin ) );
79         const Char_type c3( *( ++begin ) );
80         const Char_type c4( *( ++begin ) );
81 
82         return ( hex_to_num( c1 ) << 12 ) +
83                ( hex_to_num( c2 ) <<  8 ) +
84                ( hex_to_num( c3 ) <<  4 ) +
85                hex_to_num( c4 );
86     }
87 
88     template< class String_type >
append_esc_char_and_incr_iter(String_type & s,typename String_type::const_iterator & begin,typename String_type::const_iterator end)89     void append_esc_char_and_incr_iter( String_type& s,
90                                         typename String_type::const_iterator& begin,
91                                         typename String_type::const_iterator end )
92     {
93         typedef typename String_type::value_type Char_type;
94 
95         const Char_type c2( *begin );
96 
97         switch( c2 )
98         {
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;
107             case 'x':
108             {
109                 if( end - begin >= 3 )  //  expecting "xHH..."
110                 {
111                     s += hex_str_to_char< Char_type >( begin );
112                 }
113                 break;
114             }
115             case 'u':
116             {
117                 if( end - begin >= 5 )  //  expecting "uHHHH..."
118                 {
119                     s += unicode_str_to_char< Char_type >( begin );
120                 }
121                 break;
122             }
123         }
124     }
125 
126     template< class String_type >
substitute_esc_chars(typename String_type::const_iterator begin,typename String_type::const_iterator end)127     String_type substitute_esc_chars( typename String_type::const_iterator begin,
128                                    typename String_type::const_iterator end )
129     {
130         typedef typename String_type::const_iterator Iter_type;
131 
132         if( end - begin < 2 ) return String_type( begin, end );
133 
134         String_type result;
135 
136         result.reserve( end - begin );
137 
138         const Iter_type end_minus_1( end - 1 );
139 
140         Iter_type substr_start = begin;
141         Iter_type i = begin;
142 
143         for( ; i < end_minus_1; ++i )
144         {
145             if( *i == '\\' )
146             {
147                 result.append( substr_start, i );
148 
149                 ++i;  // skip the '\'
150 
151                 append_esc_char_and_incr_iter( result, i, end );
152 
153                 substr_start = i + 1;
154             }
155         }
156 
157         result.append( substr_start, end );
158 
159         return result;
160     }
161 
162     template< class String_type >
get_str_(typename String_type::const_iterator begin,typename String_type::const_iterator end)163     String_type get_str_( typename String_type::const_iterator begin,
164                        typename String_type::const_iterator end )
165     {
166         assert( end - begin >= 2 );
167 
168         typedef typename String_type::const_iterator Iter_type;
169 
170         Iter_type str_without_quotes( ++begin );
171         Iter_type end_without_quotes( --end );
172 
173         return substitute_esc_chars< String_type >( str_without_quotes, end_without_quotes );
174     }
175 
get_str(std::string::const_iterator begin,std::string::const_iterator end)176     inline std::string get_str( std::string::const_iterator begin, std::string::const_iterator end )
177     {
178         return get_str_< std::string >( begin, end );
179     }
180 
get_str(std::wstring::const_iterator begin,std::wstring::const_iterator end)181     inline std::wstring get_str( std::wstring::const_iterator begin, std::wstring::const_iterator end )
182     {
183         return get_str_< std::wstring >( begin, end );
184     }
185 
186     template< class String_type, class Iter_type >
get_str(Iter_type begin,Iter_type end)187     String_type get_str( Iter_type begin, Iter_type end )
188     {
189         const String_type tmp( begin, end );  // convert multipass iterators to string iterators
190 
191         return get_str( tmp.begin(), tmp.end() );
192     }
193 
194     // this class's methods get called by the spirit parse resulting
195     // in the creation of a JSON object or array
196     //
197     // NB Iter_type could be a std::string iterator, wstring iterator, a position iterator or a multipass iterator
198     //
199     template< class Value_type, class Iter_type >
200     class Semantic_actions
201     {
202     public:
203 
204         typedef typename Value_type::Config_type Config_type;
205         typedef typename Config_type::String_type String_type;
206         typedef typename Config_type::Object_type Object_type;
207         typedef typename Config_type::Array_type Array_type;
208         typedef typename String_type::value_type Char_type;
209 
Semantic_actions(Value_type & value)210         Semantic_actions( Value_type& value )
211         :   value_( value )
212         ,   current_p_( 0 )
213         {
214         }
215 
begin_obj(Char_type c)216         void begin_obj( Char_type c )
217         {
218             assert( c == '{' );
219 
220             begin_compound< Object_type >();
221         }
222 
end_obj(Char_type c)223         void end_obj( Char_type c )
224         {
225             assert( c == '}' );
226 
227             end_compound();
228         }
229 
begin_array(Char_type c)230         void begin_array( Char_type c )
231         {
232             assert( c == '[' );
233 
234             begin_compound< Array_type >();
235         }
236 
end_array(Char_type c)237         void end_array( Char_type c )
238         {
239             assert( c == ']' );
240 
241             end_compound();
242         }
243 
new_name(Iter_type begin,Iter_type end)244         void new_name( Iter_type begin, Iter_type end )
245         {
246             assert( current_p_->type() == obj_type );
247 
248             name_ = get_str< String_type >( begin, end );
249         }
250 
new_str(Iter_type begin,Iter_type end)251         void new_str( Iter_type begin, Iter_type end )
252         {
253             add_to_current( get_str< String_type >( begin, end ) );
254         }
255 
new_true(Iter_type begin,Iter_type end)256         void new_true( Iter_type begin, Iter_type end )
257         {
258             assert( is_eq( begin, end, "true" ) );
259 
260             add_to_current( true );
261         }
262 
new_false(Iter_type begin,Iter_type end)263         void new_false( Iter_type begin, Iter_type end )
264         {
265             assert( is_eq( begin, end, "false" ) );
266 
267             add_to_current( false );
268         }
269 
new_null(Iter_type begin,Iter_type end)270         void new_null( Iter_type begin, Iter_type end )
271         {
272             assert( is_eq( begin, end, "null" ) );
273 
274             add_to_current( Value_type() );
275         }
276 
new_int(int64_t i)277         void new_int( int64_t i )
278         {
279             add_to_current( i );
280         }
281 
new_uint64(uint64_t ui)282         void new_uint64( uint64_t ui )
283         {
284             add_to_current( ui );
285         }
286 
new_real(double d)287         void new_real( double d )
288         {
289             add_to_current( d );
290         }
291 
292     private:
293 
294         Semantic_actions& operator=( const Semantic_actions& );
295                                     // to prevent "assignment operator could not be generated" warning
296 
add_first(const Value_type & value)297         Value_type* add_first( const Value_type& value )
298         {
299             assert( current_p_ == 0 );
300 
301             value_ = value;
302             current_p_ = &value_;
303             return current_p_;
304         }
305 
306         template< class Array_or_obj >
begin_compound()307         void begin_compound()
308         {
309             if( current_p_ == 0 )
310             {
311                 add_first( Array_or_obj() );
312             }
313             else
314             {
315                 stack_.push_back( current_p_ );
316 
317                 Array_or_obj new_array_or_obj;   // avoid copy by building new array or object in place
318 
319                 current_p_ = add_to_current( new_array_or_obj );
320             }
321         }
322 
end_compound()323         void end_compound()
324         {
325             if( current_p_ != &value_ )
326             {
327                 current_p_ = stack_.back();
328 
329                 stack_.pop_back();
330             }
331         }
332 
add_to_current(const Value_type & value)333         Value_type* add_to_current( const Value_type& value )
334         {
335             if( current_p_ == 0 )
336             {
337                 return add_first( value );
338             }
339             else if( current_p_->type() == array_type )
340             {
341                 current_p_->get_array().push_back( value );
342 
343                 return &current_p_->get_array().back();
344             }
345 
346             assert( current_p_->type() == obj_type );
347 
348             return &Config_type::add( current_p_->get_obj(), name_, value );
349         }
350 
351         Value_type& value_;             // this is the object or array that is being created
352         Value_type* current_p_;         // the child object or array that is currently being constructed
353 
354         std::vector< Value_type* > stack_;   // previous child objects and arrays
355 
356         String_type name_;              // of current name/value pair
357     };
358 
359     template< typename Iter_type >
throw_error(spirit_namespace::position_iterator<Iter_type> i,const std::string & reason)360     void throw_error( spirit_namespace::position_iterator< Iter_type > i, const std::string& reason )
361     {
362         throw Error_position( i.get_position().line, i.get_position().column, reason );
363     }
364 
365     template< typename Iter_type >
throw_error(Iter_type i,const std::string & reason)366     void throw_error( Iter_type i, const std::string& reason )
367     {
368        throw reason;
369     }
370 
371     // the spirit grammer
372     //
373     template< class Value_type, class Iter_type >
374     class Json_grammer : public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >
375     {
376     public:
377 
378         typedef Semantic_actions< Value_type, Iter_type > Semantic_actions_t;
379 
Json_grammer(Semantic_actions_t & semantic_actions)380         Json_grammer( Semantic_actions_t& semantic_actions )
381         :   actions_( semantic_actions )
382         {
383         }
384 
throw_not_value(Iter_type begin,Iter_type end)385         static void throw_not_value( Iter_type begin, Iter_type end )
386         {
387     	    throw_error( begin, "not a value" );
388         }
389 
throw_not_array(Iter_type begin,Iter_type end)390         static void throw_not_array( Iter_type begin, Iter_type end )
391         {
392     	    throw_error( begin, "not an array" );
393         }
394 
throw_not_object(Iter_type begin,Iter_type end)395         static void throw_not_object( Iter_type begin, Iter_type end )
396         {
397     	    throw_error( begin, "not an object" );
398         }
399 
throw_not_pair(Iter_type begin,Iter_type end)400         static void throw_not_pair( Iter_type begin, Iter_type end )
401         {
402     	    throw_error( begin, "not a pair" );
403         }
404 
throw_not_colon(Iter_type begin,Iter_type end)405         static void throw_not_colon( Iter_type begin, Iter_type end )
406         {
407     	    throw_error( begin, "no colon in pair" );
408         }
409 
throw_not_string(Iter_type begin,Iter_type end)410         static void throw_not_string( Iter_type begin, Iter_type end )
411         {
412     	    throw_error( begin, "not a string" );
413         }
414 
415         template< typename ScannerT >
416         class definition
417         {
418         public:
419 
definition(const Json_grammer & self)420             definition( const Json_grammer& self )
421             {
422                 using namespace spirit_namespace;
423 
424                 typedef typename Value_type::String_type::value_type Char_type;
425 
426                 // first we convert the semantic action class methods to functors with the
427                 // parameter signature expected by spirit
428 
429                 typedef boost::function< void( Char_type )            > Char_action;
430                 typedef boost::function< void( Iter_type, Iter_type ) > Str_action;
431                 typedef boost::function< void( double )               > Real_action;
432                 typedef boost::function< void( boost::int64_t )       > Int_action;
433                 typedef boost::function< void( boost::uint64_t )      > Uint64_action;
434 
435                 Char_action   begin_obj  ( boost::bind( &Semantic_actions_t::begin_obj,   &self.actions_, _1 ) );
436                 Char_action   end_obj    ( boost::bind( &Semantic_actions_t::end_obj,     &self.actions_, _1 ) );
437                 Char_action   begin_array( boost::bind( &Semantic_actions_t::begin_array, &self.actions_, _1 ) );
438                 Char_action   end_array  ( boost::bind( &Semantic_actions_t::end_array,   &self.actions_, _1 ) );
439                 Str_action    new_name   ( boost::bind( &Semantic_actions_t::new_name,    &self.actions_, _1, _2 ) );
440                 Str_action    new_str    ( boost::bind( &Semantic_actions_t::new_str,     &self.actions_, _1, _2 ) );
441                 Str_action    new_true   ( boost::bind( &Semantic_actions_t::new_true,    &self.actions_, _1, _2 ) );
442                 Str_action    new_false  ( boost::bind( &Semantic_actions_t::new_false,   &self.actions_, _1, _2 ) );
443                 Str_action    new_null   ( boost::bind( &Semantic_actions_t::new_null,    &self.actions_, _1, _2 ) );
444                 Real_action   new_real   ( boost::bind( &Semantic_actions_t::new_real,    &self.actions_, _1 ) );
445                 Int_action    new_int    ( boost::bind( &Semantic_actions_t::new_int,     &self.actions_, _1 ) );
446                 Uint64_action new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64,  &self.actions_, _1 ) );
447 
448                 // actual grammer
449 
450                 json_
451                     = value_ | eps_p[ &throw_not_value ]
452                     ;
453 
454                 value_
455                     = string_[ new_str ]
456                     | number_
457                     | object_
458                     | array_
459                     | str_p( "true" ) [ new_true  ]
460                     | str_p( "false" )[ new_false ]
461                     | str_p( "null" ) [ new_null  ]
462                     ;
463 
464                 object_
465                     = ch_p('{')[ begin_obj ]
466                     >> !members_
467                     >> ( ch_p('}')[ end_obj ] | eps_p[ &throw_not_object ] )
468                     ;
469 
470                 members_
471                     = pair_ >> *( ',' >> pair_ )
472                     ;
473 
474                 pair_
475                     = string_[ new_name ]
476                     >> ( ':' | eps_p[ &throw_not_colon ] )
477                     >> ( value_ | eps_p[ &throw_not_value ] )
478                     ;
479 
480                 array_
481                     = ch_p('[')[ begin_array ]
482                     >> !elements_
483                     >> ( ch_p(']')[ end_array ] | eps_p[ &throw_not_array ] )
484                     ;
485 
486                 elements_
487                     = value_ >> *( ',' >> value_ )
488                     ;
489 
490                 string_
491                     = lexeme_d // this causes white space and what would appear to be comments inside a string to be retained
492                       [
493                           confix_p
494                           (
495                               '"',
496                               *lex_escape_ch_p,
497                               '"'
498                           )
499                       ]
500                     ;
501 
502                 number_
503                     = strict_real_p[ new_real   ]
504                     | int64_p      [ new_int    ]
505                     | uint64_p     [ new_uint64 ]
506                     ;
507             }
508 
509             spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_, value_, string_, number_;
510 
start()511             const spirit_namespace::rule< ScannerT >& start() const { return json_; }
512         };
513 
514     private:
515 
516         Json_grammer& operator=( const Json_grammer& ); // to prevent "assignment operator could not be generated" warning
517 
518         Semantic_actions_t& actions_;
519     };
520 
521     template< class Iter_type, class Value_type >
add_posn_iter_and_read_range_or_throw(Iter_type begin,Iter_type end,Value_type & value)522     void add_posn_iter_and_read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
523     {
524         typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;
525 
526         const Posn_iter_t posn_begin( begin, end );
527         const Posn_iter_t posn_end( end, end );
528 
529         read_range_or_throw( posn_begin, posn_end, value );
530     }
531 
532     template< class Istream_type >
533     struct Multi_pass_iters
534     {
535         typedef typename Istream_type::char_type Char_type;
536         typedef std::istream_iterator< Char_type, Char_type > istream_iter;
537         typedef spirit_namespace::multi_pass< istream_iter > Mp_iter;
538 
Multi_pass_itersMulti_pass_iters539         Multi_pass_iters( Istream_type& is )
540         {
541             is.unsetf( std::ios::skipws );
542 
543             begin_ = spirit_namespace::make_multi_pass( istream_iter( is ) );
544             end_   = spirit_namespace::make_multi_pass( istream_iter() );
545         }
546 
547         Mp_iter begin_;
548         Mp_iter end_;
549     };
550 
551     // reads a JSON Value from a pair of input iterators throwing an exception on invalid input, e.g.
552     //
553     // string::const_iterator start = str.begin();
554     // const string::const_iterator next = read_range_or_throw( str.begin(), str.end(), value );
555     //
556     // The iterator 'next' will point to the character past the
557     // last one read.
558     //
559     template< class Iter_type, class Value_type >
read_range_or_throw(Iter_type begin,Iter_type end,Value_type & value)560     Iter_type read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
561     {
562         Semantic_actions< Value_type, Iter_type > semantic_actions( value );
563 
564         const spirit_namespace::parse_info< Iter_type > info =
565                             spirit_namespace::parse( begin, end,
566                                                     Json_grammer< Value_type, Iter_type >( semantic_actions ),
567                                                     spirit_namespace::space_p |
568                                                     spirit_namespace::comment_p("//") |
569                                                     spirit_namespace::comment_p("/*", "*/") );
570 
571         if( !info.hit )
572         {
573             assert( false ); // in theory exception should already have been thrown
574             throw_error( info.stop, "error" );
575         }
576 
577         return info.stop;
578     }
579 
580     // reads a JSON Value from a pair of input iterators, e.g.
581     //
582     // string::const_iterator start = str.begin();
583     // const bool success = read_string( start, str.end(), value );
584     //
585     // The iterator 'start' will point to the character past the
586     // last one read.
587     //
588     template< class Iter_type, class Value_type >
read_range(Iter_type & begin,Iter_type end,Value_type & value)589     bool read_range( Iter_type& begin, Iter_type end, Value_type& value )
590     {
591         try
592         {
593             begin = read_range_or_throw( begin, end, value );
594 
595             return true;
596         }
597         catch( ... )
598         {
599             return false;
600         }
601     }
602 
603     // reads a JSON Value from a string, e.g.
604     //
605     // const bool success = read_string( str, value );
606     //
607     template< class String_type, class Value_type >
read_string(const String_type & s,Value_type & value)608     bool read_string( const String_type& s, Value_type& value )
609     {
610         typename String_type::const_iterator begin = s.begin();
611 
612         return read_range( begin, s.end(), value );
613     }
614 
615     // reads a JSON Value from a string throwing an exception on invalid input, e.g.
616     //
617     // read_string_or_throw( is, value );
618     //
619     template< class String_type, class Value_type >
read_string_or_throw(const String_type & s,Value_type & value)620     void read_string_or_throw( const String_type& s, Value_type& value )
621     {
622         add_posn_iter_and_read_range_or_throw( s.begin(), s.end(), value );
623     }
624 
625     // reads a JSON Value from a stream, e.g.
626     //
627     // const bool success = read_stream( is, value );
628     //
629     template< class Istream_type, class Value_type >
read_stream(Istream_type & is,Value_type & value)630     bool read_stream( Istream_type& is, Value_type& value )
631     {
632         Multi_pass_iters< Istream_type > mp_iters( is );
633 
634         return read_range( mp_iters.begin_, mp_iters.end_, value );
635     }
636 
637     // reads a JSON Value from a stream throwing an exception on invalid input, e.g.
638     //
639     // read_stream_or_throw( is, value );
640     //
641     template< class Istream_type, class Value_type >
read_stream_or_throw(Istream_type & is,Value_type & value)642     void read_stream_or_throw( Istream_type& is, Value_type& value )
643     {
644         const Multi_pass_iters< Istream_type > mp_iters( is );
645 
646         add_posn_iter_and_read_range_or_throw( mp_iters.begin_, mp_iters.end_, value );
647     }
648 }
649 
650 #endif
651