Last active
September 20, 2018 06:59
-
-
Save kuribas/ff3dbea58f95b7980047cb226b0a8d99 to your computer and use it in GitHub Desktop.
simple parser
This file contains hidden or 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
| 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