Skip to content

Commit

Permalink
[WIP] Add formatting for json output
Browse files Browse the repository at this point in the history
Use one space indent per-level, and one line per node rather
than one big, unformatted line for whole tree.

TODO: Cleanup and fix output of redundant newlines (resulting in
empty lines)
  • Loading branch information
sgizler committed Nov 27, 2023
1 parent 1c1570c commit 18548db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/V3Ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -2220,7 +2220,7 @@ class AstNode VL_NOT_FINAL {
void dumpTreeDot(std::ostream& os = std::cout) const;
void dumpTreeDotFile(const string& filename, bool doDump = true);
virtual void dumpExtraJson(std::ostream& os) const {}; // node specific fields
void dumpTreeJson(std::ostream& os) const;
void dumpTreeJson(std::ostream& os, const string& indent = "") const;
void dumpTreeJsonFile(const string& filename, bool doDump = true);

// METHODS - static advancement
Expand Down
38 changes: 26 additions & 12 deletions src/V3AstNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1441,15 +1441,29 @@ void AstNode::dump(std::ostream& str) const {
}
}

static void dumpNodeListJson(std::ostream& os, const AstNode* nodep, const std::string& listName) {
os << "," << SQUOT(listName) << ": [";
bool comma = false;

static void dumpNodeListJson(std::ostream& os, const AstNode* nodep, const std::string& listName, const string& indent) {
// yeah, it is a bit dirty, TODO: cleanup
bool empty = (nodep == NULL); // TODO: Consider not dumping empty lists at all

if(listName == "op1") {
os << ",";
}

if(!empty) os << "\n " << indent;

os << SQUOT(listName) << ": [";
if(!empty) os << "\n";
for (; nodep; nodep = nodep->nextp()) {
if (comma) os << ", ";
comma = true;
nodep->dumpTreeJson(os);
nodep->dumpTreeJson(os, indent + " ");
if (nodep->nextp()) os << ",\n";
}
os << "]";

if(listName != "op4") { // dirty trick to check if it is not last field
os << ",";
if(!empty) os << "\n" << indent;
}
}

static void dumpFileInfo(std::ostream& os, const FileLine* fileinfop) {
Expand All @@ -1460,20 +1474,20 @@ static void dumpFileInfo(std::ostream& os, const FileLine* fileinfop) {
os << "}";
}

void AstNode::dumpTreeJson(std::ostream& os) const {
void AstNode::dumpTreeJson(std::ostream& os, const string& indent) const {
// TODO: dump dtype
os << "{" << SQUOT("type") << ":" << SQUOT(typeName());
os << indent << "{" << SQUOT("type") << ":" << SQUOT(typeName());
dumpJsonStr(os, "name", V3OutFormatter::quoteNameControls(name()));
dumpJsonPtr(os, "addr", this);
dumpFileInfo(os, fileline());
#ifdef VL_DEBUG
dumpJsonNum(os, "editNum", editCount());
#endif
dumpExtraJson(os);
dumpNodeListJson(os, op1p(), "op1");
dumpNodeListJson(os, op2p(), "op2");
dumpNodeListJson(os, op3p(), "op3");
dumpNodeListJson(os, op4p(), "op4");
dumpNodeListJson(os, op1p(), "op1", indent);
dumpNodeListJson(os, op2p(), "op2", indent);
dumpNodeListJson(os, op3p(), "op3", indent);
dumpNodeListJson(os, op4p(), "op4", indent);
os << "}";
}

Expand Down

0 comments on commit 18548db

Please sign in to comment.