aboutsummaryrefslogtreecommitdiff
path: root/src/tree
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-01-07 18:09:05 +0000
committerAleksey Kladov <[email protected]>2018-01-07 18:09:05 +0000
commit5562931e4f3c6c57e9122c3ce34d941fb1ff6e5b (patch)
tree0e87cba8e91d4bf96d752ada65b4d4a73e4e0ac9 /src/tree
parentfc4d6cc298fe901d2bf92a30a3efd67233c67a3a (diff)
Introduce EOF token
Diffstat (limited to 'src/tree')
-rw-r--r--src/tree/mod.rs218
1 files changed, 113 insertions, 105 deletions
diff --git a/src/tree/mod.rs b/src/tree/mod.rs
index 7f4d427ba..d8f843737 100644
--- a/src/tree/mod.rs
+++ b/src/tree/mod.rs
@@ -10,8 +10,16 @@ pub use self::file_builder::{FileBuilder, Sink};
10#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 10#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct SyntaxKind(pub(crate) u32); 11pub struct SyntaxKind(pub(crate) u32);
12 12
13pub(crate) const EOF: SyntaxKind = SyntaxKind(10000);
14pub(crate) const EOF_INFO: SyntaxInfo = SyntaxInfo {
15 name: "EOF"
16};
17
13impl SyntaxKind { 18impl SyntaxKind {
14 fn info(self) -> &'static SyntaxInfo { 19 fn info(self) -> &'static SyntaxInfo {
20 if self == EOF {
21 return &EOF_INFO;
22 }
15 syntax_info(self) 23 syntax_info(self)
16 } 24 }
17} 25}
@@ -35,72 +43,72 @@ pub struct Token {
35} 43}
36 44
37pub struct File { 45pub struct File {
38 text: String, 46 text: String,
39 nodes: Vec<NodeData>, 47 nodes: Vec<NodeData>,
40 errors: Vec<SyntaxErrorData>, 48 errors: Vec<SyntaxErrorData>,
41} 49}
42 50
43impl File { 51impl File {
44 pub fn root<'f>(&'f self) -> Node<'f> { 52 pub fn root<'f>(&'f self) -> Node<'f> {
45 assert!(!self.nodes.is_empty()); 53 assert!(!self.nodes.is_empty());
46 Node { file: self, idx: NodeIdx(0) } 54 Node { file: self, idx: NodeIdx(0) }
47 } 55 }
48} 56}
49 57
50#[derive(Clone, Copy)] 58#[derive(Clone, Copy)]
51pub struct Node<'f> { 59pub struct Node<'f> {
52 file: &'f File, 60 file: &'f File,
53 idx: NodeIdx, 61 idx: NodeIdx,
54} 62}
55 63
56impl<'f> Node<'f> { 64impl<'f> Node<'f> {
57 pub fn kind(&self) -> SyntaxKind { 65 pub fn kind(&self) -> SyntaxKind {
58 self.data().kind 66 self.data().kind
59 } 67 }
60 68
61 pub fn range(&self) -> TextRange { 69 pub fn range(&self) -> TextRange {
62 self.data().range 70 self.data().range
63 } 71 }
64 72
65 pub fn text(&self) -> &'f str { 73 pub fn text(&self) -> &'f str {
66 &self.file.text.as_str()[self.range()] 74 &self.file.text.as_str()[self.range()]
67 } 75 }
68 76
69 pub fn parent(&self) -> Option<Node<'f>> { 77 pub fn parent(&self) -> Option<Node<'f>> {
70 self.as_node(self.data().parent) 78 self.as_node(self.data().parent)
71 } 79 }
72 80
73 pub fn children(&self) -> Children<'f> { 81 pub fn children(&self) -> Children<'f> {
74 Children { next: self.as_node(self.data().first_child) } 82 Children { next: self.as_node(self.data().first_child) }
75 } 83 }
76 84
77 pub fn errors(&self) -> SyntaxErrors<'f> { 85 pub fn errors(&self) -> SyntaxErrors<'f> {
78 let pos = self.file.errors.iter().position(|e| e.node == self.idx); 86 let pos = self.file.errors.iter().position(|e| e.node == self.idx);
79 let next = pos 87 let next = pos
80 .map(|i| ErrorIdx(i as u32)) 88 .map(|i| ErrorIdx(i as u32))
81 .map(|idx| SyntaxError { file: self.file, idx }); 89 .map(|idx| SyntaxError { file: self.file, idx });
82 SyntaxErrors { next } 90 SyntaxErrors { next }
83 } 91 }
84 92
85 fn data(&self) -> &'f NodeData { 93 fn data(&self) -> &'f NodeData {
86 &self.file.nodes[self.idx] 94 &self.file.nodes[self.idx]
87 } 95 }
88 96
89 fn as_node(&self, idx: Option<NodeIdx>) -> Option<Node<'f>> { 97 fn as_node(&self, idx: Option<NodeIdx>) -> Option<Node<'f>> {
90 idx.map(|idx| Node { file: self.file, idx }) 98 idx.map(|idx| Node { file: self.file, idx })
91 } 99 }
92} 100}
93 101
94impl<'f> fmt::Debug for Node<'f> { 102impl<'f> fmt::Debug for Node<'f> {
95 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 103 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
96 write!(fmt, "{:?}@{:?}", self.kind(), self.range()) 104 write!(fmt, "{:?}@{:?}", self.kind(), self.range())
97 } 105 }
98} 106}
99 107
100impl<'f> cmp::PartialEq<Node<'f>> for Node<'f> { 108impl<'f> cmp::PartialEq<Node<'f>> for Node<'f> {
101 fn eq(&self, other: &Node<'f>) -> bool { 109 fn eq(&self, other: &Node<'f>) -> bool {
102 self.idx == other.idx && ::std::ptr::eq(self.file, other.file) 110 self.idx == other.idx && ::std::ptr::eq(self.file, other.file)
103 } 111 }
104} 112}
105 113
106impl<'f> cmp::Eq for Node<'f> { 114impl<'f> cmp::Eq for Node<'f> {
@@ -108,66 +116,66 @@ impl<'f> cmp::Eq for Node<'f> {
108 116
109#[derive(Clone, Copy)] 117#[derive(Clone, Copy)]
110pub struct SyntaxError<'f> { 118pub struct SyntaxError<'f> {
111 file: &'f File, 119 file: &'f File,
112 idx: ErrorIdx, 120 idx: ErrorIdx,
113} 121}
114 122
115impl<'f> SyntaxError<'f> { 123impl<'f> SyntaxError<'f> {
116 pub fn message(&self) -> &'f str { 124 pub fn message(&self) -> &'f str {
117 self.data().message.as_str() 125 self.data().message.as_str()
118 } 126 }
119 127
120 pub fn after_child(&self) -> Option<Node<'f>> { 128 pub fn after_child(&self) -> Option<Node<'f>> {
121 let idx = self.data().after_child?; 129 let idx = self.data().after_child?;
122 Some(Node { file: self.file, idx }) 130 Some(Node { file: self.file, idx })
123 } 131 }
124 132
125 fn data(&self) -> &'f SyntaxErrorData { 133 fn data(&self) -> &'f SyntaxErrorData {
126 &self.file.errors[self.idx] 134 &self.file.errors[self.idx]
127 } 135 }
128 136
129 fn next(&self) -> Option<SyntaxError<'f>> { 137 fn next(&self) -> Option<SyntaxError<'f>> {
130 let next_idx = self.idx.0 + 1; 138 let next_idx = self.idx.0 + 1;
131 if !((next_idx as usize) < self.file.errors.len()) { 139 if !((next_idx as usize) < self.file.errors.len()) {
132 return None; 140 return None;
133 } 141 }
134 let result = SyntaxError { 142 let result = SyntaxError {
135 file: self.file, 143 file: self.file,
136 idx: ErrorIdx(next_idx) 144 idx: ErrorIdx(next_idx)
137 }; 145 };
138 if result.data().node != self.data().node { 146 if result.data().node != self.data().node {
139 return None; 147 return None;
140 } 148 }
141 Some(result) 149 Some(result)
142 } 150 }
143} 151}
144 152
145pub struct Children<'f> { 153pub struct Children<'f> {
146 next: Option<Node<'f>>, 154 next: Option<Node<'f>>,
147} 155}
148 156
149impl<'f> Iterator for Children<'f> { 157impl<'f> Iterator for Children<'f> {
150 type Item = Node<'f>; 158 type Item = Node<'f>;
151 159
152 fn next(&mut self) -> Option<Node<'f>> { 160 fn next(&mut self) -> Option<Node<'f>> {
153 let next = self.next; 161 let next = self.next;
154 self.next = next.and_then(|node| node.as_node(node.data().next_sibling)); 162 self.next = next.and_then(|node| node.as_node(node.data().next_sibling));
155 next 163 next
156 } 164 }
157} 165}
158 166
159pub struct SyntaxErrors<'f> { 167pub struct SyntaxErrors<'f> {
160 next: Option<SyntaxError<'f>>, 168 next: Option<SyntaxError<'f>>,
161} 169}
162 170
163impl<'f> Iterator for SyntaxErrors<'f> { 171impl<'f> Iterator for SyntaxErrors<'f> {
164 type Item = SyntaxError<'f>; 172 type Item = SyntaxError<'f>;
165 173
166 fn next(&mut self) -> Option<SyntaxError<'f>> { 174 fn next(&mut self) -> Option<SyntaxError<'f>> {
167 let next = self.next; 175 let next = self.next;
168 self.next = next.as_ref().and_then(SyntaxError::next); 176 self.next = next.as_ref().and_then(SyntaxError::next);
169 next 177 next
170 } 178 }
171} 179}
172 180
173 181
@@ -175,40 +183,40 @@ impl<'f> Iterator for SyntaxErrors<'f> {
175struct NodeIdx(u32); 183struct NodeIdx(u32);
176 184
177struct NodeData { 185struct NodeData {
178 kind: SyntaxKind, 186 kind: SyntaxKind,
179 range: TextRange, 187 range: TextRange,
180 parent: Option<NodeIdx>, 188 parent: Option<NodeIdx>,
181 first_child: Option<NodeIdx>, 189 first_child: Option<NodeIdx>,
182 next_sibling: Option<NodeIdx>, 190 next_sibling: Option<NodeIdx>,
183} 191}
184 192
185impl ::std::ops::Index<NodeIdx> for Vec<NodeData> { 193impl ::std::ops::Index<NodeIdx> for Vec<NodeData> {
186 type Output = NodeData; 194 type Output = NodeData;
187 195
188 fn index(&self, NodeIdx(idx): NodeIdx) -> &NodeData { 196 fn index(&self, NodeIdx(idx): NodeIdx) -> &NodeData {
189 &self[idx as usize] 197 &self[idx as usize]
190 } 198 }
191} 199}
192 200
193impl ::std::ops::IndexMut<NodeIdx> for Vec<NodeData> { 201impl ::std::ops::IndexMut<NodeIdx> for Vec<NodeData> {
194 fn index_mut(&mut self, NodeIdx(idx): NodeIdx) -> &mut NodeData { 202 fn index_mut(&mut self, NodeIdx(idx): NodeIdx) -> &mut NodeData {
195 &mut self[idx as usize] 203 &mut self[idx as usize]
196 } 204 }
197} 205}
198 206
199#[derive(Clone, Copy)] 207#[derive(Clone, Copy)]
200struct ErrorIdx(u32); 208struct ErrorIdx(u32);
201 209
202struct SyntaxErrorData { 210struct SyntaxErrorData {
203 node: NodeIdx, 211 node: NodeIdx,
204 message: String, 212 message: String,
205 after_child: Option<NodeIdx>, 213 after_child: Option<NodeIdx>,
206} 214}
207 215
208impl ::std::ops::Index<ErrorIdx> for Vec<SyntaxErrorData> { 216impl ::std::ops::Index<ErrorIdx> for Vec<SyntaxErrorData> {
209 type Output = SyntaxErrorData; 217 type Output = SyntaxErrorData;
210 218
211 fn index(&self, ErrorIdx(idx): ErrorIdx) -> &SyntaxErrorData { 219 fn index(&self, ErrorIdx(idx): ErrorIdx) -> &SyntaxErrorData {
212 &self[idx as usize] 220 &self[idx as usize]
213 } 221 }
214} 222}