Skip to content

[csharp/v8-spec] Continuation of improvements.#4799

Merged
teverett merged 14 commits into
antlr:masterfrom
kaby76:g4-v8-continue
May 2, 2026
Merged

[csharp/v8-spec] Continuation of improvements.#4799
teverett merged 14 commits into
antlr:masterfrom
kaby76:g4-v8-continue

Conversation

@kaby76
Copy link
Copy Markdown
Contributor

@kaby76 kaby76 commented Apr 4, 2026

This is a continuation on the improvements for the csharp/v8-spec/ grammar. The changes here refactor a few more rules to reduce max-k's. The parser now completes the test suite twice as fast as compared to the grammar from the specification.

There are still quite a few performance issues, but I'm committing this group of changes as a checkpoint.

@teverett All set for merge. Ty.

kaby76 and others added 10 commits April 4, 2026 07:04
…toring.

Three rules — null_coalescing_expression, expression, and range_expression —
had alternatives sharing a long common prefix (unary_expression or
conditional_or_expression), forcing ANTLR4's SLL simulation to scan up to 30
tokens before resolving the decision. Left-factor each rule so the shared
prefix is consumed unconditionally and only a single token of lookahead is
needed for the optional suffix. Add design docs explaining the problem and
fix for each rule.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… (decision 31)

Extend IsConstantPatternAhead() to return false when LT(1) is '(' and a
comma appears at parenthesis depth 1 — indicating a tuple-positional pattern
such as ("rock", "scissors") or (0, 0), which cannot be a compile-time
constant.  Add IsPositionalPatternAhead() as its complement and gate the
positional_pattern alternative in the grammar with it, making alts 2 and 4
mutually exclusive.  Add design/pattern_positional.md documenting the
ambiguity and the fix.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… to all targets

Apply the same tuple-comma check and IsPositionalPatternAhead complement to all
remaining language targets: Antlr4ng, Cpp (header + impl), Dart, Go, Java,
JavaScript, Python3, and TypeScript.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kaby76 kaby76 marked this pull request as ready for review April 16, 2026 12:52
Copilot AI review requested due to automatic review settings April 16, 2026 12:52
@kaby76 kaby76 marked this pull request as draft April 16, 2026 12:53
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues performance-oriented refactoring of the csharp/v8-spec ANTLR grammar, primarily by left-factoring ambiguous rules to reduce SLL lookahead (max-k) and by adding/adjusting semantic predicates for pattern disambiguation. It also adds design notes documenting the underlying ambiguity/perf issues and the chosen fixes.

Changes:

  • Left-factored several expression-related rules (range_expression, null_coalescing_expression, expression, plus a refactor of conditional_expression) to reduce max-k.
  • Introduced IsPositionalPatternAhead and expanded IsConstantPatternAhead across all targets, and gated positional_pattern in pattern to address constant_pattern vs positional_pattern ambiguity.
  • Added multiple design documents explaining the root causes and refactor strategy; small template/metadata tweaks.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
csharp/v8-spec/design/range_expression.md New design note explaining SLL max-k issue and left-factoring fix for range_expression.
csharp/v8-spec/design/null_coalescing_expression.md New design note explaining SLL max-k issue and left-factoring fix for null_coalescing_expression.
csharp/v8-spec/design/expression.md New design note explaining SLL max-k issue and left-factoring fix for expression.
csharp/v8-spec/design/pattern_positional.md New design note describing constant_pattern vs positional_pattern disambiguation strategy.
csharp/v8-spec/design/primary_expression_mlr.md New design note describing removal of mutual left-recursion in primary_expression.
csharp/v8-spec/desc.xml Removes a comment from the test descriptor (no functional change).
csharp/v8-spec/CSharpParser.g4 Adds predicate gate for positional_pattern; left-factors range_expression, null_coalescing_expression, conditional_expression, and expression.
csharp/v8-spec/CSharp/CSharpParserBase.cs Extends IsConstantPatternAhead and adds IsPositionalPatternAhead.
csharp/v8-spec/Java/CSharpParserBase.java Extends IsConstantPatternAhead and adds IsPositionalPatternAhead.
csharp/v8-spec/Go/CSharpParserBase.go Extends IsConstantPatternAhead and adds IsPositionalPatternAhead.
csharp/v8-spec/Dart/CSharpParserBase.dart Extends IsConstantPatternAhead and adds IsPositionalPatternAhead.
csharp/v8-spec/JavaScript/CSharpParserBase.js Extends IsConstantPatternAhead and adds IsPositionalPatternAhead.
csharp/v8-spec/TypeScript/CSharpParserBase.ts Extends IsConstantPatternAhead and adds IsPositionalPatternAhead.
csharp/v8-spec/Antlr4ng/CSharpParserBase.ts Extends IsConstantPatternAhead and adds IsPositionalPatternAhead.
csharp/v8-spec/Python3/CSharpParserBase.py Extends IsConstantPatternAhead and adds IsPositionalPatternAhead.
csharp/v8-spec/Cpp/CSharpParserBase.h Declares IsPositionalPatternAhead.
csharp/v8-spec/Cpp/CSharpParserBase.cpp Extends IsConstantPatternAhead and defines IsPositionalPatternAhead.
_scripts/templates/CSharp/st.Test.cs Updates ambig flag parsing and adjusts start-rule lookup placeholder in the template.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread csharp/v8-spec/CSharp/CSharpParserBase.cs
Comment thread csharp/v8-spec/CSharpParser.g4
Comment thread csharp/v8-spec/CSharpParser.g4 Outdated
Comment thread csharp/v8-spec/design/primary_expression_mlr.md
Comment thread csharp/v8-spec/design/pattern_positional.md Outdated
…ional_expression, doc cleanup

- All targets: fix IsConstantPatternAhead() to detect type-headed positional
  patterns like Point(0,0) where LT(1) is an identifier, not '('.
  C#/Java/Python3/Cpp use speculative type_() parse; Antlr4ng/TS/JS/Go/Dart
  use LT(1)==identifier && LT(2)=='(' heuristic.
- CSharpParser.g4: rewrite conditional_expression to use explicit '?'
  optional instead of the empty-alt form.
- design/primary_expression_mlr.md: fix stray backslashes in code blocks
  (change '\' to '//' to match commenting style).
- design/pattern_positional.md: correct last table row — alt 2 is false
  (speculative type_() + '(' suppresses it), alt 4 is true.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kaby76 kaby76 marked this pull request as ready for review April 17, 2026 10:30
kaby76 and others added 2 commits April 17, 2026 07:22
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… before left-factoring

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@KvanTTT KvanTTT added the csharp label Apr 22, 2026
@teverett
Copy link
Copy Markdown
Member

teverett commented May 2, 2026

@kaby76 thanks!

@teverett teverett merged commit a763144 into antlr:master May 2, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants