mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-27 11:48:55 -06:00
writePGF et al. functions return path[s] of written files
This commit is contained in:
@@ -30,7 +30,7 @@ import Data.Time(UTCTime)
|
|||||||
import qualified Data.ByteString.Lazy as BSL
|
import qualified Data.ByteString.Lazy as BSL
|
||||||
import GF.Grammar.CanonicalJSON (encodeJSON)
|
import GF.Grammar.CanonicalJSON (encodeJSON)
|
||||||
import System.FilePath
|
import System.FilePath
|
||||||
import Control.Monad(when,unless,forM_)
|
import Control.Monad(when,unless,forM,void)
|
||||||
|
|
||||||
-- | Compile the given GF grammar files. The result is a number of @.gfo@ files
|
-- | Compile the given GF grammar files. The result is a number of @.gfo@ files
|
||||||
-- and, depending on the options, a @.pgf@ file. (@gf -batch@, @gf -make@)
|
-- and, depending on the options, a @.pgf@ file. (@gf -batch@, @gf -make@)
|
||||||
@@ -100,7 +100,7 @@ compileSourceFiles opts fs =
|
|||||||
linkGrammars :: Options -> (UTCTime,[(ModuleName, Grammar)]) -> IOE ()
|
linkGrammars :: Options -> (UTCTime,[(ModuleName, Grammar)]) -> IOE ()
|
||||||
linkGrammars opts (_,cnc_grs) | FmtLPGF `elem` flag optOutputFormats opts = do
|
linkGrammars opts (_,cnc_grs) | FmtLPGF `elem` flag optOutputFormats opts = do
|
||||||
lpgf <- linkl opts (head cnc_grs)
|
lpgf <- linkl opts (head cnc_grs)
|
||||||
writeLPGF opts lpgf
|
void $ writeLPGF opts lpgf
|
||||||
linkGrammars opts (t_src,~cnc_grs@(~(cnc,gr):_)) =
|
linkGrammars opts (t_src,~cnc_grs@(~(cnc,gr):_)) =
|
||||||
do let abs = render (srcAbsName gr cnc)
|
do let abs = render (srcAbsName gr cnc)
|
||||||
pgfFile = outputPath opts (grammarName' opts abs<.>"pgf")
|
pgfFile = outputPath opts (grammarName' opts abs<.>"pgf")
|
||||||
@@ -153,7 +153,7 @@ unionPGFFiles opts fs =
|
|||||||
pgfFile = outputPath opts (grammarName opts pgf <.> "pgf")
|
pgfFile = outputPath opts (grammarName opts pgf <.> "pgf")
|
||||||
if pgfFile `elem` fs
|
if pgfFile `elem` fs
|
||||||
then putStrLnE $ "Refusing to overwrite " ++ pgfFile
|
then putStrLnE $ "Refusing to overwrite " ++ pgfFile
|
||||||
else writePGF opts pgf
|
else void $ writePGF opts pgf
|
||||||
writeOutputs opts pgf
|
writeOutputs opts pgf
|
||||||
|
|
||||||
readPGFVerbose f =
|
readPGFVerbose f =
|
||||||
@@ -170,32 +170,39 @@ writeOutputs opts pgf = do
|
|||||||
-- | Write the result of compiling a grammar (e.g. with 'compileToPGF' or
|
-- | Write the result of compiling a grammar (e.g. with 'compileToPGF' or
|
||||||
-- 'link') to a @.pgf@ file.
|
-- 'link') to a @.pgf@ file.
|
||||||
-- A split PGF file is output if the @-split-pgf@ option is used.
|
-- A split PGF file is output if the @-split-pgf@ option is used.
|
||||||
writePGF :: Options -> PGF -> IOE ()
|
writePGF :: Options -> PGF -> IOE [FilePath]
|
||||||
writePGF opts pgf =
|
writePGF opts pgf =
|
||||||
if flag optSplitPGF opts then writeSplitPGF else writeNormalPGF
|
if flag optSplitPGF opts then writeSplitPGF else writeNormalPGF
|
||||||
where
|
where
|
||||||
writeNormalPGF =
|
writeNormalPGF =
|
||||||
do let outfile = outputPath opts (grammarName opts pgf <.> "pgf")
|
do let outfile = outputPath opts (grammarName opts pgf <.> "pgf")
|
||||||
writing opts outfile $ encodeFile outfile pgf
|
writing opts outfile $ encodeFile outfile pgf
|
||||||
|
return [outfile]
|
||||||
|
|
||||||
writeSplitPGF =
|
writeSplitPGF =
|
||||||
do let outfile = outputPath opts (grammarName opts pgf <.> "pgf")
|
do let outfile = outputPath opts (grammarName opts pgf <.> "pgf")
|
||||||
writing opts outfile $ BSL.writeFile outfile (runPut (putSplitAbs pgf))
|
writing opts outfile $ BSL.writeFile outfile (runPut (putSplitAbs pgf))
|
||||||
--encodeFile_ outfile (putSplitAbs pgf)
|
--encodeFile_ outfile (putSplitAbs pgf)
|
||||||
forM_ (Map.toList (concretes pgf)) $ \cnc -> do
|
outfiles <- forM (Map.toList (concretes pgf)) $ \cnc -> do
|
||||||
let outfile = outputPath opts (showCId (fst cnc) <.> "pgf_c")
|
let outfile = outputPath opts (showCId (fst cnc) <.> "pgf_c")
|
||||||
writing opts outfile $ encodeFile outfile cnc
|
writing opts outfile $ encodeFile outfile cnc
|
||||||
|
return outfile
|
||||||
|
|
||||||
writeLPGF :: Options -> LPGF -> IOE ()
|
return (outfile:outfiles)
|
||||||
|
|
||||||
|
writeLPGF :: Options -> LPGF -> IOE FilePath
|
||||||
writeLPGF opts lpgf = do
|
writeLPGF opts lpgf = do
|
||||||
let
|
let
|
||||||
grammarName = fromMaybe (showCId (LPGF.abstractName lpgf)) (flag optName opts)
|
grammarName = fromMaybe (showCId (LPGF.abstractName lpgf)) (flag optName opts)
|
||||||
outfile = outputPath opts (grammarName <.> "lpgf")
|
outfile = outputPath opts (grammarName <.> "lpgf")
|
||||||
writing opts outfile $ liftIO $ LPGF.encodeFile outfile lpgf
|
writing opts outfile $ liftIO $ LPGF.encodeFile outfile lpgf
|
||||||
|
return outfile
|
||||||
|
|
||||||
writeOutput :: Options -> FilePath-> String -> IOE ()
|
writeOutput :: Options -> FilePath-> String -> IOE FilePath
|
||||||
writeOutput opts file str = writing opts path $ writeUTF8File path str
|
writeOutput opts file str = do
|
||||||
where path = outputPath opts file
|
let outfile = outputPath opts file
|
||||||
|
writing opts outfile $ writeUTF8File outfile str
|
||||||
|
return outfile
|
||||||
|
|
||||||
-- * Useful helper functions
|
-- * Useful helper functions
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user