Last active
December 25, 2015 07:59
-
-
Save l3kn/6943542 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
module Main where | |
import Text.ParserCombinators.Parsec | |
data ListItem = List ListItem [ListItem] | |
| Value String | |
| Property String String | |
unwordsList :: [ListItem] -> String | |
unwordsList = unwords . map showVal | |
instance Show ListItem where show = showVal | |
showVal :: ListItem -> String | |
showVal (List main content) = show main ++ "(\n" ++ unwordsList content ++ "\n)" | |
showVal (Value string) = string | |
parseList :: Int -> Parser ListItem | |
parseList n = do first <- parseItem n | |
rest <- many $ try $ try (parseList (n+1)) <|> parseItem (n+1) | |
return $ List first rest | |
parseItem :: Int -> Parser ListItem | |
parseItem n = do count n (char ' ') | |
string <- manyTill anyChar newline | |
return $ Value string | |
{- | |
parseProperty :: -> Parser ListItem | |
parsePropenty = do name <- manyTill ':' | |
value <- manyTill anyChar newline | |
return $ Property name value | |
-} | |
str :: String | |
str = unlines [ | |
"body", | |
" top: 1", | |
" left: 1", | |
" a", | |
" top: 1" | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment