herb-0.1.0.0: Accessible format for structured data serialization
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Herb

Description

Herb provides manipulation and serialization of "Herbrandt universum" data structures. Most users will know these from Prolog. Module provides similar tools as Aeson, together with the typeclasses ToHerb and FromHerb (with Generics-derived defaults) and several utilities for writing custom parsers for complicated stuff.

Only minimal instances are provided in this module. For quick start, utility instances for common types can be obtained from Data.Herb.Instances.

Synopsis

Identifiers

type Ident = Text Source #

Convenience shortcut for the identifier type. Currently an alias for Text (creating Idents is thus quite easy with -XOverloadedStrings or similar tools.

(Using Ident instead of Text may help preserve compatibility with future versions.)

Herb representation

data Herb Source #

Herbrandt universum, consisting of atoms and n-ary structures.

Constructors

Struct Ident [Herb]

Structure with a name and parameters, such as structure(param1, param2).

Atom Ident

A single atom with no parameters, such as 5 or Nothing.

Instances

Instances details
Generic Herb Source # 
Instance details

Defined in Data.Herb

Methods

from :: Herb -> Rep Herb x #

to :: Rep Herb x -> Herb #

Read Herb Source # 
Instance details

Defined in Data.Herb

Show Herb Source # 
Instance details

Defined in Data.Herb

Methods

showsPrec :: Int -> Herb -> ShowS #

show :: Herb -> String #

showList :: [Herb] -> ShowS #

Eq Herb Source # 
Instance details

Defined in Data.Herb

Methods

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

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

Ord Herb Source # 
Instance details

Defined in Data.Herb

Methods

compare :: Herb -> Herb -> Ordering #

(<) :: Herb -> Herb -> Bool #

(<=) :: Herb -> Herb -> Bool #

(>) :: Herb -> Herb -> Bool #

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

max :: Herb -> Herb -> Herb #

min :: Herb -> Herb -> Herb #

FromHerb Herb Source # 
Instance details

Defined in Data.Herb.Instances

ToHerb Herb Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: Herb -> Herb Source #

type Rep Herb Source # 
Instance details

Defined in Data.Herb

formatHerb :: Herb -> Text Source #

Serialize a Herb structure as Text. The output can be parsed by parseHerb.

encodeHerb :: ToHerb a => a -> Text Source #

Shortcut for directly producing a serializable string from the data.

Equivalent to formatHerb . toHerb.

isEscapable :: Char -> Bool Source #

Should this character be escaped in quoted identifiers?

isUnquotedIdentChar :: Char -> Bool Source #

Can the character be used in unquoted identifiers?

escAtom :: Text -> Text Source #

Helper for escaping the atoms for Herb.

>>> escAtom "hello"
"hello"
>>> escAtom "hello!"
"hello!"
>>> escAtom "hello world"
"'hello world'"
>>> escAtom "''\\''"
"'\\'\\'\\\\\\'\\''"

newtype ShowAtom a Source #

Newtype wrapper for deriving simple atom-only instances for ToHerb and FromHerb from existing Show and Read (respectively) for serialization.

>>> toHerb $ ShowAtom [LT .. GT]
Atom "[LT,EQ,GT]"
>>> getShowAtom <$> fromHerb (Atom "[LT,EQ,GT]") :: Either String [Ordering]
Right [LT,EQ,GT]
>>> deriving via ShowAtom Integer instance ToHerb Integer
>>> toHerb (5::Integer)
Atom "5"

Constructors

ShowAtom 

Fields

Instances

Instances details
Read a => Read (ShowAtom a) Source # 
Instance details

Defined in Data.Herb

Show a => Show (ShowAtom a) Source # 
Instance details

Defined in Data.Herb

Methods

showsPrec :: Int -> ShowAtom a -> ShowS #

show :: ShowAtom a -> String #

showList :: [ShowAtom a] -> ShowS #

Eq a => Eq (ShowAtom a) Source # 
Instance details

Defined in Data.Herb

Methods

(==) :: ShowAtom a -> ShowAtom a -> Bool #

(/=) :: ShowAtom a -> ShowAtom a -> Bool #

Ord a => Ord (ShowAtom a) Source # 
Instance details

Defined in Data.Herb

Methods

compare :: ShowAtom a -> ShowAtom a -> Ordering #

(<) :: ShowAtom a -> ShowAtom a -> Bool #

(<=) :: ShowAtom a -> ShowAtom a -> Bool #

(>) :: ShowAtom a -> ShowAtom a -> Bool #

(>=) :: ShowAtom a -> ShowAtom a -> Bool #

max :: ShowAtom a -> ShowAtom a -> ShowAtom a #

min :: ShowAtom a -> ShowAtom a -> ShowAtom a #

Read a => FromHerb (ShowAtom a) Source # 
Instance details

Defined in Data.Herb

Show a => ToHerb (ShowAtom a) Source # 
Instance details

Defined in Data.Herb

Methods

toHerb :: ShowAtom a -> Herb Source #

newtype PlainHerb Source #

Newtype wrapper for parsing out "plain Herb", without actually converting it to another datatype via FromHerb.

>>> getPlainHerb <$> decodeHerb "foo(bar,qux)"
Right (Struct "foo" [Atom "bar",Atom "qux"])

Constructors

PlainHerb 

Fields

Instances

Instances details
Read PlainHerb Source # 
Instance details

Defined in Data.Herb

Show PlainHerb Source # 
Instance details

Defined in Data.Herb

Eq PlainHerb Source # 
Instance details

Defined in Data.Herb

Ord PlainHerb Source # 
Instance details

Defined in Data.Herb

FromHerb PlainHerb Source #

Trivial instance that just returns the incoming Herb wrapped in PlainHerb.

Instance details

Defined in Data.Herb

Serializing to Herb

class ToHerb a where Source #

Class of everything that can be converted to Herb. Can be derived from Generic.

Minimal complete definition

Nothing

Methods

toHerb :: a -> Herb Source #

Convert a to Herb.

default toHerb :: (Generic a, GToHerb a (Rep a) Herb) => a -> Herb Source #

Instances

Instances details
ToHerb Herb Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: Herb -> Herb Source #

ToHerb Ident Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: Ident -> Herb Source #

ToHerb Integer Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: Integer -> Herb Source #

ToHerb () Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: () -> Herb Source #

ToHerb Char Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: Char -> Herb Source #

ToHerb Int Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: Int -> Herb Source #

Show a => ToHerb (ShowAtom a) Source # 
Instance details

Defined in Data.Herb

Methods

toHerb :: ShowAtom a -> Herb Source #

ToHerb a => ToHerb (Maybe a) Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: Maybe a -> Herb Source #

ToHerb a => ToHerb [a] Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: [a] -> Herb Source #

(ToHerb l, ToHerb r) => ToHerb (Either l r) Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: Either l r -> Herb Source #

(ToHerb a, ToHerb b) => ToHerb (a, b) Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: (a, b) -> Herb Source #

(ToHerb a, ToHerb b, ToHerb c) => ToHerb (a, b, c) Source # 
Instance details

Defined in Data.Herb.Instances

Methods

toHerb :: (a, b, c) -> Herb Source #

class ToHerb1 (f :: Type -> Type) where Source #

Class of single-parameter constructors that can be converted to Herb. Can be derived from Generic1.

Minimal complete definition

Nothing

Methods

liftToHerb :: (a -> Herb) -> f a -> Herb Source #

Convert the f a to Herb, provided there is a function to convert the a to Herb.

default liftToHerb :: (Generic1 f, GToHerb a (Rep1 f) Herb) => (a -> Herb) -> f a -> Herb Source #

Instances

Instances details
ToHerb1 Identity Source # 
Instance details

Defined in Data.Herb.Instances

Methods

liftToHerb :: (a -> Herb) -> Identity a -> Herb Source #

(ToHerb1 f, ToHerb1 g) => ToHerb1 (Compose f g) Source # 
Instance details

Defined in Data.Herb.Instances

Methods

liftToHerb :: (a -> Herb) -> Compose f g a -> Herb Source #

Deserializing from Herb

newtype Parser h a Source #

Parser for Herb-like objects of type h into a. Used in FromHerb.

Constructors

Parser 

Fields

Instances

Instances details
Alternative (Parser h) Source # 
Instance details

Defined in Data.Herb

Methods

empty :: Parser h a #

(<|>) :: Parser h a -> Parser h a -> Parser h a #

some :: Parser h a -> Parser h [a] #

many :: Parser h a -> Parser h [a] #

Applicative (Parser h) Source # 
Instance details

Defined in Data.Herb

Methods

pure :: a -> Parser h a #

(<*>) :: Parser h (a -> b) -> Parser h a -> Parser h b #

liftA2 :: (a -> b -> c) -> Parser h a -> Parser h b -> Parser h c #

(*>) :: Parser h a -> Parser h b -> Parser h b #

(<*) :: Parser h a -> Parser h b -> Parser h a #

Functor (Parser h) Source # 
Instance details

Defined in Data.Herb

Methods

fmap :: (a -> b) -> Parser h a -> Parser h b #

(<$) :: a -> Parser h b -> Parser h a #

Monad (Parser h) Source # 
Instance details

Defined in Data.Herb

Methods

(>>=) :: Parser h a -> (a -> Parser h b) -> Parser h b #

(>>) :: Parser h a -> Parser h b -> Parser h b #

return :: a -> Parser h a #

MonadPlus (Parser h) Source # 
Instance details

Defined in Data.Herb

Methods

mzero :: Parser h a #

mplus :: Parser h a -> Parser h a -> Parser h a #

MonadFail (Parser h) Source # 
Instance details

Defined in Data.Herb

Methods

fail :: String -> Parser h a #

Monoid (Parser h a) Source # 
Instance details

Defined in Data.Herb

Methods

mempty :: Parser h a #

mappend :: Parser h a -> Parser h a -> Parser h a #

mconcat :: [Parser h a] -> Parser h a #

Semigroup (Parser h a) Source # 
Instance details

Defined in Data.Herb

Methods

(<>) :: Parser h a -> Parser h a -> Parser h a #

sconcat :: NonEmpty (Parser h a) -> Parser h a #

stimes :: Integral b => b -> Parser h a -> Parser h a #

class FromHerb a where Source #

Class of everything that can be parsed from Herb structure. If available, the method defaults from Generic instance for a. The simplest way to use the class is via fromHerb.

Minimal complete definition

Nothing

Methods

parseHerb Source #

Arguments

:: Parser Herb a

Parse Herb to a type a.

default parseHerb :: (Generic a, GFromHerb (Const () :: Type -> Type) Herb (Rep a)) => Parser Herb a Source #

Instances

Instances details
FromHerb Herb Source # 
Instance details

Defined in Data.Herb.Instances

FromHerb Ident Source # 
Instance details

Defined in Data.Herb.Instances

FromHerb PlainHerb Source #

Trivial instance that just returns the incoming Herb wrapped in PlainHerb.

Instance details

Defined in Data.Herb

FromHerb Integer Source # 
Instance details

Defined in Data.Herb.Instances

FromHerb () Source # 
Instance details

Defined in Data.Herb.Instances

FromHerb Char Source # 
Instance details

Defined in Data.Herb.Instances

FromHerb Int Source # 
Instance details

Defined in Data.Herb.Instances

Read a => FromHerb (ShowAtom a) Source # 
Instance details

Defined in Data.Herb

FromHerb a => FromHerb (Maybe a) Source # 
Instance details

Defined in Data.Herb.Instances

FromHerb a => FromHerb [a] Source # 
Instance details

Defined in Data.Herb.Instances

Methods

parseHerb :: Parser Herb [a] Source #

(FromHerb l, FromHerb r) => FromHerb (Either l r) Source # 
Instance details

Defined in Data.Herb.Instances

(FromHerb a, FromHerb b) => FromHerb (a, b) Source # 
Instance details

Defined in Data.Herb.Instances

Methods

parseHerb :: Parser Herb (a, b) Source #

(FromHerb a, FromHerb b, FromHerb c) => FromHerb (a, b, c) Source # 
Instance details

Defined in Data.Herb.Instances

Methods

parseHerb :: Parser Herb (a, b, c) Source #

class FromHerb1 (f :: Type -> Type) where Source #

Class of single-parameter constructors that can be parsed from Herb. Can be derived from Generic1.

Minimal complete definition

Nothing

Methods

liftParseHerb :: Parser Herb a -> Parser Herb (f a) Source #

Given a parser for the contained type a, parse f a.

default liftParseHerb :: (Generic1 f, GFromHerb (Parser Herb) Herb (Rep1 f)) => Parser Herb a -> Parser Herb (f a) Source #

Instances

Instances details
FromHerb1 Identity Source # 
Instance details

Defined in Data.Herb.Instances

(FromHerb1 f, FromHerb1 g) => FromHerb1 (Compose f g) Source # 
Instance details

Defined in Data.Herb.Instances

fromHerb :: FromHerb a => Herb -> Either String a Source #

Simple front-end for FromHerb's parseHerb.

Deserialization Parser utilities

gets :: (h -> a) -> Parser h a Source #

Return the current parser input as processed by a given function.

get :: Parser h h Source #

Return the current parser input (identical to gets id).

put :: h -> Parser h () Source #

Set the current parser input.

modify :: (h -> h) -> Parser h () Source #

Change the current parser using a function.

local :: l -> Parser l a -> Parser h a Source #

Run a parser with temporarily changed input specified by l.

contexted :: (h -> l) -> Parser l a -> Parser h a Source #

Run a parser with temporarily changed input, created from the current input using the first parameter.

Parsing of Atoms

satisfyAtom :: (Ident -> Bool) -> Parser Herb Ident Source #

Parse out an Atom where the identifier satisfies a predicate.

exactAtom :: Ident -> Parser Herb Ident Source #

Parse out an Atom with an exact identifier.

maybeAtom :: (Ident -> Maybe a) -> Parser Herb a Source #

Parse out an Atom if the given function converts the identifier to Just x, and return the x.

atomShow :: Show a => a -> Herb Source #

Convert a to an Atom using its Show instance.

>>> atomShow (123::Int)
Atom "123"

atomRead :: Read a => Parser Herb a Source #

Parse an Atom to a using its Read instance. Produces a parser failure if the atom's identifier can not be read.

atomRead :: Parser Herb Int

Parsing of Structs

exactStruct :: Ident -> Parser [Herb] a -> Parser Herb a Source #

Parse out a structure identified by an exact name. The second argument is a parser for the structure arguments that works on the argument list; individual arguments can be obtained via popArg and related functions.

exactStruct "pair" $ (,) <$> popArg' <*> popArg' <* endOfArgs
  :: (FromHerb a1, FromHerb a2) => Parser Herb (a1, a2)

parseArg :: Parser Herb a -> Parser [Herb] a Source #

Run a parser on the next argument, popping the argument from the argument list.

popArg :: FromHerb a => (a -> b) -> Parser [Herb] b Source #

Parse the next argument to type a (removing it from the current argument list), pass it through a given function to obtain b, and return it.

popArg' :: FromHerb a => Parser [Herb] a Source #

Like popArg but without the conversion; equivalent to popArg id.

endOfArgs :: Parser [h] () Source #

Succeed if all arguments have been already removed by parseArg, popArg or similar.

singleArg :: FromHerb a => (a -> b) -> Parser [Herb] b Source #

Shortcut for parsing out exactly one argument of a structure, failing if there are more.

parseMaybe :: FromHerb a => Parser Herb (Maybe a)
parseMaybe = Nothing <$ exactAtom "Nothing" <|> exactStruct "Just" (singleArg Just)