2019-02-21 12:24:42 +00:00
|
|
|
//! Abstract Syntax Tree, layered on top of untyped `SyntaxNode`s
|
2019-04-02 10:02:23 +00:00
|
|
|
|
2018-08-09 14:43:39 +00:00
|
|
|
mod generated;
|
2019-04-02 07:03:19 +00:00
|
|
|
mod traits;
|
2019-04-02 07:23:18 +00:00
|
|
|
mod tokens;
|
2019-04-02 09:47:39 +00:00
|
|
|
mod extensions;
|
|
|
|
mod expr_extensions;
|
2019-09-28 16:50:16 +00:00
|
|
|
pub mod edit;
|
2019-09-26 09:18:26 +00:00
|
|
|
pub mod make;
|
2018-08-09 14:43:39 +00:00
|
|
|
|
2018-09-07 22:16:07 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2018-10-15 16:55:32 +00:00
|
|
|
use crate::{
|
2020-04-09 08:47:05 +00:00
|
|
|
syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken},
|
2019-07-19 15:22:00 +00:00
|
|
|
SmolStr, SyntaxKind,
|
2018-08-09 13:03:21 +00:00
|
|
|
};
|
2018-07-30 18:58:49 +00:00
|
|
|
|
2019-04-02 07:03:19 +00:00
|
|
|
pub use self::{
|
2019-11-15 20:05:29 +00:00
|
|
|
expr_extensions::{ArrayExprKind, BinOp, ElseBranch, LiteralKind, PrefixOp, RangeOp},
|
2019-12-24 19:32:42 +00:00
|
|
|
extensions::{
|
2020-02-12 14:44:52 +00:00
|
|
|
AttrKind, FieldKind, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind,
|
|
|
|
TypeBoundKind, VisibilityKind,
|
2019-12-24 19:32:42 +00:00
|
|
|
},
|
2020-04-09 15:47:46 +00:00
|
|
|
generated::{nodes::*, tokens::*},
|
2019-04-02 07:23:18 +00:00
|
|
|
tokens::*,
|
2019-07-04 20:05:17 +00:00
|
|
|
traits::*,
|
2019-04-02 07:03:19 +00:00
|
|
|
};
|
|
|
|
|
2018-11-06 19:06:58 +00:00
|
|
|
/// The main trait to go from untyped `SyntaxNode` to a typed ast. The
|
|
|
|
/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
|
|
|
|
/// the same representation: a pointer to the tree root and a pointer to the
|
|
|
|
/// node itself.
|
2020-04-09 08:47:05 +00:00
|
|
|
pub trait AstNode {
|
2019-08-22 12:20:07 +00:00
|
|
|
fn can_cast(kind: SyntaxKind) -> bool
|
|
|
|
where
|
|
|
|
Self: Sized;
|
2019-07-19 15:22:00 +00:00
|
|
|
|
2020-04-03 19:12:08 +00:00
|
|
|
fn cast(syntax: SyntaxNode) -> Option<Self>
|
|
|
|
where
|
2020-04-09 08:47:05 +00:00
|
|
|
Self: Sized;
|
2020-04-03 19:12:08 +00:00
|
|
|
|
2019-01-07 13:15:47 +00:00
|
|
|
fn syntax(&self) -> &SyntaxNode;
|
2018-08-09 13:03:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:47:39 +00:00
|
|
|
/// Like `AstNode`, but wraps tokens rather than interior nodes.
|
2020-04-09 08:47:05 +00:00
|
|
|
pub trait AstToken {
|
2020-04-03 19:12:08 +00:00
|
|
|
fn can_cast(token: SyntaxKind) -> bool
|
2019-04-02 09:47:39 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2020-04-03 19:12:08 +00:00
|
|
|
|
|
|
|
fn cast(syntax: SyntaxToken) -> Option<Self>
|
|
|
|
where
|
2020-04-09 08:47:05 +00:00
|
|
|
Self: Sized;
|
2020-04-03 19:12:08 +00:00
|
|
|
|
2019-07-18 16:23:05 +00:00
|
|
|
fn syntax(&self) -> &SyntaxToken;
|
2020-04-03 19:12:08 +00:00
|
|
|
|
2019-07-18 16:23:05 +00:00
|
|
|
fn text(&self) -> &SmolStr {
|
2019-04-02 09:47:39 +00:00
|
|
|
self.syntax().text()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 10:02:23 +00:00
|
|
|
/// An iterator over `SyntaxNode` children of a particular AST type.
|
2020-02-29 22:24:50 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-07-18 16:23:05 +00:00
|
|
|
pub struct AstChildren<N> {
|
|
|
|
inner: SyntaxNodeChildren,
|
2019-04-02 07:09:52 +00:00
|
|
|
ph: PhantomData<N>,
|
|
|
|
}
|
|
|
|
|
2019-07-18 16:23:05 +00:00
|
|
|
impl<N> AstChildren<N> {
|
|
|
|
fn new(parent: &SyntaxNode) -> Self {
|
2019-04-02 07:09:52 +00:00
|
|
|
AstChildren { inner: parent.children(), ph: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 16:23:05 +00:00
|
|
|
impl<N: AstNode> Iterator for AstChildren<N> {
|
|
|
|
type Item = N;
|
|
|
|
fn next(&mut self) -> Option<N> {
|
2019-04-02 07:09:52 +00:00
|
|
|
self.inner.by_ref().find_map(N::cast)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 20:22:58 +00:00
|
|
|
mod support {
|
|
|
|
use super::{AstChildren, AstNode, AstToken, SyntaxNode};
|
|
|
|
|
|
|
|
pub(super) fn child<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
|
|
|
|
parent.children().find_map(N::cast)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn children<N: AstNode>(parent: &SyntaxNode) -> AstChildren<N> {
|
|
|
|
AstChildren::new(parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn token<T: AstToken>(parent: &SyntaxNode) -> Option<T> {
|
|
|
|
parent.children_with_tokens().filter_map(|it| it.into_token()).find_map(T::cast)
|
|
|
|
}
|
2018-08-22 14:01:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 20:22:58 +00:00
|
|
|
#[test]
|
|
|
|
fn assert_ast_is_object_safe() {
|
|
|
|
fn _f(_: &dyn AstNode, _: &dyn NameOwner) {}
|
2018-09-07 22:16:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 15:35:23 +00:00
|
|
|
#[test]
|
|
|
|
fn test_doc_comment_none() {
|
|
|
|
let file = SourceFile::parse(
|
|
|
|
r#"
|
|
|
|
// non-doc
|
|
|
|
mod foo {}
|
|
|
|
"#,
|
2019-05-28 14:34:28 +00:00
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
2019-01-26 15:35:23 +00:00
|
|
|
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
|
|
|
assert!(module.doc_comment_text().is_none());
|
|
|
|
}
|
|
|
|
|
2019-01-04 13:51:45 +00:00
|
|
|
#[test]
|
|
|
|
fn test_doc_comment_of_items() {
|
2019-01-07 13:15:47 +00:00
|
|
|
let file = SourceFile::parse(
|
2019-01-04 13:51:45 +00:00
|
|
|
r#"
|
|
|
|
//! doc
|
|
|
|
// non-doc
|
|
|
|
mod foo {}
|
|
|
|
"#,
|
2019-05-28 14:34:28 +00:00
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
2019-01-04 13:51:45 +00:00
|
|
|
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
2019-01-26 15:35:23 +00:00
|
|
|
assert_eq!("doc", module.doc_comment_text().unwrap());
|
2019-01-04 13:51:45 +00:00
|
|
|
}
|
2019-01-26 02:31:31 +00:00
|
|
|
|
2019-10-31 20:21:46 +00:00
|
|
|
#[test]
|
|
|
|
fn test_doc_comment_of_statics() {
|
|
|
|
let file = SourceFile::parse(
|
|
|
|
r#"
|
|
|
|
/// Number of levels
|
|
|
|
static LEVELS: i32 = 0;
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
|
|
|
let st = file.syntax().descendants().find_map(StaticDef::cast).unwrap();
|
|
|
|
assert_eq!("Number of levels", st.doc_comment_text().unwrap());
|
|
|
|
}
|
|
|
|
|
2019-01-26 02:31:31 +00:00
|
|
|
#[test]
|
|
|
|
fn test_doc_comment_preserves_indents() {
|
|
|
|
let file = SourceFile::parse(
|
|
|
|
r#"
|
|
|
|
/// doc1
|
|
|
|
/// ```
|
|
|
|
/// fn foo() {
|
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
mod foo {}
|
|
|
|
"#,
|
2019-05-28 14:34:28 +00:00
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
2019-01-26 02:31:31 +00:00
|
|
|
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
2019-02-08 11:49:43 +00:00
|
|
|
assert_eq!("doc1\n```\nfn foo() {\n // ...\n}\n```", module.doc_comment_text().unwrap());
|
2019-01-04 13:51:45 +00:00
|
|
|
}
|
2019-03-24 17:45:11 +00:00
|
|
|
|
2019-07-31 14:46:15 +00:00
|
|
|
#[test]
|
|
|
|
fn test_doc_comment_preserves_newlines() {
|
|
|
|
let file = SourceFile::parse(
|
|
|
|
r#"
|
|
|
|
/// this
|
|
|
|
/// is
|
|
|
|
/// mod
|
|
|
|
/// foo
|
|
|
|
mod foo {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
|
|
|
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
|
|
|
assert_eq!("this\nis\nmod\nfoo", module.doc_comment_text().unwrap());
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:43:00 +00:00
|
|
|
#[test]
|
|
|
|
fn test_doc_comment_single_line_block_strips_suffix() {
|
|
|
|
let file = SourceFile::parse(
|
|
|
|
r#"
|
|
|
|
/** this is mod foo*/
|
|
|
|
mod foo {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
|
|
|
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
|
|
|
assert_eq!("this is mod foo", module.doc_comment_text().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_doc_comment_single_line_block_strips_suffix_whitespace() {
|
|
|
|
let file = SourceFile::parse(
|
|
|
|
r#"
|
|
|
|
/** this is mod foo */
|
|
|
|
mod foo {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
|
|
|
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
2019-10-27 20:56:25 +00:00
|
|
|
assert_eq!("this is mod foo ", module.doc_comment_text().unwrap());
|
2019-07-31 15:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_doc_comment_multi_line_block_strips_suffix() {
|
|
|
|
let file = SourceFile::parse(
|
|
|
|
r#"
|
|
|
|
/**
|
|
|
|
this
|
|
|
|
is
|
|
|
|
mod foo
|
|
|
|
*/
|
|
|
|
mod foo {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
|
|
|
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
2019-10-27 20:56:25 +00:00
|
|
|
assert_eq!(
|
|
|
|
" this\n is\n mod foo\n ",
|
|
|
|
module.doc_comment_text().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_comments_preserve_trailing_whitespace() {
|
|
|
|
let file = SourceFile::parse(
|
2020-02-12 15:04:16 +00:00
|
|
|
"\n/// Representation of a Realm. \n/// In the specification these are called Realm Records.\nstruct Realm {}",
|
2019-10-27 20:56:25 +00:00
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
|
|
|
let def = file.syntax().descendants().find_map(StructDef::cast).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
"Representation of a Realm. \nIn the specification these are called Realm Records.",
|
|
|
|
def.doc_comment_text().unwrap()
|
|
|
|
);
|
2019-07-31 15:43:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 17:45:11 +00:00
|
|
|
#[test]
|
|
|
|
fn test_where_predicates() {
|
2019-07-18 16:23:05 +00:00
|
|
|
fn assert_bound(text: &str, bound: Option<TypeBound>) {
|
2019-03-24 17:45:11 +00:00
|
|
|
assert_eq!(text, bound.unwrap().syntax().text().to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
let file = SourceFile::parse(
|
|
|
|
r#"
|
|
|
|
fn foo()
|
|
|
|
where
|
|
|
|
T: Clone + Copy + Debug + 'static,
|
|
|
|
'a: 'b + 'c,
|
|
|
|
Iterator::Item: 'a + Debug,
|
|
|
|
Iterator::Item: Debug + 'a,
|
|
|
|
<T as Iterator>::Item: Debug + 'a,
|
|
|
|
for<'a> F: Fn(&'a str)
|
|
|
|
{}
|
|
|
|
"#,
|
2019-05-28 14:34:28 +00:00
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
2019-03-24 17:45:11 +00:00
|
|
|
let where_clause = file.syntax().descendants().find_map(WhereClause::cast).unwrap();
|
|
|
|
|
|
|
|
let mut predicates = where_clause.predicates();
|
|
|
|
|
|
|
|
let pred = predicates.next().unwrap();
|
|
|
|
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
|
|
|
|
|
|
|
assert_eq!("T", pred.type_ref().unwrap().syntax().text().to_string());
|
|
|
|
assert_bound("Clone", bounds.next());
|
|
|
|
assert_bound("Copy", bounds.next());
|
|
|
|
assert_bound("Debug", bounds.next());
|
|
|
|
assert_bound("'static", bounds.next());
|
|
|
|
|
|
|
|
let pred = predicates.next().unwrap();
|
|
|
|
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
|
|
|
|
2020-04-09 16:25:36 +00:00
|
|
|
assert_eq!("'a", pred.lifetime_token().unwrap().text());
|
2019-03-24 17:45:11 +00:00
|
|
|
|
|
|
|
assert_bound("'b", bounds.next());
|
|
|
|
assert_bound("'c", bounds.next());
|
|
|
|
|
|
|
|
let pred = predicates.next().unwrap();
|
|
|
|
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
|
|
|
|
|
|
|
assert_eq!("Iterator::Item", pred.type_ref().unwrap().syntax().text().to_string());
|
|
|
|
assert_bound("'a", bounds.next());
|
|
|
|
|
|
|
|
let pred = predicates.next().unwrap();
|
|
|
|
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
|
|
|
|
|
|
|
assert_eq!("Iterator::Item", pred.type_ref().unwrap().syntax().text().to_string());
|
|
|
|
assert_bound("Debug", bounds.next());
|
|
|
|
assert_bound("'a", bounds.next());
|
|
|
|
|
|
|
|
let pred = predicates.next().unwrap();
|
|
|
|
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
|
|
|
|
|
|
|
assert_eq!("<T as Iterator>::Item", pred.type_ref().unwrap().syntax().text().to_string());
|
|
|
|
assert_bound("Debug", bounds.next());
|
|
|
|
assert_bound("'a", bounds.next());
|
|
|
|
|
|
|
|
let pred = predicates.next().unwrap();
|
|
|
|
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
|
|
|
|
|
|
|
assert_eq!("for<'a> F", pred.type_ref().unwrap().syntax().text().to_string());
|
|
|
|
assert_bound("Fn(&'a str)", bounds.next());
|
|
|
|
}
|