feat(tvix): builtins.fromJSON: use nlohmann/json parser instead of custom parser

backported from upstream at f1fac0b5c3b75efab781949fdff2b67ffdda2cb3

Change-Id: I788e3a9b930351118a5f248b356c351afd7f5391
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2138
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
Yorick van Pelt 2020-01-09 17:38:27 +01:00 committed by glittershark
parent de44fdf92a
commit b595553f63

View file

@ -1,185 +1,146 @@
#include "libexpr/json-to-value.hh" #include "libexpr/json-to-value.hh"
#include <cstring> #include <nlohmann/json.hpp>
#include <variant>
#include <vector>
#include "libexpr/value.hh"
using json = nlohmann::json;
namespace nix { namespace nix {
static void skipWhitespace(const char*& s) { // for more information, refer to
while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') { // https://github.com/nlohmann/json/blob/master/include/nlohmann/detail/input/json_sax.hpp
s++; class JSONSax : nlohmann::json_sax<json> {
} class JSONState {
} protected:
JSONState* parent;
Value* v;
static std::string parseJSONString(const char*& s) { public:
std::string res; virtual JSONState* resolve(EvalState&) {
if (*s++ != '"') { throw std::logic_error("tried to close toplevel json parser state");
throw JSONParseError("expected JSON string"); };
} explicit JSONState(JSONState* p) : parent(p), v(nullptr){};
while (*s != '"') { explicit JSONState(Value* v) : v(v){};
if (*s == 0) { JSONState(JSONState& p) = delete;
throw JSONParseError("got end-of-string in JSON string"); Value& value(EvalState& state) {
} if (v == nullptr) v = state.allocValue();
if (*s == '\\') { return *v;
s++; };
if (*s == '"') { virtual ~JSONState(){};
res += '"'; virtual void add(){};
} else if (*s == '\\') { };
res += '\\';
} else if (*s == '/') {
res += '/';
} else if (*s == '/') {
res += '/';
} else if (*s == 'b') {
res += '\b';
} else if (*s == 'f') {
res += '\f';
} else if (*s == 'n') {
res += '\n';
} else if (*s == 'r') {
res += '\r';
} else if (*s == 't') {
res += '\t';
} else if (*s == 'u') {
throw JSONParseError(
"\\u characters in JSON strings are currently not supported");
} else {
throw JSONParseError("invalid escaped character in JSON string");
}
s++;
} else {
res += *s++;
}
}
s++;
return res;
}
static void parseJSON(EvalState& state, const char*& s, Value& v) { class JSONObjectState : public JSONState {
skipWhitespace(s); using JSONState::JSONState;
ValueMap attrs = ValueMap();
virtual JSONState* resolve(EvalState& state) override {
Value& v = parent->value(state);
state.mkAttrs(v, attrs.size());
for (auto& i : attrs) v.attrs->push_back(Attr(i.first, i.second));
return parent;
}
virtual void add() override { v = nullptr; };
if (*s == 0) { public:
throw JSONParseError("expected JSON value"); void key(string_t& name, EvalState& state) {
attrs[state.symbols.Create(name)] = &value(state);
} }
};
if (*s == '[') { class JSONListState : public JSONState {
s++; std::vector<Value*> values;
NixList values; virtual JSONState* resolve(EvalState& state) override {
values.reserve(128); Value& v = parent->value(state);
skipWhitespace(s);
while (true) {
if (values.empty() && *s == ']') {
break;
}
Value* v2 = state.allocValue();
parseJSON(state, s, *v2);
values.push_back(v2);
skipWhitespace(s);
if (*s == ']') {
break;
}
if (*s != ',') {
throw JSONParseError("expected ',' or ']' after JSON array element");
}
s++;
}
s++;
state.mkList(v, values.size()); state.mkList(v, values.size());
for (size_t n = 0; n < values.size(); ++n) { for (size_t n = 0; n < values.size(); ++n) {
(*v.list)[n] = values[n]; (*v.list)[n] = values[n];
} }
return parent;
} }
else if (*s == '{') { void add() override {
s++; values.push_back(v);
ValueMap attrs; v = nullptr;
while (true) { };
skipWhitespace(s);
if (attrs.empty() && *s == '}') { public:
break; JSONListState(JSONState* p, std::size_t reserve) : JSONState(p) {
values.reserve(reserve);
} }
std::string name = parseJSONString(s); };
skipWhitespace(s);
if (*s != ':') { EvalState& state;
throw JSONParseError("expected ':' in JSON object"); JSONState* rs;
}
s++; template <typename T, typename... Args>
Value* v2 = state.allocValue(); inline bool handle_value(T f, Args... args) {
parseJSON(state, s, *v2); f(rs->value(state), args...);
attrs[state.symbols.Create(name)] = v2; rs->add();
skipWhitespace(s); return true;
if (*s == '}') {
break;
}
if (*s != ',') {
throw JSONParseError("expected ',' or '}' after JSON member");
}
s++;
}
state.mkAttrs(v, attrs.size());
for (auto& i : attrs) {
v.attrs->push_back(Attr(i.first, i.second));
}
s++;
} }
else if (*s == '"') { public:
mkString(v, parseJSONString(s)); JSONSax(EvalState& state, Value& v) : state(state), rs(new JSONState(&v)){};
~JSONSax() { delete rs; };
bool null() { return handle_value(mkNull); }
bool boolean(bool val) { return handle_value(mkBool, val); }
bool number_integer(number_integer_t val) { return handle_value(mkInt, val); }
bool number_unsigned(number_unsigned_t val) {
return handle_value(mkInt, val);
} }
else if ((isdigit(*s) != 0) || *s == '-' || *s == '.') { bool number_float(number_float_t val, const string_t& s) {
// Buffer into a std::string first, then use built-in C++ conversions return handle_value(mkFloat, val);
std::string tmp_number;
ValueType number_type = tInt;
while ((isdigit(*s) != 0) || *s == '-' || *s == '.' || *s == 'e' ||
*s == 'E') {
if (*s == '.' || *s == 'e' || *s == 'E') {
number_type = tFloat;
}
tmp_number += *s++;
} }
try { bool string(string_t& val) {
if (number_type == tFloat) { return handle_value<void(Value&, const char*)>(mkString, val.c_str());
mkFloat(v, stod(tmp_number));
} else {
mkInt(v, stol(tmp_number));
}
} catch (std::invalid_argument& e) {
throw JSONParseError("invalid JSON number");
} catch (std::out_of_range& e) {
throw JSONParseError("out-of-range JSON number");
}
} }
else if (strncmp(s, "true", 4) == 0) { bool start_object(std::size_t len) {
s += 4; JSONState* old = rs;
mkBool(v, true); rs = new JSONObjectState(old);
return true;
} }
else if (strncmp(s, "false", 5) == 0) { bool key(string_t& name) {
s += 5; dynamic_cast<JSONObjectState*>(rs)->key(name, state);
mkBool(v, false); return true;
} }
else if (strncmp(s, "null", 4) == 0) { bool end_object() {
s += 4; JSONState* old = rs;
mkNull(v); rs = old->resolve(state);
delete old;
rs->add();
return true;
} }
else { bool end_array() { return end_object(); }
throw JSONParseError("unrecognised JSON value");
bool start_array(size_t len) {
JSONState* old = rs;
rs = new JSONListState(
old, len != std::numeric_limits<size_t>::max() ? len : 128);
return true;
} }
}
bool parse_error(std::size_t, const std::string&,
const nlohmann::detail::exception& ex) {
throw JSONParseError(ex.what());
}
};
void parseJSON(EvalState& state, const std::string& s_, Value& v) { void parseJSON(EvalState& state, const std::string& s_, Value& v) {
const char* s = s_.c_str(); JSONSax parser(state, v);
parseJSON(state, s, v); bool res = json::sax_parse(s_, &parser);
skipWhitespace(s); if (!res) throw JSONParseError("Invalid JSON Value");
if (*s != 0) {
throw JSONParseError(
format("expected end-of-string while parsing JSON value: %1%") % s);
}
} }
} // namespace nix } // namespace nix