meta-language 1.0.0

A self-describing links-network core for lossless language representation
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
//! The default parse of a language whose catalog grammar is a native Links
//! Notation grammar (the catalog's `nativeGrammars`): the grammar runs on the
//! native executor with automatic error recovery, and its tree is projected as
//! the language's tree-sitter oracle (the catalog's `oracleGrammars`) places
//! nodes, so the default concrete syntax tree keeps its shape when the native
//! grammar replaces the oracle. This is the port of
//! `js/src/native-grammar-parser.js`.
//!
//! Whitespace trivia is not a node, named trivia (a comment token) is an extra
//! node, leading trivia belongs before the node it precedes, a node spans its
//! first to last non-trivia leaf, and the root starts at its first leaf that is
//! not whitespace and ends at the end of the input. Kinds in `hidden` are
//! leaves the oracle drops, such as a byte order mark, and kinds in
//! `anonymous` are leaves the oracle keeps inside a node without a node of
//! their own; the text of both is the gap text between nodes. Node kinds in
//! `extras` are extra nodes, as the oracle marks a comment node it parses as an
//! extra. A rule renamed from its tree-sitter name keeps that name as its
//! node kind (`oracle_kinds`, from the rule's `(source-names (tree-sitter
//! NAME))`). A kind `'TEXT`, an imported anonymous alias, is an anonymous
//! node TEXT. An ERROR leaf is a named `ERROR` node and a MISSING leaf an
//! empty MISSING node, named unless it stands for a literal; a MISSING leaf
//! of a `hidden` or `anonymous` kind is a token tree-sitter hides, so it is no
//! node, though the node it is missing from still has an error.

use std::collections::{BTreeMap, BTreeSet};
use std::sync::OnceLock;

use crate::{
    FeatureGrammarParser, FeatureParseOptions, LeafText, SyntaxTree, compile_feature_grammar,
    native_grammar, parse_grammar_links,
};

/// The Links Notation text of every native grammar the catalog ships, by
/// catalog file; `tests/unit/default_cst_expectations.rs` checks that the
/// catalog's native grammars are exactly the shipped files.
const NATIVE_GRAMMAR_TEXTS: &[(&str, &str)] = &[
    (
        "native-grammars/c.lino",
        include_str!("data/native-grammars/c.lino"),
    ),
    (
        "native-grammars/csv.lino",
        include_str!("data/native-grammars/csv.lino"),
    ),
    (
        "native-grammars/diff.lino",
        include_str!("data/native-grammars/diff.lino"),
    ),
    (
        "native-grammars/go.lino",
        include_str!("data/native-grammars/go.lino"),
    ),
    (
        "native-grammars/graphql.lino",
        include_str!("data/native-grammars/graphql.lino"),
    ),
    (
        "native-grammars/ini.lino",
        include_str!("data/native-grammars/ini.lino"),
    ),
    (
        "native-grammars/java.lino",
        include_str!("data/native-grammars/java.lino"),
    ),
    (
        "native-grammars/javascript.lino",
        include_str!("data/native-grammars/javascript.lino"),
    ),
    (
        "native-grammars/json.lino",
        include_str!("data/native-grammars/json.lino"),
    ),
    (
        "native-grammars/json5.lino",
        include_str!("data/native-grammars/json5.lino"),
    ),
    (
        "native-grammars/lean.lino",
        include_str!("data/native-grammars/lean.lino"),
    ),
    (
        "native-grammars/make.lino",
        include_str!("data/native-grammars/make.lino"),
    ),
    (
        "native-grammars/proto.lino",
        include_str!("data/native-grammars/proto.lino"),
    ),
    (
        "native-grammars/racket.lino",
        include_str!("data/native-grammars/racket.lino"),
    ),
    (
        "native-grammars/regex.lino",
        include_str!("data/native-grammars/regex.lino"),
    ),
    (
        "native-grammars/rocq.lino",
        include_str!("data/native-grammars/rocq.lino"),
    ),
    (
        "native-grammars/rust.lino",
        include_str!("data/native-grammars/rust.lino"),
    ),
    (
        "native-grammars/scheme.lino",
        include_str!("data/native-grammars/scheme.lino"),
    ),
    (
        "native-grammars/solidity.lino",
        include_str!("data/native-grammars/solidity.lino"),
    ),
    (
        "native-grammars/tsx.lino",
        include_str!("data/native-grammars/tsx.lino"),
    ),
    (
        "native-grammars/typescript.lino",
        include_str!("data/native-grammars/typescript.lino"),
    ),
];

/// One node of a projected native tree; offsets are UTF-8 byte offsets. The
/// flags mirror tree-sitter's node flags.
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NativeNode {
    pub term: String,
    pub named: bool,
    pub start: usize,
    pub end: usize,
    pub is_error: bool,
    pub is_missing: bool,
    pub is_extra: bool,
    pub has_error: bool,
    /// The children with the field each is captured under.
    pub children: Vec<(Self, Option<String>)>,
}

/// Whether the grammar id `id` names a native grammar of the catalog.
pub fn is_native_grammar(id: &str) -> bool {
    native_grammar(id).is_some()
}

fn native_grammar_index(id: &str) -> Option<usize> {
    let file = &native_grammar(id)?.file;
    NATIVE_GRAMMAR_TEXTS
        .iter()
        .position(|(path, _)| path == file)
}

/// The Links Notation text of the native grammar `id`.
pub fn native_grammar_text(id: &str) -> Option<&'static str> {
    native_grammar_index(id).map(|index| NATIVE_GRAMMAR_TEXTS[index].1)
}

// Each native grammar is compiled on its first use and kept for the process.
pub fn native_parser(id: &str) -> Option<&'static FeatureGrammarParser> {
    static PARSERS: [OnceLock<Option<FeatureGrammarParser>>; NATIVE_GRAMMAR_TEXTS.len()] =
        [const { OnceLock::new() }; NATIVE_GRAMMAR_TEXTS.len()];
    let index = native_grammar_index(id)?;
    PARSERS[index]
        .get_or_init(|| {
            let grammar = parse_grammar_links(NATIVE_GRAMMAR_TEXTS[index].1).ok()?;
            compile_feature_grammar(&grammar, None, FeatureParseOptions::default()).ok()
        })
        .as_ref()
}

/// Parses `source` with the native grammar `id` and returns the projected
/// root. Input the executor cannot finish within its resource limits is one
/// ERROR root.
pub fn parse_native(id: &str, source: &str) -> NativeNode {
    let length = source.len();
    let error_root = || NativeNode {
        term: "ERROR".to_owned(),
        named: true,
        start: 0,
        end: length,
        is_error: true,
        is_missing: false,
        is_extra: false,
        has_error: true,
        children: Vec::new(),
    };
    let (Some(entry), Some(parser)) = (native_grammar(id), native_parser(id)) else {
        return error_root();
    };
    let options = FeatureParseOptions {
        error_recovery: Some(true),
        accept_recovery: Some(true),
        ..FeatureParseOptions::default()
    };
    match parser.parse_tree(source.as_bytes(), &options) {
        Ok(outcome) => match outcome.tree {
            Some(tree @ SyntaxTree::Node { .. }) => Projection {
                hidden: entry.hidden.iter().map(String::as_str).collect(),
                anonymous: entry.anonymous.iter().map(String::as_str).collect(),
                extras: entry.extras.iter().map(String::as_str).collect(),
                oracle_kinds: &entry.oracle_kinds,
            }
            .root(&tree, length),
            _ => error_root(),
        },
        Err(_) => error_root(),
    }
}

struct Projection<'a> {
    hidden: BTreeSet<&'a str>,
    anonymous: BTreeSet<&'a str>,
    extras: BTreeSet<&'a str>,
    oracle_kinds: &'a BTreeMap<String, String>,
}

impl Projection<'_> {
    /// The tree-sitter kind of a native kind: the kind itself unless its rule
    /// was renamed from it, and the text of an anonymous alias `'TEXT`.
    fn oracle_kind(&self, kind: &str) -> String {
        kind.strip_prefix('\'').map_or_else(
            || {
                self.oracle_kinds
                    .get(kind)
                    .map_or_else(|| kind.to_owned(), Clone::clone)
            },
            str::to_owned,
        )
    }

    fn invisible(&self, node: &SyntaxTree) -> bool {
        matches!(node, SyntaxTree::Token { kind, trivia, .. }
            if (*trivia && kind.is_none())
                || kind.as_deref().is_some_and(|kind| self.hidden.contains(kind)))
    }

    fn trivia(&self, node: &SyntaxTree) -> bool {
        self.invisible(node)
            || matches!(node, SyntaxTree::Token { trivia: true, .. })
            || matches!(node, SyntaxTree::Node { kind, .. } if self.extras.contains(kind.as_str()))
    }

    fn anonymous(&self, node: &SyntaxTree) -> bool {
        matches!(node, SyntaxTree::Token { kind: Some(kind), .. } if self.anonymous.contains(kind.as_str()))
    }

    fn hidden_missing(&self, node: &SyntaxTree) -> bool {
        matches!(node, SyntaxTree::Missing { kind: Some(kind), .. }
            if self.hidden.contains(kind.as_str()) || self.anonymous.contains(kind.as_str()))
    }

    /// Moves the leading trivia of a node before it.
    fn hoist(&self, node: &SyntaxTree) -> Vec<SyntaxTree> {
        let SyntaxTree::Node {
            kind,
            field,
            trivia,
            start,
            end,
            children,
            attributes,
        } = node
        else {
            return vec![node.clone()];
        };
        let mut children: Vec<SyntaxTree> = children
            .iter()
            .flat_map(|child| self.hoist(child))
            .collect();
        let first = children
            .iter()
            .take_while(|child| self.trivia(child))
            .count();
        let rest = children.split_off(first);
        children.push(SyntaxTree::Node {
            kind: kind.clone(),
            field: field.clone(),
            trivia: *trivia,
            start: *start,
            end: *end,
            children: rest,
            attributes: attributes.clone(),
        });
        children
    }

    fn span(&self, node: &SyntaxTree) -> (usize, usize) {
        match node {
            SyntaxTree::Node {
                start, children, ..
            } => {
                let inner: Vec<(usize, usize)> = children
                    .iter()
                    .filter(|child| !self.trivia(child))
                    .map(|child| self.span(child))
                    .collect();
                match (inner.first(), inner.last()) {
                    (Some(first), Some(last)) => (first.0, last.1),
                    _ => (*start, *start),
                }
            }
            SyntaxTree::Token { start, end, .. }
            | SyntaxTree::Error { start, end, .. }
            | SyntaxTree::Missing { start, end, .. }
            | SyntaxTree::Embed { start, end, .. } => (*start, *end),
        }
    }

    fn project(&self, children: &[SyntaxTree]) -> Vec<(NativeNode, Option<String>)> {
        children
            .iter()
            .flat_map(|child| self.hoist(child))
            .filter(|child| {
                !self.invisible(child) && !self.anonymous(child) && !self.hidden_missing(child)
            })
            .map(|child| {
                let field = match &child {
                    SyntaxTree::Node { field, .. } | SyntaxTree::Token { field, .. } => {
                        field.clone()
                    }
                    _ => None,
                };
                (self.node(&child), field)
            })
            .collect()
    }

    fn node(&self, node: &SyntaxTree) -> NativeNode {
        let (start, end) = self.span(node);
        let leaf = |term: String, named: bool| NativeNode {
            term,
            named,
            start,
            end,
            is_error: false,
            is_missing: false,
            is_extra: false,
            has_error: false,
            children: Vec::new(),
        };
        match node {
            SyntaxTree::Node { kind, children, .. } => {
                let hidden_missing = children.iter().any(|child| self.hidden_missing(child));
                let children = self.project(children);
                NativeNode {
                    has_error: hidden_missing || children.iter().any(|(child, _)| child.has_error),
                    is_extra: self.extras.contains(kind.as_str()),
                    children,
                    ..leaf(self.oracle_kind(kind), !anonymous_alias(kind))
                }
            }
            SyntaxTree::Error { .. } => NativeNode {
                is_error: true,
                has_error: true,
                ..leaf("ERROR".to_owned(), true)
            },
            SyntaxTree::Missing { kind, literal, .. } => NativeNode {
                is_missing: true,
                has_error: true,
                ..leaf(
                    kind.as_deref()
                        .map_or_else(|| "MISSING".to_owned(), |kind| self.oracle_kind(kind)),
                    !literal && !kind.as_deref().is_some_and(anonymous_alias),
                )
            },
            SyntaxTree::Token {
                kind, trivia, text, ..
            } => NativeNode {
                is_extra: *trivia,
                ..leaf(
                    kind.as_deref().map_or_else(
                        || match text {
                            LeafText::Text(text) | LeafText::Hex(text) => text.clone(),
                        },
                        |kind| self.oracle_kind(kind),
                    ),
                    kind.as_deref().is_some_and(|kind| !anonymous_alias(kind)),
                )
            },
            SyntaxTree::Embed { language, .. } => leaf(language.clone(), true),
        }
    }

    fn root(&self, tree: &SyntaxTree, length: usize) -> NativeNode {
        let SyntaxTree::Node { kind, children, .. } = tree else {
            unreachable!("the root of a native tree is a node");
        };
        let mut leaves = Vec::new();
        collect_leaves(tree, &mut leaves);
        let start = leaves
            .iter()
            .find(|leaf| !self.invisible(leaf))
            .map_or(length, |leaf| self.span(leaf).0);
        let hidden_missing = children.iter().any(|child| self.hidden_missing(child));
        let children = self.project(children);
        NativeNode {
            term: self.oracle_kind(kind),
            named: true,
            start,
            end: length,
            is_error: false,
            is_missing: false,
            is_extra: false,
            has_error: hidden_missing || children.iter().any(|(child, _)| child.has_error),
            children,
        }
    }
}

/// Whether a kind is an imported anonymous alias `'TEXT`.
fn anonymous_alias(kind: &str) -> bool {
    kind.starts_with('\'')
}

fn collect_leaves<'a>(node: &'a SyntaxTree, leaves: &mut Vec<&'a SyntaxTree>) {
    if let SyntaxTree::Node { children, .. } = node {
        for child in children {
            collect_leaves(child, leaves);
        }
    } else {
        leaves.push(node);
    }
}