Tree::dump

- The dump method now composes a string, instead of writing to std::cout, which
  make integration easier.
This commit is contained in:
Paul Beckingham 2013-09-01 14:28:22 -04:00
parent 314d0e9d01
commit bdf88d10a7
5 changed files with 19 additions and 17 deletions

View file

@ -181,7 +181,7 @@ int Context::initialize (int argc, const char** argv)
// Initialize the command line parser. // Initialize the command line parser.
if (parseTree && config.getBoolean ("debug")) if (parseTree && config.getBoolean ("debug"))
parseTree->dump (); debug (parseTree->dump ());
// END EXPERIMENTAL CODE // END EXPERIMENTAL CODE

View file

@ -65,7 +65,7 @@ private:
// TODO Prepare infix // TODO Prepare infix
// TODO Expand operators // TODO Expand operators
// TODO Expand sequence // TODO Expand sequence
// TODO Convert to postfix // TODO Convert to postfix - not necessary given parse tree?
private: private:
Tree* _tree; Tree* _tree;

View file

@ -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 // Dump node
for (int i = 0; i < depth; ++i) for (int i = 0; i < depth; ++i)
std::cout << " "; output << " ";
// std::cout << t << " \033[1m" << t->_name << "\033[0m"; output << "\033[1m" << t->_name << "\033[0m";
std::cout << "\033[1m" << t->_name << "\033[0m";
// Dump attributes. // Dump attributes.
std::string atts; std::string atts;
@ -270,7 +269,7 @@ void Tree::dumpNode (Tree* t, int depth)
} }
if (atts.length ()) if (atts.length ())
std::cout << " " << atts; output << " " << atts;
// Dump tags. // Dump tags.
std::string tags; std::string tags;
@ -279,21 +278,23 @@ void Tree::dumpNode (Tree* t, int depth)
tags += (tags.length () ? " " : "") + *tag; tags += (tags.length () ? " " : "") + *tag;
if (tags.length ()) if (tags.length ())
std::cout << " \033[32m" << tags << "\033[0m"; output << " \033[32m" << tags << "\033[0m";
std::cout << "\n"; output << "\n";
// Recurse for branches. // Recurse for branches.
std::vector <Tree*>::iterator b; std::vector <Tree*>::iterator b;
for (b = t->_branches.begin (); b != t->_branches.end (); ++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"; std::stringstream output;
dumpNode (this, 1); output << "Tree (" << count () << " nodes)\n";
dumpNode (this, 1, output);
return output.str ();
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -30,6 +30,7 @@
#include <map> #include <map>
#include <vector> #include <vector>
#include <string> #include <string>
#include <sstream>
class Tree; class Tree;
@ -61,10 +62,10 @@ public:
Tree* find (const std::string&); Tree* find (const std::string&);
void dump (); std::string dump ();
private: private:
void dumpNode (Tree*, int); void dumpNode (Tree*, int, std::stringstream&);
public: public:
Tree* _trunk; // Parent. Tree* _trunk; // Parent.

View file

@ -32,7 +32,7 @@
Context context; Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv) int main (int argc, const char** argv)
{ {
try try
{ {
@ -139,7 +139,7 @@ int main (int argc, char** argv)
Tree* tree = a3t.parse (); Tree* tree = a3t.parse ();
if (tree) if (tree)
tree->dump (); std::cout << tree->dump ();
} }
catch (const std::string& error) catch (const std::string& error)