parcom-lib-0.8.0.2: A simple parser-combinator library, a bit like Parsec but without the frills

Safe HaskellSafe-Inferred

Text.Parcom.Stream

Description

Typeclasses to describe various aspects of parsable streams and tokens. The standard range of Haskell string-like types (String, and both the lazy and strict flavors of ByteString and Text) is supported already, as well as any List, so under normal circumstances, you should not need to touch this module directly.

Synopsis

Documentation

class Stream s t | s -> t whereSource

Typeclass for types that are suitable as source streams. Note that implementing just Stream gives you only a small subset of Parcom's features; if you want to implement your own Stream instances, you will most likely also want to implement Token for the corresponding token type, Listish to enable parsers that need to convert to or from lists of tokens, and Textish to enable parsers that work on Unicode text.

Methods

peek :: s -> tSource

atEnd :: s -> BoolSource

consume :: s -> sSource

pop :: s -> (t, s)Source

Instances

Stream ByteString Word8 
Stream ByteString Word8 
Stream Text Char 
Stream Text Char 
Stream [a] a

All lists are instances of Stream, and the corresponding token type is the list element type. Obviously, this also includes String ('[Char]') and '[Word8]'

class Token t whereSource

This typeclass is pretty much required to do anything useful with Parcom; it is needed for Parcom to detect line endings so that parser errors will report the correct source positions. If you need to parse streams that do not support any meaningful concept of lines, consider implementing a dummy instance, like so: instance Token Foobar where isLineDelimiter _ = False This will treat the entire input as a single line.

Instances

Token Char

Unicode characters are valid tokens.

Token Word8 

class Listish s t | s -> t whereSource

List-like types.

Methods

toList :: s -> [t]Source

fromList :: [t] -> sSource

Instances

Listish ByteString Word8 
Listish ByteString Word8 
Listish Text Char 
Listish Text Char 
Listish [a] a

Any list is trivially list-like

class Textish s whereSource

Enables parsing on a per-character basis rather than per-token. For stream types where the token type is Char already, this is trivial, but for other streams (e.g., bytestrings), some extra processing is required to perform a conversion to Unicode.

Methods

peekChar :: s -> (Maybe Char, Int)Source

Character-wise equivalent of peek. Returns a pair, where the first element is Just the parsed Unicode character, or Nothing on failure, and the second element is the number of tokens that the character has consumed. Generally, there are two reasons why parsing may fail: end-of-input, and a token sequence that does not represent a valid Unicode character according to the underlying stream's semantics.