Skip to content

Instantly share code, notes, and snippets.

@lovelymono
Created May 3, 2022 12:03
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 lovelymono/57fadbc768d9db8f61b0647f6d39d8c9 to your computer and use it in GitHub Desktop.
Save lovelymono/57fadbc768d9db8f61b0647f6d39d8c9 to your computer and use it in GitHub Desktop.
Baseline for parser combinators in Haskell
import qualified Data.Text as T
data Error = EndOfInput | Unexpected Char
deriving (Eq, Show)
-- Parser from T.Text to some type a.
data Parser a = Parser { runParser :: T.Text -> Either Error (a, T.Text) }
satisfy :: (Char -> Bool) -> Parser Char
satisfy p = Parser $ \input ->
case T.uncons input of
Just (c, t) -> if p c then Right (c, t) else Left (Unexpected c)
Nothing -> Left EndOfInput
char :: Char -> Parser Char
char c = satisfy (== c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment