aboutsummaryrefslogtreecommitdiff
path: root/src/tree/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tree/mod.rs')
-rw-r--r--src/tree/mod.rs44
1 files changed, 24 insertions, 20 deletions
diff --git a/src/tree/mod.rs b/src/tree/mod.rs
index 3315b926e..a330caf54 100644
--- a/src/tree/mod.rs
+++ b/src/tree/mod.rs
@@ -1,4 +1,4 @@
1use text::{TextUnit, TextRange}; 1use text::{TextRange, TextUnit};
2use syntax_kinds::syntax_info; 2use syntax_kinds::syntax_info;
3 3
4use std::fmt; 4use std::fmt;
@@ -11,15 +11,10 @@ pub use self::file_builder::{FileBuilder, Sink};
11pub struct SyntaxKind(pub(crate) u32); 11pub struct SyntaxKind(pub(crate) u32);
12 12
13pub(crate) const EOF: SyntaxKind = SyntaxKind(!0); 13pub(crate) const EOF: SyntaxKind = SyntaxKind(!0);
14pub(crate) const EOF_INFO: SyntaxInfo = SyntaxInfo { 14pub(crate) const EOF_INFO: SyntaxInfo = SyntaxInfo { name: "EOF" };
15 name: "EOF"
16};
17 15
18pub(crate) const TOMBSTONE: SyntaxKind = SyntaxKind(!0 - 1); 16pub(crate) const TOMBSTONE: SyntaxKind = SyntaxKind(!0 - 1);
19pub(crate) const TOMBSTONE_INFO: SyntaxInfo = SyntaxInfo { 17pub(crate) const TOMBSTONE_INFO: SyntaxInfo = SyntaxInfo { name: "TOMBSTONE" };
20 name: "TOMBSTONE"
21};
22
23 18
24impl SyntaxKind { 19impl SyntaxKind {
25 fn info(self) -> &'static SyntaxInfo { 20 fn info(self) -> &'static SyntaxInfo {
@@ -38,7 +33,6 @@ impl fmt::Debug for SyntaxKind {
38 } 33 }
39} 34}
40 35
41
42pub(crate) struct SyntaxInfo { 36pub(crate) struct SyntaxInfo {
43 pub name: &'static str, 37 pub name: &'static str,
44} 38}
@@ -58,7 +52,10 @@ pub struct File {
58impl File { 52impl File {
59 pub fn root<'f>(&'f self) -> Node<'f> { 53 pub fn root<'f>(&'f self) -> Node<'f> {
60 assert!(!self.nodes.is_empty()); 54 assert!(!self.nodes.is_empty());
61 Node { file: self, idx: NodeIdx(0) } 55 Node {
56 file: self,
57 idx: NodeIdx(0),
58 }
62 } 59 }
63} 60}
64 61
@@ -86,14 +83,17 @@ impl<'f> Node<'f> {
86 } 83 }
87 84
88 pub fn children(&self) -> Children<'f> { 85 pub fn children(&self) -> Children<'f> {
89 Children { next: self.as_node(self.data().first_child) } 86 Children {
87 next: self.as_node(self.data().first_child),
88 }
90 } 89 }
91 90
92 pub fn errors(&self) -> SyntaxErrors<'f> { 91 pub fn errors(&self) -> SyntaxErrors<'f> {
93 let pos = self.file.errors.iter().position(|e| e.node == self.idx); 92 let pos = self.file.errors.iter().position(|e| e.node == self.idx);
94 let next = pos 93 let next = pos.map(|i| ErrorIdx(i as u32)).map(|idx| SyntaxError {
95 .map(|i| ErrorIdx(i as u32)) 94 file: self.file,
96 .map(|idx| SyntaxError { file: self.file, idx }); 95 idx,
96 });
97 SyntaxErrors { next } 97 SyntaxErrors { next }
98 } 98 }
99 99
@@ -102,7 +102,10 @@ impl<'f> Node<'f> {
102 } 102 }
103 103
104 fn as_node(&self, idx: Option<NodeIdx>) -> Option<Node<'f>> { 104 fn as_node(&self, idx: Option<NodeIdx>) -> Option<Node<'f>> {
105 idx.map(|idx| Node { file: self.file, idx }) 105 idx.map(|idx| Node {
106 file: self.file,
107 idx,
108 })
106 } 109 }
107} 110}
108 111
@@ -118,8 +121,7 @@ impl<'f> cmp::PartialEq<Node<'f>> for Node<'f> {
118 } 121 }
119} 122}
120 123
121impl<'f> cmp::Eq for Node<'f> { 124impl<'f> cmp::Eq for Node<'f> {}
122}
123 125
124#[derive(Clone, Copy)] 126#[derive(Clone, Copy)]
125pub struct SyntaxError<'f> { 127pub struct SyntaxError<'f> {
@@ -134,7 +136,10 @@ impl<'f> SyntaxError<'f> {
134 136
135 pub fn after_child(&self) -> Option<Node<'f>> { 137 pub fn after_child(&self) -> Option<Node<'f>> {
136 let idx = self.data().after_child?; 138 let idx = self.data().after_child?;
137 Some(Node { file: self.file, idx }) 139 Some(Node {
140 file: self.file,
141 idx,
142 })
138 } 143 }
139 144
140 fn data(&self) -> &'f SyntaxErrorData { 145 fn data(&self) -> &'f SyntaxErrorData {
@@ -148,7 +153,7 @@ impl<'f> SyntaxError<'f> {
148 } 153 }
149 let result = SyntaxError { 154 let result = SyntaxError {
150 file: self.file, 155 file: self.file,
151 idx: ErrorIdx(next_idx) 156 idx: ErrorIdx(next_idx),
152 }; 157 };
153 if result.data().node != self.data().node { 158 if result.data().node != self.data().node {
154 return None; 159 return None;
@@ -185,7 +190,6 @@ impl<'f> Iterator for SyntaxErrors<'f> {
185 } 190 }
186} 191}
187 192
188
189#[derive(Clone, Copy, PartialEq, Eq)] 193#[derive(Clone, Copy, PartialEq, Eq)]
190struct NodeIdx(u32); 194struct NodeIdx(u32);
191 195