plotnik-compiler 0.3.2

Compiler for Plotnik query language (parser, analyzer, bytecode emitter)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Typed AST wrappers over CST nodes.
//!
//! Each struct wraps a `SyntaxNode` and provides typed accessors.
//! Cast is infallible for correct `SyntaxKind` - validation happens elsewhere.
//!
//! ## String Lifetime Limitation
//!
//! `SyntaxToken::text()` returns `&str` tied to the token's lifetime, not to the
//! source `&'q str`. This is a rowan design: tokens store interned strings, not
//! spans into the original source.
//!
//! When building data structures that need source-lifetime strings (e.g.,
//! `SymbolTable<'q>`), use [`token_src`] instead of `token.text()`.

use super::cst::{SyntaxKind, SyntaxNode, SyntaxToken};
use rowan::TextRange;

/// Extracts token text with source lifetime.
///
/// Use this instead of `token.text()` when you need `&'q str`.
pub fn token_src<'q>(token: &SyntaxToken, source: &'q str) -> &'q str {
    let range = token.text_range();
    &source[range.start().into()..range.end().into()]
}

macro_rules! ast_node {
    ($name:ident, $kind:ident) => {
        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
        pub struct $name(SyntaxNode);

        impl $name {
            pub fn cast(node: SyntaxNode) -> Option<Self> {
                Self::can_cast(node.kind()).then(|| Self(node))
            }

            pub fn can_cast(kind: SyntaxKind) -> bool {
                kind == SyntaxKind::$kind
            }

            pub fn as_cst(&self) -> &SyntaxNode {
                &self.0
            }

            pub fn text_range(&self) -> TextRange {
                self.0.text_range()
            }
        }
    };
}

macro_rules! define_expr {
    ($($variant:ident),+ $(,)?) => {
        /// Expression: any pattern that can appear in the tree.
        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
        pub enum Expr {
            $($variant($variant)),+
        }

        impl Expr {
            pub fn cast(node: SyntaxNode) -> Option<Self> {
                let kind = node.kind();
                $(if $variant::can_cast(kind) { return Some(Expr::$variant($variant(node))); })+
                None
            }

            pub fn as_cst(&self) -> &SyntaxNode {
                match self { $(Expr::$variant(n) => n.as_cst()),+ }
            }

            pub fn text_range(&self) -> TextRange {
                match self { $(Expr::$variant(n) => n.text_range()),+ }
            }
        }
    };
}

impl Expr {
    /// Returns direct child expressions.
    pub fn children(&self) -> Vec<Expr> {
        match self {
            Expr::NamedNode(n) => n.children().collect(),
            Expr::SeqExpr(s) => s.children().collect(),
            Expr::CapturedExpr(c) => c.inner().into_iter().collect(),
            Expr::QuantifiedExpr(q) => q.inner().into_iter().collect(),
            Expr::FieldExpr(f) => f.value().into_iter().collect(),
            Expr::AltExpr(a) => a.branches().filter_map(|b| b.body()).collect(),
            Expr::Ref(_) | Expr::AnonymousNode(_) => vec![],
        }
    }
}

ast_node!(Root, Root);
ast_node!(Def, Def);
ast_node!(NamedNode, Tree);
ast_node!(Ref, Ref);
ast_node!(AltExpr, Alt);
ast_node!(Branch, Branch);
ast_node!(SeqExpr, Seq);
ast_node!(CapturedExpr, Capture);
ast_node!(Type, Type);
ast_node!(QuantifiedExpr, Quantifier);
ast_node!(FieldExpr, Field);
ast_node!(NegatedField, NegatedField);
ast_node!(Anchor, Anchor);
ast_node!(NodePredicate, NodePredicate);
ast_node!(RegexLiteral, Regex);

/// Either an expression or an anchor in a sequence.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SeqItem {
    Expr(Expr),
    Anchor(Anchor),
}

impl SeqItem {
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        if let Some(expr) = Expr::cast(node.clone()) {
            return Some(SeqItem::Expr(expr));
        }
        if let Some(anchor) = Anchor::cast(node) {
            return Some(SeqItem::Anchor(anchor));
        }
        None
    }

    pub fn as_anchor(&self) -> Option<&Anchor> {
        match self {
            SeqItem::Anchor(a) => Some(a),
            _ => None,
        }
    }

    pub fn as_expr(&self) -> Option<&Expr> {
        match self {
            SeqItem::Expr(e) => Some(e),
            _ => None,
        }
    }
}

/// Anonymous node: string literal (`"+"`) or wildcard (`_`).
/// Maps from CST `Str` or `Wildcard`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AnonymousNode(SyntaxNode);

impl AnonymousNode {
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        Self::can_cast(node.kind()).then(|| Self(node))
    }

    pub fn can_cast(kind: SyntaxKind) -> bool {
        matches!(kind, SyntaxKind::Str | SyntaxKind::Wildcard)
    }

    pub fn as_cst(&self) -> &SyntaxNode {
        &self.0
    }

    pub fn text_range(&self) -> TextRange {
        self.0.text_range()
    }

    /// Returns the string value if this is a literal, `None` if wildcard.
    pub fn value(&self) -> Option<SyntaxToken> {
        if self.0.kind() == SyntaxKind::Wildcard {
            return None;
        }
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| t.kind() == SyntaxKind::StrVal)
    }

    /// Returns true if this is the "any" wildcard (`_`).
    pub fn is_any(&self) -> bool {
        self.0.kind() == SyntaxKind::Wildcard
    }
}

/// Whether an alternation uses tagged or untagged branches.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AltKind {
    /// All branches have labels: `[A: expr1 B: expr2]`
    Tagged,
    /// No branches have labels: `[expr1 expr2]`
    Untagged,
    /// Mixed tagged and untagged branches (invalid)
    Mixed,
}

// Re-export PredicateOp from bytecode crate
pub use plotnik_bytecode::PredicateOp;

/// Convert SyntaxKind to PredicateOp.
pub fn predicate_op_from_syntax_kind(kind: SyntaxKind) -> Option<PredicateOp> {
    match kind {
        SyntaxKind::OpEq => Some(PredicateOp::Eq),
        SyntaxKind::OpNe => Some(PredicateOp::Ne),
        SyntaxKind::OpStartsWith => Some(PredicateOp::StartsWith),
        SyntaxKind::OpEndsWith => Some(PredicateOp::EndsWith),
        SyntaxKind::OpContains => Some(PredicateOp::Contains),
        SyntaxKind::OpRegexMatch => Some(PredicateOp::RegexMatch),
        SyntaxKind::OpRegexNoMatch => Some(PredicateOp::RegexNoMatch),
        _ => None,
    }
}

/// Predicate value: either a string or a regex pattern.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PredicateValue<'q> {
    /// String literal value
    String(&'q str),
    /// Regex pattern (the content between `/` delimiters)
    Regex(&'q str),
}

define_expr!(
    NamedNode,
    Ref,
    AnonymousNode,
    AltExpr,
    SeqExpr,
    CapturedExpr,
    QuantifiedExpr,
    FieldExpr,
);

impl Root {
    pub fn defs(&self) -> impl Iterator<Item = Def> + '_ {
        self.0.children().filter_map(Def::cast)
    }

    pub fn exprs(&self) -> impl Iterator<Item = Expr> + '_ {
        self.0.children().filter_map(Expr::cast)
    }
}

impl Def {
    pub fn name(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| t.kind() == SyntaxKind::Id)
    }

    pub fn body(&self) -> Option<Expr> {
        self.0.children().find_map(Expr::cast)
    }
}

impl NamedNode {
    pub fn node_type(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| {
                matches!(
                    t.kind(),
                    SyntaxKind::Id
                        | SyntaxKind::Underscore
                        | SyntaxKind::KwError
                        | SyntaxKind::KwMissing
                )
            })
    }

    /// Returns true if the node type is wildcard (`_`), matching any named node.
    pub fn is_any(&self) -> bool {
        self.node_type()
            .map(|t| t.kind() == SyntaxKind::Underscore)
            .unwrap_or(false)
    }

    /// Returns true if this is a MISSING node: `(MISSING ...)`.
    pub fn is_missing(&self) -> bool {
        self.node_type()
            .map(|t| t.kind() == SyntaxKind::KwMissing)
            .unwrap_or(false)
    }

    /// For MISSING nodes, returns the inner type constraint if present.
    ///
    /// `(MISSING identifier)` → Some("identifier")
    /// `(MISSING ";")` → Some(";")
    /// `(MISSING)` → None
    pub fn missing_constraint(&self) -> Option<SyntaxToken> {
        if !self.is_missing() {
            return None;
        }
        // After KwMissing, look for Id or StrVal token
        let mut found_missing = false;
        for child in self.0.children_with_tokens() {
            if let Some(token) = child.into_token() {
                if token.kind() == SyntaxKind::KwMissing {
                    found_missing = true;
                } else if found_missing
                    && matches!(token.kind(), SyntaxKind::Id | SyntaxKind::StrVal)
                {
                    return Some(token);
                }
            }
        }
        None
    }

    pub fn children(&self) -> impl Iterator<Item = Expr> + '_ {
        self.0.children().filter_map(Expr::cast)
    }

    /// Returns all anchors in this node.
    pub fn anchors(&self) -> impl Iterator<Item = Anchor> + '_ {
        self.0.children().filter_map(Anchor::cast)
    }

    /// Returns children interleaved with anchors, preserving order.
    pub fn items(&self) -> impl Iterator<Item = SeqItem> + '_ {
        self.0.children().filter_map(SeqItem::cast)
    }

    /// Returns the predicate if present: `(identifier == "foo")`.
    pub fn predicate(&self) -> Option<NodePredicate> {
        self.0.children().find_map(NodePredicate::cast)
    }
}

impl NodePredicate {
    /// Returns the operator token.
    pub fn operator_token(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| predicate_op_from_syntax_kind(t.kind()).is_some())
    }

    /// Returns the operator kind.
    pub fn operator(&self) -> Option<PredicateOp> {
        self.operator_token()
            .and_then(|t| predicate_op_from_syntax_kind(t.kind()))
    }

    /// Returns the string value if the predicate uses a string.
    pub fn string_value(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| t.kind() == SyntaxKind::StrVal)
    }

    /// Returns the regex literal if the predicate uses a regex.
    pub fn regex(&self) -> Option<RegexLiteral> {
        self.0.children().find_map(RegexLiteral::cast)
    }

    /// Returns the predicate value (string or regex pattern).
    pub fn value<'q>(&self, source: &'q str) -> Option<PredicateValue<'q>> {
        if let Some(str_token) = self.string_value() {
            return Some(PredicateValue::String(token_src(&str_token, source)));
        }
        if let Some(regex) = self.regex() {
            return Some(PredicateValue::Regex(regex.pattern(source)));
        }
        None
    }
}

impl RegexLiteral {
    /// Returns the regex pattern content (between the `/` delimiters).
    pub fn pattern<'q>(&self, source: &'q str) -> &'q str {
        let range = self.0.text_range();
        let text = &source[usize::from(range.start())..usize::from(range.end())];

        let Some(without_prefix) = text.strip_prefix('/') else {
            return text;
        };
        without_prefix.strip_suffix('/').unwrap_or(without_prefix)
    }
}

impl Ref {
    pub fn name(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| t.kind() == SyntaxKind::Id)
    }
}

impl AltExpr {
    pub fn kind(&self) -> AltKind {
        let mut tagged = false;
        let mut untagged = false;

        for child in self.0.children().filter(|c| c.kind() == SyntaxKind::Branch) {
            let has_label = child
                .children_with_tokens()
                .filter_map(|it| it.into_token())
                .any(|t| t.kind() == SyntaxKind::Id);

            if has_label {
                tagged = true;
            } else {
                untagged = true;
            }
        }

        match (tagged, untagged) {
            (true, true) => AltKind::Mixed,
            (true, false) => AltKind::Tagged,
            _ => AltKind::Untagged,
        }
    }

    pub fn branches(&self) -> impl Iterator<Item = Branch> + '_ {
        self.0.children().filter_map(Branch::cast)
    }

    pub fn exprs(&self) -> impl Iterator<Item = Expr> + '_ {
        self.0.children().filter_map(Expr::cast)
    }
}

impl Branch {
    pub fn label(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| t.kind() == SyntaxKind::Id)
    }

    pub fn body(&self) -> Option<Expr> {
        self.0.children().find_map(Expr::cast)
    }
}

impl SeqExpr {
    pub fn children(&self) -> impl Iterator<Item = Expr> + '_ {
        self.0.children().filter_map(Expr::cast)
    }

    /// Returns all anchors in this sequence.
    pub fn anchors(&self) -> impl Iterator<Item = Anchor> + '_ {
        self.0.children().filter_map(Anchor::cast)
    }

    /// Returns children interleaved with anchors, preserving order.
    pub fn items(&self) -> impl Iterator<Item = SeqItem> + '_ {
        self.0.children().filter_map(SeqItem::cast)
    }
}

impl CapturedExpr {
    /// Returns the capture token (@name or @_name).
    /// The token text includes the @ prefix.
    pub fn name(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| {
                matches!(
                    t.kind(),
                    SyntaxKind::CaptureToken | SyntaxKind::SuppressiveCapture
                )
            })
    }

    /// Returns true if this is a suppressive capture (@_ or @_name).
    /// Suppressive captures match structurally but don't contribute to output.
    pub fn is_suppressive(&self) -> bool {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .any(|t| t.kind() == SyntaxKind::SuppressiveCapture)
    }

    pub fn inner(&self) -> Option<Expr> {
        self.0.children().find_map(Expr::cast)
    }

    pub fn type_annotation(&self) -> Option<Type> {
        self.0.children().find_map(Type::cast)
    }

    /// Returns true if this capture has a `:: string` type annotation.
    pub fn has_string_annotation(&self) -> bool {
        self.type_annotation()
            .is_some_and(|t| t.name().is_some_and(|n| n.text() == "string"))
    }
}

impl Type {
    pub fn name(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| t.kind() == SyntaxKind::Id)
    }
}

impl QuantifiedExpr {
    pub fn inner(&self) -> Option<Expr> {
        self.0.children().find_map(Expr::cast)
    }

    pub fn operator(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| {
                matches!(
                    t.kind(),
                    SyntaxKind::Star
                        | SyntaxKind::Plus
                        | SyntaxKind::Question
                        | SyntaxKind::StarQuestion
                        | SyntaxKind::PlusQuestion
                        | SyntaxKind::QuestionQuestion
                )
            })
    }

    /// Returns true if quantifier allows zero matches (?, *, ??, *?).
    pub fn is_optional(&self) -> bool {
        self.operator()
            .map(|op| {
                matches!(
                    op.kind(),
                    SyntaxKind::Question
                        | SyntaxKind::Star
                        | SyntaxKind::QuestionQuestion
                        | SyntaxKind::StarQuestion
                )
            })
            .unwrap_or(false)
    }
}

impl FieldExpr {
    pub fn name(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| t.kind() == SyntaxKind::Id)
    }

    pub fn value(&self) -> Option<Expr> {
        self.0.children().find_map(Expr::cast)
    }
}

impl NegatedField {
    pub fn name(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|it| it.into_token())
            .find(|t| t.kind() == SyntaxKind::Id)
    }
}

/// Checks if expression is a truly empty scope (sequence/alternation with no children).
/// Used to distinguish `{ } @x` (empty struct) from `{(expr) @_} @x` (Node capture).
pub fn is_truly_empty_scope(inner: &Expr) -> bool {
    match inner {
        Expr::SeqExpr(seq) => seq.children().next().is_none(),
        Expr::AltExpr(alt) => alt.branches().next().is_none(),
        Expr::QuantifiedExpr(q) => q.inner().is_some_and(|i| is_truly_empty_scope(&i)),
        _ => false,
    }
}