diff --git a/src/Parser.cpp b/src/Parser.cpp index 3420b742c..ce5a43bae 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -25,6 +25,7 @@ //////////////////////////////////////////////////////////////////////////////// #include +#include // TODO Remove. #include #include #include @@ -296,6 +297,47 @@ bool Parser::canonicalize ( return false; } +//////////////////////////////////////////////////////////////////////////////// +// Recursively scan all nodes, depth first, and create a linear list of node +// pointers, for simple iteration. This eliminates the need for recursion in +// each ::find* method. +void Parser::collect ( + std::vector & nodes, + collectType type /* = collectType::collectLeaf */, + Tree* tree /* = NULL */) const +{ + if (tree == NULL) + tree = _tree; + + std::vector ::iterator i; + for (i = tree->_branches.begin (); i != tree->_branches.end (); ++i) + { + if ((*i)->_branches.size ()) + { + if (type == collectAll) + nodes.push_back (*i); + + collect (nodes, type, *i); + } + else + { + if (type == collectLeaf) + { + // Parser override operator. + if ((*i)->hasTag ("TERMINATOR") || + (*i)->hasTag ("TERMINATED")) + break; + + // Skip known args. + if (! (*i)->hasTag ("?")) + continue; + } + + nodes.push_back (*i); + } + } +} + //////////////////////////////////////////////////////////////////////////////// // Recursively scan all nodes, depth first, and create a linear list of node // pointers, for simple iteration. This eliminates the need for recursion in diff --git a/src/Parser.h b/src/Parser.h index cbef7153c..0ccaa5df0 100644 --- a/src/Parser.h +++ b/src/Parser.h @@ -47,6 +47,8 @@ public: bool exactMatch (const std::string&, const std::string&) const; bool canonicalize (std::string&, const std::string&, const std::string&) const; + enum collectType {collectLeaf, collectAll, collectUnterminated}; + void collect (std::vector &, collectType = collectLeaf, Tree* tree = NULL) const; void collect (std::vector &, bool, Tree* tree = NULL) const; void findBinary ();