refactor(tvix): JSONSax: Use a RootValue

More #3377.

Backported from upstream at 9f46f54de4e55267df492456fc0393f74616366

Change-Id: I11bfca4ec56bd4e45291ce3f98a60f198dff0196
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2140
Tested-by: BuildkiteCI
Reviewed-by: andi <andi@notmuch.email>
This commit is contained in:
Eelco Dolstra 2020-04-16 17:28:32 +02:00 committed by glittershark
parent 89f1489916
commit a30e616efb

View file

@ -16,22 +16,21 @@ class JSONSax : nlohmann::json_sax<json> {
class JSONState { class JSONState {
protected: protected:
std::unique_ptr<JSONState> parent; std::unique_ptr<JSONState> parent;
Value* v; std::shared_ptr<Value*> v;
public: public:
virtual std::unique_ptr<JSONState> resolve(EvalState&) { virtual std::unique_ptr<JSONState> resolve(EvalState&) {
throw std::logic_error("tried to close toplevel json parser state"); throw std::logic_error("tried to close toplevel json parser state");
}; }
explicit JSONState(std::unique_ptr<JSONState>&& p) explicit JSONState(std::unique_ptr<JSONState>&& p) : parent(std::move(p)) {}
: parent(std::move(p)), v(nullptr){}; explicit JSONState(Value* v) : v(allocRootValue(v)) {}
explicit JSONState(Value* v) : v(v){};
JSONState(JSONState& p) = delete; JSONState(JSONState& p) = delete;
Value& value(EvalState& state) { Value& value(EvalState& state) {
if (v == nullptr) v = state.allocValue(); if (!v) v = allocRootValue(state.allocValue());
return *v; return **v;
}; }
virtual ~JSONState(){}; virtual ~JSONState() {}
virtual void add(){}; virtual void add() {}
}; };
class JSONObjectState : public JSONState { class JSONObjectState : public JSONState {
@ -63,7 +62,7 @@ class JSONSax : nlohmann::json_sax<json> {
return std::move(parent); return std::move(parent);
} }
void add() override { void add() override {
values.push_back(v); values.push_back(*v);
v = nullptr; v = nullptr;
}; };
@ -87,50 +86,52 @@ class JSONSax : nlohmann::json_sax<json> {
public: public:
JSONSax(EvalState& state, Value& v) : state(state), rs(new JSONState(&v)){}; JSONSax(EvalState& state, Value& v) : state(state), rs(new JSONState(&v)){};
bool null() { return handle_value(mkNull); } bool null() override { return handle_value(mkNull); }
bool boolean(bool val) { return handle_value(mkBool, val); } bool boolean(bool val) override { return handle_value(mkBool, val); }
bool number_integer(number_integer_t val) { return handle_value(mkInt, val); } bool number_integer(number_integer_t val) override {
bool number_unsigned(number_unsigned_t val) {
return handle_value(mkInt, val); return handle_value(mkInt, val);
} }
bool number_float(number_float_t val, const string_t& s) { bool number_unsigned(number_unsigned_t val) override {
return handle_value(mkInt, val);
}
bool number_float(number_float_t val, const string_t&) override {
return handle_value(mkFloat, val); return handle_value(mkFloat, val);
} }
bool string(string_t& val) { bool string(string_t& val) override {
return handle_value<void(Value&, const char*)>(mkString, val.c_str()); return handle_value<void(Value&, const char*)>(mkString, val.c_str());
} }
bool start_object(std::size_t len) { bool start_object(std::size_t) override {
rs = std::make_unique<JSONObjectState>(std::move(rs)); rs = std::make_unique<JSONObjectState>(std::move(rs));
return true; return true;
} }
bool key(string_t& name) { bool key(string_t& name) override {
dynamic_cast<JSONObjectState*>(rs.get())->key(name, state); dynamic_cast<JSONObjectState*>(rs.get())->key(name, state);
return true; return true;
} }
bool end_object() { bool end_object() override {
rs = rs->resolve(state); rs = rs->resolve(state);
rs->add(); rs->add();
return true; return true;
} }
bool end_array() { return end_object(); } bool end_array() override { return end_object(); }
bool start_array(size_t len) { bool start_array(size_t len) override {
rs = std::make_unique<JSONListState>( rs = std::make_unique<JSONListState>(
std::move(rs), len != std::numeric_limits<size_t>::max() ? len : 128); std::move(rs), len != std::numeric_limits<size_t>::max() ? len : 128);
return true; return true;
} }
bool parse_error(std::size_t, const std::string&, bool parse_error(std::size_t, const std::string&,
const nlohmann::detail::exception& ex) { const nlohmann::detail::exception& ex) override {
throw JSONParseError(ex.what()); throw JSONParseError(ex.what());
} }
}; };