distributors-grammar
Copyright(C) 2026 - Eitan Chatav
LicenseBSD-style (see the file LICENSE)
MaintainerEitan Chatav <eitan.chatav@gmail.com>
Stabilityprovisional
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Control.Lens.Grammar.Pretty

Description

Grammars which lay themselves out, by the layout combinators of Wadler, A prettier printer, printing a Doc of the prettyprinter package, which chooses, by the page's width, between their flat & broken layouts, & parsing every layout they print, with any indentation.

Synopsis

PrettyGrammar

class PrettyGrammar a where Source #

A type's grammar, laid out by Lined & LaidOut combinators.

Methods

prettyGrammar :: (Grammatical Char p, forall x y. (x ~ (), y ~ ()) => Lined (p x y), forall x y. (x ~ (), y ~ ()) => IsString (p x y), forall x y. LaidOut (p x y)) => p a a Source #

prettyG :: PrettyGrammar a => a -> Maybe (Doc ann) Source #

A type's PrettyGrammar, printed to a Doc, if it prints.

layoutG :: PrettyGrammar a => LayoutStyle -> a -> Maybe String Source #

A type's PrettyGrammar, printed in a LayoutStyle, if it prints.

data LayoutStyle Source #

A layout style of a Doc, as prettyprinter lays them out.

Constructors

PrettyLayout LayoutOptions

by layoutPretty, fitting the rest of a line

SmartLayout LayoutOptions

by layoutSmart, fitting more than the rest of a line

CompactLayout

by layoutCompact, broken & unindented

Layout

class Lined p where Source #

Grammars with a hard line break.

Methods

hardlineP :: p Source #

A newline, which no flat layout removes.

Instances

Instances details
(Ord rule, Lined rule) => Lined (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

hardlineP :: Bnf rule Source #

Lined (RegEx Char) Source #

A newline, then any indentation.

Instance details

Defined in Control.Lens.Grammar.Pretty

Lined (Doc ann) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

hardlineP :: Doc ann Source #

Lined k => Lined (Grammor k a b) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

hardlineP :: Grammor k a b Source #

(Lined w, Applicative f) => Lined (Prettor w f () ()) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

hardlineP :: Prettor w f () () Source #

(Char ~ Item s, IsList s, Cons s s Char Char, Snoc s s Char Char, Filterable m, Alternative m, Monad m) => Lined (Parsor s m () ()) Source #

A newline, then any indentation, unparsing none.

Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

hardlineP :: Parsor s m () () Source #

class LaidOut p where Source #

Grammars laid out by Wadler-Leijen layout combinators, choosing between flat & broken layouts.

prop> nestP 0 = id
prop> nestP i . nestP j = nestP (i + j)

Methods

flatAltP :: p -> p -> p Source #

A broken layout, by default, & a flat one, where it's grouped flat.

nestP :: Int -> p -> p Source #

Indented further after its line breaks.

groupP :: p -> p Source #

Flat, where it fits.

alignP :: p -> p Source #

Indented to the current column after its line breaks.

Instances

Instances details
(Ord rule, LaidOut rule) => LaidOut (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

flatAltP :: Bnf rule -> Bnf rule -> Bnf rule Source #

nestP :: Int -> Bnf rule -> Bnf rule Source #

groupP :: Bnf rule -> Bnf rule Source #

alignP :: Bnf rule -> Bnf rule Source #

Categorized token => LaidOut (RegEx token) Source #

Either layout, any indentation.

Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

flatAltP :: RegEx token -> RegEx token -> RegEx token Source #

nestP :: Int -> RegEx token -> RegEx token Source #

groupP :: RegEx token -> RegEx token Source #

alignP :: RegEx token -> RegEx token Source #

LaidOut (Doc ann) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

flatAltP :: Doc ann -> Doc ann -> Doc ann Source #

nestP :: Int -> Doc ann -> Doc ann Source #

groupP :: Doc ann -> Doc ann Source #

alignP :: Doc ann -> Doc ann Source #

LaidOut k => LaidOut (Grammor k a b) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

flatAltP :: Grammor k a b -> Grammor k a b -> Grammor k a b Source #

nestP :: Int -> Grammor k a b -> Grammor k a b Source #

groupP :: Grammor k a b -> Grammor k a b Source #

alignP :: Grammor k a b -> Grammor k a b Source #

(LaidOut w, Applicative f) => LaidOut (Prettor w f a b) Source #

Both layouts print a value, the layout of their prints chosen.

Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

flatAltP :: Prettor w f a b -> Prettor w f a b -> Prettor w f a b Source #

nestP :: Int -> Prettor w f a b -> Prettor w f a b Source #

groupP :: Prettor w f a b -> Prettor w f a b Source #

alignP :: Prettor w f a b -> Prettor w f a b Source #

(Alternative m, Monad m) => LaidOut (Parsor s m a b) Source #

Either layout, any indentation.

Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

flatAltP :: Parsor s m a b -> Parsor s m a b -> Parsor s m a b Source #

nestP :: Int -> Parsor s m a b -> Parsor s m a b Source #

groupP :: Parsor s m a b -> Parsor s m a b Source #

alignP :: Parsor s m a b -> Parsor s m a b Source #

lineP :: (Lined p, LaidOut p, IsString p) => p Source #

A line break, or a space, where it's grouped flat.

lineP' :: (Lined p, LaidOut p, IsString p) => p Source #

A line break, or nothing, where it's grouped flat.

softlineP :: (Lined p, LaidOut p, IsString p) => p Source #

A space, or a line break, where it doesn't fit.

Prettor

newtype Prettor w (f :: Type -> Type) a b Source #

Prettor is a printer Profunctor into a Monoid, like a Doc, whose Lined & LaidOut methods it lifts.

Constructors

Prettor 

Fields

Instances

Instances details
(IsString w, Lined w, Monoid w, Applicative f) => TerminalSymbol Char (Prettor w f () ()) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

terminal :: [Char] -> Prettor w f () () Source #

(IsString w, Lined w, Monoid w, Alternative f) => TokenAlgebra Char (Prettor w f Char Char) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

(IsString w, Lined w, Monoid w, Alternative f) => Tokenized Char (Prettor w f Char Char) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

(Monoid w, Alternative f) => Alternator (Prettor w f) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

someP :: Prettor w f a b -> Prettor w f [a] [b] #

optionP :: APrism a b () () -> Prettor w f a b -> Prettor w f a b #

manyTillP :: Prettor w f a b -> Prettor w f [a] [b] -> Prettor w f [a] [b] #

quantP :: Int -> Maybe Int -> Prettor w f a b -> Prettor w f [a] [b] #

(Monoid w, Applicative f) => Distributor (Prettor w f) Source #

manyP & optionalP print their elements in turn.

Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

optionalP :: Prettor w f a b -> Prettor w f (Maybe a) (Maybe b) #

manyP :: Prettor w f a b -> Prettor w f [a] [b] #

(Monoid w, Alternative f) => CocartesianChoice (Prettor w f) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

alternate :: Either (Prettor w f a b) (Prettor w f c d) -> Prettor w f (Either a c) (Either b d) #

Functor f => CocartesianMonoidal (Prettor w f) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

zeroP :: Prettor w f Void Void #

(>+<) :: Prettor w f a b -> Prettor w f c d -> Prettor w f (Either a c) (Either b d) #

(Monoid w, Alternative f) => Choice (Prettor w f) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

left' :: Prettor w f a b -> Prettor w f (Either a c) (Either b c) #

right' :: Prettor w f a b -> Prettor w f (Either c a) (Either c b) #

Functor f => Profunctor (Prettor w f) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

dimap :: (a -> b) -> (c -> d) -> Prettor w f b c -> Prettor w f a d #

lmap :: (a -> b) -> Prettor w f b c -> Prettor w f a c #

rmap :: (b -> c) -> Prettor w f a b -> Prettor w f a c #

(#.) :: forall a b c q. Coercible c b => q b c -> Prettor w f a b -> Prettor w f a c #

(.#) :: forall a b c q. Coercible b a => Prettor w f b c -> q a b -> Prettor w f a c #

(Monoid w, Alternative f) => Alternative (Prettor w f a) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

empty :: Prettor w f a a0 #

(<|>) :: Prettor w f a a0 -> Prettor w f a a0 -> Prettor w f a a0 #

some :: Prettor w f a a0 -> Prettor w f a [a0] #

many :: Prettor w f a a0 -> Prettor w f a [a0] #

(Monoid w, Applicative f) => Applicative (Prettor w f a) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

pure :: a0 -> Prettor w f a a0 #

(<*>) :: Prettor w f a (a0 -> b) -> Prettor w f a a0 -> Prettor w f a b #

liftA2 :: (a0 -> b -> c) -> Prettor w f a a0 -> Prettor w f a b -> Prettor w f a c #

(*>) :: Prettor w f a a0 -> Prettor w f a b -> Prettor w f a b #

(<*) :: Prettor w f a a0 -> Prettor w f a b -> Prettor w f a a0 #

Functor f => Functor (Prettor w f a) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

fmap :: (a0 -> b) -> Prettor w f a a0 -> Prettor w f a b #

(<$) :: a0 -> Prettor w f a b -> Prettor w f a a0 #

(Monoid w, Alternative f) => DisjunctiveMonoid (Prettor w f a b) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

falseP :: Prettor w f a b #

(>|<) :: Prettor w f a b -> Prettor w f a b -> Prettor w f a b #

BackusNaurForm (Prettor w f a b) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

ruleRec :: String -> (Prettor w f a b -> Prettor w f a b) -> Prettor w f a b Source #

Ruled (Prettor w f a b) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

rule :: String -> Prettor w f a b -> Prettor w f a b Source #

Tried (Prettor w f a b) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

try :: Prettor w f a b -> Prettor w f a b Source #

(LaidOut w, Applicative f) => LaidOut (Prettor w f a b) Source #

Both layouts print a value, the layout of their prints chosen.

Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

flatAltP :: Prettor w f a b -> Prettor w f a b -> Prettor w f a b Source #

nestP :: Int -> Prettor w f a b -> Prettor w f a b Source #

groupP :: Prettor w f a b -> Prettor w f a b Source #

alignP :: Prettor w f a b -> Prettor w f a b Source #

(Lined w, Applicative f) => Lined (Prettor w f () ()) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

hardlineP :: Prettor w f () () Source #

(IsString w, Lined w, Monoid w, Applicative f) => IsString (Prettor w f () ()) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

fromString :: String -> Prettor w f () () #