ronin-core 0.1.0

Lossless, error-tolerant RON concrete-syntax-tree engine for RONin (WASM-clean core).
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
569
570
571
572
573
574
575
//! Typed accessors over the CST (TR-010, OBJ4).
//!
//! The CST exposed by [`crate::syntax`] is *untyped*: every interior node is a
//! [`SyntaxNode`] tagged with a [`SyntaxKind`]. This module layers a thin,
//! zero-copy *typed* view on top of it so callers can navigate RON constructs by
//! name — `Struct::fields()`, `Map::entries()`, `MapEntry::key()`/`value()`,
//! `List::items()`, `Tuple::items()`, `EnumVariant::name()`, `Document::value()`
//! — without matching on `SyntaxKind` themselves.
//!
//! # Design
//!
//! Each typed wrapper is a newtype around a [`SyntaxNode`] of the matching kind.
//! Construction goes through `cast`, which returns `None` for a node of the
//! wrong kind, so a typed handle always refers to a node of its declared kind
//! (defensive: an `Error`-recovered tree never produces a mis-typed accessor).
//! Accessors return only `ronin-core` types — other typed wrappers, [`Value`],
//! [`SyntaxNode`], [`SyntaxToken`], or `&str` — so **no rowan type ever leaks**
//! (INV-7 / TR-009). The wrappers borrow the tree; they own nothing and copy no
//! source text.
//!
//! Trivia is transparent here: child *nodes* skip trivia tokens automatically
//! (trivia are leaf tokens, never nodes), so navigation is unaffected by
//! whitespace/comments while the underlying tree stays byte-lossless.

use crate::syntax::{SyntaxKind, SyntaxNode, SyntaxToken};

/// A typed RON value: the classified wrapper for any value-position node.
///
/// Returned by the value accessors ([`Document::value`], [`StructField::value`],
/// [`MapEntry::key`]/[`MapEntry::value`], and the `items()` iterators). The
/// `Error` / unknown arm keeps navigation total over error-recovered trees
/// (INV-3): a malformed value is still reachable as [`Value::Error`] rather than
/// silently dropped.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Value {
    /// A named or anonymous struct `Name( field: v, .. )` / `( field: v, .. )`.
    Struct(Struct),
    /// A positional tuple / tuple-struct `( a, b, c )`.
    Tuple(Tuple),
    /// A list / sequence `[ a, b, c ]`.
    List(List),
    /// A map `{ k: v, .. }` (keys may be non-string values).
    Map(Map),
    /// An enum variant: bare `Ident`, or `Ident(..)` / `Ident{..}` payload.
    EnumVariant(EnumVariant),
    /// The unit value `()`.
    Unit(Unit),
    /// A scalar literal (int, float, string, raw string, char, bool).
    Literal(Literal),
    /// An unparseable / recovered value node (`Error` kind). Kept reachable so
    /// navigation is total over error-recovered trees (INV-3).
    Error(SyntaxNode),
}

impl Value {
    /// Classify a value-position [`SyntaxNode`] into a typed [`Value`].
    ///
    /// Returns `None` only for a node whose kind is not a value (e.g. `Root`,
    /// `StructField`, `MapEntry`, `ExtensionAttr`) — callers that already hold a
    /// value-position node always get `Some`.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        Some(match node.kind() {
            SyntaxKind::Struct => Self::Struct(Struct(node)),
            SyntaxKind::Tuple => Self::Tuple(Tuple(node)),
            SyntaxKind::List => Self::List(List(node)),
            SyntaxKind::Map => Self::Map(Map(node)),
            SyntaxKind::EnumVariant => Self::EnumVariant(EnumVariant(node)),
            SyntaxKind::Unit => Self::Unit(Unit(node)),
            SyntaxKind::Literal => Self::Literal(Literal(node)),
            SyntaxKind::Error => Self::Error(node),
            _ => return None,
        })
    }

    /// The underlying [`SyntaxNode`], regardless of variant.
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        match self {
            Self::Struct(n) => n.syntax(),
            Self::Tuple(n) => n.syntax(),
            Self::List(n) => n.syntax(),
            Self::Map(n) => n.syntax(),
            Self::EnumVariant(n) => n.syntax(),
            Self::Unit(n) => n.syntax(),
            Self::Literal(n) => n.syntax(),
            Self::Error(n) => n,
        }
    }
}

/// Cast the first value-position child node of `parent` into a [`Value`].
///
/// Used by accessors that contain exactly one value (struct field, map-entry
/// key/value, document root). Skips trivia and non-value nodes (e.g. a struct
/// field's name is a token, not a node).
fn first_value_child(parent: &SyntaxNode) -> Option<Value> {
    parent.children().find_map(Value::cast)
}

/// The typed root of a parsed document.
///
/// Wraps the [`SyntaxKind::Root`] node and exposes the single top-level value
/// plus any leading extension attributes.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Document(SyntaxNode);

impl Document {
    /// Wrap a [`SyntaxKind::Root`] node, or `None` for any other kind.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::Root).then_some(Self(node))
    }

    /// The underlying root [`SyntaxNode`].
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        &self.0
    }

    /// The single top-level [`Value`], if the document has one (absent for an
    /// empty / trivia-only file).
    #[must_use]
    pub fn value(&self) -> Option<Value> {
        first_value_child(&self.0)
    }

    /// The leading extension attributes `#![enable(..)]`, in source order.
    pub fn extension_attrs(&self) -> impl Iterator<Item = ExtensionAttr> + '_ {
        self.0.children().filter_map(ExtensionAttr::cast)
    }
}

/// A named or anonymous struct: `Name( field: v, .. )` or `( field: v, .. )`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Struct(SyntaxNode);

impl Struct {
    /// Wrap a [`SyntaxKind::Struct`] node, or `None` for any other kind.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::Struct).then_some(Self(node))
    }

    /// The underlying [`SyntaxNode`].
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        &self.0
    }

    /// The struct's name token (`Ident`) for a named struct, or `None` for an
    /// anonymous `( field: v, .. )` struct.
    #[must_use]
    pub fn name(&self) -> Option<SyntaxToken> {
        self.0.first_token_of(SyntaxKind::Ident)
    }

    /// The struct's name as a string slice, if named.
    #[must_use]
    pub fn name_text(&self) -> Option<String> {
        self.name().map(|t| t.text().to_string())
    }

    /// The `field: value` entries, in source order.
    pub fn fields(&self) -> impl Iterator<Item = StructField> + '_ {
        self.0.children().filter_map(StructField::cast)
    }
}

/// A single `field: value` entry inside a [`Struct`].
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StructField(SyntaxNode);

impl StructField {
    /// Wrap a [`SyntaxKind::StructField`] node, or `None` for any other kind.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::StructField).then_some(Self(node))
    }

    /// The underlying [`SyntaxNode`].
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        &self.0
    }

    /// The field-name token (`Ident`), if present.
    #[must_use]
    pub fn name(&self) -> Option<SyntaxToken> {
        self.0.first_token_of(SyntaxKind::Ident)
    }

    /// The field name as a string, if present.
    #[must_use]
    pub fn name_text(&self) -> Option<String> {
        self.name().map(|t| t.text().to_string())
    }

    /// The field's [`Value`], if present (absent in a recovered partial field).
    #[must_use]
    pub fn value(&self) -> Option<Value> {
        first_value_child(&self.0)
    }
}

/// A positional tuple / tuple-struct `( a, b, c )`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Tuple(SyntaxNode);

impl Tuple {
    /// Wrap a [`SyntaxKind::Tuple`] node, or `None` for any other kind.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::Tuple).then_some(Self(node))
    }

    /// The underlying [`SyntaxNode`].
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        &self.0
    }

    /// The positional element [`Value`]s, in source order.
    pub fn items(&self) -> impl Iterator<Item = Value> + '_ {
        self.0.children().filter_map(Value::cast)
    }
}

/// A list / sequence `[ a, b, c ]`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct List(SyntaxNode);

impl List {
    /// Wrap a [`SyntaxKind::List`] node, or `None` for any other kind.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::List).then_some(Self(node))
    }

    /// The underlying [`SyntaxNode`].
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        &self.0
    }

    /// The element [`Value`]s, in source order.
    pub fn items(&self) -> impl Iterator<Item = Value> + '_ {
        self.0.children().filter_map(Value::cast)
    }
}

/// A map `{ k: v, .. }` (keys may be non-string values).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Map(SyntaxNode);

impl Map {
    /// Wrap a [`SyntaxKind::Map`] node, or `None` for any other kind.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::Map).then_some(Self(node))
    }

    /// The underlying [`SyntaxNode`].
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        &self.0
    }

    /// The `key: value` entries, in source order.
    pub fn entries(&self) -> impl Iterator<Item = MapEntry> + '_ {
        self.0.children().filter_map(MapEntry::cast)
    }
}

/// A single `key: value` entry inside a [`Map`].
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MapEntry(SyntaxNode);

impl MapEntry {
    /// Wrap a [`SyntaxKind::MapEntry`] node, or `None` for any other kind.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::MapEntry).then_some(Self(node))
    }

    /// The underlying [`SyntaxNode`].
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        &self.0
    }

    /// The key [`Value`] (the first value child — any RON value, including
    /// non-string keys), if present.
    #[must_use]
    pub fn key(&self) -> Option<Value> {
        self.0.children().filter_map(Value::cast).next()
    }

    /// The value [`Value`] (the second value child), if present.
    #[must_use]
    pub fn value(&self) -> Option<Value> {
        self.0.children().filter_map(Value::cast).nth(1)
    }
}

/// An enum variant: a bare `Ident`, or `Ident(..)` / `Ident{..}` payload.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EnumVariant(SyntaxNode);

impl EnumVariant {
    /// Wrap a [`SyntaxKind::EnumVariant`] node, or `None` for any other kind.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::EnumVariant).then_some(Self(node))
    }

    /// The underlying [`SyntaxNode`].
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        &self.0
    }

    /// The variant name token (`Ident`), if present.
    #[must_use]
    pub fn name(&self) -> Option<SyntaxToken> {
        self.0.first_token_of(SyntaxKind::Ident)
    }

    /// The variant name as a string, if present.
    #[must_use]
    pub fn name_text(&self) -> Option<String> {
        self.name().map(|t| t.text().to_string())
    }

    /// The struct-like payload entries for `Variant { .. }`, in source order
    /// (empty for a bare or tuple-style variant).
    pub fn entries(&self) -> impl Iterator<Item = MapEntry> + '_ {
        self.0.children().filter_map(MapEntry::cast)
    }
}

/// The unit value `()`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Unit(SyntaxNode);

impl Unit {
    /// Wrap a [`SyntaxKind::Unit`] node, or `None` for any other kind.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::Unit).then_some(Self(node))
    }

    /// The underlying [`SyntaxNode`].
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        &self.0
    }
}

/// A scalar literal node (int, float, string, raw string, char, bool keyword).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Literal(SyntaxNode);

impl Literal {
    /// Wrap a [`SyntaxKind::Literal`] node, or `None` for any other kind.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::Literal).then_some(Self(node))
    }

    /// The underlying [`SyntaxNode`].
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        &self.0
    }

    /// The single scalar token this literal wraps (the first non-trivia token).
    #[must_use]
    pub fn token(&self) -> Option<SyntaxToken> {
        self.0
            .children_with_tokens()
            .filter_map(|el| el.as_token().cloned())
            .find(|t| !t.is_trivia())
    }

    /// The [`SyntaxKind`] of the underlying scalar token (e.g. `Integer`,
    /// `String`, `Char`, `TrueKw`), if present.
    #[must_use]
    pub fn token_kind(&self) -> Option<SyntaxKind> {
        self.token().map(|t| t.kind())
    }

    /// The verbatim source text of the scalar token (never normalized), if
    /// present.
    #[must_use]
    pub fn text(&self) -> Option<String> {
        self.token().map(|t| t.text().to_string())
    }
}

/// An extension attribute `#![enable(ext, ..)]`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ExtensionAttr(SyntaxNode);

impl ExtensionAttr {
    /// Wrap a [`SyntaxKind::ExtensionAttr`] node, or `None` for any other kind.
    #[must_use]
    pub fn cast(node: SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::ExtensionAttr).then_some(Self(node))
    }

    /// The underlying [`SyntaxNode`].
    #[must_use]
    pub fn syntax(&self) -> &SyntaxNode {
        &self.0
    }

    /// The enabled-extension identifier tokens (e.g. `implicit_some`), in source
    /// order. Unknown extensions are still preserved verbatim as `Ident` tokens.
    pub fn extensions(&self) -> impl Iterator<Item = SyntaxToken> + '_ {
        self.0
            .children_with_tokens()
            .filter_map(|el| el.as_token().cloned())
            .filter(|t| t.kind() == SyntaxKind::Ident)
    }
}

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

    /// Build the typed [`Document`] for `src`.
    fn doc_of(src: &str) -> Document {
        Document::cast(parse(src).root()).expect("root is always a Document")
    }

    #[test]
    fn struct_fields_and_name() {
        let d = doc_of("Point(x: 1, y: -2.0)");
        let Some(Value::Struct(s)) = d.value() else {
            panic!("expected a struct");
        };
        assert_eq!(s.name_text().as_deref(), Some("Point"));
        let fields: Vec<_> = s.fields().collect();
        assert_eq!(fields.len(), 2);
        assert_eq!(fields[0].name_text().as_deref(), Some("x"));
        assert_eq!(fields[1].name_text().as_deref(), Some("y"));
        // Field values are reachable and typed.
        let Some(Value::Literal(lit)) = fields[0].value() else {
            panic!("x should be a literal");
        };
        assert_eq!(lit.text().as_deref(), Some("1"));
        assert_eq!(lit.token_kind(), Some(SyntaxKind::Integer));
    }

    #[test]
    fn anonymous_struct_has_no_name() {
        let d = doc_of("(a: 1, b: 2)");
        let Some(Value::Struct(s)) = d.value() else {
            panic!("expected a struct");
        };
        assert_eq!(s.name_text(), None);
        assert_eq!(s.fields().count(), 2);
    }

    #[test]
    fn list_items() {
        let d = doc_of("[1, 2, 3,]");
        let Some(Value::List(list)) = d.value() else {
            panic!("expected a list");
        };
        let items: Vec<_> = list.items().collect();
        assert_eq!(items.len(), 3);
        for it in &items {
            assert!(matches!(it, Value::Literal(_)));
        }
    }

    #[test]
    fn tuple_items() {
        let d = doc_of("(1, \"two\", 'c')");
        let Some(Value::Tuple(t)) = d.value() else {
            panic!("expected a tuple");
        };
        assert_eq!(t.items().count(), 3);
    }

    #[test]
    fn map_entries_with_non_string_keys() {
        let d = doc_of("{ 1: \"one\", 'c': true }");
        let Some(Value::Map(m)) = d.value() else {
            panic!("expected a map");
        };
        let entries: Vec<_> = m.entries().collect();
        assert_eq!(entries.len(), 2);
        // First key is an integer literal (a non-string key).
        let Some(Value::Literal(k0)) = entries[0].key() else {
            panic!("key 0 should be a literal");
        };
        assert_eq!(k0.token_kind(), Some(SyntaxKind::Integer));
        let Some(Value::Literal(v0)) = entries[0].value() else {
            panic!("value 0 should be a literal");
        };
        assert_eq!(v0.token_kind(), Some(SyntaxKind::String));
    }

    #[test]
    fn enum_variant_struct_like() {
        let d = doc_of("Variant { field: 1 }");
        let Some(Value::EnumVariant(v)) = d.value() else {
            panic!("expected an enum variant");
        };
        assert_eq!(v.name_text().as_deref(), Some("Variant"));
        assert_eq!(v.entries().count(), 1);
    }

    #[test]
    fn bare_enum_variant() {
        let d = doc_of("Unit");
        let Some(Value::EnumVariant(v)) = d.value() else {
            panic!("expected a bare variant");
        };
        assert_eq!(v.name_text().as_deref(), Some("Unit"));
        assert_eq!(v.entries().count(), 0);
    }

    #[test]
    fn unit_value() {
        let d = doc_of("()");
        assert!(matches!(d.value(), Some(Value::Unit(_))));
    }

    #[test]
    fn literal_text_is_verbatim() {
        let d = doc_of("r#\"raw \"q\" str\"#");
        let Some(Value::Literal(lit)) = d.value() else {
            panic!("expected a literal");
        };
        assert_eq!(lit.token_kind(), Some(SyntaxKind::RawString));
        assert_eq!(lit.text().as_deref(), Some("r#\"raw \"q\" str\"#"));
    }

    #[test]
    fn extension_attrs_and_value() {
        let d = doc_of("#![enable(implicit_some)]\nSome(5)");
        let attrs: Vec<_> = d.extension_attrs().collect();
        assert_eq!(attrs.len(), 1);
        let exts: Vec<_> = attrs[0]
            .extensions()
            .map(|t| t.text().to_string())
            .collect();
        // `enable` is a keyword token, so only the extension idents surface here.
        assert!(exts.contains(&"implicit_some".to_string()));
        // The top-level value is still reachable past the attributes.
        assert!(d.value().is_some());
    }

    #[test]
    fn error_value_is_reachable() {
        // A stray top-level token recovers into an Error node; navigation stays
        // total — the value is reachable as Value::Error rather than dropped.
        let d = doc_of("@");
        assert!(matches!(d.value(), Some(Value::Error(_))));
    }

    #[test]
    fn value_syntax_round_trips_text() {
        let src = "Foo(x: [1, 2], y: { 'a': 'b' })";
        let d = doc_of(src);
        let v = d.value().expect("has a value");
        // The typed value's underlying node text equals the source span.
        assert_eq!(v.syntax().text(), src);
    }
}