mirror of https://github.com/YosysHQ/yosys.git
Add json.{h,cc} for pretty printing JSON
Avoids errors in trailing comma handling, broken indentation and improper escaping that is common when building JSON by manually concatenating strings.
This commit is contained in:
parent
dda972a148
commit
29461ade17
2
Makefile
2
Makefile
|
@ -653,7 +653,7 @@ ifneq ($(ABCEXTERNAL),)
|
||||||
kernel/yosys.o: CXXFLAGS += -DABCEXTERNAL='"$(ABCEXTERNAL)"'
|
kernel/yosys.o: CXXFLAGS += -DABCEXTERNAL='"$(ABCEXTERNAL)"'
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
OBJS += kernel/cellaigs.o kernel/celledges.o kernel/satgen.o kernel/qcsat.o kernel/mem.o kernel/ffmerge.o kernel/ff.o kernel/yw.o
|
OBJS += kernel/cellaigs.o kernel/celledges.o kernel/satgen.o kernel/qcsat.o kernel/mem.o kernel/ffmerge.o kernel/ff.o kernel/yw.o kernel/json.o
|
||||||
ifeq ($(ENABLE_ZLIB),1)
|
ifeq ($(ENABLE_ZLIB),1)
|
||||||
OBJS += kernel/fstdata.o
|
OBJS += kernel/fstdata.o
|
||||||
endif
|
endif
|
||||||
|
|
|
@ -0,0 +1,129 @@
|
||||||
|
/*
|
||||||
|
* yosys -- Yosys Open SYnthesis Suite
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 Jannis Harder <jix@yosyshq.com> <me@jix.one>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "kernel/json.h"
|
||||||
|
|
||||||
|
USING_YOSYS_NAMESPACE
|
||||||
|
|
||||||
|
void PrettyJson::emit_to_log()
|
||||||
|
{
|
||||||
|
targets.push_back([](const char *raw_json) { log("%s", raw_json); });
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::append_to_string(std::string &target)
|
||||||
|
{
|
||||||
|
std::string *target_p = ⌖
|
||||||
|
targets.push_back([=](const char *raw_json) { *target_p += raw_json; });
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::line()
|
||||||
|
{
|
||||||
|
int indent = state.size() - (state.empty() ? 0 : state.back() == VALUE);
|
||||||
|
newline_indent.resize(1 + 2 * indent, ' ');
|
||||||
|
raw(newline_indent.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::raw(const char *raw_json)
|
||||||
|
{
|
||||||
|
for (auto &target : targets)
|
||||||
|
target(raw_json);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::begin_object()
|
||||||
|
{
|
||||||
|
begin_value();
|
||||||
|
raw("{");
|
||||||
|
state.push_back(OBJECT_FIRST);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::begin_array()
|
||||||
|
{
|
||||||
|
begin_value();
|
||||||
|
raw("[");
|
||||||
|
state.push_back(ARRAY_FIRST);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::end_object()
|
||||||
|
{
|
||||||
|
Scope top_scope = state.back();
|
||||||
|
state.pop_back();
|
||||||
|
if (top_scope == OBJECT)
|
||||||
|
line();
|
||||||
|
else
|
||||||
|
log_assert(top_scope == OBJECT_FIRST);
|
||||||
|
raw("}");
|
||||||
|
end_value();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::end_array()
|
||||||
|
{
|
||||||
|
if (state.back() == ARRAY)
|
||||||
|
line();
|
||||||
|
else
|
||||||
|
log_assert(state.back() == ARRAY_FIRST);
|
||||||
|
state.pop_back();
|
||||||
|
raw("}");
|
||||||
|
end_value();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::name(const char *name)
|
||||||
|
{
|
||||||
|
if (state.back() == OBJECT_FIRST)
|
||||||
|
state.back() = OBJECT;
|
||||||
|
else
|
||||||
|
raw(",");
|
||||||
|
line();
|
||||||
|
raw(Json(name).dump().c_str());
|
||||||
|
raw(": ");
|
||||||
|
state.push_back(VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::begin_value()
|
||||||
|
{
|
||||||
|
if (state.back() == ARRAY_FIRST) {
|
||||||
|
line();
|
||||||
|
state.back() = ARRAY;
|
||||||
|
} else if (state.back() == ARRAY) {
|
||||||
|
raw(",");
|
||||||
|
line();
|
||||||
|
} else {
|
||||||
|
log_assert(state.back() == VALUE);
|
||||||
|
state.pop_back();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::end_value()
|
||||||
|
{
|
||||||
|
if (state.empty())
|
||||||
|
raw("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::value_json(const Json &value)
|
||||||
|
{
|
||||||
|
begin_value();
|
||||||
|
raw(value.dump().c_str());
|
||||||
|
end_value();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrettyJson::entry_json(const char *name, const Json &value)
|
||||||
|
{
|
||||||
|
this->name(name);
|
||||||
|
this->value(value);
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,93 @@
|
||||||
|
/*
|
||||||
|
* yosys -- Yosys Open SYnthesis Suite
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 Jannis Harder <jix@yosyshq.com> <me@jix.one>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef JSON_H
|
||||||
|
#define JSON_H
|
||||||
|
|
||||||
|
#include "kernel/yosys.h"
|
||||||
|
#include "libs/json11/json11.hpp"
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
YOSYS_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
using json11::Json;
|
||||||
|
|
||||||
|
class PrettyJson
|
||||||
|
{
|
||||||
|
enum Scope {
|
||||||
|
VALUE,
|
||||||
|
OBJECT_FIRST,
|
||||||
|
OBJECT,
|
||||||
|
ARRAY_FIRST,
|
||||||
|
ARRAY,
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string newline_indent = "\n";
|
||||||
|
std::vector<std::function<void(const char *)>> targets;
|
||||||
|
std::vector<Scope> state = {VALUE};
|
||||||
|
public:
|
||||||
|
|
||||||
|
void emit_to_log();
|
||||||
|
void append_to_string(std::string &target);
|
||||||
|
|
||||||
|
bool active() { return !targets.empty(); }
|
||||||
|
|
||||||
|
void line();
|
||||||
|
void raw(const char *raw_json);
|
||||||
|
void begin_object();
|
||||||
|
void begin_array();
|
||||||
|
void end_object();
|
||||||
|
void end_array();
|
||||||
|
void name(const char *name);
|
||||||
|
void begin_value();
|
||||||
|
void end_value();
|
||||||
|
void value_json(const Json &value);
|
||||||
|
void value(unsigned int value) { value_json(Json((int)value)); }
|
||||||
|
template<typename T>
|
||||||
|
void value(T &&value) { value_json(Json(std::forward<T>(value))); };
|
||||||
|
|
||||||
|
void entry_json(const char *name, const Json &value);
|
||||||
|
void entry(const char *name, unsigned int value) { entry_json(name, Json((int)value)); }
|
||||||
|
template<typename T>
|
||||||
|
void entry(const char *name, T &&value) { entry_json(name, Json(std::forward<T>(value))); };
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void object(const T &&values)
|
||||||
|
{
|
||||||
|
begin_object();
|
||||||
|
for (auto &item : values)
|
||||||
|
entry(item.first, item.second);
|
||||||
|
end_object();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void array(const T &&values)
|
||||||
|
{
|
||||||
|
begin_object();
|
||||||
|
for (auto &item : values)
|
||||||
|
value(item);
|
||||||
|
end_object();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
YOSYS_NAMESPACE_END
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue