2018-08-09 09:43:39 -05:00
|
|
|
mod generated;
|
|
|
|
|
2018-07-30 13:58:49 -05:00
|
|
|
use std::sync::Arc;
|
2018-08-09 08:03:21 -05:00
|
|
|
use {
|
2018-08-09 13:27:44 -05:00
|
|
|
SyntaxNode, SyntaxRoot, TreeRoot, SyntaxError,
|
2018-08-09 08:03:21 -05:00
|
|
|
SyntaxKind::*,
|
|
|
|
};
|
2018-08-09 09:43:39 -05:00
|
|
|
pub use self::generated::*;
|
2018-07-30 13:58:49 -05:00
|
|
|
|
2018-08-09 09:43:39 -05:00
|
|
|
pub trait AstNode<R: TreeRoot>: Sized {
|
|
|
|
fn cast(syntax: SyntaxNode<R>) -> Option<Self>;
|
|
|
|
fn syntax(&self) -> &SyntaxNode<R>;
|
2018-08-09 08:03:21 -05:00
|
|
|
}
|
|
|
|
|
2018-08-11 04:28:59 -05:00
|
|
|
pub trait NameOwner<R: TreeRoot>: AstNode<R> {
|
|
|
|
fn name(&self) -> Option<Name<R>> {
|
|
|
|
self.syntax()
|
|
|
|
.children()
|
|
|
|
.filter_map(Name::cast)
|
|
|
|
.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 13:58:49 -05:00
|
|
|
impl File<Arc<SyntaxRoot>> {
|
|
|
|
pub fn parse(text: &str) -> Self {
|
2018-08-09 09:43:39 -05:00
|
|
|
File::cast(::parse(text)).unwrap()
|
2018-07-30 13:58:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 08:03:21 -05:00
|
|
|
impl<R: TreeRoot> File<R> {
|
2018-08-09 13:27:44 -05:00
|
|
|
pub fn errors(&self) -> Vec<SyntaxError> {
|
|
|
|
self.syntax().root.errors.clone()
|
|
|
|
}
|
2018-08-09 08:03:21 -05:00
|
|
|
}
|
|
|
|
|
2018-08-09 09:44:40 -05:00
|
|
|
impl<R: TreeRoot> Function<R> {
|
2018-08-09 08:03:21 -05:00
|
|
|
pub fn has_atom_attr(&self, atom: &str) -> bool {
|
2018-08-09 09:43:39 -05:00
|
|
|
self.syntax()
|
2018-08-09 08:03:21 -05:00
|
|
|
.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 {
|
2018-08-09 09:43:39 -05:00
|
|
|
self.syntax().text()
|
2018-07-30 13:58:49 -05:00
|
|
|
}
|
|
|
|
}
|