diff options
author | dim <dim@FreeBSD.org> | 2017-04-02 17:24:58 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2017-04-02 17:24:58 +0000 |
commit | 60b571e49a90d38697b3aca23020d9da42fc7d7f (patch) | |
tree | 99351324c24d6cb146b6285b6caffa4d26fce188 /contrib/llvm/tools/lldb/source/Utility/JSON.cpp | |
parent | bea1b22c7a9bce1dfdd73e6e5b65bc4752215180 (diff) | |
download | FreeBSD-src-60b571e49a90d38697b3aca23020d9da42fc7d7f.zip FreeBSD-src-60b571e49a90d38697b3aca23020d9da42fc7d7f.tar.gz |
Update clang, llvm, lld, lldb, compiler-rt and libc++ to 4.0.0 release:
MFC r309142 (by emaste):
Add WITH_LLD_AS_LD build knob
If set it installs LLD as /usr/bin/ld. LLD (as of version 3.9) is not
capable of linking the world and kernel, but can self-host and link many
substantial applications. GNU ld continues to be used for the world and
kernel build, regardless of how this knob is set.
It is on by default for arm64, and off for all other CPU architectures.
Sponsored by: The FreeBSD Foundation
MFC r310840:
Reapply 310775, now it also builds correctly if lldb is disabled:
Move llvm-objdump from CLANG_EXTRAS to installed by default
We currently install three tools from binutils 2.17.50: as, ld, and
objdump. Work is underway to migrate to a permissively-licensed
tool-chain, with one goal being the retirement of binutils 2.17.50.
LLVM's llvm-objdump is intended to be compatible with GNU objdump
although it is currently missing some options and may have formatting
differences. Enable it by default for testing and further investigation.
It may later be changed to install as /usr/bin/objdump, it becomes a
fully viable replacement.
Reviewed by: emaste
Differential Revision: https://reviews.freebsd.org/D8879
MFC r312855 (by emaste):
Rename LLD_AS_LD to LLD_IS_LD, for consistency with CLANG_IS_CC
Reported by: Dan McGregor <dan.mcgregor usask.ca>
MFC r313559 | glebius | 2017-02-10 18:34:48 +0100 (Fri, 10 Feb 2017) | 5 lines
Don't check struct rtentry on FreeBSD, it is an internal kernel structure.
On other systems it may be API structure for SIOCADDRT/SIOCDELRT.
Reviewed by: emaste, dim
MFC r314152 (by jkim):
Remove an assembler flag, which is redundant since r309124. The upstream
took care of it by introducing a macro NO_EXEC_STACK_DIRECTIVE.
http://llvm.org/viewvc/llvm-project?rev=273500&view=rev
Reviewed by: dim
MFC r314564:
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
4.0.0 (branches/release_40 296509). The release will follow soon.
Please note that from 3.5.0 onwards, clang, llvm and lldb require C++11
support to build; see UPDATING for more information.
Also note that as of 4.0.0, lld should be able to link the base system
on amd64 and aarch64. See the WITH_LLD_IS_LLD setting in src.conf(5).
Though please be aware that this is work in progress.
Release notes for llvm, clang and lld will be available here:
<http://releases.llvm.org/4.0.0/docs/ReleaseNotes.html>
<http://releases.llvm.org/4.0.0/tools/clang/docs/ReleaseNotes.html>
<http://releases.llvm.org/4.0.0/tools/lld/docs/ReleaseNotes.html>
Thanks to Ed Maste, Jan Beich, Antoine Brodin and Eric Fiselier for
their help.
Relnotes: yes
Exp-run: antoine
PR: 215969, 216008
MFC r314708:
For now, revert r287232 from upstream llvm trunk (by Daniil Fukalov):
[SCEV] limit recursion depth of CompareSCEVComplexity
Summary:
CompareSCEVComplexity goes too deep (50+ on a quite a big unrolled
loop) and runs almost infinite time.
Added cache of "equal" SCEV pairs to earlier cutoff of further
estimation. Recursion depth limit was also introduced as a parameter.
Reviewers: sanjoy
Subscribers: mzolotukhin, tstellarAMD, llvm-commits
Differential Revision: https://reviews.llvm.org/D26389
This commit is the cause of excessive compile times on skein_block.c
(and possibly other files) during kernel builds on amd64.
We never saw the problematic behavior described in this upstream commit,
so for now it is better to revert it. An upstream bug has been filed
here: https://bugs.llvm.org/show_bug.cgi?id=32142
Reported by: mjg
MFC r314795:
Reapply r287232 from upstream llvm trunk (by Daniil Fukalov):
[SCEV] limit recursion depth of CompareSCEVComplexity
Summary:
CompareSCEVComplexity goes too deep (50+ on a quite a big unrolled
loop) and runs almost infinite time.
Added cache of "equal" SCEV pairs to earlier cutoff of further
estimation. Recursion depth limit was also introduced as a parameter.
Reviewers: sanjoy
Subscribers: mzolotukhin, tstellarAMD, llvm-commits
Differential Revision: https://reviews.llvm.org/D26389
Pull in r296992 from upstream llvm trunk (by Sanjoy Das):
[SCEV] Decrease the recursion threshold for CompareValueComplexity
Fixes PR32142.
r287232 accidentally increased the recursion threshold for
CompareValueComplexity from 2 to 32. This change reverses that
change by introducing a separate flag for CompareValueComplexity's
threshold.
The latter revision fixes the excessive compile times for skein_block.c.
MFC r314907 | mmel | 2017-03-08 12:40:27 +0100 (Wed, 08 Mar 2017) | 7 lines
Unbreak ARMv6 world.
The new compiler_rt library imported with clang 4.0.0 have several fatal
issues (non-functional __udivsi3 for example) with ARM specific instrict
functions. As temporary workaround, until upstream solve these problems,
disable all thumb[1][2] related feature.
MFC r315016:
Update clang, llvm, lld, lldb, compiler-rt and libc++ to 4.0.0 release.
We were already very close to the last release candidate, so this is a
pretty minor update.
Relnotes: yes
MFC r316005:
Revert r314907, and pull in r298713 from upstream compiler-rt trunk (by
Weiming Zhao):
builtins: Select correct code fragments when compiling for Thumb1/Thum2/ARM ISA.
Summary:
Value of __ARM_ARCH_ISA_THUMB isn't based on the actual compilation
mode (-mthumb, -marm), it reflect's capability of given CPU.
Due to this:
- use __tbumb__ and __thumb2__ insteand of __ARM_ARCH_ISA_THUMB
- use '.thumb' directive consistently in all affected files
- decorate all thumb functions using
DEFINE_COMPILERRT_THUMB_FUNCTION()
---------
Note: This patch doesn't fix broken Thumb1 variant of __udivsi3 !
Reviewers: weimingz, rengolin, compnerd
Subscribers: aemerson, dim
Differential Revision: https://reviews.llvm.org/D30938
Discussed with: mmel
Diffstat (limited to 'contrib/llvm/tools/lldb/source/Utility/JSON.cpp')
-rw-r--r-- | contrib/llvm/tools/lldb/source/Utility/JSON.cpp | 1083 |
1 files changed, 470 insertions, 613 deletions
diff --git a/contrib/llvm/tools/lldb/source/Utility/JSON.cpp b/contrib/llvm/tools/lldb/source/Utility/JSON.cpp index 7ad3232..5b809c5 100644 --- a/contrib/llvm/tools/lldb/source/Utility/JSON.cpp +++ b/contrib/llvm/tools/lldb/source/Utility/JSON.cpp @@ -9,685 +9,542 @@ #include "lldb/Utility/JSON.h" -#include <limits.h> #include "lldb/Core/StreamString.h" #include "lldb/Host/StringConvert.h" #include "llvm/Support/ErrorHandling.h" +#include <limits.h> using namespace lldb_private; -std::string -JSONString::json_string_quote_metachars (const std::string &s) -{ - if (s.find('"') == std::string::npos) - return s; - - std::string output; - const size_t s_size = s.size(); - const char *s_chars = s.c_str(); - for (size_t i = 0; i < s_size; i++) - { - unsigned char ch = *(s_chars + i); - if (ch == '"') - { - output.push_back ('\\'); - } - output.push_back (ch); +std::string JSONString::json_string_quote_metachars(const std::string &s) { + if (s.find('"') == std::string::npos) + return s; + + std::string output; + const size_t s_size = s.size(); + const char *s_chars = s.c_str(); + for (size_t i = 0; i < s_size; i++) { + unsigned char ch = *(s_chars + i); + if (ch == '"') { + output.push_back('\\'); } - return output; + output.push_back(ch); + } + return output; } -JSONString::JSONString () : - JSONValue(JSONValue::Kind::String), - m_data() -{ -} +JSONString::JSONString() : JSONValue(JSONValue::Kind::String), m_data() {} -JSONString::JSONString (const char* s) : - JSONValue(JSONValue::Kind::String), - m_data(s ? s : "") -{ -} +JSONString::JSONString(const char *s) + : JSONValue(JSONValue::Kind::String), m_data(s ? s : "") {} -JSONString::JSONString (const std::string& s) : - JSONValue(JSONValue::Kind::String), - m_data(s) -{ -} +JSONString::JSONString(const std::string &s) + : JSONValue(JSONValue::Kind::String), m_data(s) {} -void -JSONString::Write (Stream& s) -{ - s.Printf("\"%s\"", json_string_quote_metachars(m_data).c_str()); +void JSONString::Write(Stream &s) { + s.Printf("\"%s\"", json_string_quote_metachars(m_data).c_str()); } -uint64_t -JSONNumber::GetAsUnsigned() const -{ - switch (m_data_type) - { - case DataType::Unsigned: - return m_data.m_unsigned; - case DataType::Signed: - return (uint64_t)m_data.m_signed; - case DataType::Double: - return (uint64_t)m_data.m_double; - } - llvm_unreachable("Unhandled data type"); +uint64_t JSONNumber::GetAsUnsigned() const { + switch (m_data_type) { + case DataType::Unsigned: + return m_data.m_unsigned; + case DataType::Signed: + return (uint64_t)m_data.m_signed; + case DataType::Double: + return (uint64_t)m_data.m_double; + } + llvm_unreachable("Unhandled data type"); } -int64_t -JSONNumber::GetAsSigned() const -{ - switch (m_data_type) - { - case DataType::Unsigned: - return (int64_t)m_data.m_unsigned; - case DataType::Signed: - return m_data.m_signed; - case DataType::Double: - return (int64_t)m_data.m_double; - } - llvm_unreachable("Unhandled data type"); +int64_t JSONNumber::GetAsSigned() const { + switch (m_data_type) { + case DataType::Unsigned: + return (int64_t)m_data.m_unsigned; + case DataType::Signed: + return m_data.m_signed; + case DataType::Double: + return (int64_t)m_data.m_double; + } + llvm_unreachable("Unhandled data type"); } -double -JSONNumber::GetAsDouble() const -{ - switch (m_data_type) - { - case DataType::Unsigned: - return (double)m_data.m_unsigned; - case DataType::Signed: - return (double)m_data.m_signed; - case DataType::Double: - return m_data.m_double; - } - llvm_unreachable("Unhandled data type"); +double JSONNumber::GetAsDouble() const { + switch (m_data_type) { + case DataType::Unsigned: + return (double)m_data.m_unsigned; + case DataType::Signed: + return (double)m_data.m_signed; + case DataType::Double: + return m_data.m_double; + } + llvm_unreachable("Unhandled data type"); } -void -JSONNumber::Write (Stream& s) -{ - switch (m_data_type) - { - case DataType::Unsigned: - s.Printf("%" PRIu64, m_data.m_unsigned); - break; - case DataType::Signed: - s.Printf("%" PRId64, m_data.m_signed); - break; - case DataType::Double: - s.Printf("%g", m_data.m_double); - break; - } +void JSONNumber::Write(Stream &s) { + switch (m_data_type) { + case DataType::Unsigned: + s.Printf("%" PRIu64, m_data.m_unsigned); + break; + case DataType::Signed: + s.Printf("%" PRId64, m_data.m_signed); + break; + case DataType::Double: + s.Printf("%g", m_data.m_double); + break; + } } -JSONTrue::JSONTrue () : - JSONValue(JSONValue::Kind::True) -{ -} +JSONTrue::JSONTrue() : JSONValue(JSONValue::Kind::True) {} -void -JSONTrue::Write(Stream& s) -{ - s.Printf("true"); -} +void JSONTrue::Write(Stream &s) { s.Printf("true"); } -JSONFalse::JSONFalse () : - JSONValue(JSONValue::Kind::False) -{ -} +JSONFalse::JSONFalse() : JSONValue(JSONValue::Kind::False) {} -void -JSONFalse::Write(Stream& s) -{ - s.Printf("false"); -} +void JSONFalse::Write(Stream &s) { s.Printf("false"); } -JSONNull::JSONNull () : - JSONValue(JSONValue::Kind::Null) -{ -} +JSONNull::JSONNull() : JSONValue(JSONValue::Kind::Null) {} -void -JSONNull::Write(Stream& s) -{ - s.Printf("null"); -} +void JSONNull::Write(Stream &s) { s.Printf("null"); } -JSONObject::JSONObject () : - JSONValue(JSONValue::Kind::Object) -{ -} +JSONObject::JSONObject() : JSONValue(JSONValue::Kind::Object) {} -void -JSONObject::Write (Stream& s) -{ - bool first = true; - s.PutChar('{'); - auto iter = m_elements.begin(), end = m_elements.end(); - for (;iter != end; iter++) - { - if (first) - first = false; - else - s.PutChar(','); - JSONString key(iter->first); - JSONValue::SP value(iter->second); - key.Write(s); - s.PutChar(':'); - value->Write(s); - } - s.PutChar('}'); +void JSONObject::Write(Stream &s) { + bool first = true; + s.PutChar('{'); + auto iter = m_elements.begin(), end = m_elements.end(); + for (; iter != end; iter++) { + if (first) + first = false; + else + s.PutChar(','); + JSONString key(iter->first); + JSONValue::SP value(iter->second); + key.Write(s); + s.PutChar(':'); + value->Write(s); + } + s.PutChar('}'); } -bool -JSONObject::SetObject (const std::string& key, - JSONValue::SP value) -{ - if (key.empty() || nullptr == value.get()) - return false; - m_elements[key] = value; - return true; +bool JSONObject::SetObject(const std::string &key, JSONValue::SP value) { + if (key.empty() || nullptr == value.get()) + return false; + m_elements[key] = value; + return true; } -JSONValue::SP -JSONObject::GetObject (const std::string& key) -{ - auto iter = m_elements.find(key), end = m_elements.end(); - if (iter == end) - return JSONValue::SP(); - return iter->second; +JSONValue::SP JSONObject::GetObject(const std::string &key) { + auto iter = m_elements.find(key), end = m_elements.end(); + if (iter == end) + return JSONValue::SP(); + return iter->second; } -JSONArray::JSONArray () : - JSONValue(JSONValue::Kind::Array) -{ +JSONArray::JSONArray() : JSONValue(JSONValue::Kind::Array) {} + +void JSONArray::Write(Stream &s) { + bool first = true; + s.PutChar('['); + auto iter = m_elements.begin(), end = m_elements.end(); + for (; iter != end; iter++) { + if (first) + first = false; + else + s.PutChar(','); + (*iter)->Write(s); + } + s.PutChar(']'); } -void -JSONArray::Write (Stream& s) -{ - bool first = true; - s.PutChar('['); - auto iter = m_elements.begin(), end = m_elements.end(); - for (;iter != end; iter++) - { - if (first) - first = false; - else - s.PutChar(','); - (*iter)->Write(s); - } - s.PutChar(']'); -} - -bool -JSONArray::SetObject (Index i, - JSONValue::SP value) -{ - if (value.get() == nullptr) - return false; - if (i < m_elements.size()) - { - m_elements[i] = value; - return true; - } - if (i == m_elements.size()) - { - m_elements.push_back(value); - return true; - } +bool JSONArray::SetObject(Index i, JSONValue::SP value) { + if (value.get() == nullptr) return false; -} - -bool -JSONArray::AppendObject (JSONValue::SP value) -{ - if (value.get() == nullptr) - return false; + if (i < m_elements.size()) { + m_elements[i] = value; + return true; + } + if (i == m_elements.size()) { m_elements.push_back(value); return true; + } + return false; } -JSONValue::SP -JSONArray::GetObject (Index i) -{ - if (i < m_elements.size()) - return m_elements[i]; - return JSONValue::SP(); +bool JSONArray::AppendObject(JSONValue::SP value) { + if (value.get() == nullptr) + return false; + m_elements.push_back(value); + return true; } -JSONArray::Size -JSONArray::GetNumElements () -{ - return m_elements.size(); +JSONValue::SP JSONArray::GetObject(Index i) { + if (i < m_elements.size()) + return m_elements[i]; + return JSONValue::SP(); } +JSONArray::Size JSONArray::GetNumElements() { return m_elements.size(); } + +JSONParser::JSONParser(const char *cstr) : StringExtractor(cstr) {} + +JSONParser::Token JSONParser::GetToken(std::string &value) { + StreamString error; + + value.clear(); + SkipSpaces(); + const uint64_t start_index = m_index; + const char ch = GetChar(); + switch (ch) { + case '{': + return Token::ObjectStart; + case '}': + return Token::ObjectEnd; + case '[': + return Token::ArrayStart; + case ']': + return Token::ArrayEnd; + case ',': + return Token::Comma; + case ':': + return Token::Colon; + case '\0': + return Token::EndOfFile; + case 't': + if (GetChar() == 'r') + if (GetChar() == 'u') + if (GetChar() == 'e') + return Token::True; + break; + + case 'f': + if (GetChar() == 'a') + if (GetChar() == 'l') + if (GetChar() == 's') + if (GetChar() == 'e') + return Token::False; + break; + + case 'n': + if (GetChar() == 'u') + if (GetChar() == 'l') + if (GetChar() == 'l') + return Token::Null; + break; + + case '"': { + while (1) { + bool was_escaped = false; + int escaped_ch = GetEscapedChar(was_escaped); + if (escaped_ch == -1) { + error.Printf( + "error: an error occurred getting a character from offset %" PRIu64, + start_index); + value = std::move(error.GetString()); + return Token::Error; + + } else { + const bool is_end_quote = escaped_ch == '"'; + const bool is_null = escaped_ch == 0; + if (was_escaped || (!is_end_quote && !is_null)) { + if (CHAR_MIN <= escaped_ch && escaped_ch <= CHAR_MAX) { + value.append(1, (char)escaped_ch); + } else { + error.Printf("error: wide character support is needed for unicode " + "character 0x%4.4x at offset %" PRIu64, + escaped_ch, start_index); + value = std::move(error.GetString()); + return Token::Error; + } + } else if (is_end_quote) { + return Token::String; + } else if (is_null) { + value = "error: missing end quote for string"; + return Token::Error; + } + } + } + } break; + + case '-': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': { + bool done = false; + bool got_decimal_point = false; + uint64_t exp_index = 0; + bool got_int_digits = (ch >= '0') && (ch <= '9'); + bool got_frac_digits = false; + bool got_exp_digits = false; + while (!done) { + const char next_ch = PeekChar(); + switch (next_ch) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + if (exp_index != 0) { + got_exp_digits = true; + } else if (got_decimal_point) { + got_frac_digits = true; + } else { + got_int_digits = true; + } + ++m_index; // Skip this character + break; + + case '.': + if (got_decimal_point) { + error.Printf("error: extra decimal point found at offset %" PRIu64, + start_index); + value = std::move(error.GetString()); + return Token::Error; + } else { + got_decimal_point = true; + ++m_index; // Skip this character + } + break; + + case 'e': + case 'E': + if (exp_index != 0) { + error.Printf( + "error: extra exponent character found at offset %" PRIu64, + start_index); + value = std::move(error.GetString()); + return Token::Error; + } else { + exp_index = m_index; + ++m_index; // Skip this character + } + break; + + case '+': + case '-': + // The '+' and '-' can only come after an exponent character... + if (exp_index == m_index - 1) { + ++m_index; // Skip the exponent sign character + } else { + error.Printf("error: unexpected %c character at offset %" PRIu64, + next_ch, start_index); + value = std::move(error.GetString()); + return Token::Error; + } + break; -JSONParser::JSONParser (const char *cstr) : - StringExtractor(cstr) -{ -} - -JSONParser::Token -JSONParser::GetToken (std::string &value) -{ - StreamString error; - - value.clear(); - SkipSpaces (); - const uint64_t start_index = m_index; - const char ch = GetChar(); - switch (ch) - { - case '{': return Token::ObjectStart; - case '}': return Token::ObjectEnd; - case '[': return Token::ArrayStart; - case ']': return Token::ArrayEnd; - case ',': return Token::Comma; - case ':': return Token::Colon; - case '\0': return Token::EndOfFile; - case 't': - if (GetChar() == 'r') - if (GetChar() == 'u') - if (GetChar() == 'e') - return Token::True; - break; - - case 'f': - if (GetChar() == 'a') - if (GetChar() == 'l') - if (GetChar() == 's') - if (GetChar() == 'e') - return Token::False; - break; - - case 'n': - if (GetChar() == 'u') - if (GetChar() == 'l') - if (GetChar() == 'l') - return Token::Null; - break; - - case '"': - { - while (1) - { - bool was_escaped = false; - int escaped_ch = GetEscapedChar(was_escaped); - if (escaped_ch == -1) - { - error.Printf("error: an error occurred getting a character from offset %" PRIu64, start_index); - value = std::move(error.GetString()); - return Token::Error; - - } - else - { - const bool is_end_quote = escaped_ch == '"'; - const bool is_null = escaped_ch == 0; - if (was_escaped || (!is_end_quote && !is_null)) - { - if (CHAR_MIN <= escaped_ch && escaped_ch <= CHAR_MAX) - { - value.append(1, (char)escaped_ch); - } - else - { - error.Printf("error: wide character support is needed for unicode character 0x%4.4x at offset %" PRIu64, escaped_ch, start_index); - value = std::move(error.GetString()); - return Token::Error; - } - } - else if (is_end_quote) - { - return Token::String; - } - else if (is_null) - { - value = "error: missing end quote for string"; - return Token::Error; - } - } - } - } - break; - - case '-': - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - { - bool done = false; - bool got_decimal_point = false; - uint64_t exp_index = 0; - bool got_int_digits = (ch >= '0') && (ch <= '9'); - bool got_frac_digits = false; - bool got_exp_digits = false; - while (!done) - { - const char next_ch = PeekChar(); - switch (next_ch) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - if (exp_index != 0) - { - got_exp_digits = true; - } - else if (got_decimal_point) - { - got_frac_digits = true; - } - else - { - got_int_digits = true; - } - ++m_index; // Skip this character - break; - - case '.': - if (got_decimal_point) - { - error.Printf("error: extra decimal point found at offset %" PRIu64, start_index); - value = std::move(error.GetString()); - return Token::Error; - } - else - { - got_decimal_point = true; - ++m_index; // Skip this character - } - break; - - case 'e': - case 'E': - if (exp_index != 0) - { - error.Printf("error: extra exponent character found at offset %" PRIu64, start_index); - value = std::move(error.GetString()); - return Token::Error; - } - else - { - exp_index = m_index; - ++m_index; // Skip this character - } - break; - - case '+': - case '-': - // The '+' and '-' can only come after an exponent character... - if (exp_index == m_index - 1) - { - ++m_index; // Skip the exponent sign character - } - else - { - error.Printf("error: unexpected %c character at offset %" PRIu64, next_ch, start_index); - value = std::move(error.GetString()); - return Token::Error; - } - break; - - default: - done = true; - break; - } - } - - if (m_index > start_index) - { - value = m_packet.substr(start_index, m_index - start_index); - if (got_decimal_point) - { - if (exp_index != 0) - { - // We have an exponent, make sure we got exponent digits - if (got_exp_digits) - { - return Token::Float; - } - else - { - error.Printf("error: got exponent character but no exponent digits at offset in float value \"%s\"", value.c_str()); - value = std::move(error.GetString()); - return Token::Error; - } - } - else - { - // No exponent, but we need at least one decimal after the decimal point - if (got_frac_digits) - { - return Token::Float; - } - else - { - error.Printf("error: no digits after decimal point \"%s\"", value.c_str()); - value = std::move(error.GetString()); - return Token::Error; - } - } - } - else - { - // No decimal point - if (got_int_digits) - { - // We need at least some integer digits to make an integer - return Token::Integer; - } - else - { - error.Printf("error: no digits negate sign \"%s\"", value.c_str()); - value = std::move(error.GetString()); - return Token::Error; - } - } - } - else - { - error.Printf("error: invalid number found at offset %" PRIu64, start_index); - value = std::move(error.GetString()); - return Token::Error; - } - } - break; - default: - break; + default: + done = true; + break; + } } - error.Printf("error: failed to parse token at offset %" PRIu64 " (around character '%c')", start_index, ch); - value = std::move(error.GetString()); - return Token::Error; -} -int -JSONParser::GetEscapedChar(bool &was_escaped) -{ - was_escaped = false; - const char ch = GetChar(); - if (ch == '\\') - { - was_escaped = true; - const char ch2 = GetChar(); - switch (ch2) - { - case '"': - case '\\': - case '/': - default: - break; - - case 'b': return '\b'; - case 'f': return '\f'; - case 'n': return '\n'; - case 'r': return '\r'; - case 't': return '\t'; - case 'u': - { - const int hi_byte = DecodeHexU8(); - const int lo_byte = DecodeHexU8(); - if (hi_byte >=0 && lo_byte >= 0) - return hi_byte << 8 | lo_byte; - return -1; - } - break; + if (m_index > start_index) { + value = m_packet.substr(start_index, m_index - start_index); + if (got_decimal_point) { + if (exp_index != 0) { + // We have an exponent, make sure we got exponent digits + if (got_exp_digits) { + return Token::Float; + } else { + error.Printf("error: got exponent character but no exponent digits " + "at offset in float value \"%s\"", + value.c_str()); + value = std::move(error.GetString()); + return Token::Error; + } + } else { + // No exponent, but we need at least one decimal after the decimal + // point + if (got_frac_digits) { + return Token::Float; + } else { + error.Printf("error: no digits after decimal point \"%s\"", + value.c_str()); + value = std::move(error.GetString()); + return Token::Error; + } + } + } else { + // No decimal point + if (got_int_digits) { + // We need at least some integer digits to make an integer + return Token::Integer; + } else { + error.Printf("error: no digits negate sign \"%s\"", value.c_str()); + value = std::move(error.GetString()); + return Token::Error; } - return ch2; + } + } else { + error.Printf("error: invalid number found at offset %" PRIu64, + start_index); + value = std::move(error.GetString()); + return Token::Error; } - return ch; + } break; + default: + break; + } + error.Printf("error: failed to parse token at offset %" PRIu64 + " (around character '%c')", + start_index, ch); + value = std::move(error.GetString()); + return Token::Error; } -JSONValue::SP -JSONParser::ParseJSONObject () -{ - // The "JSONParser::Token::ObjectStart" token should have already been consumed - // by the time this function is called - std::unique_ptr<JSONObject> dict_up(new JSONObject()); - - std::string value; - std::string key; - while (1) - { - JSONParser::Token token = GetToken(value); - - if (token == JSONParser::Token::String) - { - key.swap(value); - token = GetToken(value); - if (token == JSONParser::Token::Colon) - { - JSONValue::SP value_sp = ParseJSONValue(); - if (value_sp) - dict_up->SetObject(key, value_sp); - else - break; - } - } - else if (token == JSONParser::Token::ObjectEnd) - { - return JSONValue::SP(dict_up.release()); - } - else if (token == JSONParser::Token::Comma) - { - continue; - } - else - { - break; - } +int JSONParser::GetEscapedChar(bool &was_escaped) { + was_escaped = false; + const char ch = GetChar(); + if (ch == '\\') { + was_escaped = true; + const char ch2 = GetChar(); + switch (ch2) { + case '"': + case '\\': + case '/': + default: + break; + + case 'b': + return '\b'; + case 'f': + return '\f'; + case 'n': + return '\n'; + case 'r': + return '\r'; + case 't': + return '\t'; + case 'u': { + const int hi_byte = DecodeHexU8(); + const int lo_byte = DecodeHexU8(); + if (hi_byte >= 0 && lo_byte >= 0) + return hi_byte << 8 | lo_byte; + return -1; + } break; } - return JSONValue::SP(); + return ch2; + } + return ch; } -JSONValue::SP -JSONParser::ParseJSONArray () -{ - // The "JSONParser::Token::ObjectStart" token should have already been consumed - // by the time this function is called - std::unique_ptr<JSONArray> array_up(new JSONArray()); - - std::string value; - std::string key; - while (1) - { +JSONValue::SP JSONParser::ParseJSONObject() { + // The "JSONParser::Token::ObjectStart" token should have already been + // consumed + // by the time this function is called + std::unique_ptr<JSONObject> dict_up(new JSONObject()); + + std::string value; + std::string key; + while (1) { + JSONParser::Token token = GetToken(value); + + if (token == JSONParser::Token::String) { + key.swap(value); + token = GetToken(value); + if (token == JSONParser::Token::Colon) { JSONValue::SP value_sp = ParseJSONValue(); if (value_sp) - array_up->AppendObject(value_sp); + dict_up->SetObject(key, value_sp); else - break; + break; + } + } else if (token == JSONParser::Token::ObjectEnd) { + return JSONValue::SP(dict_up.release()); + } else if (token == JSONParser::Token::Comma) { + continue; + } else { + break; + } + } + return JSONValue::SP(); +} - JSONParser::Token token = GetToken(value); - if (token == JSONParser::Token::Comma) - { - continue; - } - else if (token == JSONParser::Token::ArrayEnd) - { - return JSONValue::SP(array_up.release()); - } - else - { - break; - } +JSONValue::SP JSONParser::ParseJSONArray() { + // The "JSONParser::Token::ObjectStart" token should have already been + // consumed + // by the time this function is called + std::unique_ptr<JSONArray> array_up(new JSONArray()); + + std::string value; + std::string key; + while (1) { + JSONValue::SP value_sp = ParseJSONValue(); + if (value_sp) + array_up->AppendObject(value_sp); + else + break; + + JSONParser::Token token = GetToken(value); + if (token == JSONParser::Token::Comma) { + continue; + } else if (token == JSONParser::Token::ArrayEnd) { + return JSONValue::SP(array_up.release()); + } else { + break; } - return JSONValue::SP(); + } + return JSONValue::SP(); } -JSONValue::SP -JSONParser::ParseJSONValue () -{ - std::string value; - const JSONParser::Token token = GetToken(value); - switch (token) - { - case JSONParser::Token::ObjectStart: - return ParseJSONObject(); - - case JSONParser::Token::ArrayStart: - return ParseJSONArray(); - - case JSONParser::Token::Integer: - { - if (value.front() == '-') - { - bool success = false; - int64_t sval = StringConvert::ToSInt64(value.c_str(), 0, 0, &success); - if (success) - return JSONValue::SP(new JSONNumber(sval)); - } - else - { - bool success = false; - uint64_t uval = StringConvert::ToUInt64(value.c_str(), 0, 0, &success); - if (success) - return JSONValue::SP(new JSONNumber(uval)); - } - } - break; - - case JSONParser::Token::Float: - { - bool success = false; - double val = StringConvert::ToDouble(value.c_str(), 0.0, &success); - if (success) - return JSONValue::SP(new JSONNumber(val)); - } - break; +JSONValue::SP JSONParser::ParseJSONValue() { + std::string value; + const JSONParser::Token token = GetToken(value); + switch (token) { + case JSONParser::Token::ObjectStart: + return ParseJSONObject(); + + case JSONParser::Token::ArrayStart: + return ParseJSONArray(); + + case JSONParser::Token::Integer: { + if (value.front() == '-') { + bool success = false; + int64_t sval = StringConvert::ToSInt64(value.c_str(), 0, 0, &success); + if (success) + return JSONValue::SP(new JSONNumber(sval)); + } else { + bool success = false; + uint64_t uval = StringConvert::ToUInt64(value.c_str(), 0, 0, &success); + if (success) + return JSONValue::SP(new JSONNumber(uval)); + } + } break; - case JSONParser::Token::String: - return JSONValue::SP(new JSONString(value)); + case JSONParser::Token::Float: { + bool success = false; + double val = StringConvert::ToDouble(value.c_str(), 0.0, &success); + if (success) + return JSONValue::SP(new JSONNumber(val)); + } break; - case JSONParser::Token::True: - return JSONValue::SP(new JSONTrue()); + case JSONParser::Token::String: + return JSONValue::SP(new JSONString(value)); - case JSONParser::Token::False: - return JSONValue::SP(new JSONFalse()); + case JSONParser::Token::True: + return JSONValue::SP(new JSONTrue()); - case JSONParser::Token::Null: - return JSONValue::SP(new JSONNull()); + case JSONParser::Token::False: + return JSONValue::SP(new JSONFalse()); - default: - break; - } - return JSONValue::SP(); - + case JSONParser::Token::Null: + return JSONValue::SP(new JSONNull()); + + default: + break; + } + return JSONValue::SP(); } |