From bdf88d10a7a7c3dc4457b03dcbca3c5259479187 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 1 Sep 2013 14:28:22 -0400 Subject: [PATCH] Tree::dump - The dump method now composes a string, instead of writing to std::cout, which make integration easier. --- src/Context.cpp | 2 +- src/parser/A3t.h | 2 +- src/parser/Tree.cpp | 23 ++++++++++++----------- src/parser/Tree.h | 5 +++-- src/parser/args.cpp | 4 ++-- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/Context.cpp b/src/Context.cpp index 75f7aabac..7f3d3629b 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -181,7 +181,7 @@ int Context::initialize (int argc, const char** argv) // Initialize the command line parser. if (parseTree && config.getBoolean ("debug")) - parseTree->dump (); + debug (parseTree->dump ()); // END EXPERIMENTAL CODE diff --git a/src/parser/A3t.h b/src/parser/A3t.h index ec160f313..2a04bb794 100644 --- a/src/parser/A3t.h +++ b/src/parser/A3t.h @@ -65,7 +65,7 @@ private: // TODO Prepare infix // TODO Expand operators // TODO Expand sequence - // TODO Convert to postfix + // TODO Convert to postfix - not necessary given parse tree? private: Tree* _tree; diff --git a/src/parser/Tree.cpp b/src/parser/Tree.cpp index f0897d88f..d5975a8bc 100644 --- a/src/parser/Tree.cpp +++ b/src/parser/Tree.cpp @@ -248,14 +248,13 @@ Tree* Tree::find (const std::string& path) } //////////////////////////////////////////////////////////////////////////////// -void Tree::dumpNode (Tree* t, int depth) +void Tree::dumpNode (Tree* t, int depth, std::stringstream& output) { // Dump node for (int i = 0; i < depth; ++i) - std::cout << " "; + output << " "; -// std::cout << t << " \033[1m" << t->_name << "\033[0m"; - std::cout << "\033[1m" << t->_name << "\033[0m"; + output << "\033[1m" << t->_name << "\033[0m"; // Dump attributes. std::string atts; @@ -270,7 +269,7 @@ void Tree::dumpNode (Tree* t, int depth) } if (atts.length ()) - std::cout << " " << atts; + output << " " << atts; // Dump tags. std::string tags; @@ -279,21 +278,23 @@ void Tree::dumpNode (Tree* t, int depth) tags += (tags.length () ? " " : "") + *tag; if (tags.length ()) - std::cout << " \033[32m" << tags << "\033[0m"; + output << " \033[32m" << tags << "\033[0m"; - std::cout << "\n"; + output << "\n"; // Recurse for branches. std::vector ::iterator b; for (b = t->_branches.begin (); b != t->_branches.end (); ++b) - dumpNode (*b, depth + 1); + dumpNode (*b, depth + 1, output); } //////////////////////////////////////////////////////////////////////////////// -void Tree::dump () +std::string Tree::dump () { - std::cout << "Tree (" << count () << " nodes)\n"; - dumpNode (this, 1); + std::stringstream output; + output << "Tree (" << count () << " nodes)\n"; + dumpNode (this, 1, output); + return output.str (); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/parser/Tree.h b/src/parser/Tree.h index 19c36c884..de3d72610 100644 --- a/src/parser/Tree.h +++ b/src/parser/Tree.h @@ -30,6 +30,7 @@ #include #include #include +#include class Tree; @@ -61,10 +62,10 @@ public: Tree* find (const std::string&); - void dump (); + std::string dump (); private: - void dumpNode (Tree*, int); + void dumpNode (Tree*, int, std::stringstream&); public: Tree* _trunk; // Parent. diff --git a/src/parser/args.cpp b/src/parser/args.cpp index c20bc1450..1da3ab898 100644 --- a/src/parser/args.cpp +++ b/src/parser/args.cpp @@ -32,7 +32,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// -int main (int argc, char** argv) +int main (int argc, const char** argv) { try { @@ -139,7 +139,7 @@ int main (int argc, char** argv) Tree* tree = a3t.parse (); if (tree) - tree->dump (); + std::cout << tree->dump (); } catch (const std::string& error)