11module Text.Parsing.Parser
2- ( ParseError (..)
2+ ( ParseError
3+ , parseErrorMessage
4+ , parseErrorPosition
35 , ParseState (..)
46 , ParserT (..)
57 , Parser
@@ -23,22 +25,23 @@ import Data.Tuple (Tuple(..))
2325import Text.Parsing.Parser.Pos (Position , initialPos )
2426
2527-- | A parsing error, consisting of a message and position information.
26- newtype ParseError = ParseError
27- { message :: String
28- , position :: Position
29- }
28+ data ParseError = ParseError String Position
29+
30+ parseErrorMessage :: ParseError -> String
31+ parseErrorMessage (ParseError msg _) = msg
32+
33+ parseErrorPosition :: ParseError -> Position
34+ parseErrorPosition (ParseError _ pos) = pos
3035
3136instance showParseError :: Show ParseError where
32- show (ParseError msg) = " ParseError { message: " <> msg.message <> " , position: " <> show msg.position <> " }"
37+ show (ParseError msg pos) =
38+ " (ParseError " <> show msg <> show pos <> " )"
3339
3440derive instance eqParseError :: Eq ParseError
41+ derive instance ordParseError :: Ord ParseError
3542
36- -- | `PState` contains the remaining input and current position.
37- newtype ParseState s = ParseState
38- { input :: s
39- , position :: Position
40- , consumed :: Boolean
41- }
43+ -- | Contains the remaining input and current position.
44+ data ParseState s = ParseState s Position Boolean
4245
4346-- | The Parser monad transformer.
4447-- |
@@ -51,7 +54,7 @@ derive instance newtypeParserT :: Newtype (ParserT s m a) _
5154-- | Apply a parser, keeping only the parsed result.
5255runParserT :: forall m s a . Monad m => s -> ParserT s m a -> m (Either ParseError a )
5356runParserT s p = evalStateT (runExceptT (unwrap p)) initialState where
54- initialState = ParseState { input: s, position: initialPos, consumed: false }
57+ initialState = ParseState s initialPos false
5558
5659-- | The `Parser` monad is a synonym for the parser monad transformer applied to the `Identity` monad.
5760type Parser s a = ParserT s Identity a
@@ -73,12 +76,12 @@ derive newtype instance monadStateParserT :: Monad m => MonadState (ParseState s
7376derive newtype instance monadErrorParserT :: Monad m => MonadError ParseError (ParserT s m )
7477
7578instance altParserT :: Monad m => Alt (ParserT s m ) where
76- alt p1 p2 = (ParserT <<< ExceptT <<< StateT ) \(s@(ParseState { input, position } )) -> do
77- Tuple e (ParseState s' ) <- runStateT (runExceptT (unwrap p1)) (ParseState { input, position, consumed: false } )
79+ alt p1 p2 = (ParserT <<< ExceptT <<< StateT ) \(s@(ParseState i p _ )) -> do
80+ Tuple e s'@ (ParseState i' p' c' ) <- runStateT (runExceptT (unwrap p1)) (ParseState i p false )
7881 case e of
7982 Left err
80- | not s'.consumed -> runStateT (runExceptT (unwrap p2)) s
81- _ -> pure (Tuple e ( ParseState s') )
83+ | not c' -> runStateT (runExceptT (unwrap p2)) s
84+ _ -> pure (Tuple e s' )
8285
8386instance plusParserT :: Monad m => Plus (ParserT s m ) where
8487 empty = fail " No alternative"
@@ -94,11 +97,11 @@ instance monadTransParserT :: MonadTrans (ParserT s) where
9497
9598-- | Set the consumed flag.
9699consume :: forall s m . Monad m => ParserT s m Unit
97- consume = modify \(ParseState { input, position } ) ->
98- ParseState { input, position, consumed: true }
100+ consume = modify \(ParseState input position _ ) ->
101+ ParseState input position true
99102
100103-- | Fail with a message.
101104fail :: forall m s a . Monad m => String -> ParserT s m a
102105fail message = do
103- position <- gets \(ParseState s ) -> s.position
104- throwError (ParseError { message, position } )
106+ position <- gets \(ParseState _ pos _ ) -> pos
107+ throwError (ParseError message position)
0 commit comments