From a3d2750f40dad3966b1cc9e111b25543bdda75ff Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 17 Aug 2014 13:41:17 -0400 Subject: [PATCH] Parser - Converted all remaining methods to use collect. Now there is full recursion everywhere, which means the parse tree can be arbitrarily deep. --- src/Parser.cpp | 48 +++++++++++++++++++++++++++++------------------- src/Parser.h | 2 +- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/Parser.cpp b/src/Parser.cpp index fd1b062eb..da669b1db 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -193,7 +193,6 @@ Tree* Parser::parse () findStrayModifications (); findPlainArgs (); - // GOOD ^^^ findMissingOperators (); validate (); @@ -276,7 +275,7 @@ bool Parser::canonicalize ( // 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 */) +void Parser::collect (std::vector & nodes, bool all, Tree* tree /* = NULL */) const { if (tree == NULL) tree = _tree; @@ -505,8 +504,10 @@ void Parser::getOverrides ( std::string& home, File& rc) { + std::vector nodes; + collect (nodes, false); std::vector ::iterator i; - for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i) + for (i = nodes.begin (); i != nodes.end (); ++i) { if ((*i)->hasTag ("RC")) { @@ -535,8 +536,10 @@ void Parser::getDataLocation (Path& data) if (location != "") data = location; + std::vector nodes; + collect (nodes, false); std::vector ::iterator i; - for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i) + for (i = nodes.begin (); i != nodes.end (); ++i) { if ((*i)->hasTag ("CONFIG") && (*i)->attribute ("name") == "data.location") @@ -580,8 +583,11 @@ void Parser::injectDefaults () bool found_command = false; bool found_sequence = false; bool found_other = false; + + std::vector nodes; + collect (nodes, true); std::vector ::iterator i; - for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i) + for (i = nodes.begin (); i != nodes.end (); ++i) { if ((*i)->hasTag ("BINARY")) ; @@ -625,8 +631,10 @@ void Parser::injectDefaults () } std::string combined; + std::vector nodes; + collect (nodes, false); std::vector ::iterator i; - for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i) + for (i = nodes.begin (); i != nodes.end (); ++i) { if (combined.length ()) combined += ' '; @@ -701,8 +709,10 @@ const std::string Parser::getFilterExpression () { // Construct an efficient ID/UUID clause. std::string sequence = ""; + std::vector nodes; + collect (nodes, true); std::vector ::iterator i; - for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i) + for (i = nodes.begin (); i != nodes.end (); ++i) { if ((*i)->hasTag ("FILTER") && ! (*i)->hasTag ("PSEUDO")) { @@ -746,8 +756,10 @@ const std::string Parser::getFilterExpression () const std::vector Parser::getWords () const { std::vector words; - std::vector ::const_iterator i; - for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i) + std::vector nodes; + collect (nodes, true); + std::vector ::iterator i; + for (i = nodes.begin (); i != nodes.end (); ++i) { if (! (*i)->hasTag ("BINARY") && ! (*i)->hasTag ("RC") && @@ -764,8 +776,10 @@ const std::vector Parser::getWords () const //////////////////////////////////////////////////////////////////////////////// std::string Parser::getLimit () const { - std::vector ::const_iterator i; - for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i) + std::vector nodes; + collect (nodes, false); + std::vector ::iterator i; + for (i = nodes.begin (); i != nodes.end (); ++i) { // Parser override operator. if ((*i)->attribute ("raw") == "--") @@ -784,16 +798,12 @@ std::string Parser::getLimit () const //////////////////////////////////////////////////////////////////////////////// std::string Parser::getCommand () const { - std::vector ::const_iterator i; - for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i) - { - // Parser override operator. - if ((*i)->attribute ("raw") == "--") - break; - + std::vector nodes; + collect (nodes, false); + std::vector ::iterator i; + for (i = nodes.begin (); i != nodes.end (); ++i) if ((*i)->hasTag ("CMD")) return (*i)->attribute ("canonical"); - } return ""; } diff --git a/src/Parser.h b/src/Parser.h index 8076ca849..cbef7153c 100644 --- a/src/Parser.h +++ b/src/Parser.h @@ -47,7 +47,7 @@ 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 collect (std::vector &, bool, Tree* tree = NULL) const; void findBinary (); void resolveAliases ();