diff --git a/eo-phi-normalizer/Setup.hs b/eo-phi-normalizer/Setup.hs index 63712bcfc..69c89b45f 100644 --- a/eo-phi-normalizer/Setup.hs +++ b/eo-phi-normalizer/Setup.hs @@ -32,7 +32,7 @@ -- for the parsers included in Ogma. module Main (main) where -import Control.Exception (evaluate) +import Control.Exception (SomeException, catch, displayException, evaluate) import Data.ByteString as BS (readFile, writeFile) import Data.List (intercalate) import Data.Text (Text) @@ -41,7 +41,7 @@ import Distribution.Simple (defaultMainWithHooks, hookedPrograms, postConf, preB import Distribution.Simple.Program (Program (..), findProgramVersion, simpleProgram) import Main.Utf8 (withUtf8) import PyF (fmt) -import System.Exit (ExitCode (..)) +import System.Exit (ExitCode (..), exitWith) import System.IO.CodePage (withCP65001) import System.Process (callCommand) @@ -51,12 +51,22 @@ readFile' = (decodeUtf8 <$>) . BS.readFile writeFile' :: FilePath -> Text -> IO () writeFile' path = BS.writeFile path . encodeUtf8 +withCorrectLocale :: IO a -> IO a +withCorrectLocale act = do + let withCorrectLocale' = withCP65001 . Main.Utf8.withUtf8 + withCorrectLocale' act + `catch` ( \(x :: SomeException) -> + withCorrectLocale' do + putStrLn (displayException x) + exitWith (ExitFailure 1) + ) + -- | Run BNFC, happy, and alex on the grammar before the actual build step. -- -- All options for bnfc are hard-coded here. main :: IO () main = - withCP65001 . withUtf8 $ + withCorrectLocale $ defaultMainWithHooks $ simpleUserHooks { hookedPrograms = [bnfcProgram] diff --git a/eo-phi-normalizer/app/Main.hs b/eo-phi-normalizer/app/Main.hs index 66815f044..4c49185fb 100644 --- a/eo-phi-normalizer/app/Main.hs +++ b/eo-phi-normalizer/app/Main.hs @@ -51,7 +51,7 @@ module Main (main) where -import Control.Exception (Exception (..), SomeException, catch, throwIO) +import Control.Exception (Exception (..), SomeException, catch, throwIO, displayException) import Control.Lens.Lens ((&)) import Control.Lens.Operators ((?~)) import Control.Monad (forM, unless, when) @@ -89,6 +89,7 @@ import Options.Applicative qualified as Optparse (metavar) import Paths_eo_phi_normalizer (version) import PyF (fmt, fmtTrim) import System.Directory (createDirectoryIfMissing, doesFileExist) +import System.Exit (ExitCode (ExitFailure), exitWith) import System.FilePath (takeDirectory) import System.IO (IOMode (WriteMode), getContents', hFlush, hPutStr, hPutStrLn, openFile, stdout) import System.IO.CodePage (withCP65001) @@ -560,10 +561,20 @@ wrapRawBytesIn = \case obj@MetaTailContext{} -> obj obj@MetaFunction{} -> obj +withCorrectLocale :: IO a -> IO a +withCorrectLocale act = do + let withCorrectLocale' = withCP65001 . Main.Utf8.withUtf8 + withCorrectLocale' act + `catch` ( \(x :: SomeException) -> + withCorrectLocale' do + putStrLn (displayException x) + exitWith (ExitFailure 1) + ) + -- * Main main :: IO () -main = (withCP65001 . withUtf8) do +main = withCorrectLocale do opts <- customExecParser pprefs (cliOpts (showVersion version)) let printAsProgramOrAsObject = \case Formation bindings' -> printTree $ Program bindings' diff --git a/eo-phi-normalizer/eo-phi-normalizer.cabal b/eo-phi-normalizer/eo-phi-normalizer.cabal index ff41d522c..15260363a 100644 --- a/eo-phi-normalizer/eo-phi-normalizer.cabal +++ b/eo-phi-normalizer/eo-phi-normalizer.cabal @@ -204,7 +204,6 @@ custom-setup , base >=4.11.0.0 && <5.0 , bytestring , code-page - , extra , process >=1.6.3.0 , text , with-utf8 @@ -276,6 +275,7 @@ library , template-haskell , text , unordered-containers + , with-utf8 , yaml default-language: Haskell2010 @@ -396,6 +396,7 @@ test-suite doctests , template-haskell , text , unordered-containers + , with-utf8 , yaml default-language: Haskell2010 diff --git a/eo-phi-normalizer/package.yaml b/eo-phi-normalizer/package.yaml index fa150bbb9..082775e08 100644 --- a/eo-phi-normalizer/package.yaml +++ b/eo-phi-normalizer/package.yaml @@ -91,6 +91,7 @@ dependencies: - unordered-containers - containers - code-page + - with-utf8 default-extensions: - ImportQualifiedPost