Skip to content

Commit

Permalink
Add formatting for json output
Browse files Browse the repository at this point in the history
Use indentation and one line per node rather than one big,
unformatted line for whole tree.
  • Loading branch information
sgizler committed Nov 28, 2023
1 parent 4481206 commit b55edf4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 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
33 changes: 19 additions & 14 deletions src/V3AstNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1441,15 +1441,20 @@ 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;
for (; nodep; nodep = nodep->nextp()) {
if (comma) os << ", ";
comma = true;
nodep->dumpTreeJson(os);
static void dumpNodeListJson(std::ostream& os, const AstNode* nodep, const std::string& listName,
const string& indent) {
os << ",";
if (nodep == NULL) { // empty list, print inline
os << SQUOT(listName) << ": []";
} else {
os << "\n" << indent << SQUOT(listName) << ": [\n";
os << indent + " "; // we have to indent 1st node here as dumpTreeJson() only indents '}'
for (; nodep; nodep = nodep->nextp()) {
nodep->dumpTreeJson(os, indent + " ");
if (nodep->nextp()) os << ",";
}
os << "]";
}
os << "]";
}

static void dumpFileInfo(std::ostream& os, const FileLine* fileinfop) {
Expand All @@ -1460,7 +1465,7 @@ 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());
dumpJsonStr(os, "name", V3OutFormatter::quoteNameControls(name()));
Expand All @@ -1470,11 +1475,11 @@ void AstNode::dumpTreeJson(std::ostream& os) const {
dumpJsonNum(os, "editNum", editCount());
#endif
dumpExtraJson(os);
dumpNodeListJson(os, op1p(), "op1");
dumpNodeListJson(os, op2p(), "op2");
dumpNodeListJson(os, op3p(), "op3");
dumpNodeListJson(os, op4p(), "op4");
os << "}";
dumpNodeListJson(os, op1p(), "op1", indent + " ");
dumpNodeListJson(os, op2p(), "op2", indent + " ");
dumpNodeListJson(os, op3p(), "op3", indent + " ");
dumpNodeListJson(os, op4p(), "op4", indent + " ");
os << "\n" << indent << "}";
}

void AstNodeProcedure::dump(std::ostream& str) const {
Expand Down

0 comments on commit b55edf4

Please sign in to comment.