~paul-lucas/zorba/bug-932374

« back to all changes in this revision

Viewing changes to src/util/json_parser.cpp

  • Committer: Paul J. Lucas
  • Date: 2012-09-21 20:26:47 UTC
  • mfrom: (10819.2.235 zorba)
  • Revision ID: paul@lucasmail.org-20120921202647-fy9n4jduhrnljrnb
MergeĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
235
235
        t->loc_ = cur_loc_;
236
236
        parse_number( c, &t->value_ );
237
237
        return true;
238
 
      case 'f':
239
 
      case 'n':
240
 
      case 't':
 
238
      case 'f': // false
 
239
      case 'n': // null
 
240
      case 't': // true
241
241
        t->type_ = parse_literal( c, &t->value_ );
242
242
        t->loc_ = cur_loc_;
243
243
        return true;
259
259
unicode::code_point lexer::parse_codepoint() {
260
260
  static char const hex_digits[] = "0123456789ABCDEF";
261
261
 
 
262
  char c;
262
263
  zstring cp_string( "\\u" );           // needed only for error message
263
 
 
264
 
  unicode::code_point cp = 0;
265
 
  for ( int i = 1; i <= 4; ++i ) {
266
 
    char c;
267
 
    if ( !get_char( &c ) || !ascii::is_xdigit( c ) )
 
264
  unicode::code_point high_surrogate = 0;
 
265
 
 
266
  while ( true ) {
 
267
    unicode::code_point cp = 0;
 
268
    for ( int i = 1; i <= 4; ++i ) {
 
269
      if ( !get_char( &c ) )
 
270
        throw illegal_codepoint( cur_loc_, cp_string );
 
271
      cp_string += c;
 
272
      if ( !ascii::is_xdigit( c ) )
 
273
        throw illegal_codepoint( cur_loc_, cp_string );
 
274
      c = ascii::to_upper( c );
 
275
      char const *const p = std::strchr( hex_digits, c );
 
276
      assert( p );
 
277
      cp = (cp << 4) | (p - hex_digits);
 
278
    }
 
279
 
 
280
    if ( unicode::is_high_surrogate( cp ) ) {
 
281
      if ( high_surrogate )
 
282
        throw illegal_codepoint( cur_loc_, cp_string );
 
283
      //
 
284
      // It's easier to parse the \u for the low surrogate here rather than
 
285
      // trying to manage state in parse_string().
 
286
      //
 
287
      if ( !get_char( &c ) )
 
288
        throw illegal_codepoint( cur_loc_, cp_string );
 
289
      cp_string += c;
 
290
      if ( c != '\\' )
 
291
        throw illegal_codepoint( cur_loc_, cp_string );
 
292
      if ( !get_char( &c ) )
 
293
        throw illegal_codepoint( cur_loc_, cp_string );
 
294
      cp_string += c;
 
295
      if ( c != 'u' )
 
296
        throw illegal_codepoint( cur_loc_, cp_string );
 
297
 
 
298
      high_surrogate = cp;
 
299
      continue;
 
300
    }
 
301
    if ( unicode::is_low_surrogate( cp ) ) {
 
302
      if ( !high_surrogate )
 
303
        throw illegal_codepoint( cur_loc_, cp_string );
 
304
      return unicode::convert_surrogate( high_surrogate, cp );
 
305
    }
 
306
    if ( high_surrogate )
268
307
      throw illegal_codepoint( cur_loc_, cp_string );
269
 
    cp_string += c;
270
 
    c = ascii::to_upper( c );
271
 
    char const *const p = std::strchr( hex_digits, c );
272
 
    assert( p );
273
 
    cp = (cp << 4) | (p - hex_digits);
 
308
 
 
309
    return cp;
274
310
  }
275
 
  return cp;
276
311
}
277
312
 
278
313
token::type lexer::parse_literal( char first_c, token::value_type *value ) {