diff options
author | Aleksey Kladov <[email protected]> | 2017-12-31 14:54:33 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2017-12-31 14:54:33 +0000 |
commit | 9ce4db545efba697f20ab5cecbefc0589c7146ca (patch) | |
tree | ecda02126426b473bf2f70777a148e569c114c98 /src/tree.rs | |
parent | 98a58bf806ffda1b4d3352ed0f3e494fa25c8c74 (diff) |
Parser: groundwork
Diffstat (limited to 'src/tree.rs')
-rw-r--r-- | src/tree.rs | 74 |
1 files changed, 72 insertions, 2 deletions
diff --git a/src/tree.rs b/src/tree.rs index 0924f38d0..2ac25e795 100644 --- a/src/tree.rs +++ b/src/tree.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use text::{TextUnit}; | 1 | use text::{TextUnit, TextRange}; |
2 | use syntax_kinds::syntax_info; | 2 | use syntax_kinds::syntax_info; |
3 | 3 | ||
4 | use std::fmt; | 4 | use std::fmt; |
@@ -28,4 +28,74 @@ pub(crate) struct SyntaxInfo { | |||
28 | pub struct Token { | 28 | pub struct Token { |
29 | pub kind: SyntaxKind, | 29 | pub kind: SyntaxKind, |
30 | pub len: TextUnit, | 30 | pub len: TextUnit, |
31 | } \ No newline at end of file | 31 | } |
32 | |||
33 | pub struct File { | ||
34 | text: String, | ||
35 | nodes: Vec<NodeData>, | ||
36 | } | ||
37 | |||
38 | impl File { | ||
39 | pub fn root<'f>(&'f self) -> Node<'f> { | ||
40 | assert!(!self.nodes.is_empty()); | ||
41 | Node { file: self, idx: NodeIdx(0) } | ||
42 | } | ||
43 | } | ||
44 | |||
45 | #[derive(Clone, Copy)] | ||
46 | pub struct Node<'f> { | ||
47 | file: &'f File, | ||
48 | idx: NodeIdx, | ||
49 | } | ||
50 | |||
51 | impl<'f> Node<'f> { | ||
52 | pub fn kind(&self) -> SyntaxKind { | ||
53 | self.data().kind | ||
54 | } | ||
55 | |||
56 | pub fn text(&self) -> &'f str { | ||
57 | let range = self.data().range; | ||
58 | &self.file.text.as_str()[range] | ||
59 | } | ||
60 | |||
61 | pub fn parent(&self) -> Option<Node<'f>> { | ||
62 | self.as_node(self.data().parent) | ||
63 | } | ||
64 | |||
65 | pub fn children(&self) -> Children<'f> { | ||
66 | Children { next: self.as_node(self.data().first_child) } | ||
67 | } | ||
68 | |||
69 | fn data(&self) -> &'f NodeData { | ||
70 | &self.file.nodes[self.idx.0 as usize] | ||
71 | } | ||
72 | |||
73 | fn as_node(&self, idx: Option<NodeIdx>) -> Option<Node<'f>> { | ||
74 | idx.map(|idx| Node { file: self.file, idx }) | ||
75 | } | ||
76 | } | ||
77 | |||
78 | pub struct Children<'f> { | ||
79 | next: Option<Node<'f>>, | ||
80 | } | ||
81 | |||
82 | impl<'f> Iterator for Children<'f> { | ||
83 | type Item = Node<'f>; | ||
84 | |||
85 | fn next(&mut self) -> Option<Node<'f>> { | ||
86 | let next = self.next; | ||
87 | self.next = next.and_then(|node| node.as_node(node.data().next_sibling)); | ||
88 | next | ||
89 | } | ||
90 | } | ||
91 | |||
92 | #[derive(Clone, Copy)] | ||
93 | struct NodeIdx(u32); | ||
94 | |||
95 | struct NodeData { | ||
96 | kind: SyntaxKind, | ||
97 | range: TextRange, | ||
98 | parent: Option<NodeIdx>, | ||
99 | first_child: Option<NodeIdx>, | ||
100 | next_sibling: Option<NodeIdx>, | ||
101 | } | ||