如果从文件中读取,Parsec 失败且没有错误

Parsec fails without error if reading from file

我写了一个小的 parsec 解析器来从用户提供的输入字符串或输入文件中读取样本。如果输入以分号分隔的字符串形式提供,它会在错误输入时正确失败并显示有用的错误消息:

> readUncalC14String "test1,7444,37;6800,36;testA,testB,2000,222;test3,7750,40"
*** Exception: Error in parsing dates from string: (line 1, column 29):
unexpected "t"
expecting digit

但是对于具有相同条目的输入文件 inputFile.txt,它会静默失败:

test1,7444,37
6800,36
testA,testB,2000,222
test3,7750,40
> readUncalC14FromFile "inputFile.txt"
[UncalC14 "test1" 7444 37,UncalC14 "unknownSampleName" 6800 36]

为什么会这样,我怎样才能让 readUncalC14FromFile 以有用的方式失败?

这是我的代码的最小子集:

import qualified Text.Parsec                    as P
import qualified Text.Parsec.String             as P

data UncalC14 = UncalC14 String Int Int deriving Show

readUncalC14FromFile :: FilePath -> IO [UncalC14]
readUncalC14FromFile uncalFile = do
    s <- readFile uncalFile
    case P.runParser uncalC14SepByNewline () "" s of
        Left err -> error $ "Error in parsing dates from file: " ++ show err
        Right x -> return x
    where
        uncalC14SepByNewline :: P.Parser [UncalC14]
        uncalC14SepByNewline = P.endBy parseOneUncalC14 (P.newline <* P.spaces)

readUncalC14String :: String -> Either String [UncalC14]
readUncalC14String s = 
    case P.runParser uncalC14SepBySemicolon () "" s of
        Left err -> error $ "Error in parsing dates from string: " ++ show err
        Right x -> Right x
    where 
        uncalC14SepBySemicolon :: P.Parser [UncalC14]
        uncalC14SepBySemicolon = P.sepBy parseOneUncalC14 (P.char ';' <* P.spaces)

parseOneUncalC14 :: P.Parser UncalC14
parseOneUncalC14 = do
    P.try long P.<|> short
    where
        long = do
            name <- P.many (P.noneOf ",")
            _ <- P.oneOf ","
            mean <- read <$> P.many1 P.digit
            _ <- P.oneOf ","
            std <- read <$> P.many1 P.digit
            return (UncalC14 name mean std)
        short = do
            mean <- read <$> P.many1 P.digit
            _ <- P.oneOf ","
            std <- read <$> P.many1 P.digit
            return (UncalC14 "unknownSampleName" mean std)

这里发生的是你输入的前缀是一个有效的字符串。要强制 parsec 使用整个输入,您可以使用 eof 解析器:

uncalC14SepByNewline = P.endBy parseOneUncalC14 (P.newline <* P.spaces) <* P.eof

一个有效而另一个无效的原因是 sepByendBy 之间的差异。这是一个更简单的例子:

sepTest, endTest :: String -> Either P.ParseError String
sepTest s = P.runParser (P.sepBy (P.char 'a') (P.char 'b')) () "" s
endTest s = P.runParser (P.endBy (P.char 'a') (P.char 'b')) () "" s

这里有一些有趣的例子:

ghci> sepTest "abababb"
Left (line 1, column 7):
unexpected "b"
expecting "a"

ghci> endTest "abababb"
Right "aaa"

ghci> sepTest "ababaa"
Right "aaa"

ghci> endTest "ababaa"
Left (line 1, column 6):
unexpected "a"
expecting "b"

如您所见,sepByendBy 都可以无提示地失败,但是如果前缀没有以分隔符 bendBy 如果前缀没有在主解析器中结束,则静默失败 a.

因此,如果您想确保阅读整个 file/string,您应该在两个解析器之后使用 eof