haskell / attoparsec

A fast Haskell library for parsing ByteStrings
http://hackage.haskell.org/package/attoparsec
Other
514 stars 93 forks source link

some strange infinite loop problems. #141

Closed Handora closed 6 years ago

Handora commented 6 years ago

I have no idea why the below question will introduce infinite loop problems.

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
Handora commented 6 years ago

Sorry, I found that (many' pChar >>= \s -> return (DontNeedTrim, s)) can cause it to consume empty string forever