2018-07-30 18:58:49 +00:00
|
|
|
use std::sync::Arc;
|
2018-08-09 13:03:21 +00:00
|
|
|
use {
|
|
|
|
SyntaxNode, SyntaxRoot, TreeRoot,
|
|
|
|
SyntaxKind::*,
|
|
|
|
};
|
2018-07-30 18:58:49 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct File<R: TreeRoot = Arc<SyntaxRoot>> {
|
2018-07-31 12:40:40 +00:00
|
|
|
syntax: SyntaxNode<R>,
|
2018-07-30 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 13:03:21 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Function<R: TreeRoot = Arc<SyntaxRoot>> {
|
|
|
|
syntax: SyntaxNode<R>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> {
|
|
|
|
syntax: SyntaxNode<R>,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 18:58:49 +00:00
|
|
|
impl File<Arc<SyntaxRoot>> {
|
|
|
|
pub fn parse(text: &str) -> Self {
|
2018-07-31 12:40:40 +00:00
|
|
|
File {
|
2018-08-01 07:40:07 +00:00
|
|
|
syntax: ::parse(text),
|
2018-07-31 12:40:40 +00:00
|
|
|
}
|
2018-07-30 18:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 13:03:21 +00:00
|
|
|
impl<R: TreeRoot> File<R> {
|
|
|
|
pub fn functions<'a>(&'a self) -> impl Iterator<Item = Function<R>> + 'a {
|
|
|
|
self.syntax
|
|
|
|
.children()
|
|
|
|
.filter(|node| node.kind() == FN_ITEM)
|
|
|
|
.map(|node| Function { syntax: node })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: TreeRoot> Function<R> {
|
|
|
|
pub fn syntax(&self) -> SyntaxNode<R> {
|
|
|
|
self.syntax.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name(&self) -> Option<Name<R>> {
|
|
|
|
self.syntax
|
|
|
|
.children()
|
|
|
|
.filter(|node| node.kind() == NAME)
|
|
|
|
.map(|node| Name { syntax: node })
|
|
|
|
.next()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_atom_attr(&self, atom: &str) -> bool {
|
|
|
|
self.syntax
|
|
|
|
.children()
|
|
|
|
.filter(|node| node.kind() == ATTR)
|
|
|
|
.any(|attr| {
|
|
|
|
let mut metas = attr.children().filter(|node| node.kind() == META_ITEM);
|
|
|
|
let meta = match metas.next() {
|
|
|
|
None => return false,
|
|
|
|
Some(meta) => {
|
|
|
|
if metas.next().is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
meta
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let mut children = meta.children();
|
|
|
|
match children.next() {
|
|
|
|
None => false,
|
|
|
|
Some(child) => {
|
|
|
|
if children.next().is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
child.kind() == IDENT && child.text() == atom
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: TreeRoot> Name<R> {
|
|
|
|
pub fn text(&self) -> String {
|
|
|
|
self.syntax.text()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-30 18:58:49 +00:00
|
|
|
impl<R: TreeRoot> File<R> {
|
|
|
|
pub fn syntax(&self) -> SyntaxNode<R> {
|
|
|
|
self.syntax.clone()
|
|
|
|
}
|
|
|
|
}
|