stav 0.1.0

A statically typed, eager and total configuration language
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
//! The lossless syntax tree.
//!
//! A [`SyntaxTree`] holds every token of a source text, including whitespace,
//! comments, line feeds and text that could not be parsed. Nodes group tokens
//! into grammar constructs. Reading the tokens of the tree in order yields
//! the source text byte for byte.
//!
//! Whitespace and comments sit between nodes rather than at their edges: a
//! node starts at its first meaningful token and ends at its last one. Line
//! feeds that the grammar ignores, for example inside parentheses, are kept
//! in the node they occur in.

use std::fmt;

use crate::span::Span;
use crate::token::{Token, TokenKind};

/// The kind of a [`SyntaxNode`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NodeKind {
    /// A whole source file: its declarations and statements.
    File,
    /// Tokens that could not be parsed. A diagnostic always explains them,
    /// unless the lexer already reported the tokens themselves.
    Error,

    /// `import "path" as alias`.
    Import,
    /// `struct Name { fields }`.
    Struct,
    /// A field of a struct: `name: Type = default block kind`.
    Field,
    /// `type Name = variant | variant`.
    TypeDecl,
    /// A variant of a sum type, with an optional parameter list.
    Variant,
    /// `fn name(params) -> Type = body`.
    FnDecl,
    /// The parenthesized parameters of a function, lambda or variant.
    ParamList,
    /// A parameter: `name: Type = default`.
    Param,
    /// `let name: Type = value`, as a statement or a top-level declaration.
    Let,

    /// `{ statements }`, the body of a block, an `if`, a `for` or a `part`.
    Body,
    /// `path = value`.
    Attr,
    /// The dotted path of an attribute statement, such as `a.b."c"`.
    AttrPath,
    /// `kind label { body }`, with an optional label.
    Block,
    /// `include value`.
    Include,
    /// `if condition { body } else { body }` as a statement.
    IfStmt,
    /// `for name in collection { body }`.
    ForStmt,

    /// A literal token: an integer, a float, a path, `true`, `false` or
    /// `none`.
    Literal,
    /// A string in any of its forms, including its interpolations.
    Str,
    /// `#{ expression }` inside a string.
    Interp,
    /// An identifier in expression position.
    Name,
    /// A symbol word such as `compiler@19:static` in expression position.
    Symbol,
    /// `self`.
    SelfExpr,
    /// `[items]`.
    List,
    /// `[for x in xs if guard -> value]`.
    ListComp,
    /// `{ key = value }`, a map literal or a struct literal without a type
    /// name.
    Map,
    /// `key = value` inside a map or struct literal.
    MapEntry,
    /// `{for x in xs if guard -> key = value}`.
    MapComp,
    /// `Type { key = value }`.
    StructLit,
    /// `(expression)`.
    Paren,
    /// `callee(arguments)`.
    Call,
    /// The parenthesized arguments of a call or a predicate.
    ArgList,
    /// An argument, positional or named as in `name = value`.
    Arg,
    /// `base.field` or `base?.field`.
    FieldExpr,
    /// `base[index]`.
    IndexExpr,
    /// `lhs op rhs`.
    BinaryExpr,
    /// `!operand` or `-operand`.
    UnaryExpr,
    /// `if condition { value } else { value }` as an expression.
    IfExpr,
    /// `match scrutinee { arms }`.
    MatchExpr,
    /// `pattern if guard => value` inside a `match`.
    MatchArm,
    /// `let bindings in body`.
    LetExpr,
    /// `name: Type = value` inside a `let ... in` expression.
    Binding,
    /// `fn(params) -> Type = body`.
    Lambda,
    /// `part { body }`.
    PartExpr,

    /// The name of a type, optionally qualified: `Str` or `alias.Type`.
    TypePath,
    /// A type name with optional type arguments, such as `Map<Str, Int>`.
    NamedType,
    /// `<types>` after a type name.
    TypeArgList,
    /// `Fn(types) -> Type`.
    FnType,
    /// `Type where predicate && predicate`.
    RefinedType,
    /// A range predicate such as `1..=16`, `..5` or `0..`.
    RangePredicate,
    /// A named predicate such as `nonEmpty` or `matches(r"...")`.
    NamedPredicate,

    /// `_`.
    WildcardPat,
    /// A literal pattern: an integer, a string, `true`, `false` or `none`.
    LiteralPat,
    /// A name pattern: a binder or a nullary variant, optionally qualified.
    NamePat,
    /// A variant pattern with field patterns, such as `check(cmd, ..)`.
    VariantPat,
    /// `name` or `name = pattern` inside a variant pattern.
    FieldPat,
    /// `pattern | pattern`.
    OrPat,
}

/// A lossless syntax tree of one source text.
///
/// The tree owns a copy of the source. Its [`root`](Self::root) is always a
/// [`NodeKind::File`] node that spans the whole text.
#[derive(Debug, Clone)]
pub struct SyntaxTree {
    source: Box<str>,
    tokens: Vec<Token>,
    token_parents: Vec<usize>,
    nodes: Vec<NodeData>,
    elements: Vec<Element>,
    root: usize,
}

#[derive(Debug, Clone)]
struct NodeData {
    kind: NodeKind,
    span: Span,
    parent: Option<usize>,
    first_child: usize,
    child_count: usize,
}

#[derive(Debug, Clone, Copy)]
enum Element {
    Node(usize),
    Token(usize),
}

impl SyntaxTree {
    /// Returns the root node, which covers the whole source text.
    pub fn root(&self) -> SyntaxNode<'_> {
        SyntaxNode {
            tree: self,
            index: self.root,
        }
    }

    /// Returns the number of nodes in the tree. Node indexes run from zero
    /// up to this number, and the root has the largest index.
    pub(crate) fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Returns the node with the given index, as returned by
    /// [`SyntaxNode::index`].
    pub(crate) fn node(&self, index: usize) -> Option<SyntaxNode<'_>> {
        (index < self.nodes.len()).then_some(SyntaxNode { tree: self, index })
    }

    fn element(&self, element: Element) -> SyntaxElement<'_> {
        match element {
            Element::Node(index) => SyntaxElement::Node(SyntaxNode { tree: self, index }),
            Element::Token(index) => SyntaxElement::Token(SyntaxToken { tree: self, index }),
        }
    }
}

/// A node of a [`SyntaxTree`]: a grammar construct and its children.
///
/// A node is a cheap handle that borrows the tree.
#[derive(Clone, Copy)]
pub struct SyntaxNode<'a> {
    tree: &'a SyntaxTree,
    index: usize,
}

impl<'a> SyntaxNode<'a> {
    /// Returns what construct the node is.
    pub fn kind(self) -> NodeKind {
        self.data().kind
    }

    /// Returns the bytes the node covers, from the start of its first token
    /// to the end of its last token. A node without tokens has an empty span
    /// where it would have started.
    pub fn span(self) -> Span {
        self.data().span
    }

    /// Returns the source text the node covers.
    pub fn text(self) -> &'a str {
        &self.tree.source[self.span().range()]
    }

    /// Returns the node that contains this one, or `None` for the root.
    pub fn parent(self) -> Option<SyntaxNode<'a>> {
        let tree = self.tree;
        self.data().parent.map(|index| SyntaxNode { tree, index })
    }

    /// Returns the child nodes in source order, without tokens.
    pub fn children(self) -> impl Iterator<Item = SyntaxNode<'a>> {
        self.children_with_tokens()
            .filter_map(|element| match element {
                SyntaxElement::Node(node) => Some(node),
                SyntaxElement::Token(_) => None,
            })
    }

    /// Returns the child nodes and tokens in source order.
    pub fn children_with_tokens(self) -> impl Iterator<Item = SyntaxElement<'a>> {
        let tree = self.tree;
        let data = self.data();
        tree.elements[data.first_child..data.first_child + data.child_count]
            .iter()
            .map(move |&element| tree.element(element))
    }

    /// Returns the index of the node in its tree. Every node of a tree has
    /// a distinct index below [`SyntaxTree::node_count`].
    pub(crate) fn index(self) -> usize {
        self.index
    }

    fn data(self) -> &'a NodeData {
        &self.tree.nodes[self.index]
    }
}

impl fmt::Debug for SyntaxNode<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let span = self.span();
        write!(f, "{:?}@{}..{}", self.kind(), span.start, span.end)
    }
}

impl fmt::Display for SyntaxNode<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.text())
    }
}

/// A token of a [`SyntaxTree`].
///
/// A token is a cheap handle that borrows the tree.
#[derive(Clone, Copy)]
pub struct SyntaxToken<'a> {
    tree: &'a SyntaxTree,
    index: usize,
}

impl<'a> SyntaxToken<'a> {
    /// Returns what the token is.
    pub fn kind(self) -> TokenKind {
        self.tree.tokens[self.index].kind
    }

    /// Returns the bytes the token covers.
    pub fn span(self) -> Span {
        self.tree.tokens[self.index].span
    }

    /// Returns the source text of the token.
    pub fn text(self) -> &'a str {
        &self.tree.source[self.span().range()]
    }

    /// Returns the node that contains the token.
    pub fn parent(self) -> SyntaxNode<'a> {
        SyntaxNode {
            tree: self.tree,
            index: self.tree.token_parents[self.index],
        }
    }
}

impl fmt::Debug for SyntaxToken<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let span = self.span();
        write!(
            f,
            "{:?}@{}..{} {:?}",
            self.kind(),
            span.start,
            span.end,
            self.text()
        )
    }
}

impl fmt::Display for SyntaxToken<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.text())
    }
}

/// A child of a [`SyntaxNode`]: either a node or a token.
#[derive(Debug, Clone, Copy)]
pub enum SyntaxElement<'a> {
    /// A child node.
    Node(SyntaxNode<'a>),
    /// A child token.
    Token(SyntaxToken<'a>),
}

impl<'a> SyntaxElement<'a> {
    /// Returns the bytes the element covers.
    pub fn span(self) -> Span {
        match self {
            Self::Node(node) => node.span(),
            Self::Token(token) => token.span(),
        }
    }

    /// Returns the source text of the element.
    pub fn text(self) -> &'a str {
        match self {
            Self::Node(node) => node.text(),
            Self::Token(token) => token.text(),
        }
    }
}

/// Builds a [`SyntaxTree`] bottom up.
///
/// Children wait on a stack until the node that contains them is finished,
/// then move into one contiguous run of `elements`.
pub(crate) struct Builder {
    nodes: Vec<NodeData>,
    elements: Vec<Element>,
    token_parents: Vec<usize>,
    pending: Vec<Element>,
    open: Vec<(NodeKind, usize)>,
}

impl Builder {
    pub(crate) fn new(token_count: usize) -> Self {
        Self {
            nodes: Vec::new(),
            elements: Vec::with_capacity(token_count),
            token_parents: vec![0; token_count],
            pending: Vec::new(),
            open: Vec::new(),
        }
    }

    pub(crate) fn start_node(&mut self, kind: NodeKind) {
        self.open.push((kind, self.pending.len()));
    }

    /// Returns a position that [`Builder::start_node_at`] can later wrap
    /// into a node, together with everything added after it.
    pub(crate) fn checkpoint(&self) -> usize {
        self.pending.len()
    }

    pub(crate) fn start_node_at(&mut self, checkpoint: usize, kind: NodeKind) {
        self.open.push((kind, checkpoint.min(self.pending.len())));
    }

    pub(crate) fn token(&mut self, index: usize) {
        self.pending.push(Element::Token(index));
    }

    /// Finishes the innermost open node. `tokens` gives the spans of the
    /// children, and `empty_at` is the offset of a node without tokens.
    pub(crate) fn finish_node(&mut self, tokens: &[Token], empty_at: usize) {
        let Some((kind, first)) = self.open.pop() else {
            return;
        };
        let index = self.nodes.len();
        let first_child = self.elements.len();
        let mut span: Option<Span> = None;
        for element in self.pending.drain(first..) {
            let child_span = match element {
                Element::Node(child) => {
                    self.nodes[child].parent = Some(index);
                    self.nodes[child].span
                }
                Element::Token(token) => {
                    self.token_parents[token] = index;
                    tokens[token].span
                }
            };
            span = Some(match span {
                Some(span) => Span {
                    start: span.start,
                    end: child_span.end,
                },
                None => child_span,
            });
            self.elements.push(element);
        }
        self.nodes.push(NodeData {
            kind,
            span: span.unwrap_or(Span {
                start: empty_at,
                end: empty_at,
            }),
            parent: None,
            first_child,
            child_count: self.elements.len() - first_child,
        });
        self.pending.push(Element::Node(index));
    }

    /// Returns the tree. The last finished node becomes the root, and it
    /// spans the whole source.
    ///
    /// # Panics
    ///
    /// Panics if no node was finished, because every tree has a root.
    pub(crate) fn finish(mut self, source: &str, tokens: Vec<Token>) -> SyntaxTree {
        let root = self
            .nodes
            .len()
            .checked_sub(1)
            .expect("the root node is finished before the tree");
        self.nodes[root].span = Span {
            start: 0,
            end: source.len(),
        };
        SyntaxTree {
            root,
            source: source.into(),
            tokens,
            token_parents: self.token_parents,
            nodes: self.nodes,
            elements: self.elements,
        }
    }
}