Git Product home page Git Product logo

Comments (8)

jamesdbrock avatar jamesdbrock commented on May 27, 2024

or

sepEndBy' :: m a -> m (Either String a)
sepEndBy'  sep =  sep  <|> (fmap mconcat $ manyTill anyChar sep)

inefficient because sep has to match twice for each pattern, but still maybe something useful here?

from replace-megaparsec.

jamesdbrock avatar jamesdbrock commented on May 27, 2024

how about skipManyTill?

https://www.stackage.org/haddock/nightly-2019-09-19/parser-combinators/Control-Monad-Combinators.html#v:skipManyTill

sepEndBy' :: m a -> m (String, a)
sepEndBy' sep = do
  getOffset <- off1
  skipManyTill $ do
    off2 <- getOffset
    x <- sep
    return (stringSectionBetween off1 off2, x)
    // and what about when we come to the end of the stream?

from replace-megaparsec.

jamesdbrock avatar jamesdbrock commented on May 27, 2024

https://www.stackage.org/haddock/nightly-2019-09-19/megaparsec-7.0.5/src/Text.Megaparsec.Internal.html#pTakeWhileP

from replace-megaparsec.

jamesdbrock avatar jamesdbrock commented on May 27, 2024

See also @chshersh 's issue:

mrkkrp/megaparsec#367

from replace-megaparsec.

jamesdbrock avatar jamesdbrock commented on May 27, 2024

So here's the thing. Parsers such as takeWhileP are based on span, which has type

span :: (Char -> Bool) -> Text -> (Text, Text) 

The problem is the predicate only takes one char.

There is also breakOn which is predicated on a constant string.

What we need to write a sepCap which is fast in the sparse case is a function which doesn't seems to exist in the world of Text or ByteString, which is like breakOn, but predicated on the beginning of the second string in the break.

breakPred :: (Text -> Bool) -> Text -> (Text, Text)

Actually, what we want is not a predicate, because we need information about how much of the string to break on. So we need this function.

breakSatisfy :: (Text -> Maybe Int) -> Text -> (Text, Text, Text)

Let's call (Text -> Maybe Int) the "satisfaction" function. breakSatisfy will call the satisfaction function on each tail of the input string until the satisfaction function returns a span. Then breakSatisfy will return the substring up until satisfaction began, the satisfying substring, and the remaining unsatisfying substring.

from replace-megaparsec.

jamesdbrock avatar jamesdbrock commented on May 27, 2024
untilCap :: Parser a -> Parser (Text, a)

Parser fails when no match. Prefix or suffix may be zero length. Zero-width match allowed?

streamSplit :: Parser a -> s -> Maybe (Text, a, Text)

Nothing when untilCap fails.

from replace-megaparsec.

jamesdbrock avatar jamesdbrock commented on May 27, 2024

@mrkkrp mostly solved this problem this summer in v1.2.0, see manyTill_ and someTill_.

from replace-megaparsec.

jamesdbrock avatar jamesdbrock commented on May 27, 2024

Added anyTill for untilCap

Added splitCap for streamSplit

from replace-megaparsec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.