@@ -4,61 +4,67 @@ module Text.Parsing.StringParser.Combinators where
4
4
5
5
import Prelude
6
6
7
+ import Data.Either (Either (..))
7
8
import Data.Maybe (Maybe (..))
8
9
import Data.List (List (..), singleton )
9
- import Data.Foldable (Foldable , foldl )
10
+ import Data.Foldable (class Foldable , foldl )
10
11
11
12
import Control.Alt ((<|>))
12
13
import Control.Apply ((*>))
13
14
14
- import Text.Parsing.StringParser
15
+ import Text.Parsing.StringParser ( Parser (..), fail , unParser )
15
16
16
17
-- | Read ahead without consuming input.
17
18
lookAhead :: forall a . Parser a -> Parser a
18
- lookAhead p = Parser \ps fc sc -> unParser p ps fc (\s _ -> sc s ps)
19
+ lookAhead (Parser p) = Parser \s ->
20
+ case p s of
21
+ Right { result } -> Right { result, suffix: s }
22
+ left -> left
19
23
20
24
-- | Match zero or more times.
21
25
many :: forall a . Parser a -> Parser (List a )
22
- many p = many1 p <|> return Nil
26
+ many p = many1 p <|> pure Nil
23
27
24
28
-- | Match one or more times.
25
29
many1 :: forall a . Parser a -> Parser (List a )
26
30
many1 p = do
27
31
a <- p
28
32
as <- many p
29
- return (Cons a as)
33
+ pure (Cons a as)
30
34
31
35
-- | Provide an error message in case of failure.
32
- (<?>) :: forall a . Parser a -> String -> Parser a
33
- (<?>) p msg = p <|> fail msg
36
+ withError :: forall a . Parser a -> String -> Parser a
37
+ withError p msg = p <|> fail msg
38
+
39
+ infixl 3 withError as <?>
34
40
35
41
-- | Take the fixed point of a parser function. This function is sometimes useful when building recursive parsers.
36
42
fix :: forall a . (Parser a -> Parser a ) -> Parser a
37
- fix f = Parser ( \s fc sc -> unParser (f (fix f)) s fc sc)
43
+ fix f = Parser \s -> unParser (f (fix f)) s
38
44
39
45
-- | Parse a string between opening and closing markers.
40
46
between :: forall a open close . Parser open -> Parser close -> Parser a -> Parser a
41
47
between open close p = do
42
48
open
43
49
a <- p
44
50
close
45
- return a
51
+ pure a
46
52
47
53
-- | Parse a value with a default value in case of failure.
48
54
option :: forall a . a -> Parser a -> Parser a
49
- option a p = p <|> return a
55
+ option a p = p <|> pure a
50
56
51
57
-- | Attempt to parse a value.
52
58
optional :: forall a . Parser a -> Parser Unit
53
- optional p = (p >>= \_ -> return unit) <|> return unit
59
+ optional p = (p >>= \_ -> pure unit) <|> pure unit
54
60
55
- -- | Attempt to parse a value, returning `Nothing` in case of failure.
61
+ -- | Attempt to parse a value, pureing `Nothing` in case of failure.
56
62
optionMaybe :: forall a . Parser a -> Parser (Maybe a )
57
63
optionMaybe p = option Nothing (Just <$> p)
58
64
59
65
-- | Parse zero or more separated values.
60
66
sepBy :: forall a sep . Parser a -> Parser sep -> Parser (List a )
61
- sepBy p sep = sepBy1 p sep <|> return Nil
67
+ sepBy p sep = sepBy1 p sep <|> pure Nil
62
68
63
69
-- | Parse one or more separated values.
64
70
sepBy1 :: forall a sep . Parser a -> Parser sep -> Parser (List a )
@@ -67,41 +73,41 @@ sepBy1 p sep = do
67
73
as <- many $ do
68
74
sep
69
75
p
70
- return (Cons a as)
76
+ pure (Cons a as)
71
77
72
78
-- | Parse zero or more separated values, optionally ending with a separator.
73
79
sepEndBy :: forall a sep . Parser a -> Parser sep -> Parser (List a )
74
- sepEndBy p sep = sepEndBy1 p sep <|> return Nil
80
+ sepEndBy p sep = sepEndBy1 p sep <|> pure Nil
75
81
76
82
-- | Parse one or more separated values, optionally ending with a separator.
77
83
sepEndBy1 :: forall a sep . Parser a -> Parser sep -> Parser (List a )
78
84
sepEndBy1 p sep = do
79
85
a <- p
80
86
(do sep
81
87
as <- sepEndBy p sep
82
- return (Cons a as)) <|> return (singleton a)
88
+ pure (Cons a as)) <|> pure (singleton a)
83
89
84
90
-- | Parse zero or more separated values, ending with a separator.
85
91
endBy1 :: forall a sep . Parser a -> Parser sep -> Parser (List a )
86
92
endBy1 p sep = many1 $ do
87
93
a <- p
88
94
sep
89
- return a
95
+ pure a
90
96
91
97
-- | Parse one or more separated values, ending with a separator.
92
98
endBy :: forall a sep . Parser a -> Parser sep -> Parser (List a )
93
99
endBy p sep = many $ do
94
100
a <- p
95
101
sep
96
- return a
102
+ pure a
97
103
98
104
-- | Parse zero or more values separated by a right-associative operator.
99
105
chainr :: forall a . Parser a -> Parser (a -> a -> a ) -> a -> Parser a
100
- chainr p f a = chainr1 p f <|> return a
106
+ chainr p f a = chainr1 p f <|> pure a
101
107
102
108
-- | Parse zero or more values separated by a left-associative operator.
103
109
chainl :: forall a . Parser a -> Parser (a -> a -> a ) -> a -> Parser a
104
- chainl p f a = chainl1 p f <|> return a
110
+ chainl p f a = chainl1 p f <|> pure a
105
111
106
112
-- | Parse one or more values separated by a left-associative operator.
107
113
chainl1 :: forall a . Parser a -> Parser (a -> a -> a ) -> Parser a
@@ -113,7 +119,7 @@ chainl1 p f = do
113
119
chainl1' :: forall a . Parser a -> Parser (a -> a -> a ) -> a -> Parser a
114
120
chainl1' p f a = (do f' <- f
115
121
a' <- p
116
- chainl1' p f (f' a a')) <|> return a
122
+ chainl1' p f (f' a a')) <|> pure a
117
123
118
124
-- | Parse one or more values separated by a right-associative operator.
119
125
chainr1 :: forall a . Parser a -> Parser (a -> a -> a ) -> Parser a
@@ -125,7 +131,7 @@ chainr1 p f = do
125
131
chainr1' :: forall a . Parser a -> Parser (a -> a -> a ) -> a -> Parser a
126
132
chainr1' p f a = (do f' <- f
127
133
a' <- chainr1 p f
128
- return $ f' a a') <|> return a
134
+ pure $ f' a a') <|> pure a
129
135
130
136
-- | Parse using any of a collection of parsers.
131
137
choice :: forall f a . (Foldable f ) => f (Parser a ) -> Parser a
@@ -135,6 +141,6 @@ choice = foldl (<|>) (fail "Nothing to parse")
135
141
manyTill :: forall a end . Parser a -> Parser end -> Parser (List a )
136
142
manyTill p end = scan
137
143
where
138
- scan = (end *> return Nil ) <|> do x <- p
139
- xs <- scan
140
- return (Cons x xs)
144
+ scan = (end *> pure Nil ) <|> do x <- p
145
+ xs <- scan
146
+ pure (Cons x xs)
0 commit comments