Provide more complete AST accessors to support usage in rustc

This commit is contained in:
Luca Barbieri 2020-04-03 21:12:09 +02:00 committed by Aleksey Kladov
parent 8595693287
commit 60f4d7bd8c
18 changed files with 433 additions and 211 deletions

View file

@ -4,9 +4,9 @@
use itertools::Itertools;
use crate::{
ast::{self, child_opt, children, AstChildren, AstNode, AstToken},
syntax_node::SyntaxElementChildren,
use crate::ast::{
self, child_elements, child_opt, child_token_opt, child_tokens, children, AstChildElements,
AstChildTokens, AstChildren, AstNode, AstToken,
};
pub trait TypeAscriptionOwner: AstNode {
@ -31,6 +31,10 @@ pub trait LoopBodyOwner: AstNode {
fn loop_body(&self) -> Option<ast::BlockExpr> {
child_opt(self)
}
fn label(&self) -> Option<ast::Label> {
child_opt(self)
}
}
pub trait ArgListOwner: AstNode {
@ -65,6 +69,10 @@ pub trait TypeBoundsOwner: AstNode {
fn type_bound_list(&self) -> Option<ast::TypeBoundList> {
child_opt(self)
}
fn colon(&self) -> Option<ast::Colon> {
child_token_opt(self)
}
}
pub trait AttrsOwner: AstNode {
@ -74,11 +82,14 @@ pub trait AttrsOwner: AstNode {
fn has_atom_attr(&self, atom: &str) -> bool {
self.attrs().filter_map(|x| x.as_simple_atom()).any(|x| x == atom)
}
fn attr_or_comments(&self) -> AstChildElements<ast::AttrOrComment> {
child_elements(self)
}
}
pub trait DocCommentsOwner: AstNode {
fn doc_comments(&self) -> CommentIter {
CommentIter { iter: self.syntax().children_with_tokens() }
fn doc_comments(&self) -> AstChildTokens<ast::Comment> {
child_tokens(self)
}
/// Returns the textual content of a doc comment block as a single string.
@ -123,14 +134,3 @@ pub trait DocCommentsOwner: AstNode {
}
}
}
pub struct CommentIter {
iter: SyntaxElementChildren,
}
impl Iterator for CommentIter {
type Item = ast::Comment;
fn next(&mut self) -> Option<ast::Comment> {
self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast))
}
}