2017-05-31 22:22:55 +03:00
|
|
|
{-# LANGUAGE GADTs, RankNTypes, ScopedTypeVariables #-}
|
2017-04-20 21:00:02 +03:00
|
|
|
module Renderer.SExpression
|
2017-05-29 23:51:32 +03:00
|
|
|
( renderSExpressionDiff
|
|
|
|
, renderSExpressionTerm
|
2017-04-20 21:00:02 +03:00
|
|
|
) where
|
2016-12-09 19:31:13 +03:00
|
|
|
|
|
|
|
import Data.Bifunctor.Join
|
2017-09-09 20:14:05 +03:00
|
|
|
import Data.ByteString.Char8 hiding (intersperse, foldr, spanEnd)
|
2017-09-10 01:01:51 +03:00
|
|
|
import Data.Foldable (fold)
|
2017-09-09 18:55:32 +03:00
|
|
|
import Data.Functor.Foldable (cata)
|
2017-09-09 20:14:05 +03:00
|
|
|
import Data.List (intersperse)
|
2017-03-31 23:12:26 +03:00
|
|
|
import Data.Record
|
2017-07-28 21:37:02 +03:00
|
|
|
import Data.Semigroup
|
2016-12-09 19:31:13 +03:00
|
|
|
import Diff
|
|
|
|
import Patch
|
2017-07-28 21:37:02 +03:00
|
|
|
import Prelude hiding (replicate)
|
2017-09-08 18:40:23 +03:00
|
|
|
import Term
|
2016-12-09 19:31:13 +03:00
|
|
|
|
2017-04-21 01:13:28 +03:00
|
|
|
-- | Returns a ByteString SExpression formatted diff.
|
2017-09-09 18:55:32 +03:00
|
|
|
renderSExpressionDiff :: (ConstrainAll Show fields, Foldable f, Functor f) => Diff f (Record fields) -> ByteString
|
2017-09-10 00:56:41 +03:00
|
|
|
renderSExpressionDiff diff = cata printDiffF diff 0 0 <> "\n"
|
2017-02-22 01:01:39 +03:00
|
|
|
|
2017-04-21 01:13:28 +03:00
|
|
|
-- | Returns a ByteString SExpression formatted term.
|
2017-09-09 18:55:32 +03:00
|
|
|
renderSExpressionTerm :: (ConstrainAll Show fields, Foldable f, Functor f) => Term f (Record fields) -> ByteString
|
2017-09-10 00:56:41 +03:00
|
|
|
renderSExpressionTerm term = cata printTermF term 0 0 <> "\n"
|
2017-04-20 21:00:02 +03:00
|
|
|
|
2017-09-10 00:56:41 +03:00
|
|
|
printDiffF :: (ConstrainAll Show fields, Foldable f, Functor f) => DiffF f (Record fields) (Int -> Int -> ByteString) -> Int -> Int -> ByteString
|
|
|
|
printDiffF diff parentLevel level = case diff of
|
2017-09-09 13:23:57 +03:00
|
|
|
Patch patch -> case patch of
|
2017-09-10 00:56:41 +03:00
|
|
|
Insert term -> pad parentLevel (level - 1) <> "{+" <> printTermF term (parentLevel + level) 0 <> "+}"
|
|
|
|
Delete term -> pad parentLevel (level - 1) <> "{-" <> printTermF term (parentLevel + level) 0 <> "-}"
|
|
|
|
Replace a b -> pad parentLevel (level - 1) <> "{ " <> printTermF a (parentLevel + level) 0 <> pad parentLevel (level - 1) <> "->" <> printTermF b (parentLevel + level) 0 <> " }"
|
2017-09-10 01:01:51 +03:00
|
|
|
Copy vs (Join (_, annotation)) syntax -> pad parentLevel level <> "(" <> showBindings (fmap (\ b -> b parentLevel level) <$> vs) <> showAnnotation annotation <> foldMap (\ d -> d parentLevel (level + 1)) syntax <> ")"
|
2017-09-10 00:56:41 +03:00
|
|
|
Var v -> pad parentLevel level <> showMetaVar v
|
|
|
|
|
|
|
|
printTermF :: (ConstrainAll Show fields, Foldable f, Functor f) => TermF f (Record fields) (Int -> Int -> ByteString) -> Int -> Int -> ByteString
|
|
|
|
printTermF (annotation :< syntax) parentLevel level =
|
2017-09-10 01:01:51 +03:00
|
|
|
pad parentLevel level <> "(" <> showAnnotation annotation <> foldMap (\t -> t parentLevel (level + 1)) syntax <> ")"
|
2017-09-10 00:56:41 +03:00
|
|
|
|
|
|
|
pad :: Int -> Int -> ByteString
|
|
|
|
pad p n | n <= 0 = ""
|
|
|
|
| otherwise = "\n" <> replicate (2 * (p + n)) ' '
|
2017-09-09 23:36:18 +03:00
|
|
|
|
2016-12-09 22:30:37 +03:00
|
|
|
|
2017-05-31 22:22:55 +03:00
|
|
|
showAnnotation :: ConstrainAll Show fields => Record fields -> ByteString
|
|
|
|
showAnnotation Nil = ""
|
2017-07-28 21:37:02 +03:00
|
|
|
showAnnotation (only :. Nil) = pack (show only)
|
|
|
|
showAnnotation (first :. rest) = pack (show first) <> " " <> showAnnotation rest
|
2017-09-09 20:14:05 +03:00
|
|
|
|
|
|
|
showBindings :: [(MetaVar, ByteString)] -> ByteString
|
|
|
|
showBindings [] = ""
|
2017-09-10 01:01:51 +03:00
|
|
|
showBindings bindings = "[ " <> fold (intersperse "\n, " (showBinding <$> bindings)) <> " ]"
|
2017-09-09 20:14:05 +03:00
|
|
|
where showBinding (var, val) = showMetaVar var <> "/" <> val
|
|
|
|
|
|
|
|
showMetaVar :: MetaVar -> ByteString
|
|
|
|
showMetaVar (MetaVar s) = pack ('$' : s)
|