From b84736a05f9abfae377b0966933fa1b4cd493f7e Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 17 Aug 2014 11:37:08 -0400 Subject: [PATCH] Parser - Added ::collect method for recursive node gathering that needs some local persistence. --- src/Parser.cpp | 35 +++++++++++++++++++++++++++++++++++ src/Parser.h | 2 ++ 2 files changed, 37 insertions(+) diff --git a/src/Parser.cpp b/src/Parser.cpp index c2f27d1e8..c5f52477d 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -270,6 +270,41 @@ 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, bool all, Tree* tree /* = NULL */) +{ + if (tree == NULL) + tree = _tree; + + std::vector ::iterator i; + for (i = tree->_branches.begin (); i != tree->_branches.end (); ++i) + { + if ((*i)->_branches.size ()) + { + collect (nodes, all, *i); + } + else + { + if (! all) + { + // 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, skipping terminated nodes, and known // nodes, and cal the callback function for each node. diff --git a/src/Parser.h b/src/Parser.h index 3bbc8bdac..e8a9349be 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; + void collect (std::vector &, bool, Tree* tree = NULL); + void findBinary (); void resolveAliases (); void findOverrides ();