From 29461ade177eb3dfb2ba5714c4a6bf365b09a24e Mon Sep 17 00:00:00 2001 From: Jannis Harder Date: Fri, 23 Dec 2022 16:52:52 +0100 Subject: 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. --- kernel/json.h | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 kernel/json.h (limited to 'kernel/json.h') diff --git a/kernel/json.h b/kernel/json.h new file mode 100644 index 000000000..3ba355327 --- /dev/null +++ b/kernel/json.h @@ -0,0 +1,93 @@ +/* + * yosys -- Yosys Open SYnthesis Suite + * + * Copyright (C) 2022 Jannis Harder + * + * 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 + +YOSYS_NAMESPACE_BEGIN + +using json11::Json; + +class PrettyJson +{ + enum Scope { + VALUE, + OBJECT_FIRST, + OBJECT, + ARRAY_FIRST, + ARRAY, + }; + + std::string newline_indent = "\n"; + std::vector> targets; + std::vector 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 + void value(T &&value) { value_json(Json(std::forward(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 + void entry(const char *name, T &&value) { entry_json(name, Json(std::forward(value))); }; + + template + void object(const T &&values) + { + begin_object(); + for (auto &item : values) + entry(item.first, item.second); + end_object(); + } + + template + void array(const T &&values) + { + begin_object(); + for (auto &item : values) + value(item); + end_object(); + } +}; + + + +YOSYS_NAMESPACE_END + +#endif -- cgit v1.2.3 From 3e25e61778cc9fe427bf68f45de43f26985b12c3 Mon Sep 17 00:00:00 2001 From: Jannis Harder Date: Fri, 23 Dec 2022 17:22:24 +0100 Subject: aiger: Use new JSON code for writing aiger witness map files --- kernel/json.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'kernel/json.h') diff --git a/kernel/json.h b/kernel/json.h index 3ba355327..ae86b3aa6 100644 --- a/kernel/json.h +++ b/kernel/json.h @@ -38,18 +38,26 @@ class PrettyJson ARRAY, }; + struct Target { + virtual void emit(const char *data) = 0; + virtual void flush() {}; + virtual ~Target() {}; + }; + std::string newline_indent = "\n"; - std::vector> targets; + std::vector> targets; std::vector state = {VALUE}; public: void emit_to_log(); void append_to_string(std::string &target); + bool write_to_file(const std::string &path); bool active() { return !targets.empty(); } void line(); void raw(const char *raw_json); + void flush(); void begin_object(); void begin_array(); void end_object(); -- cgit v1.2.3 From 636b9f27052ef67192ee55a862c31e57a1ccad79 Mon Sep 17 00:00:00 2001 From: Jannis Harder Date: Tue, 3 Jan 2023 14:45:41 +0100 Subject: Support for BTOR witness to Yosys witness conversion --- kernel/json.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'kernel/json.h') diff --git a/kernel/json.h b/kernel/json.h index ae86b3aa6..c9aa0e045 100644 --- a/kernel/json.h +++ b/kernel/json.h @@ -47,6 +47,7 @@ class PrettyJson std::string newline_indent = "\n"; std::vector> targets; std::vector state = {VALUE}; + int compact_depth = INT_MAX; public: void emit_to_log(); @@ -55,7 +56,9 @@ public: bool active() { return !targets.empty(); } - void line(); + void compact() { compact_depth = GetSize(state); } + + void line(bool space_if_inline = true); void raw(const char *raw_json); void flush(); void begin_object(); -- cgit v1.2.3