Skip to content

match combinator #107

Closed
Closed
@jamesdbrock

Description

@jamesdbrock

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions