From acf5723bdd750a3d56c63224cca39c4f96b12140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 24 Feb 2014 17:22:29 +0100 Subject: [PATCH] AST: improved AST printout when --verbose is used --- lib/token.cpp | 34 ++++++++++++++++++++++++++++++++-- lib/token.h | 4 +++- lib/tokenize.cpp | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index 4881ef9b5..39bd518c7 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1201,7 +1201,7 @@ std::string Token::expressionString() const } -void Token::printAst() const +void Token::printAst(bool verbose) const { bool title = false; @@ -1211,7 +1211,10 @@ void Token::printAst() const if (!title) std::cout << "\n\n##AST" << std::endl; title = true; - std::cout << tok->astTop()->astString(" ") << std::endl; + if (verbose) + std::cout << tok->astTop()->astStringVerbose(0,0) << std::endl; + else + std::cout << tok->astTop()->astString(" ") << std::endl; print = false; if (tok->str() == "(") tok = tok->link(); @@ -1221,6 +1224,33 @@ void Token::printAst() const } } +static std::string indent(const unsigned int indent1, const unsigned int indent2) +{ + std::string ret(indent1,' '); + for (unsigned int i = indent1; i < indent2; i += 2) + ret += "| "; + return ret; +} + +std::string Token::astStringVerbose(const unsigned int indent1, const unsigned int indent2) const +{ + std::string ret = _str + "\n"; + if (_astOperand1) { + unsigned int i1 = indent1, i2 = indent2 + 2; + if (indent1==indent2 && !_astOperand2) + i1 += 2; + ret += indent(indent1,indent2) + (_astOperand2 ? "|-" : "`-") + _astOperand1->astStringVerbose(i1,i2); + } + if (_astOperand2) { + unsigned int i1 = indent1, i2 = indent2 + 2; + if (indent1==indent2) + i1 += 2; + ret += indent(indent1,indent2) + "`-" + _astOperand2->astStringVerbose(i1,i2); + } + return ret; +} + + void Token::printValueFlow() const { int line = -1; diff --git a/lib/token.h b/lib/token.h index f3eee4df0..80b0a6c24 100644 --- a/lib/token.h +++ b/lib/token.h @@ -721,9 +721,11 @@ public: return ret + sep + _str; } + std::string astStringVerbose(const unsigned int indent1, const unsigned int indent2) const; + std::string expressionString() const; - void printAst() const; + void printAst(bool verbose) const; void printValueFlow() const; }; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 80f4414a3..8b80f46a6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3652,7 +3652,7 @@ bool Tokenizer::simplifyTokenList2() if (_settings->_verbose) _symbolDatabase->printOut("Symbol database"); - list.front()->printAst(); + list.front()->printAst(_settings->_verbose); list.front()->printValueFlow(); }