mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-01 07:12:50 -06:00
Because the prompt included the name of the abstract syntax, the loading of the PGF was forced even if -retain was used. Even worse, if an error occured while loading the PGF, it was repeated and caught every time the prompt was printed, creating an infite loop. The solution is to not print the name of the abstract syntax when the grammar is imported with -retain, which is the way things were before anyway.
62 lines
2.2 KiB
Haskell
62 lines
2.2 KiB
Haskell
module GF.Command.Importing (importGrammar, importSource) where
|
|
|
|
import PGF
|
|
import PGF.Internal(optimizePGF,unionPGF,msgUnionPGF)
|
|
|
|
import GF.Compile
|
|
import GF.Compile.Multi (readMulti)
|
|
import GF.Compile.GetGrammar (getCFRules, getEBNFRules)
|
|
import GF.Grammar (SourceGrammar) -- for cc command
|
|
import GF.Grammar.CFG
|
|
import GF.Grammar.EBNF
|
|
import GF.Compile.CFGtoPGF
|
|
import GF.Infra.UseIO(die,tryIOE,useIOE)
|
|
import GF.Infra.Option
|
|
import GF.Data.ErrM
|
|
|
|
import System.FilePath
|
|
import qualified Data.Set as Set
|
|
|
|
-- 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 getCFRules id
|
|
".ebnf" -> importCF opts files getEBNFRules ebnf2cf
|
|
".gfm" -> do
|
|
ascss <- mapM readMulti files
|
|
let cs = concatMap snd ascss
|
|
importGrammar pgf0 opts cs
|
|
s | elem s [".gf",".gfo"] -> do
|
|
res <- tryIOE $ 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 :: Options -> [FilePath] -> IO SourceGrammar
|
|
importSource opts files = fmap (snd.snd) (batchCompile opts files)
|
|
|
|
-- for different cf formats
|
|
importCF opts files get convert = impCF
|
|
where
|
|
impCF = do
|
|
rules <- fmap (convert . concat) $ mapM (get opts) files
|
|
startCat <- case rules of
|
|
(CFRule cat _ _ : _) -> return cat
|
|
_ -> fail "empty CFG"
|
|
let pgf = cf2pgf (last files) (uniqueFuns (mkCFG startCat Set.empty rules))
|
|
probs <- maybe (return . defaultProbabilities) readProbabilitiesFromFile (flag optProbsFile opts) pgf
|
|
return $ setProbabilities probs
|
|
$ if flag optOptimizePGF opts then optimizePGF pgf else pgf
|