Changeset View
Changeset View
Standalone View
Standalone View
intern/cycles/lpe/dump.cpp
- This file was added.
| /* SPDX-License-Identifier: Apache-2.0 | |||||
| * Copyright 2022 Blender Foundation */ | |||||
| #include "dump.h" | |||||
| #include <fstream> | |||||
| #include <stack> | |||||
| #include "lpe/json.hpp" | |||||
| #include "lpe/lpexp.h" | |||||
| using json = nlohmann::json; | |||||
| namespace LPE { | |||||
| struct JsonSerializerVisitor : public lpexp::LPexpVisitor { | |||||
| json root; | |||||
| std::stack<json> parent; | |||||
| bool enter(const lpexp::Cat & /*exp*/) override | |||||
| { | |||||
| parent.emplace(json{{"type", "Cat"}, {"children", json::array()}}); | |||||
| return true; | |||||
| } | |||||
| bool enter(const lpexp::Symbol &exp) override | |||||
| { | |||||
| parent.emplace(json{{"type", "Symbol"}, {"value", exp.m_sym}}); | |||||
| return true; | |||||
| } | |||||
| bool enter(const lpexp::Wildexp &exp) override | |||||
| { | |||||
| const auto &wildcard = exp.m_wildcard; | |||||
| parent.emplace(json{{"type", "Wildexp"}, {"minus", wildcard.m_minus}}); | |||||
| return true; | |||||
| } | |||||
| bool enter(const lpexp::Orlist & /*exp*/) override | |||||
| { | |||||
| parent.emplace(json{{"type", "Or"}, {"children", json::array()}}); | |||||
| return true; | |||||
| } | |||||
| bool enter(const lpexp::Repeat & /*exp*/) override | |||||
| { | |||||
| parent.emplace(json{{"type", "Repeat"}, {"children", json::array()}}); | |||||
| return true; | |||||
| } | |||||
| bool enter(const lpexp::NRepeat &exp) override | |||||
| { | |||||
| parent.emplace(json{ | |||||
| {"type", "NRepeat"}, {"min", exp.m_min}, {"max", exp.m_max}, {"children", json::array()}}); | |||||
| return true; | |||||
| } | |||||
| void leave(const lpexp::LPexp & /*exp*/) override | |||||
| { | |||||
| const auto object = parent.top(); | |||||
| parent.pop(); | |||||
| if (!parent.empty()) { | |||||
| parent.top()["children"].emplace_back(object); | |||||
| } | |||||
| else { | |||||
| root = object; | |||||
| } | |||||
| } | |||||
| }; | |||||
| void dump_json(std::string filepath, | |||||
| const std::vector<lpexp::LPexp *> &expressions, | |||||
| std::function<std::string(int)> get_name, | |||||
| std::function<std::string(int)> get_expr) | |||||
| { | |||||
| json lpexpJson; | |||||
| int index = 0; | |||||
| for (lpexp::LPexp *expression : expressions) { | |||||
| JsonSerializerVisitor v; | |||||
| expression->accept(v); | |||||
| auto name = get_name(index); | |||||
| auto expr = get_expr(index); | |||||
| json object; | |||||
| object["name"] = name; | |||||
| object["lpe"] = expr; | |||||
| object["tree"] = v.root; | |||||
| lpexpJson[name] = object; | |||||
| index++; | |||||
| } | |||||
| std::ofstream jsonOut(filepath); | |||||
| jsonOut << lpexpJson.dump(2); | |||||
| } | |||||
| } // namespace LPE | |||||