From 9c804accf19d1a93739a7cbfdd1da5528833d6c3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 31 Dec 2017 18:58:03 +0300 Subject: Move tree to a separate module --- src/tree.rs | 101 -------------------------------------------------------- src/tree/mod.rs | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 101 deletions(-) delete mode 100644 src/tree.rs create mode 100644 src/tree/mod.rs diff --git a/src/tree.rs b/src/tree.rs deleted file mode 100644 index 2ac25e795..000000000 --- a/src/tree.rs +++ /dev/null @@ -1,101 +0,0 @@ -use text::{TextUnit, TextRange}; -use syntax_kinds::syntax_info; - -use std::fmt; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct SyntaxKind(pub(crate) u32); - -impl SyntaxKind { - fn info(self) -> &'static SyntaxInfo { - syntax_info(self) - } -} - -impl fmt::Debug for SyntaxKind { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let name = self.info().name; - f.write_str(name) - } -} - - -pub(crate) struct SyntaxInfo { - pub name: &'static str, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Token { - pub kind: SyntaxKind, - pub len: TextUnit, -} - -pub struct File { - text: String, - nodes: Vec, -} - -impl File { - pub fn root<'f>(&'f self) -> Node<'f> { - assert!(!self.nodes.is_empty()); - Node { file: self, idx: NodeIdx(0) } - } -} - -#[derive(Clone, Copy)] -pub struct Node<'f> { - file: &'f File, - idx: NodeIdx, -} - -impl<'f> Node<'f> { - pub fn kind(&self) -> SyntaxKind { - self.data().kind - } - - pub fn text(&self) -> &'f str { - let range = self.data().range; - &self.file.text.as_str()[range] - } - - pub fn parent(&self) -> Option> { - self.as_node(self.data().parent) - } - - pub fn children(&self) -> Children<'f> { - Children { next: self.as_node(self.data().first_child) } - } - - fn data(&self) -> &'f NodeData { - &self.file.nodes[self.idx.0 as usize] - } - - fn as_node(&self, idx: Option) -> Option> { - idx.map(|idx| Node { file: self.file, idx }) - } -} - -pub struct Children<'f> { - next: Option>, -} - -impl<'f> Iterator for Children<'f> { - type Item = Node<'f>; - - fn next(&mut self) -> Option> { - let next = self.next; - self.next = next.and_then(|node| node.as_node(node.data().next_sibling)); - next - } -} - -#[derive(Clone, Copy)] -struct NodeIdx(u32); - -struct NodeData { - kind: SyntaxKind, - range: TextRange, - parent: Option, - first_child: Option, - next_sibling: Option, -} diff --git a/src/tree/mod.rs b/src/tree/mod.rs new file mode 100644 index 000000000..2ac25e795 --- /dev/null +++ b/src/tree/mod.rs @@ -0,0 +1,101 @@ +use text::{TextUnit, TextRange}; +use syntax_kinds::syntax_info; + +use std::fmt; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct SyntaxKind(pub(crate) u32); + +impl SyntaxKind { + fn info(self) -> &'static SyntaxInfo { + syntax_info(self) + } +} + +impl fmt::Debug for SyntaxKind { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let name = self.info().name; + f.write_str(name) + } +} + + +pub(crate) struct SyntaxInfo { + pub name: &'static str, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Token { + pub kind: SyntaxKind, + pub len: TextUnit, +} + +pub struct File { + text: String, + nodes: Vec, +} + +impl File { + pub fn root<'f>(&'f self) -> Node<'f> { + assert!(!self.nodes.is_empty()); + Node { file: self, idx: NodeIdx(0) } + } +} + +#[derive(Clone, Copy)] +pub struct Node<'f> { + file: &'f File, + idx: NodeIdx, +} + +impl<'f> Node<'f> { + pub fn kind(&self) -> SyntaxKind { + self.data().kind + } + + pub fn text(&self) -> &'f str { + let range = self.data().range; + &self.file.text.as_str()[range] + } + + pub fn parent(&self) -> Option> { + self.as_node(self.data().parent) + } + + pub fn children(&self) -> Children<'f> { + Children { next: self.as_node(self.data().first_child) } + } + + fn data(&self) -> &'f NodeData { + &self.file.nodes[self.idx.0 as usize] + } + + fn as_node(&self, idx: Option) -> Option> { + idx.map(|idx| Node { file: self.file, idx }) + } +} + +pub struct Children<'f> { + next: Option>, +} + +impl<'f> Iterator for Children<'f> { + type Item = Node<'f>; + + fn next(&mut self) -> Option> { + let next = self.next; + self.next = next.and_then(|node| node.as_node(node.data().next_sibling)); + next + } +} + +#[derive(Clone, Copy)] +struct NodeIdx(u32); + +struct NodeData { + kind: SyntaxKind, + range: TextRange, + parent: Option, + first_child: Option, + next_sibling: Option, +} -- cgit v1.2.3