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