Borrowed AST

This commit is contained in:
Aleksey Kladov 2018-08-17 22:00:13 +03:00
parent 70097504f7
commit d3c90ded2b
16 changed files with 350 additions and 343 deletions

View file

@ -10,7 +10,7 @@ use std::{
};
use clap::{App, Arg, SubCommand};
use tools::collect_tests;
use libeditor::{File, syntax_tree, file_structure};
use libeditor::{ParsedFile, syntax_tree, file_structure};
type Result<T> = ::std::result::Result<T, failure::Error>;
@ -68,7 +68,7 @@ fn main() -> Result<()> {
Ok(())
}
fn file() -> Result<File> {
fn file() -> Result<ParsedFile> {
let text = read_stdin()?;
Ok(libeditor::parse(&text))
}

View file

@ -26,8 +26,8 @@ use std::{
};
use libsyntax2::{
TextUnit, TextRange, RefRoot,
ast::{self, AstNode, NameOwner},
TextUnit, TextRange,
ast::{self, AstNode, NameOwner, ParsedFile},
SyntaxKind::*,
};
use libeditor::{LineIndex, FileSymbol, find_node};
@ -109,7 +109,7 @@ impl WorldState {
impl World {
pub fn file_syntax(&self, file_id: FileId) -> Result<ast::File> {
pub fn file_syntax(&self, file_id: FileId) -> Result<ParsedFile> {
let data = self.file_data(file_id)?;
Ok(data.syntax().clone())
}
@ -137,11 +137,11 @@ impl World {
offset: TextUnit,
) -> Result<Vec<(FileId, FileSymbol)>> {
let file = self.file_syntax(id)?;
let syntax = file.syntax_ref();
if let Some(name_ref) = find_node::<ast::NameRef<_>>(syntax, offset) {
let syntax = file.syntax();
if let Some(name_ref) = find_node::<ast::NameRef>(syntax, offset) {
return Ok(self.index_resolve(name_ref));
}
if let Some(name) = find_node::<ast::Name<_>>(syntax, offset) {
if let Some(name) = find_node::<ast::Name>(syntax, offset) {
if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
if module.has_semi() {
return Ok(self.resolve_module(id, module));
@ -151,7 +151,7 @@ impl World {
Ok(vec![])
}
fn index_resolve(&self, name_ref: ast::NameRef<RefRoot>) -> Vec<(FileId, FileSymbol)> {
fn index_resolve(&self, name_ref: ast::NameRef) -> Vec<(FileId, FileSymbol)> {
let name = name_ref.text();
let mut query = Query::new(name.to_string());
query.exact();
@ -159,7 +159,7 @@ impl World {
self.world_symbols(query)
}
fn resolve_module(&self, id: FileId, module: ast::Module<RefRoot>) -> Vec<(FileId, FileSymbol)> {
fn resolve_module(&self, id: FileId, module: ast::Module) -> Vec<(FileId, FileSymbol)> {
let name = match module.name() {
Some(name) => name.text(),
None => return Vec::new(),
@ -220,7 +220,7 @@ struct WorldData {
struct FileData {
text: String,
symbols: OnceCell<FileSymbols>,
syntax: OnceCell<ast::File>,
syntax: OnceCell<ParsedFile>,
lines: OnceCell<LineIndex>,
}
@ -234,14 +234,14 @@ impl FileData {
}
}
fn syntax(&self) -> &ast::File {
fn syntax(&self) -> &ParsedFile {
self.syntax
.get_or_init(|| ast::File::parse(&self.text))
.get_or_init(|| ParsedFile::parse(&self.text))
}
fn syntax_transient(&self) -> ast::File {
fn syntax_transient(&self) -> ParsedFile {
self.syntax.get().map(|s| s.clone())
.unwrap_or_else(|| ast::File::parse(&self.text))
.unwrap_or_else(|| ParsedFile::parse(&self.text))
}
fn symbols(&self) -> &FileSymbols {

View file

@ -1,6 +1,6 @@
use libeditor::{FileSymbol, file_symbols};
use libsyntax2::{
ast,
ParsedFile,
SyntaxKind::{self, *},
};
use fst::{self, IntoStreamer, Streamer};
@ -12,7 +12,7 @@ pub(crate) struct FileSymbols {
}
impl FileSymbols {
pub(crate) fn new(file: &ast::File) -> FileSymbols {
pub(crate) fn new(file: &ParsedFile) -> FileSymbols {
let mut symbols = file_symbols(file)
.into_iter()
.map(|s| (s.name.as_str().to_lowercase(), s))

View file

@ -1,8 +1,8 @@
use {TextUnit, File, EditBuilder, Edit};
use {TextUnit, EditBuilder, Edit};
use libsyntax2::{
ast::{self, AstNode, AttrsOwner},
ast::{self, AstNode, AttrsOwner, ParsedFile},
SyntaxKind::COMMA,
SyntaxNodeRef, RefRoot,
SyntaxNodeRef,
algo::{
Direction, siblings,
find_leaf_at_offset, ancestors,
@ -19,9 +19,8 @@ pub enum CursorPosition {
Offset(TextUnit),
}
pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
pub fn flip_comma<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
let syntax = file.syntax();
let syntax = syntax.as_ref();
let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?;
let left = non_trivia_sibling(comma, Direction::Backward)?;
@ -37,8 +36,8 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce()
})
}
pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
let nominal = find_node::<ast::NominalDef<_>>(file.syntax_ref(), offset)?;
pub fn add_derive<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
let nominal = find_node::<ast::NominalDef>(file.syntax(), offset)?;
Some(move || {
let derive_attr = nominal
.attrs()
@ -70,7 +69,7 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<Synta
.find(|node| !node.kind().is_trivia())
}
pub fn find_node<'a, N: AstNode<RefRoot<'a>>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> {
pub fn find_node<'a, N: AstNode<'a>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> {
let leaves = find_leaf_at_offset(syntax, offset);
let leaf = leaves.clone()
.find(|leaf| !leaf.kind().is_trivia())

View file

@ -1,11 +1,10 @@
use libsyntax2::{
ast, AstNode,
TextRange, SyntaxNodeRef,
ParsedFile, TextRange, SyntaxNodeRef,
SyntaxKind::WHITESPACE,
algo::{find_leaf_at_offset, find_covering_node, ancestors},
};
pub fn extend_selection(file: &ast::File, range: TextRange) -> Option<TextRange> {
pub fn extend_selection(file: &ParsedFile, range: TextRange) -> Option<TextRange> {
let syntax = file.syntax();
extend(syntax.as_ref(), range)
}

View file

@ -15,7 +15,7 @@ use libsyntax2::{
algo::{walk, find_leaf_at_offset},
SyntaxKind::{self, *},
};
pub use libsyntax2::{File, TextRange, TextUnit};
pub use libsyntax2::{ParsedFile, TextRange, TextUnit};
pub use self::{
line_index::{LineIndex, LineCol},
extend_selection::extend_selection,
@ -51,18 +51,18 @@ pub enum RunnableKind {
Bin,
}
pub fn parse(text: &str) -> ast::File {
ast::File::parse(text)
pub fn parse(text: &str) -> ast::ParsedFile {
ast::ParsedFile::parse(text)
}
pub fn matching_brace(file: &ast::File, offset: TextUnit) -> Option<TextUnit> {
pub fn matching_brace(file: &ast::ParsedFile, offset: TextUnit) -> Option<TextUnit> {
const BRACES: &[SyntaxKind] = &[
L_CURLY, R_CURLY,
L_BRACK, R_BRACK,
L_PAREN, R_PAREN,
L_ANGLE, R_ANGLE,
];
let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax_ref(), offset)
let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax(), offset)
.filter_map(|node| {
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
Some((node, idx))
@ -75,9 +75,9 @@ pub fn matching_brace(file: &ast::File, offset: TextUnit) -> Option<TextUnit> {
Some(matching_node.range().start())
}
pub fn highlight(file: &ast::File) -> Vec<HighlightedRange> {
pub fn highlight(file: &ast::ParsedFile) -> Vec<HighlightedRange> {
let mut res = Vec::new();
for node in walk::preorder(file.syntax_ref()) {
for node in walk::preorder(file.syntax()) {
let tag = match node.kind() {
ERROR => "error",
COMMENT | DOC_COMMENT => "comment",
@ -98,10 +98,10 @@ pub fn highlight(file: &ast::File) -> Vec<HighlightedRange> {
res
}
pub fn diagnostics(file: &ast::File) -> Vec<Diagnostic> {
pub fn diagnostics(file: &ast::ParsedFile) -> Vec<Diagnostic> {
let mut res = Vec::new();
for node in walk::preorder(file.syntax_ref()) {
for node in walk::preorder(file.syntax()) {
if node.kind() == ERROR {
res.push(Diagnostic {
range: node.range(),
@ -116,12 +116,12 @@ pub fn diagnostics(file: &ast::File) -> Vec<Diagnostic> {
res
}
pub fn syntax_tree(file: &ast::File) -> String {
::libsyntax2::utils::dump_tree(&file.syntax())
pub fn syntax_tree(file: &ast::ParsedFile) -> String {
::libsyntax2::utils::dump_tree(file.syntax())
}
pub fn runnables(file: &ast::File) -> Vec<Runnable> {
file
pub fn runnables(file: &ast::ParsedFile) -> Vec<Runnable> {
file.ast()
.functions()
.filter_map(|f| {
let name = f.name()?.text();

View file

@ -1,6 +1,6 @@
use smol_str::SmolStr;
use libsyntax2::{
SyntaxKind, SyntaxNodeRef, AstNode, RefRoot,
SyntaxKind, SyntaxNodeRef, AstNode, ParsedFile,
ast::{self, NameOwner},
algo::{
visit::{visitor, Visitor},
@ -25,14 +25,14 @@ pub struct FileSymbol {
pub kind: SyntaxKind,
}
pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> {
preorder(file.syntax_ref())
pub fn file_symbols(file: &ParsedFile) -> Vec<FileSymbol> {
preorder(file.syntax())
.filter_map(to_symbol)
.collect()
}
fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
fn decl<'a, N: NameOwner<RefRoot<'a>>>(node: N) -> Option<FileSymbol> {
fn decl<'a, N: NameOwner<'a>>(node: N) -> Option<FileSymbol> {
let name = node.name()?;
Some(FileSymbol {
name: name.text(),
@ -41,23 +41,23 @@ fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
})
}
visitor()
.visit(decl::<ast::FnDef<_>>)
.visit(decl::<ast::StructDef<_>>)
.visit(decl::<ast::EnumDef<_>>)
.visit(decl::<ast::TraitDef<_>>)
.visit(decl::<ast::Module<_>>)
.visit(decl::<ast::TypeDef<_>>)
.visit(decl::<ast::ConstDef<_>>)
.visit(decl::<ast::StaticDef<_>>)
.visit(decl::<ast::FnDef>)
.visit(decl::<ast::StructDef>)
.visit(decl::<ast::EnumDef>)
.visit(decl::<ast::TraitDef>)
.visit(decl::<ast::Module>)
.visit(decl::<ast::TypeDef>)
.visit(decl::<ast::ConstDef>)
.visit(decl::<ast::StaticDef>)
.accept(node)?
}
pub fn file_structure(file: &ast::File) -> Vec<StructureNode> {
pub fn file_structure(file: &ParsedFile) -> Vec<StructureNode> {
let mut res = Vec::new();
let mut stack = Vec::new();
for event in walk(file.syntax_ref()) {
for event in walk(file.syntax()) {
match event {
WalkEvent::Enter(node) => {
match structure_node(node) {
@ -80,7 +80,7 @@ pub fn file_structure(file: &ast::File) -> Vec<StructureNode> {
}
fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> {
fn decl<'a, N: NameOwner<RefRoot<'a>>>(node: N) -> Option<StructureNode> {
fn decl<'a, N: NameOwner<'a>>(node: N) -> Option<StructureNode> {
let name = node.name()?;
Some(StructureNode {
parent: None,
@ -92,16 +92,16 @@ fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> {
}
visitor()
.visit(decl::<ast::FnDef<_>>)
.visit(decl::<ast::StructDef<_>>)
.visit(decl::<ast::NamedField<_>>)
.visit(decl::<ast::EnumDef<_>>)
.visit(decl::<ast::TraitDef<_>>)
.visit(decl::<ast::Module<_>>)
.visit(decl::<ast::TypeDef<_>>)
.visit(decl::<ast::ConstDef<_>>)
.visit(decl::<ast::StaticDef<_>>)
.visit(|im: ast::ImplItem<_>| {
.visit(decl::<ast::FnDef>)
.visit(decl::<ast::StructDef>)
.visit(decl::<ast::NamedField>)
.visit(decl::<ast::EnumDef>)
.visit(decl::<ast::TraitDef>)
.visit(decl::<ast::Module>)
.visit(decl::<ast::TypeDef>)
.visit(decl::<ast::ConstDef>)
.visit(decl::<ast::StaticDef>)
.visit(|im: ast::ImplItem| {
let target_type = im.target_type()?;
let target_trait = im.target_trait();
let label = match target_trait {

View file

@ -5,7 +5,7 @@ extern crate assert_eq_text;
use assert_eq_text::{assert_eq_dbg};
use libeditor::{
File, TextUnit, TextRange, ActionResult, CursorPosition,
ParsedFile, TextUnit, TextRange, ActionResult, CursorPosition,
highlight, runnables, extend_selection, file_structure,
flip_comma, add_derive, matching_brace,
};
@ -146,11 +146,11 @@ fn test_matching_brace() {
);
}
fn file(text: &str) -> File {
File::parse(text)
fn file(text: &str) -> ParsedFile {
ParsedFile::parse(text)
}
fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>(
fn check_action<F: Fn(&ParsedFile, TextUnit) -> Option<ActionResult>>(
before: &str,
after: &str,
f: F,

View file

@ -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;

View file

@ -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> {}

View file

@ -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)

View file

@ -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,
}

View file

@ -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},

View file

@ -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;

View file

@ -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(),
}
})
}

View file

@ -21,7 +21,7 @@ fn lexer_tests() {
fn parser_tests() {
dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {
let file = libsyntax2::parse(text);
libsyntax2::utils::dump_tree(&file)
libsyntax2::utils::dump_tree(file.as_ref())
})
}