haskell-json icon indicating copy to clipboard operation
haskell-json copied to clipboard

Bug in spanP

Open SpectreVert opened this issue 3 years ago • 0 comments

spanP is defined as below:

spanP :: String           -- description
      -> (Char -> Bool)   -- predicate
      -> Parser String
spanP desc = many . parseIf desc

Using many here instead of some makes it so matching nothing or even getting an empty input is not an error.

How to reproduce

> ghci Main.hs
λ> runParser (spanP "integer" isDigit) $ Input 0 ""
Right (Input {inputLoc = 0, inputStr = ""},"")
λ> runParser (spanP "integer" isDigit) $ Input 0 "abcde"
Right (Input {inputLoc = 0, inputStr = "abcde"},"")

As of now spanP creates a parser which does not return an error on parse failure of string.

I'm not sure if that is the intended behavior but that might bring some trouble later on.

N.B: See many / some documentation here.

I'm aware that for removing optional whitespaces and such the many is required. Maybe there should be a spanP' that uses some instead for the other cases.

SpectreVert avatar Mar 18 '21 00:03 SpectreVert