Skip to content

Commit 147e431

Browse files
committed
Merge pull request #6 from purescript-contrib/additions
Additions
2 parents 427bc89 + 1bf7864 commit 147e431

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/Text/Parsing/StringParser/Combinators.purs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module Text.Parsing.StringParser.Combinators where
22

3+
import Control.Alt ((<|>))
4+
import Control.Apply ((*>))
35
import Data.Maybe (Maybe(..))
4-
5-
import Control.Alt
6-
76
import Text.Parsing.StringParser
87

8+
lookAhead :: forall a. Parser a -> Parser a
9+
lookAhead p = Parser \ps fc sc -> unParser p ps fc (\s _ -> sc s ps)
10+
911
many :: forall a. Parser a -> Parser [a]
1012
many p = many1 p <|> return []
1113

@@ -100,3 +102,10 @@ choice :: forall a. [Parser a] -> Parser a
100102
choice [] = fail "Nothing to parse"
101103
choice [x] = x
102104
choice (x:xs) = x <|> choice xs
105+
106+
manyTill :: forall a end. Parser a -> Parser end -> Parser [a]
107+
manyTill p end = scan
108+
where
109+
scan = (end *> return []) <|> do x <- p
110+
xs <- scan
111+
return (x:xs)

src/Text/Parsing/StringParser/String.purs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
module Text.Parsing.StringParser.String where
1+
module Text.Parsing.StringParser.String
2+
( eof
3+
, anyChar
4+
, anyDigit
5+
, string
6+
) where
27

3-
import Data.Maybe
4-
import Data.String (charAt, fromChar, length, take, indexOf')
8+
import Data.Maybe (Maybe(..))
9+
import Data.String (charAt, fromChar, length, indexOf')
510
import Text.Parsing.StringParser
11+
import qualified Data.String.Regex as Rx
612

713
eof :: Parser Unit
814
eof = Parser (\s fc sc -> case s of
@@ -15,6 +21,18 @@ anyChar = Parser (\s fc sc -> case s of
1521
Just chr -> sc (fromChar chr) { str: str, pos: i + 1 }
1622
Nothing -> fc i (ParseError "Unexpected EOF"))
1723

24+
anyDigit :: Parser String
25+
anyDigit = Parser \{ str: str, pos: i } fc sc -> case charAt i str of
26+
Just chr ->
27+
let chrS = fromChar chr
28+
in if Rx.test rxDigit chrS
29+
then sc chrS { str: str, pos: i + 1 }
30+
else fc i (ParseError "Expected digit")
31+
Nothing -> fc i (ParseError "Unexpected EOF")
32+
where
33+
rxDigit :: Rx.Regex
34+
rxDigit = Rx.regex "^[0-9]" Rx.noFlags
35+
1836
string :: String -> Parser String
1937
string nt = Parser (\s fc sc -> case s of
2038
{ str = str, pos = i } | indexOf' nt i str == i -> sc nt { str: str, pos: i + length nt }

0 commit comments

Comments
 (0)