Commit f874b5f0 by Niels

clean up

- removed m_final member - parse_internal now takes a reference to a basic_json object to fill with data - header is automatically regenerated if re2c file is changed
parent f09df967
...@@ -16,7 +16,9 @@ json_unit: test/unit.cpp src/json.hpp test/catch.hpp ...@@ -16,7 +16,9 @@ json_unit: test/unit.cpp src/json.hpp test/catch.hpp
$(CXX) -std=c++11 $(CXXFLAGS) $(FLAGS) $(CPPFLAGS) -I src -I test $< $(LDFLAGS) -o $@ $(CXX) -std=c++11 $(CXXFLAGS) $(FLAGS) $(CPPFLAGS) -I src -I test $< $(LDFLAGS) -o $@
# create scanner with re2c # create scanner with re2c
re2c: src/json.hpp.re2c re2c: src/json.hpp
src/json.hpp: src/json.hpp.re2c
$(RE2C) -b -s -i --no-generation-date $< | $(SED) '1d' > src/json.hpp $(RE2C) -b -s -i --no-generation-date $< | $(SED) '1d' > src/json.hpp
# static analyser # static analyser
......
...@@ -416,13 +416,12 @@ class basic_json ...@@ -416,13 +416,12 @@ class basic_json
// the initializer list could describe an object // the initializer list could describe an object
bool is_object = true; bool is_object = true;
// check if each element is an array with two elements whose first element // check if each element is an array with two elements whose first
// is a string // element is a string
for (const auto& element : init) for (const auto& element : init)
{ {
if ((element.m_final and element.m_type == value_t::array) if ((element.m_type != value_t::array or element.size() != 2
or (element.m_type != value_t::array or element.size() != 2 or element[0].m_type != value_t::string))
or element[0].m_type != value_t::string))
{ {
// we found an element that makes it impossible to use the // we found an element that makes it impossible to use the
// initializer list as object // initializer list as object
...@@ -434,9 +433,6 @@ class basic_json ...@@ -434,9 +433,6 @@ class basic_json
// adjust type if type deduction is not wanted // adjust type if type deduction is not wanted
if (not type_deduction) if (not type_deduction)
{ {
// mark this object's type as final
m_final = true;
// if array is wanted, do not create an object though possible // if array is wanted, do not create an object though possible
if (manual_type == value_t::array) if (manual_type == value_t::array)
{ {
...@@ -758,6 +754,12 @@ class basic_json ...@@ -758,6 +754,12 @@ class basic_json
return m_type; return m_type;
} }
/// return the type of the object (implicit)
inline operator value_t() const noexcept
{
return m_type;
}
// return whether value is null // return whether value is null
inline bool is_null() const noexcept inline bool is_null() const noexcept
{ {
...@@ -794,12 +796,6 @@ class basic_json ...@@ -794,12 +796,6 @@ class basic_json
return m_type == value_t::string; return m_type == value_t::string;
} }
/// return the type of the object (implicit)
inline operator value_t() const noexcept
{
return m_type;
}
private: private:
////////////////////// //////////////////////
// value conversion // // value conversion //
...@@ -1083,6 +1079,7 @@ class basic_json ...@@ -1083,6 +1079,7 @@ class basic_json
throw std::runtime_error("cannot use [] with " + type_name()); throw std::runtime_error("cannot use [] with " + type_name());
} }
// fill gaps with null values
for (size_t i = m_value.array->size(); i <= idx; ++i) for (size_t i = m_value.array->size(); i <= idx; ++i)
{ {
m_value.array->push_back(basic_json()); m_value.array->push_back(basic_json());
...@@ -1520,6 +1517,7 @@ class basic_json ...@@ -1520,6 +1517,7 @@ class basic_json
{ {
case (value_t::null): case (value_t::null):
{ {
// null values are empty
return true; return true;
} }
...@@ -1551,6 +1549,7 @@ class basic_json ...@@ -1551,6 +1549,7 @@ class basic_json
{ {
case (value_t::null): case (value_t::null):
{ {
// null values are empty
return 0; return 0;
} }
...@@ -1582,6 +1581,7 @@ class basic_json ...@@ -1582,6 +1581,7 @@ class basic_json
{ {
case (value_t::null): case (value_t::null):
{ {
// null values are empty
return 0; return 0;
} }
...@@ -2258,9 +2258,6 @@ class basic_json ...@@ -2258,9 +2258,6 @@ class basic_json
/// the type of the current element /// the type of the current element
value_t m_type = value_t::null; value_t m_type = value_t::null;
/// whether the type of JSON object may change later
bool m_final = false;
/// the value of the current element /// the value of the current element
json_value m_value = {}; json_value m_value = {};
...@@ -3565,758 +3562,369 @@ class basic_json ...@@ -3565,758 +3562,369 @@ class basic_json
// remember the begin of the token // remember the begin of the token
m_start = m_cursor; m_start = m_cursor;
{
lexer_char_t yych;
unsigned int yyaccept = 0;
static const unsigned char yybm[] = {
0, 64, 64, 64, 64, 64, 64, 64,
64, 96, 96, 64, 64, 96, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
96, 64, 0, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
192, 192, 192, 192, 192, 192, 192, 192,
192, 192, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 0, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64,
};
{ if ((m_limit - m_cursor) < 5) { yyfill(); };
lexer_char_t yych; yych = *m_cursor;
unsigned int yyaccept = 0; if (yych <= '9') {
static const unsigned char yybm[] = if (yych <= ' ') {
{ if (yych <= '\n') {
0, 64, 64, 64, 64, 64, 64, 64, if (yych <= 0x00) goto basic_json_parser_27;
64, 96, 96, 64, 64, 96, 64, 64, if (yych <= 0x08) goto basic_json_parser_29;
64, 64, 64, 64, 64, 64, 64, 64, if (yych >= '\n') goto basic_json_parser_4;
64, 64, 64, 64, 64, 64, 64, 64, } else {
96, 64, 0, 64, 64, 64, 64, 64, if (yych == '\r') goto basic_json_parser_2;
64, 64, 64, 64, 64, 64, 64, 64, if (yych <= 0x1F) goto basic_json_parser_29;
192, 192, 192, 192, 192, 192, 192, 192, }
192, 192, 64, 64, 64, 64, 64, 64, } else {
64, 64, 64, 64, 64, 64, 64, 64, if (yych <= ',') {
64, 64, 64, 64, 64, 64, 64, 64, if (yych == '"') goto basic_json_parser_26;
64, 64, 64, 64, 64, 64, 64, 64, if (yych <= '+') goto basic_json_parser_29;
64, 64, 64, 64, 0, 64, 64, 64, goto basic_json_parser_14;
64, 64, 64, 64, 64, 64, 64, 64, } else {
64, 64, 64, 64, 64, 64, 64, 64, if (yych <= '-') goto basic_json_parser_22;
64, 64, 64, 64, 64, 64, 64, 64, if (yych <= '/') goto basic_json_parser_29;
64, 64, 64, 64, 64, 64, 64, 64, if (yych <= '0') goto basic_json_parser_23;
64, 64, 64, 64, 64, 64, 64, 64, goto basic_json_parser_25;
64, 64, 64, 64, 64, 64, 64, 64, }
64, 64, 64, 64, 64, 64, 64, 64, }
64, 64, 64, 64, 64, 64, 64, 64, } else {
64, 64, 64, 64, 64, 64, 64, 64, if (yych <= 'm') {
64, 64, 64, 64, 64, 64, 64, 64, if (yych <= '\\') {
64, 64, 64, 64, 64, 64, 64, 64, if (yych <= ':') goto basic_json_parser_16;
64, 64, 64, 64, 64, 64, 64, 64, if (yych == '[') goto basic_json_parser_6;
64, 64, 64, 64, 64, 64, 64, 64, goto basic_json_parser_29;
64, 64, 64, 64, 64, 64, 64, 64, } else {
64, 64, 64, 64, 64, 64, 64, 64, if (yych <= ']') goto basic_json_parser_8;
64, 64, 64, 64, 64, 64, 64, 64, if (yych == 'f') goto basic_json_parser_21;
64, 64, 64, 64, 64, 64, 64, 64, goto basic_json_parser_29;
64, 64, 64, 64, 64, 64, 64, 64, }
64, 64, 64, 64, 64, 64, 64, 64, } else {
64, 64, 64, 64, 64, 64, 64, 64, if (yych <= 'z') {
}; if (yych <= 'n') goto basic_json_parser_18;
if (yych == 't') goto basic_json_parser_20;
if ((m_limit - m_cursor) < 5) goto basic_json_parser_29;
{ } else {
yyfill(); if (yych <= '{') goto basic_json_parser_10;
}; if (yych == '}') goto basic_json_parser_12;
yych = *m_cursor; goto basic_json_parser_29;
if (yych <= '9')
{
if (yych <= ' ')
{
if (yych <= '\n')
{
if (yych <= 0x00)
{
goto basic_json_parser_27;
}
if (yych <= 0x08)
{
goto basic_json_parser_29;
}
if (yych >= '\n')
{
goto basic_json_parser_4;
}
}
else
{
if (yych == '\r')
{
goto basic_json_parser_2;
}
if (yych <= 0x1F)
{
goto basic_json_parser_29;
}
}
}
else
{
if (yych <= ',')
{
if (yych == '"')
{
goto basic_json_parser_26;
}
if (yych <= '+')
{
goto basic_json_parser_29;
}
goto basic_json_parser_14;
}
else
{
if (yych <= '-')
{
goto basic_json_parser_22;
}
if (yych <= '/')
{
goto basic_json_parser_29;
}
if (yych <= '0')
{
goto basic_json_parser_23;
}
goto basic_json_parser_25;
}
}
}
else
{
if (yych <= 'm')
{
if (yych <= '\\')
{
if (yych <= ':')
{
goto basic_json_parser_16;
}
if (yych == '[')
{
goto basic_json_parser_6;
}
goto basic_json_parser_29;
}
else
{
if (yych <= ']')
{
goto basic_json_parser_8;
}
if (yych == 'f')
{
goto basic_json_parser_21;
}
goto basic_json_parser_29;
}
}
else
{
if (yych <= 'z')
{
if (yych <= 'n')
{
goto basic_json_parser_18;
}
if (yych == 't')
{
goto basic_json_parser_20;
}
goto basic_json_parser_29;
}
else
{
if (yych <= '{')
{
goto basic_json_parser_10;
}
if (yych == '}')
{
goto basic_json_parser_12;
}
goto basic_json_parser_29;
}
}
} }
}
}
basic_json_parser_2: basic_json_parser_2:
++m_cursor; ++m_cursor;
yych = *m_cursor; yych = *m_cursor;
goto basic_json_parser_5; goto basic_json_parser_5;
basic_json_parser_3: basic_json_parser_3:
{ { return scan(); }
return scan();
}
basic_json_parser_4: basic_json_parser_4:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) if (m_limit <= m_cursor) { yyfill(); };
{ yych = *m_cursor;
yyfill();
};
yych = *m_cursor;
basic_json_parser_5: basic_json_parser_5:
if (yybm[0 + yych] & 32) if (yybm[0+yych] & 32) {
{ goto basic_json_parser_4;
goto basic_json_parser_4; }
} goto basic_json_parser_3;
goto basic_json_parser_3;
basic_json_parser_6: basic_json_parser_6:
++m_cursor; ++m_cursor;
{ { return token_type::begin_array; }
return token_type::begin_array;
}
basic_json_parser_8: basic_json_parser_8:
++m_cursor; ++m_cursor;
{ { return token_type::end_array; }
return token_type::end_array;
}
basic_json_parser_10: basic_json_parser_10:
++m_cursor; ++m_cursor;
{ { return token_type::begin_object; }
return token_type::begin_object;
}
basic_json_parser_12: basic_json_parser_12:
++m_cursor; ++m_cursor;
{ { return token_type::end_object; }
return token_type::end_object;
}
basic_json_parser_14: basic_json_parser_14:
++m_cursor; ++m_cursor;
{ { return token_type::value_separator; }
return token_type::value_separator;
}
basic_json_parser_16: basic_json_parser_16:
++m_cursor; ++m_cursor;
{ { return token_type::name_separator; }
return token_type::name_separator;
}
basic_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 basic_json_parser_59;
{
goto basic_json_parser_59;
}
basic_json_parser_19: basic_json_parser_19:
{ { return token_type::parse_error; }
return token_type::parse_error;
}
basic_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 basic_json_parser_55;
{ goto basic_json_parser_19;
goto basic_json_parser_55;
}
goto basic_json_parser_19;
basic_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 basic_json_parser_50;
{ goto basic_json_parser_19;
goto basic_json_parser_50;
}
goto basic_json_parser_19;
basic_json_parser_22: basic_json_parser_22:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= '/') if (yych <= '/') goto basic_json_parser_19;
{ if (yych <= '0') goto basic_json_parser_49;
goto basic_json_parser_19; if (yych <= '9') goto basic_json_parser_40;
} goto basic_json_parser_19;
if (yych <= '0')
{
goto basic_json_parser_49;
}
if (yych <= '9')
{
goto basic_json_parser_40;
}
goto basic_json_parser_19;
basic_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 == '.') goto basic_json_parser_42;
if (yych == '.') } else {
{ if (yych <= 'E') goto basic_json_parser_43;
goto basic_json_parser_42; if (yych == 'e') goto basic_json_parser_43;
} }
}
else
{
if (yych <= 'E')
{
goto basic_json_parser_43;
}
if (yych == 'e')
{
goto basic_json_parser_43;
}
}
basic_json_parser_24: basic_json_parser_24:
{ { return token_type::value_number; }
return token_type::value_number;
}
basic_json_parser_25: basic_json_parser_25:
yyaccept = 1; yyaccept = 1;
yych = *(m_marker = ++m_cursor); yych = *(m_marker = ++m_cursor);
goto basic_json_parser_41; goto basic_json_parser_41;
basic_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 basic_json_parser_19;
{ goto basic_json_parser_31;
goto basic_json_parser_19;
}
goto basic_json_parser_31;
basic_json_parser_27: basic_json_parser_27:
++m_cursor; ++m_cursor;
{ { return token_type::end_of_input; }
return token_type::end_of_input;
}
basic_json_parser_29: basic_json_parser_29:
yych = *++m_cursor; yych = *++m_cursor;
goto basic_json_parser_19; goto basic_json_parser_19;
basic_json_parser_30: basic_json_parser_30:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) if (m_limit <= m_cursor) { yyfill(); };
{ yych = *m_cursor;
yyfill();
};
yych = *m_cursor;
basic_json_parser_31: basic_json_parser_31:
if (yybm[0 + yych] & 64) if (yybm[0+yych] & 64) {
{ goto basic_json_parser_30;
goto basic_json_parser_30; }
} if (yych <= 0x00) goto basic_json_parser_32;
if (yych <= 0x00) if (yych <= '"') goto basic_json_parser_34;
{ goto basic_json_parser_33;
goto basic_json_parser_32;
}
if (yych <= '"')
{
goto basic_json_parser_34;
}
goto basic_json_parser_33;
basic_json_parser_32: basic_json_parser_32:
m_cursor = m_marker; m_cursor = m_marker;
if (yyaccept == 0) if (yyaccept == 0) {
{ goto basic_json_parser_19;
goto basic_json_parser_19; } else {
} goto basic_json_parser_24;
else }
{
goto basic_json_parser_24;
}
basic_json_parser_33: basic_json_parser_33:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) if (m_limit <= m_cursor) { yyfill(); };
{ yych = *m_cursor;
yyfill(); if (yych <= 'e') {
}; if (yych <= '/') {
yych = *m_cursor; if (yych == '"') goto basic_json_parser_30;
if (yych <= 'e') if (yych <= '.') goto basic_json_parser_32;
{ goto basic_json_parser_30;
if (yych <= '/') } else {
{ if (yych <= '\\') {
if (yych == '"') if (yych <= '[') goto basic_json_parser_32;
{ goto basic_json_parser_30;
goto basic_json_parser_30; } else {
} if (yych == 'b') goto basic_json_parser_30;
if (yych <= '.') goto basic_json_parser_32;
{
goto basic_json_parser_32;
}
goto basic_json_parser_30;
}
else
{
if (yych <= '\\')
{
if (yych <= '[')
{
goto basic_json_parser_32;
}
goto basic_json_parser_30;
}
else
{
if (yych == 'b')
{
goto basic_json_parser_30;
}
goto basic_json_parser_32;
}
}
} }
else }
{ } else {
if (yych <= 'q') if (yych <= 'q') {
{ if (yych <= 'f') goto basic_json_parser_30;
if (yych <= 'f') if (yych == 'n') goto basic_json_parser_30;
{ goto basic_json_parser_32;
goto basic_json_parser_30; } else {
} if (yych <= 's') {
if (yych == 'n') if (yych <= 'r') goto basic_json_parser_30;
{ goto basic_json_parser_32;
goto basic_json_parser_30; } else {
} if (yych <= 't') goto basic_json_parser_30;
goto basic_json_parser_32; if (yych <= 'u') goto basic_json_parser_36;
} goto basic_json_parser_32;
else
{
if (yych <= 's')
{
if (yych <= 'r')
{
goto basic_json_parser_30;
}
goto basic_json_parser_32;
}
else
{
if (yych <= 't')
{
goto basic_json_parser_30;
}
if (yych <= 'u')
{
goto basic_json_parser_36;
}
goto basic_json_parser_32;
}
}
} }
}
}
basic_json_parser_34: basic_json_parser_34:
++m_cursor; ++m_cursor;
{ { return token_type::value_string; }
return token_type::value_string;
}
basic_json_parser_36: basic_json_parser_36:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) if (m_limit <= m_cursor) { yyfill(); };
{ yych = *m_cursor;
yyfill(); if (yych <= '@') {
}; if (yych <= '/') goto basic_json_parser_32;
yych = *m_cursor; if (yych >= ':') goto basic_json_parser_32;
if (yych <= '@') } else {
{ if (yych <= 'F') goto basic_json_parser_37;
if (yych <= '/') if (yych <= '`') goto basic_json_parser_32;
{ if (yych >= 'g') goto basic_json_parser_32;
goto basic_json_parser_32; }
}
if (yych >= ':')
{
goto basic_json_parser_32;
}
}
else
{
if (yych <= 'F')
{
goto basic_json_parser_37;
}
if (yych <= '`')
{
goto basic_json_parser_32;
}
if (yych >= 'g')
{
goto basic_json_parser_32;
}
}
basic_json_parser_37: basic_json_parser_37:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) if (m_limit <= m_cursor) { yyfill(); };
{ yych = *m_cursor;
yyfill(); if (yych <= '@') {
}; if (yych <= '/') goto basic_json_parser_32;
yych = *m_cursor; if (yych >= ':') goto basic_json_parser_32;
if (yych <= '@') } else {
{ if (yych <= 'F') goto basic_json_parser_38;
if (yych <= '/') if (yych <= '`') goto basic_json_parser_32;
{ if (yych >= 'g') goto basic_json_parser_32;
goto basic_json_parser_32; }
}
if (yych >= ':')
{
goto basic_json_parser_32;
}
}
else
{
if (yych <= 'F')
{
goto basic_json_parser_38;
}
if (yych <= '`')
{
goto basic_json_parser_32;
}
if (yych >= 'g')
{
goto basic_json_parser_32;
}
}
basic_json_parser_38: basic_json_parser_38:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) if (m_limit <= m_cursor) { yyfill(); };
{ yych = *m_cursor;
yyfill(); if (yych <= '@') {
}; if (yych <= '/') goto basic_json_parser_32;
yych = *m_cursor; if (yych >= ':') goto basic_json_parser_32;
if (yych <= '@') } else {
{ if (yych <= 'F') goto basic_json_parser_39;
if (yych <= '/') if (yych <= '`') goto basic_json_parser_32;
{ if (yych >= 'g') goto basic_json_parser_32;
goto basic_json_parser_32; }
}
if (yych >= ':')
{
goto basic_json_parser_32;
}
}
else
{
if (yych <= 'F')
{
goto basic_json_parser_39;
}
if (yych <= '`')
{
goto basic_json_parser_32;
}
if (yych >= 'g')
{
goto basic_json_parser_32;
}
}
basic_json_parser_39: basic_json_parser_39:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) if (m_limit <= m_cursor) { yyfill(); };
{ yych = *m_cursor;
yyfill(); if (yych <= '@') {
}; if (yych <= '/') goto basic_json_parser_32;
yych = *m_cursor; if (yych <= '9') goto basic_json_parser_30;
if (yych <= '@') goto basic_json_parser_32;
{ } else {
if (yych <= '/') if (yych <= 'F') goto basic_json_parser_30;
{ if (yych <= '`') goto basic_json_parser_32;
goto basic_json_parser_32; if (yych <= 'f') goto basic_json_parser_30;
} goto basic_json_parser_32;
if (yych <= '9') }
{
goto basic_json_parser_30;
}
goto basic_json_parser_32;
}
else
{
if (yych <= 'F')
{
goto basic_json_parser_30;
}
if (yych <= '`')
{
goto basic_json_parser_32;
}
if (yych <= 'f')
{
goto basic_json_parser_30;
}
goto basic_json_parser_32;
}
basic_json_parser_40: basic_json_parser_40:
yyaccept = 1; yyaccept = 1;
m_marker = ++m_cursor; m_marker = ++m_cursor;
if ((m_limit - m_cursor) < 3) if ((m_limit - m_cursor) < 3) { yyfill(); };
{ yych = *m_cursor;
yyfill();
};
yych = *m_cursor;
basic_json_parser_41: basic_json_parser_41:
if (yybm[0 + yych] & 128) if (yybm[0+yych] & 128) {
{ goto basic_json_parser_40;
goto basic_json_parser_40; }
} if (yych <= 'D') {
if (yych <= 'D') if (yych != '.') goto basic_json_parser_24;
{ } else {
if (yych != '.') if (yych <= 'E') goto basic_json_parser_43;
{ if (yych == 'e') goto basic_json_parser_43;
goto basic_json_parser_24; goto basic_json_parser_24;
} }
}
else
{
if (yych <= 'E')
{
goto basic_json_parser_43;
}
if (yych == 'e')
{
goto basic_json_parser_43;
}
goto basic_json_parser_24;
}
basic_json_parser_42: basic_json_parser_42:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= '/') if (yych <= '/') goto basic_json_parser_32;
{ if (yych <= '9') goto basic_json_parser_47;
goto basic_json_parser_32; goto basic_json_parser_32;
}
if (yych <= '9')
{
goto basic_json_parser_47;
}
goto basic_json_parser_32;
basic_json_parser_43: basic_json_parser_43:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= ',') if (yych <= ',') {
{ if (yych != '+') goto basic_json_parser_32;
if (yych != '+') } else {
{ if (yych <= '-') goto basic_json_parser_44;
goto basic_json_parser_32; if (yych <= '/') goto basic_json_parser_32;
} if (yych <= '9') goto basic_json_parser_45;
} goto basic_json_parser_32;
else }
{
if (yych <= '-')
{
goto basic_json_parser_44;
}
if (yych <= '/')
{
goto basic_json_parser_32;
}
if (yych <= '9')
{
goto basic_json_parser_45;
}
goto basic_json_parser_32;
}
basic_json_parser_44: basic_json_parser_44:
yych = *++m_cursor; yych = *++m_cursor;
if (yych <= '/') if (yych <= '/') goto basic_json_parser_32;
{ if (yych >= ':') goto basic_json_parser_32;
goto basic_json_parser_32;
}
if (yych >= ':')
{
goto basic_json_parser_32;
}
basic_json_parser_45: basic_json_parser_45:
++m_cursor; ++m_cursor;
if (m_limit <= m_cursor) if (m_limit <= m_cursor) { yyfill(); };
{ yych = *m_cursor;
yyfill(); if (yych <= '/') goto basic_json_parser_24;
}; if (yych <= '9') goto basic_json_parser_45;
yych = *m_cursor; goto basic_json_parser_24;
if (yych <= '/')
{
goto basic_json_parser_24;
}
if (yych <= '9')
{
goto basic_json_parser_45;
}
goto basic_json_parser_24;
basic_json_parser_47: basic_json_parser_47:
yyaccept = 1; yyaccept = 1;
m_marker = ++m_cursor; m_marker = ++m_cursor;
if ((m_limit - m_cursor) < 3) if ((m_limit - m_cursor) < 3) { yyfill(); };
{ yych = *m_cursor;
yyfill(); if (yych <= 'D') {
}; if (yych <= '/') goto basic_json_parser_24;
yych = *m_cursor; if (yych <= '9') goto basic_json_parser_47;
if (yych <= 'D') goto basic_json_parser_24;
{ } else {
if (yych <= '/') if (yych <= 'E') goto basic_json_parser_43;
{ if (yych == 'e') goto basic_json_parser_43;
goto basic_json_parser_24; goto basic_json_parser_24;
} }
if (yych <= '9')
{
goto basic_json_parser_47;
}
goto basic_json_parser_24;
}
else
{
if (yych <= 'E')
{
goto basic_json_parser_43;
}
if (yych == 'e')
{
goto basic_json_parser_43;
}
goto basic_json_parser_24;
}
basic_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 == '.') goto basic_json_parser_42;
if (yych == '.') goto basic_json_parser_24;
{ } else {
goto basic_json_parser_42; if (yych <= 'E') goto basic_json_parser_43;
} if (yych == 'e') goto basic_json_parser_43;
goto basic_json_parser_24; goto basic_json_parser_24;
} }
else
{
if (yych <= 'E')
{
goto basic_json_parser_43;
}
if (yych == 'e')
{
goto basic_json_parser_43;
}
goto basic_json_parser_24;
}
basic_json_parser_50: basic_json_parser_50:
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'l') if (yych != 'l') goto basic_json_parser_32;
{ yych = *++m_cursor;
goto basic_json_parser_32; if (yych != 's') goto basic_json_parser_32;
} yych = *++m_cursor;
yych = *++m_cursor; if (yych != 'e') goto basic_json_parser_32;
if (yych != 's') ++m_cursor;
{ { return token_type::literal_false; }
goto basic_json_parser_32;
}
yych = *++m_cursor;
if (yych != 'e')
{
goto basic_json_parser_32;
}
++m_cursor;
{
return token_type::literal_false;
}
basic_json_parser_55: basic_json_parser_55:
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'u') if (yych != 'u') goto basic_json_parser_32;
{ yych = *++m_cursor;
goto basic_json_parser_32; if (yych != 'e') goto basic_json_parser_32;
} ++m_cursor;
yych = *++m_cursor; { return token_type::literal_true; }
if (yych != 'e')
{
goto basic_json_parser_32;
}
++m_cursor;
{
return token_type::literal_true;
}
basic_json_parser_59: basic_json_parser_59:
yych = *++m_cursor; yych = *++m_cursor;
if (yych != 'l') if (yych != 'l') goto basic_json_parser_32;
{ yych = *++m_cursor;
goto basic_json_parser_32; if (yych != 'l') goto basic_json_parser_32;
} ++m_cursor;
yych = *++m_cursor; { return token_type::literal_null; }
if (yych != 'l') }
{
goto basic_json_parser_32;
}
++m_cursor;
{
return token_type::literal_null;
}
}
} }
...@@ -4544,7 +4152,8 @@ basic_json_parser_59: ...@@ -4544,7 +4152,8 @@ basic_json_parser_59:
/// public parser interface /// public parser interface
inline basic_json parse() inline basic_json parse()
{ {
basic_json result = parse_internal(); basic_json result;
parse_internal(result);
expect(lexer::token_type::end_of_input); expect(lexer::token_type::end_of_input);
...@@ -4553,14 +4162,17 @@ basic_json_parser_59: ...@@ -4553,14 +4162,17 @@ basic_json_parser_59:
private: private:
/// the actual parser /// the actual parser
inline basic_json parse_internal() inline void parse_internal(basic_json& pos)
{ {
switch (last_token) switch (last_token)
{ {
case (lexer::token_type::begin_object): case (lexer::token_type::begin_object):
{ {
// explicitly set result to object to cope with {} // explicitly set result to object to cope with {}
basic_json result(value_t::object); pos.m_type = value_t::object;
AllocatorType<object_t> alloc;
pos.m_value.object = alloc.allocate(1);
alloc.construct(pos.m_value.object);
// read next token // read next token
get_token(); get_token();
...@@ -4569,7 +4181,7 @@ basic_json_parser_59: ...@@ -4569,7 +4181,7 @@ basic_json_parser_59:
if (last_token == lexer::token_type::end_object) if (last_token == lexer::token_type::end_object)
{ {
get_token(); get_token();
return result; return;
} }
// otherwise: parse key-value pairs // otherwise: parse key-value pairs
...@@ -4591,21 +4203,25 @@ basic_json_parser_59: ...@@ -4591,21 +4203,25 @@ basic_json_parser_59:
// parse and add value // parse and add value
get_token(); get_token();
result.m_value.object->emplace(key, parse_internal()); auto it = pos.m_value.object->emplace(key, nullptr);
parse_internal(it.first->second);
} }
while (last_token == lexer::token_type::value_separator); while (last_token == lexer::token_type::value_separator);
// closing } // closing }
expect(lexer::token_type::end_object); expect(lexer::token_type::end_object);
get_token();
return result; get_token();
return;
} }
case (lexer::token_type::begin_array): case (lexer::token_type::begin_array):
{ {
// explicitly set result to object to cope with [] // explicitly set result to object to cope with []
basic_json result(value_t::array); pos.m_type = value_t::array;
AllocatorType<array_t> alloc;
pos.m_value.array = alloc.allocate(1);
alloc.construct(pos.m_value.array);
// read next token // read next token
get_token(); get_token();
...@@ -4614,7 +4230,7 @@ basic_json_parser_59: ...@@ -4614,7 +4230,7 @@ basic_json_parser_59:
if (last_token == lexer::token_type::end_array) if (last_token == lexer::token_type::end_array)
{ {
get_token(); get_token();
return result; return;
} }
// otherwise: parse values // otherwise: parse values
...@@ -4627,40 +4243,51 @@ basic_json_parser_59: ...@@ -4627,40 +4243,51 @@ basic_json_parser_59:
} }
// parse and add value // parse and add value
result.m_value.array->emplace_back(parse_internal()); auto it = pos.m_value.array->insert(pos.m_value.array->end(), nullptr);
parse_internal(*it);
} }
while (last_token == lexer::token_type::value_separator); while (last_token == lexer::token_type::value_separator);
// closing ] // closing ]
expect(lexer::token_type::end_array); expect(lexer::token_type::end_array);
get_token();
return result; get_token();
return;
} }
case (lexer::token_type::literal_null): case (lexer::token_type::literal_null):
{ {
get_token(); get_token();
return basic_json(nullptr); return;
} }
case (lexer::token_type::value_string): case (lexer::token_type::value_string):
{ {
const auto s = m_lexer.get_string(); pos.m_type = value_t::string;
AllocatorType<string_t> alloc;
pos.m_value.string = alloc.allocate(1);
alloc.construct(pos.m_value.string, m_lexer.get_string());
get_token(); get_token();
return basic_json(s); return;
} }
case (lexer::token_type::literal_true): case (lexer::token_type::literal_true):
{ {
pos.m_type = value_t::boolean;
pos.m_value.boolean = true;
get_token(); get_token();
return basic_json(true); return;
} }
case (lexer::token_type::literal_false): case (lexer::token_type::literal_false):
{ {
pos.m_type = value_t::boolean;
pos.m_value.boolean = false;
get_token(); get_token();
return basic_json(false); return;
} }
case (lexer::token_type::value_number): case (lexer::token_type::value_number):
...@@ -4675,20 +4302,23 @@ basic_json_parser_59: ...@@ -4675,20 +4302,23 @@ basic_json_parser_59:
m_lexer.get_token() + " is not a number"); m_lexer.get_token() + " is not a number");
} }
get_token();
// check if conversion loses precision // check if conversion loses precision
const auto int_val = static_cast<number_integer_t>(float_val); const auto int_val = static_cast<number_integer_t>(float_val);
if (approx(float_val, static_cast<number_float_t>(int_val))) if (approx(float_val, static_cast<number_float_t>(int_val)))
{ {
// we basic_json not lose precision -> return int // we basic_json not lose precision -> return int
return basic_json(int_val); pos.m_type = value_t::number_integer;
pos.m_value.number_integer = int_val;
} }
else else
{ {
// we would lose precision -> returnfloat // we would lose precision -> returnfloat
return basic_json(float_val); pos.m_type = value_t::number_float;
pos.m_value.number_float = float_val;
} }
get_token();
return;
} }
default: default:
......
...@@ -416,13 +416,12 @@ class basic_json ...@@ -416,13 +416,12 @@ class basic_json
// the initializer list could describe an object // the initializer list could describe an object
bool is_object = true; bool is_object = true;
// check if each element is an array with two elements whose first element // check if each element is an array with two elements whose first
// is a string // element is a string
for (const auto& element : init) for (const auto& element : init)
{ {
if ((element.m_final and element.m_type == value_t::array) if ((element.m_type != value_t::array or element.size() != 2
or (element.m_type != value_t::array or element.size() != 2 or element[0].m_type != value_t::string))
or element[0].m_type != value_t::string))
{ {
// we found an element that makes it impossible to use the // we found an element that makes it impossible to use the
// initializer list as object // initializer list as object
...@@ -434,9 +433,6 @@ class basic_json ...@@ -434,9 +433,6 @@ class basic_json
// adjust type if type deduction is not wanted // adjust type if type deduction is not wanted
if (not type_deduction) if (not type_deduction)
{ {
// mark this object's type as final
m_final = true;
// if array is wanted, do not create an object though possible // if array is wanted, do not create an object though possible
if (manual_type == value_t::array) if (manual_type == value_t::array)
{ {
...@@ -758,6 +754,12 @@ class basic_json ...@@ -758,6 +754,12 @@ class basic_json
return m_type; return m_type;
} }
/// return the type of the object (implicit)
inline operator value_t() const noexcept
{
return m_type;
}
// return whether value is null // return whether value is null
inline bool is_null() const noexcept inline bool is_null() const noexcept
{ {
...@@ -794,12 +796,6 @@ class basic_json ...@@ -794,12 +796,6 @@ class basic_json
return m_type == value_t::string; return m_type == value_t::string;
} }
/// return the type of the object (implicit)
inline operator value_t() const noexcept
{
return m_type;
}
private: private:
////////////////////// //////////////////////
// value conversion // // value conversion //
...@@ -1083,6 +1079,7 @@ class basic_json ...@@ -1083,6 +1079,7 @@ class basic_json
throw std::runtime_error("cannot use [] with " + type_name()); throw std::runtime_error("cannot use [] with " + type_name());
} }
// fill gaps with null values
for (size_t i = m_value.array->size(); i <= idx; ++i) for (size_t i = m_value.array->size(); i <= idx; ++i)
{ {
m_value.array->push_back(basic_json()); m_value.array->push_back(basic_json());
...@@ -1520,6 +1517,7 @@ class basic_json ...@@ -1520,6 +1517,7 @@ class basic_json
{ {
case (value_t::null): case (value_t::null):
{ {
// null values are empty
return true; return true;
} }
...@@ -1551,6 +1549,7 @@ class basic_json ...@@ -1551,6 +1549,7 @@ class basic_json
{ {
case (value_t::null): case (value_t::null):
{ {
// null values are empty
return 0; return 0;
} }
...@@ -1582,6 +1581,7 @@ class basic_json ...@@ -1582,6 +1581,7 @@ class basic_json
{ {
case (value_t::null): case (value_t::null):
{ {
// null values are empty
return 0; return 0;
} }
...@@ -2258,9 +2258,6 @@ class basic_json ...@@ -2258,9 +2258,6 @@ class basic_json
/// the type of the current element /// the type of the current element
value_t m_type = value_t::null; value_t m_type = value_t::null;
/// whether the type of JSON object may change later
bool m_final = false;
/// the value of the current element /// the value of the current element
json_value m_value = {}; json_value m_value = {};
...@@ -3850,7 +3847,8 @@ class basic_json ...@@ -3850,7 +3847,8 @@ class basic_json
/// public parser interface /// public parser interface
inline basic_json parse() inline basic_json parse()
{ {
basic_json result = parse_internal(); basic_json result;
parse_internal(result);
expect(lexer::token_type::end_of_input); expect(lexer::token_type::end_of_input);
...@@ -3859,14 +3857,17 @@ class basic_json ...@@ -3859,14 +3857,17 @@ class basic_json
private: private:
/// the actual parser /// the actual parser
inline basic_json parse_internal() inline void parse_internal(basic_json& pos)
{ {
switch (last_token) switch (last_token)
{ {
case (lexer::token_type::begin_object): case (lexer::token_type::begin_object):
{ {
// explicitly set result to object to cope with {} // explicitly set result to object to cope with {}
basic_json result(value_t::object); pos.m_type = value_t::object;
AllocatorType<object_t> alloc;
pos.m_value.object = alloc.allocate(1);
alloc.construct(pos.m_value.object);
// read next token // read next token
get_token(); get_token();
...@@ -3875,7 +3876,7 @@ class basic_json ...@@ -3875,7 +3876,7 @@ class basic_json
if (last_token == lexer::token_type::end_object) if (last_token == lexer::token_type::end_object)
{ {
get_token(); get_token();
return result; return;
} }
// otherwise: parse key-value pairs // otherwise: parse key-value pairs
...@@ -3897,21 +3898,25 @@ class basic_json ...@@ -3897,21 +3898,25 @@ class basic_json
// parse and add value // parse and add value
get_token(); get_token();
result.m_value.object->emplace(key, parse_internal()); auto it = pos.m_value.object->emplace(key, nullptr);
parse_internal(it.first->second);
} }
while (last_token == lexer::token_type::value_separator); while (last_token == lexer::token_type::value_separator);
// closing } // closing }
expect(lexer::token_type::end_object); expect(lexer::token_type::end_object);
get_token();
return result; get_token();
return;
} }
case (lexer::token_type::begin_array): case (lexer::token_type::begin_array):
{ {
// explicitly set result to object to cope with [] // explicitly set result to object to cope with []
basic_json result(value_t::array); pos.m_type = value_t::array;
AllocatorType<array_t> alloc;
pos.m_value.array = alloc.allocate(1);
alloc.construct(pos.m_value.array);
// read next token // read next token
get_token(); get_token();
...@@ -3920,7 +3925,7 @@ class basic_json ...@@ -3920,7 +3925,7 @@ class basic_json
if (last_token == lexer::token_type::end_array) if (last_token == lexer::token_type::end_array)
{ {
get_token(); get_token();
return result; return;
} }
// otherwise: parse values // otherwise: parse values
...@@ -3933,40 +3938,51 @@ class basic_json ...@@ -3933,40 +3938,51 @@ class basic_json
} }
// parse and add value // parse and add value
result.m_value.array->emplace_back(parse_internal()); auto it = pos.m_value.array->insert(pos.m_value.array->end(), nullptr);
parse_internal(*it);
} }
while (last_token == lexer::token_type::value_separator); while (last_token == lexer::token_type::value_separator);
// closing ] // closing ]
expect(lexer::token_type::end_array); expect(lexer::token_type::end_array);
get_token();
return result; get_token();
return;
} }
case (lexer::token_type::literal_null): case (lexer::token_type::literal_null):
{ {
get_token(); get_token();
return basic_json(nullptr); return;
} }
case (lexer::token_type::value_string): case (lexer::token_type::value_string):
{ {
const auto s = m_lexer.get_string(); pos.m_type = value_t::string;
AllocatorType<string_t> alloc;
pos.m_value.string = alloc.allocate(1);
alloc.construct(pos.m_value.string, m_lexer.get_string());
get_token(); get_token();
return basic_json(s); return;
} }
case (lexer::token_type::literal_true): case (lexer::token_type::literal_true):
{ {
pos.m_type = value_t::boolean;
pos.m_value.boolean = true;
get_token(); get_token();
return basic_json(true); return;
} }
case (lexer::token_type::literal_false): case (lexer::token_type::literal_false):
{ {
pos.m_type = value_t::boolean;
pos.m_value.boolean = false;
get_token(); get_token();
return basic_json(false); return;
} }
case (lexer::token_type::value_number): case (lexer::token_type::value_number):
...@@ -3981,20 +3997,23 @@ class basic_json ...@@ -3981,20 +3997,23 @@ class basic_json
m_lexer.get_token() + " is not a number"); m_lexer.get_token() + " is not a number");
} }
get_token();
// check if conversion loses precision // check if conversion loses precision
const auto int_val = static_cast<number_integer_t>(float_val); const auto int_val = static_cast<number_integer_t>(float_val);
if (approx(float_val, static_cast<number_float_t>(int_val))) if (approx(float_val, static_cast<number_float_t>(int_val)))
{ {
// we basic_json not lose precision -> return int // we basic_json not lose precision -> return int
return basic_json(int_val); pos.m_type = value_t::number_integer;
pos.m_value.number_integer = int_val;
} }
else else
{ {
// we would lose precision -> returnfloat // we would lose precision -> returnfloat
return basic_json(float_val); pos.m_type = value_t::number_float;
pos.m_value.number_float = float_val;
} }
get_token();
return;
} }
default: default:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment