2018-07-29 13:51:55 +03:00
|
|
|
use std::{
|
|
|
|
|
fmt,
|
|
|
|
|
sync::Arc,
|
2018-07-30 02:23:01 +03:00
|
|
|
ptr
|
2018-07-29 13:51:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use {
|
2018-07-29 15:16:07 +03:00
|
|
|
TextRange, TextUnit,
|
|
|
|
|
SyntaxKind::{self, *},
|
2018-07-30 02:23:01 +03:00
|
|
|
yellow::{RedNode, GreenNode},
|
2018-07-29 13:51:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct SyntaxNode {
|
|
|
|
|
pub(crate) root: SyntaxRoot,
|
2018-07-30 02:23:01 +03:00
|
|
|
// guaranteed to be alive bc SyntaxRoot holds a strong ref
|
|
|
|
|
red: ptr::NonNull<RedNode>,
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct SyntaxRoot {
|
|
|
|
|
red: Arc<RedNode>,
|
2018-07-29 15:16:07 +03:00
|
|
|
pub(crate) errors: Arc<Vec<SyntaxError>>,
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
2018-07-29 15:16:07 +03:00
|
|
|
pub(crate) struct SyntaxError {
|
2018-07-29 13:51:55 +03:00
|
|
|
pub(crate) message: String,
|
|
|
|
|
pub(crate) offset: TextUnit,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SyntaxNode {
|
2018-07-29 16:19:16 +03:00
|
|
|
pub(crate) fn new(root: GreenNode, errors: Vec<SyntaxError>) -> SyntaxNode {
|
2018-07-30 02:23:01 +03:00
|
|
|
let red = Arc::new(RedNode::new_root(root));
|
|
|
|
|
let red_weak: ptr::NonNull<RedNode> = (&*red).into();
|
|
|
|
|
let root = SyntaxRoot { red, errors: Arc::new(errors) };
|
|
|
|
|
SyntaxNode { root, red: red_weak }
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn kind(&self) -> SyntaxKind {
|
2018-07-29 16:19:16 +03:00
|
|
|
self.red().green().kind()
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn range(&self) -> TextRange {
|
|
|
|
|
let red = self.red();
|
2018-07-29 16:19:16 +03:00
|
|
|
TextRange::offset_len(
|
|
|
|
|
red.start_offset(),
|
|
|
|
|
red.green().text_len(),
|
|
|
|
|
)
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn text(&self) -> String {
|
2018-07-29 16:19:16 +03:00
|
|
|
self.red().green().text()
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn children(&self) -> Vec<SyntaxNode> {
|
|
|
|
|
let red = self.red();
|
|
|
|
|
let n_children = red.n_children();
|
2018-07-29 16:19:16 +03:00
|
|
|
let mut res = Vec::with_capacity(n_children);
|
2018-07-29 13:51:55 +03:00
|
|
|
for i in 0..n_children {
|
|
|
|
|
res.push(SyntaxNode {
|
|
|
|
|
root: self.root.clone(),
|
2018-07-30 02:39:26 +03:00
|
|
|
red: red.nth_child(i),
|
2018-07-29 13:51:55 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn red(&self) -> &RedNode {
|
2018-07-30 02:23:01 +03:00
|
|
|
unsafe { self.red.as_ref() }
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for SyntaxNode {
|
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
write!(fmt, "{:?}@{:?}", self.kind(), self.range())?;
|
|
|
|
|
if has_short_text(self.kind()) {
|
|
|
|
|
write!(fmt, " \"{}\"", self.text())?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn has_short_text(kind: SyntaxKind) -> bool {
|
|
|
|
|
match kind {
|
|
|
|
|
IDENT | LIFETIME => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|