{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ <= 708
{-# LANGUAGE OverlappingInstances #-}
#endif
{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}

-- | Pretty-printer for PrintSciDbAFL.
--   Generated by the BNF converter.

module PrintSciDbAFL where

import qualified AbsSciDbAFL
import Data.Char

-- | The top-level printing method.

printTree :: Print a => a -> String
printTree = render . prt 0

type Doc = [ShowS] -> [ShowS]

doc :: ShowS -> Doc
doc = (:)

render :: Doc -> String
render d = rend 0 (map ($ "") $ d []) "" where
  rend i ss = case ss of
    "["      :ts -> showChar '[' . rend i ts
    "("      :ts -> showChar '(' . rend i ts
    "{"      :ts -> showChar '{' . new (i+1) . rend (i+1) ts
    "}" : ";":ts -> new (i-1) . space "}" . showChar ';' . new (i-1) . rend (i-1) ts
    "}"      :ts -> new (i-1) . showChar '}' . new (i-1) . rend (i-1) ts
    ";"      :ts -> showChar ';' . new i . rend i ts
    t  : ts@(p:_) | closingOrPunctuation p -> showString t . rend i ts
    t        :ts -> space t . rend i ts
    _            -> id
  new i   = showChar '\n' . replicateS (2*i) (showChar ' ') . dropWhile isSpace
  space t = showString t . (\s -> if null s then "" else ' ':s)

  closingOrPunctuation :: String -> Bool
  closingOrPunctuation [c] = c `elem` closerOrPunct
  closingOrPunctuation _   = False

  closerOrPunct :: String
  closerOrPunct = ")],;"

parenth :: Doc -> Doc
parenth ss = doc (showChar '(') . ss . doc (showChar ')')

concatS :: [ShowS] -> ShowS
concatS = foldr (.) id

concatD :: [Doc] -> Doc
concatD = foldr (.) id

replicateS :: Int -> ShowS -> ShowS
replicateS n f = concatS (replicate n f)

-- | The printer class does the job.

class Print a where
  prt :: Int -> a -> Doc
  prtList :: Int -> [a] -> Doc
  prtList i = concatD . map (prt i)

instance {-# OVERLAPPABLE #-} Print a => Print [a] where
  prt = prtList

instance Print Char where
  prt _ s = doc (showChar '\'' . mkEsc '\'' s . showChar '\'')
  prtList _ s = doc (showChar '"' . concatS (map (mkEsc '"') s) . showChar '"')

mkEsc :: Char -> Char -> ShowS
mkEsc q s = case s of
  _ | s == q -> showChar '\\' . showChar s
  '\\'-> showString "\\\\"
  '\n' -> showString "\\n"
  '\t' -> showString "\\t"
  _ -> showChar s

prPrec :: Int -> Int -> Doc -> Doc
prPrec i j = if j < i then parenth else id

instance Print Integer where
  prt _ x = doc (shows x)

instance Print Double where
  prt _ x = doc (shows x)

instance Print AbsSciDbAFL.ResAnd where
  prt _ (AbsSciDbAFL.ResAnd i) = doc (showString i)

instance Print AbsSciDbAFL.ResArray where
  prt _ (AbsSciDbAFL.ResArray i) = doc (showString i)

instance Print AbsSciDbAFL.ResAs where
  prt _ (AbsSciDbAFL.ResAs i) = doc (showString i)

instance Print AbsSciDbAFL.ResAsc where
  prt _ (AbsSciDbAFL.ResAsc i) = doc (showString i)

instance Print AbsSciDbAFL.ResCompression where
  prt _ (AbsSciDbAFL.ResCompression i) = doc (showString i)

instance Print AbsSciDbAFL.ResCreate where
  prt _ (AbsSciDbAFL.ResCreate i) = doc (showString i)

instance Print AbsSciDbAFL.ResDefault where
  prt _ (AbsSciDbAFL.ResDefault i) = doc (showString i)

instance Print AbsSciDbAFL.ResDesc where
  prt _ (AbsSciDbAFL.ResDesc i) = doc (showString i)

instance Print AbsSciDbAFL.ResFalse where
  prt _ (AbsSciDbAFL.ResFalse i) = doc (showString i)

instance Print AbsSciDbAFL.ResNot where
  prt _ (AbsSciDbAFL.ResNot i) = doc (showString i)

instance Print AbsSciDbAFL.ResNull where
  prt _ (AbsSciDbAFL.ResNull i) = doc (showString i)

instance Print AbsSciDbAFL.ResOr where
  prt _ (AbsSciDbAFL.ResOr i) = doc (showString i)

instance Print AbsSciDbAFL.ResTemp where
  prt _ (AbsSciDbAFL.ResTemp i) = doc (showString i)

instance Print AbsSciDbAFL.ResTrue where
  prt _ (AbsSciDbAFL.ResTrue i) = doc (showString i)

instance Print AbsSciDbAFL.ADouble where
  prt _ (AbsSciDbAFL.ADouble i) = doc (showString i)

instance Print AbsSciDbAFL.AString where
  prt _ (AbsSciDbAFL.AString i) = doc (showString i)

instance Print AbsSciDbAFL.Id where
  prt _ (AbsSciDbAFL.Id i) = doc (showString i)

instance Print AbsSciDbAFL.Exp where
  prt i e = case e of
    AbsSciDbAFL.Eor exp1 resor exp2 -> prPrec i 0 (concatD [prt 0 exp1, prt 0 resor, prt 1 exp2])
    AbsSciDbAFL.Eand exp1 resand exp2 -> prPrec i 1 (concatD [prt 1 exp1, prt 0 resand, prt 2 exp2])
    AbsSciDbAFL.Eeq exp1 exp2 -> prPrec i 2 (concatD [prt 2 exp1, doc (showString "="), prt 3 exp2])
    AbsSciDbAFL.Ene exp1 exp2 -> prPrec i 2 (concatD [prt 2 exp1, doc (showString "<>"), prt 3 exp2])
    AbsSciDbAFL.Elt exp1 exp2 -> prPrec i 3 (concatD [prt 3 exp1, doc (showString "<"), prt 4 exp2])
    AbsSciDbAFL.Egt exp1 exp2 -> prPrec i 3 (concatD [prt 3 exp1, doc (showString ">"), prt 4 exp2])
    AbsSciDbAFL.Ele exp1 exp2 -> prPrec i 3 (concatD [prt 3 exp1, doc (showString "<="), prt 4 exp2])
    AbsSciDbAFL.Ege exp1 exp2 -> prPrec i 3 (concatD [prt 3 exp1, doc (showString ">="), prt 4 exp2])
    AbsSciDbAFL.EAdd exp1 exp2 -> prPrec i 4 (concatD [prt 4 exp1, doc (showString "+"), prt 5 exp2])
    AbsSciDbAFL.ESub exp1 exp2 -> prPrec i 4 (concatD [prt 4 exp1, doc (showString "-"), prt 5 exp2])
    AbsSciDbAFL.EMul exp1 exp2 -> prPrec i 5 (concatD [prt 5 exp1, doc (showString "*"), prt 6 exp2])
    AbsSciDbAFL.EDiv exp1 exp2 -> prPrec i 5 (concatD [prt 5 exp1, doc (showString "/"), prt 6 exp2])
    AbsSciDbAFL.EMod exp1 exp2 -> prPrec i 5 (concatD [prt 5 exp1, doc (showString "%"), prt 6 exp2])
    AbsSciDbAFL.ENeg exp -> prPrec i 7 (concatD [doc (showString "-"), prt 6 exp])
    AbsSciDbAFL.EFunc id exps -> prPrec i 8 (concatD [prt 0 id, doc (showString "("), prt 0 exps, doc (showString ")")])
    AbsSciDbAFL.EVersion id n -> prPrec i 8 (concatD [prt 0 id, doc (showString "@"), prt 0 n])
    AbsSciDbAFL.EArrayVar id1 id2 -> prPrec i 8 (concatD [prt 0 id1, doc (showString "."), prt 0 id2])
    AbsSciDbAFL.EOption id exp -> prPrec i 8 (concatD [prt 0 id, doc (showString ":"), prt 0 exp])
    AbsSciDbAFL.EAsId exp resas id -> prPrec i 9 (concatD [prt 8 exp, prt 0 resas, prt 0 id])
    AbsSciDbAFL.EAsc exp resasc -> prPrec i 9 (concatD [prt 8 exp, prt 0 resasc])
    AbsSciDbAFL.EDesc exp resdesc -> prPrec i 9 (concatD [prt 8 exp, prt 0 resdesc])
    AbsSciDbAFL.EVar id -> prPrec i 10 (concatD [prt 0 id])
    AbsSciDbAFL.EScheme schema -> prPrec i 10 (concatD [prt 0 schema])
    AbsSciDbAFL.EString astring -> prPrec i 10 (concatD [prt 0 astring])
    AbsSciDbAFL.EFalse resfalse -> prPrec i 10 (concatD [prt 0 resfalse])
    AbsSciDbAFL.ETrue restrue -> prPrec i 10 (concatD [prt 0 restrue])
    AbsSciDbAFL.ENull resnull -> prPrec i 10 (concatD [prt 0 resnull])
    AbsSciDbAFL.EInt n -> prPrec i 10 (concatD [prt 0 n])
    AbsSciDbAFL.EDouble adouble -> prPrec i 10 (concatD [prt 0 adouble])
    AbsSciDbAFL.EWildcard -> prPrec i 10 (concatD [doc (showString "*")])
    AbsSciDbAFL.EDefault -> prPrec i 10 (concatD [doc (showString "?")])
  prtList _ [] = concatD []
  prtList _ [x] = concatD [prt 0 x]
  prtList _ (x:xs) = concatD [prt 0 x, doc (showString ","), prt 0 xs]

instance Print [AbsSciDbAFL.Exp] where
  prt = prtList

instance Print AbsSciDbAFL.AFL where
  prt i e = case e of
    AbsSciDbAFL.Queries querys -> prPrec i 0 (concatD [prt 0 querys])

instance Print [AbsSciDbAFL.Query] where
  prt = prtList

instance Print AbsSciDbAFL.Query where
  prt i e = case e of
    AbsSciDbAFL.QueryNil -> prPrec i 0 (concatD [])
    AbsSciDbAFL.QueryExp exp -> prPrec i 0 (concatD [prt 0 exp])
    AbsSciDbAFL.QueryArray rescreate resarray id schema -> prPrec i 0 (concatD [prt 0 rescreate, prt 0 resarray, prt 0 id, prt 0 schema])
    AbsSciDbAFL.QueryTemp rescreate restemp resarray id schema -> prPrec i 0 (concatD [prt 0 rescreate, prt 0 restemp, prt 0 resarray, prt 0 id, prt 0 schema])
  prtList _ [] = concatD []
  prtList _ (x:xs) = concatD [prt 0 x, doc (showString ";"), prt 0 xs]

instance Print AbsSciDbAFL.Schema where
  prt i e = case e of
    AbsSciDbAFL.Scheme attributes dimensions -> prPrec i 0 (concatD [doc (showString "<"), prt 0 attributes, doc (showString ">"), doc (showString "["), prt 0 dimensions, doc (showString "]")])

instance Print [AbsSciDbAFL.Attribute] where
  prt = prtList

instance Print AbsSciDbAFL.Attribute where
  prt i e = case e of
    AbsSciDbAFL.Attrib id1 id2 nullableoption defaultoption compressionoption -> prPrec i 0 (concatD [prt 0 id1, doc (showString ":"), prt 0 id2, prt 0 nullableoption, prt 0 defaultoption, prt 0 compressionoption])
  prtList _ [x] = concatD [prt 0 x]
  prtList _ (x:xs) = concatD [prt 0 x, doc (showString ","), prt 0 xs]

instance Print AbsSciDbAFL.NullableOption where
  prt i e = case e of
    AbsSciDbAFL.NullabeOff -> prPrec i 0 (concatD [])
    AbsSciDbAFL.NullableOn resnull -> prPrec i 0 (concatD [prt 0 resnull])
    AbsSciDbAFL.NullableNot resnot resnull -> prPrec i 0 (concatD [prt 0 resnot, prt 0 resnull])

instance Print AbsSciDbAFL.DefaultOption where
  prt i e = case e of
    AbsSciDbAFL.DefaultOff -> prPrec i 0 (concatD [])
    AbsSciDbAFL.DefaultOn resdefault exp -> prPrec i 0 (concatD [prt 0 resdefault, prt 6 exp])

instance Print AbsSciDbAFL.CompressionOption where
  prt i e = case e of
    AbsSciDbAFL.CompressionOff -> prPrec i 0 (concatD [])
    AbsSciDbAFL.CompressionOn rescompression astring -> prPrec i 0 (concatD [prt 0 rescompression, prt 0 astring])

instance Print AbsSciDbAFL.Dimensions where
  prt i e = case e of
    AbsSciDbAFL.Dim dimension -> prPrec i 0 (concatD [prt 0 dimension])
    AbsSciDbAFL.DimSemicolon dimension dimensions -> prPrec i 0 (concatD [prt 0 dimension, doc (showString ";"), prt 0 dimensions])
    AbsSciDbAFL.DimComma dimension dimensions -> prPrec i 0 (concatD [prt 0 dimension, doc (showString ","), prt 0 dimensions])

instance Print AbsSciDbAFL.Dimension where
  prt i e = case e of
    AbsSciDbAFL.DimId id -> prPrec i 0 (concatD [prt 0 id])
    AbsSciDbAFL.DimLoHi id exp1 exp2 -> prPrec i 0 (concatD [prt 0 id, doc (showString "="), prt 0 exp1, doc (showString ":"), prt 0 exp2])
    AbsSciDbAFL.DimLoHiOverlap id exp1 exp2 exp3 -> prPrec i 0 (concatD [prt 0 id, doc (showString "="), prt 0 exp1, doc (showString ":"), prt 0 exp2, doc (showString ":"), prt 0 exp3])
    AbsSciDbAFL.DimAll id exp1 exp2 exp3 exp4 -> prPrec i 0 (concatD [prt 0 id, doc (showString "="), prt 0 exp1, doc (showString ":"), prt 0 exp2, doc (showString ":"), prt 0 exp3, doc (showString ":"), prt 0 exp4])
    AbsSciDbAFL.DimDeprecated id exp1 exp2 exp3 exp4 -> prPrec i 0 (concatD [prt 0 id, doc (showString "="), prt 0 exp1, doc (showString ":"), prt 0 exp2, doc (showString ","), prt 0 exp3, doc (showString ","), prt 0 exp4])