    module Text.Parse
      ( -- * The Parse class is a replacement for the standard Read class.
        -- $parser
        TextParser  -- synonym for Parser Char, i.e. string input, no state
      , Parse(..)   -- instances: (), (a,b), (a,b,c), Maybe a, Either a, [a],
                    --            Int, Integer, Float, Double, Char, Bool
      , parseByRead -- :: Read a => String -> TextParser a
      , readByParse -- :: TextParser a -> ReadS a
      , readsPrecByParsePrec -- :: (Int->TextParser a) -> Int -> ReadS a
        -- ** Combinators specific to string input, lexed haskell-style
      , word        -- :: TextParser String
      , isWord      -- :: String -> TextParser ()
      , literal     -- :: String -> TextParser ()
      , 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
        -- ** Parsers for literal numerics and characters
      , parseSigned
      , parseInt
      , parseDec
      , parseOct
      , parseHex
      , parseFloat
      , parseLitChar
      , parseLitChar'
        -- ** Re-export all the more general combinators from Poly too
      , module Text.ParserCombinators.Poly
        -- ** Strings as whole entities
      , allAsString
      ) where

    import Data.Char as Char (isSpace,toLower,isUpper,isDigit,isOctDigit
                             ,isHexDigit,digitToInt,isAlpha,isAlphaNum,ord,chr)
    import Data.List (intersperse)
    import Data.Ratio
    import Text.ParserCombinators.Poly

    ------------------------------------------------------------------------
    -- $parser
    -- 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 !-}

    -- | A synonym for Parser Char, i.e. string input (no state)
    type TextParser a = Parser Char a

    -- | 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@.
    class Parse a where
        -- | A straightforward parser for an item.  (A minimal definition of
        --   a class instance requires either |parse| or |parsePrec|.)
        parse     :: TextParser a
        parse       = Int -> TextParser a
    forall a. Parse a => Int -> TextParser a
    parsePrec Int
    0
        -- | A straightforward parser for an item, given the precedence of
        --   any surrounding expression.  (Precedence determines whether
        --   parentheses are mandatory or optional.)
        parsePrec :: Int -> TextParser a
        parsePrec Int
    _ = TextParser a -> TextParser a
    forall a. TextParser a -> TextParser a
    optionalParens TextParser a
    forall a. Parse a => TextParser a
    parse
        -- | Parsing a list of items by default accepts the [] and comma syntax,
        --   except when the list is really a character string using \"\".
        parseList :: TextParser [a] -- only to distinguish [] and ""
        parseList  = do { String -> TextParser String
    isWord String
    "[]"; [a] -> TextParser [a]
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return [] }
                       TextParser [a] -> TextParser [a] -> TextParser [a]
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                     do { String -> TextParser String
    isWord String
    "["; String -> TextParser String
    isWord String
    "]"; [a] -> TextParser [a]
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return [] }
                       TextParser [a] -> TextParser [a] -> TextParser [a]
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                     TextParser String
    -> TextParser String
    -> TextParser String
    -> TextParser a
    -> TextParser [a]
    forall (p :: * -> *) bra sep ket a.
    PolyParse p =>
    p bra -> p sep -> p ket -> p a -> p [a]
    bracketSep (String -> TextParser String
    isWord String
    "[") (String -> TextParser String
    isWord String
    ",") (String -> TextParser String
    isWord String
    "]")
                                (TextParser a -> TextParser a
    forall a. TextParser a -> TextParser a
    optionalParens TextParser a
    forall a. Parse a => TextParser a
    parse)
                       TextParser [a] -> (String -> String) -> TextParser [a]
    forall a. Parser Char a -> (String -> String) -> Parser Char a
    forall (p :: * -> *) a.
    Commitment p =>
    p a -> (String -> String) -> p a
    `adjustErr` (String
    "Expected a list, but\n"String -> String -> String
    forall a. [a] -> [a] -> [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).
    parseByRead :: Read a => String -> TextParser a
    parseByRead :: forall a. Read a => String -> TextParser a
    parseByRead String
    name =
        (String -> Result String a) -> Parser Char a
    forall t a. ([t] -> Result [t] a) -> Parser t a
    P (\String
    s-> case ReadS a
    forall a. Read a => ReadS a
    reads String
    s of
                    []       -> String -> String -> Result String a
    forall z a. z -> String -> Result z a
    Failure String
    s (String
    "no parse, expected a "String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    name)
                    [(a
    a,String
    s')] -> String -> a -> Result String a
    forall z a. z -> a -> Result z a
    Success String
    s' a
    a
                    [(a, String)]
    _        -> String -> String -> Result String a
    forall z a. z -> String -> Result z a
    Failure String
    s (String
    "ambiguous parse, expected a "String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    name)
          )

    -- | If you have a TextParser for a type, you can easily make it into
    --   a Read instance, by throwing away any error messages.
    readByParse :: TextParser a -> ReadS a
    readByParse :: forall a. TextParser a -> ReadS a
    readByParse TextParser a
    p = \String
    inp->
        case TextParser a -> String -> (Either String a, String)
    forall t a. Parser t a -> [t] -> (Either String a, [t])
    runParser TextParser a
    p String
    inp of
            (Left String
    err,  String
    rest) -> []
            (Right a
    val, String
    rest) -> [(a
    val,String
    rest)]

    -- | 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
    readsPrecByParsePrec :: forall a. (Int -> TextParser a) -> Int -> ReadS a
    readsPrecByParsePrec Int -> TextParser a
    p = \Int
    prec String
    inp->
        case TextParser a -> String -> (Either String a, String)
    forall t a. Parser t a -> [t] -> (Either String a, [t])
    runParser (Int -> TextParser a
    p Int
    prec) String
    inp of
            (Left String
    err,  String
    rest) -> []
            (Right a
    val, String
    rest) -> [(a
    val,String
    rest)]

    -- | One lexical chunk.  This is Haskell'98-style lexing - the result
    --   should match Prelude.lex apart from better error-reporting.
    word :: TextParser String
    word :: TextParser String
    word = (String -> Result String String) -> TextParser String
    forall t a. ([t] -> Result [t] a) -> Parser t a
    P String -> Result String String
    p
      where
        p :: String -> Result String String
    p String
    ""       = String -> String -> Result String String
    forall z a. z -> String -> Result z a
    Failure String
    "" String
    "end of input"
        p (Char
    c:String
    s)    | Char -> Bool
    isSpace Char
    c = String -> Result String String
    p ((Char -> Bool) -> String -> String
    forall a. (a -> Bool) -> [a] -> [a]
    dropWhile Char -> Bool
    isSpace String
    s)
        p (Char
    '\'':String
    s) = let (P String -> Result String Char
    lit) = Parser Char Char
    parseLitChar' in (Char -> String) -> Result String Char -> Result String String
    forall a b. (a -> b) -> Result String a -> Result String b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Char -> String
    forall a. Show a => a -> String
    show (String -> Result String Char
    lit (Char
    '\''Char -> String -> String
    forall a. a -> [a] -> [a]
    :String
    s))
        p (Char
    '"':String
    s)  = String -> String -> Result String String
    lexString String
    "\"" String
    s
                 where lexString :: String -> String -> Result String String
    lexString String
    acc (Char
    '"':String
    s)      = String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    s (String -> String
    forall a. [a] -> [a]
    reverse (Char
    '"'Char -> String -> String
    forall a. a -> [a] -> [a]
    :String
    acc))
                       lexString String
    acc []           = String -> String -> Result String String
    forall z a. z -> String -> Result z a
    Failure [] (String
    "end of input in "
                                                               String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    "string literal "
                                                               String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    acc)
                       lexString String
    acc String
    s = let (P String -> Result String Char
    lit) = Parser Char Char
    parseLitChar
                                         in case String -> Result String Char
    lit String
    s of
                                              Failure String
    a String
    b -> String -> String -> Result String String
    forall z a. z -> String -> Result z a
    Failure String
    a String
    b
                                              Success String
    t Char
    c -> String -> String -> Result String String
    lexString (Char
    cChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    acc) String
    t
        p (Char
    '0':Char
    'x':String
    s) = String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    t (Char
    '0'Char -> String -> String
    forall a. a -> [a] -> [a]
    :Char
    'x'Char -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ds) where (String
    ds,String
    t) = (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isHexDigit String
    s
        p (Char
    '0':Char
    'X':String
    s) = String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    t (Char
    '0'Char -> String -> String
    forall a. a -> [a] -> [a]
    :Char
    'X'Char -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ds) where (String
    ds,String
    t) = (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isHexDigit String
    s
        p (Char
    '0':Char
    'o':String
    s) = String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    t (Char
    '0'Char -> String -> String
    forall a. a -> [a] -> [a]
    :Char
    'o'Char -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ds) where (String
    ds,String
    t) = (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isOctDigit String
    s
        p (Char
    '0':Char
    'O':String
    s) = String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    t (Char
    '0'Char -> String -> String
    forall a. a -> [a] -> [a]
    :Char
    'O'Char -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ds) where (String
    ds,String
    t) = (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isOctDigit String
    s
        p (Char
    c:String
    s) | Char -> Bool
    isSingle Char
    c = String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    s [Char
    c]
                | Char -> Bool
    isSym    Char
    c = let (String
    sym,String
    t) = (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isSym String
    s in String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    t (Char
    cChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    sym)
                | Char -> Bool
    isIdInit Char
    c = let (String
    nam,String
    t) = (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isIdChar String
    s in String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    t (Char
    cChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    nam)
                | Char -> Bool
    isDigit  Char
    c = let (String
    ds,String
    t)  = (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isDigit String
    s in
                               String -> String -> Result String String
    lexFracExp (Char
    cChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ds) String
    t
                | Bool
    otherwise  = String -> String -> Result String String
    forall z a. z -> String -> Result z a
    Failure (Char
    cChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    s) (String
    "Bad character: "String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++Char -> String
    forall a. Show a => a -> String
    show Char
    c)
                 where isSingle :: Char -> Bool
    isSingle Char
    c  =  Char
    c Char -> String -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` String
    ",;()[]{}`"
                       isSym :: Char -> Bool
    isSym    Char
    c  =  Char
    c Char -> String -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` String
    "!@#$%&*+./<=>?\\^|:-~"
                       isIdInit :: Char -> Bool
    isIdInit Char
    c  =  Char -> Bool
    isAlpha Char
    c Bool -> Bool -> Bool
    || Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == Char
    '_'
                       isIdChar :: Char -> Bool
    isIdChar Char
    c  =  Char -> Bool
    isAlphaNum Char
    c Bool -> Bool -> Bool
    || Char
    c Char -> String -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` String
    "_'"
                       lexFracExp :: String -> String -> Result String String
    lexFracExp String
    acc (Char
    '.':Char
    d:String
    s) | Char -> Bool
    isDigit Char
    d   =
                                          String -> String -> Result String String
    lexExp (String
    accString -> String -> String
    forall a. [a] -> [a] -> [a]
    ++Char
    '.'Char -> String -> String
    forall a. a -> [a] -> [a]
    :Char
    dChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ds) String
    t
                                                  where (String
    ds,String
    t) = (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isDigit String
    s
                       lexFracExp String
    acc String
    s = String -> String -> Result String String
    lexExp String
    acc String
    s
                       lexExp :: String -> String -> Result String String
    lexExp     String
    acc (Char
    e:String
    s) | Char
    eChar -> String -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem`String
    "eE" =
                                          case String
    s of
                                            (Char
    '+':Char
    d:String
    t) | Char -> Bool
    isDigit Char
    d ->
                                                        let (String
    ds,String
    u)=(Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isDigit String
    t in
                                                        String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    u (String
    accString -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    "e+"String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++Char
    dChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ds)
                                            (Char
    '-':Char
    d:String
    t) | Char -> Bool
    isDigit Char
    d ->
                                                        let (String
    ds,String
    u)=(Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isDigit String
    t in
                                                        String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    u (String
    accString -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    "e-"String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++Char
    dChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ds)
                                            (Char
    d:String
    t) |Char -> Bool
    isDigit Char
    d ->
                                                        let (String
    ds,String
    u)=(Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span Char -> Bool
    isDigit String
    t in
                                                        String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    u (String
    accString -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    "e"String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++Char
    dChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    ds)
                                            String
    _ -> String -> String -> Result String String
    forall z a. z -> String -> Result z a
    Failure String
    s (String
    "missing +/-/digit "
                                                           String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    "after e in float "
                                                           String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    "literal: "
                                                           String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String -> String
    forall a. Show a => a -> String
    show (String
    accString -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    "e"String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    "..."))
                       lexExp     String
    acc String
    s     = String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    s String
    acc

    -- | One lexical chunk (Haskell'98-style lexing - the result should match
    --   Prelude.lex apart from error-reporting).
    oldword :: TextParser String
    oldword :: TextParser String
    oldword = (String -> Result String String) -> TextParser String
    forall t a. ([t] -> Result [t] a) -> Parser t a
    P (\String
    s-> case ReadS String
    lex String
    s of
                       []         -> String -> String -> Result String String
    forall z a. z -> String -> Result z a
    Failure String
    s  (String
    "no input? (impossible)")
                       [(String
    "",String
    "")]  -> String -> String -> Result String String
    forall z a. z -> String -> Result z a
    Failure String
    "" (String
    "no input?")
                       [(String
    "",String
    s')]  -> String -> String -> Result String String
    forall z a. z -> String -> Result z a
    Failure String
    s  (String
    "lexing failed?")
                       ((String
    x,String
    s'):[(String, String)]
    _) -> String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success String
    s' String
    x
             )

    -- | Ensure that the next input word is the given string.  (Note the input
    --   is lexed as haskell, so wordbreaks at spaces, symbols, etc.)
    isWord :: String -> TextParser String
    isWord :: String -> TextParser String
    isWord String
    w = do { w' <- TextParser String
    word
                  ; if w'==w then return w else fail ("expected "++w++" got "++w')
                  }

    -- | Ensure that the next input word is the given string.  (No
    --   lexing, so mixed spaces, symbols, are accepted.)
    literal :: String -> TextParser String
    literal :: String -> TextParser String
    literal String
    w = do { w' <- String -> TextParser String
    forall {a}. Eq a => [a] -> Parser a String
    walk String
    w
                   ; if w'==w then return w else fail ("expected "++w++" got "++w')
                   }
      where walk :: [a] -> Parser a String
    walk []     = String -> Parser a String
    forall a. a -> Parser a a
    forall (m :: * -> *) a. Monad m => a -> m a
    return String
    w
            walk (a
    c:[a]
    cs) = do { x <- Parser a a
    forall t. Parser t t
    next
                             ; if x==c then walk cs
                                       else return []
                             }

    -- | Allow nested parens around an item.
    optionalParens :: TextParser a -> TextParser a
    optionalParens :: forall a. TextParser a -> TextParser a
    optionalParens TextParser a
    p = Bool -> TextParser a -> TextParser a
    forall a. Bool -> TextParser a -> TextParser a
    parens Bool
    False TextParser a
    p

    -- | Allow nested parens around an item (one set required when Bool is True).
    parens :: Bool -> TextParser a -> TextParser a
    parens :: forall a. Bool -> TextParser a -> TextParser a
    parens Bool
    True  TextParser a
    p = TextParser String
    -> TextParser String -> TextParser a -> TextParser a
    forall (p :: * -> *) bra ket a.
    PolyParse p =>
    p bra -> p ket -> p a -> p a
    bracket (String -> TextParser String
    isWord String
    "(") (String -> TextParser String
    isWord String
    ")") (Bool -> TextParser a -> TextParser a
    forall a. Bool -> TextParser a -> TextParser a
    parens Bool
    False TextParser a
    p)
    parens Bool
    False TextParser a
    p = Bool -> TextParser a -> TextParser a
    forall a. Bool -> TextParser a -> TextParser a
    parens Bool
    True TextParser a
    p TextParser a -> TextParser a -> TextParser a
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail` TextParser a
    p

    -- | Deal with named field syntax.  The string argument is the field name,
    --   and the parser returns the value of the field.
    field :: Parse a => String -> TextParser a
    field :: forall a. Parse a => String -> TextParser a
    field String
    name = do { String -> TextParser String
    isWord String
    name; Parser Char a -> Parser Char a
    forall a. TextParser a -> TextParser a
    forall (p :: * -> *) a. Commitment p => p a -> p a
    commit (Parser Char a -> Parser Char a) -> Parser Char a -> Parser Char a
    forall a b. (a -> b) -> a -> b
    $ do { String -> TextParser String
    isWord String
    "="; Parser Char a
    forall a. Parse a => TextParser a
    parse } }

    -- | 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.
    constructors :: [(String,TextParser a)] -> TextParser a
    constructors :: forall a. [(String, TextParser a)] -> TextParser a
    constructors [(String, TextParser a)]
    cs = [(String, TextParser a)] -> TextParser a
    forall a. [(String, TextParser a)] -> TextParser a
    forall (p :: * -> *) a. Commitment p => [(String, p a)] -> p a
    oneOf' (((String, TextParser a) -> (String, TextParser a))
    -> [(String, TextParser a)] -> [(String, TextParser a)]
    forall a b. (a -> b) -> [a] -> [b]
    map (String, TextParser a) -> (String, TextParser a)
    forall {b}. (String, Parser Char b) -> (String, Parser Char b)
    cons [(String, TextParser a)]
    cs)
        where cons :: (String, Parser Char b) -> (String, Parser Char b)
    cons (String
    name,Parser Char b
    p) =
                   ( String
    name
                   , do { String -> TextParser String
    isWord String
    name
                        ; Parser Char b
    p Parser Char b -> (String -> String) -> Parser Char b
    forall (p :: * -> *) a.
    PolyParse p =>
    p a -> (String -> String) -> p a
    `adjustErrBad` ((String
    "got constructor, but within "
                                            String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    nameString -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    ",\n")String -> String -> String
    forall a. [a] -> [a] -> [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.
    enumeration :: (Show a) => String -> [a] -> TextParser a
    enumeration :: forall a. Show a => String -> [a] -> TextParser a
    enumeration String
    typ [a]
    cs = [Parser Char a] -> Parser Char a
    forall (p :: * -> *) a. PolyParse p => [p a] -> p a
    oneOf ((a -> Parser Char a) -> [a] -> [Parser Char a]
    forall a b. (a -> b) -> [a] -> [b]
    map (\a
    c-> do { String -> TextParser String
    isWord (a -> String
    forall a. Show a => a -> String
    show a
    c); a -> Parser Char a
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return a
    c }) [a]
    cs)
                             Parser Char a -> (String -> String) -> Parser Char a
    forall a. Parser Char a -> (String -> String) -> Parser Char a
    forall (p :: * -> *) a.
    Commitment p =>
    p a -> (String -> String) -> p a
    `adjustErr`
                         (String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++(String
    "\n  expected "String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    typString -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    " value ("String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    eString -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    ")"))
        where e :: String
    e = [String] -> String
    forall (t :: * -> *) a. Foldable t => t [a] -> [a]
    concat (String -> [String] -> [String]
    forall a. a -> [a] -> [a]
    intersperse String
    ", " ((a -> String) -> [a] -> [String]
    forall a b. (a -> b) -> [a] -> [b]
    map a -> String
    forall a. Show a => a -> String
    show ([a] -> [a]
    forall a. HasCallStack => [a] -> [a]
    init [a]
    cs)))
                  String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++ String
    ", or " String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++ a -> String
    forall a. Show a => a -> String
    show ([a] -> a
    forall a. HasCallStack => [a] -> a
    last [a]
    cs)

    ------------------------------------------------------------------------
    -- Instances for all the Standard Prelude types.

    -- Numeric types
    parseSigned :: Real a => TextParser a -> TextParser a
    parseSigned :: forall a. Real a => TextParser a -> TextParser a
    parseSigned TextParser a
    p = do Char -> TextParser a
    '-' <- Parser Char Char
    forall t. Parser t t
    next; commit (fmap negate p)
                    TextParser a -> TextParser a -> TextParser a
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                    do p

    parseInt :: (Integral a) => String ->
                                a -> (Char -> Bool) -> (Char -> Int) ->
                                TextParser a
    parseInt :: forall a.
    Integral a =>
    String -> a -> (Char -> Bool) -> (Char -> Int) -> TextParser a
    parseInt String
    base a
    radix Char -> Bool
    isDigit Char -> Int
    digitToInt =
                     do cs <- Parser Char Char -> TextParser String
    forall (p :: * -> *) a. PolyParse p => p a -> p [a]
    many1 ((Char -> Bool) -> Parser Char Char
    forall t. (t -> Bool) -> Parser t t
    satisfy Char -> Bool
    isDigit)
                        return (foldl1 (\a
    n a
    d-> a
    na -> a -> a
    forall a. Num a => a -> a -> a
    *a
    radixa -> a -> a
    forall a. Num a => a -> a -> a
    +a
    d)
                                       (map (fromIntegral . digitToInt) cs))
                     Parser Char a -> (String -> String) -> Parser Char a
    forall a. Parser Char a -> (String -> String) -> Parser Char a
    forall (p :: * -> *) a.
    Commitment p =>
    p a -> (String -> String) -> p a
    `adjustErr` (String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++(String
    "\nexpected one or more "String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    baseString -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    " digits"))
    parseDec, parseOct, parseHex :: (Integral a) => TextParser a
    parseDec :: forall a. Integral a => TextParser a
    parseDec = String -> a -> (Char -> Bool) -> (Char -> Int) -> TextParser a
    forall a.
    Integral a =>
    String -> a -> (Char -> Bool) -> (Char -> Int) -> TextParser a
    parseInt String
    "decimal" a
    10 Char -> Bool
    Char.isDigit    Char -> Int
    Char.digitToInt
    parseOct :: forall a. Integral a => TextParser a
    parseOct = String -> a -> (Char -> Bool) -> (Char -> Int) -> TextParser a
    forall a.
    Integral a =>
    String -> a -> (Char -> Bool) -> (Char -> Int) -> TextParser a
    parseInt String
    "octal"    a
    8 Char -> Bool
    Char.isOctDigit Char -> Int
    Char.digitToInt
    parseHex :: forall a. Integral a => TextParser a
    parseHex = String -> a -> (Char -> Bool) -> (Char -> Int) -> TextParser a
    forall a.
    Integral a =>
    String -> a -> (Char -> Bool) -> (Char -> Int) -> TextParser a
    parseInt String
    "hex"     a
    16 Char -> Bool
    Char.isHexDigit Char -> Int
    Char.digitToInt

    parseFloat :: (RealFrac a) => TextParser a
    parseFloat :: forall a. RealFrac a => TextParser a
    parseFloat = do ds   <- Parser Char Char -> TextParser String
    forall (p :: * -> *) a. PolyParse p => p a -> p [a]
    many1 ((Char -> Bool) -> Parser Char Char
    forall t. (t -> Bool) -> Parser t t
    satisfy Char -> Bool
    isDigit)
                    frac <- (do '.' <- next
                                many (satisfy isDigit)
                                  `adjustErrBad` (++"expected digit after .")
                             `onFail` return [] )
                    exp  <- exponent `onFail` return 0
                    ( return . fromRational . (* (10^^(exp - length frac)))
                      . (% 1) .  (\ (Right Integer
    x) -> Integer
    x) . fst
                      . runParser parseDec ) (ds++frac)
                 Parser Char a -> Parser Char a -> Parser Char a
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                 do w <- Parser Char Char -> TextParser String
    forall a. Parser Char a -> Parser Char [a]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many ((Char -> Bool) -> Parser Char Char
    forall t. (t -> Bool) -> Parser t t
    satisfy (Bool -> Bool
    not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Char -> Bool
    isSpace))
                    case map toLower w of
                      String
    "nan"      -> a -> Parser Char a
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (a
    0a -> a -> a
    forall a. Fractional a => a -> a -> a
    /a
    0)
                      String
    "infinity" -> a -> Parser Char a
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (a
    1a -> a -> a
    forall a. Fractional a => a -> a -> a
    /a
    0)
                      String
    _          -> String -> Parser Char a
    forall a. String -> Parser Char a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail String
    "expected a floating point number"
      where exponent :: Parser Char Int
    exponent = do Char -> Parser Char Int
    'e' <- (Char -> Char) -> Parser Char Char -> Parser Char Char
    forall a b. (a -> b) -> Parser Char a -> Parser Char b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Char -> Char
    toLower Parser Char Char
    forall t. Parser t t
    next
                          commit (do '+' <- next; parseDec
                                  `onFail`
                                  parseSigned parseDec )

    -- | Parse a Haskell character literal, including the surrounding single quotes.
    parseLitChar' :: TextParser Char
    parseLitChar' :: Parser Char Char
    parseLitChar' = do Char -> Parser Char Char
    '\'' <- Parser Char Char
    forall t. Parser t t
    next Parser Char Char -> (String -> String) -> Parser Char Char
    forall a. Parser Char a -> (String -> String) -> Parser Char a
    forall (p :: * -> *) a.
    Commitment p =>
    p a -> (String -> String) -> p a
    `adjustErr` (String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    "expected a literal char")
                       char <- parseLitChar
                       '\'' <- next `adjustErrBad` (++"literal char has no final '")
                       return char

    -- | Parse a Haskell character literal, excluding the surrounding single quotes.
    parseLitChar :: TextParser Char
    parseLitChar :: Parser Char Char
    parseLitChar = do c <- Parser Char Char
    forall t. Parser t t
    next
                      char <- case c of
                                Char
    '\\' -> Parser Char Char
    forall t. Parser t t
    next Parser Char Char -> (Char -> Parser Char Char) -> Parser Char Char
    forall a b. Parser Char a -> (a -> Parser Char b) -> Parser Char b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Char -> Parser Char Char
    escape
                                Char
    '\'' -> String -> Parser Char Char
    forall a. String -> Parser Char a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail String
    "expected a literal char, got ''"
                                Char
    _    -> Char -> Parser Char Char
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Char
    c
                      return char

      where
        escape :: Char -> Parser Char Char
    escape Char
    'a'  = Char -> Parser Char Char
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Char
    '\a'
        escape Char
    'b'  = Char -> Parser Char Char
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Char
    '\b'
        escape Char
    'f'  = Char -> Parser Char Char
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Char
    '\f'
        escape Char
    'n'  = Char -> Parser Char Char
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Char
    '\n'
        escape Char
    'r'  = Char -> Parser Char Char
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Char
    '\r'
        escape Char
    't'  = Char -> Parser Char Char
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Char
    '\t'
        escape Char
    'v'  = Char -> Parser Char Char
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Char
    '\v'
        escape Char
    '\\' = Char -> Parser Char Char
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Char
    '\\'
        escape Char
    '"'  = Char -> Parser Char Char
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Char
    '"'
        escape Char
    '\'' = Char -> Parser Char Char
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Char
    '\''
        escape Char
    '^'  = do ctrl <- Parser Char Char
    forall t. Parser t t
    next
                         if ctrl >= '@' && ctrl <= '_'
                           then return (chr (ord ctrl - ord '@'))
                           else fail ("literal char ctrl-escape malformed: \\^"
                                       ++[ctrl])
        escape Char
    d | Char -> Bool
    isDigit Char
    d
                    = (Int -> Char) -> Parser Char Int -> Parser Char Char
    forall a b. (a -> b) -> Parser Char a -> Parser Char b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Int -> Char
    chr (Parser Char Int -> Parser Char Char)
    -> Parser Char Int -> Parser Char Char
    forall a b. (a -> b) -> a -> b
    $  (String -> Parser Char ()
    forall t. [t] -> Parser t ()
    reparse [Char
    d] Parser Char () -> Parser Char Int -> Parser Char Int
    forall a b. Parser Char a -> Parser Char b -> Parser Char b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Parser Char Int
    forall a. Integral a => TextParser a
    parseDec)
        escape Char
    'o'  = (Int -> Char) -> Parser Char Int -> Parser Char Char
    forall a b. (a -> b) -> Parser Char a -> Parser Char b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Int -> Char
    chr (Parser Char Int -> Parser Char Char)
    -> Parser Char Int -> Parser Char Char
    forall a b. (a -> b) -> a -> b
    $  Parser Char Int
    forall a. Integral a => TextParser a
    parseOct
        escape Char
    'x'  = (Int -> Char) -> Parser Char Int -> Parser Char Char
    forall a b. (a -> b) -> Parser Char a -> Parser Char b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Int -> Char
    chr (Parser Char Int -> Parser Char Char)
    -> Parser Char Int -> Parser Char Char
    forall a b. (a -> b) -> a -> b
    $  Parser Char Int
    forall a. Integral a => TextParser a
    parseHex
        escape Char
    c | Char -> Bool
    isUpper Char
    c
                    = Char -> Parser Char Char
    mnemonic Char
    c
        escape Char
    c    = String -> Parser Char Char
    forall a. String -> Parser Char a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail (String
    "unrecognised escape sequence in literal char: \\"String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++[Char
    c])

        mnemonic :: Char -> Parser Char Char
    mnemonic Char
    'A' = do Char -> Parser Char Char
    'C' <- Parser Char Char
    forall t. Parser t t
    next; 'K' <- next; return '\ACK'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\ACK'"
        mnemonic Char
    'B' = do Char -> Parser Char Char
    'E' <- Parser Char Char
    forall t. Parser t t
    next; 'L' <- next; return '\BEL'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'S' <- Parser Char Char
    forall t. Parser t t
    next; return '\BS'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\BEL' or '\\BS'"
        mnemonic Char
    'C' = do Char -> Parser Char Char
    'R' <- Parser Char Char
    forall t. Parser t t
    next; return '\CR'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'A' <- Parser Char Char
    forall t. Parser t t
    next; 'N' <- next; return '\CAN'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\CR' or '\\CAN'"
        mnemonic Char
    'D' = do Char -> Parser Char Char
    'E' <- Parser Char Char
    forall t. Parser t t
    next; 'L' <- next; return '\DEL'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'L' <- Parser Char Char
    forall t. Parser t t
    next; 'E' <- next; return '\DLE'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'C' <- Parser Char Char
    forall t. Parser t t
    next; ( do '1' <- next; return '\DC1'
                                         `onFail`
                                         do '2' <- next; return '\DC2'
                                         `onFail`
                                         do '3' <- next; return '\DC3'
                                         `onFail`
                                         do '4' <- next; return '\DC4' )
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\DEL' or '\\DLE' or '\\DC[1..4]'"
        mnemonic Char
    'E' = do Char -> Parser Char Char
    'T' <- Parser Char Char
    forall t. Parser t t
    next; 'X' <- next; return '\ETX'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'O' <- Parser Char Char
    forall t. Parser t t
    next; 'T' <- next; return '\EOT'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'N' <- Parser Char Char
    forall t. Parser t t
    next; 'Q' <- next; return '\ENQ'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'T' <- Parser Char Char
    forall t. Parser t t
    next; 'B' <- next; return '\ETB'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'M' <- Parser Char Char
    forall t. Parser t t
    next; return '\EM'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'S' <- Parser Char Char
    forall t. Parser t t
    next; 'C' <- next; return '\ESC'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "one of '\\ETX' '\\EOT' '\\ENQ' '\\ETB' '\\EM' or '\\ESC'"
        mnemonic Char
    'F' = do Char -> Parser Char Char
    'F' <- Parser Char Char
    forall t. Parser t t
    next; return '\FF'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'S' <- Parser Char Char
    forall t. Parser t t
    next; return '\FS'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\FF' or '\\FS'"
        mnemonic Char
    'G' = do Char -> Parser Char Char
    'S' <- Parser Char Char
    forall t. Parser t t
    next; return '\GS'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\GS'"
        mnemonic Char
    'H' = do Char -> Parser Char Char
    'T' <- Parser Char Char
    forall t. Parser t t
    next; return '\HT'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\HT'"
        mnemonic Char
    'L' = do Char -> Parser Char Char
    'F' <- Parser Char Char
    forall t. Parser t t
    next; return '\LF'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\LF'"
        mnemonic Char
    'N' = do Char -> Parser Char Char
    'U' <- Parser Char Char
    forall t. Parser t t
    next; 'L' <- next; return '\NUL'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'A' <- Parser Char Char
    forall t. Parser t t
    next; 'K' <- next; return '\NAK'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\NUL' or '\\NAK'"
        mnemonic Char
    'R' = do Char -> Parser Char Char
    'S' <- Parser Char Char
    forall t. Parser t t
    next; return '\RS'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\RS'"
        mnemonic Char
    'S' = do Char -> Parser Char Char
    'O' <- Parser Char Char
    forall t. Parser t t
    next; 'H' <- next; return '\SOH'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'O' <- Parser Char Char
    forall t. Parser t t
    next; return '\SO'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'T' <- Parser Char Char
    forall t. Parser t t
    next; 'X' <- next; return '\STX'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'I' <- Parser Char Char
    forall t. Parser t t
    next; return '\SI'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'Y' <- Parser Char Char
    forall t. Parser t t
    next; 'N' <- next; return '\SYN'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'U' <- Parser Char Char
    forall t. Parser t t
    next; 'B' <- next; return '\SUB'
                       Parser Char Char -> Parser Char Char -> Parser Char Char
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                       do Char -> Parser Char Char
    'P' <- Parser Char Char
    forall t. Parser t t
    next; return '\SP'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\SOH' '\\SO' '\\STX' '\\SI' '\\SYN' '\\SUB' or '\\SP'"
        mnemonic Char
    'U' = do Char -> Parser Char Char
    'S' <- Parser Char Char
    forall t. Parser t t
    next; return '\US'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\US'"
        mnemonic Char
    'V' = do Char -> Parser Char Char
    'T' <- Parser Char Char
    forall t. Parser t t
    next; return '\VT'
                       Parser Char Char -> String -> Parser Char Char
    forall {t} {a}. Parser t a -> String -> Parser t a
    `wrap` String
    "'\\VT'"
        wrap :: Parser t a -> String -> Parser t a
    wrap Parser t a
    p String
    s = Parser t a
    p Parser t a -> Parser t a -> Parser t a
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail` String -> Parser t a
    forall a. String -> Parser t a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail (String
    "expected literal char "String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    s)

    -- Basic types
    instance Parse Int where
     -- parse = parseByRead "Int"   -- convert from Integer, deals with minInt
        parse :: Parser Char Int
    parse = (Integer -> Int) -> Parser Char Integer -> Parser Char Int
    forall a b. (a -> b) -> Parser Char a -> Parser Char b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Parser Char Integer -> Parser Char Int)
    -> Parser Char Integer -> Parser Char Int
    forall a b. (a -> b) -> a -> b
    $
                  do Parser Char Char -> TextParser String
    forall a. Parser Char a -> Parser Char [a]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many ((Char -> Bool) -> Parser Char Char
    forall t. (t -> Bool) -> Parser t t
    satisfy Char -> Bool
    isSpace); Parser Char Integer -> Parser Char Integer
    forall a. Real a => TextParser a -> TextParser a
    parseSigned Parser Char Integer
    forall a. Integral a => TextParser a
    parseDec
    instance Parse Integer where
     -- parse = parseByRead "Integer"
        parse :: Parser Char Integer
    parse = do Parser Char Char -> TextParser String
    forall a. Parser Char a -> Parser Char [a]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many ((Char -> Bool) -> Parser Char Char
    forall t. (t -> Bool) -> Parser t t
    satisfy Char -> Bool
    isSpace); Parser Char Integer -> Parser Char Integer
    forall a. Real a => TextParser a -> TextParser a
    parseSigned Parser Char Integer
    forall a. Integral a => TextParser a
    parseDec
    instance Parse Float where
     -- parse = parseByRead "Float"
        parse :: TextParser Float
    parse = do Parser Char Char -> TextParser String
    forall a. Parser Char a -> Parser Char [a]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many ((Char -> Bool) -> Parser Char Char
    forall t. (t -> Bool) -> Parser t t
    satisfy Char -> Bool
    isSpace); TextParser Float -> TextParser Float
    forall a. Real a => TextParser a -> TextParser a
    parseSigned TextParser Float
    forall a. RealFrac a => TextParser a
    parseFloat
    instance Parse Double where
     -- parse = parseByRead "Double"
        parse :: TextParser Double
    parse = do Parser Char Char -> TextParser String
    forall a. Parser Char a -> Parser Char [a]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many ((Char -> Bool) -> Parser Char Char
    forall t. (t -> Bool) -> Parser t t
    satisfy Char -> Bool
    isSpace); TextParser Double -> TextParser Double
    forall a. Real a => TextParser a -> TextParser a
    parseSigned TextParser Double
    forall a. RealFrac a => TextParser a
    parseFloat
    instance Parse Char where
    --  parse = parseByRead "Char"
        parse :: Parser Char Char
    parse = do Parser Char Char -> TextParser String
    forall a. Parser Char a -> Parser Char [a]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many ((Char -> Bool) -> Parser Char Char
    forall t. (t -> Bool) -> Parser t t
    satisfy Char -> Bool
    isSpace); Parser Char Char
    parseLitChar'
     -- parse = do { w <- word; if head w == '\'' then readLitChar (tail w)
     --                                           else fail "expected a char" }
     -- parseList = bracket (isWord "\"") (satisfy (=='"'))
     --                     (many (satisfy (/='"')))
            -- not totally correct for strings...
        parseList :: TextParser String
    parseList = do { w <- TextParser String
    word; if head w == '"' then return (init (tail w))
                                    else fail "not a string" }

    instance Parse Bool where
        parse :: TextParser Bool
    parse = String -> [Bool] -> TextParser Bool
    forall a. Show a => String -> [a] -> TextParser a
    enumeration String
    "Bool" [Bool
    False,Bool
    True]

    instance Parse Ordering where
        parse :: TextParser Ordering
    parse = String -> [Ordering] -> TextParser Ordering
    forall a. Show a => String -> [a] -> TextParser a
    enumeration String
    "Ordering" [Ordering
    LT,Ordering
    EQ,Ordering
    GT]

    -- Structural types
    instance Parse () where
        parse :: Parser Char ()
    parse = (String -> Result String ()) -> Parser Char ()
    forall t a. ([t] -> Result [t] a) -> Parser t a
    P String -> Result String ()
    p
          where p :: String -> Result String ()
    p []       = String -> String -> Result String ()
    forall z a. z -> String -> Result z a
    Failure [] String
    "no input: expected a ()"
                p (Char
    '(':String
    cs) = case (Char -> Bool) -> String -> String
    forall a. (a -> Bool) -> [a] -> [a]
    dropWhile Char -> Bool
    isSpace String
    cs of
                                 (Char
    ')':String
    s) -> String -> () -> Result String ()
    forall z a. z -> a -> Result z a
    Success String
    s ()
                                 String
    _       -> String -> String -> Result String ()
    forall z a. z -> String -> Result z a
    Failure String
    cs String
    "Expected ) after ("
                p (Char
    c:String
    cs) | Char -> Bool
    isSpace Char
    c = String -> Result String ()
    p String
    cs
                         | Bool
    otherwise = String -> String -> Result String ()
    forall z a. z -> String -> Result z a
    Failure (Char
    cChar -> String -> String
    forall a. a -> [a] -> [a]
    :String
    cs) (String
    "Expected a (), got "String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++Char -> String
    forall a. Show a => a -> String
    show Char
    c)

    instance (Parse a, Parse b) => Parse (a,b) where
        parse :: TextParser (a, b)
    parse = do{ String -> TextParser String
    isWord String
    "(" TextParser String -> (String -> String) -> TextParser String
    forall a. Parser Char a -> (String -> String) -> Parser Char a
    forall (p :: * -> *) a.
    Commitment p =>
    p a -> (String -> String) -> p a
    `adjustErr` (String
    "Opening a 2-tuple\n"String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++)
                  ; x <- TextParser a
    forall a. Parse a => TextParser a
    parse TextParser a -> (String -> String) -> TextParser a
    forall a. Parser Char a -> (String -> String) -> Parser Char a
    forall (p :: * -> *) a.
    Commitment p =>
    p a -> (String -> String) -> p a
    `adjustErr` (String
    "In 1st item of a 2-tuple\n"String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++)
                  ; isWord "," `adjustErr` ("Separating a 2-tuple\n"++)
                  ; y <- parse `adjustErr` ("In 2nd item of a 2-tuple\n"++)
                  ; isWord ")" `adjustErr` ("Closing a 2-tuple\n"++)
                  ; return (x,y) }

    instance (Parse a, Parse b, Parse c) => Parse (a,b,c) where
        parse :: TextParser (a, b, c)
    parse = do{ String -> TextParser String
    isWord String
    "(" TextParser String -> (String -> String) -> TextParser String
    forall a. Parser Char a -> (String -> String) -> Parser Char a
    forall (p :: * -> *) a.
    Commitment p =>
    p a -> (String -> String) -> p a
    `adjustErr` (String
    "Opening a 3-tuple\n"String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++)
                  ; x <- TextParser a
    forall a. Parse a => TextParser a
    parse TextParser a -> (String -> String) -> TextParser a
    forall a. Parser Char a -> (String -> String) -> Parser Char a
    forall (p :: * -> *) a.
    Commitment p =>
    p a -> (String -> String) -> p a
    `adjustErr` (String
    "In 1st item of a 3-tuple\n"String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++)
                  ; isWord "," `adjustErr` ("Separating(1) a 3-tuple\n"++)
                  ; y <- parse `adjustErr` ("In 2nd item of a 3-tuple\n"++)
                  ; isWord "," `adjustErr` ("Separating(2) a 3-tuple\n"++)
                  ; z <- parse `adjustErr` ("In 3rd item of a 3-tuple\n"++)
                  ; isWord ")" `adjustErr` ("Closing a 3-tuple\n"++)
                  ; return (x,y,z) }

    instance Parse a => Parse (Maybe a) where
        parsePrec :: Int -> TextParser (Maybe a)
    parsePrec Int
    p =
                TextParser (Maybe a) -> TextParser (Maybe a)
    forall a. TextParser a -> TextParser a
    optionalParens (do { String -> TextParser String
    isWord String
    "Nothing"; Maybe a -> TextParser (Maybe a)
    forall a. a -> Parser Char a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Maybe a
    forall a. Maybe a
    Nothing })
                TextParser (Maybe a)
    -> TextParser (Maybe a) -> TextParser (Maybe a)
    forall t a. Parser t a -> Parser t a -> Parser t a
    `onFail`
                Bool -> TextParser (Maybe a) -> TextParser (Maybe a)
    forall a. Bool -> TextParser a -> TextParser a
    parens (Int
    pInt -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >Int
    9)   (do { String -> TextParser String
    isWord String
    "Just"
                                   ; (a -> Maybe a) -> Parser Char a -> TextParser (Maybe a)
    forall a b. (a -> b) -> Parser Char a -> Parser Char b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap a -> Maybe a
    forall a. a -> Maybe a
    Just (Parser Char a -> TextParser (Maybe a))
    -> Parser Char a -> TextParser (Maybe a)
    forall a b. (a -> b) -> a -> b
    $ Int -> Parser Char a
    forall a. Parse a => Int -> TextParser a
    parsePrec Int
    10
                                         Parser Char a -> (String -> String) -> Parser Char a
    forall (p :: * -> *) a.
    PolyParse p =>
    p a -> (String -> String) -> p a
    `adjustErrBad` (String
    "but within Just, "String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++) })
                TextParser (Maybe a) -> (String -> String) -> TextParser (Maybe a)
    forall a. Parser Char a -> (String -> String) -> Parser Char a
    forall (p :: * -> *) a.
    Commitment p =>
    p a -> (String -> String) -> p a
    `adjustErr` ((String
    "expected a Maybe (Just or Nothing)\n"String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++) (String -> String) -> (String -> String) -> String -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> String -> String
    indent Int
    2)

    instance (Parse a, Parse b) => Parse (Either a b) where
        parsePrec :: Int -> TextParser (Either a b)
    parsePrec Int
    p =
                Bool -> TextParser (Either a b) -> TextParser (Either a b)
    forall a. Bool -> TextParser a -> TextParser a
    parens (Int
    pInt -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >Int
    9) (TextParser (Either a b) -> TextParser (Either a b))
    -> TextParser (Either a b) -> TextParser (Either a b)
    forall a b. (a -> b) -> a -> b
    $
                [(String, TextParser (Either a b))] -> TextParser (Either a b)
    forall a. [(String, TextParser a)] -> TextParser a
    constructors [ (String
    "Left",  do { (a -> Either a b) -> Parser Char a -> TextParser (Either a b)
    forall a b. (a -> b) -> Parser Char a -> Parser Char b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap a -> Either a b
    forall a b. a -> Either a b
    Left  (Parser Char a -> TextParser (Either a b))
    -> Parser Char a -> TextParser (Either a b)
    forall a b. (a -> b) -> a -> b
    $ Int -> Parser Char a
    forall a. Parse a => Int -> TextParser a
    parsePrec Int
    10 } )
                             , (String
    "Right", do { (b -> Either a b) -> Parser Char b -> TextParser (Either a b)
    forall a b. (a -> b) -> Parser Char a -> Parser Char b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap b -> Either a b
    forall a b. b -> Either a b
    Right (Parser Char b -> TextParser (Either a b))
    -> Parser Char b -> TextParser (Either a b)
    forall a b. (a -> b) -> a -> b
    $ Int -> Parser Char b
    forall a. Parse a => Int -> TextParser a
    parsePrec Int
    10 } )
                             ]

    instance Parse a => Parse [a] where
        parse :: TextParser [a]
    parse = TextParser [a]
    forall a. Parse a => TextParser [a]
    parseList

    -- | Simply return the entire remaining input String.
    allAsString :: TextParser String
    allAsString :: TextParser String
    allAsString =  (String -> Result String String) -> TextParser String
    forall t a. ([t] -> Result [t] a) -> Parser t a
    P (\String
    s-> String -> String -> Result String String
    forall z a. z -> a -> Result z a
    Success [] String
    s)

    ------------------------------------------------------------------------