Skip to content

refactor(webui): verify the Berry lexer against the vendored C lexer - #40

Open
dzid26 wants to merge 2 commits into
mainfrom
refactor/berry-lexer-provenance
Open

dzid26 wants to merge 2 commits into
mainfrom
refactor/berry-lexer-provenance

Conversation

@dzid26

@dzid26 dzid26 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

What

This is the lexer-provenance follow-up to #35. The preprocessor already parses calls into nodes and the compiler oracle already proves the generated .bep parses. This PR makes the TypeScript lexer itself checkable against the vendored Berry lexer and removes the last hand-synced lexical tables.

Changes

  • Delete the one-time old-parser differential. The fix(webui): make the DBC preprocessor lexer-faithful #35 PR body records the 0-diff migration result; the C compiler and C lexer are the durable references.
  • Share the compiler-oracle source list. firmware/tools/parse_check_sources.txt now feeds both parse_check_examples.sh and the Node oracle test.
  • Add firmware/tools/lex_dump.c. A test-only harness dumps numeric token type, decoded value, and line from be_lexer.c / be_parser.c. It uses numeric types because be_tokentype2str is off by one for keywords.
  • Generate the lexer tables. webui/scripts/gen-lexer-tables.ts probes the C harness for keywords, punctuation, and simple escapes, writes lexer-tables.ts, and writes lexer-source.lock. The lock hashes normalized text so Windows and Linux agree.
  • Add lexer-conformance.test.ts. It compares the TypeScript token stream with the C stream over all real .be files and a valid edge-case corpus covering both comment forms, both quote styles, every escape family, adjacent literals, line endings, call vs grouping (, and all keywords/operators. It also asserts the TS lexer reports no errors where the C lexer accepts the input.
  • Refactor lexer.ts. scanString is flattened into decodeEscape + unfinishedString; the hand tables and hexValue are gone. 1. is a real, 1..2 stays 1 .. 2, trailing malformed suffixes recover like C, and a leading ( is call-form (C resets had_whitespace before the first token).
  • CI/docs. The webui workflow watches the harness, manifest, be_lexer.h, and berry_conf.h. The README documents both native gates and the regeneration command.

Verification

  • npm test: 201 pass, including the compiler oracle, lexer conformance, and fuzz invariants.
  • npm run check: 0 errors (2 pre-existing Svelte warnings).
  • node webui/scripts/gen-lexer-tables.ts: idempotent; no diff after running.
  • firmware/tools/parse_check_examples.sh test_scripts/bench_test.be: OK.

Limits

  • F-strings stay opaque in the TypeScript lexer and are excluded from token conformance; calls inside interpolation remain a documented limitation.
  • \xNN values above ASCII are byte strings in C but JS code points in TS; the conformance corpus keeps escaped values ASCII. DBC names are ASCII.
  • The conformance test skips locally without gcc and fails in CI.

PR 2 will follow for the policy layer: move the argument-shape helpers to calls.ts, add the top-level/wholeCall argument view, and extract the variable tracker to berry/tracker.ts.

The parse_check build list now lives in firmware/tools/parse_check_sources.txt and is used by both the shell helper and the Node test. The one-time old-preprocessor differential is deleted; the #35 result is its migration record, and the C compiler is the durable oracle.
A test-only C harness dumps numeric token types, decoded values, and lines from be_lexer.c. The generator probes it for keywords, punctuation, and simple escapes, writes the static tables plus a source lock, and the conformance test compares the TypeScript stream over the real corpus and lexical edge cases. The hand-synced tables are gone, scanString is flattened, and Berry's 1. real and leading-( call forms now match the C lexer.

This branch has not been deployed

No deployments
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