Skip to main content

syntax/
ast.rs

1//! Abstract Syntax Tree, layered on top of untyped `SyntaxNode`s
2
3pub mod edit;
4mod expr_ext;
5mod generated;
6pub mod make;
7mod node_ext;
8mod operators;
9pub mod prec;
10pub mod syntax_factory;
11mod token_ext;
12mod traits;
13
14use std::marker::PhantomData;
15
16use either::Either;
17
18use crate::{
19    SyntaxKind,
20    syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken},
21};
22
23pub use self::{
24    expr_ext::{ArrayExprKind, BlockModifier, CallableExpr, ElseBranch, LiteralKind},
25    generated::{nodes::*, tokens::*},
26    node_ext::{
27        AttrKind, CfgAtomKey, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind,
28        SelfParamKind, SlicePatComponents, StructKind, TokenTreeChildren, TypeBoundKind,
29        TypeOrConstParam, VisibilityKind,
30    },
31    operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
32    token_ext::{AnyComment, AnyString, CommentKind, CommentShape, IsString, QuoteOffsets, Radix},
33    traits::{
34        AttrsIter, HasArgList, HasAttrs, HasGenericArgs, HasGenericParams, HasLoopBody,
35        HasModuleItem, HasName, HasTypeBounds, HasVisibility, attrs_including_inner,
36        attrs_with_doc_including_inner,
37    },
38};
39
40/// The main trait to go from untyped `SyntaxNode`  to a typed ast. The
41/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
42/// the same representation: a pointer to the tree root and a pointer to the
43/// node itself.
44pub trait AstNode {
45    /// This panics if the `SyntaxKind` is not statically known.
46    fn kind() -> SyntaxKind
47    where
48        Self: Sized,
49    {
50        panic!("dynamic `SyntaxKind` for `AstNode::kind()`")
51    }
52
53    fn can_cast(kind: SyntaxKind) -> bool
54    where
55        Self: Sized;
56
57    fn cast(syntax: SyntaxNode) -> Option<Self>
58    where
59        Self: Sized;
60
61    fn syntax(&self) -> &SyntaxNode;
62    fn clone_subtree(&self) -> Self
63    where
64        Self: Sized,
65    {
66        Self::cast(self.syntax().clone_subtree()).unwrap()
67    }
68}
69
70/// Like `AstNode`, but wraps tokens rather than interior nodes.
71pub trait AstToken {
72    fn can_cast(token: SyntaxKind) -> bool
73    where
74        Self: Sized;
75
76    fn cast(syntax: SyntaxToken) -> Option<Self>
77    where
78        Self: Sized;
79
80    fn syntax(&self) -> &SyntaxToken;
81
82    fn text(&self) -> &str {
83        self.syntax().text()
84    }
85}
86
87/// An iterator over `SyntaxNode` children of a particular AST type.
88#[derive(Debug, Clone)]
89pub struct AstChildren<N> {
90    inner: SyntaxNodeChildren,
91    ph: PhantomData<N>,
92}
93
94impl<N> AstChildren<N> {
95    fn new(parent: &SyntaxNode) -> Self {
96        AstChildren { inner: parent.children(), ph: PhantomData }
97    }
98}
99
100impl<N: AstNode> Iterator for AstChildren<N> {
101    type Item = N;
102    fn next(&mut self) -> Option<N> {
103        self.inner.find_map(N::cast)
104    }
105}
106
107impl<L, R> AstNode for Either<L, R>
108where
109    L: AstNode,
110    R: AstNode,
111{
112    fn can_cast(kind: SyntaxKind) -> bool
113    where
114        Self: Sized,
115    {
116        L::can_cast(kind) || R::can_cast(kind)
117    }
118
119    fn cast(syntax: SyntaxNode) -> Option<Self>
120    where
121        Self: Sized,
122    {
123        if L::can_cast(syntax.kind()) {
124            L::cast(syntax).map(Either::Left)
125        } else {
126            R::cast(syntax).map(Either::Right)
127        }
128    }
129
130    fn syntax(&self) -> &SyntaxNode {
131        self.as_ref().either(L::syntax, R::syntax)
132    }
133}
134
135impl<L, R> HasAttrs for Either<L, R>
136where
137    L: HasAttrs,
138    R: HasAttrs,
139{
140}
141
142/// Trait to describe operations common to both `RangeExpr` and `RangePat`.
143pub trait RangeItem {
144    type Bound;
145
146    fn start(&self) -> Option<Self::Bound>;
147    fn end(&self) -> Option<Self::Bound>;
148    fn op_kind(&self) -> Option<RangeOp>;
149    fn op_token(&self) -> Option<SyntaxToken>;
150}
151
152mod support {
153    use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken};
154
155    #[inline]
156    pub(super) fn child<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
157        parent.children().find_map(N::cast)
158    }
159
160    #[inline]
161    pub(super) fn children<N: AstNode>(parent: &SyntaxNode) -> AstChildren<N> {
162        AstChildren::new(parent)
163    }
164
165    #[inline]
166    pub(super) fn token(parent: &SyntaxNode, kind: SyntaxKind) -> Option<SyntaxToken> {
167        parent.children_with_tokens().filter_map(|it| it.into_token()).find(|it| it.kind() == kind)
168    }
169}
170
171#[cfg(test)]
172fn doc_comment_text(owner: impl HasAttrs) -> Option<std::string::String> {
173    use itertools::Itertools;
174
175    let docs = owner.doc_comments().map(|comment| comment.text().to_owned()).join("\n");
176    if docs.is_empty() { None } else { Some(docs) }
177}
178
179#[test]
180fn assert_ast_is_dyn_compatible() {
181    fn _f(_: &dyn AstNode, _: &dyn HasName) {}
182}
183
184#[test]
185fn test_doc_comment_none() {
186    let file = SourceFile::parse(
187        r#"
188        // non-doc
189        mod foo {}
190        "#,
191        parser::Edition::CURRENT,
192    )
193    .ok()
194    .unwrap();
195    let module = file.syntax().descendants().find_map(Module::cast).unwrap();
196    assert!(doc_comment_text(module).is_none());
197}
198
199#[test]
200fn test_outer_doc_comment_of_items() {
201    let file = SourceFile::parse(
202        r#"
203        /// doc
204        // non-doc
205        mod foo {}
206        "#,
207        parser::Edition::CURRENT,
208    )
209    .ok()
210    .unwrap();
211    let module = file.syntax().descendants().find_map(Module::cast).unwrap();
212    assert_eq!(" doc", doc_comment_text(module).unwrap());
213}
214
215#[test]
216fn test_inner_doc_comment_of_items() {
217    let file = SourceFile::parse(
218        r#"
219        //! doc
220        // non-doc
221        mod foo {}
222        "#,
223        parser::Edition::CURRENT,
224    )
225    .ok()
226    .unwrap();
227    let module = file.syntax().descendants().find_map(Module::cast).unwrap();
228    assert!(doc_comment_text(module).is_none());
229}
230
231#[test]
232fn test_doc_comment_of_statics() {
233    let file = SourceFile::parse(
234        r#"
235        /// Number of levels
236        static LEVELS: i32 = 0;
237        "#,
238        parser::Edition::CURRENT,
239    )
240    .ok()
241    .unwrap();
242    let st = file.syntax().descendants().find_map(Static::cast).unwrap();
243    assert_eq!(" Number of levels", doc_comment_text(st).unwrap());
244}
245
246#[test]
247fn test_doc_comment_preserves_indents() {
248    let file = SourceFile::parse(
249        r#"
250        /// doc1
251        /// ```
252        /// fn foo() {
253        ///     // ...
254        /// }
255        /// ```
256        mod foo {}
257        "#,
258        parser::Edition::CURRENT,
259    )
260    .ok()
261    .unwrap();
262    let module = file.syntax().descendants().find_map(Module::cast).unwrap();
263    assert_eq!(
264        " doc1\n ```\n fn foo() {\n     // ...\n }\n ```",
265        doc_comment_text(module).unwrap()
266    );
267}
268
269#[test]
270fn test_doc_comment_preserves_newlines() {
271    let file = SourceFile::parse(
272        r#"
273        /// this
274        /// is
275        /// mod
276        /// foo
277        mod foo {}
278        "#,
279        parser::Edition::CURRENT,
280    )
281    .ok()
282    .unwrap();
283    let module = file.syntax().descendants().find_map(Module::cast).unwrap();
284    assert_eq!(" this\n is\n mod\n foo", doc_comment_text(module).unwrap());
285}
286
287#[test]
288fn test_doc_comment_single_line_block_strips_suffix() {
289    let file = SourceFile::parse(
290        r#"
291        /** this is mod foo*/
292        mod foo {}
293        "#,
294        parser::Edition::CURRENT,
295    )
296    .ok()
297    .unwrap();
298    let module = file.syntax().descendants().find_map(Module::cast).unwrap();
299    assert_eq!(" this is mod foo", doc_comment_text(module).unwrap());
300}
301
302#[test]
303fn test_doc_comment_single_line_block_strips_suffix_whitespace() {
304    let file = SourceFile::parse(
305        r#"
306        /** this is mod foo */
307        mod foo {}
308        "#,
309        parser::Edition::CURRENT,
310    )
311    .ok()
312    .unwrap();
313    let module = file.syntax().descendants().find_map(Module::cast).unwrap();
314    assert_eq!(" this is mod foo ", doc_comment_text(module).unwrap());
315}
316
317#[test]
318fn test_doc_comment_multi_line_block_strips_suffix() {
319    let file = SourceFile::parse(
320        r#"
321        /**
322        this
323        is
324        mod foo
325        */
326        mod foo {}
327        "#,
328        parser::Edition::CURRENT,
329    )
330    .ok()
331    .unwrap();
332    let module = file.syntax().descendants().find_map(Module::cast).unwrap();
333    assert_eq!(
334        "\n        this\n        is\n        mod foo\n        ",
335        doc_comment_text(module).unwrap()
336    );
337}
338
339#[test]
340fn test_comments_preserve_trailing_whitespace() {
341    let file = SourceFile::parse(
342        "\n/// Representation of a Realm.   \n/// In the specification these are called Realm Records.\nstruct Realm {}", parser::Edition::CURRENT,
343    )
344    .ok()
345    .unwrap();
346    let def = file.syntax().descendants().find_map(Struct::cast).unwrap();
347    assert_eq!(
348        " Representation of a Realm.   \n In the specification these are called Realm Records.",
349        doc_comment_text(def).unwrap()
350    );
351}
352
353#[test]
354fn test_four_slash_line_comment() {
355    let file = SourceFile::parse(
356        r#"
357        //// too many slashes to be a doc comment
358        /// doc comment
359        mod foo {}
360        "#,
361        parser::Edition::CURRENT,
362    )
363    .ok()
364    .unwrap();
365    let module = file.syntax().descendants().find_map(Module::cast).unwrap();
366    assert_eq!(" doc comment", doc_comment_text(module).unwrap());
367}
368
369#[test]
370fn test_where_predicates() {
371    fn assert_bound(text: &str, bound: Option<TypeBound>) {
372        assert_eq!(text, bound.unwrap().syntax().text().to_string());
373    }
374
375    let file = SourceFile::parse(
376        r#"
377fn foo()
378where
379   T: Clone + Copy + Debug + 'static,
380   'a: 'b + 'c,
381   Iterator::Item: 'a + Debug,
382   Iterator::Item: Debug + 'a,
383   <T as Iterator>::Item: Debug + 'a,
384   for<'a> F: Fn(&'a str)
385{}
386        "#,
387        parser::Edition::CURRENT,
388    )
389    .ok()
390    .unwrap();
391    let where_clause = file.syntax().descendants().find_map(WhereClause::cast).unwrap();
392
393    let mut predicates = where_clause.predicates();
394
395    let pred = predicates.next().unwrap();
396    let mut bounds = pred.type_bound_list().unwrap().bounds();
397
398    assert!(pred.for_binder().is_none());
399    assert_eq!("T", pred.ty().unwrap().syntax().text().to_string());
400    assert_bound("Clone", bounds.next());
401    assert_bound("Copy", bounds.next());
402    assert_bound("Debug", bounds.next());
403    assert_bound("'static", bounds.next());
404
405    let pred = predicates.next().unwrap();
406    let mut bounds = pred.type_bound_list().unwrap().bounds();
407
408    assert_eq!("'a", pred.lifetime().unwrap().lifetime_ident_token().unwrap().text());
409
410    assert_bound("'b", bounds.next());
411    assert_bound("'c", bounds.next());
412
413    let pred = predicates.next().unwrap();
414    let mut bounds = pred.type_bound_list().unwrap().bounds();
415
416    assert_eq!("Iterator::Item", pred.ty().unwrap().syntax().text().to_string());
417    assert_bound("'a", bounds.next());
418
419    let pred = predicates.next().unwrap();
420    let mut bounds = pred.type_bound_list().unwrap().bounds();
421
422    assert_eq!("Iterator::Item", pred.ty().unwrap().syntax().text().to_string());
423    assert_bound("Debug", bounds.next());
424    assert_bound("'a", bounds.next());
425
426    let pred = predicates.next().unwrap();
427    let mut bounds = pred.type_bound_list().unwrap().bounds();
428
429    assert_eq!("<T as Iterator>::Item", pred.ty().unwrap().syntax().text().to_string());
430    assert_bound("Debug", bounds.next());
431    assert_bound("'a", bounds.next());
432
433    let pred = predicates.next().unwrap();
434    let mut bounds = pred.type_bound_list().unwrap().bounds();
435
436    assert_eq!(
437        "<'a>",
438        pred.for_binder().unwrap().generic_param_list().unwrap().syntax().text().to_string()
439    );
440    assert_eq!("F", pred.ty().unwrap().syntax().text().to_string());
441    assert_bound("Fn(&'a str)", bounds.next());
442}