1 use text::{TextRange, TextUnit};
2 use syntax_kinds::syntax_info;
8 pub use self::file_builder::{FileBuilder, Sink};
10 /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`.
11 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
12 pub struct SyntaxKind(pub(crate) u32);
14 pub(crate) const EOF: SyntaxKind = SyntaxKind(!0);
15 pub(crate) const EOF_INFO: SyntaxInfo = SyntaxInfo { name: "EOF" };
17 pub(crate) const TOMBSTONE: SyntaxKind = SyntaxKind(!0 - 1);
18 pub(crate) const TOMBSTONE_INFO: SyntaxInfo = SyntaxInfo { name: "TOMBSTONE" };
21 fn info(self) -> &'static SyntaxInfo {
24 TOMBSTONE => &TOMBSTONE_INFO,
25 _ => syntax_info(self),
30 impl fmt::Debug for SyntaxKind {
31 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32 let name = self.info().name;
37 pub(crate) struct SyntaxInfo {
38 pub name: &'static str,
41 /// A token of Rust source.
42 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
44 /// The kind of token.
46 /// The length of the token.
50 /// The contents of a Rust source file.
55 errors: Vec<SyntaxErrorData>,
59 /// The root node of this source file.
60 pub fn root<'f>(&'f self) -> Node<'f> {
61 assert!(!self.nodes.is_empty());
69 /// A reference to a token in a Rust source file.
70 #[derive(Clone, Copy)]
77 /// The kind of the token at this node.
78 pub fn kind(&self) -> SyntaxKind {
82 /// The text range covered by the token at this node.
83 pub fn range(&self) -> TextRange {
87 /// The text at this node.
88 pub fn text(&self) -> &'f str {
89 &self.file.text.as_str()[self.range()]
92 /// The parent node to this node.
93 pub fn parent(&self) -> Option<Node<'f>> {
94 self.as_node(self.data().parent)
97 /// The children nodes of this node.
98 pub fn children(&self) -> Children<'f> {
100 next: self.as_node(self.data().first_child),
104 /// Any errors contained in this node.
105 pub fn errors(&self) -> SyntaxErrors<'f> {
106 let pos = self.file.errors.iter().position(|e| e.node == self.idx);
107 let next = pos.map(|i| ErrorIdx(i as u32)).map(|idx| SyntaxError {
111 SyntaxErrors { next }
114 fn data(&self) -> &'f NodeData {
115 &self.file.nodes[self.idx]
118 fn as_node(&self, idx: Option<NodeIdx>) -> Option<Node<'f>> {
126 impl<'f> fmt::Debug for Node<'f> {
127 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
128 write!(fmt, "{:?}@{:?}", self.kind(), self.range())
132 impl<'f> cmp::PartialEq<Node<'f>> for Node<'f> {
133 fn eq(&self, other: &Node<'f>) -> bool {
134 self.idx == other.idx && ::std::ptr::eq(self.file, other.file)
138 impl<'f> cmp::Eq for Node<'f> {}
140 #[derive(Clone, Copy, Debug)]
141 pub struct SyntaxError<'f> {
146 impl<'f> SyntaxError<'f> {
147 pub fn message(&self) -> &'f str {
148 self.data().message.as_str()
151 pub fn after_child(&self) -> Option<Node<'f>> {
152 let idx = self.data().after_child?;
159 fn data(&self) -> &'f SyntaxErrorData {
160 &self.file.errors[self.idx]
163 fn next(&self) -> Option<SyntaxError<'f>> {
164 let next_idx = self.idx.0 + 1;
165 if !((next_idx as usize) < self.file.errors.len()) {
168 let result = SyntaxError {
170 idx: ErrorIdx(next_idx),
172 if result.data().node != self.data().node {
180 pub struct Children<'f> {
181 next: Option<Node<'f>>,
184 impl<'f> Iterator for Children<'f> {
185 type Item = Node<'f>;
187 fn next(&mut self) -> Option<Node<'f>> {
188 let next = self.next;
189 self.next = next.and_then(|node| node.as_node(node.data().next_sibling));
195 pub struct SyntaxErrors<'f> {
196 next: Option<SyntaxError<'f>>,
199 impl<'f> Iterator for SyntaxErrors<'f> {
200 type Item = SyntaxError<'f>;
202 fn next(&mut self) -> Option<SyntaxError<'f>> {
203 let next = self.next;
204 self.next = next.as_ref().and_then(SyntaxError::next);
209 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
216 parent: Option<NodeIdx>,
217 first_child: Option<NodeIdx>,
218 next_sibling: Option<NodeIdx>,
221 impl ::std::ops::Index<NodeIdx> for Vec<NodeData> {
222 type Output = NodeData;
224 fn index(&self, NodeIdx(idx): NodeIdx) -> &NodeData {
229 impl ::std::ops::IndexMut<NodeIdx> for Vec<NodeData> {
230 fn index_mut(&mut self, NodeIdx(idx): NodeIdx) -> &mut NodeData {
231 &mut self[idx as usize]
235 #[derive(Clone, Copy, Debug)]
236 struct ErrorIdx(u32);
239 struct SyntaxErrorData {
242 after_child: Option<NodeIdx>,
245 impl ::std::ops::Index<ErrorIdx> for Vec<SyntaxErrorData> {
246 type Output = SyntaxErrorData;
248 fn index(&self, ErrorIdx(idx): ErrorIdx) -> &SyntaxErrorData {