{- FOURMOLU_DISABLE -}
-- The MIT License (MIT)

-- Copyright (c) 2016-2024 Objectionary.com

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:

-- The above copyright notice and this permission notice shall be included
-- in all copies or substantial portions of the Software.

-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
{- FOURMOLU_ENABLE -}
{-# 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

-- * Overriding generated pretty-printer

-- | Like 'Phi.printTree', but without spaces around dots and no indentation for curly braces.
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

-- | Remove spaces around dots.
--
-- >>> shrinkDots "a ↦ ξ . a" == "a ↦ ξ.a"
-- True
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

-- | Copy of 'Phi.render', except no indentation is made for curly braces.
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 ->
    -- \^ Indentation level.
    Bool ->
    -- \^ Pending indentation to be output before next character?
    [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
    -- Output character after pending indentation.
    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)
          )

    -- Output pending indentation.
    pending :: ShowS
    pending :: String -> String
pending = if Bool
p then Int -> String -> String
indent Int
i else String -> String
forall a. a -> a
id

  -- Indentation (spaces) for given indentation level.
  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
' ')

  -- Continue rendering in new line with new indentation.
  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

  -- Separate given string from following text by a space (if needed).
  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) -> [] -- remove trailing space
      (Bool
False, Bool
_, Bool
True) -> String
t -- remove trailing space
      (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 -- add space if none
      (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
")],;"

  -- Make sure we are on a fresh line.
  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