mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 11:42:49 -06:00
Refactor grammar export code.
This commit is contained in:
@@ -441,7 +441,7 @@ allCommands pgf = Map.fromList [
|
|||||||
unlines $ [unwords (la:":": map prCId cs) |
|
unlines $ [unwords (la:":": map prCId cs) |
|
||||||
la <- optLangs opts, let cs = missingLins pgf (mkCId la)]
|
la <- optLangs opts, let cs = missingLins pgf (mkCId la)]
|
||||||
_ -> case valIdOpts "printer" "pgf" opts of
|
_ -> case valIdOpts "printer" "pgf" opts of
|
||||||
v -> prPGF noOptions (read v) pgf (prCId (absname pgf))
|
v -> concatMap snd $ exportPGF noOptions (read v) pgf
|
||||||
|
|
||||||
morphos opts s =
|
morphos opts s =
|
||||||
[lookupMorpho (buildMorpho pgf (mkCId la)) s | la <- optLangs opts]
|
[lookupMorpho (buildMorpho pgf (mkCId la)) s | la <- optLangs opts]
|
||||||
|
|||||||
@@ -15,26 +15,35 @@ import GF.Speech.GSL
|
|||||||
import GF.Speech.VoiceXML
|
import GF.Speech.VoiceXML
|
||||||
import GF.Text.UTF8
|
import GF.Text.UTF8
|
||||||
|
|
||||||
|
import Data.Maybe
|
||||||
|
import System.FilePath
|
||||||
|
|
||||||
-- top-level access to code generation
|
-- top-level access to code generation
|
||||||
|
|
||||||
prPGF :: Options
|
exportPGF :: Options
|
||||||
-> OutputFormat
|
-> OutputFormat
|
||||||
-> PGF
|
-> PGF
|
||||||
-> String -- ^ Output name, for example used for generated Haskell
|
-> [(FilePath,String)] -- ^ List of recommended file names and contents.
|
||||||
-- module name.
|
exportPGF opts fmt pgf =
|
||||||
-> String
|
case fmt of
|
||||||
prPGF opts fmt gr name = case fmt of
|
FmtPGF -> multi "pgf" printPGF
|
||||||
FmtPGF -> printPGF gr
|
FmtJavaScript -> multi "js" pgf2js
|
||||||
FmtJavaScript -> pgf2js gr
|
FmtHaskell -> multi "hs" (grammar2haskell name)
|
||||||
FmtHaskell -> grammar2haskell gr name
|
FmtHaskell_GADT -> multi "hs" (grammar2haskellGADT name)
|
||||||
FmtHaskell_GADT -> grammar2haskellGADT gr name
|
FmtBNF -> single "bnf" bnfPrinter
|
||||||
FmtBNF -> prCFG $ pgfToCFG gr (outputConcr gr)
|
FmtSRGS_XML -> single "grxml" (srgsXmlPrinter sisr)
|
||||||
FmtSRGS_XML -> srgsXmlPrinter (flag optSISR opts) gr (outputConcr gr)
|
FmtJSGF -> single "jsgf" (jsgfPrinter sisr)
|
||||||
FmtJSGF -> jsgfPrinter (flag optSISR opts) gr (outputConcr gr)
|
FmtGSL -> single "gsl" gslPrinter
|
||||||
FmtGSL -> gslPrinter gr (outputConcr gr)
|
FmtVoiceXML -> single "vxml" grammar2vxml
|
||||||
FmtVoiceXML -> grammar2vxml gr (outputConcr gr)
|
where
|
||||||
|
name = fromMaybe (prCId (absname pgf)) (moduleFlag optName opts)
|
||||||
|
sisr = flag optSISR opts
|
||||||
|
|
||||||
|
multi :: String -> (PGF -> String) -> [(FilePath,String)]
|
||||||
|
multi ext pr = [(name <.> ext, pr pgf)]
|
||||||
|
|
||||||
|
single :: String -> (PGF -> CId -> String) -> [(FilePath,String)]
|
||||||
|
single ext pr = [(prCId cnc <.> ext, pr pgf cnc) | cnc <- cncnames pgf]
|
||||||
|
|
||||||
-- | Get the name of the concrete syntax to generate output from.
|
-- | Get the name of the concrete syntax to generate output from.
|
||||||
-- FIXME: there should be an option to change this.
|
-- FIXME: there should be an option to change this.
|
||||||
|
|||||||
@@ -27,15 +27,15 @@ import Data.List --(isPrefixOf, find, intersperse)
|
|||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
|
|
||||||
-- | the main function
|
-- | the main function
|
||||||
grammar2haskell :: PGF
|
grammar2haskell :: String -- ^ Module name.
|
||||||
-> String -- ^ Module name.
|
-> PGF
|
||||||
-> String
|
-> String
|
||||||
grammar2haskell gr name = encodeUTF8 $ foldr (++++) [] $
|
grammar2haskell name gr = encodeUTF8 $ foldr (++++) [] $
|
||||||
haskPreamble name ++ [datatypes gr', gfinstances gr']
|
haskPreamble name ++ [datatypes gr', gfinstances gr']
|
||||||
where gr' = hSkeleton gr
|
where gr' = hSkeleton gr
|
||||||
|
|
||||||
grammar2haskellGADT :: PGF -> String -> String
|
grammar2haskellGADT :: String -> PGF -> String
|
||||||
grammar2haskellGADT gr name = encodeUTF8 $ foldr (++++) [] $
|
grammar2haskellGADT name gr = encodeUTF8 $ foldr (++++) [] $
|
||||||
["{-# OPTIONS_GHC -fglasgow-exts #-}"] ++
|
["{-# OPTIONS_GHC -fglasgow-exts #-}"] ++
|
||||||
haskPreamble name ++ [datatypesGADT gr', gfinstances gr']
|
haskPreamble name ++ [datatypesGADT gr', gfinstances gr']
|
||||||
where gr' = hSkeleton gr
|
where gr' = hSkeleton gr
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
--
|
--
|
||||||
-- Approximates PGF grammars with context-free grammars.
|
-- Approximates PGF grammars with context-free grammars.
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
module GF.Speech.PGFToCFG (pgfToCFG) where
|
module GF.Speech.PGFToCFG (bnfPrinter, pgfToCFG) where
|
||||||
|
|
||||||
import PGF.CId
|
import PGF.CId
|
||||||
import PGF.Data as PGF
|
import PGF.Data as PGF
|
||||||
@@ -19,6 +19,8 @@ import Data.Maybe
|
|||||||
import Data.Set (Set)
|
import Data.Set (Set)
|
||||||
import qualified Data.Set as Set
|
import qualified Data.Set as Set
|
||||||
|
|
||||||
|
bnfPrinter :: PGF -> CId -> String
|
||||||
|
bnfPrinter pgf cnc = prCFG $ pgfToCFG pgf cnc
|
||||||
|
|
||||||
pgfToCFG :: PGF
|
pgfToCFG :: PGF
|
||||||
-> CId -- ^ Concrete syntax name
|
-> CId -- ^ Concrete syntax name
|
||||||
|
|||||||
@@ -26,30 +26,17 @@ mainGFC opts fs =
|
|||||||
writeOutputs opts pgf
|
writeOutputs opts pgf
|
||||||
|
|
||||||
writeOutputs :: Options -> PGF -> IOE ()
|
writeOutputs :: Options -> PGF -> IOE ()
|
||||||
writeOutputs opts pgf = mapM_ (\fmt -> writeOutput opts fmt pgf) (flag optOutputFormats opts)
|
writeOutputs opts pgf =
|
||||||
|
sequence_ [writeOutput opts name str
|
||||||
|
| fmt <- flag optOutputFormats opts,
|
||||||
|
(name,str) <- exportPGF opts fmt pgf]
|
||||||
|
|
||||||
writeOutput :: Options -> OutputFormat-> PGF -> IOE ()
|
writeOutput :: Options -> FilePath-> String -> IOE ()
|
||||||
writeOutput opts fmt pgf =
|
writeOutput opts file str =
|
||||||
do let name = fromMaybe (prCId (absname pgf)) (moduleFlag optName opts)
|
do let path = case flag optOutputDir opts of
|
||||||
path = outputFilePath opts fmt name
|
Nothing -> file
|
||||||
s = prPGF opts fmt pgf name
|
Just dir -> dir </> file
|
||||||
writeOutputFile path s
|
writeOutputFile path str
|
||||||
|
|
||||||
outputFilePath :: Options -> OutputFormat -> String -> FilePath
|
|
||||||
outputFilePath opts fmt name0 = addDir name <.> fmtExtension fmt
|
|
||||||
where name = fromMaybe name0 (moduleFlag optName opts)
|
|
||||||
addDir = maybe id (</>) (flag optOutputDir opts)
|
|
||||||
|
|
||||||
fmtExtension :: OutputFormat -> String
|
|
||||||
fmtExtension FmtPGF = "pgf"
|
|
||||||
fmtExtension FmtJavaScript = "js"
|
|
||||||
fmtExtension FmtHaskell = "hs"
|
|
||||||
fmtExtension FmtHaskell_GADT = "hs"
|
|
||||||
fmtExtension FmtBNF = "bnf"
|
|
||||||
fmtExtension FmtSRGS_XML = "grxml"
|
|
||||||
fmtExtension FmtJSGF = "jsgf"
|
|
||||||
fmtExtension FmtGSL = "gsl"
|
|
||||||
fmtExtension FmtVoiceXML = "vxml"
|
|
||||||
|
|
||||||
writeOutputFile :: FilePath -> String -> IOE ()
|
writeOutputFile :: FilePath -> String -> IOE ()
|
||||||
writeOutputFile outfile output = ioeIO $
|
writeOutputFile outfile output = ioeIO $
|
||||||
|
|||||||
Reference in New Issue
Block a user