2021-02-05 20:57:17 +00:00
|
|
|
{-# LANGUAGE BlockArguments #-}
|
|
|
|
{-# LANGUAGE DerivingStrategies #-}
|
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TemplateHaskellQuotes #-}
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
|
|
|
|
|
|
|
module DearImGui.Generator.Parser
|
|
|
|
( CustomParseError(..)
|
|
|
|
, headers
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
-- base
|
|
|
|
import Control.Applicative
|
|
|
|
( (<|>), many, optional, some )
|
|
|
|
import Control.Monad
|
|
|
|
( void )
|
|
|
|
import Data.Bits
|
|
|
|
( Bits(shiftL) )
|
|
|
|
import Data.Char
|
|
|
|
( isSpace, toLower )
|
|
|
|
import Data.Either
|
|
|
|
( partitionEithers )
|
|
|
|
import Data.Functor
|
|
|
|
( ($>) )
|
|
|
|
import Data.Int
|
|
|
|
( Int64 )
|
|
|
|
import Data.Maybe
|
|
|
|
( catMaybes, fromMaybe )
|
|
|
|
import Foreign.C.Types
|
|
|
|
( CInt )
|
|
|
|
|
|
|
|
-- template-haskell
|
|
|
|
import qualified Language.Haskell.TH as TH
|
|
|
|
( Name )
|
|
|
|
|
|
|
|
-- megaparsec
|
|
|
|
import Text.Megaparsec
|
|
|
|
( MonadParsec(..), ShowErrorComponent(..)
|
|
|
|
, (<?>), anySingle, customFailure, single
|
|
|
|
)
|
|
|
|
|
|
|
|
-- parser-combinators
|
|
|
|
import Control.Applicative.Combinators
|
|
|
|
( manyTill, option, sepBy1, skipManyTill )
|
|
|
|
|
|
|
|
-- scientific
|
|
|
|
import Data.Scientific
|
|
|
|
( floatingOrInteger, toBoundedInteger )
|
|
|
|
|
|
|
|
-- text
|
|
|
|
import Data.Text
|
|
|
|
( Text )
|
|
|
|
import qualified Data.Text as Text
|
|
|
|
( all, any, breakOn, drop, dropWhile, dropWhileEnd
|
2023-07-17 15:50:17 +00:00
|
|
|
, length, stripPrefix, unlines, unpack, pack
|
2021-02-05 20:57:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
-- transformers
|
|
|
|
import Control.Monad.Trans.State.Strict
|
|
|
|
( StateT(..)
|
|
|
|
, get, modify'
|
|
|
|
)
|
|
|
|
|
|
|
|
-- unordered-containers
|
|
|
|
import Data.HashMap.Strict
|
|
|
|
( HashMap )
|
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
|
|
|
( fromList, insert, lookup )
|
|
|
|
|
2021-05-08 11:58:25 +00:00
|
|
|
-- dear-imgui-generator
|
2021-02-05 20:57:17 +00:00
|
|
|
import DearImGui.Generator.Tokeniser
|
|
|
|
( Tok(..) )
|
|
|
|
import DearImGui.Generator.Types
|
|
|
|
( Comment(..), Enumeration(..), Headers(..) )
|
|
|
|
|
2023-07-17 15:50:17 +00:00
|
|
|
import qualified Text.Show as Text
|
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Parse error type.
|
|
|
|
|
|
|
|
data CustomParseError
|
|
|
|
= Couldn'tLookupEnumValues
|
|
|
|
{ enumName :: !Text
|
|
|
|
, problems :: ![Text]
|
|
|
|
}
|
|
|
|
| MissingForwardDeclaration
|
2023-07-17 15:50:17 +00:00
|
|
|
{ enumName :: !Text
|
|
|
|
, library :: HashMap Text ( TH.Name, Comment )
|
|
|
|
}
|
2021-02-05 20:57:17 +00:00
|
|
|
| UnexpectedSection
|
|
|
|
{ sectionName :: !Text
|
|
|
|
, problem :: ![Text]
|
|
|
|
}
|
|
|
|
deriving stock ( Show, Eq, Ord )
|
|
|
|
|
|
|
|
instance ShowErrorComponent CustomParseError where
|
|
|
|
showErrorComponent ( Couldn'tLookupEnumValues { enumName, problems } ) = Text.unpack $
|
|
|
|
"Couldn't lookup the following values in enum " <> enumName <> ":\n"
|
|
|
|
<> Text.unlines ( map ( " - " <> ) problems )
|
2023-07-17 15:50:17 +00:00
|
|
|
showErrorComponent ( MissingForwardDeclaration { enumName, library } ) = Text.unpack $
|
|
|
|
"Missing forward declaration for enum named " <> enumName <> "\n"
|
|
|
|
<> "In Library: " <> Text.pack ( Text.show library)
|
2021-02-05 20:57:17 +00:00
|
|
|
showErrorComponent ( UnexpectedSection { sectionName, problem } ) = Text.unpack $
|
|
|
|
"Unexpected section name.\n\
|
|
|
|
\Expected: " <> sectionName <> "\n\
|
|
|
|
\ Actual: " <> Text.unlines ( map ( " " <> ) problem )
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Parsing headers.
|
|
|
|
|
2021-02-07 23:07:14 +00:00
|
|
|
headers :: MonadParsec CustomParseError [Tok] m => m ( Headers () )
|
2021-02-05 20:57:17 +00:00
|
|
|
headers = do
|
|
|
|
_ <- skipManyTill anySingle ( namedSection "Header mess" )
|
2021-02-07 23:07:14 +00:00
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "Forward declarations" )
|
|
|
|
( _structNames, enumNamesAndTypes ) <- forwardDeclarations
|
2021-02-07 23:07:14 +00:00
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "Dear ImGui end-user API functions" )
|
2021-02-07 23:07:14 +00:00
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "Flags & Enumerations" )
|
2021-02-07 23:07:14 +00:00
|
|
|
( _defines, basicEnums ) <- partitionEithers <$>
|
2021-02-05 20:57:17 +00:00
|
|
|
manyTill
|
|
|
|
( ( Left <$> try ignoreDefine )
|
2023-07-17 15:50:17 +00:00
|
|
|
<|> ( Left <$> try cppConditional )
|
2021-02-05 20:57:17 +00:00
|
|
|
<|> ( Right <$> enumeration enumNamesAndTypes )
|
|
|
|
)
|
|
|
|
( namedSection "Helpers: Memory allocations macros, ImVector<>" )
|
2021-02-07 23:07:14 +00:00
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "ImGuiStyle" )
|
2021-02-07 23:07:14 +00:00
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "ImGuiIO" )
|
2021-02-07 23:07:14 +00:00
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "Misc data structures" )
|
2021-02-07 23:07:14 +00:00
|
|
|
|
2023-07-17 15:50:17 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, Math Operators, ImColor)" )
|
2021-02-07 23:07:14 +00:00
|
|
|
|
2021-05-08 11:58:25 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "Drawing API (ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawListFlags, ImDrawList, ImDrawData)" )
|
2021-02-07 23:07:14 +00:00
|
|
|
skipManyTill anySingle ( try . lookAhead $ many comment *> keyword "enum" )
|
|
|
|
drawingEnums <- many ( enumeration enumNamesAndTypes )
|
|
|
|
|
2021-05-08 11:58:25 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "Font API (ImFontConfig, ImFontGlyph, ImFontAtlasFlags, ImFontAtlas, ImFontGlyphRangesBuilder, ImFont)" )
|
2021-02-07 23:07:14 +00:00
|
|
|
skipManyTill anySingle ( try . lookAhead $ many comment *> keyword "enum" )
|
|
|
|
fontEnums <- many ( enumeration enumNamesAndTypes )
|
2021-02-05 20:57:17 +00:00
|
|
|
|
2021-05-08 11:58:25 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "Viewports" )
|
|
|
|
|
2022-02-12 21:26:39 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "Platform Dependent Interfaces" ) -- XXX: since 1.87
|
|
|
|
|
2021-05-08 11:58:25 +00:00
|
|
|
_ <- skipManyTill anySingle ( namedSection "Obsolete functions and types" )
|
|
|
|
|
2021-02-07 23:07:14 +00:00
|
|
|
let
|
|
|
|
enums :: [ Enumeration () ]
|
|
|
|
enums = basicEnums <> drawingEnums <> fontEnums
|
2021-02-05 20:57:17 +00:00
|
|
|
pure ( Headers { enums } )
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Parsing forward declarations.
|
|
|
|
|
|
|
|
forwardDeclarations
|
|
|
|
:: MonadParsec CustomParseError [Tok] m
|
|
|
|
=> m ( HashMap Text Comment, HashMap Text ( TH.Name, Comment ) )
|
|
|
|
forwardDeclarations = do
|
|
|
|
_ <- many comment
|
|
|
|
structs <- many do
|
|
|
|
keyword "struct"
|
|
|
|
structName <- identifier
|
|
|
|
reservedSymbol ';'
|
|
|
|
doc <- comment
|
|
|
|
pure ( structName, doc )
|
|
|
|
_ <- many comment
|
|
|
|
enums <- many do
|
2023-07-17 15:50:17 +00:00
|
|
|
keyword "enum"
|
|
|
|
enumName <- identifier
|
|
|
|
symbol ":"
|
|
|
|
ty <- cTypeName
|
|
|
|
reservedSymbol ';'
|
|
|
|
doc <- commentText <$> comment
|
|
|
|
pure ( enumName, ( ty, CommentText <$> Text.drop 2 . snd $ Text.breakOn "//" doc ) )
|
|
|
|
_ <- many comment
|
|
|
|
typedefs <- many do
|
2021-02-05 20:57:17 +00:00
|
|
|
keyword "typedef"
|
2021-02-07 23:07:14 +00:00
|
|
|
ty <- cTypeName
|
2021-02-05 20:57:17 +00:00
|
|
|
enumName <- identifier
|
|
|
|
reservedSymbol ';'
|
|
|
|
doc <- commentText <$> comment
|
2023-07-17 15:50:17 +00:00
|
|
|
_ <- many comment
|
2021-02-05 20:57:17 +00:00
|
|
|
pure ( enumName, ( ty, CommentText <$> Text.drop 2 . snd $ Text.breakOn "//" doc ) )
|
|
|
|
-- Stopping after simple structs and enums for now.
|
2023-07-17 15:50:17 +00:00
|
|
|
pure ( HashMap.fromList structs, HashMap.fromList (enums <> typedefs) )
|
2021-02-05 20:57:17 +00:00
|
|
|
|
2021-02-07 23:07:14 +00:00
|
|
|
cTypeName :: MonadParsec e [Tok] m => m TH.Name
|
|
|
|
cTypeName = keyword "int" $> ''CInt
|
2021-02-05 20:57:17 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Parsing enumerations.
|
|
|
|
|
|
|
|
data EnumState = EnumState
|
|
|
|
{ enumValues :: HashMap Text Integer
|
|
|
|
, currEnumTag :: Integer
|
|
|
|
, enumSize :: Integer
|
|
|
|
, hasExplicitCount :: Bool
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:07:14 +00:00
|
|
|
enumeration :: MonadParsec CustomParseError [Tok] m => HashMap Text ( TH.Name, Comment ) -> m ( Enumeration () )
|
2021-02-05 20:57:17 +00:00
|
|
|
enumeration enumNamesAndTypes = do
|
2021-02-07 23:07:14 +00:00
|
|
|
inlineDocs <- try do
|
|
|
|
inlineDocs <- many comment
|
|
|
|
keyword "enum"
|
|
|
|
pure inlineDocs
|
2021-02-05 20:57:17 +00:00
|
|
|
fullEnumName <- identifier
|
2023-07-17 15:50:17 +00:00
|
|
|
_ <- try $ (symbol ":" >> cTypeName >> pure ()) <|> pure ()
|
2021-02-05 20:57:17 +00:00
|
|
|
let
|
|
|
|
enumName :: Text
|
|
|
|
enumName = Text.dropWhileEnd ( == '_' ) fullEnumName
|
2021-02-07 23:07:14 +00:00
|
|
|
enumTypeName :: ()
|
|
|
|
enumTypeName = ()
|
|
|
|
( underlyingType, forwardDoc ) <- case HashMap.lookup enumName enumNamesAndTypes of
|
2021-02-05 20:57:17 +00:00
|
|
|
Just res -> pure res
|
2023-07-17 15:50:17 +00:00
|
|
|
Nothing -> customFailure ( MissingForwardDeclaration { enumName, library=enumNamesAndTypes } )
|
2021-02-05 20:57:17 +00:00
|
|
|
let
|
|
|
|
docs :: [Comment]
|
|
|
|
docs = forwardDoc : CommentText "" : inlineDocs
|
|
|
|
reservedSymbol '{'
|
2021-05-08 11:58:25 +00:00
|
|
|
( patterns, EnumState { enumSize, hasExplicitCount } ) <-
|
2021-02-05 20:57:17 +00:00
|
|
|
( `runStateT` EnumState { enumValues = mempty, currEnumTag = 0, enumSize = 0, hasExplicitCount = False } ) $
|
|
|
|
catMaybes
|
|
|
|
<$> many
|
|
|
|
( some ignoredPatternContent $> Nothing
|
|
|
|
<|> enumerationPattern fullEnumName
|
|
|
|
)
|
|
|
|
reservedSymbol '}'
|
|
|
|
reservedSymbol ';'
|
|
|
|
pure ( Enumeration { .. } )
|
|
|
|
|
|
|
|
ignoredPatternContent :: MonadParsec e [Tok] m => m ()
|
|
|
|
ignoredPatternContent = void ( try comment ) <|> cppConditional
|
|
|
|
|
|
|
|
enumerationPattern
|
|
|
|
:: MonadParsec CustomParseError [ Tok ] m
|
|
|
|
=> Text
|
|
|
|
-> StateT EnumState m ( Maybe ( Text, Integer, Comment ) )
|
|
|
|
enumerationPattern enumName = do
|
|
|
|
mbPatNameVal <- patternNameAndValue enumName
|
|
|
|
_ <- optional $ reservedSymbol ','
|
|
|
|
comm <- fromMaybe ( CommentText "" ) <$> optional comment
|
|
|
|
pure $
|
|
|
|
case mbPatNameVal of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just ( patName, patValue ) -> Just ( patName, patValue, comm )
|
|
|
|
|
|
|
|
patternNameAndValue
|
|
|
|
:: forall m
|
|
|
|
. MonadParsec CustomParseError [ Tok ] m
|
|
|
|
=> Text
|
|
|
|
-> StateT EnumState m ( Maybe ( Text, Integer ) )
|
|
|
|
patternNameAndValue enumName =
|
|
|
|
try do
|
|
|
|
sz <- count
|
2021-04-09 14:18:00 +00:00
|
|
|
modify' ( \ ( EnumState {..} ) -> EnumState { enumSize = sz, hasExplicitCount = True, .. } )
|
2021-02-05 20:57:17 +00:00
|
|
|
pure Nothing
|
|
|
|
<|> do
|
|
|
|
pat@( _, val ) <- value
|
2021-04-09 14:18:00 +00:00
|
|
|
modify' ( \ ( EnumState {..} ) -> EnumState { enumSize = enumSize + 1, currEnumTag = val + 1, .. } )
|
2021-02-05 20:57:17 +00:00
|
|
|
pure ( Just pat )
|
|
|
|
where
|
|
|
|
count :: StateT EnumState m Integer
|
|
|
|
count = do
|
2022-02-12 21:26:39 +00:00
|
|
|
let idName = enumName <> "COUNT"
|
|
|
|
_ <- single ( Identifier idName )
|
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
mbVal <- optional do
|
|
|
|
_ <- reservedSymbol '='
|
2022-02-12 21:26:39 +00:00
|
|
|
EnumState{enumValues} <- get
|
|
|
|
integerExpression enumValues
|
|
|
|
|
|
|
|
countVal <- case mbVal of
|
2021-02-05 20:57:17 +00:00
|
|
|
Nothing -> currEnumTag <$> get
|
|
|
|
Just ct -> pure ct
|
2022-02-12 21:26:39 +00:00
|
|
|
|
|
|
|
modify' ( \ st -> st { enumValues = HashMap.insert idName countVal ( enumValues st ) } )
|
|
|
|
pure countVal
|
|
|
|
|
2021-02-05 20:57:17 +00:00
|
|
|
value :: StateT EnumState m ( Text, Integer )
|
|
|
|
value = do
|
|
|
|
name <- identifier
|
|
|
|
val <- patternRHS
|
|
|
|
modify' ( \ st -> st { enumValues = HashMap.insert name val ( enumValues st ) } )
|
|
|
|
pure ( name, val )
|
|
|
|
patternRHS :: StateT EnumState m Integer
|
|
|
|
patternRHS =
|
|
|
|
( do
|
|
|
|
reservedSymbol '='
|
2022-02-12 21:26:39 +00:00
|
|
|
EnumState{enumValues} <- get
|
|
|
|
try disjunction <|> try (integerExpression enumValues)
|
2021-02-05 20:57:17 +00:00
|
|
|
)
|
|
|
|
<|> ( currEnumTag <$> get )
|
|
|
|
|
|
|
|
disjunction :: StateT EnumState m Integer
|
|
|
|
disjunction = do
|
2022-02-12 21:26:39 +00:00
|
|
|
initial <- identifier <* symbol "|"
|
|
|
|
( rest :: [Text] ) <- identifier `sepBy1` symbol "|"
|
|
|
|
let summands = initial : rest
|
2021-02-05 20:57:17 +00:00
|
|
|
valsMap <- enumValues <$> get
|
|
|
|
let
|
|
|
|
res :: Either [ Text ] Integer
|
|
|
|
res = foldr
|
|
|
|
( \ summand errsOrVal -> case HashMap.lookup summand valsMap of
|
|
|
|
Nothing -> case errsOrVal of { Right _ -> Left [ summand ]; Left errs -> Left ( summand : errs ) }
|
|
|
|
Just v -> case errsOrVal of { Right v' -> Right ( v + v' ); Left errs -> Left errs }
|
|
|
|
)
|
|
|
|
( Right 0 )
|
|
|
|
summands
|
|
|
|
case res of
|
|
|
|
Left problems -> customFailure ( Couldn'tLookupEnumValues { enumName, problems } )
|
|
|
|
Right v -> pure v
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Simple token parsers.
|
|
|
|
|
|
|
|
comment :: MonadParsec e [ Tok ] m => m Comment
|
|
|
|
comment = CommentText <$>
|
|
|
|
token ( \ case { Comment comm -> Just comm; _ -> Nothing } ) mempty
|
|
|
|
<?> "comment"
|
|
|
|
|
|
|
|
keyword :: MonadParsec e [ Tok ] m => Text -> m ()
|
|
|
|
keyword kw = token ( \ case { Keyword kw' | kw == kw' -> Just (); _ -> Nothing } ) mempty
|
|
|
|
<?> ( Text.unpack kw <> " (keyword)" )
|
|
|
|
|
|
|
|
identifier :: MonadParsec e [ Tok ] m => m Text
|
|
|
|
identifier = token ( \ case { Identifier i -> Just i; _ -> Nothing } ) mempty
|
|
|
|
<?> "identifier"
|
|
|
|
|
|
|
|
{-
|
|
|
|
prefixedIdentifier :: MonadParsec e [ Tok ] m => Text -> m Text
|
|
|
|
prefixedIdentifier prefix =
|
|
|
|
token
|
|
|
|
( \ case
|
|
|
|
{ Identifier i -> Text.dropWhile ( == '_' ) <$> Text.stripPrefix prefix i
|
|
|
|
; _ -> Nothing
|
|
|
|
}
|
|
|
|
) mempty
|
|
|
|
-}
|
|
|
|
|
|
|
|
reservedSymbol :: MonadParsec e [ Tok ] m => Char -> m ()
|
|
|
|
reservedSymbol s = token ( \ case { ReservedSymbol s' | s == s' -> Just (); _ -> Nothing } ) mempty
|
|
|
|
<?> ( [s] <> " (reserved symbol)" )
|
|
|
|
|
|
|
|
symbol :: MonadParsec e [ Tok ] m => Text -> m ()
|
|
|
|
symbol s = token ( \ case { Symbolic s' | s == s' -> Just (); _ -> Nothing } ) mempty
|
|
|
|
<?> ( Text.unpack s <> " (symbol)" )
|
|
|
|
|
2022-02-12 21:26:39 +00:00
|
|
|
integerExpression :: MonadParsec e [ Tok ] m => HashMap Text Integer -> m Integer
|
|
|
|
integerExpression enums = try integerPower <|> try integerAdd <|> try integerSub <|> integer
|
2021-02-05 20:57:17 +00:00
|
|
|
where
|
2022-02-12 21:26:39 +00:00
|
|
|
integerPower :: MonadParsec e [ Tok ] m => m Integer
|
|
|
|
integerPower = do
|
|
|
|
a <- integer
|
|
|
|
_ <- symbol "<<"
|
|
|
|
i <- integer
|
|
|
|
pure ( a `shiftL` fromIntegral i )
|
|
|
|
|
|
|
|
integerAdd :: MonadParsec e [ Tok ] m => m Integer
|
|
|
|
integerAdd = do
|
|
|
|
a <- integer
|
|
|
|
_ <- symbol "+"
|
|
|
|
i <- integer
|
|
|
|
pure ( a + i )
|
|
|
|
|
|
|
|
integerSub :: MonadParsec e [ Tok ] m => m Integer
|
|
|
|
integerSub = do
|
|
|
|
a <- integer
|
|
|
|
_ <- symbol "-"
|
|
|
|
i <- integer
|
|
|
|
pure ( a - i )
|
|
|
|
|
|
|
|
integer :: forall e m. MonadParsec e [ Tok ] m => m Integer
|
|
|
|
integer =
|
|
|
|
option id mkSign <*>
|
|
|
|
token
|
|
|
|
( \case
|
|
|
|
Number i suff
|
|
|
|
| Just _ <- toBoundedInteger @Int64 i
|
|
|
|
, Right i' <- floatingOrInteger @Float @Integer i
|
|
|
|
, not ( Text.any ( (== 'f' ) . toLower ) suff )
|
|
|
|
->
|
|
|
|
Just i'
|
|
|
|
|
|
|
|
Identifier name ->
|
|
|
|
HashMap.lookup name enums
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Nothing
|
|
|
|
)
|
|
|
|
mempty
|
|
|
|
<?> "integer"
|
|
|
|
where
|
|
|
|
mkSign :: m ( Integer -> Integer )
|
|
|
|
mkSign = ( symbol "+" $> id ) <|> ( symbol "-" $> negate )
|
2021-02-05 20:57:17 +00:00
|
|
|
|
|
|
|
section :: MonadParsec e [ Tok ] m => m [Text]
|
|
|
|
section =
|
|
|
|
do
|
|
|
|
sectionText <- try do
|
|
|
|
separator
|
|
|
|
token
|
|
|
|
( \ case
|
|
|
|
{ Comment txt -> fmap ( Text.dropWhile isSpace )
|
|
|
|
. Text.stripPrefix "[SECTION]"
|
|
|
|
. Text.dropWhile isSpace
|
|
|
|
$ txt
|
|
|
|
; _ -> Nothing
|
|
|
|
}
|
|
|
|
) mempty
|
|
|
|
rest <- endOfSectionHeader
|
|
|
|
pure ( sectionText : filter ( not . Text.all ( \ c -> c == '-' || isSpace c ) ) rest )
|
|
|
|
<?> "section"
|
|
|
|
|
|
|
|
separator :: MonadParsec e [ Tok ] m => m ()
|
|
|
|
separator = token
|
|
|
|
( \ case
|
|
|
|
{ Comment hyphens | Text.length hyphens > 10 && Text.all ( == '-') hyphens -> Just ()
|
|
|
|
; _ -> Nothing
|
|
|
|
}
|
|
|
|
) mempty
|
|
|
|
<?> "separator"
|
|
|
|
|
|
|
|
endOfSectionHeader :: MonadParsec e [ Tok ] m => m [Text]
|
|
|
|
endOfSectionHeader = try ( (:) <$> ( commentText <$> comment ) <*> endOfSectionHeader )
|
|
|
|
<|> ( separator $> [] )
|
|
|
|
|
|
|
|
namedSection :: MonadParsec CustomParseError [ Tok ] m => Text -> m ()
|
|
|
|
namedSection sectionName =
|
|
|
|
do
|
|
|
|
sectionTexts <- section
|
|
|
|
case sectionTexts of
|
|
|
|
sectionText : _
|
|
|
|
| Just _ <- Text.stripPrefix sectionName sectionText
|
|
|
|
-> pure ()
|
|
|
|
_ -> customFailure ( UnexpectedSection { sectionName, problem = sectionTexts } )
|
|
|
|
<?> ( "section named " <> Text.unpack sectionName )
|
|
|
|
|
|
|
|
cppDirective :: MonadParsec e [Tok] m => ( Text -> Maybe a ) -> m a
|
|
|
|
cppDirective f = token ( \case { BeginCPP a -> f a; _ -> Nothing } ) mempty
|
|
|
|
|
|
|
|
cppConditional :: MonadParsec e [Tok] m => m ()
|
|
|
|
cppConditional = do
|
|
|
|
void $ cppDirective ( \case { "ifdef" -> Just True; "ifndef" -> Just False; _ -> Nothing } )
|
|
|
|
-- assumes no nesting
|
|
|
|
void $ skipManyTill anySingle ( cppDirective ( \case { "endif" -> Just (); _ -> Nothing } ) )
|
|
|
|
void $ skipManyTill anySingle ( single EndCPPLine )
|
|
|
|
|
|
|
|
ignoreDefine :: MonadParsec e [Tok] m => m ()
|
|
|
|
ignoreDefine = do
|
|
|
|
void $ many comment
|
|
|
|
void $ cppDirective ( \case { "define" -> Just (); _ -> Nothing } )
|
|
|
|
void $ skipManyTill anySingle ( single EndCPPLine )
|