sexpresso-1.0.0.2: A flexible library for parsing and printing S-expression

Copyright© 2019 Vincent Archambault
License0BSD
MaintainerVincent Archambault <archambault.v@gmail.com>
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Data.SExpresso.Language.SchemeR5RS

Contents

Description

Module for parsing the Scheme R5RS language.

Scheme R5RS s-expressions are parsed as SExpr SExprType SchemeToken. Such s-expressions can be converted into a Scheme R5RS datum (see Datum) by the function sexpr2Datum.

Synopsis

SchemeToken and Datum related data types and functions

data SExprType Source #

Scheme R5RS defines two types of s-expressions. Standard list beginning with '(' and vector beginning with '#('. The SExprType data type indicates which one was parsed.

Constructors

STList

A standard list

STVector

A vector

data SchemeToken Source #

The SchemeToken data type defines the atoms of an Scheme R5RS s-expression. An SExpr SExprType SchemeToken object containning the atoms TQuote, TQuasiquote, TComma, TCommaAt and TDot need futher processing in order to get what the R5RS report calls a datum. See also Datum.

Constructors

TBoolean Bool

A boolean.

TNumber SchemeNumber

A number. See SchemeNumber.

TChar Char

A unicode character.

TString Text

A string.

TIdentifier Text

A valid R5RS identifier.

TQuote

The quote (') symbol.

TQuasiquote

The quasiquote (`) symbol.

TComma

The comma (,) symbol.

TCommaAt

The comma at (,@) symbol.

TDot

The dot (.) symbol.

sexpr :: forall e s m. (MonadParsec e s m, Token s ~ Char) => SExprParser m SExprType SchemeToken Source #

The sexpr defines a SExprParser to parse a Scheme R5RS s-expression as an SExpr SExprType SchemeToken. If you also want source position see the withLocation function.

Space is optional before and after the following tokens:

data Datum Source #

The Datum data type implements the Scheme R5RS definition of a Datum. See also sexpr2Datum.

Instances
Eq Datum Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

Methods

(==) :: Datum -> Datum -> Bool #

(/=) :: Datum -> Datum -> Bool #

Show Datum Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

Methods

showsPrec :: Int -> Datum -> ShowS #

show :: Datum -> String #

showList :: [Datum] -> ShowS #

sexpr2Datum :: [SExpr SExprType SchemeToken] -> Either String [Datum] Source #

The sexpr2Datum function takes a list of SchemeToken and returns a list of Datum. In case of failure it will report an error, hence the Either data type in the signature.

As defined in the Scheme R5RS report, the TQuote, TQuasiquote, TComma, TCommaAt and TDot tokens must be followed by another token.

Scheme R5RS whitespace parsers

whitespace :: (MonadParsec e s m, Token s ~ Char) => m () Source #

The whitespace parser parses one space, tab or end of line (\n and \r\n).

comment :: (MonadParsec e s m, Token s ~ Char) => m () Source #

The comment parser parses a semi-colon (;) character and everything until the end of line included.

interTokenSpace :: (MonadParsec e s m, Token s ~ Char) => m () Source #

The interTokenSpace parser parses zero or more whitespace or comment.

interTokenSpace1 :: (MonadParsec e s m, Token s ~ Char) => m () Source #

The interTokenSpace1 parser parses one or more whitespace or comment.

Individual parser for each of the constructors of SchemeToken

identifier :: forall e s m. (MonadParsec e s m, Token s ~ Char) => m Text Source #

The identifier parser parses a Scheme R5RS identifier.

boolean :: forall e s m. (MonadParsec e s m, Token s ~ Char) => m Bool Source #

The boolean parser parses a Scheme R5RS boolean (#t or #f).

character :: forall e s m. (MonadParsec e s m, Token s ~ Char) => m Char Source #

The character parser parses a Scheme R5RS character.

stringParser :: forall e s m. (MonadParsec e s m, Token s ~ Char) => m Text Source #

The stringParser parser parses a Scheme R5RS character.

quote :: (MonadParsec e s m, Token s ~ Char) => m Char Source #

The quote parser parses a quote character (').

quasiquote :: (MonadParsec e s m, Token s ~ Char) => m Char Source #

The quasiquote parser parses a quasiquote character (`).

comma :: (MonadParsec e s m, Token s ~ Char) => m Char Source #

The comma parser parses a comma (,).

commaAt :: forall e s m. (MonadParsec e s m, Token s ~ Char) => m Text Source #

The commaAt parser parses a comma followed by @ (,@).

dot :: (MonadParsec e s m, Token s ~ Char) => m Char Source #

The dot parser parses a single dot character (.).

Scheme Number

Scheme R5RS numbers are quite exotic. They can have exactness prefix, radix prefix and the pound sign (#) can replace a digit. On top of that, you can define integer, rational, decimal and complex numbers of arbitrary precision. Decimal numbers can also have a suffix indicating the machine precision.

Since Haskell does not have native types to express this complexity, this module defines the SchemeNumber data type to encode the parsed number. User of this module can then convert a SchemeNumber object to a more appropriate data type according to their needs.

data SchemeNumber Source #

A Scheme R5RS number is an exact or inexact complex number.

data Exactness Source #

A Scheme R5RS number is either exact or inexact. The paragraph 6.4.2 from the R5RS report should clarify the meaning of exact and inexact :

"""A numerical constant may be specified to be either exact or inexact by a prefix. The prefixes are #e for exact, and #i for inexact. An exactness prefix may appear before or after any radix prefix that is used. If the written representation of a number has no exactness prefix, the constant may be either inexact or exact. It is inexact if it contains a decimal point, an exponent, or a “#” character in the place of a digit, otherwise it is exact."""

Constructors

Exact 
Inexact 

data Complex Source #

The Complex data type represents a Scheme R5RS complex number.

Constructors

CReal SReal

A real number.

CAngle SReal SReal

A complex number in angular notation.

CAbsolute SReal SReal

A complex number in absolute notation.

Instances
Eq Complex Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

Methods

(==) :: Complex -> Complex -> Bool #

(/=) :: Complex -> Complex -> Bool #

Show Complex Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

data SReal Source #

The SReal data type represents a Scheme R5RS real number.

Constructors

SInteger Sign UInteger

A signed integer.

SRational Sign UInteger UInteger

A signed rational. The first number is the numerator and the second one the denominator.

SDecimal Sign UInteger UInteger (Maybe Suffix)

A signed decimal number. The first number appears before the dot, the second one after the dot.

Instances
Eq SReal Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

Methods

(==) :: SReal -> SReal -> Bool #

(/=) :: SReal -> SReal -> Bool #

Show SReal Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

Methods

showsPrec :: Int -> SReal -> ShowS #

show :: SReal -> String #

showList :: [SReal] -> ShowS #

data Sign Source #

The Sign datatype indicates if a number is positive (Plus) or negative (Minus)

Constructors

Plus 
Minus 
Instances
Eq Sign Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

Methods

(==) :: Sign -> Sign -> Bool #

(/=) :: Sign -> Sign -> Bool #

Show Sign Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

Methods

showsPrec :: Int -> Sign -> ShowS #

show :: Sign -> String #

showList :: [Sign] -> ShowS #

data UInteger Source #

A Scheme R5RS unsigned integer can be written in three ways.

  • With digits only
  • With digits and # signs
  • With only # signs in some special context.

Constructors

UInteger Integer

Integer made only of digits

UIntPounds Integer Pounds

Integer made of digits and #. The first argument is the number that was parsed and the second the number of # signs. For example, 123## is represented as UIntPounds 123 2. Do not take the first argument as a good approximation of the number. It needs to be shifted by the number of pounds.

UPounds Pounds

Integer made only of #. It can only appear as the third argument in numbers of the form SDecimal _ _ _ _.

Instances
Eq UInteger Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

Show UInteger Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

type Pounds = Integer Source #

A Scheme R5RS number can have many # signs at the end. This type alias indicates the number of # signs parsed.

data Precision Source #

Scheme R5RS defines 5 types of machine precision for a decimal number. The machine precision is specified in the suffix (see Suffix).

Constructors

PDefault

Suffix starting with e.

PShort

Suffix starting with s.

PSingle

Suffix starting with f.

PDouble

Suffix starting with d.

PLong

Suffix starting with l.

data Suffix Source #

The Suffix data type represents the suffix for a Scheme R5RS decimal number. It is a based 10 exponent.

Instances
Eq Suffix Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

Methods

(==) :: Suffix -> Suffix -> Bool #

(/=) :: Suffix -> Suffix -> Bool #

Show Suffix Source # 
Instance details

Defined in Data.SExpresso.Language.SchemeR5RS

number :: (MonadParsec e s m, Token s ~ Char) => m SchemeNumber Source #

The number parser parses a Scheme R5RS number.