tabnas-path (Rust)
Property-path tracking plugin for the
tabnas parsing engine, crate
tabnas_path.
It tracks the property path, the chain of map keys and array indices
leading from the root to each value, as a document is parsed, and records
it in the parser's per-rule k bag, where your own rule actions can read
it. It computes nothing visible on its own: the plugin adds no alternates
and changes nothing about what the host grammar parses.
This is the Rust port of the canonical TypeScript implementation in
../ts; the TypeScript version is authoritative and this crate
tracks it. The Go port is in ../go.
Use
The engine ships no grammar of its own. Bring a grammar that defines the
val / map / pair / list / elem rules (here tabnas-json),
install it first, then Path on top, then read the path from a rule action
of your own. This example tags every scalar <value:path>:
use ;
// A key or scalar as text: keys as themselves, numbers without a
// fractional part, anything else as JSON.
Value 1 lives at path a, 0 (key a, then index 0). Every Rust
example on this page runs as a doctest of the crate, so it stays true.
The plugin writes these k entries, and nothing else:
| key | value |
|---|---|
path (tabnas_path::PATH) |
a Value::Array of segments: map keys as Value::String, array indices as Value::Number |
pathDepth (tabnas_path::PATH_DEPTH) |
the number of segments |
key (tabnas_path::KEY) |
the last segment |
index (tabnas_path::INDEX) |
the current element index inside a list, -1 before the first element |
tabnas_path::path_of(rule) reads the path as a slice. The root value has
an empty path unless the parse meta seeds one:
use ;
The plugin is also available as a tabnas::Plugin, for
parser.use_plugin(tabnas_path::plugin(), None).
Install
The tabnas crate is not published to a registry, so the engine is
consumed as a sibling checkout, the standard tabnas development
model. Clone https://github.com/tabnas/parser next to this repository
and point at it:
[]
= { = "../path/rs" }
= { = "tabnas-parser", = "../parser/rs" }
Both entries are needed. A crate's dependencies are not passed on to its
dependents, so tabnas-path alone does not put tabnas in your extern
prelude, and the examples above that name tabnas::Tabnas would not
resolve. Only PathError (the engine's TabnasError) is re-exported.
The engine is the crate's only dependency. The test suite also uses two
sibling checkouts, https://github.com/tabnas/support (the shared fixture
runner) and https://github.com/tabnas/json (the grammar the example
above runs on), as dev-dependencies.
Differences from the canonical TypeScript
Three, all deliberate, and none changes the path a value gets on the conventional rule set:
- Each level's path is a fresh array. TypeScript pools one array per
depth and mutates it in place, so its callers must copy a path they
keep. This crate, like the Go port, builds a new
Value::Arrayper level, so a path read from the bag is an owned value and there is nothing to copy. - The child's path is set from the child's side. TypeScript writes
the child's
kfrom the parent's@pair-aoand@elem-aohooks. This engine hands a parent's after-open hook the child only as an immutable snapshot, and clones the child'skbag from the parent's before that hook runs, so the parent cannot write it. The port does the same work in the child's@val-bohook instead, reading the parent's snapshot. Wherepairandelempushval, as the conventional rule set does, the result is identical. A host grammar whosepairorelempushes a rule of another name gets no path on that child here, where TypeScript would set one. - The hooks are
/prependreferences. Function references are one instance-wide namespace in this engine, so a name the host grammar also registers is overwritten and only one of the two hooks runs. The bare@val-bo,@map-boand@list-boare also the names of hooks the engine provides itself, and the jsonic port registers@map-bo/appendand@list-bo/append, so the plugin registers@val-bo/prepend,@map-bo/prepend,@list-bo/prependand@elem-ao/prepend. The engine wires a/prependreference without touching the phase's other hooks, so a host installed before or after Path keeps its own. The hooks run first in their phase where TypeScript's run last; forvalthat matches the canonical order, where the path is already set when the child'sbohooks run, and the other three touch onlyk. A host that registers one of those four/prependnames itself, or a/replacereference for one of those four phases, loses one hook.
Build and test
The engine and the two test-only crates are path dependencies on sibling checkouts, so there is nothing to fetch:
Or, from the repository root, make test-rs. For what CI would say,
including formatting, doctests and the lockfile check, run
ci/rust/run.sh.
The suite runs the shared ../test/spec/*.tsv conformance fixtures, the
same files the TypeScript and Go suites run, through the tabnas-support
runner, plus the in-language cases the fixtures cannot express: the bag
entries, instance reuse, concurrent callers, malformed input, and nesting
one thousand levels deep.
License
MIT.