Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
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
- type Ident = Text
- unpackIdent :: Ident -> String
- packIdent :: String -> Ident
- data Herb
- formatHerb :: Herb -> Text
- encodeHerb :: ToHerb a => a -> Text
- isEscapable :: Char -> Bool
- isUnquotedIdentChar :: Char -> Bool
- escAtom :: Text -> Text
- newtype ShowAtom a = ShowAtom {
- getShowAtom :: a
- newtype PlainHerb = PlainHerb {
- getPlainHerb :: Herb
- class ToHerb a where
- class ToHerb1 (f :: Type -> Type) where
- liftToHerb :: (a -> Herb) -> f a -> Herb
- newtype Parser h a = Parser {}
- class FromHerb a where
- class FromHerb1 (f :: Type -> Type) where
- liftParseHerb :: Parser Herb a -> Parser Herb (f a)
- fromHerb :: FromHerb a => Herb -> Either String a
- gets :: (h -> a) -> Parser h a
- get :: Parser h h
- put :: h -> Parser h ()
- modify :: (h -> h) -> Parser h ()
- local :: l -> Parser l a -> Parser h a
- contexted :: (h -> l) -> Parser l a -> Parser h a
- satisfyAtom :: (Ident -> Bool) -> Parser Herb Ident
- exactAtom :: Ident -> Parser Herb Ident
- maybeAtom :: (Ident -> Maybe a) -> Parser Herb a
- atomShow :: Show a => a -> Herb
- atomRead :: Read a => Parser Herb a
- exactStruct :: Ident -> Parser [Herb] a -> Parser Herb a
- parseArg :: Parser Herb a -> Parser [Herb] a
- popArg :: FromHerb a => (a -> b) -> Parser [Herb] b
- popArg' :: FromHerb a => Parser [Herb] a
- endOfArgs :: Parser [h] ()
- singleArg :: FromHerb a => (a -> b) -> Parser [Herb] b
Identifiers
Herb representation
Herbrandt universum, consisting of atoms and n-ary structures.
Struct Ident [Herb] | Structure with a name and parameters, such as |
Atom Ident | A single atom with no parameters, such as |
Instances
formatHerb :: Herb -> Text Source #
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 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"
ShowAtom | |
|
Instances
Read a => Read (ShowAtom a) Source # | |
Show a => Show (ShowAtom a) Source # | |
Eq a => Eq (ShowAtom a) Source # | |
Ord a => Ord (ShowAtom a) Source # | |
Read a => FromHerb (ShowAtom a) Source # | |
Show a => ToHerb (ShowAtom a) 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"])
Serializing to Herb
Nothing
Convert a
to Herb
.
Instances
ToHerb Herb Source # | |
ToHerb Ident Source # | |
ToHerb Integer Source # | |
ToHerb () Source # | |
Defined in Data.Herb.Instances | |
ToHerb Char Source # | |
ToHerb Int Source # | |
Show a => ToHerb (ShowAtom a) Source # | |
ToHerb a => ToHerb (Maybe a) Source # | |
ToHerb a => ToHerb [a] Source # | |
Defined in Data.Herb.Instances | |
(ToHerb l, ToHerb r) => ToHerb (Either l r) Source # | |
(ToHerb a, ToHerb b) => ToHerb (a, b) Source # | |
Defined in Data.Herb.Instances | |
(ToHerb a, ToHerb b, ToHerb c) => ToHerb (a, b, c) Source # | |
Defined in Data.Herb.Instances |
class ToHerb1 (f :: Type -> Type) where Source #
Nothing
Deserializing from Herb
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
.
Nothing
Instances
FromHerb Herb Source # | |
FromHerb Ident Source # | |
FromHerb PlainHerb Source # | Trivial instance that just returns the incoming |
FromHerb Integer Source # | |
FromHerb () Source # | |
FromHerb Char Source # | |
FromHerb Int Source # | |
Read a => FromHerb (ShowAtom a) Source # | |
FromHerb a => FromHerb (Maybe a) Source # | |
FromHerb a => FromHerb [a] Source # | |
(FromHerb l, FromHerb r) => FromHerb (Either l r) Source # | |
(FromHerb a, FromHerb b) => FromHerb (a, b) Source # | |
(FromHerb a, FromHerb b, FromHerb c) => FromHerb (a, b, c) Source # | |
class FromHerb1 (f :: Type -> Type) where Source #
Nothing
liftParseHerb :: Parser Herb a -> Parser Herb (f a) Source #
Given a parser for the contained type a
, parse f a
.
Instances
Deserialization Parser utilities
gets :: (h -> a) -> Parser h a Source #
Return the current parser input as processed by a given 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.
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
.
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
.