{-# OPTIONS_GHC -Wno-redundant-constraints #-}
-- | Printing helpers.
module Language.Drasil.Printing.Import.ExprCommon (
  -- * Expression-related
  parens,
  -- * Symbol and Term Resolution
  lookupC, lookupC', lookupSymb
) where

import Control.Lens ((^.))

import Drasil.Database (UID, ChunkDB, findOrErr, UIDRef, IsChunk, raw)
import Language.Drasil (Stage(..), codeSymb, eqSymb, Symbol, HasSymbol,
  DefinedQuantityDict)

import qualified Language.Drasil.Printing.AST as P
import Language.Drasil.Printing.PrintingInformation (PrintingInformation, stg, sysdb)

-- * Expr-related

-- | Helper for inserting parentheses.
parens :: P.Expr -> P.Expr
parens :: Expr -> Expr
parens = Fence -> Fence -> Expr -> Expr
P.Fenced Fence
P.Paren Fence
P.Paren

-- * Lookup/Term Resolution Functions

-- | Given the stage of the symbol, looks up a character/symbol
-- inside a chunk database that matches the given 'UID'.
lookupC :: Stage -> ChunkDB -> UID -> Symbol
lookupC :: Stage -> ChunkDB -> UID -> Symbol
lookupC Stage
Equational     ChunkDB
sm UID
c = DefinedQuantityDict -> Symbol
forall q. HasSymbol q => q -> Symbol
eqSymb   (UID -> ChunkDB -> DefinedQuantityDict
forall a. Typeable a => UID -> ChunkDB -> a
findOrErr UID
c ChunkDB
sm :: DefinedQuantityDict)
lookupC Stage
Implementation ChunkDB
sm UID
c = DefinedQuantityDict -> Symbol
forall q. HasSymbol q => q -> Symbol
codeSymb (UID -> ChunkDB -> DefinedQuantityDict
forall a. Typeable a => UID -> ChunkDB -> a
findOrErr UID
c ChunkDB
sm :: DefinedQuantityDict)

lookupC' :: PrintingInformation -> UID -> Symbol
lookupC' :: PrintingInformation -> UID -> Symbol
lookupC' PrintingInformation
pinfo = Stage -> ChunkDB -> UID -> Symbol
lookupC (PrintingInformation
pinfo PrintingInformation
-> Getting Stage PrintingInformation Stage -> Stage
forall s a. s -> Getting a s a -> a
^. Getting Stage PrintingInformation Stage
Lens' PrintingInformation Stage
stg) (PrintingInformation
pinfo PrintingInformation
-> Getting ChunkDB PrintingInformation ChunkDB -> ChunkDB
forall s a. s -> Getting a s a -> a
^. Getting ChunkDB PrintingInformation ChunkDB
Lens' PrintingInformation ChunkDB
sysdb)

-- | Look up a symbol given a chunk database and a 'UID' associated with the
-- symbol. Hack: Always uses 'DefinedQuantityDict' as the chunk type to look up,
-- despite that not being the _actual type_ of the chunk being looked up.
--
-- Note: It is because of this function that that the
-- `-Wno-redundant-constraints` OPTIONS_GHC pragma is at the top of the file.
-- This is because we technically don't use `t` at all in the output expression.
-- It can technically be anything!
lookupSymb :: (IsChunk t, HasSymbol t) => PrintingInformation -> UIDRef t -> Symbol
lookupSymb :: forall t.
(IsChunk t, HasSymbol t) =>
PrintingInformation -> UIDRef t -> Symbol
lookupSymb PrintingInformation
pinfo UIDRef t
u = Stage -> DefinedQuantityDict -> Symbol
forall {q}. HasSymbol q => Stage -> q -> Symbol
sytyF (PrintingInformation
pinfo PrintingInformation
-> Getting Stage PrintingInformation Stage -> Stage
forall s a. s -> Getting a s a -> a
^. Getting Stage PrintingInformation Stage
Lens' PrintingInformation Stage
stg) (UID -> ChunkDB -> DefinedQuantityDict
forall a. Typeable a => UID -> ChunkDB -> a
findOrErr (UIDRef t -> UID
forall {k} (t :: k). UIDRef t -> UID
raw UIDRef t
u) (PrintingInformation
pinfo PrintingInformation
-> Getting ChunkDB PrintingInformation ChunkDB -> ChunkDB
forall s a. s -> Getting a s a -> a
^. Getting ChunkDB PrintingInformation ChunkDB
Lens' PrintingInformation ChunkDB
sysdb) :: DefinedQuantityDict)
  where sytyF :: Stage -> q -> Symbol
sytyF Stage
Equational = q -> Symbol
forall q. HasSymbol q => q -> Symbol
eqSymb
        sytyF Stage
Implementation = q -> Symbol
forall q. HasSymbol q => q -> Symbol
codeSymb