import Control.Applicative hiding (many)
import Text.Parsec
import Text.Parsec.String
lexeme :: Parser a -> Parser a
lexeme p = many (oneOf " \n\r") *> p
identifier :: Parser String
identifier = lexeme $ many1 $ oneOf (['a'..'z'] ++ ['A'..'Z'])
operator :: String -> Parser String
operator = lexeme . string
field :: Parser (String, String)
field = (,) <$> identifier <* operator ":" <*> identifier <* operator ";"
fields :: Parser [(String, String)]
fields = many (try field) <* eof
testInput :: String
testInput = unlines
[ " FCheckErrors : Boolean ;"
, " FAcl : TStrings ;"
]
main :: IO ()
main = parseTest fields testInput
At startup, this gives:
parse error in (row 3, column 1): unexpected end of input
When I remove the explicit eof mapping, there is no such parsing error:
fields = many (try field)
I also tried try field `manyTill` eof, but this will lead to the same behavior as the source code.
I want to make sure that the parser consumes all the input, how can I do this?
source
share