2018-07-30 11:08:06 +00:00
|
|
|
mod builder;
|
2018-07-29 10:51:55 +00:00
|
|
|
mod green;
|
|
|
|
mod red;
|
|
|
|
mod syntax;
|
|
|
|
|
2018-08-10 14:49:45 +00:00
|
|
|
use std::{
|
|
|
|
sync::Arc,
|
|
|
|
ptr,
|
|
|
|
};
|
|
|
|
pub use self::syntax::{SyntaxNode, SyntaxNodeRef, SyntaxError};
|
2018-07-29 10:51:55 +00:00
|
|
|
pub(crate) use self::{
|
2018-07-30 11:08:06 +00:00
|
|
|
builder::GreenBuilder,
|
2018-08-08 18:14:18 +00:00
|
|
|
green::GreenNode,
|
2018-07-29 10:51:55 +00:00
|
|
|
red::RedNode,
|
|
|
|
};
|
2018-08-10 14:49:45 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SyntaxRoot {
|
|
|
|
red: RedNode,
|
|
|
|
pub(crate) errors: Vec<SyntaxError>,
|
|
|
|
}
|
|
|
|
|
2018-08-17 18:10:55 +00:00
|
|
|
pub trait TreeRoot: Clone + Send + Sync {
|
|
|
|
fn borrowed(&self) -> RefRoot;
|
|
|
|
fn owned(&self) -> OwnedRoot;
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
fn syntax_root(&self) -> &SyntaxRoot;
|
|
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct OwnedRoot(Arc<SyntaxRoot>);
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct RefRoot<'a>(&'a OwnedRoot); // TODO: shared_from_this instead of double indirection
|
2018-08-10 14:49:45 +00:00
|
|
|
|
2018-08-17 18:10:55 +00:00
|
|
|
impl TreeRoot for OwnedRoot {
|
|
|
|
fn borrowed(&self) -> RefRoot {
|
|
|
|
RefRoot(&self)
|
|
|
|
}
|
|
|
|
fn owned(&self) -> OwnedRoot {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn syntax_root(&self) -> &SyntaxRoot {
|
|
|
|
&*self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TreeRoot for RefRoot<'a> {
|
|
|
|
fn borrowed(&self) -> RefRoot {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
fn owned(&self) -> OwnedRoot {
|
|
|
|
self.0.clone()
|
|
|
|
}
|
|
|
|
fn syntax_root(&self) -> &SyntaxRoot {
|
|
|
|
self.0.syntax_root()
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 14:49:45 +00:00
|
|
|
|
|
|
|
impl SyntaxRoot {
|
|
|
|
pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxRoot {
|
|
|
|
SyntaxRoot {
|
|
|
|
red: RedNode::new_root(green),
|
|
|
|
errors,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
|
|
pub(crate) struct RedPtr(ptr::NonNull<RedNode>);
|
|
|
|
|
|
|
|
unsafe impl Send for RedPtr {}
|
|
|
|
|
|
|
|
unsafe impl Sync for RedPtr {}
|
|
|
|
|
|
|
|
impl RedPtr {
|
|
|
|
fn new(red: &RedNode) -> RedPtr {
|
|
|
|
RedPtr(red.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get<'a>(self, _root: &'a impl TreeRoot) -> &'a RedNode {
|
|
|
|
&*self.0.as_ptr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn assert_send_sync() {
|
|
|
|
fn f<T: Send + Sync>() {}
|
|
|
|
f::<GreenNode>();
|
|
|
|
f::<RedNode>();
|
|
|
|
f::<SyntaxNode>();
|
|
|
|
}
|