()
{
return make_table();
}
+} // namespace detail
template
std::shared_ptr value::clone() const
@@ -1702,7 +1727,7 @@ inline std::shared_ptr array::clone() const
inline std::shared_ptr table_array::clone() const
{
- auto result = make_table_array();
+ auto result = make_table_array(is_inline());
result->reserve(array_.size());
for (const auto& ptr : array_)
result->array_.push_back(ptr->clone()->as_table());
@@ -1738,6 +1763,11 @@ inline bool is_number(char c)
return c >= '0' && c <= '9';
}
+inline bool is_hex(char c)
+{
+ return is_number(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
+}
+
/**
* Helper object for consuming expected characters.
*/
@@ -1766,6 +1796,13 @@ class consumer
[&](char c) { (*this)(c); });
}
+ void eat_or(char a, char b)
+ {
+ if (it_ == end_ || (*it_ != a && *it_ != b))
+ on_error_();
+ ++it_;
+ }
+
int eat_digits(int len)
{
int val = 0;
@@ -1830,7 +1867,7 @@ inline std::istream& getline(std::istream& input, std::string& line)
line.push_back(static_cast(c));
}
}
-}
+} // namespace detail
/**
* The parser class.
@@ -1914,21 +1951,25 @@ class parser
std::string full_table_name;
bool inserted = false;
- while (it != end && *it != ']')
- {
- auto part = parse_key(it, end,
- [](char c) { return c == '.' || c == ']'; });
+ auto key_end = [](char c) { return c == ']'; };
+
+ auto key_part_handler = [&](const std::string& part) {
if (part.empty())
throw_parse_exception("Empty component of table name");
if (!full_table_name.empty())
- full_table_name += ".";
+ full_table_name += '.';
full_table_name += part;
if (curr_table->contains(part))
{
+#if !defined(__PGI)
auto b = curr_table->get(part);
+#else
+ // Workaround for PGI compiler
+ std::shared_ptr b = curr_table->get(part);
+#endif
if (b->is_table())
curr_table = static_cast
(b.get());
else if (b->is_table_array())
@@ -1946,16 +1987,23 @@ class parser
curr_table->insert(part, make_table());
curr_table = static_cast
(curr_table->get(part).get());
}
- consume_whitespace(it, end);
- if (it != end && *it == '.')
- ++it;
- consume_whitespace(it, end);
- }
+ };
+
+ key_part_handler(parse_key(it, end, key_end, key_part_handler));
if (it == end)
throw_parse_exception(
"Unterminated table declaration; did you forget a ']'?");
+ if (*it != ']')
+ {
+ std::string errmsg{"Unexpected character in table definition: "};
+ errmsg += '"';
+ errmsg += *it;
+ errmsg += '"';
+ throw_parse_exception(errmsg);
+ }
+
// table already existed
if (!inserted)
{
@@ -1969,8 +2017,9 @@ class parser
// since it has already been defined. If there aren't any
// values, then it was implicitly created by something like
// [a.b]
- if (curr_table->empty() || std::any_of(curr_table->begin(),
- curr_table->end(), is_value))
+ if (curr_table->empty()
+ || std::any_of(curr_table->begin(), curr_table->end(),
+ is_value))
{
throw_parse_exception("Redefinition of table "
+ full_table_name);
@@ -1989,36 +2038,45 @@ class parser
if (it == end || *it == ']')
throw_parse_exception("Table array name cannot be empty");
- std::string full_ta_name;
- while (it != end && *it != ']')
- {
- auto part = parse_key(it, end,
- [](char c) { return c == '.' || c == ']'; });
+ auto key_end = [](char c) { return c == ']'; };
+ std::string full_ta_name;
+ auto key_part_handler = [&](const std::string& part) {
if (part.empty())
throw_parse_exception("Empty component of table array name");
if (!full_ta_name.empty())
- full_ta_name += ".";
+ full_ta_name += '.';
full_ta_name += part;
- consume_whitespace(it, end);
- if (it != end && *it == '.')
- ++it;
- consume_whitespace(it, end);
-
if (curr_table->contains(part))
{
+#if !defined(__PGI)
auto b = curr_table->get(part);
+#else
+ // Workaround for PGI compiler
+ std::shared_ptr b = curr_table->get(part);
+#endif
// if this is the end of the table array name, add an
- // element to the table array that we just looked up
+ // element to the table array that we just looked up,
+ // provided it was not declared inline
if (it != end && *it == ']')
{
if (!b->is_table_array())
+ {
throw_parse_exception("Key " + full_ta_name
+ " is not a table array");
+ }
+
auto v = b->as_table_array();
+
+ if (v->is_inline())
+ {
+ throw_parse_exception("Static array " + full_ta_name
+ + " cannot be appended to");
+ }
+
v->get().push_back(make_table());
curr_table = v->get().back().get();
}
@@ -2059,15 +2117,16 @@ class parser
= static_cast
(curr_table->get(part).get());
}
}
- }
+ };
+
+ key_part_handler(parse_key(it, end, key_end, key_part_handler));
// consume the last "]]"
- if (it == end)
+ auto eat = make_consumer(it, end, [this]() {
throw_parse_exception("Unterminated table array name");
- ++it;
- if (it == end)
- throw_parse_exception("Unterminated table array name");
- ++it;
+ });
+ eat(']');
+ eat(']');
consume_whitespace(it, end);
eol_or_comment(it, end);
@@ -2076,7 +2135,35 @@ class parser
void parse_key_value(std::string::iterator& it, std::string::iterator& end,
table* curr_table)
{
- auto key = parse_key(it, end, [](char c) { return c == '='; });
+ auto key_end = [](char c) { return c == '='; };
+
+ auto key_part_handler = [&](const std::string& part) {
+ // two cases: this key part exists already, in which case it must
+ // be a table, or it doesn't exist in which case we must create
+ // an implicitly defined table
+ if (curr_table->contains(part))
+ {
+ auto val = curr_table->get(part);
+ if (val->is_table())
+ {
+ curr_table = static_cast
(val.get());
+ }
+ else
+ {
+ throw_parse_exception("Key " + part
+ + " already exists as a value");
+ }
+ }
+ else
+ {
+ auto newtable = make_table();
+ curr_table->insert(part, newtable);
+ curr_table = newtable.get();
+ }
+ };
+
+ auto key = parse_key(it, end, key_end, key_part_handler);
+
if (curr_table->contains(key))
throw_parse_exception("Key " + key + " already present");
if (it == end || *it != '=')
@@ -2087,18 +2174,57 @@ class parser
consume_whitespace(it, end);
}
- template
- std::string parse_key(std::string::iterator& it,
- const std::string::iterator& end, Function&& fun)
+ template
+ std::string
+ parse_key(std::string::iterator& it, const std::string::iterator& end,
+ KeyEndFinder&& key_end, KeyPartHandler&& key_part_handler)
+ {
+ // parse the key as a series of one or more simple-keys joined with '.'
+ while (it != end && !key_end(*it))
+ {
+ auto part = parse_simple_key(it, end);
+ consume_whitespace(it, end);
+
+ if (it == end || key_end(*it))
+ {
+ return part;
+ }
+
+ if (*it != '.')
+ {
+ std::string errmsg{"Unexpected character in key: "};
+ errmsg += '"';
+ errmsg += *it;
+ errmsg += '"';
+ throw_parse_exception(errmsg);
+ }
+
+ key_part_handler(part);
+
+ // consume the dot
+ ++it;
+ }
+
+ throw_parse_exception("Unexpected end of key");
+ }
+
+ std::string parse_simple_key(std::string::iterator& it,
+ const std::string::iterator& end)
{
consume_whitespace(it, end);
- if (*it == '"')
+
+ if (it == end)
+ throw_parse_exception("Unexpected end of key (blank key?)");
+
+ if (*it == '"' || *it == '\'')
{
- return parse_quoted_key(it, end);
+ return string_literal(it, end, *it);
}
else
{
- auto bke = std::find_if(it, end, std::forward(fun));
+ auto bke = std::find_if(it, end, [](char c) {
+ return c == '.' || c == '=' || c == ']';
+ });
return parse_bare_key(it, bke);
}
}
@@ -2142,12 +2268,6 @@ class parser
return key;
}
- std::string parse_quoted_key(std::string::iterator& it,
- const std::string::iterator& end)
- {
- return string_literal(it, end, '"');
- }
-
enum class parse_type
{
STRING = 1,
@@ -2193,7 +2313,7 @@ class parser
parse_type determine_value_type(const std::string::iterator& it,
const std::string::iterator& end)
{
- if(it == end)
+ if (it == end)
{
throw_parse_exception("Failed to parse value type");
}
@@ -2209,7 +2329,11 @@ class parser
{
return *dtype;
}
- else if (is_number(*it) || *it == '-' || *it == '+')
+ else if (is_number(*it) || *it == '-' || *it == '+'
+ || (*it == 'i' && it + 1 != end && it[1] == 'n'
+ && it + 2 != end && it[2] == 'f')
+ || (*it == 'n' && it + 1 != end && it[1] == 'a'
+ && it + 2 != end && it[2] == 'n'))
{
return determine_number_type(it, end);
}
@@ -2235,6 +2359,13 @@ class parser
auto check_it = it;
if (*check_it == '-' || *check_it == '+')
++check_it;
+
+ if (check_it == end)
+ throw_parse_exception("Malformed number");
+
+ if (*check_it == 'i' || *check_it == 'n')
+ return parse_type::FLOAT;
+
while (check_it != end && is_number(*check_it))
++check_it;
if (check_it != end && *check_it == '.')
@@ -2283,57 +2414,56 @@ class parser
bool consuming = false;
std::shared_ptr> ret;
- auto handle_line
- = [&](std::string::iterator& local_it,
- std::string::iterator& local_end) {
- if (consuming)
- {
- local_it = std::find_if_not(local_it, local_end, is_ws);
+ auto handle_line = [&](std::string::iterator& local_it,
+ std::string::iterator& local_end) {
+ if (consuming)
+ {
+ local_it = std::find_if_not(local_it, local_end, is_ws);
- // whole line is whitespace
- if (local_it == local_end)
- return;
- }
+ // whole line is whitespace
+ if (local_it == local_end)
+ return;
+ }
- consuming = false;
+ consuming = false;
- while (local_it != local_end)
- {
- // handle escaped characters
- if (delim == '"' && *local_it == '\\')
- {
- auto check = local_it;
- // check if this is an actual escape sequence or a
- // whitespace escaping backslash
- ++check;
- consume_whitespace(check, local_end);
- if (check == local_end)
- {
- consuming = true;
- break;
- }
+ while (local_it != local_end)
+ {
+ // handle escaped characters
+ if (delim == '"' && *local_it == '\\')
+ {
+ auto check = local_it;
+ // check if this is an actual escape sequence or a
+ // whitespace escaping backslash
+ ++check;
+ consume_whitespace(check, local_end);
+ if (check == local_end)
+ {
+ consuming = true;
+ break;
+ }
- ss << parse_escape_code(local_it, local_end);
- continue;
- }
+ ss << parse_escape_code(local_it, local_end);
+ continue;
+ }
- // if we can end the string
- if (std::distance(local_it, local_end) >= 3)
- {
- auto check = local_it;
- // check for """
- if (*check++ == delim && *check++ == delim
- && *check++ == delim)
- {
- local_it = check;
- ret = make_value(ss.str());
- break;
- }
- }
+ // if we can end the string
+ if (std::distance(local_it, local_end) >= 3)
+ {
+ auto check = local_it;
+ // check for """
+ if (*check++ == delim && *check++ == delim
+ && *check++ == delim)
+ {
+ local_it = check;
+ ret = make_value(ss.str());
+ break;
+ }
+ }
- ss << *local_it++;
- }
- };
+ ss << *local_it++;
+ }
+ };
// handle the remainder of the current line
handle_line(it, end);
@@ -2514,17 +2644,13 @@ class parser
return value;
}
- bool is_hex(char c)
- {
- return is_number(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
- }
-
uint32_t hex_to_digit(char c)
{
if (is_number(c))
return static_cast(c - '0');
- return 10 + static_cast(
- c - ((c >= 'a' && c <= 'f') ? 'a' : 'A'));
+ return 10
+ + static_cast(c
+ - ((c >= 'a' && c <= 'f') ? 'a' : 'A'));
}
std::shared_ptr parse_number(std::string::iterator& it,
@@ -2538,25 +2664,6 @@ class parser
++check_it;
};
- eat_sign();
-
- auto eat_numbers = [&]() {
- auto beg = check_it;
- while (check_it != end && is_number(*check_it))
- {
- ++check_it;
- if (check_it != end && *check_it == '_')
- {
- ++check_it;
- if (check_it == end || !is_number(*check_it))
- throw_parse_exception("Malformed number");
- }
- }
-
- if (check_it == beg)
- throw_parse_exception("Malformed number");
- };
-
auto check_no_leading_zero = [&]() {
if (check_it != end && *check_it == '0' && check_it + 1 != check_end
&& check_it[1] != '.')
@@ -2565,7 +2672,80 @@ class parser
}
};
+ auto eat_digits = [&](bool (*check_char)(char)) {
+ auto beg = check_it;
+ while (check_it != end && check_char(*check_it))
+ {
+ ++check_it;
+ if (check_it != end && *check_it == '_')
+ {
+ ++check_it;
+ if (check_it == end || !check_char(*check_it))
+ throw_parse_exception("Malformed number");
+ }
+ }
+
+ if (check_it == beg)
+ throw_parse_exception("Malformed number");
+ };
+
+ auto eat_hex = [&]() { eat_digits(&is_hex); };
+
+ auto eat_numbers = [&]() { eat_digits(&is_number); };
+
+ if (check_it != end && *check_it == '0' && check_it + 1 != check_end
+ && (check_it[1] == 'x' || check_it[1] == 'o' || check_it[1] == 'b'))
+ {
+ ++check_it;
+ char base = *check_it;
+ ++check_it;
+ if (base == 'x')
+ {
+ eat_hex();
+ return parse_int(it, check_it, 16);
+ }
+ else if (base == 'o')
+ {
+ auto start = check_it;
+ eat_numbers();
+ auto val = parse_int(start, check_it, 8, "0");
+ it = start;
+ return val;
+ }
+ else // if (base == 'b')
+ {
+ auto start = check_it;
+ eat_numbers();
+ auto val = parse_int(start, check_it, 2);
+ it = start;
+ return val;
+ }
+ }
+
+ eat_sign();
check_no_leading_zero();
+
+ if (check_it != end && check_it + 1 != end && check_it + 2 != end)
+ {
+ if (check_it[0] == 'i' && check_it[1] == 'n' && check_it[2] == 'f')
+ {
+ auto val = std::numeric_limits::infinity();
+ if (*it == '-')
+ val = -val;
+ it = check_it + 3;
+ return make_value(val);
+ }
+ else if (check_it[0] == 'n' && check_it[1] == 'a'
+ && check_it[2] == 'n')
+ {
+ auto val = std::numeric_limits::quiet_NaN();
+ if (*it == '-')
+ val = -val;
+ it = check_it + 3;
+ return make_value(val);
+ }
+ }
+
eat_numbers();
if (check_it != end
@@ -2604,14 +2784,17 @@ class parser
}
std::shared_ptr> parse_int(std::string::iterator& it,
- const std::string::iterator& end)
+ const std::string::iterator& end,
+ int base = 10,
+ const char* prefix = "")
{
std::string v{it, end};
+ v = prefix + v;
v.erase(std::remove(v.begin(), v.end(), '_'), v.end());
it = end;
try
{
- return make_value(std::stoll(v));
+ return make_value(std::stoll(v, nullptr, base));
}
catch (const std::invalid_argument& ex)
{
@@ -2674,18 +2857,33 @@ class parser
std::string::iterator find_end_of_number(std::string::iterator it,
std::string::iterator end)
{
- return std::find_if(it, end, [](char c) {
+ auto ret = std::find_if(it, end, [](char c) {
return !is_number(c) && c != '_' && c != '.' && c != 'e' && c != 'E'
- && c != '-' && c != '+';
+ && c != '-' && c != '+' && c != 'x' && c != 'o' && c != 'b';
});
+ if (ret != end && ret + 1 != end && ret + 2 != end)
+ {
+ if ((ret[0] == 'i' && ret[1] == 'n' && ret[2] == 'f')
+ || (ret[0] == 'n' && ret[1] == 'a' && ret[2] == 'n'))
+ {
+ ret = ret + 3;
+ }
+ }
+ return ret;
}
std::string::iterator find_end_of_date(std::string::iterator it,
std::string::iterator end)
{
- return std::find_if(it, end, [](char c) {
- return !is_number(c) && c != 'T' && c != 'Z' && c != ':' && c != '-'
- && c != '+' && c != '.';
+ auto end_of_date = std::find_if(it, end, [](char c) {
+ return !is_number(c) && c != '-';
+ });
+ if (end_of_date != end && *end_of_date == ' ' && end_of_date + 1 != end
+ && is_number(end_of_date[1]))
+ end_of_date++;
+ return std::find_if(end_of_date, end, [](char c) {
+ return !is_number(c) && c != 'T' && c != 'Z' && c != ':'
+ && c != '-' && c != '+' && c != '.';
});
}
@@ -2754,7 +2952,7 @@ class parser
if (it == date_end)
return make_value(ldate);
- eat('T');
+ eat.eat_or('T', ' ');
local_datetime ldt;
static_cast(ldt) = ldate;
@@ -2850,9 +3048,9 @@ class parser
auto arr = make_array();
while (it != end && *it != ']')
{
- auto value = parse_value(it, end);
- if (auto v = value->as())
- arr->get().push_back(value);
+ auto val = parse_value(it, end);
+ if (auto v = val->as())
+ arr->get().push_back(val);
else
throw_parse_exception("Arrays must be homogeneous");
skip_whitespace_and_comments(it, end);
@@ -2871,7 +3069,7 @@ class parser
std::string::iterator& it,
std::string::iterator& end)
{
- auto arr = make_element