Skip to content

state_exec: grammar spec, conformance cases and parse corpus (grammar P0) - #644

Merged
transfix merged 4 commits into
masterfrom
feat/state-exec-grammar-p0
Oct 17, 2026
Merged

transfix merged 4 commits into
masterfrom
feat/state-exec-grammar-p0

Conversation

@transfix

@transfix transfix commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

Phase P0 of the state_exec grammar work: write the grammar down, and pin today's parser against it. No parser behaviour changes in this PR. Later phases build the parser from this declaration:

  • P2 moves it onto PEGTL rules with the same production names;
  • P3 adds the Unicode source rules;
  • P4 adds octal escapes and raw strings.

The spec: docs/STATE_EXEC_GRAMMAR.md

  • A W3C-EBNF grammar with 52 productions.
  • Unicode source rules. Source must be valid UTF-8. Whitespace is Pattern_White_Space. NBSP and the other look-alike spaces are errors between tokens. Raw bidi controls are errors anywhere (CVE-2021-42574). Symbols may use any other character, emoji included, with no normalization.
  • Literals.
    • Integers in 0x/0o/0b and #x/#o/#b, with _ separators. Overflow is an error.
    • Floats +inf.0/-inf.0/+nan.0.
    • Octal \NNN escapes, plus \a \b \f \v.
    • Raw strings r"…"/r#"…"# and raw bytes br"…".
    • The reserved characters , [ \ ] ` { | }.
    • A limit on how deeply lists and quotes nest (default 256; the deepest script in the corpus
      nests 13 levels). Like the scheduler's settings, it is configured on the state tree:
      state_exec.schedulers.<id>.parser.max_depth, then state_exec.defaults.parser.max_depth,
      then 256. A follow-up PR implements it.
  • A changes table. Every way the grammar differs from the hand-written parser, tagged with the phase that delivers it. For example:
    • a stray ) reads as a symbol today;
    • parse("1 2") drops the 2;
    • 0x1F reads as the double 31.0;
    • inf/nan can't be symbols;
    • error columns count bytes.

Tests

  • state_exec_grammar_test has 75 cases, each tagged with the phase it holds from: 32 hold today, and 23, 11 and 9 belong to P2, P3 and P4. It checks that:
    • cases for landed phases hold (kLandedPhase = 0);
    • cases for pending phases still behave differently today, so the table stays an exact list of what each phase changes;
    • the doc's EBNF is closed;
    • every production has a case.
  • state_exec_corpus_test parses 597 DSL scripts and compares each parse with state_exec_corpus/expected.txt. The scripts come from the .ari fields, the docs' lisp blocks, balanced C++ string literals in state_exec/ariadne sources, and the pycvc tests. A later phase can't change how an existing script reads without that file changing in the same PR.
    • To regenerate: gen_state_exec_corpus.py (needs PyYAML), then CVC_UPDATE_GOLDENS=1.
    • The corpus files are pinned to LF by .gitattributes.
  • Both compare parses through state_exec_grammar_dump.h, an exact dump that tags every value with its type. They don't use to_string, which is lossy and changes in P5.

Audit

  • All 597 corpus scripts parse today.
  • The grammar's intentional changes touch only one of them: a {...} dict-literal sketch in docs/roadmap/STATE_BINARY_STREAMING.md, which no parser implements.
  • No corpus script uses \0 followed by a digit, an unknown escape, inf/nan, radix-like tokens, or multi-form on: programs. So the C-style octal escapes break nothing.

Docs

  • parser.h no longer claims quasiquote support.
  • The developer guide's types table now says false and #f read as nil, and lists bytes literals.

Test (local, Windows MSVC 14.38 Release, cvcpkg prefix)

  • state_exec_grammar_test 4/4, state_exec_corpus_test 1/1, state_exec_parser_test 30/30 and state_exec_unicode_test 23/23. The new tests also pass under ctest.
  • check_test_targets.py is OK, and clang-format 18 is clean.
  • Mutation checks: renaming a production in the doc fails both doc tests, and editing one golden fails the corpus test.

… 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.
@transfix
transfix merged commit a5b6330 into master Oct 17, 2026
15 checks passed
@transfix
transfix deleted the feat/state-exec-grammar-p0 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