mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Borrowed AST
This commit is contained in:
parent
70097504f7
commit
d3c90ded2b
16 changed files with 350 additions and 343 deletions
|
@ -1,5 +1,5 @@
|
|||
use std::marker::PhantomData;
|
||||
use {SyntaxNodeRef, AstNode, RefRoot};
|
||||
use {SyntaxNodeRef, AstNode};
|
||||
|
||||
|
||||
pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> {
|
||||
|
@ -10,7 +10,7 @@ pub trait Visitor<'a>: Sized {
|
|||
type Output;
|
||||
fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>;
|
||||
fn visit<N, F>(self, f: F) -> Vis<Self, N, F>
|
||||
where N: AstNode<RefRoot<'a>>,
|
||||
where N: AstNode<'a>,
|
||||
F: FnOnce(N) -> Self::Output,
|
||||
{
|
||||
Vis { inner: self, f, ph: PhantomData }
|
||||
|
@ -40,7 +40,7 @@ pub struct Vis<V, N, F> {
|
|||
impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F>
|
||||
where
|
||||
V: Visitor<'a>,
|
||||
N: AstNode<RefRoot<'a>>,
|
||||
N: AstNode<'a>,
|
||||
F: FnOnce(N) -> <V as Visitor<'a>>::Output,
|
||||
{
|
||||
type Output = <V as Visitor<'a>>::Output;
|
||||
|
|
|
@ -1,45 +1,45 @@
|
|||
use {
|
||||
ast,
|
||||
SyntaxNode, OwnedRoot, TreeRoot, AstNode,
|
||||
SyntaxNodeRef, AstNode,
|
||||
SyntaxKind::*,
|
||||
};
|
||||
|
||||
// ArrayType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ArrayType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct ArrayType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for ArrayType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for ArrayType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
ARRAY_TYPE => Some(ArrayType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ArrayType<R> {}
|
||||
impl<'a> ArrayType<'a> {}
|
||||
|
||||
// Attr
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Attr<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct Attr<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for Attr<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for Attr<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
ATTR => Some(Attr { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> Attr<R> {
|
||||
pub fn value(&self) -> Option<TokenTree<R>> {
|
||||
impl<'a> Attr<'a> {
|
||||
pub fn value(self) -> Option<TokenTree<'a>> {
|
||||
self.syntax()
|
||||
.children()
|
||||
.filter_map(TokenTree::cast)
|
||||
|
@ -49,80 +49,80 @@ impl<R: TreeRoot> Attr<R> {
|
|||
|
||||
// ConstDef
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ConstDef<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct ConstDef<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for ConstDef<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for ConstDef<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
CONST_DEF => Some(ConstDef { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ast::NameOwner<R> for ConstDef<R> {}
|
||||
impl<R: TreeRoot> ast::AttrsOwner<R> for ConstDef<R> {}
|
||||
impl<R: TreeRoot> ConstDef<R> {}
|
||||
impl<'a> ast::NameOwner<'a> for ConstDef<'a> {}
|
||||
impl<'a> ast::AttrsOwner<'a> for ConstDef<'a> {}
|
||||
impl<'a> ConstDef<'a> {}
|
||||
|
||||
// DynTraitType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct DynTraitType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct DynTraitType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for DynTraitType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for DynTraitType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
DYN_TRAIT_TYPE => Some(DynTraitType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> DynTraitType<R> {}
|
||||
impl<'a> DynTraitType<'a> {}
|
||||
|
||||
// EnumDef
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct EnumDef<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct EnumDef<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for EnumDef<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for EnumDef<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
ENUM_DEF => Some(EnumDef { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ast::NameOwner<R> for EnumDef<R> {}
|
||||
impl<R: TreeRoot> ast::AttrsOwner<R> for EnumDef<R> {}
|
||||
impl<R: TreeRoot> EnumDef<R> {}
|
||||
impl<'a> ast::NameOwner<'a> for EnumDef<'a> {}
|
||||
impl<'a> ast::AttrsOwner<'a> for EnumDef<'a> {}
|
||||
impl<'a> EnumDef<'a> {}
|
||||
|
||||
// File
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct File<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct File<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for File<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for File<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
FILE => Some(File { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> File<R> {
|
||||
pub fn functions<'a>(&'a self) -> impl Iterator<Item = FnDef<R>> + 'a {
|
||||
impl<'a> File<'a> {
|
||||
pub fn functions(self) -> impl Iterator<Item = FnDef<'a>> + 'a {
|
||||
self.syntax()
|
||||
.children()
|
||||
.filter_map(FnDef::cast)
|
||||
|
@ -131,206 +131,206 @@ impl<R: TreeRoot> File<R> {
|
|||
|
||||
// FnDef
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct FnDef<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct FnDef<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for FnDef<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for FnDef<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
FN_DEF => Some(FnDef { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ast::NameOwner<R> for FnDef<R> {}
|
||||
impl<R: TreeRoot> ast::AttrsOwner<R> for FnDef<R> {}
|
||||
impl<R: TreeRoot> FnDef<R> {}
|
||||
impl<'a> ast::NameOwner<'a> for FnDef<'a> {}
|
||||
impl<'a> ast::AttrsOwner<'a> for FnDef<'a> {}
|
||||
impl<'a> FnDef<'a> {}
|
||||
|
||||
// FnPointerType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct FnPointerType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct FnPointerType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for FnPointerType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for FnPointerType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
FN_POINTER_TYPE => Some(FnPointerType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> FnPointerType<R> {}
|
||||
impl<'a> FnPointerType<'a> {}
|
||||
|
||||
// ForType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ForType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct ForType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for ForType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for ForType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
FOR_TYPE => Some(ForType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ForType<R> {}
|
||||
impl<'a> ForType<'a> {}
|
||||
|
||||
// ImplItem
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ImplItem<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct ImplItem<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for ImplItem<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for ImplItem<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
IMPL_ITEM => Some(ImplItem { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ImplItem<R> {}
|
||||
impl<'a> ImplItem<'a> {}
|
||||
|
||||
// ImplTraitType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ImplTraitType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct ImplTraitType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for ImplTraitType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for ImplTraitType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
IMPL_TRAIT_TYPE => Some(ImplTraitType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ImplTraitType<R> {}
|
||||
impl<'a> ImplTraitType<'a> {}
|
||||
|
||||
// Module
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Module<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct Module<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for Module<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for Module<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
MODULE => Some(Module { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ast::NameOwner<R> for Module<R> {}
|
||||
impl<R: TreeRoot> ast::AttrsOwner<R> for Module<R> {}
|
||||
impl<R: TreeRoot> Module<R> {}
|
||||
impl<'a> ast::NameOwner<'a> for Module<'a> {}
|
||||
impl<'a> ast::AttrsOwner<'a> for Module<'a> {}
|
||||
impl<'a> Module<'a> {}
|
||||
|
||||
// Name
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Name<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct Name<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for Name<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for Name<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
NAME => Some(Name { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> Name<R> {}
|
||||
impl<'a> Name<'a> {}
|
||||
|
||||
// NameRef
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct NameRef<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct NameRef<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for NameRef<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for NameRef<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
NAME_REF => Some(NameRef { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> NameRef<R> {}
|
||||
impl<'a> NameRef<'a> {}
|
||||
|
||||
// NamedField
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct NamedField<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct NamedField<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for NamedField<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for NamedField<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
NAMED_FIELD => Some(NamedField { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ast::NameOwner<R> for NamedField<R> {}
|
||||
impl<R: TreeRoot> ast::AttrsOwner<R> for NamedField<R> {}
|
||||
impl<R: TreeRoot> NamedField<R> {}
|
||||
impl<'a> ast::NameOwner<'a> for NamedField<'a> {}
|
||||
impl<'a> ast::AttrsOwner<'a> for NamedField<'a> {}
|
||||
impl<'a> NamedField<'a> {}
|
||||
|
||||
// NeverType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct NeverType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct NeverType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for NeverType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for NeverType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
NEVER_TYPE => Some(NeverType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> NeverType<R> {}
|
||||
impl<'a> NeverType<'a> {}
|
||||
|
||||
// NominalDef
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum NominalDef<R: TreeRoot = OwnedRoot> {
|
||||
StructDef(StructDef<R>),
|
||||
EnumDef(EnumDef<R>),
|
||||
pub enum NominalDef<'a> {
|
||||
StructDef(StructDef<'a>),
|
||||
EnumDef(EnumDef<'a>),
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for NominalDef<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for NominalDef<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
STRUCT_DEF => Some(NominalDef::StructDef(StructDef { syntax })),
|
||||
ENUM_DEF => Some(NominalDef::EnumDef(EnumDef { syntax })),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> {
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> {
|
||||
match self {
|
||||
NominalDef::StructDef(inner) => inner.syntax(),
|
||||
NominalDef::EnumDef(inner) => inner.syntax(),
|
||||
|
@ -338,157 +338,157 @@ impl<R: TreeRoot> AstNode<R> for NominalDef<R> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ast::AttrsOwner<R> for NominalDef<R> {}
|
||||
impl<R: TreeRoot> NominalDef<R> {}
|
||||
impl<'a> ast::AttrsOwner<'a> for NominalDef<'a> {}
|
||||
impl<'a> NominalDef<'a> {}
|
||||
|
||||
// ParenType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ParenType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct ParenType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for ParenType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for ParenType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
PAREN_TYPE => Some(ParenType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ParenType<R> {}
|
||||
impl<'a> ParenType<'a> {}
|
||||
|
||||
// PathType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct PathType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct PathType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for PathType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for PathType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
PATH_TYPE => Some(PathType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> PathType<R> {}
|
||||
impl<'a> PathType<'a> {}
|
||||
|
||||
// PlaceholderType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct PlaceholderType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct PlaceholderType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for PlaceholderType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for PlaceholderType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
PLACEHOLDER_TYPE => Some(PlaceholderType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> PlaceholderType<R> {}
|
||||
impl<'a> PlaceholderType<'a> {}
|
||||
|
||||
// PointerType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct PointerType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct PointerType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for PointerType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for PointerType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
POINTER_TYPE => Some(PointerType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> PointerType<R> {}
|
||||
impl<'a> PointerType<'a> {}
|
||||
|
||||
// ReferenceType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ReferenceType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct ReferenceType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for ReferenceType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for ReferenceType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
REFERENCE_TYPE => Some(ReferenceType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ReferenceType<R> {}
|
||||
impl<'a> ReferenceType<'a> {}
|
||||
|
||||
// SliceType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct SliceType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct SliceType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for SliceType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for SliceType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
SLICE_TYPE => Some(SliceType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> SliceType<R> {}
|
||||
impl<'a> SliceType<'a> {}
|
||||
|
||||
// StaticDef
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct StaticDef<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct StaticDef<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for StaticDef<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for StaticDef<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
STATIC_DEF => Some(StaticDef { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ast::NameOwner<R> for StaticDef<R> {}
|
||||
impl<R: TreeRoot> ast::AttrsOwner<R> for StaticDef<R> {}
|
||||
impl<R: TreeRoot> StaticDef<R> {}
|
||||
impl<'a> ast::NameOwner<'a> for StaticDef<'a> {}
|
||||
impl<'a> ast::AttrsOwner<'a> for StaticDef<'a> {}
|
||||
impl<'a> StaticDef<'a> {}
|
||||
|
||||
// StructDef
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct StructDef<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct StructDef<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for StructDef<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for StructDef<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
STRUCT_DEF => Some(StructDef { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ast::NameOwner<R> for StructDef<R> {}
|
||||
impl<R: TreeRoot> ast::AttrsOwner<R> for StructDef<R> {}
|
||||
impl<R: TreeRoot> StructDef<R> {
|
||||
pub fn fields<'a>(&'a self) -> impl Iterator<Item = NamedField<R>> + 'a {
|
||||
impl<'a> ast::NameOwner<'a> for StructDef<'a> {}
|
||||
impl<'a> ast::AttrsOwner<'a> for StructDef<'a> {}
|
||||
impl<'a> StructDef<'a> {
|
||||
pub fn fields(self) -> impl Iterator<Item = NamedField<'a>> + 'a {
|
||||
self.syntax()
|
||||
.children()
|
||||
.filter_map(NamedField::cast)
|
||||
|
@ -497,100 +497,100 @@ impl<R: TreeRoot> StructDef<R> {
|
|||
|
||||
// TokenTree
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct TokenTree<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct TokenTree<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for TokenTree<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for TokenTree<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
TOKEN_TREE => Some(TokenTree { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> TokenTree<R> {}
|
||||
impl<'a> TokenTree<'a> {}
|
||||
|
||||
// TraitDef
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct TraitDef<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct TraitDef<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for TraitDef<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for TraitDef<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
TRAIT_DEF => Some(TraitDef { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ast::NameOwner<R> for TraitDef<R> {}
|
||||
impl<R: TreeRoot> ast::AttrsOwner<R> for TraitDef<R> {}
|
||||
impl<R: TreeRoot> TraitDef<R> {}
|
||||
impl<'a> ast::NameOwner<'a> for TraitDef<'a> {}
|
||||
impl<'a> ast::AttrsOwner<'a> for TraitDef<'a> {}
|
||||
impl<'a> TraitDef<'a> {}
|
||||
|
||||
// TupleType
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct TupleType<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct TupleType<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for TupleType<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for TupleType<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
TUPLE_TYPE => Some(TupleType { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> TupleType<R> {}
|
||||
impl<'a> TupleType<'a> {}
|
||||
|
||||
// TypeDef
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct TypeDef<R: TreeRoot = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct TypeDef<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for TypeDef<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for TypeDef<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
TYPE_DEF => Some(TypeDef { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> ast::NameOwner<R> for TypeDef<R> {}
|
||||
impl<R: TreeRoot> ast::AttrsOwner<R> for TypeDef<R> {}
|
||||
impl<R: TreeRoot> TypeDef<R> {}
|
||||
impl<'a> ast::NameOwner<'a> for TypeDef<'a> {}
|
||||
impl<'a> ast::AttrsOwner<'a> for TypeDef<'a> {}
|
||||
impl<'a> TypeDef<'a> {}
|
||||
|
||||
// TypeRef
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum TypeRef<R: TreeRoot = OwnedRoot> {
|
||||
ParenType(ParenType<R>),
|
||||
TupleType(TupleType<R>),
|
||||
NeverType(NeverType<R>),
|
||||
PathType(PathType<R>),
|
||||
PointerType(PointerType<R>),
|
||||
ArrayType(ArrayType<R>),
|
||||
SliceType(SliceType<R>),
|
||||
ReferenceType(ReferenceType<R>),
|
||||
PlaceholderType(PlaceholderType<R>),
|
||||
FnPointerType(FnPointerType<R>),
|
||||
ForType(ForType<R>),
|
||||
ImplTraitType(ImplTraitType<R>),
|
||||
DynTraitType(DynTraitType<R>),
|
||||
pub enum TypeRef<'a> {
|
||||
ParenType(ParenType<'a>),
|
||||
TupleType(TupleType<'a>),
|
||||
NeverType(NeverType<'a>),
|
||||
PathType(PathType<'a>),
|
||||
PointerType(PointerType<'a>),
|
||||
ArrayType(ArrayType<'a>),
|
||||
SliceType(SliceType<'a>),
|
||||
ReferenceType(ReferenceType<'a>),
|
||||
PlaceholderType(PlaceholderType<'a>),
|
||||
FnPointerType(FnPointerType<'a>),
|
||||
ForType(ForType<'a>),
|
||||
ImplTraitType(ImplTraitType<'a>),
|
||||
DynTraitType(DynTraitType<'a>),
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for TypeRef<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for TypeRef<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
PAREN_TYPE => Some(TypeRef::ParenType(ParenType { syntax })),
|
||||
TUPLE_TYPE => Some(TypeRef::TupleType(TupleType { syntax })),
|
||||
|
@ -608,7 +608,7 @@ impl<R: TreeRoot> AstNode<R> for TypeRef<R> {
|
|||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> {
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> {
|
||||
match self {
|
||||
TypeRef::ParenType(inner) => inner.syntax(),
|
||||
TypeRef::TupleType(inner) => inner.syntax(),
|
||||
|
@ -627,5 +627,5 @@ impl<R: TreeRoot> AstNode<R> for TypeRef<R> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> TypeRef<R> {}
|
||||
impl<'a> TypeRef<'a> {}
|
||||
|
||||
|
|
|
@ -1,21 +1,20 @@
|
|||
use std::sync::Arc;
|
||||
use {
|
||||
ast,
|
||||
SyntaxNode, SyntaxRoot, TreeRoot, AstNode,
|
||||
SyntaxNodeRef, AstNode,
|
||||
SyntaxKind::*,
|
||||
};
|
||||
{% for node, methods in ast %}
|
||||
// {{ node }}
|
||||
{%- if methods.enum %}
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum {{ node }}<R: TreeRoot = Arc<SyntaxRoot>> {
|
||||
pub enum {{ node }}<'a> {
|
||||
{%- for kind in methods.enum %}
|
||||
{{ kind }}({{ kind }}<R>),
|
||||
{{ kind }}({{ kind }}<'a>),
|
||||
{%- endfor %}
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for {{ node }}<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for {{ node }}<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
{%- for kind in methods.enum %}
|
||||
{{ kind | SCREAM }} => Some({{ node }}::{{ kind }}({{ kind }} { syntax })),
|
||||
|
@ -23,7 +22,7 @@ impl<R: TreeRoot> AstNode<R> for {{ node }}<R> {
|
|||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> {
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> {
|
||||
match self {
|
||||
{%- for kind in methods.enum %}
|
||||
{{ node }}::{{ kind }}(inner) => inner.syntax(),
|
||||
|
@ -33,32 +32,32 @@ impl<R: TreeRoot> AstNode<R> for {{ node }}<R> {
|
|||
}
|
||||
{% else %}
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct {{ node }}<R: TreeRoot = Arc<SyntaxRoot>> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub struct {{ node }}<'a> {
|
||||
syntax: SyntaxNodeRef<'a>,
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> AstNode<R> for {{ node }}<R> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
||||
impl<'a> AstNode<'a> for {{ node }}<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
{{ node | SCREAM }} => Some({{ node }} { syntax }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
||||
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
|
||||
}
|
||||
{% endif %}
|
||||
{% if methods.traits -%}
|
||||
{%- for t in methods.traits -%}
|
||||
impl<R: TreeRoot> ast::{{ t }}<R> for {{ node }}<R> {}
|
||||
impl<'a> ast::{{ t }}<'a> for {{ node }}<'a> {}
|
||||
{% endfor -%}
|
||||
{%- endif -%}
|
||||
|
||||
impl<R: TreeRoot> {{ node }}<R> {
|
||||
impl<'a> {{ node }}<'a> {
|
||||
{%- if methods.collections -%}
|
||||
{%- for m in methods.collections -%}
|
||||
{%- set method_name = m.0 -%}
|
||||
{%- set ChildName = m.1 %}
|
||||
pub fn {{ method_name }}<'a>(&'a self) -> impl Iterator<Item = {{ ChildName }}<R>> + 'a {
|
||||
pub fn {{ method_name }}(self) -> impl Iterator<Item = {{ ChildName }}<'a>> + 'a {
|
||||
self.syntax()
|
||||
.children()
|
||||
.filter_map({{ ChildName }}::cast)
|
||||
|
@ -70,7 +69,7 @@ impl<R: TreeRoot> {{ node }}<R> {
|
|||
{%- for m in methods.options -%}
|
||||
{%- set method_name = m.0 -%}
|
||||
{%- set ChildName = m.1 %}
|
||||
pub fn {{ method_name }}(&self) -> Option<{{ ChildName }}<R>> {
|
||||
pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> {
|
||||
self.syntax()
|
||||
.children()
|
||||
.filter_map({{ ChildName }}::cast)
|
||||
|
|
|
@ -4,22 +4,19 @@ use itertools::Itertools;
|
|||
use smol_str::SmolStr;
|
||||
|
||||
use {
|
||||
SyntaxNode, SyntaxNodeRef, OwnedRoot, TreeRoot, SyntaxError,
|
||||
SyntaxNode, SyntaxNodeRef, TreeRoot, SyntaxError,
|
||||
SyntaxKind::*,
|
||||
};
|
||||
pub use self::generated::*;
|
||||
|
||||
pub trait AstNode<R: TreeRoot> {
|
||||
fn cast(syntax: SyntaxNode<R>) -> Option<Self>
|
||||
pub trait AstNode<'a>: Clone + Copy {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self>
|
||||
where Self: Sized;
|
||||
fn syntax(&self) -> &SyntaxNode<R>;
|
||||
fn syntax_ref<'a>(&'a self) -> SyntaxNodeRef<'a> where R: 'a {
|
||||
self.syntax().as_ref()
|
||||
}
|
||||
fn syntax(self) -> SyntaxNodeRef<'a>;
|
||||
}
|
||||
|
||||
pub trait NameOwner<R: TreeRoot>: AstNode<R> {
|
||||
fn name(&self) -> Option<Name<R>> {
|
||||
pub trait NameOwner<'a>: AstNode<'a> {
|
||||
fn name(self) -> Option<Name<'a>> {
|
||||
self.syntax()
|
||||
.children()
|
||||
.filter_map(Name::cast)
|
||||
|
@ -27,27 +24,37 @@ pub trait NameOwner<R: TreeRoot>: AstNode<R> {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait AttrsOwner<R: TreeRoot>: AstNode<R> {
|
||||
fn attrs<'a>(&'a self) -> Box<Iterator<Item=Attr<R>> + 'a> where R: 'a {
|
||||
pub trait AttrsOwner<'a>: AstNode<'a> {
|
||||
fn attrs(&self) -> Box<Iterator<Item=Attr<'a>> + 'a> {
|
||||
let it = self.syntax().children()
|
||||
.filter_map(Attr::cast);
|
||||
Box::new(it)
|
||||
}
|
||||
}
|
||||
|
||||
impl File<OwnedRoot> {
|
||||
pub fn parse(text: &str) -> Self {
|
||||
File::cast(::parse(text)).unwrap()
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ParsedFile {
|
||||
root: SyntaxNode
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> File<R> {
|
||||
impl ParsedFile {
|
||||
pub fn parse(text: &str) -> Self {
|
||||
let root = ::parse(text);
|
||||
ParsedFile { root }
|
||||
}
|
||||
pub fn ast(&self) -> File {
|
||||
File::cast(self.syntax()).unwrap()
|
||||
}
|
||||
pub fn syntax(&self) -> SyntaxNodeRef {
|
||||
self.root.as_ref()
|
||||
}
|
||||
pub fn errors(&self) -> Vec<SyntaxError> {
|
||||
self.syntax().root.syntax_root().errors.clone()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> FnDef<R> {
|
||||
impl<'a> FnDef<'a> {
|
||||
pub fn has_atom_attr(&self, atom: &str) -> bool {
|
||||
self.attrs()
|
||||
.filter_map(|x| x.as_atom())
|
||||
|
@ -55,7 +62,7 @@ impl<R: TreeRoot> FnDef<R> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> Attr<R> {
|
||||
impl<'a> Attr<'a> {
|
||||
pub fn as_atom(&self) -> Option<SmolStr> {
|
||||
let tt = self.value()?;
|
||||
let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?;
|
||||
|
@ -66,7 +73,7 @@ impl<R: TreeRoot> Attr<R> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn as_call(&self) -> Option<(SmolStr, TokenTree<R>)> {
|
||||
pub fn as_call(&self) -> Option<(SmolStr, TokenTree<'a>)> {
|
||||
let tt = self.value()?;
|
||||
let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?;
|
||||
let args = TokenTree::cast(args)?;
|
||||
|
@ -78,7 +85,7 @@ impl<R: TreeRoot> Attr<R> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> Name<R> {
|
||||
impl<'a> Name<'a> {
|
||||
pub fn text(&self) -> SmolStr {
|
||||
let ident = self.syntax().first_child()
|
||||
.unwrap();
|
||||
|
@ -86,7 +93,7 @@ impl<R: TreeRoot> Name<R> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<R: TreeRoot> NameRef<R> {
|
||||
impl<'a> NameRef<'a> {
|
||||
pub fn text(&self) -> SmolStr {
|
||||
let ident = self.syntax().first_child()
|
||||
.unwrap();
|
||||
|
@ -94,22 +101,22 @@ impl<R: TreeRoot> NameRef<R> {
|
|||
}
|
||||
}
|
||||
|
||||
impl <R: TreeRoot> ImplItem<R> {
|
||||
pub fn target_type(&self) -> Option<TypeRef<R>> {
|
||||
impl<'a> ImplItem<'a> {
|
||||
pub fn target_type(&self) -> Option<TypeRef<'a>> {
|
||||
match self.target() {
|
||||
(Some(t), None) | (_, Some(t)) => Some(t),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn target_trait(&self) -> Option<TypeRef<R>> {
|
||||
pub fn target_trait(&self) -> Option<TypeRef<'a>> {
|
||||
match self.target() {
|
||||
(Some(t), Some(_)) => Some(t),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn target(&self) -> (Option<TypeRef<R>>, Option<TypeRef<R>>) {
|
||||
fn target(&self) -> (Option<TypeRef<'a>>, Option<TypeRef<'a>>) {
|
||||
let mut types = self.syntax().children().filter_map(TypeRef::cast);
|
||||
let first = types.next();
|
||||
let second = types.next();
|
||||
|
@ -117,9 +124,9 @@ impl <R: TreeRoot> ImplItem<R> {
|
|||
}
|
||||
}
|
||||
|
||||
impl <R: TreeRoot> Module<R> {
|
||||
impl<'a> Module<'a> {
|
||||
pub fn has_semi(&self) -> bool {
|
||||
match self.syntax_ref().last_child() {
|
||||
match self.syntax().last_child() {
|
||||
None => false,
|
||||
Some(node) => node.kind() == SEMI,
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ mod yellow;
|
|||
pub mod utils;
|
||||
|
||||
pub use {
|
||||
ast::{AstNode, File},
|
||||
ast::{AstNode, ParsedFile},
|
||||
lexer::{tokenize, Token},
|
||||
syntax_kinds::SyntaxKind,
|
||||
text_unit::{TextRange, TextUnit},
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
use std::fmt::Write;
|
||||
use {
|
||||
algo::walk::{walk, WalkEvent},
|
||||
SyntaxNode, TreeRoot,
|
||||
SyntaxNodeRef, TreeRoot,
|
||||
};
|
||||
|
||||
/// Parse a file and create a string representation of the resulting parse tree.
|
||||
pub fn dump_tree(syntax: &SyntaxNode) -> String {
|
||||
let syntax = syntax.as_ref();
|
||||
pub fn dump_tree(syntax: SyntaxNodeRef) -> String {
|
||||
let mut errors: Vec<_> = syntax.root.syntax_root().errors.iter().cloned().collect();
|
||||
errors.sort_by_key(|e| e.offset);
|
||||
let mut err_pos = 0;
|
||||
|
|
|
@ -71,12 +71,16 @@ impl<R: TreeRoot> SyntaxNode<R> {
|
|||
self.red().green().text()
|
||||
}
|
||||
|
||||
pub fn children<'a>(&'a self) -> impl Iterator<Item = SyntaxNode<R>> + 'a {
|
||||
let red = self.red();
|
||||
let n_children = red.n_children();
|
||||
(0..n_children).map(move |i| SyntaxNode {
|
||||
root: self.root.clone(),
|
||||
red: red.get_child(i).unwrap(),
|
||||
pub fn children(&self) -> impl Iterator<Item = SyntaxNode<R>> {
|
||||
let red = self.red;
|
||||
let n_children = self.red().n_children();
|
||||
let root = self.root.clone();
|
||||
(0..n_children).map(move |i| {
|
||||
let red = unsafe { red.get(&root) };
|
||||
SyntaxNode {
|
||||
root: root.clone(),
|
||||
red: red.get_child(i).unwrap(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue