Skip to content

Commit 9fd2fce

Browse files
committed
Merge pull request #11 from Thimoteus/master
added satisfy, char, whiteSpace, skipSpaces, oneOf, noneOf, and Eq instance
2 parents fd18816 + e8f53db commit 9fd2fce

File tree

5 files changed

+99
-7
lines changed

5 files changed

+99
-7
lines changed

bower.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
"package.json"
1818
],
1919
"dependencies": {
20-
"purescript-control": "^0.3.0",
21-
"purescript-arrays": "^0.4.0",
22-
"purescript-maybe": "^0.3.0",
20+
"purescript-control": "~0.3.1",
21+
"purescript-arrays": "~0.4.2",
22+
"purescript-maybe": "~0.3.4",
2323
"purescript-strings": "~0.7.0",
24-
"purescript-foldable-traversable": "^0.4.0"
24+
"purescript-foldable-traversable": "~0.4.0"
2525
},
2626
"devDependencies": {
27-
"purescript-math": "^0.2.0",
28-
"purescript-quickcheck": "^0.12.0"
27+
"purescript-math": "~0.2.0",
28+
"purescript-quickcheck": "~0.12.0"
2929
}
3030
}

docs/Text/Parsing/StringParser.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The type of parsing errors.
3737
##### Instances
3838
``` purescript
3939
instance showParseError :: Show ParseError
40+
instance eqParseError :: Eq ParseError
4041
```
4142

4243
#### `Parser`

docs/Text/Parsing/StringParser/String.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,52 @@ string :: String -> Parser String
3434

3535
Match the specified string.
3636

37+
#### `satisfy`
38+
39+
``` purescript
40+
satisfy :: (Char -> Boolean) -> Parser Char
41+
```
42+
43+
Match a character satisfying the given predicate.
44+
45+
#### `char`
46+
47+
``` purescript
48+
char :: Char -> Parser Char
49+
```
50+
51+
Match the specified character.
52+
53+
#### `whiteSpace`
54+
55+
``` purescript
56+
whiteSpace :: Parser String
57+
```
58+
59+
Match many whitespace characters.
60+
61+
#### `skipSpaces`
62+
63+
``` purescript
64+
skipSpaces :: Parser Unit
65+
```
66+
67+
Skip many whitespace characters.
68+
69+
#### `oneOf`
70+
71+
``` purescript
72+
oneOf :: forall f. (Foldable f) => f Char -> Parser Char
73+
```
74+
75+
Match one of the characters in the foldable structure.
76+
77+
#### `noneOf`
78+
79+
``` purescript
80+
noneOf :: forall f. (Foldable f) => f Char -> Parser Char
81+
```
82+
83+
Match any character not in the foldable structure.
84+
3785

src/Text/Parsing/StringParser.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ data ParseError = ParseError String
3030
instance showParseError :: Show ParseError where
3131
show (ParseError msg) = msg
3232

33+
instance eqParseError :: Eq ParseError where
34+
eq (ParseError x) (ParseError y) = x == y
35+
3336
-- | A parser is represented as a function which takes a pair of
3437
-- | continuations for failure and success.
3538
data Parser a = Parser (forall r. PosString -> (Pos -> ParseError -> r) -> (a -> PosString -> r) -> r)

src/Text/Parsing/StringParser/String.purs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@ module Text.Parsing.StringParser.String
55
, anyChar
66
, anyDigit
77
, string
8+
, satisfy
9+
, char
10+
, whiteSpace
11+
, skipSpaces
12+
, oneOf
13+
, noneOf
814
) where
915

1016
import Prelude
1117

1218
import Data.Maybe (Maybe(..))
13-
import Data.String (charAt, fromChar, length, indexOf')
19+
import Data.String (charAt, fromChar, length, indexOf', fromCharArray)
20+
import Data.Char (toString)
21+
import Data.Foldable (Foldable, foldMap, elem, notElem)
22+
23+
import Text.Parsing.StringParser.Combinators (many, (<?>))
1424
import Text.Parsing.StringParser
1525

1626
import qualified Data.String.Regex as Rx
@@ -46,3 +56,33 @@ string :: String -> Parser String
4656
string nt = Parser (\s fc sc -> case s of
4757
{ str = str, pos = i } | indexOf' nt i str == Just i -> sc nt { str: str, pos: i + length nt }
4858
{ pos = i } -> fc i (ParseError $ "Expected '" ++ nt ++ "'."))
59+
60+
-- | Match a character satisfying the given predicate.
61+
satisfy :: (Char -> Boolean) -> Parser Char
62+
satisfy f = try do
63+
c <- anyChar
64+
if f c
65+
then return c
66+
else fail $ "Character " <> toString c <> " did not satisfy predicate"
67+
68+
-- | Match the specified character.
69+
char :: Char -> Parser Char
70+
char c = satisfy (== c) <?> "Could not match character " <> toString c
71+
72+
-- | Match many whitespace characters.
73+
whiteSpace :: Parser String
74+
whiteSpace = do
75+
cs <- many (satisfy \ c -> c == 'n' || c == 'r' || c == ' ' || c == '\t')
76+
return (foldMap toString cs)
77+
78+
-- | Skip many whitespace characters.
79+
skipSpaces :: Parser Unit
80+
skipSpaces = void whiteSpace
81+
82+
-- | Match one of the characters in the foldable structure.
83+
oneOf :: forall f. (Foldable f) => f Char -> Parser Char
84+
oneOf = satisfy <<< flip elem
85+
86+
-- | Match any character not in the foldable structure.
87+
noneOf :: forall f. (Foldable f) => f Char -> Parser Char
88+
noneOf = satisfy <<< flip notElem

0 commit comments

Comments
 (0)