1
1
module Text.Parsing.Parser where
2
2
3
- import Prelude
4
-
5
3
import Data.Either
6
4
import Data.Maybe
7
5
import Data.Monoid
8
6
import Data.Tuple
9
7
8
+ import Control.Alt
9
+ import Control.Alternative
10
+ import Control.Lazy
10
11
import Control.Monad
11
12
import Control.Monad.Identity
12
-
13
13
import Control.Monad.Trans
14
14
import Control.Monad.State.Class
15
15
import Control.Monad.State.Trans
16
16
import Control.Monad.Error
17
17
import Control.Monad.Error.Class
18
18
import Control.Monad.Error.Trans
19
+ import Control.MonadPlus
20
+ import Control.Plus
19
21
20
22
data ParseError = ParseError
21
23
{ message :: String
@@ -25,7 +27,7 @@ instance errorParseError :: Error ParseError where
25
27
noMsg = ParseError { message: " " }
26
28
strMsg msg = ParseError { message: msg }
27
29
28
- data ParserT s m a = ParserT (s -> m { input :: s , result :: Either ParseError a , consumed :: Boolean } )
30
+ newtype ParserT s m a = ParserT (s -> m { input :: s , result :: Either ParseError a , consumed :: Boolean } )
29
31
30
32
unParserT :: forall m s a . ParserT s m a -> s -> m { input :: s , result :: Either ParseError a , consumed :: Boolean }
31
33
unParserT (ParserT p) = p
@@ -41,23 +43,27 @@ runParser :: forall s a. s -> Parser s a -> Either ParseError a
41
43
runParser s = runIdentity <<< runParserT s
42
44
43
45
instance functorParserT :: (Functor m ) => Functor (ParserT s m ) where
44
- (<$>) f p = ParserT $ \s -> f' <$> unParserT p s
46
+ (<$>) f p = ParserT $ \s -> f' <$> unParserT p s
45
47
where
46
48
f' o = { input: o.input, result: f <$> o.result, consumed: o.consumed }
47
49
48
50
instance applyParserT :: (Monad m ) => Apply (ParserT s m ) where
49
51
(<*>) = ap
50
-
52
+
51
53
instance applicativeParserT :: (Monad m ) => Applicative (ParserT s m ) where
52
54
pure a = ParserT $ \s -> pure { input: s, result: Right a, consumed: false }
53
-
54
- instance alternativeParserT :: (Monad m ) => Alternative (ParserT s m ) where
55
- empty = fail " No alternative"
55
+
56
+ instance altParserT :: (Monad m ) => Alt (ParserT s m ) where
56
57
(<|>) p1 p2 = ParserT $ \s -> unParserT p1 s >>= \o ->
57
58
case o.result of
58
59
Left _ | not o.consumed -> unParserT p2 s
59
60
_ -> return o
60
61
62
+ instance plusParserT :: (Monad m ) => Plus (ParserT s m ) where
63
+ empty = fail " No alternative"
64
+
65
+ instance alternativeParserT :: (Monad m ) => Alternative (ParserT s m )
66
+
61
67
instance bindParserT :: (Monad m ) => Bind (ParserT s m ) where
62
68
(>>=) p f = ParserT $ \s -> unParserT p s >>= \o ->
63
69
case o.result of
@@ -68,16 +74,21 @@ instance bindParserT :: (Monad m) => Bind (ParserT s m) where
68
74
69
75
instance monadParserT :: (Monad m ) => Monad (ParserT s m )
70
76
77
+ instance monadPlusParserT :: (Monad m ) => MonadPlus (ParserT s m )
78
+
71
79
instance monadTransParserT :: MonadTrans (ParserT s ) where
72
80
lift m = ParserT $ \s -> (\a -> { input: s, consumed: false , result: Right a }) <$> m
73
81
74
82
instance monadStateParserT :: (Monad m ) => MonadState s (ParserT s m ) where
75
- state f = ParserT $ \s ->
83
+ state f = ParserT $ \s ->
76
84
return $ case f s of
77
85
Tuple a s' -> { input: s', consumed: false , result: Right a }
78
86
79
- consume :: forall s m . (Monad m ) => ParserT s m { }
80
- consume = ParserT $ \s -> return { consumed: true , input: s, result: Right {} }
87
+ instance lazy1ParserT :: Lazy1 (ParserT s m ) where
88
+ defer1 f = ParserT $ \s -> unParserT (f unit) s
89
+
90
+ consume :: forall s m . (Monad m ) => ParserT s m Unit
91
+ consume = ParserT $ \s -> return { consumed: true , input: s, result: Right unit }
81
92
82
93
fail :: forall m s a . (Monad m ) => String -> ParserT s m a
83
94
fail message = ParserT $ \s -> return { input: s, consumed: false , result: Left (ParseError { message: message }) }
0 commit comments