module Command.CStructs2MsgHandlers
( cstructs2MsgHandlers
, ErrorCode
)
where
import Data.String.Extra as S ( safeReadFile )
import Command.Result ( Result (..) )
import Data.Location ( Location (..) )
import qualified Language.C.AbsC as C ( TranslationUnit )
import qualified Language.C.ParC as C ( myLexer, pTranslationUnit )
import qualified Language.Trans.CStructs2MsgHandlers as T ( cstructs2MsgHandlers )
cstructs2MsgHandlers :: FilePath
-> IO (Result ErrorCode)
cstructs2MsgHandlers :: String -> IO (Result ErrorCode)
cstructs2MsgHandlers String
fp = do
Either String TranslationUnit
result <- String -> IO (Either String TranslationUnit)
parseCFile String
fp
case TranslationUnit -> Either String String
T.cstructs2MsgHandlers (TranslationUnit -> Either String String)
-> Either String TranslationUnit -> Either String String
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Either String TranslationUnit
result of
Right String
content -> String -> IO ()
putStrLn String
content IO () -> IO (Result ErrorCode) -> IO (Result ErrorCode)
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Result ErrorCode -> IO (Result ErrorCode)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Result ErrorCode
forall a. Result a
Success
Left String
msg -> Result ErrorCode -> IO (Result ErrorCode)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Result ErrorCode -> IO (Result ErrorCode))
-> Result ErrorCode -> IO (Result ErrorCode)
forall a b. (a -> b) -> a -> b
$ ErrorCode -> String -> Location -> Result ErrorCode
forall a. a -> String -> Location -> Result a
Error ErrorCode
ecCStructError String
msg (String -> Location
LocationFile String
fp)
where
parseCFile :: FilePath -> IO (Either String C.TranslationUnit)
parseCFile :: String -> IO (Either String TranslationUnit)
parseCFile String
fp' = do
Either String String
content <- String -> IO (Either String String)
S.safeReadFile String
fp'
Either String TranslationUnit -> IO (Either String TranslationUnit)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String TranslationUnit
-> IO (Either String TranslationUnit))
-> Either String TranslationUnit
-> IO (Either String TranslationUnit)
forall a b. (a -> b) -> a -> b
$ [Token] -> Either String TranslationUnit
C.pTranslationUnit ([Token] -> Either String TranslationUnit)
-> (String -> [Token]) -> String -> Either String TranslationUnit
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [Token]
C.myLexer (String -> Either String TranslationUnit)
-> Either String String -> Either String TranslationUnit
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Either String String
content
type ErrorCode = Int
ecCStructError :: ErrorCode
ecCStructError :: ErrorCode
ecCStructError = ErrorCode
1