tabnas-parser 0.12.9

A dynamic JSON parser that isn't strict and can be customized.
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
// Copyright (c) 2013-2026 Richard Rodger, MIT License

use crate::options::InfoOptions;
use crate::rule::Rule;
use crate::token::{TIN_ST, TIN_TX};
use crate::value::{ListRef, MapRef, Text, Value};
use crate::Context;
use indexmap::IndexMap;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;

pub(crate) fn is_builtin_action(name: &str) -> bool {
    matches!(
        name,
        "@node$"
            | "@capture$"
            | "@bubble$"
            | "@fold$"
            | "@probeInit$"
            | "@probeDecide$"
            | "@object$"
            | "@array$"
            | "@reset$"
            | "@key$"
            | "@setval$"
            | "@push$"
            | "@value$"
            | "@map-bo"
            | "@list-bo"
            | "@pairkey"
            | "@pair-bc"
            | "@elem-bc"
            | "@val-bo"
            | "@val-bc"
    )
}

fn config_object(config: Option<&Value>) -> Option<&IndexMap<String, Value>> {
    config.and_then(|value| match value {
        Value::Object(map) => Some(map.as_ref()),
        _ => None,
    })
}

fn config_bool(config: Option<&Value>, name: &str) -> bool {
    matches!(
        config_object(config).and_then(|map| map.get(name)),
        Some(Value::Bool(true))
    )
}

fn config_string(config: Option<&Value>, name: &str) -> String {
    match config_object(config).and_then(|map| map.get(name)) {
        Some(Value::String(value)) => value.clone(),
        _ => String::new(),
    }
}

/// The config value at `name` only when it really is a string.
///
/// Distinct from `config_string`, which folds "absent" and "" into the
/// same empty String: `lit` needs them apart, because an empty string is
/// a real key. Matching the TS `typeof` guard and the Go type assertion,
/// anything that is not a string reads as absent, so the three ports
/// agree on every input and not merely on well-typed ones.
fn config_str_opt(config: Option<&Value>, name: &str) -> Option<String> {
    match config_object(config).and_then(|map| map.get(name)) {
        Some(Value::String(value)) => Some(value.clone()),
        _ => None,
    }
}

fn config_usize(config: Option<&Value>, name: &str) -> usize {
    match config_object(config).and_then(|map| map.get(name)) {
        Some(Value::Number(value)) if value.is_finite() && *value >= 0.0 => *value as usize,
        _ => 0,
    }
}

fn config_index(config: Option<&Value>, name: &str) -> Option<usize> {
    match config_object(config).and_then(|map| map.get(name)) {
        None => Some(0),
        Some(Value::Number(value))
            if value.is_finite()
                && value.fract() == 0.0
                && *value >= 0.0
                && *value <= usize::MAX as f64 =>
        {
            Some(*value as usize)
        }
        _ => None,
    }
}

fn ast_node(rule: String, kind: String) -> Value {
    let mut node = IndexMap::new();
    if kind == "user" {
        node.insert("rule".into(), Value::String(rule));
    }
    node.insert("src".into(), Value::String(String::new()));
    node.insert("kids".into(), Value::array(Vec::new()));
    Value::object(node)
}

/// The accumulated source text of a tree node -- the `{rule?, src, kids}`
/// shape `ast_node` builds -- or the node unchanged when it is not one.
///
/// "src" is how a member whose value IS its matched text gets that text:
/// the tree builders already accumulate it, and nothing else could read it
/// back out. A compiler emits "src" only where it already knows the member
/// is a scalar, so this never has to guess which it is: the fall-through
/// exists so asking for src where no tree node was built passes the value
/// along rather than erasing it.
fn src_val(node: Value) -> Value {
    if let Value::Object(map) = &node {
        if let Some(Value::String(source)) = map.get("src") {
            return Value::String(source.clone());
        }
    }
    node
}

fn append_src(node: &mut IndexMap<String, Value>, source: &str) {
    if let Some(Value::String(current)) = node.get_mut("src") {
        current.push_str(source);
    }
}

fn append_kid(node: &mut IndexMap<String, Value>, child: Value) {
    if let Some(kids) = node.get_mut("kids").and_then(Value::as_array_mut) {
        kids.push(child);
    }
}

fn capture_child(node: &mut IndexMap<String, Value>, child: Value) {
    if let Value::Object(child_map) = &child {
        if let Some(Value::String(source)) = child_map.get("src") {
            append_src(node, source);
            if child_map
                .get("rule")
                .is_some_and(|value| !matches!(value, Value::String(rule) if rule.is_empty()))
            {
                append_kid(node, child);
            } else if let Some(Value::Array(children)) = child_map.get("kids") {
                extend_kids(node, children);
            }
            return;
        }
    }
    append_kid(node, child);
}

/// Flatten an untagged child's kids into `node`'s.
///
/// A repetition compiles to untagged rules nested one per element, and
/// each level flattens the level below it, so a rule with N elements
/// flattens N + (N-1) + ... kids: quadratic in the element count of ONE
/// rule. TypeScript spreads the same way, but a JavaScript push is a
/// pointer store; here each kid cost a hash lookup, an `Arc` clone and a
/// later drop, and 800 fields took 46 seconds against a tenth of a
/// second (#195). A level whose own kids are still empty, which is every
/// level of that chain, now takes the child's array by handle instead:
/// one refcount bump, and the chain is linear again. Copy-on-write keeps
/// the child's own view intact should anything still read it.
fn extend_kids(node: &mut IndexMap<String, Value>, children: &Arc<Vec<Value>>) {
    match node.get_mut("kids") {
        Some(Value::Array(kids)) if kids.is_empty() => *kids = Arc::clone(children),
        Some(Value::Array(kids)) => Arc::make_mut(kids).extend(children.iter().cloned()),
        _ => {}
    }
}

fn map_value(info: &InfoOptions, implicit: bool) -> Value {
    if info.map {
        Value::MapRef(Arc::new(MapRef {
            value: IndexMap::new(),
            implicit,
            meta: IndexMap::new(),
        }))
    } else {
        Value::object(IndexMap::new())
    }
}

fn list_value(info: &InfoOptions, implicit: bool) -> Value {
    if info.list {
        Value::ListRef(Arc::new(ListRef {
            value: Vec::new(),
            implicit,
            child: None,
            meta: IndexMap::new(),
        }))
    } else {
        Value::array(Vec::new())
    }
}

fn token_value(rule: &mut Rule, context: &mut Context, index: usize, info: &InfoOptions) -> Value {
    let Some(token) = rule.o.get(index).cloned() else {
        return Value::Undefined;
    };
    let value = token.resolve_val(rule, context);
    if info.text && matches!(token.tin, TIN_ST | TIN_TX) {
        let string = match &value {
            Value::String(value) => value.clone(),
            Value::Text(value) => value.string.clone(),
            _ => return value,
        };
        let quote = if token.tin == TIN_ST {
            token
                .src
                .chars()
                .next()
                .map_or_else(String::new, |ch| ch.to_string())
        } else {
            String::new()
        };
        Value::Text(Text { quote, string })
    } else {
        value
    }
}

fn map_insert(node: &mut Value, key: String, value: Value) {
    match node {
        Value::Object(map) => {
            Arc::make_mut(map).insert(key, value);
        }
        Value::MapRef(map) => {
            Arc::make_mut(map).value.insert(key, value);
        }
        _ => {}
    }
}

fn list_push(node: &mut Value, value: Value) {
    match node {
        Value::Array(array) => Arc::make_mut(array).push(value),
        Value::ListRef(list) => Arc::make_mut(list).value.push(value),
        _ => {}
    }
}

/// Run a built-in with metadata wrappers disabled.
///
/// This keeps the original public helper stable for embedders. Parser-owned
/// execution goes through the crate-internal `run_builtin_action_with_info`,
/// which is handed the parse's own [`InfoOptions`]; the public way to the same
/// behaviour is [`run_builtin_action_with_context`], which reads them off the
/// context it is given.
pub fn run_builtin_action(name: &str, rule: &mut Rule, config: Option<&Value>) -> bool {
    let options = std::sync::Arc::new(crate::Options::default());
    let mut context = Context::new(
        options.rewind.history,
        "",
        Value::Undefined,
        std::sync::Arc::clone(&options),
        crate::InstanceInfo::default(),
    );
    run_builtin_action_with_info(name, rule, &mut context, config, &InfoOptions::default())
}

/// Run a builtin against an explicit live parse context. Embedders that use
/// lazy token values should prefer this over [`run_builtin_action`].
pub fn run_builtin_action_with_context(
    name: &str,
    rule: &mut Rule,
    context: &mut Context,
    config: Option<&Value>,
) -> bool {
    let info = context.options.info.clone();
    run_builtin_action_with_info(name, rule, context, config, &info)
}

pub(crate) fn run_builtin_action_with_info(
    name: &str,
    rule: &mut Rule,
    context: &mut Context,
    config: Option<&Value>,
    info: &InfoOptions,
) -> bool {
    match name {
        "@node$" => {
            if config_bool(config, "init") {
                rule.node = Rc::new(RefCell::new(ast_node(
                    config_string(config, "rule"),
                    config_string(config, "kind"),
                )));
            }
            let nterms = config_usize(config, "nterms");
            if let Some(node) = rule.node.borrow_mut().as_object_mut() {
                for token in rule.o.iter().take(nterms) {
                    append_src(node, &token.src);
                }
            }
        }
        "@capture$" => {
            if rule.node.borrow().is_undefined() {
                rule.node = Rc::new(RefCell::new(ast_node(
                    config_string(config, "rule"),
                    config_string(config, "kind"),
                )));
            }
            if !rule.child_node.is_undefined() && !rule.child_node_is_self {
                if let Some(node) = rule.node.borrow_mut().as_object_mut() {
                    capture_child(node, rule.child_node.clone());
                }
            }
        }
        "@bubble$" => {
            if rule.has_child_value() {
                rule.node = Rc::new(RefCell::new(rule.child_value()));
            }
        }
        "@fold$" => {
            if let Some(parent_node) = &rule.parent_node {
                let same_node = Rc::ptr_eq(parent_node, &rule.node);
                let own = rule.node.borrow().clone();
                if let Some(parent) = parent_node.borrow_mut().as_object_mut() {
                    if !same_node {
                        capture_child(parent, own);
                    }
                    let close_count = config_usize(config, "cN");
                    for token in rule.c.iter().take(close_count) {
                        append_src(parent, &token.src);
                    }
                }
            }
            rule.node = Rc::new(RefCell::new(Value::Undefined));
        }
        "@val-bo" => {
            rule.node = Rc::new(RefCell::new(Value::Undefined));
        }
        "@val-bc" => {
            let is_undef = rule.node.borrow().is_undefined();
            if is_undef {
                if rule.has_child_value() {
                    rule.node = Rc::new(RefCell::new(rule.child_value()));
                } else if rule.os() > 0 {
                    rule.node = Rc::new(RefCell::new(token_value(rule, context, 0, info)));
                }
            }
        }
        "@value$" => {
            if rule.has_child_value() {
                rule.node = Rc::new(RefCell::new(rule.child_value()));
            } else {
                let value = config_index(config, "from").map_or(Value::Undefined, |index| {
                    token_value(rule, context, index, info)
                });
                rule.node = Rc::new(RefCell::new(value));
            }
        }
        "@object$" => {
            rule.node = Rc::new(RefCell::new(map_value(
                info,
                config_bool(config, "implicit"),
            )));
        }
        "@array$" => {
            rule.node = Rc::new(RefCell::new(list_value(
                info,
                config_bool(config, "implicit"),
            )));
        }
        "@reset$" => {
            rule.node = Rc::new(RefCell::new(Value::Undefined));
            rule.child_node = Value::Undefined;
            rule.child_node_is_self = false;
        }
        "@key$" => {
            let slot = {
                let configured = config_string(config, "slot");
                if configured.is_empty() {
                    "key".to_string()
                } else {
                    configured
                }
            };
            // `lit` supplies the key as a CONSTANT instead of reading it
            // from a token, and wins outright when set. A grammar whose
            // structure is declared rather than delimited -- `ver = maj
            // "." min`, where `maj` names a part but no token carries the
            // text "maj" -- has no token for @key$ to read, so without
            // this the key side of @setval$ is unreachable for it.
            if let Some(lit) = config_str_opt(config, "lit") {
                rule.u_mut().insert(slot, Value::String(lit));
            } else if let Some(value) = config_index(config, "from")
                .and_then(|index| rule.o.get(index))
                .map(|token| token.val.clone())
            {
                rule.u_mut().insert(slot, value);
            }
        }
        "@setval$" => {
            let slot = {
                let configured = config_string(config, "slot");
                if configured.is_empty() {
                    "key".to_string()
                } else {
                    configured
                }
            };
            if let Some(Value::String(key)) = rule.u.get(&slot).cloned() {
                let mut val = rule.child_value();
                if config_bool(config, "src") {
                    val = src_val(val);
                }
                map_insert(&mut rule.node.borrow_mut(), key, val);
            }
        }
        "@push$" => {
            if rule.has_child_value() {
                let mut val = rule.child_value();
                if config_bool(config, "src") {
                    val = src_val(val);
                }
                list_push(&mut rule.node.borrow_mut(), val);
            }
        }
        "@map-bo" => {
            rule.node = Rc::new(RefCell::new(map_value(info, false)));
        }
        "@list-bo" => {
            rule.node = Rc::new(RefCell::new(list_value(info, false)));
        }
        // The captured key belongs only to this pair rule and is consumed by
        // @pair-bc on that same rule. Store it in `u`, the non-propagating
        // scratch bag, so it cannot leak into child rules as `n` or `k` would.
        "@pairkey" => {
            if let Some(t0) = rule.o0() {
                let key = match &t0.val {
                    Value::String(s) => s.clone(),
                    _ => t0.src.to_string(),
                };
                rule.u_mut().insert("key".to_string(), Value::String(key));
            }
        }
        "@pair-bc" => {
            if let Some(Value::String(key)) = rule.u.get("key").cloned() {
                let child = rule.child_value();
                let mut node = rule.node.borrow_mut();
                map_insert(&mut node, key, child);
            }
        }
        "@elem-bc" if rule.has_child_value() => {
            let child = rule.child_value();
            let mut node = rule.node.borrow_mut();
            list_push(&mut node, child);
        }
        _ => return false,
    }
    true
}