From 36bd28633baf6015b767e9e70d2d53185271db50 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Aug 2018 16:03:21 +0300 Subject: Extract runnables --- src/ast.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) (limited to 'src/ast.rs') diff --git a/src/ast.rs b/src/ast.rs index caf5fb7ef..a595b9324 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,11 +1,25 @@ use std::sync::Arc; -use {SyntaxNode, SyntaxRoot, TreeRoot}; +use { + SyntaxNode, SyntaxRoot, TreeRoot, + SyntaxKind::*, +}; #[derive(Debug)] pub struct File> { syntax: SyntaxNode, } +#[derive(Debug)] +pub struct Function> { + syntax: SyntaxNode, +} + +#[derive(Debug)] +pub struct Name> { + syntax: SyntaxNode, +} + + impl File> { pub fn parse(text: &str) -> Self { File { @@ -14,6 +28,65 @@ impl File> { } } +impl File { + pub fn functions<'a>(&'a self) -> impl Iterator> + 'a { + self.syntax + .children() + .filter(|node| node.kind() == FN_ITEM) + .map(|node| Function { syntax: node }) + } +} + +impl Function { + pub fn syntax(&self) -> SyntaxNode { + self.syntax.clone() + } + + pub fn name(&self) -> Option> { + 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 Name { + pub fn text(&self) -> String { + self.syntax.text() + } +} + + + impl File { pub fn syntax(&self) -> SyntaxNode { self.syntax.clone() -- cgit v1.2.3