Skip to content

Commit 31a1e80

Browse files
committed
Use BSD3 license, update deps, incorporate #33
1 parent 5e40f4f commit 31a1e80

File tree

7 files changed

+67
-67
lines changed

7 files changed

+67
-67
lines changed

LICENSE

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
The MIT License (MIT)
2-
3-
Copyright (c) 2014 PureScript
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy of
6-
this software and associated documentation files (the "Software"), to deal in
7-
the Software without restriction, including without limitation the rights to
8-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9-
the Software, and to permit persons to whom the Software is furnished to do so,
10-
subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17-
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19-
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20-
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1+
Copyright 2014-2016 PureScript
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are met:
5+
6+
* Redistributions of source code must retain the above copyright notice,
7+
this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above copyright
9+
notice, this list of conditions and the following disclaimer in the
10+
documentation and/or other materials provided with the distribution.
11+
12+
This software is provided by the copyright holders "as is" and any express or
13+
implied warranties, including, but not limited to, the implied warranties of
14+
merchantability and fitness for a particular purpose are disclaimed. In no
15+
event shall the copyright holders be liable for any direct, indirect,
16+
incidental, special, exemplary, or consequential damages (including, but not
17+
limited to, procurement of substitute goods or services; loss of use, data,
18+
or profits; or business interruption) however caused and on any theory of
19+
liability, whether in contract, strict liability, or tort (including
20+
negligence or otherwise) arising in any way out of the use of this software,
21+
even if advised of the possibility of such damage.
2122

2223
-------------------------------------------------------------------------------
2324

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"keywords": [
66
"purescript"
77
],
8-
"license": "MIT",
8+
"license": "BSD3",
99
"repository": {
1010
"type": "git",
1111
"url": "git://github.com/purescript-contrib/purescript-parsing.git"
@@ -29,7 +29,7 @@
2929
"purescript-maybe": "^2.0.0",
3030
"purescript-strings": "^2.0.0",
3131
"purescript-transformers": "^2.0.0",
32-
"purescript-unicode": "6d9a4ab9d239da4cecb33283994cce56350bbe87"
32+
"purescript-unicode": "^2.0.0"
3333
},
3434
"devDependencies": {
3535
"purescript-assert": "^2.0.0",

src/Text/Parsing/Parser.purs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module 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(..))
2325
import 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

3136
instance 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

3440
derive 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.
5255
runParserT :: forall m s a. Monad m => s -> ParserT s m a -> m (Either ParseError a)
5356
runParserT 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.
5760
type Parser s a = ParserT s Identity a
@@ -73,12 +76,12 @@ derive newtype instance monadStateParserT :: Monad m => MonadState (ParseState s
7376
derive newtype instance monadErrorParserT :: Monad m => MonadError ParseError (ParserT s m)
7477

7578
instance 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

8386
instance 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.
9699
consume :: 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.
101104
fail :: forall m s a. Monad m => String -> ParserT s m a
102105
fail 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)

src/Text/Parsing/Parser/Combinators.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ optionMaybe p = option Nothing (Just <$> p)
7373

7474
-- | In case of failure, reset the stream to the unconsumed state.
7575
try :: forall m s a. Monad m => ParserT s m a -> ParserT s m a
76-
try p = (ParserT <<< ExceptT <<< StateT) \(s@(ParseState { consumed })) -> do
77-
Tuple e s'@(ParseState { input, position }) <- runStateT (runExceptT (unwrap p)) s
76+
try p = (ParserT <<< ExceptT <<< StateT) \(s@(ParseState _ _ consumed)) -> do
77+
Tuple e s'@(ParseState input position _) <- runStateT (runExceptT (unwrap p)) s
7878
case e of
79-
Left _ -> pure (Tuple e (ParseState { input, position, consumed }))
79+
Left _ -> pure (Tuple e (ParseState input position consumed))
8080
_ -> pure (Tuple e s')
8181

8282
-- | Parse a phrase, without modifying the consumed state or stream position.

src/Text/Parsing/Parser/String.purs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,33 @@ instance stringLikeString :: StringLike String where
3131
-- | Match end-of-file.
3232
eof :: forall s m. (StringLike s, Monad m) => ParserT s m Unit
3333
eof = do
34-
input <- gets \(ParseState { input }) -> input
34+
input <- gets \(ParseState input _ _) -> input
3535
unless (null input) (fail "Expected EOF")
3636

3737
-- | Match the specified string.
3838
string :: forall s m. (StringLike s, Monad m) => String -> ParserT s m String
3939
string str = do
40-
input <- gets \(ParseState { input }) -> input
40+
input <- gets \(ParseState input _ _) -> input
4141
case indexOf (wrap str) input of
4242
Just 0 -> do
43-
modify \(ParseState { position }) ->
44-
ParseState { position: updatePosString position str
45-
, consumed: true
46-
, input: drop (length str) input
47-
}
43+
modify \(ParseState _ position _) ->
44+
ParseState (drop (length str) input)
45+
(updatePosString position str)
46+
true
4847
pure str
4948
_ -> fail ("Expected " <> show str)
5049

5150
-- | Match any character.
5251
anyChar :: forall s m. (StringLike s, Monad m) => ParserT s m Char
5352
anyChar = do
54-
input <- gets \(ParseState { input }) -> input
53+
input <- gets \(ParseState input _ _) -> input
5554
case uncons input of
5655
Nothing -> fail "Unexpected EOF"
5756
Just { head, tail } -> do
58-
modify \(ParseState { position }) ->
59-
ParseState { position: updatePosString position (singleton head)
60-
, consumed: true
61-
, input: tail
62-
}
57+
modify \(ParseState _ position _) ->
58+
ParseState tail
59+
(updatePosString position (singleton head))
60+
true
6361
pure head
6462

6563
-- | Match a character satisfying the specified predicate.

src/Text/Parsing/Parser/Token.purs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,12 @@ import Prelude hiding (when,between)
4747
-- | Create a parser which Returns the first token in the stream.
4848
token :: forall m a. Monad m => (a -> Position) -> ParserT (List a) m a
4949
token tokpos = do
50-
input <- gets \(ParseState { input }) -> input
50+
input <- gets \(ParseState input _ _) -> input
5151
case List.uncons input of
5252
Nothing -> fail "Unexpected EOF"
5353
Just { head, tail } -> do
54-
modify \(ParseState { position }) ->
55-
ParseState { position: tokpos head
56-
, consumed: true
57-
, input: tail
58-
}
54+
modify \(ParseState _ position _) ->
55+
ParseState tail (tokpos head) true
5956
pure head
6057

6158
-- | Create a parser which matches any token satisfying the predicate.

test/Main.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Data.Maybe (Maybe(..))
1111
import Data.String (fromCharArray, singleton)
1212
import Data.Tuple (Tuple(..))
1313
import Test.Assert (ASSERT, assert')
14-
import Text.Parsing.Parser (Parser, ParserT, ParseError(..), runParser)
14+
import Text.Parsing.Parser (Parser, ParserT, runParser, parseErrorPosition)
1515
import Text.Parsing.Parser.Combinators (endBy1, sepBy1, optionMaybe, try, chainl, between)
1616
import Text.Parsing.Parser.Expr (Assoc(..), Operator(..), buildExprParser)
1717
import Text.Parsing.Parser.Language (javaStyle, haskellStyle, haskellDef)
@@ -38,7 +38,8 @@ parseTest input expected p = case runParser input p of
3838
parseErrorTestPosition :: forall s a eff. (Show a) => Parser s a -> s -> Position -> Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
3939
parseErrorTestPosition p input expected = case runParser input p of
4040
Right _ -> assert' "error: ParseError expected!" false
41-
Left (ParseError { position: pos }) -> do
41+
Left err -> do
42+
let pos = parseErrorPosition err
4243
assert' ("expected: " <> show expected <> ", pos: " <> show pos) (expected == pos)
4344
logShow expected
4445

0 commit comments

Comments
 (0)