switch ra_syntax to new rowan API

This commit is contained in:
Aleksey Kladov 2019-01-07 16:15:47 +03:00
parent 55272f2023
commit d91a98ec84
15 changed files with 266 additions and 327 deletions

View file

@ -1,19 +1,23 @@
pub mod visit; pub mod visit;
use crate::{SyntaxNode, SyntaxNodeRef, TextRange, TextUnit}; use rowan::TransparentNewType;
use crate::{SyntaxNode, TextRange, TextUnit};
pub use rowan::LeafAtOffset; pub use rowan::LeafAtOffset;
pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffset<SyntaxNodeRef> { pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset<&SyntaxNode> {
match node.0.leaf_at_offset(offset) { match node.0.leaf_at_offset(offset) {
LeafAtOffset::None => LeafAtOffset::None, LeafAtOffset::None => LeafAtOffset::None,
LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode(n)), LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode::from_repr(n)),
LeafAtOffset::Between(l, r) => LeafAtOffset::Between(SyntaxNode(l), SyntaxNode(r)), LeafAtOffset::Between(l, r) => {
LeafAtOffset::Between(SyntaxNode::from_repr(l), SyntaxNode::from_repr(r))
}
} }
} }
pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef { pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode {
SyntaxNode(root.0.covering_node(range)) SyntaxNode::from_repr(root.0.covering_node(range))
} }
pub fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item = T> { pub fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item = T> {

View file

@ -1,4 +1,4 @@
use crate::{AstNode, SyntaxNodeRef}; use crate::{AstNode, SyntaxNode};
use std::marker::PhantomData; use std::marker::PhantomData;
@ -15,11 +15,11 @@ pub fn visitor_ctx<'a, T, C>(ctx: C) -> impl VisitorCtx<'a, Output = T, Ctx = C>
pub trait Visitor<'a>: Sized { pub trait Visitor<'a>: Sized {
type Output; type Output;
fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>; fn accept(self, node: &'a SyntaxNode) -> Option<Self::Output>;
fn visit<N, F>(self, f: F) -> Vis<Self, N, F> fn visit<N, F>(self, f: F) -> Vis<Self, N, F>
where where
N: AstNode<'a>, N: AstNode + 'a,
F: FnOnce(N) -> Self::Output, F: FnOnce(&'a N) -> Self::Output,
{ {
Vis { Vis {
inner: self, inner: self,
@ -32,11 +32,11 @@ pub trait Visitor<'a>: Sized {
pub trait VisitorCtx<'a>: Sized { pub trait VisitorCtx<'a>: Sized {
type Output; type Output;
type Ctx; type Ctx;
fn accept(self, node: SyntaxNodeRef<'a>) -> Result<Self::Output, Self::Ctx>; fn accept(self, node: &'a SyntaxNode) -> Result<Self::Output, Self::Ctx>;
fn visit<N, F>(self, f: F) -> VisCtx<Self, N, F> fn visit<N, F>(self, f: F) -> VisCtx<Self, N, F>
where where
N: AstNode<'a>, N: AstNode + 'a,
F: FnOnce(N, Self::Ctx) -> Self::Output, F: FnOnce(&'a N, Self::Ctx) -> Self::Output,
{ {
VisCtx { VisCtx {
inner: self, inner: self,
@ -54,7 +54,7 @@ struct EmptyVisitor<T> {
impl<'a, T> Visitor<'a> for EmptyVisitor<T> { impl<'a, T> Visitor<'a> for EmptyVisitor<T> {
type Output = T; type Output = T;
fn accept(self, _node: SyntaxNodeRef<'a>) -> Option<T> { fn accept(self, _node: &'a SyntaxNode) -> Option<T> {
None None
} }
} }
@ -69,7 +69,7 @@ impl<'a, T, C> VisitorCtx<'a> for EmptyVisitorCtx<T, C> {
type Output = T; type Output = T;
type Ctx = C; type Ctx = C;
fn accept(self, _node: SyntaxNodeRef<'a>) -> Result<T, C> { fn accept(self, _node: &'a SyntaxNode) -> Result<T, C> {
Err(self.ctx) Err(self.ctx)
} }
} }
@ -84,12 +84,12 @@ pub struct Vis<V, N, F> {
impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F> impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F>
where where
V: Visitor<'a>, V: Visitor<'a>,
N: AstNode<'a>, N: AstNode + 'a,
F: FnOnce(N) -> <V as Visitor<'a>>::Output, F: FnOnce(&'a N) -> <V as Visitor<'a>>::Output,
{ {
type Output = <V as Visitor<'a>>::Output; type Output = <V as Visitor<'a>>::Output;
fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output> { fn accept(self, node: &'a SyntaxNode) -> Option<Self::Output> {
let Vis { inner, f, .. } = self; let Vis { inner, f, .. } = self;
inner.accept(node).or_else(|| N::cast(node).map(f)) inner.accept(node).or_else(|| N::cast(node).map(f))
} }
@ -105,13 +105,13 @@ pub struct VisCtx<V, N, F> {
impl<'a, V, N, F> VisitorCtx<'a> for VisCtx<V, N, F> impl<'a, V, N, F> VisitorCtx<'a> for VisCtx<V, N, F>
where where
V: VisitorCtx<'a>, V: VisitorCtx<'a>,
N: AstNode<'a>, N: AstNode + 'a,
F: FnOnce(N, <V as VisitorCtx<'a>>::Ctx) -> <V as VisitorCtx<'a>>::Output, F: FnOnce(&'a N, <V as VisitorCtx<'a>>::Ctx) -> <V as VisitorCtx<'a>>::Output,
{ {
type Output = <V as VisitorCtx<'a>>::Output; type Output = <V as VisitorCtx<'a>>::Output;
type Ctx = <V as VisitorCtx<'a>>::Ctx; type Ctx = <V as VisitorCtx<'a>>::Ctx;
fn accept(self, node: SyntaxNodeRef<'a>) -> Result<Self::Output, Self::Ctx> { fn accept(self, node: &'a SyntaxNode) -> Result<Self::Output, Self::Ctx> {
let VisCtx { inner, f, .. } = self; let VisCtx { inner, f, .. } = self;
inner.accept(node).or_else(|ctx| match N::cast(node) { inner.accept(node).or_else(|ctx| match N::cast(node) {
None => Err(ctx), None => Err(ctx),

View file

@ -1,119 +1,88 @@
mod generated; mod generated;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::string::String as RustString;
use itertools::Itertools; use itertools::Itertools;
pub use self::generated::*; pub use self::generated::*;
use crate::{ use crate::{
yellow::{RefRoot, SyntaxNodeChildren}, yellow::{SyntaxNode, SyntaxNodeChildren, TreePtr, RaTypes},
SmolStr, SmolStr,
SyntaxKind::*, SyntaxKind::*,
SyntaxNodeRef,
}; };
/// The main trait to go from untyped `SyntaxNode` to a typed ast. The /// The main trait to go from untyped `SyntaxNode` to a typed ast. The
/// conversion itself has zero runtime cost: ast and syntax nodes have exactly /// conversion itself has zero runtime cost: ast and syntax nodes have exactly
/// the same representation: a pointer to the tree root and a pointer to the /// the same representation: a pointer to the tree root and a pointer to the
/// node itself. /// node itself.
pub trait AstNode<'a>: Clone + Copy + 'a { pub trait AstNode: rowan::TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> fn cast(syntax: &SyntaxNode) -> Option<&Self>
where where
Self: Sized; Self: Sized;
fn syntax(self) -> SyntaxNodeRef<'a>; fn syntax(&self) -> &SyntaxNode;
fn to_owned(&self) -> TreePtr<Self>;
} }
pub trait NameOwner<'a>: AstNode<'a> { pub trait NameOwner: AstNode {
fn name(self) -> Option<Name<'a>> { fn name(&self) -> Option<&Name> {
child_opt(self) child_opt(self)
} }
} }
pub trait VisibilityOwner<'a>: AstNode<'a> { pub trait VisibilityOwner: AstNode {
fn visibility(self) -> Option<Visibility<'a>> { fn visibility(&self) -> Option<&Visibility> {
child_opt(self) child_opt(self)
} }
} }
pub trait LoopBodyOwner<'a>: AstNode<'a> { pub trait LoopBodyOwner: AstNode {
fn loop_body(self) -> Option<Block<'a>> { fn loop_body(&self) -> Option<&Block> {
child_opt(self) child_opt(self)
} }
} }
pub trait ArgListOwner<'a>: AstNode<'a> { pub trait ArgListOwner: AstNode {
fn arg_list(self) -> Option<ArgList<'a>> { fn arg_list(&self) -> Option<&ArgList> {
child_opt(self) child_opt(self)
} }
} }
pub trait FnDefOwner<'a>: AstNode<'a> { pub trait FnDefOwner: AstNode {
fn functions(self) -> AstChildren<'a, FnDef<'a>> { fn functions(&self) -> AstChildren<FnDef> {
children(self) children(self)
} }
} }
// ModuleItem pub trait ModuleItemOwner: AstNode {
#[derive(Debug, Clone, Copy, PartialEq, Eq)] fn items(&self) -> AstChildren<ModuleItem> {
pub enum ItemOrMacro<'a> {
Item(ModuleItem<'a>),
Macro(MacroCall<'a>),
}
impl<'a> AstNode<'a> for ItemOrMacro<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
let res = if let Some(item) = ModuleItem::cast(syntax) {
ItemOrMacro::Item(item)
} else if let Some(macro_call) = MacroCall::cast(syntax) {
ItemOrMacro::Macro(macro_call)
} else {
return None;
};
Some(res)
}
fn syntax(self) -> SyntaxNodeRef<'a> {
match self {
ItemOrMacro::Item(it) => it.syntax(),
ItemOrMacro::Macro(it) => it.syntax(),
}
}
}
pub trait ModuleItemOwner<'a>: AstNode<'a> {
fn items(self) -> AstChildren<'a, ModuleItem<'a>> {
children(self)
}
fn items_with_macros(self) -> AstChildren<'a, ItemOrMacro<'a>> {
children(self) children(self)
} }
} }
pub trait TypeParamsOwner<'a>: AstNode<'a> { pub trait TypeParamsOwner: AstNode {
fn type_param_list(self) -> Option<TypeParamList<'a>> { fn type_param_list(&self) -> Option<&TypeParamList> {
child_opt(self) child_opt(self)
} }
fn where_clause(self) -> Option<WhereClause<'a>> { fn where_clause(&self) -> Option<&WhereClause> {
child_opt(self) child_opt(self)
} }
} }
pub trait AttrsOwner<'a>: AstNode<'a> { pub trait AttrsOwner: AstNode {
fn attrs(self) -> AstChildren<'a, Attr<'a>> { fn attrs(&self) -> AstChildren<Attr> {
children(self) children(self)
} }
} }
pub trait DocCommentsOwner<'a>: AstNode<'a> { pub trait DocCommentsOwner: AstNode {
fn doc_comments(self) -> AstChildren<'a, Comment<'a>> { fn doc_comments(&self) -> AstChildren<Comment> {
children(self) children(self)
} }
/// Returns the textual content of a doc comment block as a single string. /// Returns the textual content of a doc comment block as a single string.
/// That is, strips leading `///` and joins lines /// That is, strips leading `///` and joins lines
fn doc_comment_text(self) -> RustString { fn doc_comment_text(&self) -> std::string::String {
self.doc_comments() self.doc_comments()
.filter(|comment| comment.is_doc_comment()) .filter(|comment| comment.is_doc_comment())
.map(|comment| { .map(|comment| {
@ -130,13 +99,13 @@ pub trait DocCommentsOwner<'a>: AstNode<'a> {
} }
} }
impl<'a> FnDef<'a> { impl FnDef {
pub fn has_atom_attr(&self, atom: &str) -> bool { pub fn has_atom_attr(&self, atom: &str) -> bool {
self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom) self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom)
} }
} }
impl<'a> Attr<'a> { impl Attr {
pub fn as_atom(&self) -> Option<SmolStr> { pub fn as_atom(&self) -> Option<SmolStr> {
let tt = self.value()?; let tt = self.value()?;
let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?;
@ -147,7 +116,7 @@ impl<'a> Attr<'a> {
} }
} }
pub fn as_call(&self) -> Option<(SmolStr, TokenTree<'a>)> { pub fn as_call(&self) -> Option<(SmolStr, &TokenTree)> {
let tt = self.value()?; let tt = self.value()?;
let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?;
let args = TokenTree::cast(args)?; let args = TokenTree::cast(args)?;
@ -159,37 +128,37 @@ impl<'a> Attr<'a> {
} }
} }
impl<'a> Lifetime<'a> { impl Lifetime {
pub fn text(&self) -> SmolStr { pub fn text(&self) -> SmolStr {
self.syntax().leaf_text().unwrap().clone() self.syntax().leaf_text().unwrap().clone()
} }
} }
impl<'a> Char<'a> { impl Char {
pub fn text(&self) -> &SmolStr { pub fn text(&self) -> &SmolStr {
&self.syntax().leaf_text().unwrap() &self.syntax().leaf_text().unwrap()
} }
} }
impl<'a> Byte<'a> { impl Byte {
pub fn text(&self) -> &SmolStr { pub fn text(&self) -> &SmolStr {
&self.syntax().leaf_text().unwrap() &self.syntax().leaf_text().unwrap()
} }
} }
impl<'a> ByteString<'a> { impl ByteString {
pub fn text(&self) -> &SmolStr { pub fn text(&self) -> &SmolStr {
&self.syntax().leaf_text().unwrap() &self.syntax().leaf_text().unwrap()
} }
} }
impl<'a> String<'a> { impl String {
pub fn text(&self) -> &SmolStr { pub fn text(&self) -> &SmolStr {
&self.syntax().leaf_text().unwrap() &self.syntax().leaf_text().unwrap()
} }
} }
impl<'a> Comment<'a> { impl Comment {
pub fn text(&self) -> &SmolStr { pub fn text(&self) -> &SmolStr {
self.syntax().leaf_text().unwrap() self.syntax().leaf_text().unwrap()
} }
@ -251,7 +220,7 @@ impl CommentFlavor {
} }
} }
impl<'a> Whitespace<'a> { impl Whitespace {
pub fn text(&self) -> &SmolStr { pub fn text(&self) -> &SmolStr {
&self.syntax().leaf_text().unwrap() &self.syntax().leaf_text().unwrap()
} }
@ -265,36 +234,36 @@ impl<'a> Whitespace<'a> {
} }
} }
impl<'a> Name<'a> { impl Name {
pub fn text(&self) -> SmolStr { pub fn text(&self) -> SmolStr {
let ident = self.syntax().first_child().unwrap(); let ident = self.syntax().first_child().unwrap();
ident.leaf_text().unwrap().clone() ident.leaf_text().unwrap().clone()
} }
} }
impl<'a> NameRef<'a> { impl NameRef {
pub fn text(&self) -> SmolStr { pub fn text(&self) -> SmolStr {
let ident = self.syntax().first_child().unwrap(); let ident = self.syntax().first_child().unwrap();
ident.leaf_text().unwrap().clone() ident.leaf_text().unwrap().clone()
} }
} }
impl<'a> ImplBlock<'a> { impl ImplBlock {
pub fn target_type(self) -> Option<TypeRef<'a>> { pub fn target_type(&self) -> Option<&TypeRef> {
match self.target() { match self.target() {
(Some(t), None) | (_, Some(t)) => Some(t), (Some(t), None) | (_, Some(t)) => Some(t),
_ => None, _ => None,
} }
} }
pub fn target_trait(self) -> Option<TypeRef<'a>> { pub fn target_trait(&self) -> Option<&TypeRef> {
match self.target() { match self.target() {
(Some(t), Some(_)) => Some(t), (Some(t), Some(_)) => Some(t),
_ => None, _ => None,
} }
} }
fn target(self) -> (Option<TypeRef<'a>>, Option<TypeRef<'a>>) { fn target(&self) -> (Option<&TypeRef>, Option<&TypeRef>) {
let mut types = children(self); let mut types = children(self);
let first = types.next(); let first = types.next();
let second = types.next(); let second = types.next();
@ -302,8 +271,8 @@ impl<'a> ImplBlock<'a> {
} }
} }
impl<'a> Module<'a> { impl Module {
pub fn has_semi(self) -> bool { pub fn has_semi(&self) -> bool {
match self.syntax().last_child() { match self.syntax().last_child() {
None => false, None => false,
Some(node) => node.kind() == SEMI, Some(node) => node.kind() == SEMI,
@ -311,8 +280,8 @@ impl<'a> Module<'a> {
} }
} }
impl<'a> LetStmt<'a> { impl LetStmt {
pub fn has_semi(self) -> bool { pub fn has_semi(&self) -> bool {
match self.syntax().last_child() { match self.syntax().last_child() {
None => false, None => false,
Some(node) => node.kind() == SEMI, Some(node) => node.kind() == SEMI,
@ -320,35 +289,35 @@ impl<'a> LetStmt<'a> {
} }
} }
impl<'a> IfExpr<'a> { impl IfExpr {
pub fn then_branch(self) -> Option<Block<'a>> { pub fn then_branch(&self) -> Option<&Block> {
self.blocks().nth(0) self.blocks().nth(0)
} }
pub fn else_branch(self) -> Option<Block<'a>> { pub fn else_branch(&self) -> Option<&Block> {
self.blocks().nth(1) self.blocks().nth(1)
} }
fn blocks(self) -> AstChildren<'a, Block<'a>> { fn blocks(&self) -> AstChildren<Block> {
children(self) children(self)
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PathSegmentKind<'a> { pub enum PathSegmentKind<'a> {
Name(NameRef<'a>), Name(&'a NameRef),
SelfKw, SelfKw,
SuperKw, SuperKw,
CrateKw, CrateKw,
} }
impl<'a> PathSegment<'a> { impl PathSegment {
pub fn parent_path(self) -> Path<'a> { pub fn parent_path(&self) -> &Path {
self.syntax() self.syntax()
.parent() .parent()
.and_then(Path::cast) .and_then(Path::cast)
.expect("segments are always nested in paths") .expect("segments are always nested in paths")
} }
pub fn kind(self) -> Option<PathSegmentKind<'a>> { pub fn kind(&self) -> Option<PathSegmentKind> {
let res = if let Some(name_ref) = self.name_ref() { let res = if let Some(name_ref) = self.name_ref() {
PathSegmentKind::Name(name_ref) PathSegmentKind::Name(name_ref)
} else { } else {
@ -363,20 +332,20 @@ impl<'a> PathSegment<'a> {
} }
} }
impl<'a> Path<'a> { impl Path {
pub fn parent_path(self) -> Option<Path<'a>> { pub fn parent_path(&self) -> Option<&Path> {
self.syntax().parent().and_then(Path::cast) self.syntax().parent().and_then(Path::cast)
} }
} }
impl<'a> UseTree<'a> { impl UseTree {
pub fn has_star(self) -> bool { pub fn has_star(&self) -> bool {
self.syntax().children().any(|it| it.kind() == STAR) self.syntax().children().any(|it| it.kind() == STAR)
} }
} }
impl<'a> UseTreeList<'a> { impl UseTreeList {
pub fn parent_use_tree(self) -> UseTree<'a> { pub fn parent_use_tree(&self) -> &UseTree {
self.syntax() self.syntax()
.parent() .parent()
.and_then(UseTree::cast) .and_then(UseTree::cast)
@ -384,22 +353,22 @@ impl<'a> UseTreeList<'a> {
} }
} }
fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option<C> { fn child_opt<P: AstNode, C: AstNode>(parent: &P) -> Option<&C> {
children(parent).next() children(parent).next()
} }
fn children<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> AstChildren<'a, C> { fn children<P: AstNode, C: AstNode>(parent: &P) -> AstChildren<C> {
AstChildren::new(parent.syntax()) AstChildren::new(parent.syntax())
} }
#[derive(Debug)] #[derive(Debug)]
pub struct AstChildren<'a, N> { pub struct AstChildren<'a, N> {
inner: SyntaxNodeChildren<RefRoot<'a>>, inner: SyntaxNodeChildren<'a>,
ph: PhantomData<N>, ph: PhantomData<N>,
} }
impl<'a, N> AstChildren<'a, N> { impl<'a, N> AstChildren<'a, N> {
fn new(parent: SyntaxNodeRef<'a>) -> Self { fn new(parent: &'a SyntaxNode) -> Self {
AstChildren { AstChildren {
inner: parent.children(), inner: parent.children(),
ph: PhantomData, ph: PhantomData,
@ -407,9 +376,9 @@ impl<'a, N> AstChildren<'a, N> {
} }
} }
impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> { impl<'a, N: AstNode + 'a> Iterator for AstChildren<'a, N> {
type Item = N; type Item = &'a N;
fn next(&mut self) -> Option<N> { fn next(&mut self) -> Option<&'a N> {
loop { loop {
if let Some(n) = N::cast(self.inner.next()?) { if let Some(n) = N::cast(self.inner.next()?) {
return Some(n); return Some(n);
@ -420,13 +389,13 @@ impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> {
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum StructFlavor<'a> { pub enum StructFlavor<'a> {
Tuple(PosFieldList<'a>), Tuple(&'a PosFieldList),
Named(NamedFieldDefList<'a>), Named(&'a NamedFieldDefList),
Unit, Unit,
} }
impl<'a> StructFlavor<'a> { impl StructFlavor<'_> {
fn from_node<N: AstNode<'a>>(node: N) -> StructFlavor<'a> { fn from_node<N: AstNode>(node: &N) -> StructFlavor {
if let Some(nfdl) = child_opt::<_, NamedFieldDefList>(node) { if let Some(nfdl) = child_opt::<_, NamedFieldDefList>(node) {
StructFlavor::Named(nfdl) StructFlavor::Named(nfdl)
} else if let Some(pfl) = child_opt::<_, PosFieldList>(node) { } else if let Some(pfl) = child_opt::<_, PosFieldList>(node) {
@ -437,31 +406,31 @@ impl<'a> StructFlavor<'a> {
} }
} }
impl<'a> StructDef<'a> { impl StructDef {
pub fn flavor(self) -> StructFlavor<'a> { pub fn flavor(&self) -> StructFlavor {
StructFlavor::from_node(self) StructFlavor::from_node(self)
} }
} }
impl<'a> EnumVariant<'a> { impl EnumVariant {
pub fn flavor(self) -> StructFlavor<'a> { pub fn flavor(&self) -> StructFlavor {
StructFlavor::from_node(self) StructFlavor::from_node(self)
} }
} }
impl<'a> PointerType<'a> { impl PointerType {
pub fn is_mut(&self) -> bool { pub fn is_mut(&self) -> bool {
self.syntax().children().any(|n| n.kind() == MUT_KW) self.syntax().children().any(|n| n.kind() == MUT_KW)
} }
} }
impl<'a> ReferenceType<'a> { impl ReferenceType {
pub fn is_mut(&self) -> bool { pub fn is_mut(&self) -> bool {
self.syntax().children().any(|n| n.kind() == MUT_KW) self.syntax().children().any(|n| n.kind() == MUT_KW)
} }
} }
impl<'a> RefExpr<'a> { impl RefExpr {
pub fn is_mut(&self) -> bool { pub fn is_mut(&self) -> bool {
self.syntax().children().any(|n| n.kind() == MUT_KW) self.syntax().children().any(|n| n.kind() == MUT_KW)
} }
@ -477,7 +446,7 @@ pub enum PrefixOp {
Neg, Neg,
} }
impl<'a> PrefixExpr<'a> { impl PrefixExpr {
pub fn op(&self) -> Option<PrefixOp> { pub fn op(&self) -> Option<PrefixOp> {
match self.syntax().first_child()?.kind() { match self.syntax().first_child()?.kind() {
STAR => Some(PrefixOp::Deref), STAR => Some(PrefixOp::Deref),
@ -552,7 +521,7 @@ pub enum BinOp {
BitXorAssign, BitXorAssign,
} }
impl<'a> BinExpr<'a> { impl BinExpr {
pub fn op(&self) -> Option<BinOp> { pub fn op(&self) -> Option<BinOp> {
self.syntax() self.syntax()
.children() .children()
@ -592,15 +561,15 @@ impl<'a> BinExpr<'a> {
.next() .next()
} }
pub fn lhs(self) -> Option<Expr<'a>> { pub fn lhs(&self) -> Option<&Expr> {
children(self).nth(0) children(self).nth(0)
} }
pub fn rhs(self) -> Option<Expr<'a>> { pub fn rhs(&self) -> Option<&Expr> {
children(self).nth(1) children(self).nth(1)
} }
pub fn sub_exprs(self) -> (Option<Expr<'a>>, Option<Expr<'a>>) { pub fn sub_exprs(&self) -> (Option<&Expr>, Option<&Expr>) {
let mut children = children(self); let mut children = children(self);
let first = children.next(); let first = children.next();
let second = children.next(); let second = children.next();
@ -618,7 +587,7 @@ pub enum SelfParamFlavor {
MutRef, MutRef,
} }
impl<'a> SelfParam<'a> { impl SelfParam {
pub fn flavor(&self) -> SelfParamFlavor { pub fn flavor(&self) -> SelfParamFlavor {
let borrowed = self.syntax().children().any(|n| n.kind() == AMP); let borrowed = self.syntax().children().any(|n| n.kind() == AMP);
if borrowed { if borrowed {
@ -641,7 +610,7 @@ impl<'a> SelfParam<'a> {
#[test] #[test]
fn test_doc_comment_of_items() { fn test_doc_comment_of_items() {
let file = SourceFileNode::parse( let file = SourceFile::parse(
r#" r#"
//! doc //! doc
// non-doc // non-doc

View file

@ -11,89 +11,92 @@ the below applies to the result of this template
#![cfg_attr(rustfmt, rustfmt_skip)] #![cfg_attr(rustfmt, rustfmt_skip)]
use std::hash::{Hash, Hasher}; use rowan::TransparentNewType;
use crate::{ use crate::{
ast, SyntaxNode, SyntaxKind::*,
SyntaxNode, SyntaxNodeRef, AstNode, yellow::{RaTypes, TreePtr},
yellow::{TreeRoot, RaTypes, OwnedRoot, RefRoot}, ast::{self, AstNode},
SyntaxKind::*,
}; };
{% for node, methods in ast %} {% for node, methods in ast %}
// {{ node }} // {{ node }}
{%- if methods.enum %} {%- if methods.enum %}
#[derive(Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct {{ node }} {
pub(crate) syntax: SyntaxNode,
}
unsafe impl TransparentNewType for {{ node }} {
type Repr = rowan::SyntaxNode<RaTypes>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum {{ node }}<'a> { pub enum {{ node }}Kind<'a> {
{%- for kind in methods.enum %} {%- for kind in methods.enum %}
{{ kind }}({{ kind }}<'a>), {{ kind }}(&'a {{ kind }}),
{%- endfor %} {%- endfor %}
} }
impl<'a> AstNode<'a> for {{ node }}<'a> { impl AstNode for {{ node }} {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { fn cast(syntax: &SyntaxNode) -> Option<&Self> {
match syntax.kind() { match syntax.kind() {
{%- for kind in methods.enum %} {%- for kind in methods.enum %}
{{ kind | SCREAM }} => Some({{ node }}::{{ kind }}({{ kind }} { syntax })), | {{ kind | SCREAM }}
{%- endfor %} {%- endfor %} => Some({{ node }}::from_repr(syntax.into_repr())),
_ => None, _ => None,
} }
} }
fn syntax(self) -> SyntaxNodeRef<'a> { fn syntax(&self) -> &SyntaxNode { &self.syntax }
match self { fn to_owned(&self) -> TreePtr<{{ node }}> { TreePtr::cast(self.syntax.to_owned()) }
}
impl {{ node }} {
pub fn kind(&self) -> {{ node }}Kind {
match self.syntax.kind() {
{%- for kind in methods.enum %} {%- for kind in methods.enum %}
{{ node }}::{{ kind }}(inner) => inner.syntax(), {{ kind | SCREAM }} => {{ node }}Kind::{{ kind }}({{ kind }}::cast(&self.syntax).unwrap()),
{%- endfor %} {%- endfor %}
_ => unreachable!(),
} }
} }
} }
{% else %} {% else %}
#[derive(Debug, Clone, Copy,)] #[derive(Debug, PartialEq, Eq, Hash)]
pub struct {{ node }}Node<R: TreeRoot<RaTypes> = OwnedRoot> { #[repr(transparent)]
pub(crate) syntax: SyntaxNode<R>, pub struct {{ node }} {
pub(crate) syntax: SyntaxNode,
} }
pub type {{ node }}<'a> = {{ node }}Node<RefRoot<'a>>; unsafe impl TransparentNewType for {{ node }} {
type Repr = rowan::SyntaxNode<RaTypes>;
impl<R1: TreeRoot<RaTypes>, R2: TreeRoot<RaTypes>> PartialEq<{{node}}Node<R1>> for {{node}}Node<R2> {
fn eq(&self, other: &{{node}}Node<R1>) -> bool { self.syntax == other.syntax }
}
impl<R: TreeRoot<RaTypes>> Eq for {{node}}Node<R> {}
impl<R: TreeRoot<RaTypes>> Hash for {{node}}Node<R> {
fn hash<H: Hasher>(&self, state: &mut H) { self.syntax.hash(state) }
} }
impl<'a> AstNode<'a> for {{ node }}<'a> { impl AstNode for {{ node }} {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { fn cast(syntax: &SyntaxNode) -> Option<&Self> {
match syntax.kind() { match syntax.kind() {
{{ node | SCREAM }} => Some({{ node }} { syntax }), {{ node | SCREAM }} => Some({{ node }}::from_repr(syntax.into_repr())),
_ => None, _ => None,
} }
} }
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} fn to_owned(&self) -> TreePtr<{{ node }}> { TreePtr::cast(self.syntax.to_owned()) }
impl<R: TreeRoot<RaTypes>> {{ node }}Node<R> {
pub fn borrowed(&self) -> {{ node }} {
{{ node }}Node { syntax: self.syntax.borrowed() }
}
pub fn owned(&self) -> {{ node }}Node {
{{ node }}Node { syntax: self.syntax.owned() }
}
} }
{% endif %} {% endif %}
{% if methods.traits -%} {% if methods.traits -%}
{%- for t in methods.traits -%} {%- for t in methods.traits -%}
impl<'a> ast::{{ t }}<'a> for {{ node }}<'a> {} impl ast::{{ t }} for {{ node }} {}
{% endfor -%} {% endfor -%}
{%- endif -%} {%- endif -%}
impl<'a> {{ node }}<'a> { impl {{ node }} {
{%- if methods.collections -%} {%- if methods.collections -%}
{%- for m in methods.collections -%} {%- for m in methods.collections -%}
{%- set method_name = m.0 -%} {%- set method_name = m.0 -%}
{%- set ChildName = m.1 %} {%- set ChildName = m.1 %}
pub fn {{ method_name }}(self) -> impl Iterator<Item = {{ ChildName }}<'a>> + 'a { pub fn {{ method_name }}(&self) -> impl Iterator<Item = &{{ ChildName }}> {
super::children(self) super::children(self)
} }
{% endfor -%} {% endfor -%}
@ -109,7 +112,7 @@ impl<'a> {{ node }}<'a> {
{%- set method_name = m.0 -%} {%- set method_name = m.0 -%}
{%- set ChildName = m.1 %} {%- set ChildName = m.1 %}
{%- endif %} {%- endif %}
pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> { pub fn {{ method_name }}(&self) -> Option<&{{ ChildName }}> {
super::child_opt(self) super::child_opt(self)
} }
{% endfor -%} {% endfor -%}

View file

@ -42,52 +42,42 @@ pub use crate::{
ast::AstNode, ast::AstNode,
lexer::{tokenize, Token}, lexer::{tokenize, Token},
syntax_kinds::SyntaxKind, syntax_kinds::SyntaxKind,
yellow::{ yellow::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreePtr},
Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent, Location,
},
}; };
use ra_text_edit::AtomTextEdit; use ra_text_edit::AtomTextEdit;
use crate::yellow::GreenNode; use crate::yellow::GreenNode;
/// `SourceFileNode` represents a parse tree for a single Rust file. /// `SourceFile` represents a parse tree for a single Rust file.
pub use crate::ast::{SourceFile, SourceFileNode}; pub use crate::ast::SourceFile;
impl SourceFileNode { impl SourceFile {
fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SourceFileNode { fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreePtr<SourceFile> {
let root = SyntaxNode::new(green, errors); let root = SyntaxNode::new(green, errors);
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
utils::validate_block_structure(root.borrowed()); utils::validate_block_structure(&root);
} }
assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE); assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE);
ast::SourceFileNode { syntax: root } TreePtr::cast(root)
} }
pub fn parse(text: &str) -> SourceFileNode { pub fn parse(text: &str) -> TreePtr<SourceFile> {
let tokens = tokenize(&text); let tokens = tokenize(&text);
let (green, errors) = let (green, errors) =
parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root); parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root);
SourceFileNode::new(green, errors) SourceFile::new(green, errors)
} }
pub fn reparse(&self, edit: &AtomTextEdit) -> SourceFileNode { pub fn reparse(&self, edit: &AtomTextEdit) -> TreePtr<SourceFile> {
self.incremental_reparse(edit) self.incremental_reparse(edit)
.unwrap_or_else(|| self.full_reparse(edit)) .unwrap_or_else(|| self.full_reparse(edit))
} }
pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<SourceFileNode> { pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<TreePtr<SourceFile>> {
reparsing::incremental_reparse(self.syntax(), edit, self.errors()) reparsing::incremental_reparse(self.syntax(), edit, self.errors())
.map(|(green_node, errors)| SourceFileNode::new(green_node, errors)) .map(|(green_node, errors)| SourceFile::new(green_node, errors))
} }
fn full_reparse(&self, edit: &AtomTextEdit) -> SourceFileNode { fn full_reparse(&self, edit: &AtomTextEdit) -> TreePtr<SourceFile> {
let text = let text =
text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert);
SourceFileNode::parse(&text) SourceFile::parse(&text)
}
/// Typed AST representation of the parse tree.
pub fn ast(&self) -> ast::SourceFile {
self.borrowed()
}
/// Untyped homogeneous representation of the parse tree.
pub fn syntax(&self) -> SyntaxNodeRef {
self.syntax.borrowed()
} }
pub fn errors(&self) -> Vec<SyntaxError> { pub fn errors(&self) -> Vec<SyntaxError> {
let mut errors = self.syntax.root_data().clone(); let mut errors = self.syntax.root_data().clone();

View file

@ -4,12 +4,12 @@ use crate::lexer::{tokenize, Token};
use crate::parser_api::Parser; use crate::parser_api::Parser;
use crate::parser_impl; use crate::parser_impl;
use crate::text_utils::replace_range; use crate::text_utils::replace_range;
use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNodeRef}; use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNode};
use crate::{SyntaxKind::*, TextRange, TextUnit}; use crate::{SyntaxKind::*, TextRange, TextUnit};
use ra_text_edit::AtomTextEdit; use ra_text_edit::AtomTextEdit;
pub(crate) fn incremental_reparse( pub(crate) fn incremental_reparse(
node: SyntaxNodeRef, node: &SyntaxNode,
edit: &AtomTextEdit, edit: &AtomTextEdit,
errors: Vec<SyntaxError>, errors: Vec<SyntaxError>,
) -> Option<(GreenNode, Vec<SyntaxError>)> { ) -> Option<(GreenNode, Vec<SyntaxError>)> {
@ -21,9 +21,9 @@ pub(crate) fn incremental_reparse(
} }
fn reparse_leaf<'node>( fn reparse_leaf<'node>(
node: SyntaxNodeRef<'node>, node: &'node SyntaxNode,
edit: &AtomTextEdit, edit: &AtomTextEdit,
) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> { ) -> Option<(&'node SyntaxNode, GreenNode, Vec<SyntaxError>)> {
let node = algo::find_covering_node(node, edit.delete); let node = algo::find_covering_node(node, edit.delete);
match node.kind() { match node.kind() {
WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => { WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => {
@ -47,9 +47,9 @@ fn reparse_leaf<'node>(
} }
fn reparse_block<'node>( fn reparse_block<'node>(
node: SyntaxNodeRef<'node>, node: &'node SyntaxNode,
edit: &AtomTextEdit, edit: &AtomTextEdit,
) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> { ) -> Option<(&'node SyntaxNode, GreenNode, Vec<SyntaxError>)> {
let (node, reparser) = find_reparsable_node(node, edit.delete)?; let (node, reparser) = find_reparsable_node(node, edit.delete)?;
let text = get_text_after_edit(node, &edit); let text = get_text_after_edit(node, &edit);
let tokens = tokenize(&text); let tokens = tokenize(&text);
@ -61,7 +61,7 @@ fn reparse_block<'node>(
Some((node, green, new_errors)) Some((node, green, new_errors))
} }
fn get_text_after_edit(node: SyntaxNodeRef, edit: &AtomTextEdit) -> String { fn get_text_after_edit(node: &SyntaxNode, edit: &AtomTextEdit) -> String {
replace_range( replace_range(
node.text().to_string(), node.text().to_string(),
edit.delete - node.range().start(), edit.delete - node.range().start(),
@ -77,17 +77,14 @@ fn is_contextual_kw(text: &str) -> bool {
} }
type ParseFn = fn(&mut Parser); type ParseFn = fn(&mut Parser);
fn find_reparsable_node( fn find_reparsable_node(node: &SyntaxNode, range: TextRange) -> Option<(&SyntaxNode, ParseFn)> {
node: SyntaxNodeRef<'_>,
range: TextRange,
) -> Option<(SyntaxNodeRef<'_>, ParseFn)> {
let node = algo::find_covering_node(node, range); let node = algo::find_covering_node(node, range);
return node return node
.ancestors() .ancestors()
.filter_map(|node| reparser(node).map(|r| (node, r))) .filter_map(|node| reparser(node).map(|r| (node, r)))
.next(); .next();
fn reparser(node: SyntaxNodeRef) -> Option<ParseFn> { fn reparser(node: &SyntaxNode) -> Option<ParseFn> {
let res = match node.kind() { let res = match node.kind() {
BLOCK => grammar::block, BLOCK => grammar::block,
NAMED_FIELD_DEF_LIST => grammar::named_field_def_list, NAMED_FIELD_DEF_LIST => grammar::named_field_def_list,
@ -138,7 +135,7 @@ fn is_balanced(tokens: &[Token]) -> bool {
fn merge_errors( fn merge_errors(
old_errors: Vec<SyntaxError>, old_errors: Vec<SyntaxError>,
new_errors: Vec<SyntaxError>, new_errors: Vec<SyntaxError>,
old_node: SyntaxNodeRef, old_node: &SyntaxNode,
edit: &AtomTextEdit, edit: &AtomTextEdit,
) -> Vec<SyntaxError> { ) -> Vec<SyntaxError> {
let mut res = Vec::new(); let mut res = Vec::new();
@ -159,22 +156,22 @@ fn merge_errors(
mod tests { mod tests {
use test_utils::{extract_range, assert_eq_text}; use test_utils::{extract_range, assert_eq_text};
use crate::{SourceFileNode, text_utils::replace_range, utils::dump_tree }; use crate::{SourceFile, AstNode, text_utils::replace_range, utils::dump_tree};
use super::*; use super::*;
fn do_check<F>(before: &str, replace_with: &str, reparser: F) fn do_check<F>(before: &str, replace_with: &str, reparser: F)
where where
for<'a> F: Fn( for<'a> F: Fn(
SyntaxNodeRef<'a>, &'a SyntaxNode,
&AtomTextEdit, &AtomTextEdit,
) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec<SyntaxError>)>, ) -> Option<(&'a SyntaxNode, GreenNode, Vec<SyntaxError>)>,
{ {
let (range, before) = extract_range(before); let (range, before) = extract_range(before);
let after = replace_range(before.clone(), range, replace_with); let after = replace_range(before.clone(), range, replace_with);
let fully_reparsed = SourceFileNode::parse(&after); let fully_reparsed = SourceFile::parse(&after);
let incrementally_reparsed = { let incrementally_reparsed = {
let f = SourceFileNode::parse(&before); let f = SourceFile::parse(&before);
let edit = AtomTextEdit { let edit = AtomTextEdit {
delete: range, delete: range,
insert: replace_with.to_string(), insert: replace_with.to_string(),
@ -183,7 +180,7 @@ mod tests {
reparser(f.syntax(), &edit).expect("cannot incrementally reparse"); reparser(f.syntax(), &edit).expect("cannot incrementally reparse");
let green_root = node.replace_with(green); let green_root = node.replace_with(green);
let errors = super::merge_errors(f.errors(), new_errors, node, &edit); let errors = super::merge_errors(f.errors(), new_errors, node, &edit);
SourceFileNode::new(green_root, errors) SourceFile::new(green_root, errors)
}; };
assert_eq_text!( assert_eq_text!(

View file

@ -1,11 +1,11 @@
use crate::{SourceFileNode, SyntaxKind, SyntaxNodeRef, WalkEvent, AstNode}; use std::{str, fmt::Write};
use std::fmt::Write;
use std::str; use crate::{SourceFile, SyntaxKind, WalkEvent, AstNode, SyntaxNode};
/// Parse a file and create a string representation of the resulting parse tree. /// Parse a file and create a string representation of the resulting parse tree.
pub fn dump_tree(syntax: SyntaxNodeRef) -> String { pub fn dump_tree(syntax: &SyntaxNode) -> String {
let mut errors: Vec<_> = match syntax.ancestors().find_map(SourceFileNode::cast) { let mut errors: Vec<_> = match syntax.ancestors().find_map(SourceFile::cast) {
Some(file) => file.owned().errors(), Some(file) => file.errors(),
None => syntax.root_data().to_vec(), None => syntax.root_data().to_vec(),
}; };
errors.sort_by_key(|e| e.offset()); errors.sort_by_key(|e| e.offset());
@ -48,14 +48,13 @@ pub fn dump_tree(syntax: SyntaxNodeRef) -> String {
} }
pub fn check_fuzz_invariants(text: &str) { pub fn check_fuzz_invariants(text: &str) {
let file = SourceFileNode::parse(text); let file = SourceFile::parse(text);
let root = file.syntax(); let root = file.syntax();
validate_block_structure(root); validate_block_structure(root);
let _ = file.ast();
let _ = file.errors(); let _ = file.errors();
} }
pub(crate) fn validate_block_structure(root: SyntaxNodeRef) { pub(crate) fn validate_block_structure(root: &SyntaxNode) {
let mut stack = Vec::new(); let mut stack = Vec::new();
for node in root.descendants() { for node in root.descendants() {
match node.kind() { match node.kind() {

View file

@ -1,16 +1,15 @@
use crate::{
algo::visit::{visitor_ctx, VisitorCtx},
ast,
SourceFileNode,
yellow::SyntaxError,
};
mod byte; mod byte;
mod byte_string; mod byte_string;
mod char; mod char;
mod string; mod string;
pub(crate) fn validate(file: &SourceFileNode) -> Vec<SyntaxError> { use crate::{
SourceFile, yellow::SyntaxError, AstNode,
ast,
algo::visit::{visitor_ctx, VisitorCtx},
};
pub(crate) fn validate(file: &SourceFile) -> Vec<SyntaxError> {
let mut errors = Vec::new(); let mut errors = Vec::new();
for node in file.syntax().descendants() { for node in file.syntax().descendants() {
let _ = visitor_ctx(&mut errors) let _ = visitor_ctx(&mut errors)

View file

@ -11,7 +11,7 @@ use crate::{
}, },
}; };
pub(super) fn validate_byte_node(node: ast::Byte, errors: &mut Vec<SyntaxError>) { pub(super) fn validate_byte_node(node: &ast::Byte, errors: &mut Vec<SyntaxError>) {
let literal_text = node.text(); let literal_text = node.text();
let literal_range = node.syntax().range(); let literal_range = node.syntax().range();
let mut components = string_lexing::parse_byte_literal(literal_text); let mut components = string_lexing::parse_byte_literal(literal_text);
@ -106,11 +106,11 @@ fn validate_byte_code_escape(text: &str, range: TextRange, errors: &mut Vec<Synt
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::SourceFileNode; use crate::{SourceFile, TreePtr};
fn build_file(literal: &str) -> SourceFileNode { fn build_file(literal: &str) -> TreePtr<SourceFile> {
let src = format!("const C: u8 = b'{}';", literal); let src = format!("const C: u8 = b'{}';", literal);
SourceFileNode::parse(&src) SourceFile::parse(&src)
} }
fn assert_valid_byte(literal: &str) { fn assert_valid_byte(literal: &str) {

View file

@ -9,7 +9,7 @@ use crate::{
use super::byte; use super::byte;
pub(crate) fn validate_byte_string_node(node: ast::ByteString, errors: &mut Vec<SyntaxError>) { pub(crate) fn validate_byte_string_node(node: &ast::ByteString, errors: &mut Vec<SyntaxError>) {
let literal_text = node.text(); let literal_text = node.text();
let literal_range = node.syntax().range(); let literal_range = node.syntax().range();
let mut components = string_lexing::parse_byte_string_literal(literal_text); let mut components = string_lexing::parse_byte_string_literal(literal_text);
@ -43,12 +43,12 @@ pub(crate) fn validate_byte_string_node(node: ast::ByteString, errors: &mut Vec<
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::SourceFileNode; use crate::{SourceFile, TreePtr};
fn build_file(literal: &str) -> SourceFileNode { fn build_file(literal: &str) -> TreePtr<SourceFile> {
let src = format!(r#"const S: &'static [u8] = b"{}";"#, literal); let src = format!(r#"const S: &'static [u8] = b"{}";"#, literal);
println!("Source: {}", src); println!("Source: {}", src);
SourceFileNode::parse(&src) SourceFile::parse(&src)
} }
fn assert_valid_str(literal: &str) { fn assert_valid_str(literal: &str) {

View file

@ -14,7 +14,7 @@ use crate::{
}, },
}; };
pub(super) fn validate_char_node(node: ast::Char, errors: &mut Vec<SyntaxError>) { pub(super) fn validate_char_node(node: &ast::Char, errors: &mut Vec<SyntaxError>) {
let literal_text = node.text(); let literal_text = node.text();
let literal_range = node.syntax().range(); let literal_range = node.syntax().range();
let mut components = string_lexing::parse_char_literal(literal_text); let mut components = string_lexing::parse_char_literal(literal_text);
@ -175,11 +175,11 @@ fn validate_unicode_escape(text: &str, range: TextRange, errors: &mut Vec<Syntax
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::SourceFileNode; use crate::{SourceFile, TreePtr};
fn build_file(literal: &str) -> SourceFileNode { fn build_file(literal: &str) -> TreePtr<SourceFile> {
let src = format!("const C: char = '{}';", literal); let src = format!("const C: char = '{}';", literal);
SourceFileNode::parse(&src) SourceFile::parse(&src)
} }
fn assert_valid_char(literal: &str) { fn assert_valid_char(literal: &str) {

View file

@ -9,7 +9,7 @@ use crate::{
use super::char; use super::char;
pub(crate) fn validate_string_node(node: ast::String, errors: &mut Vec<SyntaxError>) { pub(crate) fn validate_string_node(node: &ast::String, errors: &mut Vec<SyntaxError>) {
let literal_text = node.text(); let literal_text = node.text();
let literal_range = node.syntax().range(); let literal_range = node.syntax().range();
let mut components = string_lexing::parse_string_literal(literal_text); let mut components = string_lexing::parse_string_literal(literal_text);
@ -38,12 +38,12 @@ pub(crate) fn validate_string_node(node: ast::String, errors: &mut Vec<SyntaxErr
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::SourceFileNode; use crate::{SourceFile, TreePtr};
fn build_file(literal: &str) -> SourceFileNode { fn build_file(literal: &str) -> TreePtr<SourceFile> {
let src = format!(r#"const S: &'static str = "{}";"#, literal); let src = format!(r#"const S: &'static str = "{}";"#, literal);
println!("Source: {}", src); println!("Source: {}", src);
SourceFileNode::parse(&src) SourceFile::parse(&src)
} }
fn assert_valid_str(literal: &str) { fn assert_valid_str(literal: &str) {

View file

@ -4,15 +4,12 @@ mod syntax_text;
use self::syntax_text::SyntaxText; use self::syntax_text::SyntaxText;
use crate::{SmolStr, SyntaxKind, TextRange}; use crate::{SmolStr, SyntaxKind, TextRange};
use rowan::Types; use rowan::{Types, TransparentNewType};
use std::{ use std::fmt;
fmt,
hash::{Hash, Hasher},
};
pub(crate) use self::builder::GreenBuilder; pub(crate) use self::builder::GreenBuilder;
pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location}; pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location};
pub use rowan::{TreeRoot, WalkEvent}; pub use rowan::WalkEvent;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum RaTypes {} pub enum RaTypes {}
@ -21,35 +18,19 @@ impl Types for RaTypes {
type RootData = Vec<SyntaxError>; type RootData = Vec<SyntaxError>;
} }
pub type OwnedRoot = ::rowan::OwnedRoot<RaTypes>;
pub type RefRoot<'a> = ::rowan::RefRoot<'a, RaTypes>;
pub type GreenNode = ::rowan::GreenNode<RaTypes>; pub type GreenNode = ::rowan::GreenNode<RaTypes>;
pub type TreePtr<T> = ::rowan::TreePtr<RaTypes, T>;
#[derive(Clone, Copy)] #[derive(PartialEq, Eq, Hash)]
pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>(pub(crate) ::rowan::SyntaxNode<RaTypes, R>); #[repr(transparent)]
pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>; pub struct SyntaxNode(pub(crate) ::rowan::SyntaxNode<RaTypes>);
unsafe impl TransparentNewType for SyntaxNode {
impl<R1, R2> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> type Repr = ::rowan::SyntaxNode<RaTypes>;
where
R1: TreeRoot<RaTypes>,
R2: TreeRoot<RaTypes>,
{
fn eq(&self, other: &SyntaxNode<R1>) -> bool {
self.0 == other.0
}
}
impl<R: TreeRoot<RaTypes>> Eq for SyntaxNode<R> {}
impl<R: TreeRoot<RaTypes>> Hash for SyntaxNode<R> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
} }
impl SyntaxNode { impl SyntaxNode {
pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxNode { pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreePtr<SyntaxNode> {
SyntaxNode(::rowan::SyntaxNode::new(green, errors)) TreePtr::cast(::rowan::SyntaxNode::new(green, errors))
} }
} }
@ -59,45 +40,42 @@ pub enum Direction {
Prev, Prev,
} }
impl<'a> SyntaxNodeRef<'a> { impl SyntaxNode {
pub fn leaf_text(self) -> Option<&'a SmolStr> { pub fn leaf_text(&self) -> Option<&SmolStr> {
self.0.leaf_text() self.0.leaf_text()
} }
pub fn ancestors(self) -> impl Iterator<Item = SyntaxNodeRef<'a>> { pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> {
crate::algo::generate(Some(self), |&node| node.parent()) crate::algo::generate(Some(self), |&node| node.parent())
} }
pub fn descendants(self) -> impl Iterator<Item = SyntaxNodeRef<'a>> { pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> {
self.preorder().filter_map(|event| match event { self.preorder().filter_map(|event| match event {
WalkEvent::Enter(node) => Some(node), WalkEvent::Enter(node) => Some(node),
WalkEvent::Leave(_) => None, WalkEvent::Leave(_) => None,
}) })
} }
pub fn siblings(self, direction: Direction) -> impl Iterator<Item = SyntaxNodeRef<'a>> { pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> {
crate::algo::generate(Some(self), move |&node| match direction { crate::algo::generate(Some(self), move |&node| match direction {
Direction::Next => node.next_sibling(), Direction::Next => node.next_sibling(),
Direction::Prev => node.prev_sibling(), Direction::Prev => node.prev_sibling(),
}) })
} }
pub fn preorder(self) -> impl Iterator<Item = WalkEvent<SyntaxNodeRef<'a>>> { pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode>> {
self.0.preorder().map(|event| match event { self.0.preorder().map(|event| match event {
WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode(n)), WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)),
WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode(n)), WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode::from_repr(n)),
}) })
} }
} }
impl<R: TreeRoot<RaTypes>> SyntaxNode<R> { impl SyntaxNode {
pub(crate) fn root_data(&self) -> &Vec<SyntaxError> { pub(crate) fn root_data(&self) -> &Vec<SyntaxError> {
self.0.root_data() self.0.root_data()
} }
pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode {
self.0.replace_with(replacement) self.0.replace_self(replacement)
} }
pub fn borrowed<'a>(&'a self) -> SyntaxNode<RefRoot<'a>> { pub fn to_owned(&self) -> TreePtr<SyntaxNode> {
SyntaxNode(self.0.borrowed()) TreePtr::cast(self.0.to_owned())
}
pub fn owned(&self) -> SyntaxNode<OwnedRoot> {
SyntaxNode(self.0.owned())
} }
pub fn kind(&self) -> SyntaxKind { pub fn kind(&self) -> SyntaxKind {
self.0.kind() self.0.kind()
@ -106,32 +84,32 @@ impl<R: TreeRoot<RaTypes>> SyntaxNode<R> {
self.0.range() self.0.range()
} }
pub fn text(&self) -> SyntaxText { pub fn text(&self) -> SyntaxText {
SyntaxText::new(self.borrowed()) SyntaxText::new(self)
} }
pub fn is_leaf(&self) -> bool { pub fn is_leaf(&self) -> bool {
self.0.is_leaf() self.0.is_leaf()
} }
pub fn parent(&self) -> Option<SyntaxNode<R>> { pub fn parent(&self) -> Option<&SyntaxNode> {
self.0.parent().map(SyntaxNode) self.0.parent().map(SyntaxNode::from_repr)
} }
pub fn first_child(&self) -> Option<SyntaxNode<R>> { pub fn first_child(&self) -> Option<&SyntaxNode> {
self.0.first_child().map(SyntaxNode) self.0.first_child().map(SyntaxNode::from_repr)
} }
pub fn last_child(&self) -> Option<SyntaxNode<R>> { pub fn last_child(&self) -> Option<&SyntaxNode> {
self.0.last_child().map(SyntaxNode) self.0.last_child().map(SyntaxNode::from_repr)
} }
pub fn next_sibling(&self) -> Option<SyntaxNode<R>> { pub fn next_sibling(&self) -> Option<&SyntaxNode> {
self.0.next_sibling().map(SyntaxNode) self.0.next_sibling().map(SyntaxNode::from_repr)
} }
pub fn prev_sibling(&self) -> Option<SyntaxNode<R>> { pub fn prev_sibling(&self) -> Option<&SyntaxNode> {
self.0.prev_sibling().map(SyntaxNode) self.0.prev_sibling().map(SyntaxNode::from_repr)
} }
pub fn children(&self) -> SyntaxNodeChildren<R> { pub fn children(&self) -> SyntaxNodeChildren {
SyntaxNodeChildren(self.0.children()) SyntaxNodeChildren(self.0.children())
} }
} }
impl<R: TreeRoot<RaTypes>> fmt::Debug for SyntaxNode<R> { impl fmt::Debug for SyntaxNode {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; write!(fmt, "{:?}@{:?}", self.kind(), self.range())?;
if has_short_text(self.kind()) { if has_short_text(self.kind()) {
@ -142,13 +120,13 @@ impl<R: TreeRoot<RaTypes>> fmt::Debug for SyntaxNode<R> {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct SyntaxNodeChildren<R: TreeRoot<RaTypes>>(::rowan::SyntaxNodeChildren<RaTypes, R>); pub struct SyntaxNodeChildren<'a>(::rowan::SyntaxNodeChildren<'a, RaTypes>);
impl<R: TreeRoot<RaTypes>> Iterator for SyntaxNodeChildren<R> { impl<'a> Iterator for SyntaxNodeChildren<'a> {
type Item = SyntaxNode<R>; type Item = &'a SyntaxNode;
fn next(&mut self) -> Option<SyntaxNode<R>> { fn next(&mut self) -> Option<&'a SyntaxNode> {
self.0.next().map(SyntaxNode) self.0.next().map(SyntaxNode::from_repr)
} }
} }

View file

@ -3,17 +3,17 @@ use std::{fmt, ops};
use ra_text_edit::text_utils::contains_offset_nonstrict; use ra_text_edit::text_utils::contains_offset_nonstrict;
use crate::{ use crate::{
text_utils::intersect, text_utils::intersect,
SyntaxNodeRef, TextRange, TextUnit, SyntaxNode, TextRange, TextUnit,
}; };
#[derive(Clone)] #[derive(Clone)]
pub struct SyntaxText<'a> { pub struct SyntaxText<'a> {
node: SyntaxNodeRef<'a>, node: &'a SyntaxNode,
range: TextRange, range: TextRange,
} }
impl<'a> SyntaxText<'a> { impl<'a> SyntaxText<'a> {
pub(crate) fn new(node: SyntaxNodeRef<'a>) -> SyntaxText<'a> { pub(crate) fn new(node: &'a SyntaxNode) -> SyntaxText<'a> {
SyntaxText { SyntaxText {
node, node,
range: node.range(), range: node.range(),

View file

@ -9,8 +9,8 @@ use std::{
use test_utils::{project_dir, dir_tests, read_text, collect_tests}; use test_utils::{project_dir, dir_tests, read_text, collect_tests};
use ra_syntax::{ use ra_syntax::{
SourceFile, AstNode,
utils::{check_fuzz_invariants, dump_tree}, utils::{check_fuzz_invariants, dump_tree},
SourceFileNode,
}; };
#[test] #[test]
@ -27,7 +27,7 @@ fn parser_tests() {
&test_data_dir(), &test_data_dir(),
&["parser/inline/ok", "parser/ok"], &["parser/inline/ok", "parser/ok"],
|text, path| { |text, path| {
let file = SourceFileNode::parse(text); let file = SourceFile::parse(text);
let errors = file.errors(); let errors = file.errors();
assert_eq!( assert_eq!(
&*errors, &*errors,
@ -42,7 +42,7 @@ fn parser_tests() {
&test_data_dir(), &test_data_dir(),
&["parser/err", "parser/inline/err"], &["parser/err", "parser/inline/err"],
|text, path| { |text, path| {
let file = SourceFileNode::parse(text); let file = SourceFile::parse(text);
let errors = file.errors(); let errors = file.errors();
assert_ne!( assert_ne!(
&*errors, &*errors,
@ -85,7 +85,7 @@ fn self_hosting_parsing() {
{ {
count += 1; count += 1;
let text = read_text(entry.path()); let text = read_text(entry.path());
let node = SourceFileNode::parse(&text); let node = SourceFile::parse(&text);
let errors = node.errors(); let errors = node.errors();
assert_eq!( assert_eq!(
&*errors, &*errors,