Skip to main content

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    node_ext::{BinOp, LitKind},
41    nodes::*,
42    traits::{HasCreateTable, HasParamList, HasWithClause, NameLike},
43};
44
45/// The main trait to go from untyped `SyntaxNode`  to a typed ast. The
46/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
47/// the same representation: a pointer to the tree root and a pointer to the
48/// node itself.
49pub trait AstNode {
50    fn can_cast(kind: SyntaxKind) -> bool
51    where
52        Self: Sized;
53
54    fn cast(syntax: SyntaxNode) -> Option<Self>
55    where
56        Self: Sized;
57
58    fn syntax(&self) -> &SyntaxNode;
59    fn clone_for_update(&self) -> Self
60    where
61        Self: Sized,
62    {
63        Self::cast(self.syntax().clone_for_update()).unwrap()
64    }
65    fn clone_subtree(&self) -> Self
66    where
67        Self: Sized,
68    {
69        Self::cast(self.syntax().clone_subtree()).unwrap()
70    }
71}
72
73/// Like `AstNode`, but wraps tokens rather than interior nodes.
74pub trait AstToken {
75    fn can_cast(token: SyntaxKind) -> bool
76    where
77        Self: Sized;
78
79    fn cast(syntax: SyntaxToken) -> Option<Self>
80    where
81        Self: Sized;
82
83    fn syntax(&self) -> &SyntaxToken;
84
85    fn text(&self) -> &str {
86        self.syntax().text()
87    }
88}
89
90/// An iterator over `SyntaxNode` children of a particular AST type.
91#[derive(Debug, Clone)]
92pub struct AstChildren<N> {
93    inner: SyntaxNodeChildren,
94    ph: PhantomData<N>,
95}
96
97impl<N> AstChildren<N> {
98    fn new(parent: &SyntaxNode) -> Self {
99        AstChildren {
100            inner: parent.children(),
101            ph: PhantomData,
102        }
103    }
104}
105
106impl<N: AstNode> Iterator for AstChildren<N> {
107    type Item = N;
108    fn next(&mut self) -> Option<N> {
109        self.inner.find_map(N::cast)
110    }
111}