The code is based on Haskell Attoparsec
, and when I use parseOnly pString "v"
, it gives me the right answer as Right (DontNeedTrim, "v")
.
While when I use the instruction parseOnly (many' pString) "v"
, it seems drops into the infinite loop
and finally failed with the overflowed stack.
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
The second alternative in pString
accepts the empty string: many' pChar >>= \s -> return (...)
. Thus many' pString
keeps consuming the empty string ad infinitum.