Skip to content

Instantly share code, notes, and snippets.

@beleon
Created November 9, 2020 11:05
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 beleon/97273023d3b95fb62d28da6bec273aeb to your computer and use it in GitHub Desktop.
Save beleon/97273023d3b95fb62d28da6bec273aeb to your computer and use it in GitHub Desktop.
data ParserBox a = ParserBox ParserBoxInfo (Either String (Parser a))
data ParserBoxInfo = ParserBoxInfo (Maybe String)
defaultParserBoxInfo :: ParserBoxInfo
defaultParserBoxInfo = ParserBoxInfo Nothing
instance Functor ParserBox where
fmap _ (ParserBox i (Left pe)) = ParserBox i $ Left pe
fmap f (ParserBox i (Right x)) = ParserBox i $ Right $ fmap f x
instance Applicative ParserBox where
pure = ParserBox defaultParserBoxInfo . Right . pure
(ParserBox i (Left pe)) <*> _ = ParserBox i $ Left pe
_ <*> (ParserBox i (Left pe)) = ParserBox i $ Left pe
p1@(ParserBox i1 (Right f)) <*> p2@(ParserBox i2 (Right a)) =
case mergeParserBoxInfos i1 i2 of
(Left pe) -> ParserBox i2 $ Left pe
(Right mergedi) -> ParserBox mergedi $ Right $ (f <*> a)
instance Monad ParserBox where
(ParserBox i (Left pe)) >>= _ = ParserBox i $ Left pe
(ParserBox i1 (Right ba)) >>= k = ?
mergeParserBoxInfos :: ParserBoxInfo -> ParserBoxInfo -> (Either String ParserBoxInfo)
mergeParserBoxInfos p1@(ParserBoxInfo ind1) p2@(ParserBoxInfo ind2)
| isNothing ind1 = Right p2
| isNothing ind2 = Right p1
| ind1 == ind2 = Right p1
| otherwise = Left "different indentation styles mixed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment