Skip to content

Parser: feed the input given back at end of input to the next parser - #3273

Open
GuillaumedeVolpiano wants to merge 1 commit into
composewell:masterfrom
GuillaumedeVolpiano:fix-parser-seq-extract-backtrack
Open

GuillaumedeVolpiano wants to merge 1 commit into
composewell:masterfrom
GuillaumedeVolpiano:fix-parser-seq-extract-backtrack

Conversation

@GuillaumedeVolpiano

@GuillaumedeVolpiano GuillaumedeVolpiano commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3270.

Problem

many p <* eof accepts an input that ends inside an element: with the parser
of the issue, nab:ncd gives Right ["ab"] instead of an error.

The issue blames many, but many is correct: when the input ends inside an
element, its extract returns FDone (- cnt) and so gives back the input
consumed by the failed element. The fault is in the sequencing combinators
splitWith (<*>, <*), split_ (*>) and concatMap (>>=). In
extract, when the left parser returns FDone n and the right parser
initializes with IPartial, they call the extract of the right parser at
once. The right parser is never driven on the n elements given back, so eof
succeeds, and any other parser misses that input.

This affects any left parser that backtracks on extract, not only many.

Change

In the extract of splitWith, split_ and concatMap, when the left parser
returns FDone n with n /= 0, return FContinue n with the right parser
state, so that the driver backtracks and feeds it the remaining input. When
n == 0 the right parser is extracted as before.

The noErrorUnsafe* variants have the same code and are not changed, as they
are documented not to work with parsers that can fail.

Tests

Ten cases added to the common parser tests, run by the Data.Parser and
Data.ParserK test suites:

  • many p <* eof, many p *> eof and many p >>= ... eof fail when the input ends inside an element.
  • A parser sequenced after many with <*>, *> or >>= receives the incomplete element.
  • Three controls that pass with or without the change: many alone, a well formed input, and an element stopped by a wrong input.

Without the change 7 of the 10 fail, with it all pass. Data.Parser (209
examples) and Data.ParserK (121 examples) pass with GHC 9.12.4. The other
test suites and the benchmarks were not run locally.

🤖 Generated with the help of Claude Code

When the left parser of splitWith, split_ or concatMap finished on
extract with a backtrack count, the right parser was extracted at once,
without being driven on the input given back. So in "many p <* eof",
when the input ended inside an element, eof succeeded even though the
input of the failed element was left unconsumed.

Return FContinue with the right parser in that case, so that the driver
backtracks and feeds it the remaining input.

The noErrorUnsafe variants are not changed.

Fixes composewell#3270

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_019RtUYCYCwddhe8t5hVfLiv
@harendra-kumar

Copy link
Copy Markdown
Member

this may take a bit more time. I am aware of this issue and a few more similar ones and I have a pending change which may be a bit more broad in several parser functions, but I need to find time to get to that. If you are stuck because of this let me know and I can prioritize it. This PR may be a subset of my changes and if it aligns with those we can merge this first.

@GuillaumedeVolpiano

Copy link
Copy Markdown
Contributor Author

No hurry, in the meantime I've designed an integration of flatparse into streamly, so my code doesn't rely on this anymore. Anyhow, happy to help once we're done with takeInterval if you want me to ;)

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.

Parser: many accepts input that ends inside an element

2 participants