Skip to content

Commit

Permalink
[FIXME] implement non-const iteration on Nodes
Browse files Browse the repository at this point in the history
We provide an API supporting range-based for loops and legacy iteration over an
AST or a subtree rooted at a particular node. The purpose of this is to abstract
patterns like indexing and validation into a common implementation. This needs a
const counterpart, but then it should be ready to near-entirely replace the
functionality of indexing, validation, and friends. This should then be a
suitable base to build Node::all, Node::any, Node::none, etc on top of.

FIXME: Have not had time to review this code or do anything more than ensure it
compiles.

Github: related to #152 "traversal abort"
  • Loading branch information
Smattr committed Nov 17, 2019
1 parent dcaee42 commit a539d6c
Show file tree
Hide file tree
Showing 2 changed files with 428 additions and 0 deletions.
30 changes: 30 additions & 0 deletions librumur/include/rumur/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstddef>
#include <iostream>
#include "location.hh"
#include <vector>

namespace rumur {

Expand All @@ -28,6 +29,35 @@ struct Node {
*/
virtual void validate() const { }

// Iteration-supporting infrastructure. You do not need to pay much attention
// to the following details, but their purpose is to allow you to use C++11
// range-based for loops on AST nodes:
//
// for (Node *n : myAST) {
// ...
// }
//
// The traversal order is not guaranteed, so do not use this API if you
// specifically require pre-order or post-order.

class Iterator {

private:
std::vector<Node*> remaining;

public:
explicit Iterator();
explicit Iterator(Node &base);
Iterator &operator++();
Iterator operator++(int);
Node *operator*();
bool operator==(const Iterator &other) const;
bool operator!=(const Iterator &other) const;
};

Iterator begin();
Iterator end();

};

}
Loading

0 comments on commit a539d6c

Please sign in to comment.