-
-
Save achudnov/f3af65f11d5162c73064 to your computer and use it in GitHub Desktop.
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 #-} | |
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