{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TypeOperators #-}
module Language.SexpGrammar
(
Sexp
, Position
, SexpGrammar
, Grammar
, (:-)
, SexpIso (..)
, toSexp
, encode
, encodeWith
, encodePretty
, encodePrettyWith
, fromSexp
, decode
, decodeWith
, module Control.Category
, module Data.InvertibleGrammar.Combinators
, module Language.SexpGrammar.Base
, Mismatch
, expected
, unexpected
) where
import Control.Category ((<<<), (>>>))
import Data.ByteString.Lazy.Char8 (ByteString)
import Data.InvertibleGrammar
import Data.InvertibleGrammar.Combinators
import Language.Sexp.Located (Sexp, Position)
import qualified Language.Sexp.Located as Sexp
import Language.SexpGrammar.Base
import Language.SexpGrammar.Class
fromSexp :: SexpGrammar a -> Sexp -> Either String a
fromSexp :: forall a. SexpGrammar a -> Sexp -> Either String a
fromSexp SexpGrammar a
g =
forall p a.
Show p =>
p
-> ContextError (Propagation p) (GrammarError p) a
-> Either String a
runGrammarString Position
Sexp.dummyPos forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall p a b.
Grammar p a b
-> a -> ContextError (Propagation p) (GrammarError p) b
forward (forall p a b. Grammar p (a :- Void) (b :- Void) -> Grammar p a b
sealed SexpGrammar a
g)
toSexp :: SexpGrammar a -> a -> Either String Sexp
toSexp :: forall a. SexpGrammar a -> a -> Either String Sexp
toSexp SexpGrammar a
g =
forall p a.
Show p =>
p
-> ContextError (Propagation p) (GrammarError p) a
-> Either String a
runGrammarString Position
Sexp.dummyPos forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall p a b.
Grammar p a b
-> b -> ContextError (Propagation p) (GrammarError p) a
backward (forall p a b. Grammar p (a :- Void) (b :- Void) -> Grammar p a b
sealed SexpGrammar a
g)
encode :: SexpIso a => a -> Either String ByteString
encode :: forall a. SexpIso a => a -> Either String ByteString
encode =
forall a. SexpGrammar a -> a -> Either String ByteString
encodeWith forall a. SexpIso a => SexpGrammar a
sexpIso
encodeWith :: SexpGrammar a -> a -> Either String ByteString
encodeWith :: forall a. SexpGrammar a -> a -> Either String ByteString
encodeWith SexpGrammar a
g =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Sexp -> ByteString
Sexp.encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SexpGrammar a -> a -> Either String Sexp
toSexp SexpGrammar a
g
encodePretty :: SexpIso a => a -> Either String ByteString
encodePretty :: forall a. SexpIso a => a -> Either String ByteString
encodePretty =
forall a. SexpGrammar a -> a -> Either String ByteString
encodePrettyWith forall a. SexpIso a => SexpGrammar a
sexpIso
encodePrettyWith :: SexpGrammar a -> a -> Either String ByteString
encodePrettyWith :: forall a. SexpGrammar a -> a -> Either String ByteString
encodePrettyWith SexpGrammar a
g =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Sexp -> ByteString
Sexp.format forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SexpGrammar a -> a -> Either String Sexp
toSexp SexpGrammar a
g
decode :: SexpIso a => ByteString -> Either String a
decode :: forall a. SexpIso a => ByteString -> Either String a
decode =
forall a. SexpGrammar a -> String -> ByteString -> Either String a
decodeWith forall a. SexpIso a => SexpGrammar a
sexpIso String
"<string>"
decodeWith :: SexpGrammar a -> FilePath -> ByteString -> Either String a
decodeWith :: forall a. SexpGrammar a -> String -> ByteString -> Either String a
decodeWith SexpGrammar a
g String
fn ByteString
input =
String -> ByteString -> Either String Sexp
Sexp.parseSexp String
fn ByteString
input forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. SexpGrammar a -> Sexp -> Either String a
fromSexp SexpGrammar a
g