Skip to content

Instantly share code, notes, and snippets.

@danlei
Created July 2, 2010 15:46
Show Gist options
  • Save danlei/461535 to your computer and use it in GitHub Desktop.
Save danlei/461535 to your computer and use it in GitHub Desktop.
ConstituentParser
module ConstituentParser
where
import Text.ParserCombinators.Parsec
import Control.Monad
import Control.Applicative ((<$>))
symbol = oneOf "!#$%&|*+-/:<=>?@^_~"
parseSymbol = Symbol <$> many1 (letter <|> digit <|> symbol)
parseList = List <$> sepBy parseConstituent spaces
parseConstituent = try (skipMany space >> parseSymbol)
<|> try (do skipMany space
char '('
x <- parseList
char ')'
return x)
data Constituent = Symbol String
| List [Constituent]
instance Show Constituent where
show (Symbol x) = x
show (List x) = "(" ++ unwords (map show x) ++ ")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment