Repository navigation
state_exec: stackless parser + parser settings on the state tree (grammar P1b) - #649
Merged
Merged
Conversation
… 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).
This was referenced Oct 17, 2026
# 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #644 (the grammar spec, P0). The base is
feat/state-exec-grammar-p0; it retargets tomasteronce #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)
read(out, budget). It reads up tobudgettokens, returnsdatum,endormore, 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()andparse_all()call it with no budget.parse_fib20_exprtakes 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_depthstill matters, but now for the data rather than the parser itself. Destroying a nested list, the snapshot codec andto_stringstill 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_depthresolves the wayscheduler::load_settings()already resolvesmax_pending_messages:state_exec.schedulers.<id>.parser.max_depthstate_exec.defaults.parser.max_depthA missing, empty, malformed, zero or out-of-range value is skipped.
parser:parse_options { max_depth }onparse(),parse_all()andparser;parse_options_from_state(root, scheduler_id).scheduler:load_settings()resolvesparser.*and publishes the effective value understate_exec.schedulers.<id>.parser.max_depth.execute()parses with it.parser_settings()exposes it.async_scheduleris the app's exec scheduler, and it has no settings loader. It readsstate_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):parse_errorinstead of a crash;state_exec_integration_testSchedulerSettingsTest(5 new):state_exec_grammar_testmoves from a numerickLandedPhaseto named phases.kLanded = {P0, P1b}, and the 257-deep case now holds.Local, Windows MSVC 14.38 Release, cvcpkg prefix:
state_exec_parser_teststate_exec_grammar_teststate_exec_corpus_teststate_exec_unicode_teststate_exec_integration_teststate_exec_async_testariadne_runtime_testclang-format 18 is clean.
Not covered: Ariadne's predicate compile path has no dedicated test. It is a one-line change, and
ariadne_runtime_teststill passes.P2 (the PEGTL parser) carries the guard and
parse_optionsover. The tree resolution and the scheduler wiring stay as they are.