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