Haskell Attoparsec 无限循环

Haskell Attoparsec infinite loop

该代码基于 Haskell Attoparsec,当我使用 parseOnly pString "v" 时,它给我的正确答案是 Right (DontNeedTrim, "v")

而当我使用指令 parseOnly (many' pString) "v" 时,它似乎掉入 infinite loop 并最终因堆栈溢出而失败。

data Signal = NeedTrim
              | DontNeedTrim
              deriving (Show)

pString :: Parser (Signal, [Char])
pString = ((char '\"' *> many' pChar' <* char '\"') >>= \s -> return (NeedTrim, s))
          <|> (many' pChar >>= \s -> return (DontNeedTrim, s))

pChar :: Parser Char
pChar = char '\' *> (pEscape <|> spaces *> endOfLine *> pChar)
        <|> satisfy (`C.notElem` "\"\\n#;")

pChar' :: Parser Char
pChar' = char '\' *> pEscape
         <|> satisfy (`C.notElem` "\\"")

pEscape :: Parser Char
pEscape = choice (zipWith decode "bnt\\"" "\b\n\t\\"")
  where decode c r = r <$ char c

pString 中的第二个选项接受空字符串:many' pChar >>= \s -> return (...)。因此 many' pString 无限地消耗空字符串。