Created
May 3, 2022 12:03
-
-
Save lovelymono/57fadbc768d9db8f61b0647f6d39d8c9 to your computer and use it in GitHub Desktop.
Baseline for parser combinators in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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