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