diff options
Diffstat (limited to 'crates/ra_syntax/src/algo/walk.rs')
-rw-r--r-- | crates/ra_syntax/src/algo/walk.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/algo/walk.rs b/crates/ra_syntax/src/algo/walk.rs new file mode 100644 index 000000000..536ee705f --- /dev/null +++ b/crates/ra_syntax/src/algo/walk.rs | |||
@@ -0,0 +1,38 @@ | |||
1 | use { | ||
2 | SyntaxNodeRef, | ||
3 | algo::generate, | ||
4 | }; | ||
5 | |||
6 | pub fn preorder<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator<Item = SyntaxNodeRef<'a>> { | ||
7 | walk(root).filter_map(|event| match event { | ||
8 | WalkEvent::Enter(node) => Some(node), | ||
9 | WalkEvent::Exit(_) => None, | ||
10 | }) | ||
11 | } | ||
12 | |||
13 | #[derive(Debug, Copy, Clone)] | ||
14 | pub enum WalkEvent<'a> { | ||
15 | Enter(SyntaxNodeRef<'a>), | ||
16 | Exit(SyntaxNodeRef<'a>), | ||
17 | } | ||
18 | |||
19 | pub fn walk<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator<Item = WalkEvent<'a>> { | ||
20 | generate(Some(WalkEvent::Enter(root)), move |pos| { | ||
21 | let next = match *pos { | ||
22 | WalkEvent::Enter(node) => match node.first_child() { | ||
23 | Some(child) => WalkEvent::Enter(child), | ||
24 | None => WalkEvent::Exit(node), | ||
25 | }, | ||
26 | WalkEvent::Exit(node) => { | ||
27 | if node == root { | ||
28 | return None; | ||
29 | } | ||
30 | match node.next_sibling() { | ||
31 | Some(sibling) => WalkEvent::Enter(sibling), | ||
32 | None => WalkEvent::Exit(node.parent().unwrap()), | ||
33 | } | ||
34 | } | ||
35 | }; | ||
36 | Some(next) | ||
37 | }) | ||
38 | } | ||