Last active
December 14, 2015 12:38
-
-
Save carlohamalainen/5087207 to your computer and use it in GitHub Desktop.
Response to Immanuel Normann on haskell-cafe thread "simple parsec question"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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