Files
rust/src/yellow/syntax.rs

142 lines
3.5 KiB
Rust
Raw Normal View History

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 {}
#[derive(Clone, Copy)]
pub struct SyntaxNode<R: TreeRoot = Arc<SyntaxRoot>> {
pub(crate) root: R,
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 23:45:10 +03:00
impl <R1: TreeRoot, R2: TreeRoot> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> {
fn eq(&self, other: &SyntaxNode<R1>) -> bool {
self.red == other.red
}
}
impl <R: TreeRoot> Eq for SyntaxNode<R> {
}
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 14:08:06 +03:00
SyntaxNode {
root,
red: red_weak,
}
2018-07-29 13:51:55 +03:00
}
}
impl<R: TreeRoot> SyntaxNode<R> {
2018-07-30 23:45:10 +03:00
pub fn as_ref<'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
}
pub fn children<'a>(&'a self) -> impl Iterator<Item=SyntaxNode<R>> + 'a {
2018-07-29 13:51:55 +03:00
let red = self.red();
let n_children = red.n_children();
(0..n_children).map(move |i| {
SyntaxNode {
2018-07-29 13:51:55 +03:00
root: self.root.clone(),
red: red.nth_child(i),
}
})
2018-07-29 13:51:55 +03:00
}
pub fn parent(&self) -> Option<SyntaxNode<R>> {
2018-07-30 12:44:14 +03:00
let parent = self.red().parent()?;
Some(SyntaxNode {
root: self.root.clone(),
red: parent,
})
}
2018-07-30 23:45:10 +03:00
pub fn first_child(&self) -> Option<SyntaxNode<R>> {
self.children().next()
}
pub fn next_sibling(&self) -> Option<SyntaxNode<R>> {
let red = self.red();
let parent = self.parent()?;
let next_sibling_idx = red.index_in_parent()? + 1;
if next_sibling_idx == red.n_children() {
return None;
}
Some(SyntaxNode {
root: self.root.clone(),
red: parent.red().nth_child(next_sibling_idx),
})
}
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
}
}
impl<R: TreeRoot> fmt::Debug for SyntaxNode<R> {
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,
}
}