@@ -31,9 +31,8 @@ import Data.Either (Either(..))
31
31
import Data.Enum (fromEnum )
32
32
import Data.Foldable (class Foldable , foldMap , elem , notElem )
33
33
import Data.Maybe (Maybe (..))
34
- import Data.String.CodePoints (codePointAt , drop , indexOf , length )
35
- import Data.String.CodeUnits (singleton )
36
- import Data.String.Pattern (Pattern (..))
34
+ import Data.String.CodePoints as SCP
35
+ import Data.String.CodeUnits as SCU
37
36
import Data.String.Regex as Regex
38
37
import Data.String.Regex.Flags (noFlags )
39
38
import Text.Parsing.StringParser (Parser (..), try , fail )
@@ -43,15 +42,15 @@ import Text.Parsing.StringParser.Combinators (many, (<?>))
43
42
eof :: Parser Unit
44
43
eof = Parser \s ->
45
44
case s of
46
- { substr, posFromStart } | 0 < length substr -> Left { pos: posFromStart, error: " Expected EOF" }
45
+ { substr, posFromStart } | 0 < SCU . length substr -> Left { pos: posFromStart, error: " Expected EOF" }
47
46
_ -> Right { result: unit, suffix: s }
48
47
49
48
-- | Match any character.
50
49
anyChar :: Parser Char
51
50
anyChar = Parser \{ substr, posFromStart } ->
52
- case codePointAt 0 substr of
51
+ case SCP . codePointAt 0 substr of
53
52
Just cp -> case toChar cp of
54
- Just chr -> Right { result: chr, suffix: { substr: drop 1 substr, posFromStart: posFromStart + 1 } }
53
+ Just chr -> Right { result: chr, suffix: { substr: SCP . drop 1 substr, posFromStart: posFromStart + 1 } }
55
54
Nothing -> Left { pos: posFromStart, error: " CodePoint " <> show cp <> " is not a character" }
56
55
Nothing -> Left { pos: posFromStart, error: " Unexpected EOF" }
57
56
where
@@ -66,10 +65,13 @@ anyDigit = try do
66
65
67
66
-- | Match the specified string.
68
67
string :: String -> Parser String
69
- string nt = Parser \s ->
70
- case s of
71
- { substr, posFromStart } | indexOf (Pattern nt) substr == Just 0 -> Right { result: nt, suffix: { substr: drop (length nt) substr, posFromStart: posFromStart + length nt } }
72
- { posFromStart } -> Left { pos: posFromStart, error: " Expected '" <> nt <> " '." }
68
+ string pattern = Parser \{ substr, posFromStart } ->
69
+ let
70
+ length = SCU .length pattern
71
+ { before, after } = SCU .splitAt length substr
72
+ in
73
+ if before == pattern then Right { result: pattern, suffix: { substr: after, posFromStart: posFromStart + length } }
74
+ else Left { pos: posFromStart, error: " Expected '" <> pattern <> " '." }
73
75
74
76
-- | Match a character satisfying the given predicate.
75
77
satisfy :: (Char -> Boolean ) -> Parser Char
@@ -86,7 +88,7 @@ char c = satisfy (_ == c) <?> "Could not match character " <> show c
86
88
whiteSpace :: Parser String
87
89
whiteSpace = do
88
90
cs <- many (satisfy \c -> c == ' \n ' || c == ' \r ' || c == ' ' || c == ' \t ' )
89
- pure (foldMap singleton cs)
91
+ pure (foldMap SCU . singleton cs)
90
92
91
93
-- | Skip many whitespace characters.
92
94
skipSpaces :: Parser Unit
@@ -138,6 +140,6 @@ regex pat =
138
140
matchRegex r = Parser \{ substr, posFromStart } -> do
139
141
case NEA .head <$> Regex .match r substr of
140
142
Just (Just matched) ->
141
- Right { result: matched, suffix: { substr: drop (length matched) substr, posFromStart: posFromStart + length matched } }
143
+ Right { result: matched, suffix: { substr: SCU . drop (SCU . length matched) substr, posFromStart: posFromStart + SCU . length matched } }
142
144
_ ->
143
145
Left { pos: posFromStart, error: " no match" }
0 commit comments