Copyright | (c) comp 2020 |
---|---|
License | MIT |
Maintainer | onecomputer00@gmail.com |
Stability | stable |
Portability | portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
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] instanceStream
TokStream where typeToken
TokStream = Tok typeChunk
TokStream = [Tok]streamUncons
(TokStream xs) = second TokStream <$> uncons xsupdatePosToken
_ tok _ = snd (tokSpan tok)
Synopsis
- class Stream s where
- type Token s
- type Chunk s
- streamUncons :: s -> Maybe (Token s, s)
- streamSplitAt :: Int -> s -> (Chunk s, s)
- streamSpan :: (Token s -> Bool) -> s -> (Chunk s, s)
- chunkToTokens :: proxy s -> Chunk s -> [Token s]
- tokensToChunk :: proxy s -> [Token s] -> Chunk s
- chunkLength :: proxy s -> Chunk s -> Int
- chunkNull :: proxy s -> Chunk s -> Bool
- foldChunk :: proxy s -> (b -> Token s -> b) -> b -> Chunk s -> b
- updatePosToken :: proxy s -> Token s -> Pos -> Pos
- updatePosChunk :: proxy s -> Chunk s -> Pos -> Pos
Stream typeclass
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.
The token type. This is a single element in your stream.
The type of chunks. This is a sequence of tokens in your stream.
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.
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.
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.