Skip to main content

squawk_syntax/
ptr.rs

1// via https://github.com/rust-lang/rust-analyzer/blob/2efc80078029894eec0699f62ec8d5c1a56af763/crates/syntax/src/ptr.rs#L22C19-L22C19
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
27//! In squawk, syntax trees are transient objects.
28//!
29//! That means that we create trees when we need them, and tear them down to
30//! save memory. In this architecture, hanging on to a particular syntax node
31//! for a long time is ill-advisable, as that keeps the whole tree resident.
32//!
33//! Instead, we provide a [`SyntaxNodePtr`] type, which stores information about
34//! *location* of a particular syntax node in a tree. Its a small type which can
35//! be cheaply stored, and which can be resolved to a real [`SyntaxNode`] when
36//! necessary.
37
38use std::{
39    hash::{Hash, Hasher},
40    marker::PhantomData,
41};
42
43use rowan::TextRange;
44
45use crate::{AstNode, SyntaxNode, syntax_node::Sql};
46
47/// A "pointer" to a [`SyntaxNode`], via location in the source code.
48pub type SyntaxNodePtr = rowan::ast::SyntaxNodePtr<Sql>;
49
50/// Like [`SyntaxNodePtr`], but remembers the type of node.
51pub struct AstPtr<N: AstNode> {
52    raw: SyntaxNodePtr,
53    _ty: PhantomData<fn() -> N>,
54}
55
56impl<N: AstNode> std::fmt::Debug for AstPtr<N> {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        f.debug_tuple("AstPtr").field(&self.raw).finish()
59    }
60}
61
62impl<N: AstNode> Copy for AstPtr<N> {}
63impl<N: AstNode> Clone for AstPtr<N> {
64    fn clone(&self) -> AstPtr<N> {
65        *self
66    }
67}
68
69impl<N: AstNode> Eq for AstPtr<N> {}
70
71impl<N: AstNode> PartialEq for AstPtr<N> {
72    fn eq(&self, other: &AstPtr<N>) -> bool {
73        self.raw == other.raw
74    }
75}
76
77impl<N: AstNode> Hash for AstPtr<N> {
78    fn hash<H: Hasher>(&self, state: &mut H) {
79        self.raw.hash(state);
80    }
81}
82
83impl<N: AstNode> AstPtr<N> {
84    pub fn new(node: &N) -> AstPtr<N> {
85        AstPtr {
86            raw: SyntaxNodePtr::new(node.syntax()),
87            _ty: PhantomData,
88        }
89    }
90
91    pub fn to_node(&self, root: &SyntaxNode) -> N {
92        let syntax_node = self.raw.to_node(root);
93        N::cast(syntax_node).unwrap()
94    }
95
96    pub fn syntax_node_ptr(&self) -> SyntaxNodePtr {
97        self.raw
98    }
99
100    pub fn text_range(&self) -> TextRange {
101        self.raw.text_range()
102    }
103
104    pub fn cast<U: AstNode>(self) -> Option<AstPtr<U>> {
105        if !U::can_cast(self.raw.kind()) {
106            return None;
107        }
108        Some(AstPtr {
109            raw: self.raw,
110            _ty: PhantomData,
111        })
112    }
113
114    pub fn kind(&self) -> squawk_parser::SyntaxKind {
115        self.raw.kind()
116    }
117
118    pub fn upcast<M: AstNode>(self) -> AstPtr<M>
119    where
120        N: Into<M>,
121    {
122        AstPtr {
123            raw: self.raw,
124            _ty: PhantomData,
125        }
126    }
127
128    /// Like `SyntaxNodePtr::cast` but the trait bounds work out.
129    pub fn try_from_raw(raw: SyntaxNodePtr) -> Option<AstPtr<N>> {
130        N::can_cast(raw.kind()).then_some(AstPtr {
131            raw,
132            _ty: PhantomData,
133        })
134    }
135
136    pub fn wrap_left<R>(self) -> AstPtr<either::Either<N, R>>
137    where
138        either::Either<N, R>: AstNode,
139    {
140        AstPtr {
141            raw: self.raw,
142            _ty: PhantomData,
143        }
144    }
145
146    pub fn wrap_right<L>(self) -> AstPtr<either::Either<L, N>>
147    where
148        either::Either<L, N>: AstNode,
149    {
150        AstPtr {
151            raw: self.raw,
152            _ty: PhantomData,
153        }
154    }
155}
156
157impl<N: AstNode> From<AstPtr<N>> for SyntaxNodePtr {
158    fn from(ptr: AstPtr<N>) -> SyntaxNodePtr {
159        ptr.raw
160    }
161}
162
163#[test]
164fn test_local_syntax_ptr() {
165    use crate::{AstNode, ast};
166
167    let file = ast::SourceFile::parse("create table t(c int8);")
168        .ok()
169        .unwrap();
170    let field = file
171        .syntax()
172        .descendants()
173        .find_map(ast::Column::cast)
174        .unwrap();
175    let ptr = SyntaxNodePtr::new(field.syntax());
176    let field_syntax = ptr.to_node(file.syntax());
177    assert_eq!(field.syntax(), &field_syntax);
178}