squawk_syntax/ast.rs
1// via https://github.com/rust-lang/rust-analyzer/blob/d8887c0758bbd2d5f752d5bd405d4491e90e7ed6/crates/syntax/src/ast.rs
2//
3// Permission is hereby granted, free of charge, to any
4// person obtaining a copy of this software and associated
5// documentation files (the "Software"), to deal in the
6// Software without restriction, including without
7// limitation the rights to use, copy, modify, merge,
8// publish, distribute, sublicense, and/or sell copies of
9// the Software, and to permit persons to whom the Software
10// is furnished to do so, subject to the following
11// conditions:
12//
13// The above copyright notice and this permission notice
14// shall be included in all copies or substantial portions
15// of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25// DEALINGS IN THE SOFTWARE.
26
27mod generated;
28mod node_ext;
29mod nodes;
30mod support;
31mod traits;
32
33use std::marker::PhantomData;
34
35use crate::syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken};
36use squawk_parser::SyntaxKind;
37
38pub use self::{
39 generated::tokens::*,
40 nodes::*,
41 // node_ext::{
42 // AttrKind, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind,
43 // SlicePatComponents, StructKind, TraitOrAlias, TypeBoundKind, TypeOrConstParam,
44 // VisibilityKind,
45 // },
46 // operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
47 // token_ext::{CommentKind, CommentPlacement, CommentShape, IsString, QuoteOffsets, Radix},
48 traits::{HasCreateTable, HasParamList, HasWithClause, NameLike},
49};
50
51/// The main trait to go from untyped `SyntaxNode` to a typed ast. The
52/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
53/// the same representation: a pointer to the tree root and a pointer to the
54/// node itself.
55pub trait AstNode {
56 fn can_cast(kind: SyntaxKind) -> bool
57 where
58 Self: Sized;
59
60 fn cast(syntax: SyntaxNode) -> Option<Self>
61 where
62 Self: Sized;
63
64 fn syntax(&self) -> &SyntaxNode;
65 fn clone_for_update(&self) -> Self
66 where
67 Self: Sized,
68 {
69 Self::cast(self.syntax().clone_for_update()).unwrap()
70 }
71 fn clone_subtree(&self) -> Self
72 where
73 Self: Sized,
74 {
75 Self::cast(self.syntax().clone_subtree()).unwrap()
76 }
77}
78
79/// Like `AstNode`, but wraps tokens rather than interior nodes.
80pub trait AstToken {
81 fn can_cast(token: SyntaxKind) -> bool
82 where
83 Self: Sized;
84
85 fn cast(syntax: SyntaxToken) -> Option<Self>
86 where
87 Self: Sized;
88
89 fn syntax(&self) -> &SyntaxToken;
90
91 fn text(&self) -> &str {
92 self.syntax().text()
93 }
94}
95
96/// An iterator over `SyntaxNode` children of a particular AST type.
97#[derive(Debug, Clone)]
98pub struct AstChildren<N> {
99 inner: SyntaxNodeChildren,
100 ph: PhantomData<N>,
101}
102
103impl<N> AstChildren<N> {
104 fn new(parent: &SyntaxNode) -> Self {
105 AstChildren {
106 inner: parent.children(),
107 ph: PhantomData,
108 }
109 }
110}
111
112impl<N: AstNode> Iterator for AstChildren<N> {
113 type Item = N;
114 fn next(&mut self) -> Option<N> {
115 self.inner.find_map(N::cast)
116 }
117}