Skip to content

Commit e4bb89f

Browse files
committed
Updates for 1.0 core libraries
1 parent d1896ea commit e4bb89f

File tree

9 files changed

+243
-262
lines changed

9 files changed

+243
-262
lines changed

bower.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
"url": "git://github.com/purescript-contrib/purescript-parsing.git"
2323
},
2424
"dependencies": {
25-
"purescript-arrays": "^0.4.0",
26-
"purescript-either": "^0.2.0",
27-
"purescript-foldable-traversable": "^0.4.0",
28-
"purescript-identity": "^0.4.0",
29-
"purescript-lists": "^0.7.0",
30-
"purescript-maybe": "^0.3.0",
31-
"purescript-strings": "~0.7.0",
32-
"purescript-transformers": "^0.8.1",
33-
"purescript-unicode": "^0.0.1",
34-
"purescript-integers": "^0.2.0"
25+
"purescript-arrays": "^1.0.0",
26+
"purescript-either": "^1.0.0",
27+
"purescript-foldable-traversable": "^1.0.0",
28+
"purescript-identity": "^1.0.0",
29+
"purescript-lists": "^1.0.0",
30+
"purescript-maybe": "^1.0.0",
31+
"purescript-strings": "^1.0.0",
32+
"purescript-transformers": "^1.0.0",
33+
"purescript-unicode": "1.0-dependencies",
34+
"purescript-integers": "^1.0.0"
3535
},
3636
"devDependencies": {
37-
"purescript-console": "^0.1.0",
38-
"purescript-assert": "^0.1.0"
37+
"purescript-console": "^1.0.0",
38+
"purescript-assert": "^1.0.0"
3939
}
4040
}

src/Text/Parsing/Parser.purs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@ module Text.Parsing.Parser where
22

33
import Prelude
44

5-
import Data.Either
6-
import Data.Identity
7-
import Data.Tuple
8-
9-
import Control.Alt
10-
import Control.Alternative
11-
import Control.Lazy
12-
import Control.Monad.State.Class (MonadState)
13-
import Control.Monad.Trans (MonadTrans)
14-
import Control.MonadPlus
15-
import Control.Plus
16-
17-
import Text.Parsing.Parser.Pos
5+
import Control.Lazy (class Lazy)
6+
import Control.Monad.State.Class (class MonadState)
7+
import Control.Monad.Trans (class MonadTrans)
8+
import Control.MonadPlus (class MonadPlus, class MonadZero, class Alternative)
9+
import Control.Plus (class Plus, class Alt)
10+
import Data.Either (Either(..))
11+
import Data.Identity (Identity, runIdentity)
12+
import Data.Tuple (Tuple(..))
13+
import Text.Parsing.Parser.Pos (Position, initialPos)
1814

1915
-- | A parsing error, consisting of a message and position information.
2016
data ParseError = ParseError
@@ -23,7 +19,7 @@ data ParseError = ParseError
2319
}
2420

2521
instance showParseError :: Show ParseError where
26-
show (ParseError msg) = "ParseError { message: " ++ msg.message ++ ", position: " ++ show msg.position ++ " }"
22+
show (ParseError msg) = "ParseError { message: " <> msg.message <> ", position: " <> show msg.position <> " }"
2723

2824
instance eqParseError :: Eq ParseError where
2925
eq (ParseError {message : m1, position : p1}) (ParseError {message : m2, position : p2}) = m1 == m2 && p1 == p2
@@ -44,10 +40,10 @@ unParserT :: forall m s a. ParserT s m a -> PState s -> m { input :: s, result :
4440
unParserT (ParserT p) = p
4541

4642
-- | Apply a parser, keeping only the parsed result.
47-
runParserT :: forall m s a. (Monad m) => PState s -> ParserT s m a -> m (Either ParseError a)
43+
runParserT :: forall m s a. Monad m => PState s -> ParserT s m a -> m (Either ParseError a)
4844
runParserT s p = do
4945
o <- unParserT p s
50-
return o.result
46+
pure o.result
5147

5248
-- | The `Parser` monad is a synonym for the parser monad transformer applied to the `Identity` monad.
5349
type Parser s a = ParserT s Identity a
@@ -61,53 +57,55 @@ instance functorParserT :: (Functor m) => Functor (ParserT s m) where
6157
where
6258
f' o = { input: o.input, result: f <$> o.result, consumed: o.consumed, position: o.position }
6359

64-
instance applyParserT :: (Monad m) => Apply (ParserT s m) where
60+
instance applyParserT :: Monad m => Apply (ParserT s m) where
6561
apply = ap
6662

67-
instance applicativeParserT :: (Monad m) => Applicative (ParserT s m) where
63+
instance applicativeParserT :: Monad m => Applicative (ParserT s m) where
6864
pure a = ParserT $ \(PState { input: s, position: pos }) -> pure { input: s, result: Right a, consumed: false, position: pos }
6965

70-
instance altParserT :: (Monad m) => Alt (ParserT s m) where
66+
instance altParserT :: Monad m => Alt (ParserT s m) where
7167
alt p1 p2 = ParserT $ \s -> unParserT p1 s >>= \o ->
7268
case o.result of
7369
Left _ | not o.consumed -> unParserT p2 s
74-
_ -> return o
70+
_ -> pure o
7571

76-
instance plusParserT :: (Monad m) => Plus (ParserT s m) where
72+
instance plusParserT :: Monad m => Plus (ParserT s m) where
7773
empty = fail "No alternative"
7874

79-
instance alternativeParserT :: (Monad m) => Alternative (ParserT s m)
75+
instance alternativeParserT :: Monad m => Alternative (ParserT s m)
8076

81-
instance bindParserT :: (Monad m) => Bind (ParserT s m) where
77+
instance bindParserT :: Monad m => Bind (ParserT s m) where
8278
bind p f = ParserT $ \s -> unParserT p s >>= \o ->
8379
case o.result of
84-
Left err -> return { input: o.input, result: Left err, consumed: o.consumed, position: o.position }
80+
Left err -> pure { input: o.input, result: Left err, consumed: o.consumed, position: o.position }
8581
Right a -> updateConsumedFlag o.consumed <$> unParserT (f a) (PState { input: o.input, position: o.position })
8682
where
8783
updateConsumedFlag c o = { input: o.input, consumed: c || o.consumed, result: o.result, position: o.position }
8884

89-
instance monadParserT :: (Monad m) => Monad (ParserT s m)
85+
instance monadParserT :: Monad m => Monad (ParserT s m)
86+
87+
instance monadZeroParserT :: Monad m => MonadZero (ParserT s m)
9088

91-
instance monadPlusParserT :: (Monad m) => MonadPlus (ParserT s m)
89+
instance monadPlusParserT :: Monad m => MonadPlus (ParserT s m)
9290

9391
instance monadTransParserT :: MonadTrans (ParserT s) where
9492
lift m = ParserT $ \(PState { input: s, position: pos }) -> (\a -> { input: s, consumed: false, result: Right a, position: pos }) <$> m
9593

96-
instance monadStateParserT :: (Monad m) => MonadState s (ParserT s m) where
94+
instance monadStateParserT :: Monad m => MonadState s (ParserT s m) where
9795
state f = ParserT $ \(PState { input: s, position: pos }) ->
98-
return $ case f s of
96+
pure $ case f s of
9997
Tuple a s' -> { input: s', consumed: false, result: Right a, position: pos }
10098

10199
instance lazyParserT :: Lazy (ParserT s m a) where
102100
defer f = ParserT $ \s -> unParserT (f unit) s
103101

104102
-- | Set the consumed flag.
105-
consume :: forall s m. (Monad m) => ParserT s m Unit
106-
consume = ParserT $ \(PState { input: s, position: pos }) -> return { consumed: true, input: s, result: Right unit, position: pos }
103+
consume :: forall s m. Monad m => ParserT s m Unit
104+
consume = ParserT $ \(PState { input: s, position: pos }) -> pure { consumed: true, input: s, result: Right unit, position: pos }
107105

108106
-- | Fail with a message.
109-
fail :: forall m s a. (Monad m) => String -> ParserT s m a
110-
fail message = ParserT $ \(PState { input: s, position: pos }) -> return $ parseFailed s pos message
107+
fail :: forall m s a. Monad m => String -> ParserT s m a
108+
fail message = ParserT $ \(PState { input: s, position: pos }) -> pure $ parseFailed s pos message
111109

112110
-- | Creates a failed parser state for the remaining input `s` and current position
113111
-- | with an error message.

src/Text/Parsing/Parser/Combinators.purs

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,26 @@
2121

2222
module Text.Parsing.Parser.Combinators where
2323

24-
import Prelude
24+
import Prelude (class Functor, class Monad, Unit, ($), (*>), (<>), (<$>), bind, flip, pure, unit)
2525

26-
import Data.Maybe
27-
import Data.Either
26+
import Control.Plus (empty, (<|>))
27+
import Data.Either (Either(..))
28+
import Data.Foldable (class Foldable, foldl)
2829
import Data.List (List(..), (:), many, some, singleton)
29-
import Data.Foldable (Foldable, foldl)
30-
31-
import Control.Alt
32-
import Control.Plus
33-
import Control.Apply
34-
35-
import Text.Parsing.Parser
30+
import Data.Maybe (Maybe(..))
31+
import Text.Parsing.Parser (PState(..), ParserT(..), fail, unParserT)
3632

3733
-- | Provide an error message in the case of failure.
38-
(<?>) :: forall m s a. (Monad m) => ParserT s m a -> String -> ParserT s m a
39-
(<?>) p msg = p <|> fail ("Expected " ++ msg)
34+
withErrorMessage :: forall m s a. Monad m => ParserT s m a -> String -> ParserT s m a
35+
withErrorMessage p msg = p <|> fail ("Expected " <> msg)
36+
37+
infix 3 withErrorMessage as <?>
4038

4139
-- | Flipped `(<?>)`.
42-
(<??>) :: forall m s a. (Monad m) => String -> ParserT s m a -> ParserT s m a
43-
(<??>) = flip (<?>)
40+
asErrorMessage :: forall m s a. Monad m => String -> ParserT s m a -> ParserT s m a
41+
asErrorMessage = flip (<?>)
42+
43+
infix 3 asErrorMessage as <??>
4444

4545
-- | Wrap a parser with opening and closing markers.
4646
-- |
@@ -49,31 +49,31 @@ import Text.Parsing.Parser
4949
-- | ```purescript
5050
-- | parens = between (string "(") (string ")")
5151
-- | ```
52-
between :: forall m s a open close. (Monad m) => ParserT s m open -> ParserT s m close -> ParserT s m a -> ParserT s m a
52+
between :: forall m s a open close. Monad m => ParserT s m open -> ParserT s m close -> ParserT s m a -> ParserT s m a
5353
between open close p = do
5454
open
5555
a <- p
5656
close
57-
return a
57+
pure a
5858

5959
-- | Provide a default result in the case where a parser fails without consuming input.
60-
option :: forall m s a. (Monad m) => a -> ParserT s m a -> ParserT s m a
61-
option a p = p <|> return a
60+
option :: forall m s a. Monad m => a -> ParserT s m a -> ParserT s m a
61+
option a p = p <|> pure a
6262

6363
-- | Optionally parse something, failing quietly.
64-
optional :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m Unit
64+
optional :: forall m s a. Monad m => ParserT s m a -> ParserT s m Unit
6565
optional p = (do p
66-
return unit) <|> return unit
66+
pure unit) <|> pure unit
6767

68-
-- | Return `Nothing` in the case where a parser fails without consuming input.
69-
optionMaybe :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (Maybe a)
68+
-- | pure `Nothing` in the case where a parser fails without consuming input.
69+
optionMaybe :: forall m s a. Monad m => ParserT s m a -> ParserT s m (Maybe a)
7070
optionMaybe p = option Nothing (Just <$> p)
7171

7272
-- | In case of failure, reset the stream to the unconsumed state.
7373
try :: forall m s a. (Functor m) => ParserT s m a -> ParserT s m a
7474
try p = ParserT $ \(PState { input: s, position: pos }) -> try' s pos <$> unParserT p (PState { input: s, position: pos })
7575
where
76-
try' s pos o@{ result = Left _ } = { input: s, result: o.result, consumed: false, position: pos }
76+
try' s pos o@{ result: Left _ } = { input: s, result: o.result, consumed: false, position: pos }
7777
try' _ _ o = o
7878

7979
-- | Parse phrases delimited by a separator.
@@ -83,43 +83,43 @@ try p = ParserT $ \(PState { input: s, position: pos }) -> try' s pos <$> unPars
8383
-- | ```purescript
8484
-- | digit `sepBy` string ","
8585
-- | ```
86-
sepBy :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
87-
sepBy p sep = sepBy1 p sep <|> return Nil
86+
sepBy :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
87+
sepBy p sep = sepBy1 p sep <|> pure Nil
8888

8989
-- | Parse phrases delimited by a separator, requiring at least one match.
90-
sepBy1 :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
90+
sepBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
9191
sepBy1 p sep = do
9292
a <- p
9393
as <- many $ do
9494
sep
9595
p
96-
return (a : as)
96+
pure (a : as)
9797

9898
-- | Parse phrases delimited and optionally terminated by a separator.
99-
sepEndBy :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
100-
sepEndBy p sep = sepEndBy1 p sep <|> return Nil
99+
sepEndBy :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
100+
sepEndBy p sep = sepEndBy1 p sep <|> pure Nil
101101

102102
-- | Parse phrases delimited and optionally terminated by a separator, requiring at least one match.
103-
sepEndBy1 :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
103+
sepEndBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
104104
sepEndBy1 p sep = do
105105
a <- p
106106
(do sep
107107
as <- sepEndBy p sep
108-
return (a : as)) <|> return (singleton a)
108+
pure (a : as)) <|> pure (singleton a)
109109

110110
-- | Parse phrases delimited and terminated by a separator, requiring at least one match.
111-
endBy1 :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
111+
endBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
112112
endBy1 p sep = some $ do
113113
a <- p
114114
sep
115-
return a
115+
pure a
116116

117117
-- | Parse phrases delimited and terminated by a separator.
118-
endBy :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
118+
endBy :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
119119
endBy p sep = many $ do
120120
a <- p
121121
sep
122-
return a
122+
pure a
123123

124124
-- | Parse phrases delimited by a right-associative operator.
125125
-- |
@@ -128,75 +128,75 @@ endBy p sep = many $ do
128128
-- | ```purescript
129129
-- | chainr digit (string "+" *> add) 0
130130
-- | ```
131-
chainr :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
132-
chainr p f a = chainr1 p f <|> return a
131+
chainr :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
132+
chainr p f a = chainr1 p f <|> pure a
133133

134134
-- | Parse phrases delimited by a left-associative operator.
135-
chainl :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
136-
chainl p f a = chainl1 p f <|> return a
135+
chainl :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
136+
chainl p f a = chainl1 p f <|> pure a
137137

138138
-- | Parse phrases delimited by a left-associative operator, requiring at least one match.
139-
chainl1 :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m a
139+
chainl1 :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m a
140140
chainl1 p f = do
141141
a <- p
142142
chainl1' p f a
143143

144-
chainl1' :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
144+
chainl1' :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
145145
chainl1' p f a = (do f' <- f
146146
a' <- p
147-
chainl1' p f (f' a a')) <|> return a
147+
chainl1' p f (f' a a')) <|> pure a
148148

149149
-- | Parse phrases delimited by a right-associative operator, requiring at least one match.
150-
chainr1 :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m a
150+
chainr1 :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m a
151151
chainr1 p f = do
152152
a <- p
153153
chainr1' p f a
154154

155-
chainr1' :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
155+
chainr1' :: forall m s a. Monad m => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
156156
chainr1' p f a = (do f' <- f
157157
a' <- chainr1 p f
158-
return $ f' a a') <|> return a
158+
pure $ f' a a') <|> pure a
159159

160160
-- | Parse one of a set of alternatives.
161161
choice :: forall f m s a. (Foldable f, Monad m) => f (ParserT s m a) -> ParserT s m a
162162
choice = foldl (<|>) empty
163163

164164
-- | Skip many instances of a phrase.
165-
skipMany :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m Unit
166-
skipMany p = skipMany1 p <|> return unit
165+
skipMany :: forall s a m. Monad m => ParserT s m a -> ParserT s m Unit
166+
skipMany p = skipMany1 p <|> pure unit
167167

168168
-- | Skip at least one instance of a phrase.
169-
skipMany1 :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m Unit
169+
skipMany1 :: forall s a m. Monad m => ParserT s m a -> ParserT s m Unit
170170
skipMany1 p = do
171171
x <- p
172172
xs <- skipMany p
173-
return unit
173+
pure unit
174174

175175
-- | Parse a phrase, without modifying the consumed state or stream position.
176-
lookAhead :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m a
176+
lookAhead :: forall s a m. Monad m => ParserT s m a -> ParserT s m a
177177
lookAhead (ParserT p) = ParserT \(PState { input: s, position: pos }) -> do
178178
state <- p (PState { input: s, position: pos })
179-
return state{input = s, consumed = false, position = pos}
179+
pure state{input = s, consumed = false, position = pos}
180180

181181
-- | Fail if the specified parser matches.
182-
notFollowedBy :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m Unit
183-
notFollowedBy p = try $ (try p *> fail "Negated parser succeeded") <|> return unit
182+
notFollowedBy :: forall s a m. Monad m => ParserT s m a -> ParserT s m Unit
183+
notFollowedBy p = try $ (try p *> fail "Negated parser succeeded") <|> pure unit
184184

185185
-- | Parse several phrases until the specified terminator matches.
186-
manyTill :: forall s a m e. (Monad m) => ParserT s m a -> ParserT s m e -> ParserT s m (List a)
186+
manyTill :: forall s a m e. Monad m => ParserT s m a -> ParserT s m e -> ParserT s m (List a)
187187
manyTill p end = scan
188188
where
189189
scan = (do
190190
end
191-
return Nil)
191+
pure Nil)
192192
<|> (do
193193
x <- p
194194
xs <- scan
195-
return (x:xs))
195+
pure (x:xs))
196196

197197
-- | Parse several phrases until the specified terminator matches, requiring at least one match.
198-
many1Till :: forall s a m e. (Monad m) => ParserT s m a -> ParserT s m e -> ParserT s m (List a)
198+
many1Till :: forall s a m e. Monad m => ParserT s m a -> ParserT s m e -> ParserT s m (List a)
199199
many1Till p end = do
200200
x <- p
201201
xs <- manyTill p end
202-
return (x:xs)
202+
pure (x:xs)

0 commit comments

Comments
 (0)