1 //! Abstract Syntax Tree, layered on top of untyped `SyntaxNode`s
12 use std::marker::PhantomData;
15 syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken},
20 expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp},
21 generated::{nodes::*, tokens::*},
23 AttrKind, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind,
24 SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind,
30 /// The main trait to go from untyped `SyntaxNode` to a typed ast. The
31 /// conversion itself has zero runtime cost: ast and syntax nodes have exactly
32 /// the same representation: a pointer to the tree root and a pointer to the
35 fn can_cast(kind: SyntaxKind) -> bool
39 fn cast(syntax: SyntaxNode) -> Option<Self>
43 fn syntax(&self) -> &SyntaxNode;
44 fn clone_for_update(&self) -> Self
48 Self::cast(self.syntax().clone_for_update()).unwrap()
52 /// Like `AstNode`, but wraps tokens rather than interior nodes.
54 fn can_cast(token: SyntaxKind) -> bool
58 fn cast(syntax: SyntaxToken) -> Option<Self>
62 fn syntax(&self) -> &SyntaxToken;
64 fn text(&self) -> &str {
69 /// An iterator over `SyntaxNode` children of a particular AST type.
70 #[derive(Debug, Clone)]
71 pub struct AstChildren<N> {
72 inner: SyntaxNodeChildren,
76 impl<N> AstChildren<N> {
77 fn new(parent: &SyntaxNode) -> Self {
78 AstChildren { inner: parent.children(), ph: PhantomData }
82 impl<N: AstNode> Iterator for AstChildren<N> {
84 fn next(&mut self) -> Option<N> {
85 self.inner.find_map(N::cast)
90 use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken};
92 pub(super) fn child<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
93 parent.children().find_map(N::cast)
96 pub(super) fn children<N: AstNode>(parent: &SyntaxNode) -> AstChildren<N> {
97 AstChildren::new(parent)
100 pub(super) fn token(parent: &SyntaxNode, kind: SyntaxKind) -> Option<SyntaxToken> {
101 parent.children_with_tokens().filter_map(|it| it.into_token()).find(|it| it.kind() == kind)
106 fn assert_ast_is_object_safe() {
107 fn _f(_: &dyn AstNode, _: &dyn NameOwner) {}
111 fn test_doc_comment_none() {
112 let file = SourceFile::parse(
120 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
121 assert!(module.doc_comments().doc_comment_text().is_none());
125 fn test_outer_doc_comment_of_items() {
126 let file = SourceFile::parse(
135 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
136 assert_eq!(" doc", module.doc_comments().doc_comment_text().unwrap());
140 fn test_inner_doc_comment_of_items() {
141 let file = SourceFile::parse(
150 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
151 assert!(module.doc_comments().doc_comment_text().is_none());
155 fn test_doc_comment_of_statics() {
156 let file = SourceFile::parse(
159 static LEVELS: i32 = 0;
164 let st = file.syntax().descendants().find_map(Static::cast).unwrap();
165 assert_eq!(" Number of levels", st.doc_comments().doc_comment_text().unwrap());
169 fn test_doc_comment_preserves_indents() {
170 let file = SourceFile::parse(
183 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
185 " doc1\n ```\n fn foo() {\n // ...\n }\n ```",
186 module.doc_comments().doc_comment_text().unwrap()
191 fn test_doc_comment_preserves_newlines() {
192 let file = SourceFile::parse(
203 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
204 assert_eq!(" this\n is\n mod\n foo", module.doc_comments().doc_comment_text().unwrap());
208 fn test_doc_comment_single_line_block_strips_suffix() {
209 let file = SourceFile::parse(
211 /** this is mod foo*/
217 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
218 assert_eq!(" this is mod foo", module.doc_comments().doc_comment_text().unwrap());
222 fn test_doc_comment_single_line_block_strips_suffix_whitespace() {
223 let file = SourceFile::parse(
225 /** this is mod foo */
231 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
232 assert_eq!(" this is mod foo ", module.doc_comments().doc_comment_text().unwrap());
236 fn test_doc_comment_multi_line_block_strips_suffix() {
237 let file = SourceFile::parse(
249 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
251 "\n this\n is\n mod foo\n ",
252 module.doc_comments().doc_comment_text().unwrap()
257 fn test_comments_preserve_trailing_whitespace() {
258 let file = SourceFile::parse(
259 "\n/// Representation of a Realm. \n/// In the specification these are called Realm Records.\nstruct Realm {}",
263 let def = file.syntax().descendants().find_map(Struct::cast).unwrap();
265 " Representation of a Realm. \n In the specification these are called Realm Records.",
266 def.doc_comments().doc_comment_text().unwrap()
271 fn test_four_slash_line_comment() {
272 let file = SourceFile::parse(
274 //// too many slashes to be a doc comment
281 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
282 assert_eq!(" doc comment", module.doc_comments().doc_comment_text().unwrap());
286 fn test_where_predicates() {
287 fn assert_bound(text: &str, bound: Option<TypeBound>) {
288 assert_eq!(text, bound.unwrap().syntax().text().to_string());
291 let file = SourceFile::parse(
295 T: Clone + Copy + Debug + 'static,
297 Iterator::Item: 'a + Debug,
298 Iterator::Item: Debug + 'a,
299 <T as Iterator>::Item: Debug + 'a,
300 for<'a> F: Fn(&'a str)
306 let where_clause = file.syntax().descendants().find_map(WhereClause::cast).unwrap();
308 let mut predicates = where_clause.predicates();
310 let pred = predicates.next().unwrap();
311 let mut bounds = pred.type_bound_list().unwrap().bounds();
313 assert!(pred.for_token().is_none());
314 assert!(pred.generic_param_list().is_none());
315 assert_eq!("T", pred.ty().unwrap().syntax().text().to_string());
316 assert_bound("Clone", bounds.next());
317 assert_bound("Copy", bounds.next());
318 assert_bound("Debug", bounds.next());
319 assert_bound("'static", bounds.next());
321 let pred = predicates.next().unwrap();
322 let mut bounds = pred.type_bound_list().unwrap().bounds();
324 assert_eq!("'a", pred.lifetime().unwrap().lifetime_ident_token().unwrap().text());
326 assert_bound("'b", bounds.next());
327 assert_bound("'c", bounds.next());
329 let pred = predicates.next().unwrap();
330 let mut bounds = pred.type_bound_list().unwrap().bounds();
332 assert_eq!("Iterator::Item", pred.ty().unwrap().syntax().text().to_string());
333 assert_bound("'a", bounds.next());
335 let pred = predicates.next().unwrap();
336 let mut bounds = pred.type_bound_list().unwrap().bounds();
338 assert_eq!("Iterator::Item", pred.ty().unwrap().syntax().text().to_string());
339 assert_bound("Debug", bounds.next());
340 assert_bound("'a", bounds.next());
342 let pred = predicates.next().unwrap();
343 let mut bounds = pred.type_bound_list().unwrap().bounds();
345 assert_eq!("<T as Iterator>::Item", pred.ty().unwrap().syntax().text().to_string());
346 assert_bound("Debug", bounds.next());
347 assert_bound("'a", bounds.next());
349 let pred = predicates.next().unwrap();
350 let mut bounds = pred.type_bound_list().unwrap().bounds();
352 assert!(pred.for_token().is_some());
353 assert_eq!("<'a>", pred.generic_param_list().unwrap().syntax().text().to_string());
354 assert_eq!("F", pred.ty().unwrap().syntax().text().to_string());
355 assert_bound("Fn(&'a str)", bounds.next());