diff --git a/src/parser/A3t.cpp b/src/parser/A3t.cpp index 412ca03a0..bed0daff3 100644 --- a/src/parser/A3t.cpp +++ b/src/parser/A3t.cpp @@ -104,16 +104,15 @@ bool A3t::canonicalize ( // autoCompletes to a valid command/report. void A3t::findBinary () { - if (_tree->branches () >= 1) + if (_tree->_branches.size () >= 1) { - _tree->operator[](0)->tag ("BINARY"); - - std::string binary = _tree->operator[](0)->attribute ("raw"); + _tree->_branches[0]->tag ("BINARY"); + std::string binary = _tree->_branches[0]->attribute ("raw"); std::string::size_type slash = binary.rfind ('/'); if (slash != std::string::npos) binary = binary.substr (slash + 1); - _tree->operator[](0)->attribute ("basename", binary); + _tree->_branches[0]->attribute ("basename", "binary"); } } @@ -122,15 +121,15 @@ void A3t::findBinary () // all args in the raw state. void A3t::findTerminator () { - std::string command; - for (int i = 0; i < _tree->branches (); ++i) + std::vector ::iterator i; + for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i) { - if (_tree->operator[](i)->attribute ("raw") == "--") + if ((*i)->attribute ("raw") == "--") { - _tree->operator[](i)->tag ("TERMINATOR"); + (*i)->tag ("TERMINATOR"); break; } - } + } } //////////////////////////////////////////////////////////////////////////////// @@ -139,38 +138,39 @@ void A3t::findTerminator () void A3t::findCommand () { std::string command; - for (int i = 0; i < _tree->branches (); ++i) + std::vector ::iterator i; + for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i) { // Parser override operator. - if (_tree->operator[](i)->attribute ("raw") == "--") + if ((*i)->attribute ("raw") == "--") break; - if (canonicalize (command, "report", _tree->operator[](i)->attribute ("raw"))) + if (canonicalize (command, "report", (*i)->attribute ("raw"))) { - _tree->operator[](i)->attribute ("canonical", command); - _tree->operator[](i)->tag ("REPORT"); - _tree->operator[](i)->tag ("CMD"); + (*i)->attribute ("canonical", command); + (*i)->tag ("REPORT"); + (*i)->tag ("CMD"); } - else if (canonicalize (command, "readcmd", _tree->operator[](i)->attribute ("raw"))) + else if (canonicalize (command, "readcmd", (*i)->attribute ("raw"))) { - _tree->operator[](i)->attribute ("canonical", command); - _tree->operator[](i)->tag ("READCMD"); - _tree->operator[](i)->tag ("CMD"); + (*i)->attribute ("canonical", command); + (*i)->tag ("READCMD"); + (*i)->tag ("CMD"); } - else if (canonicalize (command, "writecmd", _tree->operator[](i)->attribute ("raw"))) + else if (canonicalize (command, "writecmd", (*i)->attribute ("raw"))) { - _tree->operator[](i)->attribute ("canonical", command); - _tree->operator[](i)->tag ("WRITECMD"); - _tree->operator[](i)->tag ("CMD"); + (*i)->attribute ("canonical", command); + (*i)->tag ("WRITECMD"); + (*i)->tag ("CMD"); } - else if (canonicalize (command, "specialcmd", _tree->operator[](i)->attribute ("raw"))) + else if (canonicalize (command, "specialcmd", (*i)->attribute ("raw"))) { - _tree->operator[](i)->attribute ("canonical", command); - _tree->operator[](i)->tag ("SPECIALCMD"); - _tree->operator[](i)->tag ("CMD"); + (*i)->attribute ("canonical", command); + (*i)->tag ("SPECIALCMD"); + (*i)->tag ("CMD"); } } } diff --git a/src/parser/Tree.cpp b/src/parser/Tree.cpp index c7956c455..3bf799522 100644 --- a/src/parser/Tree.cpp +++ b/src/parser/Tree.cpp @@ -65,16 +65,6 @@ Tree& Tree::operator= (const Tree& other) return *this; } -//////////////////////////////////////////////////////////////////////////////// -Tree* Tree::operator[] (const int branch) -{ - if (branch < 0 || - branch > (int) _branches.size () - 1) - throw "Tree::operator[] out of range"; - - return _branches[branch]; -} - //////////////////////////////////////////////////////////////////////////////// Tree* Tree::addBranch (Tree* branch) { @@ -115,24 +105,6 @@ void Tree::replaceBranch (Tree* from, Tree* to) } } -//////////////////////////////////////////////////////////////////////////////// -int Tree::branches () -{ - return _branches.size (); -} - -//////////////////////////////////////////////////////////////////////////////// -void Tree::name (const std::string& name) -{ - _name = name; -} - -//////////////////////////////////////////////////////////////////////////////// -std::string Tree::name () const -{ - return _name; -} - //////////////////////////////////////////////////////////////////////////////// // Accessor for attributes. void Tree::attribute (const std::string& name, const std::string& value) @@ -173,28 +145,11 @@ void Tree::removeAttribute (const std::string& name) } //////////////////////////////////////////////////////////////////////////////// -int Tree::attributes () const -{ - return _attributes.size (); -} - -//////////////////////////////////////////////////////////////////////////////// -std::vector Tree::allAttributes () const -{ - std::vector names; - std::map ::const_iterator it; - for (it = _attributes.begin (); it != _attributes.end (); ++it) - names.push_back (it->first); - - return names; -} - -//////////////////////////////////////////////////////////////////////////////// -// Recursively completes a list of Tree* objects, left to right, depth first. -// The reason for the depth-first enumeration is that a client may wish to -// traverse the tree and delete nodes. With a depth-first iteration, this is a -// safe mechanism, and a node pointer will never be dereferenced after it has -// been deleted. +// Recursively builds a list of Tree* objects, left to right, depth first. The +// reason for the depth-first enumeration is that a client may wish to traverse +// the tree and delete nodes. With a depth-first iteration, this is a safe +// mechanism, and a node pointer will never be dereferenced after it has been +// deleted. void Tree::enumerate (std::vector & all) const { for (std::vector ::const_iterator i = _branches.begin (); @@ -206,12 +161,6 @@ void Tree::enumerate (std::vector & all) const } } -//////////////////////////////////////////////////////////////////////////////// -Tree* Tree::parent () const -{ - return _trunk; -} - //////////////////////////////////////////////////////////////////////////////// bool Tree::hasTag (const std::string& tag) const { @@ -228,18 +177,6 @@ void Tree::tag (const std::string& tag) _tags.push_back (tag); } -//////////////////////////////////////////////////////////////////////////////// -int Tree::tags () const -{ - return _tags.size (); -} - -//////////////////////////////////////////////////////////////////////////////// -std::vector Tree::allTags () const -{ - return _tags; -} - //////////////////////////////////////////////////////////////////////////////// int Tree::count () const { @@ -265,7 +202,7 @@ Tree* Tree::find (const std::string& path) // Must start at the trunk. Tree* cursor = this; std::vector ::iterator it = elements.begin (); - if (cursor->name () != *it) + if (cursor->_name != *it) return NULL; // Perhaps the trunk is what is needed? @@ -278,11 +215,12 @@ Tree* Tree::find (const std::string& path) bool found = false; // If the cursor has a branch that matches *it, proceed. - for (int i = 0; i < cursor->branches (); ++i) + std::vector ::iterator i; + for (i = cursor->_branches.begin (); i != cursor->_branches.end (); ++i) { - if ((*cursor)[i]->name () == *it) + if ((*i)->_name == *it) { - cursor = (*cursor)[i]; + cursor = *i; found = true; break; } @@ -302,18 +240,18 @@ void Tree::dumpNode (Tree* t, int depth) for (int i = 0; i < depth; ++i) std::cout << " "; - std::cout << t << " \033[1m" << t->name () << "\033[0m"; + std::cout << t << " \033[1m" << t->_name << "\033[0m"; // Dump attributes. std::string atts; - std::vector attributes = t->allAttributes (); - std::vector ::iterator it; - for (it = attributes.begin (); it != attributes.end (); ++it) + + std::map ::iterator a; + for (a = t->_attributes.begin (); a != t->_attributes.end (); ++a) { - if (it != attributes.begin ()) + if (a != t->_attributes.begin ()) atts += " "; - atts += *it + "='\033[33m" + t->attribute (*it) + "\033[0m'"; + atts += a->first + "='\033[33m" + a->second + "\033[0m'"; } if (atts.length ()) @@ -321,9 +259,9 @@ void Tree::dumpNode (Tree* t, int depth) // Dump tags. std::string tags; - std::vector allTags = t->allTags (); - for (it = allTags.begin (); it != allTags.end (); ++it) - tags += (tags.length () ? " " : "") + *it; + std::vector ::iterator tag; + for (tag = t->_tags.begin (); tag != t->_tags.end (); ++tag) + tags += (tags.length () ? " " : "") + *tag; if (tags.length ()) std::cout << " \033[32m" << tags << "\033[0m"; @@ -331,8 +269,9 @@ void Tree::dumpNode (Tree* t, int depth) std::cout << "\n"; // Recurse for branches. - for (int i = 0; i < t->branches (); ++i) - dumpNode ((*t)[i], depth + 1); + std::vector ::iterator b; + for (b = t->_branches.begin (); b != t->_branches.end (); ++b) + dumpNode (*b, depth + 1); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/parser/Tree.h b/src/parser/Tree.h index fae8ebfd1..28951e8cc 100644 --- a/src/parser/Tree.h +++ b/src/parser/Tree.h @@ -40,30 +40,21 @@ public: ~Tree (); Tree (const Tree&); Tree& operator= (const Tree&); - Tree* operator[] (const int); Tree* addBranch (Tree*); void removeBranch (Tree*); void replaceBranch (Tree*, Tree*); - int branches (); - void name (const std::string&); - std::string name () const; void attribute (const std::string&, const std::string&); void attribute (const std::string&, const int); void attribute (const std::string&, const double); std::string attribute (const std::string&); void removeAttribute (const std::string&); - int attributes () const; - std::vector allAttributes () const; + + void enumerate (std::vector & all) const; bool hasTag (const std::string&) const; void tag (const std::string&); - int tags () const; - std::vector allTags () const; - - void enumerate (std::vector & all) const; - Tree* parent () const; int count () const; @@ -74,7 +65,7 @@ public: private: void dumpNode (Tree*, int); -private: +public: Tree* _trunk; // Parent. std::string _name; // Name. std::vector _branches; // Children. diff --git a/src/parser/run b/src/parser/run index 58a1727d3..3525b2bea 100755 --- a/src/parser/run +++ b/src/parser/run @@ -9,5 +9,5 @@ #echo; ./args rc:x rc.debug:1 a.b\