From fa4b498fc27f467b4310a612a63eee757d78dfcc Mon Sep 17 00:00:00 2001 From: jerry_liu Date: Thu, 7 Mar 2019 11:31:08 +0800 Subject: [PATCH 1/2] Add pretty print AST --- contributors.txt | 3 ++- runtime/Cpp/runtime/src/RuleContext.cpp | 12 +++++----- runtime/Cpp/runtime/src/RuleContext.h | 6 ++--- runtime/Cpp/runtime/src/tree/ParseTree.h | 4 ++-- .../Cpp/runtime/src/tree/TerminalNodeImpl.cpp | 4 ++-- .../Cpp/runtime/src/tree/TerminalNodeImpl.h | 4 ++-- runtime/Cpp/runtime/src/tree/Trees.cpp | 23 ++++++++++++++----- runtime/Cpp/runtime/src/tree/Trees.h | 6 ++--- 8 files changed, 37 insertions(+), 25 deletions(-) diff --git a/contributors.txt b/contributors.txt index 9751a6e90..6346f1566 100644 --- a/contributors.txt +++ b/contributors.txt @@ -230,4 +230,5 @@ YYYY/MM/DD, github id, Full name, email 2019/09/10, amorimjuliana, Juliana Amorim, juu.amorim@gmail.com 2019/09/17, kaz, Kazuki Sawada, kazuki@6715.jp 2019/09/28, lmy269, Mingyang Liu, lmy040758@gmail.com -2019/10/31, a-square, Alexei Averchenko, lex.aver@gmail.com \ No newline at end of file +2019/10/31, a-square, Alexei Averchenko, lex.aver@gmail.com +2019/03/07, foxeverl, Liu Xinfeng, liuxf1986[at]gmail[dot]com diff --git a/runtime/Cpp/runtime/src/RuleContext.cpp b/runtime/Cpp/runtime/src/RuleContext.cpp index 73cfe24e1..467e5ec42 100755 --- a/runtime/Cpp/runtime/src/RuleContext.cpp +++ b/runtime/Cpp/runtime/src/RuleContext.cpp @@ -75,16 +75,16 @@ antlrcpp::Any RuleContext::accept(tree::ParseTreeVisitor *visitor) { return visitor->visitChildren(this); } -std::string RuleContext::toStringTree(Parser *recog) { - return tree::Trees::toStringTree(this, recog); +std::string RuleContext::toStringTree(Parser *recog, bool pretty) { + return tree::Trees::toStringTree(this, recog, pretty); } -std::string RuleContext::toStringTree(std::vector &ruleNames) { - return tree::Trees::toStringTree(this, ruleNames); +std::string RuleContext::toStringTree(std::vector &ruleNames, bool pretty) { + return tree::Trees::toStringTree(this, ruleNames, pretty); } -std::string RuleContext::toStringTree() { - return toStringTree(nullptr); +std::string RuleContext::toStringTree(bool pretty) { + return toStringTree(nullptr, pretty); } diff --git a/runtime/Cpp/runtime/src/RuleContext.h b/runtime/Cpp/runtime/src/RuleContext.h index bf0ff6631..9ee0d2def 100755 --- a/runtime/Cpp/runtime/src/RuleContext.h +++ b/runtime/Cpp/runtime/src/RuleContext.h @@ -110,15 +110,15 @@ namespace antlr4 { /// (root child1 .. childN). Print just a node if this is a leaf. /// We have to know the recognizer so we can get rule names. /// - virtual std::string toStringTree(Parser *recog) override; + virtual std::string toStringTree(Parser *recog, bool pretty = false) override; /// /// Print out a whole tree, not just a node, in LISP format /// (root child1 .. childN). Print just a node if this is a leaf. /// - virtual std::string toStringTree(std::vector &ruleNames); + virtual std::string toStringTree(std::vector &ruleNames, bool pretty = false); - virtual std::string toStringTree() override; + virtual std::string toStringTree(bool pretty = false) override; virtual std::string toString() override; std::string toString(Recognizer *recog); std::string toString(const std::vector &ruleNames); diff --git a/runtime/Cpp/runtime/src/tree/ParseTree.h b/runtime/Cpp/runtime/src/tree/ParseTree.h index ee50b8039..088aac3ef 100755 --- a/runtime/Cpp/runtime/src/tree/ParseTree.h +++ b/runtime/Cpp/runtime/src/tree/ParseTree.h @@ -39,12 +39,12 @@ namespace tree { /// Print out a whole tree, not just a node, in LISP format /// {@code (root child1 .. childN)}. Print just a node if this is a leaf. - virtual std::string toStringTree() = 0; + virtual std::string toStringTree(bool pretty = false) = 0; virtual std::string toString() = 0; /// Specialize toStringTree so that it can print out more information /// based upon the parser. - virtual std::string toStringTree(Parser *parser) = 0; + virtual std::string toStringTree(Parser *parser, bool pretty = false) = 0; virtual bool operator == (const ParseTree &other) const; diff --git a/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp b/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp index 0f806d4e7..7ab121b73 100755 --- a/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp +++ b/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp @@ -41,7 +41,7 @@ std::string TerminalNodeImpl::getText() { return symbol->getText(); } -std::string TerminalNodeImpl::toStringTree(Parser * /*parser*/) { +std::string TerminalNodeImpl::toStringTree(Parser * /*parser*/, bool /*pretty*/) { return toString(); } @@ -52,6 +52,6 @@ std::string TerminalNodeImpl::toString() { return symbol->getText(); } -std::string TerminalNodeImpl::toStringTree() { +std::string TerminalNodeImpl::toStringTree(bool /*pretty*/) { return toString(); } diff --git a/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.h b/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.h index d865c58e8..6f65d8204 100755 --- a/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.h +++ b/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.h @@ -23,9 +23,9 @@ namespace tree { virtual antlrcpp::Any accept(ParseTreeVisitor *visitor) override; virtual std::string getText() override; - virtual std::string toStringTree(Parser *parser) override; + virtual std::string toStringTree(Parser *parser, bool pretty = false) override; virtual std::string toString() override; - virtual std::string toStringTree() override; + virtual std::string toStringTree(bool pretty = false) override; }; diff --git a/runtime/Cpp/runtime/src/tree/Trees.cpp b/runtime/Cpp/runtime/src/tree/Trees.cpp index fabad0141..72b9b8b48 100755 --- a/runtime/Cpp/runtime/src/tree/Trees.cpp +++ b/runtime/Cpp/runtime/src/tree/Trees.cpp @@ -25,17 +25,17 @@ using namespace antlrcpp; Trees::Trees() { } -std::string Trees::toStringTree(ParseTree *t) { - return toStringTree(t, nullptr); +std::string Trees::toStringTree(ParseTree *t, bool pretty) { + return toStringTree(t, nullptr, pretty); } -std::string Trees::toStringTree(ParseTree *t, Parser *recog) { +std::string Trees::toStringTree(ParseTree *t, Parser *recog, bool pretty) { if (recog == nullptr) - return toStringTree(t, std::vector()); - return toStringTree(t, recog->getRuleNames()); + return toStringTree(t, std::vector(), pretty); + return toStringTree(t, recog->getRuleNames(), pretty); } -std::string Trees::toStringTree(ParseTree *t, const std::vector &ruleNames) { +std::string Trees::toStringTree(ParseTree *t, const std::vector &ruleNames, bool pretty) { std::string temp = antlrcpp::escapeWhitespace(Trees::getNodeText(t, ruleNames), false); if (t->children.empty()) { return temp; @@ -48,6 +48,7 @@ std::string Trees::toStringTree(ParseTree *t, const std::vector &ru std::stack stack; size_t childIndex = 0; ParseTree *run = t; + size_t indentationLevel = 1; while (childIndex < run->children.size()) { if (childIndex > 0) { ss << ' '; @@ -59,6 +60,13 @@ std::string Trees::toStringTree(ParseTree *t, const std::vector &ru stack.push(childIndex); run = child; childIndex = 0; + if (pretty) { + ++indentationLevel; + ss << std::endl; + for (size_t i = 0; i < indentationLevel; ++i) { + ss << " "; + } + } ss << "(" << temp << " "; } else { ss << temp; @@ -68,6 +76,9 @@ std::string Trees::toStringTree(ParseTree *t, const std::vector &ru childIndex = stack.top(); stack.pop(); run = run->parent; + if (pretty) { + --indentationLevel; + } ss << ")"; } else { break; diff --git a/runtime/Cpp/runtime/src/tree/Trees.h b/runtime/Cpp/runtime/src/tree/Trees.h index e6a1bb88e..d9d04624f 100755 --- a/runtime/Cpp/runtime/src/tree/Trees.h +++ b/runtime/Cpp/runtime/src/tree/Trees.h @@ -18,17 +18,17 @@ namespace tree { /// Print out a whole tree in LISP form. getNodeText is used on the /// node payloads to get the text for the nodes. Detect /// parse trees and extract data appropriately. - static std::string toStringTree(ParseTree *t); + static std::string toStringTree(ParseTree *t, bool pretty = false); /// Print out a whole tree in LISP form. getNodeText is used on the /// node payloads to get the text for the nodes. Detect /// parse trees and extract data appropriately. - static std::string toStringTree(ParseTree *t, Parser *recog); + static std::string toStringTree(ParseTree *t, Parser *recog, bool pretty = false); /// Print out a whole tree in LISP form. getNodeText is used on the /// node payloads to get the text for the nodes. Detect /// parse trees and extract data appropriately. - static std::string toStringTree(ParseTree *t, const std::vector &ruleNames); + static std::string toStringTree(ParseTree *t, const std::vector &ruleNames, bool pretty = false); static std::string getNodeText(ParseTree *t, Parser *recog); static std::string getNodeText(ParseTree *t, const std::vector &ruleNames); From cf0658a6ab377307ee2a304c6c0352c83fb4c64c Mon Sep 17 00:00:00 2001 From: liuxinfeng Date: Mon, 11 Nov 2019 21:39:04 +0800 Subject: [PATCH 2/2] update date --- contributors.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contributors.txt b/contributors.txt index 6346f1566..b9a7b6a2e 100644 --- a/contributors.txt +++ b/contributors.txt @@ -231,4 +231,4 @@ YYYY/MM/DD, github id, Full name, email 2019/09/17, kaz, Kazuki Sawada, kazuki@6715.jp 2019/09/28, lmy269, Mingyang Liu, lmy040758@gmail.com 2019/10/31, a-square, Alexei Averchenko, lex.aver@gmail.com -2019/03/07, foxeverl, Liu Xinfeng, liuxf1986[at]gmail[dot]com +2019/11/11, foxeverl, Liu Xinfeng, liuxf1986[at]gmail[dot]com