Skip to content

docs: standalone runnable PoC of the grammar engine (#467)#489

Open
SJrX wants to merge 2 commits into
issue-345-19from
issue-345-20
Open

docs: standalone runnable PoC of the grammar engine (#467)#489
SJrX wants to merge 2 commits into
issue-345-19from
issue-345-20

Conversation

@SJrX

@SJrX SJrX commented Jun 23, 2026

Copy link
Copy Markdown
Owner

What

A single self-contained Kotlin file — poc/GrammarEnginePoc.kt — that models the new grammar engine with no IntelliJ or project dependencies (Kotlin stdlib only), so you can run it and step through it in a debugger without Gradle.

Stacked on #488 (issue-345-19). Lives in poc/, outside the source sets, so it is not compiled into the plugin.

Run it

kotlin poc/GrammarEnginePoc.kt
# or
kotlinc poc/GrammarEnginePoc.kt -include-runtime -d /tmp/poc.jar && java -jar /tmp/poc.jar
# or open it in IntelliJ and click Run on main()

What it shows

It mirrors the real grammar/ classes (Parse/Stuck/Matcher, Seq/Alt/ZeroOrMore/ZeroOrOne, Lit/Choice/Whitespace, validate, nextTokenChoices) and main() walks through four demos:

  1. CompletenessSeq(ZeroOrMore("a"), "a") accepts "aa" (the case a single greedy match fails).
  2. Validation + error localizationAF_BOGUSSemanticError; AF_INET, AF_INET6SyntaxError(furthest=7, expected=[<ws>]).
  3. CompletionnextTokenChoices("") / ("~") / ("AF_INET ") showing "what can come next" is the same expected-set question.
  4. Raw ParseStep stream — what you'd actually see in a debugger.

Sample output:

=== 1. Completeness: Seq(ZeroOrMore("a"), "a") ===
  aa   -> Valid
  ab   -> SyntaxError(furthest=1, expected=["a"])
=== 2. Validation + error localization ===
  AF_BOGUS             -> SemanticError(badToken=Token(start=0, end=8, text=AF_BOGUS, valid=false))
  AF_INET, AF_INET6    -> SyntaxError(furthest=7, expected=[<ws>])
=== 3. Completion: what can come next? ===
  after "~" -> [AF_INET, AF_INET6, AF_UNIX, AF_NETLINK, AF_PACKET]

The header comment explains the two core ideas (list-of-successes; failure-as-a-value) and maps the PoC names back to the real classes.

Refs #467

🤖 Generated with Claude Code

A single self-contained Kotlin file (stdlib only, no IntelliJ/project deps) under poc/
that models the new engine so it can be run and stepped through without Gradle:

  kotlin poc/GrammarEnginePoc.kt
  # or: kotlinc poc/GrammarEnginePoc.kt -include-runtime -d /tmp/poc.jar && java -jar /tmp/poc.jar
  # or: open in IntelliJ and Run main()

Mirrors the real grammar/ classes (Parse/Stuck/Matcher, Seq/Alt/ZeroOrMore/ZeroOrOne,
Lit/Choice/Whitespace, validate, nextTokenChoices) and main() demonstrates: the greedy
completeness win, validation + error localization, completion as the same expected-set
question, and the raw ParseStep stream you'd see in a debugger.

Lives outside the source sets, so it is not compiled into the plugin.

Refs #467

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown

Unit Test Results (grammar engine true)

1 159 tests  ±0   1 159 ✅ ±0   50s ⏱️ +2s
  308 suites ±0       0 💤 ±0 
  308 files   ±0       0 ❌ ±0 

Results for commit b79566e. ± Comparison against base commit 3002e15.

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown

Unit Test Results (grammar engine false)

1 159 tests  ±0   1 159 ✅ ±0   47s ⏱️ -3s
  308 suites ±0       0 💤 ±0 
  308 files   ±0       0 ❌ ±0 

Results for commit b79566e. ± Comparison against base commit 3002e15.

♻️ This comment has been updated with latest results.

Moved GrammarEnginePoc.kt to src/main/kotlin so IntelliJ runs main() directly, and
reworked it as a teaching aid (this branch is not merged/shipped):

- Seq.parse rewritten with the sequence { } builder (lazy, but plain for-loops + yield
  instead of flatMap/map), with clearer names (waysToHere / soFar) and the snapshot
  comment explaining why the lazy block must close over a captured value.
- Added eager List-based twins SeqEager / ZeroOrMoreEager that build the full result
  up front (no laziness) to make the logic and the recursion easier to follow.
- Demos: eager vs lazy produce identical validate() results; and the eager twin builds
  the whole ambiguous space (all 4 full parses of "aaa") where lazy validate stops at
  the first.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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