hectoparsec-0.1.0.0: Flexible and powerful parser combinators
Copyright(c) comp 2020
LicenseMIT
Maintaineronecomputer00@gmail.com
Stabilitystable
Portabilityportable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Hectoparsec.Stream

Description

Typeclass for manipulating input streams.

By default, textual input streams are supported by Hectoparsec. Custom streams, like token lists or lexers can also made into a Stream.

import Data.Bifunctor (second)
import Data.List (uncons)

data Tok = Tok
    { tokSpan  :: (Pos, Pos) -- Start and end span of the token.
    , tokValue :: String
    }

newtype TokStream = TokStream [Tok]

instance Stream TokStream where
    type Token TokStream = Tok
    type Chunk TokStream = [Tok]

    streamUncons (TokStream xs) = second TokStream <$> uncons xs
    updatePosToken _ tok _ = snd (tokSpan tok)
Synopsis

Stream typeclass

class Stream s where Source #

A Stream represents an input stream. These streams can be acted on one token at a time, or with multiple tokens at a time in a chunk. Each token should represent a span of text (possibly just a character) in the source code.

Minimal complete definition

streamUncons, updatePosToken

Associated Types

type Token s Source #

The token type. This is a single element in your stream.

type Chunk s Source #

The type of chunks. This is a sequence of tokens in your stream.

Methods

streamUncons :: s -> Maybe (Token s, s) Source #

Take the next token out of the stream. If the stream is empty, return Nothing.

streamSplitAt :: Int -> s -> (Chunk s, s) Source #

Take at most the next n tokens out of the stream. If n is negative, return an empty chunk.

By default, this repeatedly calls streamUncons, which may be ineffecient.

streamSpan :: (Token s -> Bool) -> s -> (Chunk s, s) Source #

Take tokens as long as a predicate holds.

By default, this repeatedly calls streamUncons, which may be ineffecient.

chunkToTokens :: proxy s -> Chunk s -> [Token s] Source #

Converts a chunk to tokens. This should be an isomorphism with tokensToChunk.

The default implementation is available if the chunk and tokens are coercible.

default chunkToTokens :: Coercible (Chunk s) [Token s] => proxy s -> Chunk s -> [Token s] Source #

tokensToChunk :: proxy s -> [Token s] -> Chunk s Source #

Converts tokens to a chunk. This should be an isomorphism with chunkToTokens.

The default implementation is available if the chunk and tokens are coercible.

default tokensToChunk :: Coercible (Chunk s) [Token s] => proxy s -> [Token s] -> Chunk s Source #

chunkLength :: proxy s -> Chunk s -> Int Source #

Gets the length of a chunk.

By default, this converts the chunk to tokens, which may be inefficient.

chunkLength proxy xs = length (chunkToTokens proxy xs)

chunkNull :: proxy s -> Chunk s -> Bool Source #

Checks whether a chunk is empty.

By default, this converts the chunk to tokens, which may be inefficient.

chunkNull proxy xs = null (chunkToTokens proxy xs)

foldChunk :: proxy s -> (b -> Token s -> b) -> b -> Chunk s -> b Source #

Performs a fold over a chunk.

By default, this converts the chunk to tokens then folds over the list. There might be a better performing function for your custom stream type.

updatePosToken :: proxy s -> Token s -> Pos -> Pos Source #

Increments position according to a token.

updatePosChunk :: proxy s -> Chunk s -> Pos -> Pos Source #

Increments position according to a chunk.

Instances

Instances details
Stream ByteString Source # 
Instance details

Defined in Hectoparsec.Stream

Associated Types

type Token ByteString Source #

type Chunk ByteString Source #

Stream ByteString Source # 
Instance details

Defined in Hectoparsec.Stream

Associated Types

type Token ByteString Source #

type Chunk ByteString Source #

Stream Text Source # 
Instance details

Defined in Hectoparsec.Stream

Associated Types

type Token Text Source #

type Chunk Text Source #

Stream Text Source # 
Instance details

Defined in Hectoparsec.Stream

Associated Types

type Token Text Source #

type Chunk Text Source #

Stream String Source # 
Instance details

Defined in Hectoparsec.Stream

Associated Types

type Token String Source #

type Chunk String Source #