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
|
|
|
};
|
|
|
|
|
|
2018-07-30 03:21:17 +03:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
|
pub struct SyntaxNode<ROOT: ::std::ops::Deref<Target=SyntaxRoot> + Clone = Arc<SyntaxRoot>> {
|
|
|
|
|
pub(crate) root: ROOT,
|
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
|
|
|
}
|
|
|
|
|
|
2018-07-30 03:21:17 +03:00
|
|
|
pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2018-07-29 13:51:55 +03:00
|
|
|
pub struct SyntaxRoot {
|
2018-07-30 03:21:17 +03:00
|
|
|
red: RedNode,
|
|
|
|
|
pub(crate) errors: Vec<SyntaxError>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SyntaxRoot {
|
|
|
|
|
pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxRoot {
|
|
|
|
|
SyntaxRoot {
|
|
|
|
|
red: RedNode::new_root(green),
|
|
|
|
|
errors,
|
|
|
|
|
}
|
|
|
|
|
}
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-30 03:21:17 +03:00
|
|
|
impl SyntaxNode<Arc<SyntaxRoot>> {
|
|
|
|
|
pub(crate) fn new_owned(root: SyntaxRoot) -> Self {
|
|
|
|
|
let root = Arc::new(root);
|
|
|
|
|
let red_weak = ptr::NonNull::from(&root.red);
|
2018-07-30 02:23:01 +03:00
|
|
|
SyntaxNode { root, red: red_weak }
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
2018-07-30 03:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<ROOT: ::std::ops::Deref<Target=SyntaxRoot> + Clone> SyntaxNode<ROOT> {
|
|
|
|
|
pub fn borrow<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> {
|
|
|
|
|
SyntaxNode {
|
|
|
|
|
root: &*self.root,
|
|
|
|
|
red: ptr::NonNull::clone(&self.red),
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
2018-07-30 03:21:17 +03:00
|
|
|
pub fn children(&self) -> Vec<SyntaxNode<ROOT>> {
|
2018-07-29 13:51:55 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-30 03:21:17 +03:00
|
|
|
impl<ROOT: ::std::ops::Deref<Target=SyntaxRoot> + Clone> fmt::Debug for SyntaxNode<ROOT> {
|
2018-07-29 13:51:55 +03:00
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|