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

View file

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

View file

@ -1,6 +1,6 @@
use libeditor::{FileSymbol, file_symbols}; use libeditor::{FileSymbol, file_symbols};
use libsyntax2::{ use libsyntax2::{
ast, ParsedFile,
SyntaxKind::{self, *}, SyntaxKind::{self, *},
}; };
use fst::{self, IntoStreamer, Streamer}; use fst::{self, IntoStreamer, Streamer};
@ -12,7 +12,7 @@ pub(crate) struct FileSymbols {
} }
impl FileSymbols { impl FileSymbols {
pub(crate) fn new(file: &ast::File) -> FileSymbols { pub(crate) fn new(file: &ParsedFile) -> FileSymbols {
let mut symbols = file_symbols(file) let mut symbols = file_symbols(file)
.into_iter() .into_iter()
.map(|s| (s.name.as_str().to_lowercase(), s)) .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::{ use libsyntax2::{
ast::{self, AstNode, AttrsOwner}, ast::{self, AstNode, AttrsOwner, ParsedFile},
SyntaxKind::COMMA, SyntaxKind::COMMA,
SyntaxNodeRef, RefRoot, SyntaxNodeRef,
algo::{ algo::{
Direction, siblings, Direction, siblings,
find_leaf_at_offset, ancestors, find_leaf_at_offset, ancestors,
@ -19,9 +19,8 @@ pub enum CursorPosition {
Offset(TextUnit), 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 = file.syntax();
let syntax = syntax.as_ref();
let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?; let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?;
let left = non_trivia_sibling(comma, Direction::Backward)?; 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> { pub fn add_derive<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
let nominal = find_node::<ast::NominalDef<_>>(file.syntax_ref(), offset)?; let nominal = find_node::<ast::NominalDef>(file.syntax(), offset)?;
Some(move || { Some(move || {
let derive_attr = nominal let derive_attr = nominal
.attrs() .attrs()
@ -70,7 +69,7 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<Synta
.find(|node| !node.kind().is_trivia()) .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 leaves = find_leaf_at_offset(syntax, offset);
let leaf = leaves.clone() let leaf = leaves.clone()
.find(|leaf| !leaf.kind().is_trivia()) .find(|leaf| !leaf.kind().is_trivia())

View file

@ -1,11 +1,10 @@
use libsyntax2::{ use libsyntax2::{
ast, AstNode, ParsedFile, TextRange, SyntaxNodeRef,
TextRange, SyntaxNodeRef,
SyntaxKind::WHITESPACE, SyntaxKind::WHITESPACE,
algo::{find_leaf_at_offset, find_covering_node, ancestors}, 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(); let syntax = file.syntax();
extend(syntax.as_ref(), range) extend(syntax.as_ref(), range)
} }

View file

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

View file

@ -1,6 +1,6 @@
use smol_str::SmolStr; use smol_str::SmolStr;
use libsyntax2::{ use libsyntax2::{
SyntaxKind, SyntaxNodeRef, AstNode, RefRoot, SyntaxKind, SyntaxNodeRef, AstNode, ParsedFile,
ast::{self, NameOwner}, ast::{self, NameOwner},
algo::{ algo::{
visit::{visitor, Visitor}, visit::{visitor, Visitor},
@ -25,14 +25,14 @@ pub struct FileSymbol {
pub kind: SyntaxKind, pub kind: SyntaxKind,
} }
pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> { pub fn file_symbols(file: &ParsedFile) -> Vec<FileSymbol> {
preorder(file.syntax_ref()) preorder(file.syntax())
.filter_map(to_symbol) .filter_map(to_symbol)
.collect() .collect()
} }
fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> { 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()?; let name = node.name()?;
Some(FileSymbol { Some(FileSymbol {
name: name.text(), name: name.text(),
@ -41,23 +41,23 @@ fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
}) })
} }
visitor() visitor()
.visit(decl::<ast::FnDef<_>>) .visit(decl::<ast::FnDef>)
.visit(decl::<ast::StructDef<_>>) .visit(decl::<ast::StructDef>)
.visit(decl::<ast::EnumDef<_>>) .visit(decl::<ast::EnumDef>)
.visit(decl::<ast::TraitDef<_>>) .visit(decl::<ast::TraitDef>)
.visit(decl::<ast::Module<_>>) .visit(decl::<ast::Module>)
.visit(decl::<ast::TypeDef<_>>) .visit(decl::<ast::TypeDef>)
.visit(decl::<ast::ConstDef<_>>) .visit(decl::<ast::ConstDef>)
.visit(decl::<ast::StaticDef<_>>) .visit(decl::<ast::StaticDef>)
.accept(node)? .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 res = Vec::new();
let mut stack = Vec::new(); let mut stack = Vec::new();
for event in walk(file.syntax_ref()) { for event in walk(file.syntax()) {
match event { match event {
WalkEvent::Enter(node) => { WalkEvent::Enter(node) => {
match structure_node(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 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()?; let name = node.name()?;
Some(StructureNode { Some(StructureNode {
parent: None, parent: None,
@ -92,16 +92,16 @@ fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> {
} }
visitor() visitor()
.visit(decl::<ast::FnDef<_>>) .visit(decl::<ast::FnDef>)
.visit(decl::<ast::StructDef<_>>) .visit(decl::<ast::StructDef>)
.visit(decl::<ast::NamedField<_>>) .visit(decl::<ast::NamedField>)
.visit(decl::<ast::EnumDef<_>>) .visit(decl::<ast::EnumDef>)
.visit(decl::<ast::TraitDef<_>>) .visit(decl::<ast::TraitDef>)
.visit(decl::<ast::Module<_>>) .visit(decl::<ast::Module>)
.visit(decl::<ast::TypeDef<_>>) .visit(decl::<ast::TypeDef>)
.visit(decl::<ast::ConstDef<_>>) .visit(decl::<ast::ConstDef>)
.visit(decl::<ast::StaticDef<_>>) .visit(decl::<ast::StaticDef>)
.visit(|im: ast::ImplItem<_>| { .visit(|im: ast::ImplItem| {
let target_type = im.target_type()?; let target_type = im.target_type()?;
let target_trait = im.target_trait(); let target_trait = im.target_trait();
let label = match 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 assert_eq_text::{assert_eq_dbg};
use libeditor::{ use libeditor::{
File, TextUnit, TextRange, ActionResult, CursorPosition, ParsedFile, TextUnit, TextRange, ActionResult, CursorPosition,
highlight, runnables, extend_selection, file_structure, highlight, runnables, extend_selection, file_structure,
flip_comma, add_derive, matching_brace, flip_comma, add_derive, matching_brace,
}; };
@ -146,11 +146,11 @@ fn test_matching_brace() {
); );
} }
fn file(text: &str) -> File { fn file(text: &str) -> ParsedFile {
File::parse(text) ParsedFile::parse(text)
} }
fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>>( fn check_action<F: Fn(&ParsedFile, TextUnit) -> Option<ActionResult>>(
before: &str, before: &str,
after: &str, after: &str,
f: F, f: F,

View file

@ -1,5 +1,5 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use {SyntaxNodeRef, AstNode, RefRoot}; use {SyntaxNodeRef, AstNode};
pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> { pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> {
@ -10,7 +10,7 @@ pub trait Visitor<'a>: Sized {
type Output; type Output;
fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>; fn accept(self, node: SyntaxNodeRef<'a>) -> 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 N: AstNode<RefRoot<'a>>, where N: AstNode<'a>,
F: FnOnce(N) -> Self::Output, F: FnOnce(N) -> Self::Output,
{ {
Vis { inner: self, f, ph: PhantomData } 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> impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F>
where where
V: Visitor<'a>, V: Visitor<'a>,
N: AstNode<RefRoot<'a>>, N: AstNode<'a>,
F: FnOnce(N) -> <V as Visitor<'a>>::Output, F: FnOnce(N) -> <V as Visitor<'a>>::Output,
{ {
type Output = <V as Visitor<'a>>::Output; type Output = <V as Visitor<'a>>::Output;

View file

@ -1,45 +1,45 @@
use { use {
ast, ast,
SyntaxNode, OwnedRoot, TreeRoot, AstNode, SyntaxNodeRef, AstNode,
SyntaxKind::*, SyntaxKind::*,
}; };
// ArrayType // ArrayType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ArrayType<R: TreeRoot = OwnedRoot> { pub struct ArrayType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for ArrayType<R> { impl<'a> AstNode<'a> for ArrayType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
ARRAY_TYPE => Some(ArrayType { syntax }), ARRAY_TYPE => Some(ArrayType { syntax }),
_ => None, _ => 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 // Attr
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Attr<R: TreeRoot = OwnedRoot> { pub struct Attr<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for Attr<R> { impl<'a> AstNode<'a> for Attr<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
ATTR => Some(Attr { syntax }), ATTR => Some(Attr { syntax }),
_ => None, _ => None,
} }
} }
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<R: TreeRoot> Attr<R> { impl<'a> Attr<'a> {
pub fn value(&self) -> Option<TokenTree<R>> { pub fn value(self) -> Option<TokenTree<'a>> {
self.syntax() self.syntax()
.children() .children()
.filter_map(TokenTree::cast) .filter_map(TokenTree::cast)
@ -49,80 +49,80 @@ impl<R: TreeRoot> Attr<R> {
// ConstDef // ConstDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ConstDef<R: TreeRoot = OwnedRoot> { pub struct ConstDef<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for ConstDef<R> { impl<'a> AstNode<'a> for ConstDef<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
CONST_DEF => Some(ConstDef { syntax }), CONST_DEF => Some(ConstDef { syntax }),
_ => None, _ => 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<'a> ast::NameOwner<'a> for ConstDef<'a> {}
impl<R: TreeRoot> ast::AttrsOwner<R> for ConstDef<R> {} impl<'a> ast::AttrsOwner<'a> for ConstDef<'a> {}
impl<R: TreeRoot> ConstDef<R> {} impl<'a> ConstDef<'a> {}
// DynTraitType // DynTraitType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct DynTraitType<R: TreeRoot = OwnedRoot> { pub struct DynTraitType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for DynTraitType<R> { impl<'a> AstNode<'a> for DynTraitType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
DYN_TRAIT_TYPE => Some(DynTraitType { syntax }), DYN_TRAIT_TYPE => Some(DynTraitType { syntax }),
_ => None, _ => 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 // EnumDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct EnumDef<R: TreeRoot = OwnedRoot> { pub struct EnumDef<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for EnumDef<R> { impl<'a> AstNode<'a> for EnumDef<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
ENUM_DEF => Some(EnumDef { syntax }), ENUM_DEF => Some(EnumDef { syntax }),
_ => None, _ => 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<'a> ast::NameOwner<'a> for EnumDef<'a> {}
impl<R: TreeRoot> ast::AttrsOwner<R> for EnumDef<R> {} impl<'a> ast::AttrsOwner<'a> for EnumDef<'a> {}
impl<R: TreeRoot> EnumDef<R> {} impl<'a> EnumDef<'a> {}
// File // File
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct File<R: TreeRoot = OwnedRoot> { pub struct File<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for File<R> { impl<'a> AstNode<'a> for File<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
FILE => Some(File { syntax }), FILE => Some(File { syntax }),
_ => None, _ => None,
} }
} }
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
impl<R: TreeRoot> File<R> { impl<'a> File<'a> {
pub fn functions<'a>(&'a self) -> impl Iterator<Item = FnDef<R>> + 'a { pub fn functions(self) -> impl Iterator<Item = FnDef<'a>> + 'a {
self.syntax() self.syntax()
.children() .children()
.filter_map(FnDef::cast) .filter_map(FnDef::cast)
@ -131,206 +131,206 @@ impl<R: TreeRoot> File<R> {
// FnDef // FnDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct FnDef<R: TreeRoot = OwnedRoot> { pub struct FnDef<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for FnDef<R> { impl<'a> AstNode<'a> for FnDef<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
FN_DEF => Some(FnDef { syntax }), FN_DEF => Some(FnDef { syntax }),
_ => None, _ => 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<'a> ast::NameOwner<'a> for FnDef<'a> {}
impl<R: TreeRoot> ast::AttrsOwner<R> for FnDef<R> {} impl<'a> ast::AttrsOwner<'a> for FnDef<'a> {}
impl<R: TreeRoot> FnDef<R> {} impl<'a> FnDef<'a> {}
// FnPointerType // FnPointerType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct FnPointerType<R: TreeRoot = OwnedRoot> { pub struct FnPointerType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for FnPointerType<R> { impl<'a> AstNode<'a> for FnPointerType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
FN_POINTER_TYPE => Some(FnPointerType { syntax }), FN_POINTER_TYPE => Some(FnPointerType { syntax }),
_ => None, _ => 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 // ForType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ForType<R: TreeRoot = OwnedRoot> { pub struct ForType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for ForType<R> { impl<'a> AstNode<'a> for ForType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
FOR_TYPE => Some(ForType { syntax }), FOR_TYPE => Some(ForType { syntax }),
_ => None, _ => 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 // ImplItem
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ImplItem<R: TreeRoot = OwnedRoot> { pub struct ImplItem<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for ImplItem<R> { impl<'a> AstNode<'a> for ImplItem<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
IMPL_ITEM => Some(ImplItem { syntax }), IMPL_ITEM => Some(ImplItem { syntax }),
_ => None, _ => 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 // ImplTraitType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ImplTraitType<R: TreeRoot = OwnedRoot> { pub struct ImplTraitType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for ImplTraitType<R> { impl<'a> AstNode<'a> for ImplTraitType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
IMPL_TRAIT_TYPE => Some(ImplTraitType { syntax }), IMPL_TRAIT_TYPE => Some(ImplTraitType { syntax }),
_ => None, _ => 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 // Module
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Module<R: TreeRoot = OwnedRoot> { pub struct Module<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for Module<R> { impl<'a> AstNode<'a> for Module<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
MODULE => Some(Module { syntax }), MODULE => Some(Module { syntax }),
_ => None, _ => 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<'a> ast::NameOwner<'a> for Module<'a> {}
impl<R: TreeRoot> ast::AttrsOwner<R> for Module<R> {} impl<'a> ast::AttrsOwner<'a> for Module<'a> {}
impl<R: TreeRoot> Module<R> {} impl<'a> Module<'a> {}
// Name // Name
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Name<R: TreeRoot = OwnedRoot> { pub struct Name<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for Name<R> { impl<'a> AstNode<'a> for Name<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
NAME => Some(Name { syntax }), NAME => Some(Name { syntax }),
_ => None, _ => 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 // NameRef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct NameRef<R: TreeRoot = OwnedRoot> { pub struct NameRef<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for NameRef<R> { impl<'a> AstNode<'a> for NameRef<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
NAME_REF => Some(NameRef { syntax }), NAME_REF => Some(NameRef { syntax }),
_ => None, _ => 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 // NamedField
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct NamedField<R: TreeRoot = OwnedRoot> { pub struct NamedField<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for NamedField<R> { impl<'a> AstNode<'a> for NamedField<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
NAMED_FIELD => Some(NamedField { syntax }), NAMED_FIELD => Some(NamedField { syntax }),
_ => None, _ => 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<'a> ast::NameOwner<'a> for NamedField<'a> {}
impl<R: TreeRoot> ast::AttrsOwner<R> for NamedField<R> {} impl<'a> ast::AttrsOwner<'a> for NamedField<'a> {}
impl<R: TreeRoot> NamedField<R> {} impl<'a> NamedField<'a> {}
// NeverType // NeverType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct NeverType<R: TreeRoot = OwnedRoot> { pub struct NeverType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for NeverType<R> { impl<'a> AstNode<'a> for NeverType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
NEVER_TYPE => Some(NeverType { syntax }), NEVER_TYPE => Some(NeverType { syntax }),
_ => None, _ => 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 // NominalDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum NominalDef<R: TreeRoot = OwnedRoot> { pub enum NominalDef<'a> {
StructDef(StructDef<R>), StructDef(StructDef<'a>),
EnumDef(EnumDef<R>), EnumDef(EnumDef<'a>),
} }
impl<R: TreeRoot> AstNode<R> for NominalDef<R> { impl<'a> AstNode<'a> for NominalDef<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
STRUCT_DEF => Some(NominalDef::StructDef(StructDef { syntax })), STRUCT_DEF => Some(NominalDef::StructDef(StructDef { syntax })),
ENUM_DEF => Some(NominalDef::EnumDef(EnumDef { syntax })), ENUM_DEF => Some(NominalDef::EnumDef(EnumDef { syntax })),
_ => None, _ => None,
} }
} }
fn syntax(&self) -> &SyntaxNode<R> { fn syntax(self) -> SyntaxNodeRef<'a> {
match self { match self {
NominalDef::StructDef(inner) => inner.syntax(), NominalDef::StructDef(inner) => inner.syntax(),
NominalDef::EnumDef(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<'a> ast::AttrsOwner<'a> for NominalDef<'a> {}
impl<R: TreeRoot> NominalDef<R> {} impl<'a> NominalDef<'a> {}
// ParenType // ParenType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ParenType<R: TreeRoot = OwnedRoot> { pub struct ParenType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for ParenType<R> { impl<'a> AstNode<'a> for ParenType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
PAREN_TYPE => Some(ParenType { syntax }), PAREN_TYPE => Some(ParenType { syntax }),
_ => None, _ => 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 // PathType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct PathType<R: TreeRoot = OwnedRoot> { pub struct PathType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for PathType<R> { impl<'a> AstNode<'a> for PathType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
PATH_TYPE => Some(PathType { syntax }), PATH_TYPE => Some(PathType { syntax }),
_ => None, _ => 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 // PlaceholderType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct PlaceholderType<R: TreeRoot = OwnedRoot> { pub struct PlaceholderType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for PlaceholderType<R> { impl<'a> AstNode<'a> for PlaceholderType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
PLACEHOLDER_TYPE => Some(PlaceholderType { syntax }), PLACEHOLDER_TYPE => Some(PlaceholderType { syntax }),
_ => None, _ => 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 // PointerType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct PointerType<R: TreeRoot = OwnedRoot> { pub struct PointerType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for PointerType<R> { impl<'a> AstNode<'a> for PointerType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
POINTER_TYPE => Some(PointerType { syntax }), POINTER_TYPE => Some(PointerType { syntax }),
_ => None, _ => 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 // ReferenceType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ReferenceType<R: TreeRoot = OwnedRoot> { pub struct ReferenceType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for ReferenceType<R> { impl<'a> AstNode<'a> for ReferenceType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
REFERENCE_TYPE => Some(ReferenceType { syntax }), REFERENCE_TYPE => Some(ReferenceType { syntax }),
_ => None, _ => 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 // SliceType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct SliceType<R: TreeRoot = OwnedRoot> { pub struct SliceType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for SliceType<R> { impl<'a> AstNode<'a> for SliceType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
SLICE_TYPE => Some(SliceType { syntax }), SLICE_TYPE => Some(SliceType { syntax }),
_ => None, _ => 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 // StaticDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct StaticDef<R: TreeRoot = OwnedRoot> { pub struct StaticDef<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for StaticDef<R> { impl<'a> AstNode<'a> for StaticDef<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
STATIC_DEF => Some(StaticDef { syntax }), STATIC_DEF => Some(StaticDef { syntax }),
_ => None, _ => 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<'a> ast::NameOwner<'a> for StaticDef<'a> {}
impl<R: TreeRoot> ast::AttrsOwner<R> for StaticDef<R> {} impl<'a> ast::AttrsOwner<'a> for StaticDef<'a> {}
impl<R: TreeRoot> StaticDef<R> {} impl<'a> StaticDef<'a> {}
// StructDef // StructDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct StructDef<R: TreeRoot = OwnedRoot> { pub struct StructDef<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for StructDef<R> { impl<'a> AstNode<'a> for StructDef<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
STRUCT_DEF => Some(StructDef { syntax }), STRUCT_DEF => Some(StructDef { syntax }),
_ => None, _ => 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<'a> ast::NameOwner<'a> for StructDef<'a> {}
impl<R: TreeRoot> ast::AttrsOwner<R> for StructDef<R> {} impl<'a> ast::AttrsOwner<'a> for StructDef<'a> {}
impl<R: TreeRoot> StructDef<R> { impl<'a> StructDef<'a> {
pub fn fields<'a>(&'a self) -> impl Iterator<Item = NamedField<R>> + 'a { pub fn fields(self) -> impl Iterator<Item = NamedField<'a>> + 'a {
self.syntax() self.syntax()
.children() .children()
.filter_map(NamedField::cast) .filter_map(NamedField::cast)
@ -497,100 +497,100 @@ impl<R: TreeRoot> StructDef<R> {
// TokenTree // TokenTree
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct TokenTree<R: TreeRoot = OwnedRoot> { pub struct TokenTree<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for TokenTree<R> { impl<'a> AstNode<'a> for TokenTree<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
TOKEN_TREE => Some(TokenTree { syntax }), TOKEN_TREE => Some(TokenTree { syntax }),
_ => None, _ => 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 // TraitDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct TraitDef<R: TreeRoot = OwnedRoot> { pub struct TraitDef<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for TraitDef<R> { impl<'a> AstNode<'a> for TraitDef<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
TRAIT_DEF => Some(TraitDef { syntax }), TRAIT_DEF => Some(TraitDef { syntax }),
_ => None, _ => 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<'a> ast::NameOwner<'a> for TraitDef<'a> {}
impl<R: TreeRoot> ast::AttrsOwner<R> for TraitDef<R> {} impl<'a> ast::AttrsOwner<'a> for TraitDef<'a> {}
impl<R: TreeRoot> TraitDef<R> {} impl<'a> TraitDef<'a> {}
// TupleType // TupleType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct TupleType<R: TreeRoot = OwnedRoot> { pub struct TupleType<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for TupleType<R> { impl<'a> AstNode<'a> for TupleType<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
TUPLE_TYPE => Some(TupleType { syntax }), TUPLE_TYPE => Some(TupleType { syntax }),
_ => None, _ => 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 // TypeDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct TypeDef<R: TreeRoot = OwnedRoot> { pub struct TypeDef<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for TypeDef<R> { impl<'a> AstNode<'a> for TypeDef<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
TYPE_DEF => Some(TypeDef { syntax }), TYPE_DEF => Some(TypeDef { syntax }),
_ => None, _ => 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<'a> ast::NameOwner<'a> for TypeDef<'a> {}
impl<R: TreeRoot> ast::AttrsOwner<R> for TypeDef<R> {} impl<'a> ast::AttrsOwner<'a> for TypeDef<'a> {}
impl<R: TreeRoot> TypeDef<R> {} impl<'a> TypeDef<'a> {}
// TypeRef // TypeRef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum TypeRef<R: TreeRoot = OwnedRoot> { pub enum TypeRef<'a> {
ParenType(ParenType<R>), ParenType(ParenType<'a>),
TupleType(TupleType<R>), TupleType(TupleType<'a>),
NeverType(NeverType<R>), NeverType(NeverType<'a>),
PathType(PathType<R>), PathType(PathType<'a>),
PointerType(PointerType<R>), PointerType(PointerType<'a>),
ArrayType(ArrayType<R>), ArrayType(ArrayType<'a>),
SliceType(SliceType<R>), SliceType(SliceType<'a>),
ReferenceType(ReferenceType<R>), ReferenceType(ReferenceType<'a>),
PlaceholderType(PlaceholderType<R>), PlaceholderType(PlaceholderType<'a>),
FnPointerType(FnPointerType<R>), FnPointerType(FnPointerType<'a>),
ForType(ForType<R>), ForType(ForType<'a>),
ImplTraitType(ImplTraitType<R>), ImplTraitType(ImplTraitType<'a>),
DynTraitType(DynTraitType<R>), DynTraitType(DynTraitType<'a>),
} }
impl<R: TreeRoot> AstNode<R> for TypeRef<R> { impl<'a> AstNode<'a> for TypeRef<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
PAREN_TYPE => Some(TypeRef::ParenType(ParenType { syntax })), PAREN_TYPE => Some(TypeRef::ParenType(ParenType { syntax })),
TUPLE_TYPE => Some(TypeRef::TupleType(TupleType { syntax })), TUPLE_TYPE => Some(TypeRef::TupleType(TupleType { syntax })),
@ -608,7 +608,7 @@ impl<R: TreeRoot> AstNode<R> for TypeRef<R> {
_ => None, _ => None,
} }
} }
fn syntax(&self) -> &SyntaxNode<R> { fn syntax(self) -> SyntaxNodeRef<'a> {
match self { match self {
TypeRef::ParenType(inner) => inner.syntax(), TypeRef::ParenType(inner) => inner.syntax(),
TypeRef::TupleType(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 { use {
ast, ast,
SyntaxNode, SyntaxRoot, TreeRoot, AstNode, SyntaxNodeRef, AstNode,
SyntaxKind::*, SyntaxKind::*,
}; };
{% for node, methods in ast %} {% for node, methods in ast %}
// {{ node }} // {{ node }}
{%- if methods.enum %} {%- if methods.enum %}
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum {{ node }}<R: TreeRoot = Arc<SyntaxRoot>> { pub enum {{ node }}<'a> {
{%- for kind in methods.enum %} {%- for kind in methods.enum %}
{{ kind }}({{ kind }}<R>), {{ kind }}({{ kind }}<'a>),
{%- endfor %} {%- endfor %}
} }
impl<R: TreeRoot> AstNode<R> for {{ node }}<R> { impl<'a> AstNode<'a> for {{ node }}<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> 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 }} => Some({{ node }}::{{ kind }}({{ kind }} { syntax })),
@ -23,7 +22,7 @@ impl<R: TreeRoot> AstNode<R> for {{ node }}<R> {
_ => None, _ => None,
} }
} }
fn syntax(&self) -> &SyntaxNode<R> { fn syntax(self) -> SyntaxNodeRef<'a> {
match self { match self {
{%- for kind in methods.enum %} {%- for kind in methods.enum %}
{{ node }}::{{ kind }}(inner) => inner.syntax(), {{ node }}::{{ kind }}(inner) => inner.syntax(),
@ -33,32 +32,32 @@ impl<R: TreeRoot> AstNode<R> for {{ node }}<R> {
} }
{% else %} {% else %}
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct {{ node }}<R: TreeRoot = Arc<SyntaxRoot>> { pub struct {{ node }}<'a> {
syntax: SyntaxNode<R>, syntax: SyntaxNodeRef<'a>,
} }
impl<R: TreeRoot> AstNode<R> for {{ node }}<R> { impl<'a> AstNode<'a> for {{ node }}<'a> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
{{ node | SCREAM }} => Some({{ node }} { syntax }), {{ node | SCREAM }} => Some({{ node }} { syntax }),
_ => None, _ => None,
} }
} }
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
} }
{% endif %} {% endif %}
{% if methods.traits -%} {% if methods.traits -%}
{%- for t in 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 -%} {% endfor -%}
{%- endif -%} {%- endif -%}
impl<R: TreeRoot> {{ node }}<R> { impl<'a> {{ node }}<'a> {
{%- 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 }}<'a>(&'a self) -> impl Iterator<Item = {{ ChildName }}<R>> + 'a { pub fn {{ method_name }}(self) -> impl Iterator<Item = {{ ChildName }}<'a>> + 'a {
self.syntax() self.syntax()
.children() .children()
.filter_map({{ ChildName }}::cast) .filter_map({{ ChildName }}::cast)
@ -70,7 +69,7 @@ impl<R: TreeRoot> {{ node }}<R> {
{%- for m in methods.options -%} {%- for m in methods.options -%}
{%- set method_name = m.0 -%} {%- set method_name = m.0 -%}
{%- set ChildName = m.1 %} {%- set ChildName = m.1 %}
pub fn {{ method_name }}(&self) -> Option<{{ ChildName }}<R>> { pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> {
self.syntax() self.syntax()
.children() .children()
.filter_map({{ ChildName }}::cast) .filter_map({{ ChildName }}::cast)

View file

@ -4,22 +4,19 @@ use itertools::Itertools;
use smol_str::SmolStr; use smol_str::SmolStr;
use { use {
SyntaxNode, SyntaxNodeRef, OwnedRoot, TreeRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, SyntaxError,
SyntaxKind::*, SyntaxKind::*,
}; };
pub use self::generated::*; pub use self::generated::*;
pub trait AstNode<R: TreeRoot> { pub trait AstNode<'a>: Clone + Copy {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self>
where Self: Sized; where Self: Sized;
fn syntax(&self) -> &SyntaxNode<R>; fn syntax(self) -> SyntaxNodeRef<'a>;
fn syntax_ref<'a>(&'a self) -> SyntaxNodeRef<'a> where R: 'a {
self.syntax().as_ref()
}
} }
pub trait NameOwner<R: TreeRoot>: AstNode<R> { pub trait NameOwner<'a>: AstNode<'a> {
fn name(&self) -> Option<Name<R>> { fn name(self) -> Option<Name<'a>> {
self.syntax() self.syntax()
.children() .children()
.filter_map(Name::cast) .filter_map(Name::cast)
@ -27,27 +24,37 @@ pub trait NameOwner<R: TreeRoot>: AstNode<R> {
} }
} }
pub trait AttrsOwner<R: TreeRoot>: AstNode<R> { pub trait AttrsOwner<'a>: AstNode<'a> {
fn attrs<'a>(&'a self) -> Box<Iterator<Item=Attr<R>> + 'a> where R: 'a { fn attrs(&self) -> Box<Iterator<Item=Attr<'a>> + 'a> {
let it = self.syntax().children() let it = self.syntax().children()
.filter_map(Attr::cast); .filter_map(Attr::cast);
Box::new(it) Box::new(it)
} }
} }
impl File<OwnedRoot> { #[derive(Clone, Debug)]
pub fn parse(text: &str) -> Self { pub struct ParsedFile {
File::cast(::parse(text)).unwrap() 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> { pub fn errors(&self) -> Vec<SyntaxError> {
self.syntax().root.syntax_root().errors.clone() 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 { pub fn has_atom_attr(&self, atom: &str) -> bool {
self.attrs() self.attrs()
.filter_map(|x| x.as_atom()) .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> { 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()?;
@ -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 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)?;
@ -78,7 +85,7 @@ impl<R: TreeRoot> Attr<R> {
} }
} }
impl<R: TreeRoot> Name<R> { impl<'a> Name<'a> {
pub fn text(&self) -> SmolStr { pub fn text(&self) -> SmolStr {
let ident = self.syntax().first_child() let ident = self.syntax().first_child()
.unwrap(); .unwrap();
@ -86,7 +93,7 @@ impl<R: TreeRoot> Name<R> {
} }
} }
impl<R: TreeRoot> NameRef<R> { impl<'a> NameRef<'a> {
pub fn text(&self) -> SmolStr { pub fn text(&self) -> SmolStr {
let ident = self.syntax().first_child() let ident = self.syntax().first_child()
.unwrap(); .unwrap();
@ -94,22 +101,22 @@ impl<R: TreeRoot> NameRef<R> {
} }
} }
impl <R: TreeRoot> ImplItem<R> { impl<'a> ImplItem<'a> {
pub fn target_type(&self) -> Option<TypeRef<R>> { pub fn target_type(&self) -> Option<TypeRef<'a>> {
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<R>> { pub fn target_trait(&self) -> Option<TypeRef<'a>> {
match self.target() { match self.target() {
(Some(t), Some(_)) => Some(t), (Some(t), Some(_)) => Some(t),
_ => None, _ => 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 mut types = self.syntax().children().filter_map(TypeRef::cast);
let first = types.next(); let first = types.next();
let second = 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 { pub fn has_semi(&self) -> bool {
match self.syntax_ref().last_child() { match self.syntax().last_child() {
None => false, None => false,
Some(node) => node.kind() == SEMI, Some(node) => node.kind() == SEMI,
} }

View file

@ -41,7 +41,7 @@ mod yellow;
pub mod utils; pub mod utils;
pub use { pub use {
ast::{AstNode, File}, ast::{AstNode, ParsedFile},
lexer::{tokenize, Token}, lexer::{tokenize, Token},
syntax_kinds::SyntaxKind, syntax_kinds::SyntaxKind,
text_unit::{TextRange, TextUnit}, text_unit::{TextRange, TextUnit},

View file

@ -1,12 +1,11 @@
use std::fmt::Write; use std::fmt::Write;
use { use {
algo::walk::{walk, WalkEvent}, algo::walk::{walk, WalkEvent},
SyntaxNode, TreeRoot, SyntaxNodeRef, TreeRoot,
}; };
/// 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: &SyntaxNode) -> String { pub fn dump_tree(syntax: SyntaxNodeRef) -> String {
let syntax = syntax.as_ref();
let mut errors: Vec<_> = syntax.root.syntax_root().errors.iter().cloned().collect(); let mut errors: Vec<_> = syntax.root.syntax_root().errors.iter().cloned().collect();
errors.sort_by_key(|e| e.offset); errors.sort_by_key(|e| e.offset);
let mut err_pos = 0; let mut err_pos = 0;

View file

@ -71,12 +71,16 @@ impl<R: TreeRoot> SyntaxNode<R> {
self.red().green().text() self.red().green().text()
} }
pub fn children<'a>(&'a self) -> impl Iterator<Item = SyntaxNode<R>> + 'a { pub fn children(&self) -> impl Iterator<Item = SyntaxNode<R>> {
let red = self.red(); let red = self.red;
let n_children = red.n_children(); let n_children = self.red().n_children();
(0..n_children).map(move |i| SyntaxNode { let root = self.root.clone();
root: self.root.clone(), (0..n_children).map(move |i| {
red: red.get_child(i).unwrap(), 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() { fn parser_tests() {
dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {
let file = libsyntax2::parse(text); let file = libsyntax2::parse(text);
libsyntax2::utils::dump_tree(&file) libsyntax2::utils::dump_tree(file.as_ref())
}) })
} }