Skip to content

Commit

Permalink
doc: improve documentation on AST traversal (#2905)
Browse files Browse the repository at this point in the history
  • Loading branch information
nharrand authored and monperrus committed Mar 9, 2019
1 parent d4f8c0c commit a5cb7d5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/_data/sidebar_doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ entries:
version: all

items:
- title: Filter
- title: AST Traversing
url: /filter.html
audience: writers, designers
platform: all
Expand Down
41 changes: 41 additions & 0 deletions doc/filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,47 @@ list3 = rootPackage.filterChildren(
).list();
```

Scanners
--------

`CtScanner` provides a simple way to visit a node and its children.

```java
//Scanner counting the number of CtFieldWrite
class CounterScanner extends CtScanner {
private int visited = 0;
@Override
public <T> void visitCtFieldWrite(CtFieldWrite<T> fieldWrite) {
visited++;
}
}

CounterScanner scanner = new CounterScanner();

//Run the scanner on an element, here the CtClass representing FieldAccessRes
launcher.getFactory().Class().get("FieldAccessRes").accept(scanner);

//scanner.visited now contains the number of children of type CtFieldWrite
assertEquals(1, scanner.visited);
```

`EarlyTerminatingScanner` is a specialized Class implementing `CtScanner` that stops once `terminate()` has been called.

See also `CtVisitor`.

Iterator
--------

`CtIterator` provides an iterator on all transitive children of a node in depth first order.
```java
CtIterator iterator = new CtIterator(root);
while (iterator.hasNext()) {
CtElement el = iterator.next();
//do something on each child of root
}
```
`CtBFSIterator` is similar to CtIterator but in Breadth first order.

Queries
-------

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/visitor/CtDequeScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public CtDequeScanner() {
protected void exit(CtElement e) {
CtElement ret = elementsDeque.pop();
if (ret != e) {
throw new RuntimeException("Unconsitant Stack");
throw new RuntimeException("Inconsistent Stack");
}
super.exit(e);
}
Expand Down

0 comments on commit a5cb7d5

Please sign in to comment.