From 260c0dfedcd7e50368bcbd651f4c71d425af244a Mon Sep 17 00:00:00 2001 From: hallgren Date: Thu, 21 Nov 2013 16:27:26 +0000 Subject: [PATCH] Some refactoring in GF.Compile and GF.Compile.GetGrammar Move source transcoding function GF.Compile to GF.Compile.GetGrammar, in preparation for doing transcoding before lexing. --- src/compiler/GF/Compile.hs | 9 ++------ src/compiler/GF/Compile/GetGrammar.hs | 32 +++++++++++++++++---------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/compiler/GF/Compile.hs b/src/compiler/GF/Compile.hs index 92c2ac415..0e29192c6 100644 --- a/src/compiler/GF/Compile.hs +++ b/src/compiler/GF/Compile.hs @@ -13,7 +13,6 @@ import GF.Compile.GrammarToPGF import GF.Compile.ReadFiles import GF.Compile.Update --import GF.Compile.Refresh -import GF.Compile.Coding import GF.Compile.Tags import GF.Grammar.Grammar @@ -27,7 +26,6 @@ import GF.Infra.CheckM import GF.Data.Operations import Control.Monad -import System.IO import GF.System.Directory import System.FilePath import qualified Data.Map as Map @@ -164,11 +162,8 @@ compileOne opts env@(_,srcgr,_) file = do then compileOne opts env $ (gf2gfo opts file) else do - sm00 <- putpOpt ("- parsing" +++ file) ("- compiling" +++ file ++ "... ") $ - getSourceModule opts file - enc <- liftIO $ mkTextEncoding (renameEncoding (flag optEncoding (mflags (snd sm00)))) - let sm = decodeStringsInModule enc sm00 - + sm <- putpOpt ("- parsing" +++ file) ("- compiling" +++ file ++ "... ") + $ getSourceModule opts file intermOut opts (Dump Source) (ppModule Internal sm) compileSourceModule opts env (Just file) sm diff --git a/src/compiler/GF/Compile/GetGrammar.hs b/src/compiler/GF/Compile/GetGrammar.hs index 17477f49a..2f40d0242 100644 --- a/src/compiler/GF/Compile/GetGrammar.hs +++ b/src/compiler/GF/Compile/GetGrammar.hs @@ -20,31 +20,39 @@ import GF.Data.Operations import GF.System.Catch import GF.Infra.UseIO -import GF.Infra.Option(Options,optPreprocessors,addOptions,flag) +import GF.Infra.Option(Options,optPreprocessors,addOptions,optEncoding,flag,renameEncoding) import GF.Grammar.Lexer import GF.Grammar.Parser import GF.Grammar.Grammar +import GF.Compile.Coding ---import GF.Compile.ReadFiles - ---import Data.Char (toUpper) import qualified Data.ByteString.Char8 as BS import Control.Monad (foldM) import System.Cmd (system) +import System.IO(mkTextEncoding) import System.Directory(removeFile) getSourceModule :: Options -> FilePath -> IOE SourceModule getSourceModule opts file0 = ioe $ - do tmp <- foldM runPreprocessor (Source file0) (flag optPreprocessors opts) - content <- keepTemp tmp - case runP pModDef content of - Left (Pn l c,msg) -> do file <- writeTemp tmp - let location = file++":"++show l++":"++show c - return (Bad (location++":\n "++msg)) - Right (i,mi) -> do removeTemp tmp - return (Ok (i,mi{mflags=mflags mi `addOptions` opts, msrc=file0})) + do tmp <- foldM runPreprocessor (Source file0) (flag optPreprocessors opts) + content <- keepTemp tmp + case runP pModDef content of + Left (Pn l c,msg) -> do file <- writeTemp tmp + let location = file++":"++show l++":"++show c + return (Bad (location++":\n "++msg)) + Right (i,mi00) -> + do removeTemp tmp + let mi0 =mi00 {mflags=mflags mi00 `addOptions` opts, msrc=file0} + mi <- transcodeModule (i,mi0) + return (Ok mi) `catch` (return . Bad . show) +transcodeModule sm00 = + do enc <- mkTextEncoding (renameEncoding (flag optEncoding (mflags (snd sm00)))) + let sm = decodeStringsInModule enc sm00 + return sm + + runPreprocessor :: Temporary -> String -> IO Temporary runPreprocessor tmp0 p = maybe external internal (lookup p builtin_preprocessors)