Skip to content

Instantly share code, notes, and snippets.

@jtdaugherty
Created March 27, 2012 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtdaugherty/2218723 to your computer and use it in GitHub Desktop.
Save jtdaugherty/2218723 to your computer and use it in GitHub Desktop.
Parsec + Haskell stuff
module Main where
import Text.ParserCombinators.Parsec
( Parser
, parse
, string
, try
)
import Data.Foldable
( asum
)
data Thing = Thing1
| Thing2
| Thing3
deriving (Show)
pairs :: [(String, Thing)]
pairs = [ ("thing1", Thing1)
, ("thing2", Thing2)
, ("thing3", Thing3)
]
parseThing :: Parser Thing
parseThing = asum parsers
where
parsers = map mkParser pairs
mkParser (str, thing) =
try (string str >> return thing)
main :: IO ()
main = do
print $ parse parseThing "<stdin>" "thing1"
print $ parse parseThing "<stdin>" "thing2"
print $ parse parseThing "<stdin>" "thing3"
print $ parse parseThing "<stdin>" "invalid input"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment