Merge branch 'master' into patch-1

This commit is contained in:
Ben List 2019-11-13 09:04:29 -05:00 committed by GitHub
commit a06009d72e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 36 additions and 24 deletions

View File

@ -232,3 +232,4 @@ YYYY/MM/DD, github id, Full name, email
2019/09/28, lmy269, Mingyang Liu, lmy040758@gmail.com 2019/09/28, lmy269, Mingyang Liu, lmy040758@gmail.com
2019/10/31, a-square, Alexei Averchenko, lex.aver@gmail.com 2019/10/31, a-square, Alexei Averchenko, lex.aver@gmail.com
2019/11/05, listba, Ben List, ben.list89@gmail.com 2019/11/05, listba, Ben List, ben.list89@gmail.com
2019/11/11, foxeverl, Liu Xinfeng, liuxf1986[at]gmail[dot]com

View File

@ -75,16 +75,16 @@ antlrcpp::Any RuleContext::accept(tree::ParseTreeVisitor *visitor) {
return visitor->visitChildren(this); return visitor->visitChildren(this);
} }
std::string RuleContext::toStringTree(Parser *recog) { std::string RuleContext::toStringTree(Parser *recog, bool pretty) {
return tree::Trees::toStringTree(this, recog); return tree::Trees::toStringTree(this, recog, pretty);
} }
std::string RuleContext::toStringTree(std::vector<std::string> &ruleNames) { std::string RuleContext::toStringTree(std::vector<std::string> &ruleNames, bool pretty) {
return tree::Trees::toStringTree(this, ruleNames); return tree::Trees::toStringTree(this, ruleNames, pretty);
} }
std::string RuleContext::toStringTree() { std::string RuleContext::toStringTree(bool pretty) {
return toStringTree(nullptr); return toStringTree(nullptr, pretty);
} }

View File

@ -110,15 +110,15 @@ namespace antlr4 {
/// (root child1 .. childN). Print just a node if this is a leaf. /// (root child1 .. childN). Print just a node if this is a leaf.
/// We have to know the recognizer so we can get rule names. /// We have to know the recognizer so we can get rule names.
/// </summary> /// </summary>
virtual std::string toStringTree(Parser *recog) override; virtual std::string toStringTree(Parser *recog, bool pretty = false) override;
/// <summary> /// <summary>
/// Print out a whole tree, not just a node, in LISP format /// Print out a whole tree, not just a node, in LISP format
/// (root child1 .. childN). Print just a node if this is a leaf. /// (root child1 .. childN). Print just a node if this is a leaf.
/// </summary> /// </summary>
virtual std::string toStringTree(std::vector<std::string> &ruleNames); virtual std::string toStringTree(std::vector<std::string> &ruleNames, bool pretty = false);
virtual std::string toStringTree() override; virtual std::string toStringTree(bool pretty = false) override;
virtual std::string toString() override; virtual std::string toString() override;
std::string toString(Recognizer *recog); std::string toString(Recognizer *recog);
std::string toString(const std::vector<std::string> &ruleNames); std::string toString(const std::vector<std::string> &ruleNames);

View File

@ -39,12 +39,12 @@ namespace tree {
/// Print out a whole tree, not just a node, in LISP format /// 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. /// {@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; virtual std::string toString() = 0;
/// Specialize toStringTree so that it can print out more information /// Specialize toStringTree so that it can print out more information
/// based upon the parser. /// 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; virtual bool operator == (const ParseTree &other) const;

View File

@ -41,7 +41,7 @@ std::string TerminalNodeImpl::getText() {
return symbol->getText(); return symbol->getText();
} }
std::string TerminalNodeImpl::toStringTree(Parser * /*parser*/) { std::string TerminalNodeImpl::toStringTree(Parser * /*parser*/, bool /*pretty*/) {
return toString(); return toString();
} }
@ -52,6 +52,6 @@ std::string TerminalNodeImpl::toString() {
return symbol->getText(); return symbol->getText();
} }
std::string TerminalNodeImpl::toStringTree() { std::string TerminalNodeImpl::toStringTree(bool /*pretty*/) {
return toString(); return toString();
} }

View File

@ -23,9 +23,9 @@ namespace tree {
virtual antlrcpp::Any accept(ParseTreeVisitor *visitor) override; virtual antlrcpp::Any accept(ParseTreeVisitor *visitor) override;
virtual std::string getText() 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 toString() override;
virtual std::string toStringTree() override; virtual std::string toStringTree(bool pretty = false) override;
}; };

View File

@ -25,17 +25,17 @@ using namespace antlrcpp;
Trees::Trees() { Trees::Trees() {
} }
std::string Trees::toStringTree(ParseTree *t) { std::string Trees::toStringTree(ParseTree *t, bool pretty) {
return toStringTree(t, nullptr); 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) if (recog == nullptr)
return toStringTree(t, std::vector<std::string>()); return toStringTree(t, std::vector<std::string>(), pretty);
return toStringTree(t, recog->getRuleNames()); return toStringTree(t, recog->getRuleNames(), pretty);
} }
std::string Trees::toStringTree(ParseTree *t, const std::vector<std::string> &ruleNames) { std::string Trees::toStringTree(ParseTree *t, const std::vector<std::string> &ruleNames, bool pretty) {
std::string temp = antlrcpp::escapeWhitespace(Trees::getNodeText(t, ruleNames), false); std::string temp = antlrcpp::escapeWhitespace(Trees::getNodeText(t, ruleNames), false);
if (t->children.empty()) { if (t->children.empty()) {
return temp; return temp;
@ -48,6 +48,7 @@ std::string Trees::toStringTree(ParseTree *t, const std::vector<std::string> &ru
std::stack<size_t> stack; std::stack<size_t> stack;
size_t childIndex = 0; size_t childIndex = 0;
ParseTree *run = t; ParseTree *run = t;
size_t indentationLevel = 1;
while (childIndex < run->children.size()) { while (childIndex < run->children.size()) {
if (childIndex > 0) { if (childIndex > 0) {
ss << ' '; ss << ' ';
@ -59,6 +60,13 @@ std::string Trees::toStringTree(ParseTree *t, const std::vector<std::string> &ru
stack.push(childIndex); stack.push(childIndex);
run = child; run = child;
childIndex = 0; childIndex = 0;
if (pretty) {
++indentationLevel;
ss << std::endl;
for (size_t i = 0; i < indentationLevel; ++i) {
ss << " ";
}
}
ss << "(" << temp << " "; ss << "(" << temp << " ";
} else { } else {
ss << temp; ss << temp;
@ -68,6 +76,9 @@ std::string Trees::toStringTree(ParseTree *t, const std::vector<std::string> &ru
childIndex = stack.top(); childIndex = stack.top();
stack.pop(); stack.pop();
run = run->parent; run = run->parent;
if (pretty) {
--indentationLevel;
}
ss << ")"; ss << ")";
} else { } else {
break; break;

View File

@ -18,17 +18,17 @@ namespace tree {
/// Print out a whole tree in LISP form. getNodeText is used on the /// Print out a whole tree in LISP form. getNodeText is used on the
/// node payloads to get the text for the nodes. Detect /// node payloads to get the text for the nodes. Detect
/// parse trees and extract data appropriately. /// 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 /// Print out a whole tree in LISP form. getNodeText is used on the
/// node payloads to get the text for the nodes. Detect /// node payloads to get the text for the nodes. Detect
/// parse trees and extract data appropriately. /// 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 /// Print out a whole tree in LISP form. getNodeText is used on the
/// node payloads to get the text for the nodes. Detect /// node payloads to get the text for the nodes. Detect
/// parse trees and extract data appropriately. /// parse trees and extract data appropriately.
static std::string toStringTree(ParseTree *t, const std::vector<std::string> &ruleNames); static std::string toStringTree(ParseTree *t, const std::vector<std::string> &ruleNames, bool pretty = false);
static std::string getNodeText(ParseTree *t, Parser *recog); static std::string getNodeText(ParseTree *t, Parser *recog);
static std::string getNodeText(ParseTree *t, const std::vector<std::string> &ruleNames); static std::string getNodeText(ParseTree *t, const std::vector<std::string> &ruleNames);