Movatterモバイル変換


[0]ホーム

URL:


base-4.4.0.0: Basic libraries

Portabilitynon-portable (uses Text.ParserCombinators.ReadP)
Stabilityprovisional
Maintainerlibraries@haskell.org

Text.Read

Contents

Description

Converting strings to values.

TheText.Read library is the canonical library to import forRead-class facilities. For GHC only, it offers an extended and much improvedRead class, which constitutes a proposed alternative to the Haskell 98Read. In particular, writing parsers is easier, and the parsers are much more efficient.

Synopsis

TheRead class

classRead awhereSource

Parsing ofStrings, producing values.

Minimal complete definition:readsPrec (or, for GHC only,readPrec)

Derived instances ofRead make the following assumptions, which derived instances ofText.Show.Show obey:

  • If the constructor is defined to be an infix operator, then the derivedRead instance will parse only infix applications of the constructor (not the prefix form).
  • Associativity is not used to reduce the occurrence of parentheses, although precedence may be.
  • If the constructor is defined using record syntax, the derivedRead will parse only the record-syntax form, and furthermore, the fields must be given in the same order as the original declaration.
  • The derivedRead instance allows arbitrary Haskell whitespace between tokens of the input string. Extra parentheses are also allowed.

For example, given the declarations

 infixr 5 :^: data Tree a =  Leaf a  |  Tree a :^: Tree a

the derived instance ofRead in Haskell 98 is equivalent to

 instance (Read a) => Read (Tree a) where         readsPrec d r =  readParen (d > app_prec)                          (\r -> [(Leaf m,t) |                                  ("Leaf",s) <- lex r,                                  (m,t) <- readsPrec (app_prec+1) s]) r                       ++ readParen (d > up_prec)                          (\r -> [(u:^:v,w) |                                  (u,s) <- readsPrec (up_prec+1) r,                                  (":^:",t) <- lex s,                                  (v,w) <- readsPrec (up_prec+1) t]) r           where app_prec = 10                 up_prec = 5

Note that right-associativity of:^: is unused.

The derived instance in GHC is equivalent to

 instance (Read a) => Read (Tree a) where         readPrec = parens $ (prec app_prec $ do                                  Ident "Leaf" <- lexP                                  m <- step readPrec                                  return (Leaf m))                      +++ (prec up_prec $ do                                  u <- step readPrec                                  Symbol ":^:" <- lexP                                  v <- step readPrec                                  return (u :^: v))           where app_prec = 10                 up_prec = 5         readListPrec = readListPrecDefault

Methods

readsPrecSource

Arguments

::Int

the operator precedence of the enclosing context (a number from0 to11). Function application has precedence10.

->ReadS a 

attempts to parse a value from the front of the string, returning a list of (parsed value, remaining string) pairs. If there is no successful parse, the returned list is empty.

Derived instances ofRead andText.Show.Show satisfy the following:

  • (x,"") is an element of(readsPrec d (Text.Show.showsPrec d x "")).

That is,readsPrec parses the string produced byText.Show.showsPrec, and delivers the value thatText.Show.showsPrec started with.

readList ::ReadS [a]Source

The methodreadList is provided to allow the programmer to give a specialised way of parsing lists of values. For example, this is used by the predefinedRead instance of theChar type, where values of typeString should be are expected to use double quotes, rather than square brackets.

readPrec ::ReadPrec aSource

Proposed replacement forreadsPrec using new-style parsers (GHC only).

readListPrec ::ReadPrec [a]Source

Proposed replacement forreadList using new-style parsers (GHC only). The default definition usesreadList. Instances that definereadPrec should also definereadListPrec asreadListPrecDefault.

Instances

ReadBool 
ReadChar 
ReadDouble 
ReadFloat 
ReadInt 
ReadInt8 
ReadInt16 
ReadInt32 
ReadInt64 
ReadInteger 
ReadOrdering 
ReadWord 
ReadWord8 
ReadWord16 
ReadWord32 
ReadWord64 
Read () 
ReadArity 
ReadFixity 
ReadAssociativity 
ReadLexeme 
ReadIOMode 
ReadSeekMode 
ReadCUIntMax 
ReadCIntMax 
ReadCUIntPtr 
ReadCIntPtr 
ReadCSUSeconds 
ReadCUSeconds 
ReadCTime 
ReadCClock 
ReadCSigAtomic 
ReadCWchar 
ReadCSize 
ReadCPtrdiff 
ReadCDouble 
ReadCFloat 
ReadCULLong 
ReadCLLong 
ReadCULong 
ReadCLong 
ReadCUInt 
ReadCInt 
ReadCUShort 
ReadCShort 
ReadCUChar 
ReadCSChar 
ReadCChar 
ReadAny 
ReadAll 
ReadGeneralCategory 
ReadIntPtr 
ReadWordPtr 
ReadNewlineMode 
ReadNewline 
ReadBufferMode 
ReadExitCode 
ReadFd 
ReadCRLim 
ReadCTcflag 
ReadCSpeed 
ReadCCc 
ReadCUid 
ReadCNlink 
ReadCGid 
ReadCSsize 
ReadCPid 
ReadCOff 
ReadCMode 
ReadCIno 
ReadCDev 
ReadVersion 
Read a =>Read [a] 
(Integral a,Read a) =>Read (Ratio a) 
Read a =>Read (Maybe a) 
Read a =>Read (Last a) 
Read a =>Read (First a) 
Read a =>Read (Product a) 
Read a =>Read (Sum a) 
Read a =>Read (Dual a) 
Read a =>Read (Complex a) 
HasResolution a =>Read (Fixed a) 
(Read a,Read b) =>Read (Either a b) 
(Read a,Read b) =>Read (a, b) 
(Ix a,Read a,Read b) =>Read (Array a b) 
(Read a,Read b,Read c) =>Read (a, b, c) 
(Read a,Read b,Read c,Read d) =>Read (a, b, c, d) 
(Read a,Read b,Read c,Read d,Read e) =>Read (a, b, c, d, e) 
(Read a,Read b,Read c,Read d,Read e,Read f) =>Read (a, b, c, d, e, f) 
(Read a,Read b,Read c,Read d,Read e,Read f,Read g) =>Read (a, b, c, d, e, f, g) 
(Read a,Read b,Read c,Read d,Read e,Read f,Read g,Read h) =>Read (a, b, c, d, e, f, g, h) 
(Read a,Read b,Read c,Read d,Read e,Read f,Read g,Read h,Read i) =>Read (a, b, c, d, e, f, g, h, i) 
(Read a,Read b,Read c,Read d,Read e,Read f,Read g,Read h,Read i,Read j) =>Read (a, b, c, d, e, f, g, h, i, j) 
(Read a,Read b,Read c,Read d,Read e,Read f,Read g,Read h,Read i,Read j,Read k) =>Read (a, b, c, d, e, f, g, h, i, j, k) 
(Read a,Read b,Read c,Read d,Read e,Read f,Read g,Read h,Read i,Read j,Read k,Read l) =>Read (a, b, c, d, e, f, g, h, i, j, k, l) 
(Read a,Read b,Read c,Read d,Read e,Read f,Read g,Read h,Read i,Read j,Read k,Read l,Read m) =>Read (a, b, c, d, e, f, g, h, i, j, k, l, m) 
(Read a,Read b,Read c,Read d,Read e,Read f,Read g,Read h,Read i,Read j,Read k,Read l,Read m,Read n) =>Read (a, b, c, d, e, f, g, h, i, j, k, l, m, n) 
(Read a,Read b,Read c,Read d,Read e,Read f,Read g,Read h,Read i,Read j,Read k,Read l,Read m,Read n,Read o) =>Read (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) 

typeReadS a =String -> [(a,String)]Source

A parser for a typea, represented as a function that takes aString and returns a list of possible parses as(a,String) pairs.

Note that this kind of backtracking parser is very inefficient; reading a large structure may be quite slow (cfReadP).

Haskell 98 functions

reads ::Read a =>ReadS aSource

equivalent toreadsPrec with a precedence of 0.

read ::Read a =>String -> aSource

Theread function reads input from a string, which must be completely consumed by the input process.

readParen ::Bool ->ReadS a ->ReadS aSource

readParenTrue p parses whatp parses, but surrounded with parentheses.

readParenFalse p parses whatp parses, but optionally surrounded with parentheses.

lex ::ReadSStringSource

Thelex function reads a single lexeme from the input, discarding initial white space, and returning the characters that constitute the lexeme. If the input string contains only white space,lex returns a single successful `lexeme' consisting of the empty string. (Thuslex "" = [("","")].) If there is no legal lexeme at the beginning of the input string,lex fails (i.e. returns[]).

This lexer is not completely faithful to the Haskell lexical syntax in the following respects:

  • Qualified names are not handled properly
  • Octal and hexadecimal numerics are not recognized as a single token
  • Comments are not treated properly

New parsing functions

moduleText.ParserCombinators.ReadPrec

dataLexemeSource

Haskell lexemes.

Constructors

CharChar

Character literal

StringString

String literal, with escapes interpreted

PuncString

Punctuation or reserved symbol, e.g.(,::

IdentString

Haskell identifier, e.g.foo,Baz

SymbolString

Haskell symbol, e.g.>>,:%

IntInteger

Integer literal

RatRational

Floating point literal

EOF 

Instances

EqLexeme 
ReadLexeme 
ShowLexeme 

lexP ::ReadPrecLexemeSource

Parse a single lexeme

parens ::ReadPrec a ->ReadPrec aSource

(parens p) parses "P", "(P0)", "((P0))", etc, wherep parses "P" in the current precedence context and parses "P0" in precedence context zero

readListDefault ::Read a =>ReadS [a]Source

A possible replacement definition for thereadList method (GHC only). This is only needed for GHC, and even then only forRead instances wherereadListPrec isn't defined asreadListPrecDefault.

readListPrecDefault ::Read a =>ReadPrec [a]Source

A possible replacement definition for thereadListPrec method, defined usingreadPrec (GHC only).

Produced byHaddock version 2.9.2


[8]ページ先頭

©2009-2025 Movatter.jp