diff options
author | Aleksey Kladov <[email protected]> | 2018-07-30 21:45:10 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-07-30 21:45:10 +0100 |
commit | aea86d154ec5adde6adb05088a50f01380ffb8bf (patch) | |
tree | fd10ec3e5379e24e40f3eff78cb1e035f4bb5c89 /src/yellow | |
parent | 70b337292117a9bb90e85056dcb4069f8bbc6c0a (diff) |
stackless traversal
Diffstat (limited to 'src/yellow')
-rw-r--r-- | src/yellow/red.rs | 3 | ||||
-rw-r--r-- | src/yellow/syntax.rs | 28 |
2 files changed, 30 insertions, 1 deletions
diff --git a/src/yellow/red.rs b/src/yellow/red.rs index bffb72510..8329ec5b2 100644 --- a/src/yellow/red.rs +++ b/src/yellow/red.rs | |||
@@ -84,4 +84,7 @@ impl RedNode { | |||
84 | pub(crate) fn parent(&self) -> Option<ptr::NonNull<RedNode>> { | 84 | pub(crate) fn parent(&self) -> Option<ptr::NonNull<RedNode>> { |
85 | Some(self.parent.as_ref()?.parent) | 85 | Some(self.parent.as_ref()?.parent) |
86 | } | 86 | } |
87 | pub(crate) fn index_in_parent(&self) -> Option<usize> { | ||
88 | Some(self.parent.as_ref()?.index_in_parent) | ||
89 | } | ||
87 | } | 90 | } |
diff --git a/src/yellow/syntax.rs b/src/yellow/syntax.rs index 5c31a3f35..65ce647c7 100644 --- a/src/yellow/syntax.rs +++ b/src/yellow/syntax.rs | |||
@@ -18,6 +18,15 @@ pub struct SyntaxNode<R: TreeRoot = Arc<SyntaxRoot>> { | |||
18 | red: ptr::NonNull<RedNode>, | 18 | red: ptr::NonNull<RedNode>, |
19 | } | 19 | } |
20 | 20 | ||
21 | impl <R1: TreeRoot, R2: TreeRoot> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> { | ||
22 | fn eq(&self, other: &SyntaxNode<R1>) -> bool { | ||
23 | self.red == other.red | ||
24 | } | ||
25 | } | ||
26 | |||
27 | impl <R: TreeRoot> Eq for SyntaxNode<R> { | ||
28 | } | ||
29 | |||
21 | pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; | 30 | pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; |
22 | 31 | ||
23 | #[derive(Debug)] | 32 | #[derive(Debug)] |
@@ -53,7 +62,7 @@ impl SyntaxNode<Arc<SyntaxRoot>> { | |||
53 | } | 62 | } |
54 | 63 | ||
55 | impl<R: TreeRoot> SyntaxNode<R> { | 64 | impl<R: TreeRoot> SyntaxNode<R> { |
56 | pub fn borrow<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { | 65 | pub fn as_ref<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { |
57 | SyntaxNode { | 66 | SyntaxNode { |
58 | root: &*self.root, | 67 | root: &*self.root, |
59 | red: ptr::NonNull::clone(&self.red), | 68 | red: ptr::NonNull::clone(&self.red), |
@@ -92,6 +101,23 @@ impl<R: TreeRoot> SyntaxNode<R> { | |||
92 | }) | 101 | }) |
93 | } | 102 | } |
94 | 103 | ||
104 | pub fn first_child(&self) -> Option<SyntaxNode<R>> { | ||
105 | self.children().next() | ||
106 | } | ||
107 | |||
108 | pub fn next_sibling(&self) -> Option<SyntaxNode<R>> { | ||
109 | let red = self.red(); | ||
110 | let parent = self.parent()?; | ||
111 | let next_sibling_idx = red.index_in_parent()? + 1; | ||
112 | if next_sibling_idx == red.n_children() { | ||
113 | return None; | ||
114 | } | ||
115 | Some(SyntaxNode { | ||
116 | root: self.root.clone(), | ||
117 | red: parent.red().nth_child(next_sibling_idx), | ||
118 | }) | ||
119 | } | ||
120 | |||
95 | fn red(&self) -> &RedNode { | 121 | fn red(&self) -> &RedNode { |
96 | unsafe { self.red.as_ref() } | 122 | unsafe { self.red.as_ref() } |
97 | } | 123 | } |