2021-02-05 20:57:17 +00:00
|
|
|
{-# LANGUAGE BlockArguments #-}
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
|
|
|
module DearImGui.Generator
|
2021-02-07 23:07:14 +00:00
|
|
|
( declareEnumerations, enumerationsTypesTable )
|
2021-02-05 20:57:17 +00:00
|
|
|
where
|
|
|
|
|
|
|
|
-- base
|
2021-02-07 23:07:14 +00:00
|
|
|
import Control.Arrow
|
|
|
|
( second )
|
2021-02-05 20:57:17 +00:00
|
|
|
import Data.Bits
|
|
|
|
( Bits )
|
|
|
|
import Data.Foldable
|
|
|
|
( toList )
|
2021-02-07 23:07:14 +00:00
|
|
|
import qualified Data.List.NonEmpty as NonEmpty
|
|
|
|
( head )
|
|
|
|
import Data.String
|
|
|
|
( fromString )
|
2021-02-05 20:57:17 +00:00
|
|
|
import Data.Traversable
|
|
|
|
( for )
|
|
|
|
import Foreign.Storable
|
|
|
|
( Storable )
|
|
|
|
|
2021-02-07 23:07:14 +00:00
|
|
|
-- containers
|
|
|
|
import Data.Map.Strict
|
|
|
|
( Map )
|
|
|
|
import qualified Data.Map.Strict as Map
|
|
|
|
( fromList )
|
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
-- directory
|
|
|
|
import System.Directory
|
|
|
|
( canonicalizePath )
|
|
|
|
|
|
|
|
-- filepath
|
|
|
|
import System.FilePath
|
|
|
|
( takeDirectory )
|
|
|
|
|
2021-02-07 23:07:14 +00:00
|
|
|
-- inline-c
|
|
|
|
import qualified Language.C.Types as InlineC
|
|
|
|
( TypeSpecifier(TypeName) )
|
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
-- megaparsec
|
|
|
|
import qualified Text.Megaparsec as Megaparsec
|
|
|
|
|
|
|
|
-- template-haskell
|
|
|
|
import qualified Language.Haskell.TH as TH
|
|
|
|
import qualified Language.Haskell.TH.Syntax as TH
|
|
|
|
|
|
|
|
-- text
|
|
|
|
import qualified Data.Text as Text
|
|
|
|
import qualified Data.Text.IO as Text
|
|
|
|
( readFile )
|
|
|
|
|
|
|
|
-- dear-imgui-generator
|
|
|
|
import qualified DearImGui.Generator.Parser as Parser
|
|
|
|
( headers )
|
|
|
|
import DearImGui.Generator.Tokeniser
|
2021-02-07 23:07:14 +00:00
|
|
|
( Tok, tokenise )
|
2021-02-05 20:57:17 +00:00
|
|
|
import DearImGui.Generator.Types
|
2021-02-07 23:07:14 +00:00
|
|
|
( Comment(..), Enumeration(..), Headers(..)
|
|
|
|
, generateNames
|
|
|
|
)
|
2021-02-05 20:57:17 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Obtaining parsed header data.
|
|
|
|
|
2021-02-07 23:07:14 +00:00
|
|
|
headers :: Headers ( TH.Name, TH.Name )
|
2021-02-05 20:57:17 +00:00
|
|
|
headers = $( do
|
|
|
|
currentPath <- TH.loc_filename <$> TH.location
|
2021-02-07 23:07:14 +00:00
|
|
|
basicHeaders <- TH.runIO do
|
2021-02-05 20:57:17 +00:00
|
|
|
headersPath <- canonicalizePath ( takeDirectory currentPath <> "/../../imgui/imgui.h" )
|
|
|
|
headersSource <- Text.readFile headersPath
|
|
|
|
tokens <- case tokenise headersSource of
|
|
|
|
Left err -> error ( "Couldn't tokenise Dear ImGui headers:\n\n" <> show err )
|
|
|
|
Right toks -> pure toks
|
|
|
|
case Megaparsec.parse Parser.headers "" tokens of
|
2021-02-07 23:07:14 +00:00
|
|
|
Left err -> do
|
|
|
|
let
|
|
|
|
errorPos :: Int
|
|
|
|
errorPos = Megaparsec.errorOffset . NonEmpty.head $ Megaparsec.bundleErrors err
|
|
|
|
prev, rest :: [ Tok ]
|
|
|
|
( prev, rest ) = second ( take 15 ) . splitAt 5 . drop ( errorPos - 5 ) $ tokens
|
|
|
|
error $
|
|
|
|
"Couldn't parse Dear ImGui headers:\n\n" <>
|
|
|
|
( unlines ( map Megaparsec.parseErrorPretty . toList $ Megaparsec.bundleErrors err ) ) <> "\n" <>
|
|
|
|
( unlines ( map show prev ) <> "\n\n" <> unlines ( map show rest ) )
|
2021-02-05 20:57:17 +00:00
|
|
|
Right res -> pure res
|
2021-02-09 11:23:23 +00:00
|
|
|
TH.lift $ generateNames basicHeaders
|
2021-02-05 20:57:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Generating TH splices.
|
|
|
|
|
2021-02-07 23:07:14 +00:00
|
|
|
enumerationsTypesTable :: Map InlineC.TypeSpecifier ( TH.Q TH.Type )
|
|
|
|
enumerationsTypesTable = Map.fromList . map mkTypePair $ enums headers
|
|
|
|
where
|
|
|
|
mkTypePair :: Enumeration ( TH.Name, TH.Name ) -> ( InlineC.TypeSpecifier, TH.Q TH.Type )
|
|
|
|
mkTypePair ( Enumeration { enumName, enumTypeName } ) =
|
|
|
|
( InlineC.TypeName $ fromString ( Text.unpack enumName )
|
|
|
|
, TH.conT ( fst $ enumTypeName )
|
|
|
|
)
|
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
declareEnumerations :: TH.Name -> TH.Name -> TH.Q [ TH.Dec ]
|
|
|
|
declareEnumerations finiteEnumName countName = do
|
|
|
|
concat <$> mapM ( declareEnumeration finiteEnumName countName ) ( enums headers )
|
|
|
|
|
2021-02-07 23:07:14 +00:00
|
|
|
declareEnumeration :: TH.Name -> TH.Name -> Enumeration ( TH.Name, TH.Name ) -> TH.Q [ TH.Dec ]
|
2021-02-05 20:57:17 +00:00
|
|
|
declareEnumeration finiteEnumName countName ( Enumeration {..} ) = do
|
|
|
|
let
|
2021-02-07 23:07:14 +00:00
|
|
|
tyName, conName :: TH.Name
|
|
|
|
( tyName, conName ) = enumTypeName
|
2021-02-05 20:57:17 +00:00
|
|
|
isFlagEnum :: Bool
|
|
|
|
isFlagEnum = "Flags" `Text.isInfixOf` enumName
|
|
|
|
newtypeCon :: TH.Q TH.Con
|
|
|
|
newtypeCon =
|
|
|
|
TH.normalC conName
|
|
|
|
[ TH.bangType
|
|
|
|
( TH.bang TH.noSourceUnpackedness TH.noSourceStrictness )
|
2021-02-07 23:07:14 +00:00
|
|
|
( TH.conT underlyingType )
|
2021-02-05 20:57:17 +00:00
|
|
|
]
|
|
|
|
classes :: [ TH.Q TH.Type ]
|
|
|
|
classes
|
|
|
|
| isFlagEnum
|
|
|
|
= map TH.conT [ ''Eq, ''Ord, ''Storable, ''Bits ]
|
|
|
|
| otherwise
|
|
|
|
= map TH.conT [ ''Eq, ''Ord, ''Storable ]
|
|
|
|
derivClause :: TH.Q TH.DerivClause
|
|
|
|
derivClause = TH.derivClause ( Just TH.NewtypeStrategy ) classes
|
2021-02-07 23:07:14 +00:00
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
newtypeDecl <-
|
2021-04-09 14:18:00 +00:00
|
|
|
#if MIN_VERSION_template_haskell(2,18,0)
|
2021-02-05 20:57:17 +00:00
|
|
|
( if null docs
|
|
|
|
then TH.newtypeD
|
|
|
|
else
|
|
|
|
\ ctx name bndrs kd con derivs ->
|
2021-04-09 14:18:00 +00:00
|
|
|
TH.newtypeD_doc ctx name ( fmap pure bndrs ) ( fmap pure kd ) ( con, Nothing, [] ) derivs
|
|
|
|
( Just . Text.unpack . Text.unlines . coerce $ docs )
|
2021-02-05 20:57:17 +00:00
|
|
|
)
|
|
|
|
#else
|
|
|
|
TH.newtypeD
|
|
|
|
#endif
|
|
|
|
( pure [] ) tyName [] Nothing newtypeCon [ derivClause ]
|
|
|
|
|
|
|
|
mbAddFiniteEnumInst <-
|
|
|
|
if hasExplicitCount
|
|
|
|
then do
|
|
|
|
finiteEnumInst <-
|
|
|
|
TH.instanceD ( pure [] ) ( TH.appT ( TH.conT finiteEnumName ) ( TH.conT tyName ) )
|
|
|
|
[ TH.tySynInstD ( TH.TySynEqn Nothing
|
|
|
|
<$> TH.appT ( TH.conT countName ) ( TH.conT tyName )
|
|
|
|
<*> TH.litT ( TH.numTyLit enumSize )
|
|
|
|
)
|
2021-08-30 17:08:23 +00:00
|
|
|
]
|
2021-02-05 20:57:17 +00:00
|
|
|
pure ( finiteEnumInst : )
|
|
|
|
else pure id
|
|
|
|
|
2021-08-30 17:08:23 +00:00
|
|
|
synonyms <- for patterns \ ( patternName, patternValue, CommentText _patDoc ) -> do
|
2021-02-05 20:57:17 +00:00
|
|
|
let
|
|
|
|
patNameStr :: String
|
|
|
|
patNameStr = Text.unpack patternName
|
|
|
|
patName <- TH.newName patNameStr
|
|
|
|
patSynSig <- TH.patSynSigD patName ( TH.conT tyName )
|
|
|
|
pat <-
|
2021-04-09 14:18:00 +00:00
|
|
|
#if MIN_VERSION_template_haskell(2,18,0)
|
2021-08-30 17:08:23 +00:00
|
|
|
( if Text.null _patDoc
|
2021-02-05 20:57:17 +00:00
|
|
|
then TH.patSynD
|
|
|
|
else
|
|
|
|
\ nm args dir pat ->
|
|
|
|
TH.patSynD_doc nm args dir pat
|
2021-04-09 14:18:00 +00:00
|
|
|
( Just $ Text.unpack patDoc ) []
|
2021-02-05 20:57:17 +00:00
|
|
|
)
|
|
|
|
#else
|
|
|
|
TH.patSynD
|
|
|
|
#endif
|
|
|
|
patName ( TH.prefixPatSyn [] ) TH.implBidir
|
|
|
|
( TH.conP conName [ TH.litP $ TH.integerL patternValue ] )
|
|
|
|
pure ( patSynSig, pat )
|
|
|
|
|
|
|
|
pure ( newtypeDecl : mbAddFiniteEnumInst ( unpairs synonyms ) )
|
|
|
|
|
|
|
|
unpairs :: [ ( a, a ) ] -> [ a ]
|
|
|
|
unpairs [] = []
|
|
|
|
unpairs ( ( x, y ) : as ) = x : y : unpairs as
|