Text.Parse

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

Safe Haskell | Safe
Language | Haskell98

Text.Parse

Contents

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

Synopsis

- type TextParser a = Parser Char 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
- parseFloat :: RealFrac a => TextParser a
- parseLitChar :: TextParser Char
- module Text.ParserCombinators.Poly
- 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 String 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 Char a

A synonym for Parser Char, i.e. string input (no state)

class Parse a where

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

A straightforward parser for an item. (A minimal definition of a class instance requires either |parse| or |parsePrec|.)

parsePrec :: Int -> TextParser a

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

parseList :: TextParser [a]

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

Instances

Parse Bool |
Parse Char |
Parse Double |
Parse Float |
Parse Int |
Parse Integer |
Parse Ordering |
Parse () |
Parse a => Parse [a] |
Parse a => Parse (Maybe a) |
(Parse a, Parse b) => Parse (Either a b) |
(Parse a, Parse b) => Parse (a, b) |
(Parse a, Parse b, Parse c) => Parse (a, b, c) |

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

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).

readByParse :: TextParser a -> ReadS a

If you have a TextParser for a type, you can easily make it into a Read instance, by throwing away any error messages.

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

If you have a TextParser for a type, you can easily make it into a Read instance, by throwing away any error messages.

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

word :: TextParser String

One lexical chunk. This is Haskell'98-style lexing - the result should match Prelude.lex apart from better error-reporting.

isWord :: String -> TextParser String

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

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

optionalParens :: TextParser a -> TextParser a

Allow nested parens around an item.

parens :: Bool -> TextParser a -> TextParser a

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

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

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

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

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

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

parseFloat :: RealFrac a => TextParser a

parseLitChar :: TextParser Char

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

module Text.ParserCombinators.Poly

## Strings as whole entities

allAsString :: TextParser String

Simply return the entire remaining input String.