From aea86d154ec5adde6adb05088a50f01380ffb8bf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jul 2018 23:45:10 +0300 Subject: stackless traversal --- src/yellow/red.rs | 3 +++ src/yellow/syntax.rs | 28 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) (limited to 'src/yellow') 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 { pub(crate) fn parent(&self) -> Option> { Some(self.parent.as_ref()?.parent) } + pub(crate) fn index_in_parent(&self) -> Option { + Some(self.parent.as_ref()?.index_in_parent) + } } 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> { red: ptr::NonNull, } +impl PartialEq> for SyntaxNode { + fn eq(&self, other: &SyntaxNode) -> bool { + self.red == other.red + } +} + +impl Eq for SyntaxNode { +} + pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; #[derive(Debug)] @@ -53,7 +62,7 @@ impl SyntaxNode> { } impl SyntaxNode { - pub fn borrow<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { + pub fn as_ref<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { SyntaxNode { root: &*self.root, red: ptr::NonNull::clone(&self.red), @@ -92,6 +101,23 @@ impl SyntaxNode { }) } + pub fn first_child(&self) -> Option> { + self.children().next() + } + + pub fn next_sibling(&self) -> Option> { + let red = self.red(); + let parent = self.parent()?; + let next_sibling_idx = red.index_in_parent()? + 1; + if next_sibling_idx == red.n_children() { + return None; + } + Some(SyntaxNode { + root: self.root.clone(), + red: parent.red().nth_child(next_sibling_idx), + }) + } + fn red(&self) -> &RedNode { unsafe { self.red.as_ref() } } -- cgit v1.2.3