{-# LANGUAGE LambdaCase #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Language.EO.Phi.Syntax (
module Language.EO.Phi.Syntax.Abs,
printTree,
shrinkDots,
) where
import Data.Char (isSpace)
import Language.EO.Phi.Rules.Common ()
import Language.EO.Phi.Syntax.Abs
import Language.EO.Phi.Syntax.Print qualified as Phi
printTree :: (Phi.Print a) => a -> String
printTree :: forall a. Print a => a -> String
printTree = String -> String
shrinkDots (String -> String) -> (a -> String) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> String
render (Doc -> String) -> (a -> Doc) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> Doc
forall a. Print a => Int -> a -> Doc
Phi.prt Int
0
shrinkDots :: String -> String
shrinkDots :: String -> String
shrinkDots [] = []
shrinkDots (Char
' ' : Char
'.' : Char
' ' : String
cs) = Char
'.' Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
shrinkDots String
cs
shrinkDots (Char
c : String
cs) = Char
c Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
shrinkDots String
cs
render :: Phi.Doc -> String
render :: Doc -> String
render Doc
d = Int -> Bool -> [String] -> String -> String
rend Int
0 Bool
False (((String -> String) -> String) -> [String -> String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ((String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"") ([String -> String] -> [String]) -> [String -> String] -> [String]
forall a b. (a -> b) -> a -> b
$ Doc
d []) String
""
where
rend ::
Int ->
Bool ->
[String] ->
ShowS
rend :: Int -> Bool -> [String] -> String -> String
rend Int
i Bool
p = \case
String
"[" : String
"]" : [String]
ts -> String -> String -> String
showString String
"[]" (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Bool -> [String] -> String -> String
rend Int
i Bool
False [String]
ts
String
"(" : String
")" : (String
t : [String]
ts) -> String -> String -> [String] -> String -> String
handleTrailingComma String
"()" String
t [String]
ts
String
"⟦" : String
"⟧" : (String
t : [String]
ts) -> String -> String -> [String] -> String -> String
handleTrailingComma String
"⟦⟧" String
t [String]
ts
String
"[" : [String]
ts -> Char -> String -> String
char Char
'[' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Bool -> [String] -> String -> String
rend Int
i Bool
False [String]
ts
String
"(" : [String]
ts -> Char -> String -> String
char Char
'(' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> String -> String
new (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [String]
ts
String
"{" : String
"⟦" : [String]
ts -> Char -> String -> String
showChar Char
'{' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Bool -> String -> String
onNewLine (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Bool
p (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> String -> String
showChar Char
'⟦' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> String -> String
new (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) [String]
ts
String
"⟦" : [String]
ts -> Char -> String -> String
showChar Char
'⟦' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> String -> String
new (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [String]
ts
String
")" : String
"," : [String]
ts -> Int -> Bool -> String -> String
onNewLine (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Bool
p (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
")," (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> String -> String
new (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [String]
ts
String
"⟧" : String
"," : [String]
ts -> Int -> Bool -> String -> String
onNewLine (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Bool
p (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
"⟧," (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> String -> String
new (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [String]
ts
[String
"⟧", String
"}"] -> Int -> Bool -> String -> String
onNewLine (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Bool
p (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> String -> String
showChar Char
'⟧' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> String -> String
new (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2) [String
"}"]
String
"⟧" : [String]
ts -> Int -> Bool -> String -> String
onNewLine (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Bool
p (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> String -> String
showChar Char
'⟧' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> String -> String
new (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [String]
ts
String
")" : [String]
ts -> Int -> Bool -> String -> String
onNewLine (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Bool
p (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> String -> String
showChar Char
')' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> String -> String
new (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [String]
ts
[String
";"] -> Char -> String -> String
char Char
';'
String
";" : [String]
ts -> Char -> String -> String
char Char
';' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> String -> String
new Int
i [String]
ts
String
"." : [String]
ts -> Int -> Bool -> [String] -> String -> String
rend Int
i Bool
p (String
" ." String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
ts)
String
t : (String
s : [String]
ss) | String -> Bool
closingOrPunctuation String
s -> String -> String -> [String] -> String -> String
handleTrailingComma String
t String
s [String]
ss
String
t : [String]
ts -> String -> String
pending (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
space String
t (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Bool -> [String] -> String -> String
rend Int
i Bool
False [String]
ts
[] -> String -> String
forall a. a -> a
id
where
char :: Char -> ShowS
char :: Char -> String -> String
char Char
c = String -> String
pending (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> String -> String
showChar Char
c
handleTrailingComma :: String -> String -> [String] -> String -> String
handleTrailingComma String
str String
t [String]
ts =
(String -> String
pending (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
str)
(String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ( case String
t of
String
"," -> Char -> String -> String
showChar Char
',' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> String -> String
new Int
i [String]
ts
String
_ -> Int -> Bool -> [String] -> String -> String
rend Int
i Bool
False (String
t String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
ts)
)
pending :: ShowS
pending :: String -> String
pending = if Bool
p then Int -> String -> String
indent Int
i else String -> String
forall a. a -> a
id
indent :: Int -> ShowS
indent :: Int -> String -> String
indent Int
i = Int -> (String -> String) -> String -> String
Phi.replicateS (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i) (Char -> String -> String
showChar Char
' ')
new :: Int -> [String] -> ShowS
new :: Int -> [String] -> String -> String
new Int
j [String]
ts = Char -> String -> String
showChar Char
'\n' (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Bool -> [String] -> String -> String
rend Int
j Bool
True [String]
ts
space :: String -> ShowS
space :: String -> String -> String
space String
t String
s =
case ((Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isSpace String
t, String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
spc, String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
rest) of
(Bool
True, Bool
_, Bool
True) -> []
(Bool
False, Bool
_, Bool
True) -> String
t
(Bool
False, Bool
True, Bool
False) -> String
t String -> String -> String
forall a. [a] -> [a] -> [a]
++ Char
' ' Char -> String -> String
forall a. a -> [a] -> [a]
: String
s
(Bool, Bool, Bool)
_ -> String
t String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
where
(String
spc, String
rest) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
span Char -> Bool
isSpace String
s
closingOrPunctuation :: String -> Bool
closingOrPunctuation :: String -> Bool
closingOrPunctuation [Char
c] = Char
c Char -> String -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
closerOrPunct
closingOrPunctuation String
_ = Bool
False
closerOrPunct :: String
closerOrPunct :: String
closerOrPunct = String
")],;"
onNewLine :: Int -> Bool -> ShowS
onNewLine :: Int -> Bool -> String -> String
onNewLine Int
i Bool
p = (if Bool
p then String -> String
forall a. a -> a
id else Char -> String -> String
showChar Char
'\n') (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> String
indent Int
i