6 Oct 17:38
I want my free (and good looking) parser!
From: Slavomir Kaslev <slavomir.kaslev <at> gmail.com>
Subject: I want my free (and good looking) parser!
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-10-06 15:38:58 GMT
Subject: I want my free (and good looking) parser!
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-10-06 15:38:58 GMT
I am writing a Parsec parser for a C-like language and I have several datas that
look more or less like this one:
> import Control.Monad( liftM )
> import Text.ParserCombinators.Parsec
> data FooBar = Foo | Bar
> deriving (Show,Read,Bounded,Enum)
Looking at these, it seems that there should be no need to write a parser for
this guy by hand. We already know that it is bounded and enumerable, so we can
get a list of all possible FooBars:
> enumAll :: (Bounded a, Enum a) => [a]
> enumAll = enumFromTo minBound maxBound
Also, we know how to show and read each FooBar. Therefore, I should get a free
parser!
> freeParser :: (Enum a, Bounded a, Show a, Read a) => Parser a
Here is one use of freeParser:
> paramMod = option Foo freeParser
> test = parseTest $ do { x <- paramMod; eof; return x }
Not suprisingly:
test "Foo" => Foo
test "Bar" => Bar
test "" => Foo
(Continue reading)
RSS Feed