1
- module Text.Parsing.StringParser.String where
1
+ module Text.Parsing.StringParser.String
2
+ ( eof
3
+ , anyChar
4
+ , anyDigit
5
+ , string
6
+ ) where
2
7
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' )
5
10
import Text.Parsing.StringParser
11
+ import qualified Data.String.Regex as Rx
6
12
7
13
eof :: Parser Unit
8
14
eof = Parser (\s fc sc -> case s of
@@ -15,6 +21,18 @@ anyChar = Parser (\s fc sc -> case s of
15
21
Just chr -> sc (fromChar chr) { str: str, pos: i + 1 }
16
22
Nothing -> fc i (ParseError " Unexpected EOF" ))
17
23
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
+
18
36
string :: String -> Parser String
19
37
string nt = Parser (\s fc sc -> case s of
20
38
{ str = str, pos = i } | indexOf' nt i str == i -> sc nt { str: str, pos: i + length nt }
0 commit comments