Skip to content

state_exec: stackless parser + parser settings on the state tree (grammar P1b) - #649

Merged
transfix merged 7 commits into
masterfrom
feat/state-exec-parse-settings
Oct 18, 2026
Merged

transfix merged 7 commits into
masterfrom
feat/state-exec-parse-settings

Conversation

@transfix

@transfix transfix commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #644 (the grammar spec, P0). The base is feat/state-exec-grammar-p0; it retargets to master once #644 lands, and the diff here is P1b only.

Phase P1b of the state_exec grammar work, done while the PEGTL recipe is in cvcpkg. It adds the parser's nesting limit and puts its setting on the state tree, like the scheduler's other settings.

Why

The parser recursed on the native stack once per nested list or quote. A deeply nested script overflowed the stack and crashed the process. That matters for scripts that arrive in a message, an HTTP body or over the wire.

The parser is now stackless (7a4b4b1)

  • Open lists and quotes live on a heap stack of frames, and reading is a loop. Native stack depth is constant however deeply the input nests: a test reads a million open parens and reports the unclosed list.
  • The loop is a resumable read(out, budget). It reads up to budget tokens, returns datum, end or more, and keeps open frames between calls. So a scheduler can parse a script a slice at a time; that comes in the follow-up PR. parse() and parse_all() call it with no budget.
  • Output is unchanged, quirks included. The conformance cases, the 597-script corpus and the evaluator suites all pass.
  • Speed: parse_fib20_expr takes about 3.4 µs, against 3.1 µs for the recursive parser (median of 6 runs, MSVC Release, on a loaded machine). Reserving the frame stack up front closed most of the gap.

parser.max_depth still matters, but now for the data rather than the parser itself. Destroying a nested list, the snapshot codec and to_string still walk values on the native stack. Making those iterative is separate work, after which the default can rise. Real scripts nest shallowly: the deepest in the corpus is 13 levels, and the median is 2.

What

  • The setting lives on the tree. parser.max_depth resolves the way scheduler::load_settings() already resolves max_pending_messages:

    1. state_exec.schedulers.<id>.parser.max_depth
    2. state_exec.defaults.parser.max_depth
    3. 256

    A missing, empty, malformed, zero or out-of-range value is skipped.

  • parser:

    • parse_options { max_depth } on parse(), parse_all() and parser;
    • a depth guard on lists and quotes. It checks before incrementing, so a thrown error leaves the parser's depth consistent;
    • parse_options_from_state(root, scheduler_id).
  • scheduler: load_settings() resolves parser.* and publishes the effective value under state_exec.schedulers.<id>.parser.max_depth. execute() parses with it. parser_settings() exposes it.

  • async_scheduler is the app's exec scheduler, and it has no settings loader. It reads state_exec.defaults.parser.* from its watch root on every submit, so a change applies to the next submission.

  • Ariadne: ReactiveEngine::compile() reads the setting from the app tree.

  • Evaluators: stackless_evaluator / async_stackless_evaluator::create_state(script, env, parse_options).

  • Docs: the grammar doc's "Parser settings" section is now current, and its changes table moves the 257-deep row to P1b.

Tests

  • state_exec_parser_test (5 new):
    • the default 256;
    • a custom depth;
    • each quote counting as a level;
    • 100000 open parens stopped with a parse_error instead of a crash;
    • the error position at the paren that goes too deep;
    • depth resetting between top-level forms.
  • state_exec_integration_test SchedulerSettingsTest (5 new):
    • resolution order;
    • bad values skipped, including a bad per-scheduler value falling through to the defaults key;
    • the sync scheduler publishing and enforcing the limit;
    • a scheduler without an id;
    • the async scheduler reading the setting on each submit.
  • state_exec_grammar_test moves from a numeric kLandedPhase to named phases. kLanded = {P0, P1b}, and the 257-deep case now holds.

Local, Windows MSVC 14.38 Release, cvcpkg prefix:

Suite Result
state_exec_parser_test 35/35
state_exec_grammar_test 4/4
state_exec_corpus_test 1/1 (no corpus parse changed)
state_exec_unicode_test 23/23
state_exec_integration_test 90/90
state_exec_async_test 73/73
ariadne_runtime_test 122/122

clang-format 18 is clean.

Not covered: Ariadne's predicate compile path has no dedicated test. It is a one-line change, and ariadne_runtime_test still passes.

P2 (the PEGTL parser) carries the guard and parse_options over. The tree resolution and the scheduler wiring stay as they are.

… P0)

Declare the state_exec s-expression grammar in docs/STATE_EXEC_GRAMMAR.md
(W3C EBNF, Unicode source rules, literal tables, and a table of every
intentional change from the hand-written parser, tagged by phase P2-P4).
The parser itself is unchanged; later phases build it from this grammar.

state_exec_grammar_test tags each case with the phase it holds from:
landed cases must hold, pending cases must still differ, the doc's EBNF
must be closed, and every production needs a case. state_exec_corpus_test
pins how the 597 DSL scripts the repo carries parse today
(state_exec_corpus/, regenerated with gen_state_exec_corpus.py and
CVC_UPDATE_GOLDENS=1).

Also correct parser.h (no quasiquote) and the developer guide (false and
#f read as nil, bytes literals exist).
Spec only: parse_options::max_depth on parse()/parse_all()/parser and execute_options::max_parse_depth for submitted scripts, both from P2. Records why the limit exists (the reader recurses on the native stack; the evaluators are stackless) and the corpus's real depths (max 13, median 2).
Spec only. max_depth resolves from state_exec.schedulers.<id>.parser.max_depth, then state_exec.defaults.parser.max_depth, then 256, matching scheduler::load_settings(); parse_options remains for callers without a tree. Drops the execute_options::max_parse_depth idea so the tree is the single source.
…grammar P1b)

The parser now stops at parser.max_depth nested lists and quotes (default
256) with a parse_error, instead of recursing until the native stack
overflows. Like the scheduler's other settings, the limit lives on the
state tree: state_exec.schedulers.<id>.parser.max_depth, then
state_exec.defaults.parser.max_depth, then 256; a missing, malformed or
zero value is skipped.

- parser: parse_options (max_depth) on parse()/parse_all()/parser, a
  depth guard on lists and quotes, and parse_options_from_state().
- scheduler: load_settings() resolves parser.* and publishes the
  effective value under state_exec.schedulers.<id>.parser.max_depth;
  execute() parses with it.
- async_scheduler (the app's exec scheduler) reads the defaults key on
  every submit; Ariadne predicates read it from the app tree.
- stackless evaluators: create_state(script, env, parse_options).

The grammar test moves from a numeric kLandedPhase to named phases; the
257-deep case is P1b and holds now. Tests cover the default, a custom
depth, quotes, 100000 open parens, the error position, tree resolution,
bad values, and both schedulers.
…e read)

Lists and quotes were parsed by native recursion, so input depth was
bounded by the thread's stack. The parser now keeps open lists and quotes
on a heap stack of frames and reads in a loop: native stack depth is
constant however deeply the input nests (a test reads a million open
parens and reports the unclosed list instead of crashing).

The loop is a resumable read(out, budget): it reads up to `budget` tokens
and returns datum / end / more, carrying open frames between calls, so a
scheduler can parse a script a slice at a time. parse() and parse_all()
call it with no budget.

Output is unchanged, quirks included (a stray top-level `)` still reads
as a symbol until P2): the conformance cases, the 597-script corpus and
the evaluator suites all pass. parser.max_depth stays on the state tree;
it now bounds parsed data for the code that still walks values
recursively (list destruction, the snapshot codec, to_string) rather
than the parser's own stack. parse_fib20_expr: about 3.4 us vs 3.1 us
for the recursive parser (median of 6, MSVC Release, loaded machine).
@transfix transfix changed the title state_exec: parser settings on the state tree, with a nesting limit (grammar P1b) state_exec: stackless parser + parser settings on the state tree (grammar P1b) Oct 17, 2026
@transfix
transfix changed the base branch from feat/state-exec-grammar-p0 to master October 17, 2026 23:22
# Conflicts:
#	docs/STATE_EXEC_GRAMMAR.md
#	src/cvc/tests/CMakeLists.txt
#	src/cvc/tests/state_exec_grammar_test.cpp
# Conflicts:
#	src/cvc/state/state_exec/scheduler.cpp
@transfix
transfix merged commit 98a1a33 into master Oct 18, 2026
15 checks passed
@transfix
transfix deleted the feat/state-exec-parse-settings branch October 29, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant