Skip to content

Fix token filtering when reusing repetition helpers - #1678

Open
vera-rubin wants to merge 2 commits into
lark-parser:masterfrom
vera-rubin:fix/repetition-token-filtering
Open

vera-rubin wants to merge 2 commits into
lark-parser:masterfrom
vera-rubin:fix/repetition-token-filtering

Conversation

@vera-rubin

Copy link
Copy Markdown

When ordinary and pinned rules repeat the same terminal, the first rule's filtering setting is reused for both. This prints [] instead of [Token('_A', 'a')], although pinned rules should keep all their terminals:

from lark import Lark

parser = Lark('''start: dropped "," kept
dropped: _A+
!kept: _A+
_A: "a"
''')
print(parser.parse("a,a").children[1].children)

We include keep_all_tokens in cache keys for repetition helpers so each rule keeps its own filtering behavior. The regression covers both definition orders for +, *, and large ranged repetitions, and the full test suite passes.

— ChipLab

Include keep_all_tokens in repetition cache keys so pinned and ordinary rules don't share helpers with incompatible token filtering. Cover recursive and large ranged repetitions in both rule orders.
@erezsh

erezsh commented Dec 1, 2026

Copy link
Copy Markdown
Member

It fixes the reported bug, but it breaks some LALR grammars that load fine on master, and it leaves the underlying cache-key flaw in place.

  • Reported case is fixed: the new test fails on base and passes on the PR head, and the full suite passes there.
  • Regression: the key now includes keep_all_tokens unconditionally, so a ! rule and a normal rule stop sharing a helper even when the repeated expression has nothing to filter. This grammar parses xxb on master and raises a Reduce/Reduce GrammarError at load on the PR head:
start: a | b
a: x+ "a"
!b: x+ "b"
x: "x"

The same happens with a named terminal (NAME+) in the shared prefix, which is a realistic shape.

  • Root cause not fixed: terminal equality ignores filter_out, and keep_all_tokens is only one way it can differ. With A: "a", x: A+ and y: "a"+ still share one helper, so whichever is defined first decides the filtering for both. This is the same on master.

The new test's ~50 cases sit exactly on REPEAT_BREAK_THRESHOLD, so they would stop exercising the helper path if that constant were raised; 60 or 100, as other tests use, avoids this.

Terminals compare by name only, so equal expressions can still differ in
which tokens they filter out: through keep_all_tokens, or because an
anonymous string resolves to a named terminal. Add each terminal's
effective filter_out to the cache key instead of keep_all_tokens itself.

Rules that filter the repeated expression the same way share a helper
again, so a pinned rule and an ordinary rule with a common repeated
prefix no longer collide in LALR.

Move the test sizes off REPEAT_BREAK_THRESHOLD.
@vera-rubin

Copy link
Copy Markdown
Author

Thanks for the review. I pushed a follow-up commit.

The cache key no longer includes keep_all_tokens. It now records, for each terminal in the repeated expression, whether the current rule filters it out. Rules that filter it the same way share the helper as on master, so your x+ / !b grammar and the NAME+ variant load and parse again. x: A+ and y: "a"+ now get separate helpers: x keeps its tokens and y drops them, in either order.

Tests: sizes are 60 now, plus two new tests, one for your regression grammar (x and NAME; +, ~60, ~2..60) and one for the named/anonymous case in both orders.

One limit: if two rules really filter a shared repeated prefix differently (e.g. a: "x"+ "a" and !b: "x"+ "b"), LALR still reports Reduce/Reduce at load, since the trees must differ. On master those load, but one rule gets the wrong tree.

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.

2 participants