some reorganization

This commit is contained in:
Niels 2015-02-14 17:34:06 +01:00
parent cc4a8319a1
commit 6ef3cb51a4
3 changed files with 687 additions and 668 deletions

View file

@ -2516,6 +2516,8 @@ class basic_json
// pointer for backtracking information // pointer for backtracking information
const char* m_marker = nullptr; const char* m_marker = nullptr;
while (true)
{
// remember the begin of the token // remember the begin of the token
m_start = m_cursor; m_start = m_cursor;
@ -2568,29 +2570,27 @@ class basic_json
{ {
if (yych <= 0x00) if (yych <= 0x00)
{ {
goto json_parser_27; goto basic_json_parser_27;
} }
if (yych <= 0x08) if (yych <= 0x08)
{ {
goto json_parser_29; goto basic_json_parser_29;
} }
if (yych <= '\t') if (yych >= '\n')
{ {
goto json_parser_3; goto basic_json_parser_4;
} }
goto json_parser_4;
} }
else else
{ {
if (yych == '\r') if (yych == '\r')
{ {
goto json_parser_3; goto basic_json_parser_2;
} }
if (yych <= 0x1F) if (yych <= 0x1F)
{ {
goto json_parser_29; goto basic_json_parser_29;
} }
goto json_parser_3;
} }
} }
else else
@ -2599,29 +2599,29 @@ class basic_json
{ {
if (yych == '"') if (yych == '"')
{ {
goto json_parser_26; goto basic_json_parser_26;
} }
if (yych <= '+') if (yych <= '+')
{ {
goto json_parser_29; goto basic_json_parser_29;
} }
goto json_parser_14; goto basic_json_parser_14;
} }
else else
{ {
if (yych <= '-') if (yych <= '-')
{ {
goto json_parser_22; goto basic_json_parser_22;
} }
if (yych <= '/') if (yych <= '/')
{ {
goto json_parser_29; goto basic_json_parser_29;
} }
if (yych <= '0') if (yych <= '0')
{ {
goto json_parser_23; goto basic_json_parser_23;
} }
goto json_parser_25; goto basic_json_parser_25;
} }
} }
} }
@ -2633,25 +2633,25 @@ class basic_json
{ {
if (yych <= ':') if (yych <= ':')
{ {
goto json_parser_16; goto basic_json_parser_16;
} }
if (yych == '[') if (yych == '[')
{ {
goto json_parser_6; goto basic_json_parser_6;
} }
goto json_parser_29; goto basic_json_parser_29;
} }
else else
{ {
if (yych <= ']') if (yych <= ']')
{ {
goto json_parser_8; goto basic_json_parser_8;
} }
if (yych == 'f') if (yych == 'f')
{ {
goto json_parser_21; goto basic_json_parser_21;
} }
goto json_parser_29; goto basic_json_parser_29;
} }
} }
else else
@ -2660,189 +2660,190 @@ class basic_json
{ {
if (yych <= 'n') if (yych <= 'n')
{ {
goto json_parser_18; goto basic_json_parser_18;
} }
if (yych == 't') if (yych == 't')
{ {
goto json_parser_20; goto basic_json_parser_20;
} }
goto json_parser_29; goto basic_json_parser_29;
} }
else else
{ {
if (yych <= '{') if (yych <= '{')
{ {
goto json_parser_10; goto basic_json_parser_10;
} }
if (yych == '}') if (yych == '}')
{ {
goto json_parser_12; goto basic_json_parser_12;
} }
goto json_parser_29; goto basic_json_parser_29;
} }
} }
} }
json_parser_2: basic_json_parser_2:
{
return scan();
}
json_parser_3:
yych = *++m_cursor;
goto json_parser_5;
json_parser_4:
++m_cursor; ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
json_parser_5: goto basic_json_parser_5;
basic_json_parser_3:
{
continue;
}
basic_json_parser_4:
++m_cursor;
yych = *m_cursor;
basic_json_parser_5:
if (yybm[0 + yych] & 32) if (yybm[0 + yych] & 32)
{ {
goto json_parser_4; goto basic_json_parser_4;
} }
goto json_parser_2; goto basic_json_parser_3;
json_parser_6: basic_json_parser_6:
++m_cursor; ++m_cursor;
{ {
return token_type::begin_array; return token_type::begin_array;
} }
json_parser_8: basic_json_parser_8:
++m_cursor; ++m_cursor;
{ {
return token_type::end_array; return token_type::end_array;
} }
json_parser_10: basic_json_parser_10:
++m_cursor; ++m_cursor;
{ {
return token_type::begin_object; return token_type::begin_object;
} }
json_parser_12: basic_json_parser_12:
++m_cursor; ++m_cursor;
{ {
return token_type::end_object; return token_type::end_object;
} }
json_parser_14: basic_json_parser_14:
++m_cursor; ++m_cursor;
{ {
return token_type::value_separator; return token_type::value_separator;
} }
json_parser_16: basic_json_parser_16:
++m_cursor; ++m_cursor;
{ {
return token_type::name_separator; return token_type::name_separator;
} }
json_parser_18: basic_json_parser_18:
yyaccept = 0; yyaccept = 0;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych == 'u') if (yych == 'u')
{ {
goto json_parser_59; goto basic_json_parser_59;
} }
json_parser_19: basic_json_parser_19:
{ {
return token_type::parse_error; return token_type::parse_error;
} }
json_parser_20: basic_json_parser_20:
yyaccept = 0; yyaccept = 0;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych == 'r') if (yych == 'r')
{ {
goto json_parser_55; goto basic_json_parser_55;
} }
goto json_parser_19; goto basic_json_parser_19;
json_parser_21: basic_json_parser_21:
yyaccept = 0; yyaccept = 0;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych == 'a') if (yych == 'a')
{ {
goto json_parser_50; goto basic_json_parser_50;
} }
goto json_parser_19; goto basic_json_parser_19;
json_parser_22: basic_json_parser_22:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= '/') if (yych <= '/')
{ {
goto json_parser_19; goto basic_json_parser_19;
} }
if (yych <= '0') if (yych <= '0')
{ {
goto json_parser_49; goto basic_json_parser_49;
} }
if (yych <= '9') if (yych <= '9')
{ {
goto json_parser_40; goto basic_json_parser_40;
} }
goto json_parser_19; goto basic_json_parser_19;
json_parser_23: basic_json_parser_23:
yyaccept = 1; yyaccept = 1;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych <= 'D') if (yych <= 'D')
{ {
if (yych == '.') if (yych == '.')
{ {
goto json_parser_42; goto basic_json_parser_42;
} }
} }
else else
{ {
if (yych <= 'E') if (yych <= 'E')
{ {
goto json_parser_43; goto basic_json_parser_43;
} }
if (yych == 'e') if (yych == 'e')
{ {
goto json_parser_43; goto basic_json_parser_43;
} }
} }
json_parser_24: basic_json_parser_24:
{ {
return token_type::value_number; return token_type::value_number;
} }
json_parser_25: basic_json_parser_25:
yyaccept = 1; yyaccept = 1;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
goto json_parser_41; goto basic_json_parser_41;
json_parser_26: basic_json_parser_26:
yyaccept = 0; yyaccept = 0;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych <= 0x00) if (yych <= 0x00)
{ {
goto json_parser_19; goto basic_json_parser_19;
} }
goto json_parser_31; goto basic_json_parser_31;
json_parser_27: basic_json_parser_27:
++m_cursor; ++m_cursor;
{ {
return token_type::end_of_input; return token_type::end_of_input;
} }
json_parser_29: basic_json_parser_29:
yych = *++m_cursor; yych = *++m_cursor;
goto json_parser_19; goto basic_json_parser_19;
json_parser_30: basic_json_parser_30:
++m_cursor; ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
json_parser_31: basic_json_parser_31:
if (yybm[0 + yych] & 64) if (yybm[0 + yych] & 64)
{ {
goto json_parser_30; goto basic_json_parser_30;
} }
if (yych <= 0x00) if (yych <= 0x00)
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych <= '"') if (yych <= '"')
{ {
goto json_parser_34; goto basic_json_parser_34;
} }
goto json_parser_33; goto basic_json_parser_33;
json_parser_32: basic_json_parser_32:
m_cursor = m_marker; m_cursor = m_marker;
if (yyaccept == 0) if (yyaccept == 0)
{ {
goto json_parser_19; goto basic_json_parser_19;
} }
else else
{ {
goto json_parser_24; goto basic_json_parser_24;
} }
json_parser_33: basic_json_parser_33:
++m_cursor; ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
if (yych <= 'e') if (yych <= 'e')
@ -2851,13 +2852,13 @@ json_parser_33:
{ {
if (yych == '"') if (yych == '"')
{ {
goto json_parser_30; goto basic_json_parser_30;
} }
if (yych <= '.') if (yych <= '.')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
goto json_parser_30; goto basic_json_parser_30;
} }
else else
{ {
@ -2865,17 +2866,17 @@ json_parser_33:
{ {
if (yych <= '[') if (yych <= '[')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
goto json_parser_30; goto basic_json_parser_30;
} }
else else
{ {
if (yych == 'b') if (yych == 'b')
{ {
goto json_parser_30; goto basic_json_parser_30;
} }
goto json_parser_32; goto basic_json_parser_32;
} }
} }
} }
@ -2885,13 +2886,13 @@ json_parser_33:
{ {
if (yych <= 'f') if (yych <= 'f')
{ {
goto json_parser_30; goto basic_json_parser_30;
} }
if (yych == 'n') if (yych == 'n')
{ {
goto json_parser_30; goto basic_json_parser_30;
} }
goto json_parser_32; goto basic_json_parser_32;
} }
else else
{ {
@ -2899,234 +2900,234 @@ json_parser_33:
{ {
if (yych <= 'r') if (yych <= 'r')
{ {
goto json_parser_30; goto basic_json_parser_30;
} }
goto json_parser_32; goto basic_json_parser_32;
} }
else else
{ {
if (yych <= 't') if (yych <= 't')
{ {
goto json_parser_30; goto basic_json_parser_30;
} }
if (yych <= 'u') if (yych <= 'u')
{ {
goto json_parser_36; goto basic_json_parser_36;
} }
goto json_parser_32; goto basic_json_parser_32;
} }
} }
} }
json_parser_34: basic_json_parser_34:
++m_cursor; ++m_cursor;
{ {
return token_type::value_string; return token_type::value_string;
} }
json_parser_36: basic_json_parser_36:
++m_cursor; ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
if (yych <= '@') if (yych <= '@')
{ {
if (yych <= '/') if (yych <= '/')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych >= ':') if (yych >= ':')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
} }
else else
{ {
if (yych <= 'F') if (yych <= 'F')
{ {
goto json_parser_37; goto basic_json_parser_37;
} }
if (yych <= '`') if (yych <= '`')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych >= 'g') if (yych >= 'g')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
} }
json_parser_37: basic_json_parser_37:
++m_cursor; ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
if (yych <= '@') if (yych <= '@')
{ {
if (yych <= '/') if (yych <= '/')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych >= ':') if (yych >= ':')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
} }
else else
{ {
if (yych <= 'F') if (yych <= 'F')
{ {
goto json_parser_38; goto basic_json_parser_38;
} }
if (yych <= '`') if (yych <= '`')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych >= 'g') if (yych >= 'g')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
} }
json_parser_38: basic_json_parser_38:
++m_cursor; ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
if (yych <= '@') if (yych <= '@')
{ {
if (yych <= '/') if (yych <= '/')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych >= ':') if (yych >= ':')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
} }
else else
{ {
if (yych <= 'F') if (yych <= 'F')
{ {
goto json_parser_39; goto basic_json_parser_39;
} }
if (yych <= '`') if (yych <= '`')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych >= 'g') if (yych >= 'g')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
} }
json_parser_39: basic_json_parser_39:
++m_cursor; ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
if (yych <= '@') if (yych <= '@')
{ {
if (yych <= '/') if (yych <= '/')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych <= '9') if (yych <= '9')
{ {
goto json_parser_30; goto basic_json_parser_30;
} }
goto json_parser_32; goto basic_json_parser_32;
} }
else else
{ {
if (yych <= 'F') if (yych <= 'F')
{ {
goto json_parser_30; goto basic_json_parser_30;
} }
if (yych <= '`') if (yych <= '`')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych <= 'f') if (yych <= 'f')
{ {
goto json_parser_30; goto basic_json_parser_30;
} }
goto json_parser_32; goto basic_json_parser_32;
} }
json_parser_40: basic_json_parser_40:
yyaccept = 1; yyaccept = 1;
m_marker = ++m_cursor; m_marker = ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
json_parser_41: basic_json_parser_41:
if (yybm[0 + yych] & 128) if (yybm[0 + yych] & 128)
{ {
goto json_parser_40; goto basic_json_parser_40;
} }
if (yych <= 'D') if (yych <= 'D')
{ {
if (yych != '.') if (yych != '.')
{ {
goto json_parser_24; goto basic_json_parser_24;
} }
} }
else else
{ {
if (yych <= 'E') if (yych <= 'E')
{ {
goto json_parser_43; goto basic_json_parser_43;
} }
if (yych == 'e') if (yych == 'e')
{ {
goto json_parser_43; goto basic_json_parser_43;
} }
goto json_parser_24; goto basic_json_parser_24;
} }
json_parser_42: basic_json_parser_42:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= '/') if (yych <= '/')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych <= '9') if (yych <= '9')
{ {
goto json_parser_47; goto basic_json_parser_47;
} }
goto json_parser_32; goto basic_json_parser_32;
json_parser_43: basic_json_parser_43:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= ',') if (yych <= ',')
{ {
if (yych != '+') if (yych != '+')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
} }
else else
{ {
if (yych <= '-') if (yych <= '-')
{ {
goto json_parser_44; goto basic_json_parser_44;
} }
if (yych <= '/') if (yych <= '/')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych <= '9') if (yych <= '9')
{ {
goto json_parser_45; goto basic_json_parser_45;
} }
goto json_parser_32; goto basic_json_parser_32;
} }
json_parser_44: basic_json_parser_44:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= '/') if (yych <= '/')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
if (yych >= ':') if (yych >= ':')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
json_parser_45: basic_json_parser_45:
++m_cursor; ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
if (yych <= '/') if (yych <= '/')
{ {
goto json_parser_24; goto basic_json_parser_24;
} }
if (yych <= '9') if (yych <= '9')
{ {
goto json_parser_45; goto basic_json_parser_45;
} }
goto json_parser_24; goto basic_json_parser_24;
json_parser_47: basic_json_parser_47:
yyaccept = 1; yyaccept = 1;
m_marker = ++m_cursor; m_marker = ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
@ -3134,94 +3135,94 @@ json_parser_47:
{ {
if (yych <= '/') if (yych <= '/')
{ {
goto json_parser_24; goto basic_json_parser_24;
} }
if (yych <= '9') if (yych <= '9')
{ {
goto json_parser_47; goto basic_json_parser_47;
} }
goto json_parser_24; goto basic_json_parser_24;
} }
else else
{ {
if (yych <= 'E') if (yych <= 'E')
{ {
goto json_parser_43; goto basic_json_parser_43;
} }
if (yych == 'e') if (yych == 'e')
{ {
goto json_parser_43; goto basic_json_parser_43;
} }
goto json_parser_24; goto basic_json_parser_24;
} }
json_parser_49: basic_json_parser_49:
yyaccept = 1; yyaccept = 1;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
if (yych <= 'D') if (yych <= 'D')
{ {
if (yych == '.') if (yych == '.')
{ {
goto json_parser_42; goto basic_json_parser_42;
} }
goto json_parser_24; goto basic_json_parser_24;
} }
else else
{ {
if (yych <= 'E') if (yych <= 'E')
{ {
goto json_parser_43; goto basic_json_parser_43;
} }
if (yych == 'e') if (yych == 'e')
{ {
goto json_parser_43; goto basic_json_parser_43;
} }
goto json_parser_24; goto basic_json_parser_24;
} }
json_parser_50: basic_json_parser_50:
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'l') if (yych != 'l')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 's') if (yych != 's')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'e') if (yych != 'e')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
++m_cursor; ++m_cursor;
{ {
return token_type::literal_false; return token_type::literal_false;
} }
json_parser_55: basic_json_parser_55:
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'u') if (yych != 'u')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'e') if (yych != 'e')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
++m_cursor; ++m_cursor;
{ {
return token_type::literal_true; return token_type::literal_true;
} }
json_parser_59: basic_json_parser_59:
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'l') if (yych != 'l')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'l') if (yych != 'l')
{ {
goto json_parser_32; goto basic_json_parser_32;
} }
++m_cursor; ++m_cursor;
{ {
@ -3230,8 +3231,9 @@ json_parser_59:
} }
} }
}
inline std::string get_string_value() const inline std::string get_token() const
{ {
return std::string(m_start, static_cast<size_t>(m_cursor - m_start)); return std::string(m_start, static_cast<size_t>(m_cursor - m_start));
} }
@ -3266,15 +3268,9 @@ json_parser_59:
char* endptr; char* endptr;
const auto float_val = std::strtod(reinterpret_cast<const char*>(m_start), &endptr); const auto float_val = std::strtod(reinterpret_cast<const char*>(m_start), &endptr);
// check if strtod read beyond the end of the lexem // return float_val if the whole number was translated and NAN
if (endptr != m_cursor) // otherwise
{ return (endptr == m_cursor) ? float_val : NAN;
return NAN;
}
else
{
return float_val;
}
} }
private: private:
@ -3441,10 +3437,12 @@ json_parser_59:
{ {
auto float_val = m_lexer.get_number(); auto float_val = m_lexer.get_number();
// NAN is returned if token could not be translated
// completely
if (std::isnan(float_val)) if (std::isnan(float_val))
{ {
throw std::invalid_argument(std::string("parse error - ") + throw std::invalid_argument(std::string("parse error - ") +
m_lexer.get_string_value() + " is not a number"); m_lexer.get_token() + " is not a number");
} }
get_token(); get_token();
@ -3466,7 +3464,7 @@ json_parser_59:
default: default:
{ {
std::string error_msg = "parse error - unexpected \'"; std::string error_msg = "parse error - unexpected \'";
error_msg += m_lexer.get_string_value(); error_msg += m_lexer.get_token();
error_msg += "\' ("; error_msg += "\' (";
error_msg += lexer::token_type_name(last_token) + ")"; error_msg += lexer::token_type_name(last_token) + ")";
throw std::invalid_argument(error_msg); throw std::invalid_argument(error_msg);
@ -3486,7 +3484,7 @@ json_parser_59:
if (t != last_token) if (t != last_token)
{ {
std::string error_msg = "parse error - unexpected \'"; std::string error_msg = "parse error - unexpected \'";
error_msg += m_lexer.get_string_value(); error_msg += m_lexer.get_token();
error_msg += "\' (" + lexer::token_type_name(last_token); error_msg += "\' (" + lexer::token_type_name(last_token);
error_msg += "); expected " + lexer::token_type_name(t); error_msg += "); expected " + lexer::token_type_name(t);
throw std::invalid_argument(error_msg); throw std::invalid_argument(error_msg);
@ -3498,6 +3496,7 @@ json_parser_59:
std::string m_buffer; std::string m_buffer;
/// the type of the last read token /// the type of the last read token
typename lexer::token_type last_token = lexer::token_type::uninitialized; typename lexer::token_type last_token = lexer::token_type::uninitialized;
/// the lexer
lexer m_lexer; lexer m_lexer;
}; };
}; };

View file

@ -2516,6 +2516,8 @@ class basic_json
// pointer for backtracking information // pointer for backtracking information
const char* m_marker = nullptr; const char* m_marker = nullptr;
while (true)
{
// remember the begin of the token // remember the begin of the token
m_start = m_cursor; m_start = m_cursor;
@ -2526,12 +2528,12 @@ class basic_json
re2c:define:YYMARKER = m_marker; re2c:define:YYMARKER = m_marker;
re2c:indent:string = " "; re2c:indent:string = " ";
re2c:indent:top = 1; re2c:indent:top = 1;
re2c:labelprefix = "json_parser_"; re2c:labelprefix = "basic_json_parser_";
re2c:yyfill:enable = 0; re2c:yyfill:enable = 0;
// whitespace // whitespace
ws = [ \t\n\r]*; ws = [ \t\n\r]+;
ws { return scan(); } ws { continue; }
// structural characters // structural characters
"[" { return token_type::begin_array; } "[" { return token_type::begin_array; }
@ -2564,7 +2566,9 @@ class basic_json
quotation_mark = [\"]; quotation_mark = [\"];
escape = [\\]; escape = [\\];
unescaped = [^\"\\\000]; unescaped = [^\"\\\000];
escaped = escape ([\"\\/bfnrt] | [u][0-9a-fA-F]{4}); single_escaped = [\"\\/bfnrt];
unicode_escaped = [u][0-9a-fA-F]{4};
escaped = escape (single_escaped | unicode_escaped);
char = unescaped | escaped; char = unescaped | escaped;
string = quotation_mark char* quotation_mark; string = quotation_mark char* quotation_mark;
string { return token_type::value_string; } string { return token_type::value_string; }
@ -2576,8 +2580,9 @@ class basic_json
. { return token_type::parse_error; } . { return token_type::parse_error; }
*/ */
} }
}
inline std::string get_string_value() const inline std::string get_token() const
{ {
return std::string(m_start, static_cast<size_t>(m_cursor - m_start)); return std::string(m_start, static_cast<size_t>(m_cursor - m_start));
} }
@ -2612,15 +2617,9 @@ class basic_json
char* endptr; char* endptr;
const auto float_val = std::strtod(reinterpret_cast<const char*>(m_start), &endptr); const auto float_val = std::strtod(reinterpret_cast<const char*>(m_start), &endptr);
// check if strtod read beyond the end of the lexem // return float_val if the whole number was translated and NAN
if (endptr != m_cursor) // otherwise
{ return (endptr == m_cursor) ? float_val : NAN;
return NAN;
}
else
{
return float_val;
}
} }
private: private:
@ -2787,10 +2786,12 @@ class basic_json
{ {
auto float_val = m_lexer.get_number(); auto float_val = m_lexer.get_number();
// NAN is returned if token could not be translated
// completely
if (std::isnan(float_val)) if (std::isnan(float_val))
{ {
throw std::invalid_argument(std::string("parse error - ") + throw std::invalid_argument(std::string("parse error - ") +
m_lexer.get_string_value() + " is not a number"); m_lexer.get_token() + " is not a number");
} }
get_token(); get_token();
@ -2812,7 +2813,7 @@ class basic_json
default: default:
{ {
std::string error_msg = "parse error - unexpected \'"; std::string error_msg = "parse error - unexpected \'";
error_msg += m_lexer.get_string_value(); error_msg += m_lexer.get_token();
error_msg += "\' ("; error_msg += "\' (";
error_msg += lexer::token_type_name(last_token) + ")"; error_msg += lexer::token_type_name(last_token) + ")";
throw std::invalid_argument(error_msg); throw std::invalid_argument(error_msg);
@ -2832,7 +2833,7 @@ class basic_json
if (t != last_token) if (t != last_token)
{ {
std::string error_msg = "parse error - unexpected \'"; std::string error_msg = "parse error - unexpected \'";
error_msg += m_lexer.get_string_value(); error_msg += m_lexer.get_token();
error_msg += "\' (" + lexer::token_type_name(last_token); error_msg += "\' (" + lexer::token_type_name(last_token);
error_msg += "); expected " + lexer::token_type_name(t); error_msg += "); expected " + lexer::token_type_name(t);
throw std::invalid_argument(error_msg); throw std::invalid_argument(error_msg);
@ -2844,6 +2845,7 @@ class basic_json
std::string m_buffer; std::string m_buffer;
/// the type of the last read token /// the type of the last read token
typename lexer::token_type last_token = lexer::token_type::uninitialized; typename lexer::token_type last_token = lexer::token_type::uninitialized;
/// the lexer
lexer m_lexer; lexer m_lexer;
}; };
}; };

View file

@ -3630,6 +3630,8 @@ TEST_CASE("capacity")
{ {
CHECK(std::distance(j.begin(), j.end()) == j.size()); CHECK(std::distance(j.begin(), j.end()) == j.size());
CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size());
CHECK(std::distance(j.rbegin(), j.rend()) == j.size());
CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size());
} }
} }
@ -3648,6 +3650,8 @@ TEST_CASE("capacity")
{ {
CHECK(std::distance(j.begin(), j.end()) == j.size()); CHECK(std::distance(j.begin(), j.end()) == j.size());
CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size());
CHECK(std::distance(j.rbegin(), j.rend()) == j.size());
CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size());
} }
} }
@ -3668,6 +3672,8 @@ TEST_CASE("capacity")
{ {
CHECK(std::distance(j.begin(), j.end()) == j.size()); CHECK(std::distance(j.begin(), j.end()) == j.size());
CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size());
CHECK(std::distance(j.rbegin(), j.rend()) == j.size());
CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size());
} }
} }
@ -3686,6 +3692,8 @@ TEST_CASE("capacity")
{ {
CHECK(std::distance(j.begin(), j.end()) == j.size()); CHECK(std::distance(j.begin(), j.end()) == j.size());
CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size());
CHECK(std::distance(j.rbegin(), j.rend()) == j.size());
CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size());
} }
} }
} }
@ -3707,6 +3715,8 @@ TEST_CASE("capacity")
{ {
CHECK(std::distance(j.begin(), j.end()) == j.size()); CHECK(std::distance(j.begin(), j.end()) == j.size());
CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size());
CHECK(std::distance(j.rbegin(), j.rend()) == j.size());
CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size());
} }
} }
@ -3725,6 +3735,8 @@ TEST_CASE("capacity")
{ {
CHECK(std::distance(j.begin(), j.end()) == j.size()); CHECK(std::distance(j.begin(), j.end()) == j.size());
CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size());
CHECK(std::distance(j.rbegin(), j.rend()) == j.size());
CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size());
} }
} }
} }
@ -3744,6 +3756,8 @@ TEST_CASE("capacity")
{ {
CHECK(std::distance(j.begin(), j.end()) == j.size()); CHECK(std::distance(j.begin(), j.end()) == j.size());
CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size());
CHECK(std::distance(j.rbegin(), j.rend()) == j.size());
CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size());
} }
} }
@ -3762,6 +3776,8 @@ TEST_CASE("capacity")
{ {
CHECK(std::distance(j.begin(), j.end()) == j.size()); CHECK(std::distance(j.begin(), j.end()) == j.size());
CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size());
CHECK(std::distance(j.rbegin(), j.rend()) == j.size());
CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size());
} }
} }
@ -3780,6 +3796,8 @@ TEST_CASE("capacity")
{ {
CHECK(std::distance(j.begin(), j.end()) == j.size()); CHECK(std::distance(j.begin(), j.end()) == j.size());
CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size()); CHECK(std::distance(j_const.begin(), j_const.end()) == j_const.size());
CHECK(std::distance(j.rbegin(), j.rend()) == j.size());
CHECK(std::distance(j_const.crbegin(), j_const.crend()) == j_const.size());
} }
} }
} }