54 lines
1.4 KiB
Haskell
54 lines
1.4 KiB
Haskell
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
module Language.Yanais.Nfker7h.Parser.Types (
|
|
Context(..),
|
|
Result(..),
|
|
Error(..),
|
|
MaybeGetSpan(..)
|
|
) where
|
|
|
|
import qualified Language.Yanais.Parser as P
|
|
|
|
-- a "classic" tristate result
|
|
data Result t = RSome t | RNone | RError P.Span Error
|
|
|
|
data Context = CtxModule
|
|
|
|
data Error = UnexpectedEof Context
|
|
| UnexpectedChar !Int !Char
|
|
| UnknownIdent !P.Ident
|
|
|
|
instance Functor Result where
|
|
fmap f (RSome x) = RSome (f x)
|
|
fmap _ RNone = RNone
|
|
fmap _ (RError s e) = RError s e
|
|
|
|
instance Applicative Result where
|
|
pure = RSome
|
|
RNone <*> _ = RNone
|
|
(RError s e) <*> _ = RError s e
|
|
_ <*> RNone = RNone
|
|
_ <*> (RError s e) = RError s e
|
|
(RSome f) <*> (RSome x) = RSome (f x)
|
|
|
|
instance Monad Result where
|
|
return = pure
|
|
(RSome x) >>= thn = thn x
|
|
RNone >>= _ = RNone
|
|
(RError s e) >>= _ = RError s e
|
|
|
|
class MaybeGetSpan a where
|
|
mgSpan :: a -> Maybe P.Span
|
|
|
|
instance MaybeGetSpan t => MaybeGetSpan (Result t) where
|
|
mgSpan (RSome x) = mgSpan x
|
|
mgSpan RNone = Nothing
|
|
mgSpan (RError s _) = Just s
|
|
|
|
instance Show Context where
|
|
show CtxModule = "module level"
|
|
|
|
instance Show Error where
|
|
show (UnexpectedEof ctx) = "end of file encountered inside " <> show ctx
|
|
show (UnknownIdent x) = "unknown identifier: " <> show x
|
|
show (UnexpectedChar pos c) = "unexpected / unknown character at " <> show pos <> ": " <> show c <> " (" <> (show $ fromEnum c) <> ")"
|