| 6 |
| 7 | #include <cmath> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 |
| 11 | #include "base/logging.h" |
| 12 | #include "base/macros.h" |
| 13 | #include "base/numerics/safe_conversions.h" |
| 14 | #include "base/strings/string_number_conversions.h" |
| 15 | #include "base/strings/string_piece.h" |
| 16 | #include "base/strings/string_util.h" |
| 17 | #include "base/strings/stringprintf.h" |
| 18 | #include "base/strings/utf_string_conversion_utils.h" |
| 19 | #include "base/strings/utf_string_conversions.h" |
| 20 | #include "base/third_party/icu/icu_utf.h" |
| 21 | #include "base/values.h" |
| 22 |
| 23 | namespace base { |
| 24 | namespace internal { |
| 25 |
| 26 | namespace { |
| 27 |
| 28 | const int32_t kExtendedASCIIStart = 0x80; |
| 29 | constexpr uint32_t kUnicodeReplacementPoint = 0xFFFD; |
| 30 |
| 31 | // UnprefixedHexStringToInt acts like |HexStringToInt|, but enforces that the |
| 32 | // input consists purely of hex digits. I.e. no "0x" nor "OX" prefix is |
| 33 | // permitted. |
| 34 | bool UnprefixedHexStringToInt(StringPiece input, int* output) { |
| 35 | for (size_t i = 0; i < input.size(); i++) { |
| 36 | if (!IsHexDigit(input[i])) { |
| 37 | return false; |
| 38 | } |
| 39 | } |
| 40 | return HexStringToInt(input, output); |
| 41 | } |
| 42 |
| 43 | } // namespace |
| 44 |
| 45 | // This is U+FFFD. |
| 46 | const char kUnicodeReplacementString[] = "\xEF\xBF\xBD"; |
| 47 |
| 48 | JSONParser::JSONParser(int options, size_t max_depth) |
| 49 | : options_(options), |
| 50 | max_depth_(max_depth), |
| 51 | index_(0), |
| 52 | stack_depth_(0), |
| 53 | line_number_(0), |
| 54 | index_last_line_(0), |
| 55 | error_code_(JSONReader::JSON_NO_ERROR), |
| 56 | error_line_(0), |
| 57 | error_column_(0) { |
| 58 | CHECK_LE(max_depth, kAbsoluteMaxDepth); |
| 59 | } |
| 60 |
| 61 | JSONParser::~JSONParser() = default; |
| 62 |
| 63 | Optional<Value> JSONParser::Parse(StringPiece input) { |
| 64 | input_ = input; |
| 65 | index_ = 0; |
| 66 | line_number_ = 1; |
| 67 | index_last_line_ = 0; |
| 68 |
| 69 | error_code_ = JSONReader::JSON_NO_ERROR; |
| 70 | error_line_ = 0; |
| 71 | error_column_ = 0; |
| 72 |
| 73 | // ICU and ReadUnicodeCharacter() use int32_t for lengths, so ensure |
| 74 | // that the index_ will not overflow when parsing. |
| 75 | if (!base::IsValueInRangeForNumericType<int32_t>(input.length())) { |
| 76 | ReportError(JSONReader::JSON_TOO_LARGE, 0); |
| 77 | return nullopt; |
| 78 | } |
| 79 |
| 80 | // When the input JSON string starts with a UTF-8 Byte-Order-Mark, |
| 81 | // advance the start position to avoid the ParseNextToken function mis- |
| 82 | // treating a Unicode BOM as an invalid character and returning NULL. |
| 83 | ConsumeIfMatch("\xEF\xBB\xBF"); |
| 84 |
| 85 | // Parse the first and any nested tokens. |
| 86 | Optional<Value> root(ParseNextToken()); |
| 87 | if (!root) |
| 88 | return nullopt; |
| 89 |
| 90 | // Make sure the input stream is at an end. |
| 91 | if (GetNextToken() != T_END_OF_INPUT) { |
| 92 | ReportError(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, 1); |
| 93 | return nullopt; |
| 94 | } |
| 95 |
| 96 | return root; |
| 97 | } |
| 98 |
| 99 | JSONReader::JsonParseError JSONParser::error_code() const { |
| 100 | return error_code_; |
| 101 | } |
| 102 |
| 103 | std::string JSONParser::GetErrorMessage() const { |
| 104 | return FormatErrorMessage(error_line_, error_column_, |
| 105 | JSONReader::ErrorCodeToString(error_code_)); |
| 106 | } |
| 107 |
| 108 | int JSONParser::error_line() const { |
| 109 | return error_line_; |
| 110 | } |
| 111 |
| 112 | int JSONParser::error_column() const { |
| 113 | return error_column_; |
| 114 | } |
| 115 |
| 116 | // StringBuilder /////////////////////////////////////////////////////////////// |
| 117 |
| 118 | JSONParser::StringBuilder::StringBuilder() : StringBuilder(nullptr) {} |
| 119 |
| 120 | JSONParser::StringBuilder::StringBuilder(const char* pos) |
| 121 | : pos_(pos), length_(0) {} |
| 122 |
| 123 | JSONParser::StringBuilder::~StringBuilder() = default; |
| 124 |
| 125 | JSONParser::StringBuilder& JSONParser::StringBuilder::operator=( |
| 126 | StringBuilder&& other) = default; |
| 127 |
| 128 | void JSONParser::StringBuilder::Append(uint32_t point) { |
| 129 | DCHECK(IsValidCodepoint(point)); |
| 130 |
| 131 | if (point < kExtendedASCIIStart && !string_) { |
| 132 | DCHECK_EQ(static_cast<char>(point), pos_[length_]); |
| 133 | ++length_; |
| 134 | } else { |
| 135 | Convert(); |
| 136 | if (UNLIKELY(point == kUnicodeReplacementPoint)) { |
| 137 | string_->append(kUnicodeReplacementString); |
| 138 | } else { |
| 139 | WriteUnicodeCharacter(point, &*string_); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 |
| 144 | void JSONParser::StringBuilder::Convert() { |
| 145 | if (string_) |
| 146 | return; |
| 147 | string_.emplace(pos_, length_); |
| 148 | } |
| 149 |
| 150 | std::string JSONParser::StringBuilder::DestructiveAsString() { |
| 151 | if (string_) |
| 152 | return std::move(*string_); |
| 153 | return std::string(pos_, length_); |
| 154 | } |
| 155 |
| 156 | // JSONParser private ////////////////////////////////////////////////////////// |
| 157 |
| 158 | Optional<StringPiece> JSONParser::PeekChars(size_t count) { |
| 159 | if (index_ + count > input_.length()) |
| 160 | return nullopt; |
| 161 | // Using StringPiece::substr() is significantly slower (according to |
| 162 | // base_perftests) than constructing a substring manually. |
| 163 | return StringPiece(input_.data() + index_, count); |
| 164 | } |
| 165 |
| 166 | Optional<char> JSONParser::PeekChar() { |
| 167 | Optional<StringPiece> chars = PeekChars(1); |
| 168 | if (chars) |
| 169 | return (*chars)[0]; |
| 170 | return nullopt; |
| 171 | } |
| 172 |
| 173 | Optional<StringPiece> JSONParser::ConsumeChars(size_t count) { |
| 174 | Optional<StringPiece> chars = PeekChars(count); |
| 175 | if (chars) |
| 176 | index_ += count; |
| 177 | return chars; |
| 178 | } |
| 179 |
| 180 | Optional<char> JSONParser::ConsumeChar() { |
| 181 | Optional<StringPiece> chars = ConsumeChars(1); |
| 182 | if (chars) |
| 183 | return (*chars)[0]; |
| 184 | return nullopt; |
| 185 | } |
| 186 |
| 187 | const char* JSONParser::pos() { |
| 188 | CHECK_LE(static_cast<size_t>(index_), input_.length()); |
| 189 | return input_.data() + index_; |
| 190 | } |
| 191 |
| 192 | JSONParser::Token JSONParser::GetNextToken() { |
| 193 | EatWhitespaceAndComments(); |
| 194 |
| 195 | Optional<char> c = PeekChar(); |
| 196 | if (!c) |
| 197 | return T_END_OF_INPUT; |
| 198 |
| 199 | switch (*c) { |
| 200 | case '{': |
| 201 | return T_OBJECT_BEGIN; |
| 202 | case '}': |
| 203 | return T_OBJECT_END; |
| 204 | case '[': |
| 205 | return T_ARRAY_BEGIN; |
| 206 | case ']': |
| 207 | return T_ARRAY_END; |
| 208 | case '"': |
| 209 | return T_STRING; |
| 210 | case '0': |
| 211 | case '1': |
| 212 | case '2': |
| 213 | case '3': |
| 214 | case '4': |
| 215 | case '5': |
| 216 | case '6': |
| 217 | case '7': |
| 218 | case '8': |
| 219 | case '9': |
| 220 | case '-': |
| 221 | return T_NUMBER; |
| 222 | case 't': |
| 223 | return T_BOOL_TRUE; |
| 224 | case 'f': |
| 225 | return T_BOOL_FALSE; |
| 226 | case 'n': |
| 227 | return T_NULL; |
| 228 | case ',': |
| 229 | return T_LIST_SEPARATOR; |
| 230 | case ':': |
| 231 | return T_OBJECT_PAIR_SEPARATOR; |
| 232 | default: |
| 233 | return T_INVALID_TOKEN; |
| 234 | } |
| 235 | } |
| 236 |
| 237 | void JSONParser::EatWhitespaceAndComments() { |
| 238 | while (Optional<char> c = PeekChar()) { |
| 239 | switch (*c) { |
| 240 | case '\r': |
| 241 | case '\n': |
| 242 | index_last_line_ = index_; |
| 243 | // Don't increment line_number_ twice for "\r\n". |
| 244 | if (!(c == '\n' && index_ > 0 && input_[index_ - 1] == '\r')) { |
| 245 | ++line_number_; |
| 246 | } |
| 247 | FALLTHROUGH; |
| 248 | case ' ': |
| 249 | case '\t': |
| 250 | ConsumeChar(); |
| 251 | break; |
| 252 | case '/': |
| 253 | if (!EatComment()) |
| 254 | return; |
| 255 | break; |
| 256 | default: |
| 257 | return; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 |
| 262 | bool JSONParser::EatComment() { |
| 263 | Optional<StringPiece> comment_start = PeekChars(2); |
| 264 | if (!comment_start) |
| 265 | return false; |
| 266 |
| 267 | if (comment_start == "//") { |
| 268 | ConsumeChars(2); |
| 269 | // Single line comment, read to newline. |
| 270 | while (Optional<char> c = PeekChar()) { |
| 271 | if (c == '\n' || c == '\r') |
| 272 | return true; |
| 273 | ConsumeChar(); |
| 274 | } |
| 275 | } else if (comment_start == "/*") { |
| 276 | ConsumeChars(2); |
| 277 | char previous_char = '\0'; |
| 278 | // Block comment, read until end marker. |
| 279 | while (Optional<char> c = PeekChar()) { |
| 280 | if (previous_char == '*' && c == '/') { |
| 281 | // EatWhitespaceAndComments will inspect pos(), which will still be on |
| 282 | // the last / of the comment, so advance once more (which may also be |
| 283 | // end of input). |
| 284 | ConsumeChar(); |
| 285 | return true; |
| 286 | } |
| 287 | previous_char = *ConsumeChar(); |
| 288 | } |
| 289 |
| 290 | // If the comment is unterminated, GetNextToken will report T_END_OF_INPUT. |
| 291 | } |
| 292 |
| 293 | return false; |
| 294 | } |
| 295 |
| 296 | Optional<Value> JSONParser::ParseNextToken() { |
| 297 | return ParseToken(GetNextToken()); |
| 298 | } |
| 299 |
| 300 | Optional<Value> JSONParser::ParseToken(Token token) { |
| 301 | switch (token) { |
| 302 | case T_OBJECT_BEGIN: |
| 303 | return ConsumeDictionary(); |
| 304 | case T_ARRAY_BEGIN: |
| 305 | return ConsumeList(); |
| 306 | case T_STRING: |
| 307 | return ConsumeString(); |
| 308 | case T_NUMBER: |
| 309 | return ConsumeNumber(); |
| 310 | case T_BOOL_TRUE: |
| 311 | case T_BOOL_FALSE: |
| 312 | case T_NULL: |
| 313 | return ConsumeLiteral(); |
| 314 | default: |
| 315 | ReportError(JSONReader::JSON_UNEXPECTED_TOKEN, 1); |
| 316 | return nullopt; |
| 317 | } |
| 318 | } |
| 319 |
| 320 | Optional<Value> JSONParser::ConsumeDictionary() { |
| 321 | if (ConsumeChar() != '{') { |
| 322 | ReportError(JSONReader::JSON_UNEXPECTED_TOKEN, 1); |
| 323 | return nullopt; |
| 324 | } |
| 325 |
| 326 | StackMarker depth_check(max_depth_, &stack_depth_); |
| 327 | if (depth_check.IsTooDeep()) { |
| 328 | ReportError(JSONReader::JSON_TOO_MUCH_NESTING, 0); |
| 329 | return nullopt; |
| 330 | } |
| 331 |
| 332 | std::vector<Value::DictStorage::value_type> dict_storage; |
| 333 |
| 334 | Token token = GetNextToken(); |
| 335 | while (token != T_OBJECT_END) { |
| 336 | if (token != T_STRING) { |
| 337 | ReportError(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, 1); |
| 338 | return nullopt; |
| 339 | } |
| 340 |
| 341 | // First consume the key. |
| 342 | StringBuilder key; |
| 343 | if (!ConsumeStringRaw(&key)) { |
| 344 | return nullopt; |
| 345 | } |
| 346 |
| 347 | // Read the separator. |
| 348 | token = GetNextToken(); |
| 349 | if (token != T_OBJECT_PAIR_SEPARATOR) { |
| 350 | ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 351 | return nullopt; |
| 352 | } |
| 353 |
| 354 | // The next token is the value. Ownership transfers to |dict|. |
| 355 | ConsumeChar(); |
| 356 | Optional<Value> value = ParseNextToken(); |
| 357 | if (!value) { |
| 358 | // ReportError from deeper level. |
| 359 | return nullopt; |
| 360 | } |
| 361 |
| 362 | dict_storage.emplace_back(key.DestructiveAsString(), |
| 363 | std::make_unique<Value>(std::move(*value))); |
| 364 |
| 365 | token = GetNextToken(); |
| 366 | if (token == T_LIST_SEPARATOR) { |
| 367 | ConsumeChar(); |
| 368 | token = GetNextToken(); |
| 369 | if (token == T_OBJECT_END && !(options_ & JSON_ALLOW_TRAILING_COMMAS)) { |
| 370 | ReportError(JSONReader::JSON_TRAILING_COMMA, 1); |
| 371 | return nullopt; |
| 372 | } |
| 373 | } else if (token != T_OBJECT_END) { |
| 374 | ReportError(JSONReader::JSON_SYNTAX_ERROR, 0); |
| 375 | return nullopt; |
| 376 | } |
| 377 | } |
| 378 |
| 379 | ConsumeChar(); // Closing '}'. |
| 380 | // Reverse |dict_storage| to keep the last of elements with the same key in |
| 381 | // the input. |
| 382 | std::reverse(dict_storage.begin(), dict_storage.end()); |
| 383 | return Value(Value::DictStorage(std::move(dict_storage))); |
| 384 | } |
| 385 |
| 386 | Optional<Value> JSONParser::ConsumeList() { |
| 387 | if (ConsumeChar() != '[') { |
| 388 | ReportError(JSONReader::JSON_UNEXPECTED_TOKEN, 1); |
| 389 | return nullopt; |
| 390 | } |
| 391 |
| 392 | StackMarker depth_check(max_depth_, &stack_depth_); |
| 393 | if (depth_check.IsTooDeep()) { |
| 394 | ReportError(JSONReader::JSON_TOO_MUCH_NESTING, 0); |
| 395 | return nullopt; |
| 396 | } |
| 397 |
| 398 | Value::ListStorage list_storage; |
| 399 |
| 400 | Token token = GetNextToken(); |
| 401 | while (token != T_ARRAY_END) { |
| 402 | Optional<Value> item = ParseToken(token); |
| 403 | if (!item) { |
| 404 | // ReportError from deeper level. |
| 405 | return nullopt; |
| 406 | } |
| 407 |
| 408 | list_storage.push_back(std::move(*item)); |
| 409 |
| 410 | token = GetNextToken(); |
| 411 | if (token == T_LIST_SEPARATOR) { |
| 412 | ConsumeChar(); |
| 413 | token = GetNextToken(); |
| 414 | if (token == T_ARRAY_END && !(options_ & JSON_ALLOW_TRAILING_COMMAS)) { |
| 415 | ReportError(JSONReader::JSON_TRAILING_COMMA, 1); |
| 416 | return nullopt; |
| 417 | } |
| 418 | } else if (token != T_ARRAY_END) { |
| 419 | ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 420 | return nullopt; |
| 421 | } |
| 422 | } |
| 423 |
| 424 | ConsumeChar(); // Closing ']'. |
| 425 |
| 426 | return Value(std::move(list_storage)); |
| 427 | } |
| 428 |
| 429 | Optional<Value> JSONParser::ConsumeString() { |
| 430 | StringBuilder string; |
| 431 | if (!ConsumeStringRaw(&string)) |
| 432 | return nullopt; |
| 433 | return Value(string.DestructiveAsString()); |
| 434 | } |
| 435 |
| 436 | bool JSONParser::ConsumeStringRaw(StringBuilder* out) { |
| 437 | if (ConsumeChar() != '"') { |
| 438 | ReportError(JSONReader::JSON_UNEXPECTED_TOKEN, 1); |
| 439 | return false; |
| 440 | } |
| 441 |
| 442 | // StringBuilder will internally build a StringPiece unless a UTF-16 |
| 443 | // conversion occurs, at which point it will perform a copy into a |
| 444 | // std::string. |
| 445 | StringBuilder string(pos()); |
| 446 |
| 447 | while (PeekChar()) { |
| 448 | uint32_t next_char = 0; |
| 449 | if (!ReadUnicodeCharacter(input_.data(), |
| 450 | static_cast<int32_t>(input_.length()), &index_, |
| 451 | &next_char) || |
| 452 | !IsValidCodepoint(next_char)) { |
| 453 | if ((options_ & JSON_REPLACE_INVALID_CHARACTERS) == 0) { |
| 454 | ReportError(JSONReader::JSON_UNSUPPORTED_ENCODING, 1); |
| 455 | return false; |
| 456 | } |
| 457 | ConsumeChar(); |
| 458 | string.Append(kUnicodeReplacementPoint); |
| 459 | continue; |
| 460 | } |
| 461 |
| 462 | if (next_char == '"') { |
| 463 | ConsumeChar(); |
| 464 | *out = std::move(string); |
| 465 | return true; |
| 466 | } |
| 467 | if (next_char != '\\') { |
| 468 | // If this character is not an escape sequence... |
| 469 | ConsumeChar(); |
| 470 | string.Append(next_char); |
| 471 | } else { |
| 472 | // And if it is an escape sequence, the input string will be adjusted |
| 473 | // (either by combining the two characters of an encoded escape sequence, |
| 474 | // or with a UTF conversion), so using StringPiece isn't possible -- force |
| 475 | // a conversion. |
| 476 | string.Convert(); |
| 477 |
| 478 | // Read past the escape '\' and ensure there's a character following. |
| 479 | Optional<StringPiece> escape_sequence = ConsumeChars(2); |
| 480 | if (!escape_sequence) { |
| 481 | ReportError(JSONReader::JSON_INVALID_ESCAPE, 0); |
| 482 | return false; |
| 483 | } |
| 484 |
| 485 | switch ((*escape_sequence)[1]) { |
| 486 | // Allowed esape sequences: |
| 487 | case 'x': { // UTF-8 sequence. |
| 488 | // UTF-8 \x escape sequences are not allowed in the spec, but they |
| 489 | // are supported here for backwards-compatiblity with the old parser. |
| 490 | escape_sequence = ConsumeChars(2); |
| 491 | if (!escape_sequence) { |
| 492 | ReportError(JSONReader::JSON_INVALID_ESCAPE, -2); |
| 493 | return false; |
| 494 | } |
| 495 |
| 496 | int hex_digit = 0; |
| 497 | if (!UnprefixedHexStringToInt(*escape_sequence, &hex_digit) || |
| 498 | !IsValidCharacter(hex_digit)) { |
| 499 | ReportError(JSONReader::JSON_INVALID_ESCAPE, -2); |
| 500 | return false; |
| 501 | } |
| 502 |
| 503 | string.Append(hex_digit); |
| 504 | break; |
| 505 | } |
| 506 | case 'u': { // UTF-16 sequence. |
| 507 | // UTF units are of the form \uXXXX. |
| 508 | uint32_t code_point; |
| 509 | if (!DecodeUTF16(&code_point)) { |
| 510 | ReportError(JSONReader::JSON_INVALID_ESCAPE, 0); |
| 511 | return false; |
| 512 | } |
| 513 | string.Append(code_point); |
| 514 | break; |
| 515 | } |
| 516 | case '"': |
| 517 | string.Append('"'); |
| 518 | break; |
| 519 | case '\\': |
| 520 | string.Append('\\'); |
| 521 | break; |
| 522 | case '/': |
| 523 | string.Append('/'); |
| 524 | break; |
| 525 | case 'b': |
| 526 | string.Append('\b'); |
| 527 | break; |
| 528 | case 'f': |
| 529 | string.Append('\f'); |
| 530 | break; |
| 531 | case 'n': |
| 532 | string.Append('\n'); |
| 533 | break; |
| 534 | case 'r': |
| 535 | string.Append('\r'); |
| 536 | break; |
| 537 | case 't': |
| 538 | string.Append('\t'); |
| 539 | break; |
| 540 | case 'v': // Not listed as valid escape sequence in the RFC. |
| 541 | string.Append('\v'); |
| 542 | break; |
| 543 | // All other escape squences are illegal. |
| 544 | default: |
| 545 | ReportError(JSONReader::JSON_INVALID_ESCAPE, 0); |
| 546 | return false; |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 |
| 551 | ReportError(JSONReader::JSON_SYNTAX_ERROR, 0); |
| 552 | return false; |
| 553 | } |
| 554 |
| 555 | // Entry is at the first X in \uXXXX. |
| 556 | bool JSONParser::DecodeUTF16(uint32_t* out_code_point) { |
| 557 | Optional<StringPiece> escape_sequence = ConsumeChars(4); |
| 558 | if (!escape_sequence) |
| 559 | return false; |
| 560 |
| 561 | // Consume the UTF-16 code unit, which may be a high surrogate. |
| 562 | int code_unit16_high = 0; |
| 563 | if (!UnprefixedHexStringToInt(*escape_sequence, &code_unit16_high)) |
| 564 | return false; |
| 565 |
| 566 | // If this is a high surrogate, consume the next code unit to get the |
| 567 | // low surrogate. |
| 568 | if (CBU16_IS_SURROGATE(code_unit16_high)) { |
| 569 | // Make sure this is the high surrogate. If not, it's an encoding |
| 570 | // error. |
| 571 | if (!CBU16_IS_SURROGATE_LEAD(code_unit16_high)) |
| 572 | return false; |
| 573 |
| 574 | // Make sure that the token has more characters to consume the |
| 575 | // lower surrogate. |
| 576 | if (!ConsumeIfMatch("\\u")) { |
| 577 | if ((options_ & JSON_REPLACE_INVALID_CHARACTERS) == 0) |
| 578 | return false; |
| 579 | *out_code_point = kUnicodeReplacementPoint; |
| 580 | return true; |
| 581 | } |
| 582 |
| 583 | escape_sequence = ConsumeChars(4); |
| 584 | if (!escape_sequence) |
| 585 | return false; |
| 586 |
| 587 | int code_unit16_low = 0; |
| 588 | if (!UnprefixedHexStringToInt(*escape_sequence, &code_unit16_low)) |
| 589 | return false; |
| 590 |
| 591 | if (!CBU16_IS_TRAIL(code_unit16_low)) { |
| 592 | if ((options_ & JSON_REPLACE_INVALID_CHARACTERS) == 0) |
| 593 | return false; |
| 594 | *out_code_point = kUnicodeReplacementPoint; |
| 595 | return true; |
| 596 | } |
| 597 |
| 598 | uint32_t code_point = |
| 599 | CBU16_GET_SUPPLEMENTARY(code_unit16_high, code_unit16_low); |
| 600 |
| 601 | *out_code_point = code_point; |
| 602 | } else { |
| 603 | // Not a surrogate. |
| 604 | DCHECK(CBU16_IS_SINGLE(code_unit16_high)); |
| 605 |
| 606 | *out_code_point = code_unit16_high; |
| 607 | } |
| 608 |
| 609 | return true; |
| 610 | } |
| 611 |
| 612 | Optional<Value> JSONParser::ConsumeNumber() { |
| 613 | const char* num_start = pos(); |
| 614 | const int start_index = index_; |
| 615 | int end_index = start_index; |
| 616 |
| 617 | if (PeekChar() == '-') |
| 618 | ConsumeChar(); |
| 619 |
| 620 | if (!ReadInt(false)) { |
| 621 | ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 622 | return nullopt; |
| 623 | } |
| 624 | end_index = index_; |
| 625 |
| 626 | // The optional fraction part. |
| 627 | if (PeekChar() == '.') { |
| 628 | ConsumeChar(); |
| 629 | if (!ReadInt(true)) { |
| 630 | ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 631 | return nullopt; |
| 632 | } |
| 633 | end_index = index_; |
| 634 | } |
| 635 |
| 636 | // Optional exponent part. |
| 637 | Optional<char> c = PeekChar(); |
| 638 | if (c == 'e' || c == 'E') { |
| 639 | ConsumeChar(); |
| 640 | if (PeekChar() == '-' || PeekChar() == '+') { |
| 641 | ConsumeChar(); |
| 642 | } |
| 643 | if (!ReadInt(true)) { |
| 644 | ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 645 | return nullopt; |
| 646 | } |
| 647 | end_index = index_; |
| 648 | } |
| 649 |
| 650 | // ReadInt is greedy because numbers have no easily detectable sentinel, |
| 651 | // so save off where the parser should be on exit (see Consume invariant at |
| 652 | // the top of the header), then make sure the next token is one which is |
| 653 | // valid. |
| 654 | int exit_index = index_; |
| 655 |
| 656 | switch (GetNextToken()) { |
| 657 | case T_OBJECT_END: |
| 658 | case T_ARRAY_END: |
| 659 | case T_LIST_SEPARATOR: |
| 660 | case T_END_OF_INPUT: |
| 661 | break; |
| 662 | default: |
| 663 | ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 664 | return nullopt; |
| 665 | } |
| 666 |
| 667 | index_ = exit_index; |
| 668 |
| 669 | StringPiece num_string(num_start, end_index - start_index); |
| 670 |
| 671 | int num_int; |
| 672 | if (StringToInt(num_string, &num_int)) |
| 673 | return Value(num_int); |
| 674 |
| 675 | double num_double; |
| 676 | if (StringToDouble(num_string.as_string(), &num_double) && |
| 677 | std::isfinite(num_double)) { |
| 678 | return Value(num_double); |
| 679 | } |
| 680 |
| 681 | ReportError(JSONReader::JSON_UNREPRESENTABLE_NUMBER, 1); |
| 682 | return nullopt; |
| 683 | } |
| 684 |
| 685 | bool JSONParser::ReadInt(bool allow_leading_zeros) { |
| 686 | size_t len = 0; |
| 687 | char first = 0; |
| 688 |
| 689 | while (Optional<char> c = PeekChar()) { |
| 690 | if (!IsAsciiDigit(c)) |
| 691 | break; |
| 692 |
| 693 | if (len == 0) |
| 694 | first = *c; |
| 695 |
| 696 | ++len; |
| 697 | ConsumeChar(); |
| 698 | } |
| 699 |
| 700 | if (len == 0) |
| 701 | return false; |
| 702 |
| 703 | if (!allow_leading_zeros && len > 1 && first == '0') |
| 704 | return false; |
| 705 |
| 706 | return true; |
| 707 | } |
| 708 |
| 709 | Optional<Value> JSONParser::ConsumeLiteral() { |
| 710 | if (ConsumeIfMatch("true")) |
| 711 | return Value(true); |
| 712 | if (ConsumeIfMatch("false")) |
| 713 | return Value(false); |
| 714 | if (ConsumeIfMatch("null")) |
| 715 | return Value(Value::Type::NONE); |
| 716 | ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 717 | return nullopt; |
| 718 | } |
| 719 |
| 720 | bool JSONParser::ConsumeIfMatch(StringPiece match) { |
| 721 | if (match == PeekChars(match.size())) { |
| 722 | ConsumeChars(match.size()); |
| 723 | return true; |
| 724 | } |
| 725 | return false; |
| 726 | } |
| 727 |
| 728 | void JSONParser::ReportError(JSONReader::JsonParseError code, |
| 729 | int column_adjust) { |
| 730 | error_code_ = code; |
| 731 | error_line_ = line_number_; |
| 732 | error_column_ = index_ - index_last_line_ + column_adjust; |
| 733 | } |
| 734 |
| 735 | // static |
| 736 | std::string JSONParser::FormatErrorMessage(int line, int column, |
| 737 | const std::string& description) { |
| 738 | if (line || column) { |
| 739 | return StringPrintf("Line: %i, column: %i, %s", |
| 740 | line, column, description.c_str()); |
| 741 | } |
| 742 | return description; |
| 743 | } |
| 744 |
| 745 | } // namespace internal |
| 746 | } // namespace base |
| 747 |