mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-03 00:02:50 -06:00
+ References to modules under src/compiler have been eliminated from the PGF library (under src/runtime/haskell). Only two functions had to be moved (from GF.Data.Utilities to PGF.Utilities) to make this possible, other apparent dependencies turned out to be vacuous. + In gf.cabal, the GF executable no longer directly depends on the PGF library source directory, but only on the exposed library modules. This means that there is less duplication in gf.cabal and that the 30 modules in the PGF library will no longer be compiled twice while building GF. To make this possible, additional PGF library modules have been exposed, even though they should probably be considered for internal use only. They could be collected in a PGF.Internal module, or marked as "unstable", to make this explicit. + Also, by using the -fwarn-unused-imports flag, ~220 redundant imports were found and removed, reducing the total number of imports by ~15%.
65 lines
2.0 KiB
Haskell
65 lines
2.0 KiB
Haskell
module GF.Command.Importing (importGrammar, importSource) where
|
|
|
|
import PGF
|
|
import PGF.Data
|
|
|
|
import GF.Compile
|
|
import GF.Compile.Multi (readMulti)
|
|
import GF.Grammar (identS, SourceGrammar) -- for cc command
|
|
import GF.Grammar.CF
|
|
import GF.Grammar.EBNF
|
|
import GF.Infra.UseIO
|
|
import GF.Infra.Option
|
|
import GF.Data.ErrM
|
|
|
|
--import Data.List (nubBy)
|
|
import System.FilePath
|
|
|
|
-- import a grammar in an environment where it extends an existing grammar
|
|
importGrammar :: PGF -> Options -> [FilePath] -> IO PGF
|
|
importGrammar pgf0 _ [] = return pgf0
|
|
importGrammar pgf0 opts files =
|
|
case takeExtensions (last files) of
|
|
".cf" -> importCF opts files getCF
|
|
".ebnf" -> importCF opts files getEBNF
|
|
".gfm" -> do
|
|
ascss <- mapM readMulti files
|
|
let cs = concatMap snd ascss
|
|
importGrammar pgf0 opts cs
|
|
s | elem s [".gf",".gfo"] -> do
|
|
res <- appIOE $ compileToPGF opts files
|
|
case res of
|
|
Ok pgf2 -> ioUnionPGF pgf0 pgf2
|
|
Bad msg -> do putStrLn ('\n':'\n':msg)
|
|
return pgf0
|
|
".pgf" -> do
|
|
pgf2 <- mapM readPGF files >>= return . foldl1 unionPGF
|
|
ioUnionPGF pgf0 pgf2
|
|
ext -> die $ "Unknown filename extension: " ++ show ext
|
|
|
|
ioUnionPGF :: PGF -> PGF -> IO PGF
|
|
ioUnionPGF one two = case msgUnionPGF one two of
|
|
(pgf, Just msg) -> putStrLn msg >> return pgf
|
|
(pgf,_) -> return pgf
|
|
|
|
importSource :: SourceGrammar -> Options -> [FilePath] -> IO SourceGrammar
|
|
importSource src0 opts files = do
|
|
src <- appIOE $ batchCompile opts files
|
|
case src of
|
|
Ok gr -> return gr
|
|
Bad msg -> do
|
|
putStrLn msg
|
|
return src0
|
|
|
|
-- for different cf formats
|
|
importCF opts files get = do
|
|
s <- fmap unlines $ mapM readFile files
|
|
gf <- case get (last files) s of
|
|
Ok gf -> return gf
|
|
Bad s -> error s ----
|
|
Ok gr <- appIOE $ compileSourceGrammar opts gf
|
|
epgf <- appIOE $ link opts (identS (justModuleName (last files) ++ "Abs")) gr
|
|
case epgf of
|
|
Ok pgf -> return pgf
|
|
Bad s -> error s ----
|