Type class for inputs that can be consumed by the library.
Note that the Stream instances for Text and ByteString (strict and
lazy) default to "input sharing" (see ShareInput, NoShareInput). We plan
to move away from input sharing in a future major release; if you want to
retain the current behaviour and are concerned with maximum performance you
should consider using the ShareInput wrapper explicitly.
Note: before the version 9.0.0 the class included the methods from VisualStream and TraversableStream.
Associated types
Methods
tokenToChunk :: Proxy s -> Token s -> Tokens sLift a single token to chunk of the stream. The default implementation is:
tokenToChunk pxy = tokensToChunk pxy . pureHowever for some types of stream there may be a more efficient way to lift.
tokensToChunk :: Proxy s -> [Token s] -> Tokens sThe first method that establishes isomorphism between list of tokens and chunk of the stream. Valid implementation should satisfy:
chunkToTokens pxy (tokensToChunk pxy ts) == tschunkToTokens :: Proxy s -> Tokens s -> [Token s]The second method that establishes isomorphism between list of tokens and chunk of the stream. Valid implementation should satisfy:
tokensToChunk pxy (chunkToTokens pxy chunk) == chunkchunkLength :: Proxy s -> Tokens s -> IntReturn length of a chunk of the stream.
chunkEmpty :: Proxy s -> Tokens s -> BoolCheck if a chunk of the stream is empty. The default implementation is in terms of the more general chunkLength:
chunkEmpty pxy ts = chunkLength pxy ts <= 0However for many streams there may be a more efficient implementation.
take1_ :: s -> Maybe (Token s, s)Extract a single token form the stream. Return Nothing if the stream is empty.
takeN_ :: Int -> s -> Maybe (Tokens s, s)takeN_ n sshould try to extract a chunk of lengthn, or if the stream is too short, the rest of the stream. Valid implementation should follow the rules:If the requested length
nis 0 (or less), Nothing should never be returned, insteadJust ("", s)should be returned, where""stands for the empty chunk, andsis the original stream (second argument).If the requested length is greater than 0 and the stream is empty, Nothing should be returned indicating end of input.
In other cases, take chunk of length
n(or shorter if the stream is not long enough) from the input stream and return the chunk along with the rest of the stream.
takeWhile_ :: (Token s -> Bool) -> s -> (Tokens s, s)Extract chunk of the stream taking tokens while the supplied predicate returns True. Return the chunk and the rest of the stream.
For many types of streams, the method allows for significant performance improvements, although it is not strictly necessary from conceptual point of view.
Instances14Stream, …
Stream ByteStringDefined in megaparsec-9.7.0 · Text.Megaparsec.StreamStream ByteStringDefined in megaparsec-9.7.0 · Text.Megaparsec.StreamStream TextDefined in megaparsec-9.7.0 · Text.Megaparsec.StreamStream TextDefined in megaparsec-9.7.0 · Text.Megaparsec.StreamOrd a => Stream (Seq a)Defined in megaparsec-9.7.0 · Text.Megaparsec.StreamOrd a => Stream [a]Defined in megaparsec-9.7.0 · Text.Megaparsec.StreamStream (NoShareInput ByteString)Defined in megaparsec-9.7.0 · Text.Megaparsec.StreamStream (NoShareInput ByteString)Defined in megaparsec-9.7.0 · Text.Megaparsec.StreamStream (NoShareInput Text)Defined in megaparsec-9.7.0 · Text.Megaparsec.StreamStream (NoShareInput Text)Defined in megaparsec-9.7.0 · Text.Megaparsec.StreamStream (ShareInput ByteString)Defined in megaparsec-9.7.0 · Text.Megaparsec.StreamStream (ShareInput ByteString)Defined in megaparsec-9.7.0 · Text.Megaparsec.StreamStream (ShareInput Text)Defined in megaparsec-9.7.0 · Text.Megaparsec.StreamStream (ShareInput Text)Defined in megaparsec-9.7.0 · Text.Megaparsec.Stream