syntax-1.0.0.0: Reversible parsing and pretty-printing.

Copyright(c) Paweł Nowak
LicenseMIT
MaintainerPaweł Nowak <pawel834@gmail.com>
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Data.Syntax

Contents

Description

Reversible parsing and pretty-printing.

Synopsis

Syntax.

class (SIArrow syn, IsSequence (Seq syn), Eq (Seq syn), Eq (Element (Seq syn))) => Syntax syn where Source

An abstract syntax description based on semi-isomorphisms.

This class can be implemented by both parsers and printers.

The usual use is to write a polymorphic syntax description and instantiate it both as a parser and a printer. Examples are available in 'syntax-example' and 'syntax-example-json' packages.

Methods of this class try to mimic Data.Attoparsec.Text interface.

Minimal complete definition

anyChar

Associated Types

type Seq syn :: * Source

The sequence type used by this syntax.

Methods

anyChar :: syn () (Element (Seq syn)) Source

Any character.

char :: Element (Seq syn) -> syn () () Source

A specific character.

notChar :: Element (Seq syn) -> syn () (Element (Seq syn)) Source

Any character except the given one.

satisfy :: (Element (Seq syn) -> Bool) -> syn () (Element (Seq syn)) Source

Any character satisfying a predicate.

satisfyWith :: ASemiIso' a (Element (Seq syn)) -> (a -> Bool) -> syn () a Source

Transforms a character using a SemiIso and filters out values not satisfying the predicate.

string :: Seq syn -> syn () () Source

A specific string.

take :: Int -> syn () (Seq syn) Source

A string of length n.

takeWhile :: (Element (Seq syn) -> Bool) -> syn () (Seq syn) Source

Maximal string which elements satisfy a predicate.

takeWhile1 :: (Element (Seq syn) -> Bool) -> syn () (Seq syn) Source

Maximal non-empty string which elements satisfy a predicate.

takeTill :: (Element (Seq syn) -> Bool) -> syn () (Seq syn) Source

Maximal string which elements do not satisfy a predicate.

takeTill1 :: (Element (Seq syn) -> Bool) -> syn () (Seq syn) Source

Maximal non-empty string which elements do not satisfy a predicate.

vecN :: Int -> syn () a -> syn () (Vector a) Source

Constant size vector. The default implementation uses lists, but "syntax-attoparsec" and "syntax-printer" override it with an efficient implementation that works on a vector directly.

vecN n e describes a size n vector with elements e.

Also see vec.

ivecN :: Int -> syn Int (Int, a) -> syn () (Vector a) Source

Constant size vector with index-aware element. The default implementation uses lists, but "syntax-attoparsec" and "syntax-printer" override it with an efficient implementation that works on a vector directly.

ivecN n e describes a size n vector with elements e. Each element gets its index and should output a value and the index unchanged.

Also see ivec.

uvecN :: Unbox a => Int -> syn () a -> syn () (Vector a) Source

Constant size unboxed vector. The default implementation uses lists, but "syntax-attoparsec" and "syntax-printer" override it with an efficient implementation that works on a vector directly.

vecN n e describes a size n vector with elements e.

Also see vec.

uivecN :: Unbox a => Int -> syn Int (Int, a) -> syn () (Vector a) Source

Constant size unboxed vector with index-aware element. The default implementation uses lists, but "syntax-attoparsec" and "syntax-printer" override it with an efficient implementation that works on a vector directly.

ivecN n e describes a size n vector with elements e. Each element gets its index and should output a value and the index unchanged.

Also see ivec.

Instances

Syntax syn => Syntax (Indent syn) 
Syntax syn => Syntax (ReaderCT env syn) 

class Syntax syn => Isolable syn where Source

Execute a computation in an isolated context.

The motivating example: you want to write a function

serializeList :: Syntax syn => syn () a -> syn () [a]

Notice that we cannot just use simany, because the first syn () a could eat the entire sequence (even though we printed more than 1 value!), so we have to insert some kind of separators between the element. But how can we be sure that syn () a will not eat our separators? We can't! Thats why we have to do the parsing in two stages: first extract the sequence between separators, then run the syn () a on this sequence.

Methods

isolate :: syn () b -> syn (Seq syn) b Source

Turns a computation into one executed in an isolated context.

Common isomorphisms.

packed :: IsSequence seq => Iso' seq [Element seq] Source

An isomorphism between a sequence and a list of its elements.