Skip to content

Instantly share code, notes, and snippets.

@kuribas
Last active September 20, 2018 06:59
Show Gist options
  • Select an option

  • Save kuribas/ff3dbea58f95b7980047cb226b0a8d99 to your computer and use it in GitHub Desktop.

Select an option

Save kuribas/ff3dbea58f95b7980047cb226b0a8d99 to your computer and use it in GitHub Desktop.
simple parser
module SimpleParser where
import Text.Parsec
import Data.Text as T
import Data.Char
type SimpleParser a b = Parsec [a] () b
parseMaybe :: Show a => (a -> Maybe b) -> SimpleParser a b
parseMaybe f = tokenPrim show (\s _ _ -> incSourceColumn s (sourceColumn s)) f
satisfy :: Show a => (a -> Bool) -> SimpleParser a a
satisfy f = parseMaybe (\x -> if f x then Just x else Nothing)
emptyLine :: SimpleParser Text Text
emptyLine = SimpleParser.satisfy (T.all isSpace)
runSimpleParser :: SimpleParser a b -> [a] -> Maybe b
runSimpleParser p stream =
case runParser p () "" stream of
Left _ -> Nothing
Right r -> Just r
execSimpleParser :: SimpleParser a b -> [a] -> b
execSimpleParser p stream =
case runParser p () "" stream of
Left _ -> error "parser failed"
Right r -> r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment