1 use text::{TextRange, TextUnit};
7 pub use self::file_builder::{FileBuilder, Sink};
9 pub use syntax_kinds::SyntaxKind;
11 impl fmt::Debug for SyntaxKind {
12 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13 let name = self.info().name;
18 pub(crate) struct SyntaxInfo {
19 pub name: &'static str,
22 /// A token of Rust source.
23 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
25 /// The kind of token.
27 /// The length of the token.
31 /// The contents of a Rust source file.
36 errors: Vec<SyntaxErrorData>,
40 /// The root node of this source file.
41 pub fn root<'f>(&'f self) -> Node<'f> {
42 assert!(!self.nodes.is_empty());
50 /// A reference to a token in a Rust source file.
51 #[derive(Clone, Copy)]
58 /// The kind of the token at this node.
59 pub fn kind(&self) -> SyntaxKind {
63 /// The text range covered by the token at this node.
64 pub fn range(&self) -> TextRange {
68 /// The text at this node.
69 pub fn text(&self) -> &'f str {
70 &self.file.text.as_str()[self.range()]
73 /// The parent node to this node.
74 pub fn parent(&self) -> Option<Node<'f>> {
75 self.as_node(self.data().parent)
78 /// The children nodes of this node.
79 pub fn children(&self) -> Children<'f> {
81 next: self.as_node(self.data().first_child),
85 /// Any errors contained in this node.
86 pub fn errors(&self) -> SyntaxErrors<'f> {
87 let pos = self.file.errors.iter().position(|e| e.node == self.idx);
88 let next = pos.map(|i| ErrorIdx(i as u32)).map(|idx| SyntaxError {
95 fn data(&self) -> &'f NodeData {
96 &self.file.nodes[self.idx]
99 fn as_node(&self, idx: Option<NodeIdx>) -> Option<Node<'f>> {
107 impl<'f> fmt::Debug for Node<'f> {
108 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
109 write!(fmt, "{:?}@{:?}", self.kind(), self.range())
113 impl<'f> cmp::PartialEq<Node<'f>> for Node<'f> {
114 fn eq(&self, other: &Node<'f>) -> bool {
115 self.idx == other.idx && ::std::ptr::eq(self.file, other.file)
119 impl<'f> cmp::Eq for Node<'f> {}
121 #[derive(Clone, Copy, Debug)]
122 pub struct SyntaxError<'f> {
127 impl<'f> SyntaxError<'f> {
128 pub fn message(&self) -> &'f str {
129 self.data().message.as_str()
132 pub fn after_child(&self) -> Option<Node<'f>> {
133 let idx = self.data().after_child?;
140 fn data(&self) -> &'f SyntaxErrorData {
141 &self.file.errors[self.idx]
144 fn next(&self) -> Option<SyntaxError<'f>> {
145 let next_idx = self.idx.0 + 1;
146 if !((next_idx as usize) < self.file.errors.len()) {
149 let result = SyntaxError {
151 idx: ErrorIdx(next_idx),
153 if result.data().node != self.data().node {
161 pub struct Children<'f> {
162 next: Option<Node<'f>>,
165 impl<'f> Iterator for Children<'f> {
166 type Item = Node<'f>;
168 fn next(&mut self) -> Option<Node<'f>> {
169 let next = self.next;
170 self.next = next.and_then(|node| node.as_node(node.data().next_sibling));
176 pub struct SyntaxErrors<'f> {
177 next: Option<SyntaxError<'f>>,
180 impl<'f> Iterator for SyntaxErrors<'f> {
181 type Item = SyntaxError<'f>;
183 fn next(&mut self) -> Option<SyntaxError<'f>> {
184 let next = self.next;
185 self.next = next.as_ref().and_then(SyntaxError::next);
190 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
197 parent: Option<NodeIdx>,
198 first_child: Option<NodeIdx>,
199 next_sibling: Option<NodeIdx>,
202 impl ::std::ops::Index<NodeIdx> for Vec<NodeData> {
203 type Output = NodeData;
205 fn index(&self, NodeIdx(idx): NodeIdx) -> &NodeData {
210 impl ::std::ops::IndexMut<NodeIdx> for Vec<NodeData> {
211 fn index_mut(&mut self, NodeIdx(idx): NodeIdx) -> &mut NodeData {
212 &mut self[idx as usize]
216 #[derive(Clone, Copy, Debug)]
217 struct ErrorIdx(u32);
220 struct SyntaxErrorData {
223 after_child: Option<NodeIdx>,
226 impl ::std::ops::Index<ErrorIdx> for Vec<SyntaxErrorData> {
227 type Output = SyntaxErrorData;
229 fn index(&self, ErrorIdx(idx): ErrorIdx) -> &SyntaxErrorData {