You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Parser: many accepts input that ends inside an element #3270
importData.Functor.IdentityimportqualifiedStreamly.Data.FoldasFoldimportqualifiedStreamly.Data.ParserasPimportqualifiedStreamly.Data.StreamasS-- "many" of ('n', then one or more non-':' characters, then ':'), then eof.line::P.ParserCharIdentityString
line =P.satisfy (=='n') *>P.takeWhile1 (/=':') Fold.toList <*P.satisfy (==':')
p::P.ParserCharIdentity [String]
p =P.many line Fold.toList <*P.eof
main::IO()
main =dolet 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.
Activity