Skip to content

Instantly share code, notes, and snippets.

@carlohamalainen
Last active December 14, 2015 12: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 carlohamalainen/5087207 to your computer and use it in GitHub Desktop.
Save carlohamalainen/5087207 to your computer and use it in GitHub Desktop.
Response to Immanuel Normann on haskell-cafe thread "simple parsec question"
{-# LANGUAGE FlexibleContexts #-}
import Text.Parsec
import Control.Applicative hiding ((<|>),many)
-- Example input:
{-
top 1:
some text ... bla
top 2:
more text ... bla bla
-}
data Top = Top String deriving (Show)
data Content = Content [String] deriving (Show)
data Section = Section Top Content deriving (Show)
headline = do
t <- many1 (noneOf ":\n")
char ':'
newline
return $ Top t
contentLine = do
x <- many (noneOf ":\n")
newline
return x
content = do
line <- optionMaybe (try contentLine)
case line of Just x -> do xs <- content
return (x:xs)
_ -> return []
section = do
h <- headline
c <- Content <$> content
return $ Section h c
main = do
x <- readFile "simple.txt"
print $ parse (many section) "" x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment