当我的解析器明确寻找它时,为什么我得到 "unexpected end of input"?

Why do I get "unexpected end of input" when my parser is explicitly looking for it?

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

运行时会产生:

parse error at (line 3, column 1): unexpected end of input

当我删除显式 eof 匹配时,没有这样的解析错误:

fields = many (try field)

我也试过 try field `manyTill` eof,但这会导致与原始代码相同的行为。

我想确保解析器使用整个输入,我该怎么做?

问题是 eof 之前有一个换行符(由 unlines 插入)。

所以 eof 必须是 运行 到 lexeme

fields = many (try field) <* lexeme eof

否则 Parsec 正在尝试 运行 换行符上的 fields 解析器。