Skip to content

Instantly share code, notes, and snippets.

@basvandijk
Created February 4, 2012 12:13
Show Gist options
  • Save basvandijk/1737483 to your computer and use it in GitHub Desktop.
Save basvandijk/1737483 to your computer and use it in GitHub Desktop.
Bug when parsing lazy text
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Applicative
import Control.Monad
import Data.Monoid (mappend)
import System.Environment
import System.Exit
import System.IO
import qualified Data.Attoparsec.Text as A
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Attoparsec.Text.Lazy as AL
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TL
import Data.Attoparsec.Combinator
main :: IO ()
main = do
args <- getArgs
case args of
[fp, "lazy"] -> do lazyTxt <- TL.readFile fp
case AL.parse t lazyTxt of
AL.Done _ x -> putStrLn "OK"
AL.Fail txt ctx msg -> do
hPutStrLn stderr "Error:"
hPutStrLn stderr $ show $ TL.take 200 txt
[fp, "strict"] -> do strictTxt <- T.readFile fp
case handleStrictResult $ A.parse t strictTxt of
Left err -> hPutStrLn stderr err
Right hoogleDB -> putStrLn "OK"
_ -> hPutStrLn stderr "Oops!" >> exitFailure
handleStrictResult :: A.Result r -> Either String r
handleStrictResult (A.Done _ r) = Right r
handleStrictResult (A.Fail _ _ msg) = Left msg
handleStrictResult (A.Partial f) = handleStrictResult $ f ""
-- TODO: Understand why this is necessary.
data E = E
t :: A.Parser [[E]]
t = many (d a) <* A.endOfInput
d :: A.Parser a -> A.Parser a
d p = many (A.takeWhile (== 'd') <* A.endOfLine) *> p
a :: A.Parser [E]
a = A.char 'a' *> A.endOfLine *> many (d e)
e :: A.Parser E
e = empty
a
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
ddddddddddddddddddddd
a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment