summaryrefslogtreecommitdiff
path: root/src-3.0/GF/Compile
diff options
context:
space:
mode:
authorbjorn <bjorn@bringert.net>2008-06-17 12:29:11 +0000
committerbjorn <bjorn@bringert.net>2008-06-17 12:29:11 +0000
commitd13b32ea48d255c71670321c9b8d0d611afe0a17 (patch)
tree0e4a98d507eb02f7c4e83815f66667d273d36a16 /src-3.0/GF/Compile
parent2cf7a7d07eaa394c56ca020f7383ba747d9374a3 (diff)
Refactor grammar export code.
Diffstat (limited to 'src-3.0/GF/Compile')
-rw-r--r--src-3.0/GF/Compile/Export.hs41
-rw-r--r--src-3.0/GF/Compile/GFCCtoHaskell.hs10
2 files changed, 30 insertions, 21 deletions
diff --git a/src-3.0/GF/Compile/Export.hs b/src-3.0/GF/Compile/Export.hs
index 9abdc6789..f88d6d7ba 100644
--- a/src-3.0/GF/Compile/Export.hs
+++ b/src-3.0/GF/Compile/Export.hs
@@ -15,26 +15,35 @@ import GF.Speech.GSL
import GF.Speech.VoiceXML
import GF.Text.UTF8
+import Data.Maybe
+import System.FilePath
+
-- top-level access to code generation
-prPGF :: Options
- -> OutputFormat
- -> PGF
- -> String -- ^ Output name, for example used for generated Haskell
- -- module name.
- -> String
-prPGF opts fmt gr name = case fmt of
- FmtPGF -> printPGF gr
- FmtJavaScript -> pgf2js gr
- FmtHaskell -> grammar2haskell gr name
- FmtHaskell_GADT -> grammar2haskellGADT gr name
- FmtBNF -> prCFG $ pgfToCFG gr (outputConcr gr)
- FmtSRGS_XML -> srgsXmlPrinter (flag optSISR opts) gr (outputConcr gr)
- FmtJSGF -> jsgfPrinter (flag optSISR opts) gr (outputConcr gr)
- FmtGSL -> gslPrinter gr (outputConcr gr)
- FmtVoiceXML -> grammar2vxml gr (outputConcr gr)
+exportPGF :: Options
+ -> OutputFormat
+ -> PGF
+ -> [(FilePath,String)] -- ^ List of recommended file names and contents.
+exportPGF opts fmt pgf =
+ case fmt of
+ FmtPGF -> multi "pgf" printPGF
+ FmtJavaScript -> multi "js" pgf2js
+ FmtHaskell -> multi "hs" (grammar2haskell name)
+ FmtHaskell_GADT -> multi "hs" (grammar2haskellGADT name)
+ FmtBNF -> single "bnf" bnfPrinter
+ FmtSRGS_XML -> single "grxml" (srgsXmlPrinter sisr)
+ FmtJSGF -> single "jsgf" (jsgfPrinter sisr)
+ FmtGSL -> single "gsl" gslPrinter
+ FmtVoiceXML -> single "vxml" grammar2vxml
+ 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.
-- FIXME: there should be an option to change this.
diff --git a/src-3.0/GF/Compile/GFCCtoHaskell.hs b/src-3.0/GF/Compile/GFCCtoHaskell.hs
index 31f1dc0b3..9d03aa490 100644
--- a/src-3.0/GF/Compile/GFCCtoHaskell.hs
+++ b/src-3.0/GF/Compile/GFCCtoHaskell.hs
@@ -27,15 +27,15 @@ import Data.List --(isPrefixOf, find, intersperse)
import qualified Data.Map as Map
-- | the main function
-grammar2haskell :: PGF
- -> String -- ^ Module name.
+grammar2haskell :: String -- ^ Module name.
+ -> PGF
-> String
-grammar2haskell gr name = encodeUTF8 $ foldr (++++) [] $
+grammar2haskell name gr = encodeUTF8 $ foldr (++++) [] $
haskPreamble name ++ [datatypes gr', gfinstances gr']
where gr' = hSkeleton gr
-grammar2haskellGADT :: PGF -> String -> String
-grammar2haskellGADT gr name = encodeUTF8 $ foldr (++++) [] $
+grammar2haskellGADT :: String -> PGF -> String
+grammar2haskellGADT name gr = encodeUTF8 $ foldr (++++) [] $
["{-# OPTIONS_GHC -fglasgow-exts #-}"] ++
haskPreamble name ++ [datatypesGADT gr', gfinstances gr']
where gr' = hSkeleton gr