1 use text::{TextRange, TextUnit};
2 use syntax_kinds::syntax_info;
8 pub use self::file_builder::{FileBuilder, Sink};
10 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11 pub struct SyntaxKind(pub(crate) u32);
13 pub(crate) const EOF: SyntaxKind = SyntaxKind(!0);
14 pub(crate) const EOF_INFO: SyntaxInfo = SyntaxInfo { name: "EOF" };
16 pub(crate) const TOMBSTONE: SyntaxKind = SyntaxKind(!0 - 1);
17 pub(crate) const TOMBSTONE_INFO: SyntaxInfo = SyntaxInfo { name: "TOMBSTONE" };
20 fn info(self) -> &'static SyntaxInfo {
23 TOMBSTONE => &TOMBSTONE_INFO,
24 _ => syntax_info(self),
29 impl fmt::Debug for SyntaxKind {
30 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31 let name = self.info().name;
36 pub(crate) struct SyntaxInfo {
37 pub name: &'static str,
40 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
49 errors: Vec<SyntaxErrorData>,
53 pub fn root<'f>(&'f self) -> Node<'f> {
54 assert!(!self.nodes.is_empty());
62 #[derive(Clone, Copy)]
69 pub fn kind(&self) -> SyntaxKind {
73 pub fn range(&self) -> TextRange {
77 pub fn text(&self) -> &'f str {
78 &self.file.text.as_str()[self.range()]
81 pub fn parent(&self) -> Option<Node<'f>> {
82 self.as_node(self.data().parent)
85 pub fn children(&self) -> Children<'f> {
87 next: self.as_node(self.data().first_child),
91 pub fn errors(&self) -> SyntaxErrors<'f> {
92 let pos = self.file.errors.iter().position(|e| e.node == self.idx);
93 let next = pos.map(|i| ErrorIdx(i as u32)).map(|idx| SyntaxError {
100 fn data(&self) -> &'f NodeData {
101 &self.file.nodes[self.idx]
104 fn as_node(&self, idx: Option<NodeIdx>) -> Option<Node<'f>> {
112 impl<'f> fmt::Debug for Node<'f> {
113 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
114 write!(fmt, "{:?}@{:?}", self.kind(), self.range())
118 impl<'f> cmp::PartialEq<Node<'f>> for Node<'f> {
119 fn eq(&self, other: &Node<'f>) -> bool {
120 self.idx == other.idx && ::std::ptr::eq(self.file, other.file)
124 impl<'f> cmp::Eq for Node<'f> {}
126 #[derive(Clone, Copy)]
127 pub struct SyntaxError<'f> {
132 impl<'f> SyntaxError<'f> {
133 pub fn message(&self) -> &'f str {
134 self.data().message.as_str()
137 pub fn after_child(&self) -> Option<Node<'f>> {
138 let idx = self.data().after_child?;
145 fn data(&self) -> &'f SyntaxErrorData {
146 &self.file.errors[self.idx]
149 fn next(&self) -> Option<SyntaxError<'f>> {
150 let next_idx = self.idx.0 + 1;
151 if !((next_idx as usize) < self.file.errors.len()) {
154 let result = SyntaxError {
156 idx: ErrorIdx(next_idx),
158 if result.data().node != self.data().node {
165 pub struct Children<'f> {
166 next: Option<Node<'f>>,
169 impl<'f> Iterator for Children<'f> {
170 type Item = Node<'f>;
172 fn next(&mut self) -> Option<Node<'f>> {
173 let next = self.next;
174 self.next = next.and_then(|node| node.as_node(node.data().next_sibling));
179 pub struct SyntaxErrors<'f> {
180 next: Option<SyntaxError<'f>>,
183 impl<'f> Iterator for SyntaxErrors<'f> {
184 type Item = SyntaxError<'f>;
186 fn next(&mut self) -> Option<SyntaxError<'f>> {
187 let next = self.next;
188 self.next = next.as_ref().and_then(SyntaxError::next);
193 #[derive(Clone, Copy, PartialEq, Eq)]
199 parent: Option<NodeIdx>,
200 first_child: Option<NodeIdx>,
201 next_sibling: Option<NodeIdx>,
204 impl ::std::ops::Index<NodeIdx> for Vec<NodeData> {
205 type Output = NodeData;
207 fn index(&self, NodeIdx(idx): NodeIdx) -> &NodeData {
212 impl ::std::ops::IndexMut<NodeIdx> for Vec<NodeData> {
213 fn index_mut(&mut self, NodeIdx(idx): NodeIdx) -> &mut NodeData {
214 &mut self[idx as usize]
218 #[derive(Clone, Copy)]
219 struct ErrorIdx(u32);
221 struct SyntaxErrorData {
224 after_child: Option<NodeIdx>,
227 impl ::std::ops::Index<ErrorIdx> for Vec<SyntaxErrorData> {
228 type Output = SyntaxErrorData;
230 fn index(&self, ErrorIdx(idx): ErrorIdx) -> &SyntaxErrorData {