Skip to content

Instantly share code, notes, and snippets.

@achudnov
Last active December 14, 2015 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achudnov/f3af65f11d5162c73064 to your computer and use it in GitHub Desktop.
Save achudnov/f3af65f11d5162c73064 to your computer and use it in GitHub Desktop.
{-# LANGUAGE FlexibleContexts #-}
module Parser where
import Text.Parsec
import Control.Monad
testString1 = "top 1:\n\nsome text ... bla\n\ntop 2:\n\nmore text ... bla bla" -- test fails, test2 reads but incorrectly
testString2 = "top 1:\nsome text ... bla\nblabla\ntop 2:\n" -- test reads but incorrectly, test2 reads but incorrectly
testString3 = "top 1:\nsome text ... bla\n" -- test ok, test2 fails
test s = parse parser "" s
parser :: Parsec String () [(String, String)]
parser = many $ liftM2 (,) headline content
where headline = do s <- many $ noneOf ":"
char ':' >> newline
return s
content = anyChar `manyTill`
(try $ eof <|> (newline >> headline >> return ()))
test2 s = parse section "test.txt" s
data Top = Top String deriving (Show)
data Content = Content String deriving (Show)
data Section = Section Top Content deriving (Show)
headline :: Stream s m Char => ParsecT s u m Top
headline = manyTill anyChar (char ':' >> newline) >>= return . Top
content :: Stream s m Char => ParsecT s u m Content
content = manyTill anyChar (try headline) >>= return . Content
section :: Stream s m Char => ParsecT s u m Section
section = do {h <- headline; c <- content; return (Section h c)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment