2018-07-30 14:08:06 +03:00
|
|
|
use std::{fmt, ops::Deref, ptr, sync::Arc};
|
2018-07-29 13:51:55 +03:00
|
|
|
|
|
|
|
|
use {
|
2018-07-30 14:08:06 +03:00
|
|
|
yellow::{GreenNode, RedNode},
|
2018-07-29 15:16:07 +03:00
|
|
|
SyntaxKind::{self, *},
|
2018-07-30 14:08:06 +03:00
|
|
|
TextRange, TextUnit,
|
2018-07-29 13:51:55 +03:00
|
|
|
};
|
|
|
|
|
|
2018-07-30 14:08:06 +03:00
|
|
|
pub trait TreeRoot: Deref<Target = SyntaxRoot> + Clone {}
|
2018-07-30 12:37:03 +03:00
|
|
|
impl TreeRoot for Arc<SyntaxRoot> {}
|
|
|
|
|
impl<'a> TreeRoot for &'a SyntaxRoot {}
|
|
|
|
|
|
2018-07-30 03:21:17 +03:00
|
|
|
#[derive(Clone, Copy)]
|
2018-07-30 12:37:03 +03:00
|
|
|
pub struct SyntaxNode<ROOT: TreeRoot = Arc<SyntaxRoot>> {
|
2018-07-30 03:21:17 +03:00
|
|
|
pub(crate) root: ROOT,
|
2018-07-30 12:52:59 +03:00
|
|
|
// Guaranteed to not dangle, because `root` holds a
|
|
|
|
|
// strong reference to red's ancestor
|
2018-07-30 02:23:01 +03:00
|
|
|
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 14:08:06 +03:00
|
|
|
SyntaxNode {
|
|
|
|
|
root,
|
|
|
|
|
red: red_weak,
|
|
|
|
|
}
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
2018-07-30 03:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
2018-07-30 12:37:03 +03:00
|
|
|
impl<ROOT: TreeRoot> SyntaxNode<ROOT> {
|
2018-07-30 03:21:17 +03:00
|
|
|
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-30 14:08:06 +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
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-30 12:44:14 +03:00
|
|
|
pub fn parent(&self) -> Option<SyntaxNode<ROOT>> {
|
|
|
|
|
let parent = self.red().parent()?;
|
|
|
|
|
Some(SyntaxNode {
|
|
|
|
|
root: self.root.clone(),
|
|
|
|
|
red: parent,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-29 13:51:55 +03:00
|
|
|
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 12:37:03 +03:00
|
|
|
impl<ROOT: TreeRoot> 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,
|
|
|
|
|
}
|
|
|
|
|
}
|