Files
rust/src/yellow/syntax.rs

122 lines
2.8 KiB
Rust
Raw Normal View History

2018-07-29 13:51:55 +03:00
use std::{
fmt,
sync::Arc,
2018-07-30 12:37:03 +03:00
ptr,
ops::Deref,
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 12:37:03 +03:00
pub trait TreeRoot: Deref<Target=SyntaxRoot> + Clone {}
impl TreeRoot for Arc<SyntaxRoot> {}
impl<'a> TreeRoot for &'a SyntaxRoot {}
#[derive(Clone, Copy)]
2018-07-30 12:37:03 +03:00
pub struct SyntaxNode<ROOT: TreeRoot = 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
}
pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>;
#[derive(Debug)]
2018-07-29 13:51:55 +03:00
pub struct SyntaxRoot {
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,
}
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 12:37:03 +03:00
impl<ROOT: TreeRoot> 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
}
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(),
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,
}
}