Skip to content

Commit acb7121

Browse files
committed
More descriptive record labels for splitMap
1 parent dc2ee8b commit acb7121

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/Text/Parsing/Parser/String.purs

+16-16
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ eof = ParserT
7777

7878
-- | Match the entire rest of the input stream. Always succeeds.
7979
rest :: forall m. ParserT String m String
80-
rest = splitMap \before ->
81-
Right { value: before, before, after: "" }
80+
rest = splitMap \consumed ->
81+
Right { value: consumed, consumed, remainder: "" }
8282

8383
-- | Match the specified string.
8484
string :: forall m. String -> ParserT String m String
8585
string str = splitMap \input ->
8686
case stripPrefix (Pattern str) input of
87-
Just after ->
88-
Right { value: str, before: str, after }
87+
Just remainder ->
88+
Right { value: str, consumed: str, remainder }
8989
_ ->
9090
Left $ "Expected " <> show str
9191

@@ -144,7 +144,7 @@ takeN :: forall m. Int -> ParserT String m String
144144
takeN n = splitMap \input -> do
145145
let { before, after } = splitAt n input
146146
if length before == n then
147-
Right { value: before, before, after }
147+
Right { value: before, consumed: before, remainder: after }
148148
else
149149
Left $ "Could not take " <> show n <> " characters"
150150

@@ -156,9 +156,9 @@ whiteSpace = fst <$> match skipSpaces
156156
-- | Skip whitespace characters and throw them away. Always succeeds.
157157
skipSpaces :: forall m. ParserT String m Unit
158158
skipSpaces = splitMap \input -> do
159-
let before = takeWhile isSpace input
160-
let after = SCU.drop (SCU.length before) input
161-
Right { value: unit, before, after }
159+
let consumed = takeWhile isSpace input
160+
let remainder = SCU.drop (SCU.length consumed) input
161+
Right { value: unit, consumed, remainder }
162162

163163
-- | Match one of the BMP `Char`s in the array.
164164
oneOf :: forall m. Array Char -> ParserT String m Char
@@ -293,9 +293,9 @@ regex flags pattern =
293293
Right regexobj ->
294294
splitMap \input -> do
295295
case NonEmptyArray.head <$> Regex.match regexobj input of
296-
Just (Just before) -> do
297-
let after = SCU.drop (SCU.length before) input
298-
Right { value: before, before, after }
296+
Just (Just consumed) -> do
297+
let remainder = SCU.drop (SCU.length consumed) input
298+
Right { value: consumed, consumed, remainder }
299299
_ ->
300300
Left "No Regex pattern match"
301301
where
@@ -322,17 +322,17 @@ type RegexFlagsRow =
322322

323323
-- | Splits the input string while yielding a value.
324324
-- | * `value` is the value to return.
325-
-- | * `before` is the input that was consumed and is used to update the parser position.
326-
-- | * `after` is the new input state.
325+
-- | * `consumed` is the input that was consumed and is used to update the parser position.
326+
-- | * `remainder` is the new input state.
327327
splitMap
328328
:: forall m a
329-
. (String -> Either String { value :: a, before :: String, after :: String })
329+
. (String -> Either String { value :: a, consumed :: String, remainder :: String })
330330
-> ParserT String m a
331331
splitMap f = ParserT
332332
( mkFn5 \state1@(ParseState input pos _) _ _ throw done ->
333333
case f input of
334334
Left err ->
335335
runFn2 throw state1 (ParseError err pos)
336-
Right { value, before, after } ->
337-
runFn2 done (ParseState after (updatePosString pos before after) true) value
336+
Right { value, consumed, remainder } ->
337+
runFn2 done (ParseState remainder (updatePosString pos consumed remainder) true) value
338338
)

0 commit comments

Comments
 (0)