match combinator
jamesdbrock opened this issue · 3 comments
We should have the super-useful match combinator.
To write this combinator for Text.Parsing.Parser.String, I think we will need some more members of StringLike.
purescript-parsing/src/Text/Parsing/Parser/String.purs
Lines 21 to 25 in e801a0e
Maybe if we have
length :: s -> Int
length = Data.String.length
take :: Int -> s -> s
take = Data.String.takeThen we can write
match :: forall m s a. ParserT s m a -> ParserT s m (Tuple a s)
match p = do
ParseState input1 _ _ <- get
x <- p
ParseState input2 _ _ <- get
pure $ Tuple x $ take (length input1 - length input2) input1Actually we should use the Javascript length, which is in units of “code units”, instead of Data.String.length. which is in units of “code points”.
And likewise for take.
This would be more efficient, and it will be correct as long as we can assume that the ParseState input always begins on a code point boundary.
Here's an implementation of match which works on String instead of StringLike.
Done in v7.0.0