polyparse-1.13.1: A variety of alternative parser combinator libraries.

Safe Haskell | Safe-Inferred
Language | Haskell2010

Text.Parse.ByteString

Contents

- The Parse class is a replacement for the standard Read class.
  - Combinators specific to bytestring input, lexed haskell-style
  - Parsers for literal numerics and characters
  - Re-export all the more general combinators from Poly too
  - ByteStrings and Strings as whole entities

Synopsis

- type TextParser a = Parser a
- class Parse a where
  - parse :: TextParser a
  - parsePrec :: Int -> TextParser a
  - parseList :: TextParser [a]
- parseByRead :: Read a => String -> TextParser a
- readByParse :: TextParser a -> ReadS a
- readsPrecByParsePrec :: (Int -> TextParser a) -> Int -> ReadS a
- word :: TextParser String
- isWord :: String -> TextParser String
- literal :: String -> TextParser String
- optionalParens :: TextParser a -> TextParser a
- parens :: Bool -> TextParser a -> TextParser a
- field :: Parse a => String -> TextParser a
- constructors :: [(String, TextParser a)] -> TextParser a
- enumeration :: Show a => String -> [a] -> TextParser a
- parseSigned :: Real a => TextParser a -> TextParser a
- parseInt :: Integral a => String -> a -> (Char -> Bool) -> (Char -> Int) -> TextParser a
- parseDec :: Integral a => TextParser a
- parseOct :: Integral a => TextParser a
- parseHex :: Integral a => TextParser a
- parseUnsignedInteger :: TextParser Integer
- parseFloat :: RealFrac a => TextParser a
- parseLitChar :: TextParser Char
- parseLitChar' :: TextParser Char
- module Text.ParserCombinators.Poly.ByteStringChar
- allAsByteString :: TextParser ByteString
- allAsString :: TextParser String

# The Parse class is a replacement for the standard Read class.

The Parse class is a replacement for the standard Read class. It is a specialisation of the (poly) Parser monad for ByteString input. There are instances defined for all Prelude types. For user-defined types, you can write your own instance, or use DrIFT to generate them automatically, e.g. {-! derive : Parse !-}

type TextParser a = Parser a Source #

A synonym for a ByteString Parser, i.e. bytestring input (no state)

class Parse a where Source #

The class `Parse` is a replacement for `Read`, operating over String input. Essentially, it permits better error messages for why something failed to parse. It is rather important that `parse` can read back exactly what is generated by the corresponding instance of `show`. To apply a parser to some text, use `runParser`.

Minimal complete definition

Nothing

Methods

parse :: TextParser a Source #

A straightforward parser for an item. (A minimal definition of a class instance requires either |parse| or |parsePrec|. In general, for a type that never needs parens, you should define |parse|, but for a type that _may_ need parens, you should define |parsePrec|.)

parsePrec :: Int -> TextParser a Source #

A straightforward parser for an item, given the precedence of any surrounding expression. (Precedence determines whether parentheses are mandatory or optional.)

parseList :: TextParser [a] Source #

Parsing a list of items by default accepts the [] and comma syntax, except when the list is really a character string using "".

#### Instances

Instances details

Parse Ordering Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser Ordering Source # parsePrec :: Int -> TextParser Ordering Source # parseList :: TextParser [Ordering] Source #
Parse Integer Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser Integer Source # parsePrec :: Int -> TextParser Integer Source # parseList :: TextParser [Integer] Source #
Parse () Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser () Source # parsePrec :: Int -> TextParser () Source # parseList :: TextParser [()] Source #
Parse Bool Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser Bool Source # parsePrec :: Int -> TextParser Bool Source # parseList :: TextParser [Bool] Source #
Parse Char Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser Char Source # parsePrec :: Int -> TextParser Char Source # parseList :: TextParser [Char] Source #
Parse Double Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser Double Source # parsePrec :: Int -> TextParser Double Source # parseList :: TextParser [Double] Source #
Parse Float Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser Float Source # parsePrec :: Int -> TextParser Float Source # parseList :: TextParser [Float] Source #
Parse Int Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser Int Source # parsePrec :: Int -> TextParser Int Source # parseList :: TextParser [Int] Source #
Parse a => Parse (Maybe a) Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser (Maybe a) Source # parsePrec :: Int -> TextParser (Maybe a) Source # parseList :: TextParser [Maybe a] Source #
Parse a => Parse [a] Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser [a] Source # parsePrec :: Int -> TextParser [a] Source # parseList :: TextParser [[a]] Source #
(Parse a, Parse b) => Parse (Either a b) Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser (Either a b) Source # parsePrec :: Int -> TextParser (Either a b) Source # parseList :: TextParser [Either a b] Source #
(Parse a, Parse b) => Parse (a, b) Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser (a, b) Source # parsePrec :: Int -> TextParser (a, b) Source # parseList :: TextParser [(a, b)] Source #
(Parse a, Parse b, Parse c) => Parse (a, b, c) Source # |
Instance details Defined in Text.Parse.ByteString Methods parse :: TextParser (a, b, c) Source # parsePrec :: Int -> TextParser (a, b, c) Source # parseList :: TextParser [(a, b, c)] Source #

parseByRead :: Read a => String -> TextParser a Source #

If there already exists a Read instance for a type, then we can make a Parser for it, but with only poor error-reporting. The string argument is the expected type or value (for error-reporting only). Use of this wrapper function is NOT recommended with ByteString, because there is a lot of inefficiency in repeated conversions to/from String.

readByParse :: TextParser a -> ReadS a Source #

If you have a TextParser for a type, you can easily make it into a Read instance, by throwing away any error messages. Use of this wrapper function is NOT recommended with ByteString, because there is a lot of inefficiency in conversions to/from String.

readsPrecByParsePrec :: (Int -> TextParser a) -> Int -> ReadS a Source #

If you have a TextParser for a type, you can easily make it into a Read instance, by throwing away any error messages. Use of this wrapper function is NOT recommended with ByteString, because there is a lot of inefficiency in conversions to/from String.

## Combinators specific to bytestring input, lexed haskell-style

word :: TextParser String Source #

One lexical chunk (Haskell-style lexing).

isWord :: String -> TextParser String Source #

Ensure that the next input word is the given string. (Note the input is lexed as haskell, so wordbreaks at spaces, symbols, etc.)

literal :: String -> TextParser String Source #

Ensure that the next input word is the given string. (No lexing, so mixed spaces, symbols, are accepted.)

optionalParens :: TextParser a -> TextParser a Source #

Allow optional nested string parens around an item.

parens :: Bool -> TextParser a -> TextParser a Source #

Allow nested parens around an item (one set required when Bool is True).

field :: Parse a => String -> TextParser a Source #

Deal with named field syntax. The string argument is the field name, and the parser returns the value of the field.

constructors :: [(String, TextParser a)] -> TextParser a Source #

Parse one of a bunch of alternative constructors. In the list argument, the first element of the pair is the constructor name, and the second is the parser for the rest of the value. The first matching parse is returned.

enumeration :: Show a => String -> [a] -> TextParser a Source #

Parse one of the given nullary constructors (an enumeration). The string argument is the name of the type, and the list argument should contain all of the possible enumeration values.

## Parsers for literal numerics and characters

parseSigned :: Real a => TextParser a -> TextParser a Source #

For any numeric parser, permit a negation sign in front of it.

parseInt :: Integral a => String -> a -> (Char -> Bool) -> (Char -> Int) -> TextParser a Source #

Parse any (unsigned) Integral numeric literal. Needs a base, radix, isDigit predicate, and digitToInt converter, appropriate to the result type.

parseDec :: Integral a => TextParser a Source #

Parse a decimal, octal, or hexadecimal (unsigned) Integral numeric literal.

parseOct :: Integral a => TextParser a Source #

Parse a decimal, octal, or hexadecimal (unsigned) Integral numeric literal.

parseHex :: Integral a => TextParser a Source #

Parse a decimal, octal, or hexadecimal (unsigned) Integral numeric literal.

parseUnsignedInteger :: TextParser Integer Source #

parseUnsignedInteger uses the underlying ByteString readInteger, so will be a lot faster than the generic character-by-character parseInt.

parseFloat :: RealFrac a => TextParser a Source #

Parse any (unsigned) Floating numeric literal, e.g. Float or Double.

parseLitChar :: TextParser Char Source #

Parse a Haskell character literal, excluding surrounding single quotes.

parseLitChar' :: TextParser Char Source #

Parse a Haskell character literal, including surrounding single quotes.

## Re-export all the more general combinators from Poly too

module Text.ParserCombinators.Poly.ByteStringChar

## ByteStrings and Strings as whole entities

allAsByteString :: TextParser ByteString Source #

Simply return the remaining input ByteString.

allAsString :: TextParser String Source #

Simply return the remaining input as a String.