Skip to content

match combinator #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jamesdbrock opened this issue Feb 1, 2021 · 3 comments
Closed

match combinator #107

jamesdbrock opened this issue Feb 1, 2021 · 3 comments

Comments

@jamesdbrock
Copy link
Member

jamesdbrock commented Feb 1, 2021

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.

class StringLike s where
drop :: Int -> s -> s
indexOf :: Pattern -> s -> Maybe Int
null :: s -> Boolean
uncons :: s -> Maybe { head :: Char, tail :: s }

Maybe if we have

length :: s -> Int
length = Data.String.length

take :: Int -> s -> s
take = Data.String.take

Then 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) input1
@jamesdbrock
Copy link
Member Author

Actually 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.

@jamesdbrock
Copy link
Member Author

Here's an implementation of match which works on String instead of StringLike.

https://pursuit.purescript.org/packages/purescript-parsing-replace/1.0.2/docs/Text.Parsing.Replace.String.Combinator#v:match

@jamesdbrock
Copy link
Member Author

Done in v7.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant