tabnas-hoover (Rust)
Block-delimited string hoovering for the
tabnas parser engine, crate
tabnas_hoover.
Hoovering vacuums up a run of source text, internal spaces and newlines
included, between a start and an end delimiter, with optional escape
handling and rule-context gating. Two shapes drive the design: the
triple-quoted string ('''hello world''' gives "hello world") and the
end-of-line value (an unquoted run of text up to a newline, #, ; or
the end of the input).
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.
The plugin is grammar-agnostic. It installs one lexer matcher that emits
a #HV token and adds an alternate accepting that token to the host
grammar's val rule, so it is applied to a Tabnas instance that
already carries a grammar defining val, never to a bare engine. Its
only production dependency is the engine.
Use
use Tabnas;
use ;
A block is a [Block]: a name, a start (fixed delimiters, whether a
matched one is consumed, and an optional rule context), an end (fixed
delimiters, "" for the end of the input, and consumption), an
optional escape character with an escape map, and a trim flag. Blocks
are tried in the order they are listed. Once a block's start matches it
is committed: a block that never reaches an end delimiter fails with
invalid_text, and a rejected escape fails with invalid_escape.
use ;
The options the other runtimes take as plain data ({"block": [...], "lex": {"order": n}}) are read by HooverOptions::from_json (behind the serde_json feature, off by default), with each
block's fields spelled as in TypeScript.
Every token a block emits carries {block: <name>} in its use bag,
and HooverOptions::with_action runs a callback on the val rule when
that token is matched.
Install
The engine is not part of this crate. Both are on crates.io, the engine
as tabnas-parser, whose library is named tabnas in code, so add both:
Both entries are needed. A crate's dependencies are not passed on to its
dependents, so tabnas-hoover alone does not put tabnas in your
extern prelude.
In this repository, Cargo.toml takes the engine by path from a sibling
checkout instead, and the tests take tabnas-support the same way. The
release workflow swaps the engine's path for a crates.io version, and
drops the test-only one, when it publishes this crate.
Differences from the canonical TypeScript
Deliberate, and each a matter of typing rather than behaviour:
- Options are a typed struct. The alternate action is a Rust
callback, and no serialized value can carry one, so
HooverOptionstravels in the plugin closure rather than in the engine's plugin option bag. The data shape is still accepted, throughHooverOptions::from_json. consumeis an enum. The TypeScriptnull | boolean | string[]isConsume::All,Consume::NeverandConsume::Only(list).state: ''isSome(""). ANonestate means the default,"o"; the empty string means "do not check the state", exactly as in TypeScript, so no sentinel is needed.- Registration failures are returned, not thrown. A missing
valrule, a host whose alt filter drops the block alternate, and a grammar the engine refuses all come back asHooverError, as they do in Go. Two differences are behavioural, and both are recorded in../DIVERGENCE.md. After a mapped escape, the canonical implementation and Go advance the row when the replacement is a newline, while this port advances it when the escaped source character is a newline: the hoovered value is the same, and the row and column of an error reported after such a block differ. And the block token is positioned at its start here, as in Go, where the canonical implementation records the end of the block: the value, every later token's position and the error messages are identical, and the difference shows only in the token's ownsi,riandcias an action reads them.
Build and test
The engine is a path dependency on the sibling checkout, and so is the
tabnas-support fixture runner the tests use, so there is nothing to
fetch:
Or, from the repository root, make test-rs. For what CI would say,
including formatting 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, against the same tiny
host grammar (val plus a parenthesised group) as
tests/common/mini_grammar.rs. A row green in one runtime and red in
another is a failure, not a discrepancy.
License
MIT.