2018-08-09 14:43:39 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use {
|
|
|
|
SyntaxNode, SyntaxRoot, TreeRoot, AstNode,
|
|
|
|
SyntaxKind::*,
|
|
|
|
};
|
|
|
|
|
2018-08-10 12:07:43 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2018-08-09 14:43:39 +00:00
|
|
|
pub struct File<R: TreeRoot = Arc<SyntaxRoot>> {
|
|
|
|
syntax: SyntaxNode<R>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: TreeRoot> AstNode<R> for File<R> {
|
|
|
|
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
|
|
|
match syntax.kind() {
|
|
|
|
FILE => Some(File { syntax }),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
|
|
|
}
|
|
|
|
|
2018-08-11 06:55:32 +00:00
|
|
|
impl<R: TreeRoot> File<R> {
|
|
|
|
pub fn functions<'a>(&'a self) -> impl Iterator<Item = Function<R>> + 'a {
|
|
|
|
self.syntax()
|
|
|
|
.children()
|
|
|
|
.filter_map(Function::cast)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 12:07:43 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2018-08-09 14:44:40 +00:00
|
|
|
pub struct Function<R: TreeRoot = Arc<SyntaxRoot>> {
|
2018-08-09 14:43:39 +00:00
|
|
|
syntax: SyntaxNode<R>,
|
|
|
|
}
|
|
|
|
|
2018-08-09 14:44:40 +00:00
|
|
|
impl<R: TreeRoot> AstNode<R> for Function<R> {
|
2018-08-09 14:43:39 +00:00
|
|
|
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
|
|
|
match syntax.kind() {
|
2018-08-09 14:44:40 +00:00
|
|
|
FUNCTION => Some(Function { syntax }),
|
2018-08-09 14:43:39 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
|
|
|
}
|
|
|
|
|
2018-08-11 06:55:32 +00:00
|
|
|
impl<R: TreeRoot> Function<R> {
|
|
|
|
pub fn name(&self) -> Option<Name<R>> {
|
|
|
|
self.syntax()
|
|
|
|
.children()
|
|
|
|
.filter_map(Name::cast)
|
|
|
|
.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 12:07:43 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2018-08-09 14:43:39 +00:00
|
|
|
pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> {
|
|
|
|
syntax: SyntaxNode<R>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: TreeRoot> AstNode<R> for Name<R> {
|
|
|
|
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
|
|
|
|
match syntax.kind() {
|
|
|
|
NAME => Some(Name { syntax }),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
|
|
|
|
}
|
|
|
|
|
2018-08-11 06:55:32 +00:00
|
|
|
impl<R: TreeRoot> Name<R> {}
|
|
|
|
|