Parser: many accepts input that ends inside an element

Open
#3,270 0 comments 0 reactions 0 assignees View on GitHub

@GuillaumedeVolpiano is already working on this.

Since Nov 20, 2026.

  • #3273 by @GuillaumedeVolpiano — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
backend

Research direction

Start with the reproduction using P.many, line, and P.eof, and compare the reported results for parse and parsePos. The issue links #1491 as possibly related; read it for context on parser alternatives and backtracking. Done means malformed final elements fail consistently, with behavior verified against all four inputs in the reproduction.

Written by the indexing model from the issue text.

Description

Package: streamly-core 0.3.3
GHC: 9.12.4

Reproduction

import Data.Functor.Identity
import qualified Streamly.Data.Fold as Fold
import qualified Streamly.Data.Parser as P
import qualified Streamly.Data.Stream as S

-- "many" of ('n', then one or more non-':' characters, then ':'), then eof.
line :: P.Parser Char Identity String
line = P.satisfy (== 'n') *> P.takeWhile1 (/= ':') Fold.toList <* P.satisfy (== ':')

p :: P.Parser Char Identity [String]
p = P.many line Fold.toList <* P.eof

main :: IO ()
main = do
  let run s = (runIdentity (S.parse p (S.fromList s)), runIdentity (S.parsePos p (S.fromList s)))
  print (run "nab:ncd:")
  print (run "nab:ncd")
  print (run "nab:x")
  print (run "ncd")
Input Expected Actual
nab:ncd: Right ["ab","cd"] Right ["ab","cd"]
nab:ncd Left: ncd is not a line Right ["ab"]
nab:x Left: x is not a line Left (ParseError "eof: not at end of input")
ncd Left Right []

What happens

When the input ends while many is in the middle of an element, the element
fails, many stops with the elements it had, and the input the failed element
had consumed is not given back: eof then succeeds. When the element fails on
a character instead (nab:x), the input is given back and eof fails as
expected.

So a malformed last element goes unnoticed whenever the input stops inside it.
I expected many to backtrack to the start of the failed element in both
cases, as it does for nab:x.

The same happens with parsePos.

Possibly related, though about the alternative of parsers and closed: #1491.

Dominant language
Haskell
Stars
926
Forks
71
Avg merge
6h 49m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from composewell/streamly

All issues in composewell/streamly

Similar issues

More Haskell issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.