gdscript-syntax 0.1.0

Lexer, indentation pre-pass, and a lossless (cstree) parser for GDScript — gdscript-analyzer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! WS4 — the typed AST.
//!
//! A thin, zero-cost typed *view* over the lossless CST (the rust-analyzer model). An
//! AST node just wraps a red [`GdNode`] of the matching [`SyntaxKind`]; accessors are
//! filtered child lookups. No data is copied. Because [`GdNode`] is a *resolved* node
//! (it carries the interner), text accessors are clean — `Name::text()` needs no extra
//! resolver argument.

use cstree::util::NodeOrToken;

use crate::SyntaxKind;
use crate::syntax_kind::{GdNode, GdToken};

/// A typed node: a checked view over a red node of one [`SyntaxKind`].
pub trait AstNode: Sized {
    /// Whether a node of `kind` can be viewed as `Self`.
    fn can_cast(kind: SyntaxKind) -> bool;
    /// View `node` as `Self`, if its kind matches.
    fn cast(node: GdNode) -> Option<Self>;
    /// The underlying red node.
    fn syntax(&self) -> &GdNode;
}

/// Generate a single-kind AST wrapper struct + its [`AstNode`] impl.
macro_rules! ast_node {
    ($(#[$meta:meta])* $name:ident) => {
        $(#[$meta])*
        #[derive(Debug, Clone)]
        pub struct $name(GdNode);

        impl AstNode for $name {
            fn can_cast(kind: SyntaxKind) -> bool {
                kind == SyntaxKind::$name
            }
            fn cast(node: GdNode) -> Option<Self> {
                if node.kind() == SyntaxKind::$name {
                    Some(Self(node))
                } else {
                    None
                }
            }
            fn syntax(&self) -> &GdNode {
                &self.0
            }
        }
    };
}

ast_node!(
    /// The whole file.
    SourceFile
);
ast_node!(ClassNameDecl);
ast_node!(ExtendsClause);
ast_node!(Annotation);
ast_node!(FuncDecl);
ast_node!(VarDecl);
ast_node!(ConstDecl);
ast_node!(EnumDecl);
ast_node!(EnumVariant);
ast_node!(SignalDecl);
ast_node!(InnerClassDecl);
ast_node!(ClassBody);
ast_node!(ParamList);
ast_node!(Param);
ast_node!(Block);
ast_node!(TypeRef);
ast_node!(
    /// A declaration's name (wraps the declared identifier).
    Name
);

// ---- generic navigation helpers -------------------------------------------------

/// The first child node castable to `N`.
fn child<N: AstNode>(node: &GdNode) -> Option<N> {
    node.children().find_map(|c| N::cast(c.clone()))
}

/// All child nodes castable to `N`.
fn children<N: AstNode>(node: &GdNode) -> impl Iterator<Item = N> + '_ {
    node.children().filter_map(|c| N::cast(c.clone()))
}

/// Whether `node` has a direct child token of `kind`.
fn has_token(node: &GdNode, kind: SyntaxKind) -> bool {
    node.children_with_tokens()
        .filter_map(NodeOrToken::into_token)
        .any(|t| t.kind() == kind)
}

/// The text of the first direct child token of `kind`.
fn token_text(node: &GdNode, kind: SyntaxKind) -> Option<String> {
    node.children_with_tokens()
        .filter_map(NodeOrToken::into_token)
        .find(|t| t.kind() == kind)
        .map(|t| t.text().to_owned())
}

// ---- accessors ------------------------------------------------------------------

impl Name {
    /// The identifier text.
    #[must_use]
    pub fn text(&self) -> Option<String> {
        token_text(&self.0, SyntaxKind::Ident)
    }
}

impl SourceFile {
    /// The top-level declarations, in source order.
    pub fn decls(&self) -> impl Iterator<Item = Decl> + '_ {
        self.0.children().filter_map(|c| Decl::cast(c.clone()))
    }
}

impl FuncDecl {
    /// The function name.
    #[must_use]
    pub fn name(&self) -> Option<Name> {
        child(&self.0)
    }
    /// The parameter list.
    #[must_use]
    pub fn param_list(&self) -> Option<ParamList> {
        child(&self.0)
    }
    /// The body block.
    #[must_use]
    pub fn body(&self) -> Option<Block> {
        child(&self.0)
    }
    /// The declared return type, if any (the `TypeRef` after `->`).
    #[must_use]
    pub fn return_type(&self) -> Option<TypeRef> {
        child(&self.0)
    }
    /// Whether this is a `static func`.
    #[must_use]
    pub fn is_static(&self) -> bool {
        has_token(&self.0, SyntaxKind::StaticKw)
    }
}

impl ParamList {
    /// The parameters (excludes vararg rest params).
    pub fn params(&self) -> impl Iterator<Item = Param> + '_ {
        children(&self.0)
    }
}

impl Param {
    /// The parameter name.
    #[must_use]
    pub fn name(&self) -> Option<Name> {
        child(&self.0)
    }
    /// The declared type, if any.
    #[must_use]
    pub fn type_ref(&self) -> Option<TypeRef> {
        child(&self.0)
    }
}

impl VarDecl {
    /// The variable name.
    #[must_use]
    pub fn name(&self) -> Option<Name> {
        child(&self.0)
    }
    /// The declared type, if any.
    #[must_use]
    pub fn type_ref(&self) -> Option<TypeRef> {
        child(&self.0)
    }
    /// Whether this is a `static var`.
    #[must_use]
    pub fn is_static(&self) -> bool {
        has_token(&self.0, SyntaxKind::StaticKw)
    }
}

impl ConstDecl {
    /// The constant name.
    #[must_use]
    pub fn name(&self) -> Option<Name> {
        child(&self.0)
    }
}

impl EnumDecl {
    /// The enum's name, if it is a named enum.
    #[must_use]
    pub fn name(&self) -> Option<Name> {
        child(&self.0)
    }
    /// The enum variants.
    pub fn variants(&self) -> impl Iterator<Item = EnumVariant> + '_ {
        children(&self.0)
    }
}

impl EnumVariant {
    /// The variant name.
    #[must_use]
    pub fn text(&self) -> Option<String> {
        token_text(&self.0, SyntaxKind::Ident)
    }
}

impl SignalDecl {
    /// The signal name.
    #[must_use]
    pub fn name(&self) -> Option<Name> {
        child(&self.0)
    }
    /// The typed parameter list, if any.
    #[must_use]
    pub fn param_list(&self) -> Option<ParamList> {
        child(&self.0)
    }
}

impl ClassNameDecl {
    /// The registered global class name.
    #[must_use]
    pub fn name(&self) -> Option<Name> {
        child(&self.0)
    }
}

impl InnerClassDecl {
    /// The inner class name.
    #[must_use]
    pub fn name(&self) -> Option<Name> {
        child(&self.0)
    }
    /// The class body (its members), if present.
    #[must_use]
    pub fn body(&self) -> Option<ClassBody> {
        child(&self.0)
    }
}

impl ClassBody {
    /// The member declarations.
    pub fn decls(&self) -> impl Iterator<Item = Decl> + '_ {
        self.0.children().filter_map(|c| Decl::cast(c.clone()))
    }
}

impl Annotation {
    /// The annotation name (the identifier after `@`).
    #[must_use]
    pub fn name(&self) -> Option<String> {
        token_text(&self.0, SyntaxKind::Ident)
    }
}

impl TypeRef {
    /// The leading type identifier (e.g. `int`, `Array`).
    #[must_use]
    pub fn text(&self) -> Option<String> {
        self.0
            .children_with_tokens()
            .filter_map(NodeOrToken::into_token)
            .find(|t| matches!(t.kind(), SyntaxKind::Ident | SyntaxKind::VoidKw))
            .map(|t| t.text().to_owned())
    }
}

/// Any top-level or class-body declaration — the unit `document_symbols` iterates.
#[derive(Debug, Clone)]
pub enum Decl {
    /// `class_name X`
    ClassName(ClassNameDecl),
    /// `func f(...)`
    Func(FuncDecl),
    /// `var x`
    Var(VarDecl),
    /// `const X`
    Const(ConstDecl),
    /// `enum E { ... }`
    Enum(EnumDecl),
    /// `signal s`
    Signal(SignalDecl),
    /// `class Inner: ...`
    Class(InnerClassDecl),
}

impl Decl {
    /// View a node as a declaration, if it is one.
    #[must_use]
    pub fn cast(node: GdNode) -> Option<Self> {
        match node.kind() {
            SyntaxKind::ClassNameDecl => ClassNameDecl::cast(node).map(Self::ClassName),
            SyntaxKind::FuncDecl => FuncDecl::cast(node).map(Self::Func),
            SyntaxKind::VarDecl => VarDecl::cast(node).map(Self::Var),
            SyntaxKind::ConstDecl => ConstDecl::cast(node).map(Self::Const),
            SyntaxKind::EnumDecl => EnumDecl::cast(node).map(Self::Enum),
            SyntaxKind::SignalDecl => SignalDecl::cast(node).map(Self::Signal),
            SyntaxKind::InnerClassDecl => InnerClassDecl::cast(node).map(Self::Class),
            _ => None,
        }
    }

    /// The declaration's underlying node.
    #[must_use]
    pub fn syntax(&self) -> &GdNode {
        match self {
            Self::ClassName(d) => d.syntax(),
            Self::Func(d) => d.syntax(),
            Self::Var(d) => d.syntax(),
            Self::Const(d) => d.syntax(),
            Self::Enum(d) => d.syntax(),
            Self::Signal(d) => d.syntax(),
            Self::Class(d) => d.syntax(),
        }
    }

    /// The declaration's name, if it has one.
    #[must_use]
    pub fn name(&self) -> Option<String> {
        let name = match self {
            Self::ClassName(d) => d.name(),
            Self::Func(d) => d.name(),
            Self::Var(d) => d.name(),
            Self::Const(d) => d.name(),
            Self::Enum(d) => d.name(),
            Self::Signal(d) => d.name(),
            Self::Class(d) => d.name(),
        };
        name.and_then(|n| n.text())
    }
}

/// A pre-order walk over every node in the tree (depth-first), for visitors that need
/// to inspect all declarations/blocks (e.g. folding ranges).
#[must_use]
pub fn descendants(root: &GdNode) -> Vec<GdNode> {
    let mut out = vec![root.clone()];
    for child in root.children() {
        out.extend(descendants(child));
    }
    out
}

/// The token (if any) at `offset`, right-biased — the completion-context probe.
#[must_use]
pub fn token_at(root: &GdNode, offset: text_size::TextSize) -> Option<GdToken> {
    root.token_at_offset(offset).right_biased()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse;

    #[test]
    fn func_accessors() {
        let parse = parse("static func add(a: int, b := 1) -> int:\n\treturn a + b\n");
        let file = SourceFile::cast(parse.syntax_node()).unwrap();
        let func = file
            .decls()
            .find_map(|d| match d {
                Decl::Func(f) => Some(f),
                _ => None,
            })
            .unwrap();
        assert!(func.is_static());
        assert_eq!(func.name().and_then(|n| n.text()).as_deref(), Some("add"));
        assert_eq!(
            func.return_type().and_then(|t| t.text()).as_deref(),
            Some("int")
        );
        let params: Vec<_> = func
            .param_list()
            .unwrap()
            .params()
            .filter_map(|p| p.name().and_then(|n| n.text()))
            .collect();
        assert_eq!(params, vec!["a", "b"]);
        assert!(func.body().is_some());
    }

    #[test]
    fn declarations_are_enumerated() {
        let parse = parse(
            "class_name Foo\nconst K = 1\nvar x: int\nsignal s\nenum E { A, B }\nfunc f():\n\tpass\nclass Inner:\n\tvar y = 2\n",
        );
        let file = SourceFile::cast(parse.syntax_node()).unwrap();
        let names: Vec<_> = file.decls().map(|d| d.name().unwrap_or_default()).collect();
        assert_eq!(names, vec!["Foo", "K", "x", "s", "E", "f", "Inner"]);
    }

    #[test]
    fn enum_variants_and_inner_class_members() {
        let parse =
            parse("enum E { A, B = 5, C }\nclass Inner:\n\tvar a = 1\n\tfunc m():\n\t\tpass\n");
        let file = SourceFile::cast(parse.syntax_node()).unwrap();

        let en = file
            .decls()
            .find_map(|d| match d {
                Decl::Enum(e) => Some(e),
                _ => None,
            })
            .unwrap();
        let variants: Vec<_> = en.variants().filter_map(|v| v.text()).collect();
        assert_eq!(variants, vec!["A", "B", "C"]);

        let inner = file
            .decls()
            .find_map(|d| match d {
                Decl::Class(c) => Some(c),
                _ => None,
            })
            .unwrap();
        let member_names: Vec<_> = inner
            .body()
            .unwrap()
            .decls()
            .map(|d| d.name().unwrap_or_default())
            .collect();
        assert_eq!(member_names, vec!["a", "m"]);
    }

    #[test]
    fn token_at_offset_finds_identifier() {
        let src = "var hello = 1\n";
        let parse = parse(src);
        let node = parse.syntax_node();
        // offset 5 is inside "hello" (chars 4..9).
        let tok = node
            .token_at_offset(text_size::TextSize::new(5))
            .right_biased()
            .unwrap();
        assert_eq!(tok.kind(), SyntaxKind::Ident);
        assert_eq!(tok.text(), "hello");
    }
}