Remove Iterable<ASTNode> interface from ASTNode

Issue #247 resolved
Jesper Öqvist created an issue

JastAdd2 2.1.13-82-g51973c2

Having ASTNode implement Iterable<ASTNode> can cause problems for projects that need to have custom iterators on AST nodes. For example, ExtendJ uses a SimpleSet interface which provides its own iterators. This does not work well with ASTNode implementing Iterable<ASTNode>.

List nodes should still implement Iterable<ASTNode>.

Comments (6)

  1. Jesper Öqvist reporter

    If the ASTNode.iterator() method is required, it can be added via a regular inter-type declaration. The following example introduces the old iterator to ASTNode:

    import java.util.Iterator;
    aspect Iterator {
      ASTNode implements Iterable<T>;
    
      @Override
      public Iterator<T> ASTNode.iterator() {
        return new Iterator<T>() {
          private int index = 0;
    
          @Override
          public boolean hasNext() {
            return index < getNumChild();
          }
    
          @Override
          public T next() {
            return hasNext() ? (T) getChild(index++) : null;
          }
    
          @Override
          public void remove() {
            throw new UnsupportedOperationException();
          }
        };
      }
    }
    
  2. Jesper Öqvist reporter

    ASTNode no longer implements java.lang.Iterable

    ASTNode no longer implements the java.lang.Iterable interface.

    The default list node still implements java.lang.Iterable.

    fixes #247 (bitbucket)

    → <<cset 418a5b4d515a>>

  3. Jesper Mattsson Account Deactivated

    It could be useful to move implementation of List.iterator() to some method on ASTNode (e.g. childIterator()), and let List.iterator() delegate to childIterator(). Then the code needed to reintroduce the old behavior would be reduced to:

    import java.util.Iterator;
    aspect Iterator {
      ASTNode implements Iterable<T>;
    
      @Override
      public Iterator<T> ASTNode.iterator() {
        return childIterator();
      }
    }
    

    It would probably also make implementing the children() method metioned above easier.

  4. Jesper Öqvist reporter

    Add AST child iterator methods on ASTNode

    Added methods for easier iteration of AST children:

    • ASTNode.astChildIterator(), returning an Iterator<T>.
    • ASTNode.astChildren(), returning an Iterable<T>.

    The astChildren() method can be used in an enhanced for statement.

    Refactored List.iterator() to use ASTNode.astChildIterator().

    see #247 (bitbucket)

    → <<cset a44c68da9269>>

  5. Log in to comment