enable export of canonical grammars to JSON and YAML

This commit is contained in:
Peter Ljunglöf
2019-02-08 09:10:48 +01:00
parent a0c1da2548
commit 47ac01e4b9
3 changed files with 22 additions and 5 deletions

View File

@@ -36,6 +36,8 @@ exportPGF opts fmt pgf =
case fmt of case fmt of
FmtPGFPretty -> multi "txt" (render . ppPGF) FmtPGFPretty -> multi "txt" (render . ppPGF)
FmtCanonicalGF -> [] -- canon "gf" (render80 . abstract2canonical) FmtCanonicalGF -> [] -- canon "gf" (render80 . abstract2canonical)
FmtCanonicalJson-> []
FmtCanonicalYaml-> []
FmtJavaScript -> multi "js" pgf2js FmtJavaScript -> multi "js" pgf2js
FmtPython -> multi "py" pgf2python FmtPython -> multi "py" pgf2python
FmtHaskell -> multi "hs" (grammar2haskell opts name) FmtHaskell -> multi "hs" (grammar2haskell opts name)

View File

@@ -24,6 +24,7 @@ import Data.Maybe
import qualified Data.Map as Map import qualified Data.Map as Map
import qualified Data.Set as Set import qualified Data.Set as Set
import qualified Data.ByteString.Lazy as BSL import qualified Data.ByteString.Lazy as BSL
import GF.Grammar.CanonicalJSON (encodeJSON, encodeYAML)
import System.FilePath import System.FilePath
import Control.Monad(when,unless,forM_) import Control.Monad(when,unless,forM_)
@@ -48,7 +49,7 @@ mainGFC opts fs = do
compileSourceFiles :: Options -> [FilePath] -> IOE () compileSourceFiles :: Options -> [FilePath] -> IOE ()
compileSourceFiles opts fs = compileSourceFiles opts fs =
do output <- batchCompile opts fs do output <- batchCompile opts fs
exportCncs output exportCanonical output
unless (flag optStopAfterPhase opts == Compile) $ unless (flag optStopAfterPhase opts == Compile) $
linkGrammars opts output linkGrammars opts output
where where
@@ -56,13 +57,15 @@ compileSourceFiles opts fs =
batchCompile' opts fs = do (t,cnc_gr) <- S.batchCompile opts fs batchCompile' opts fs = do (t,cnc_gr) <- S.batchCompile opts fs
return (t,[cnc_gr]) return (t,[cnc_gr])
exportCncs output = exportCanonical (_time, canonical) =
do when (FmtHaskell `elem` ofmts && haskellOption opts HaskellConcrete) $ do when (FmtHaskell `elem` ofmts && haskellOption opts HaskellConcrete) $
mapM_ cnc2haskell (snd output) mapM_ cnc2haskell canonical
when (FmtCanonicalGF `elem` ofmts) $ when (FmtCanonicalGF `elem` ofmts) $
do createDirectoryIfMissing False "canonical" do createDirectoryIfMissing False "canonical"
mapM_ abs2canonical (snd output) mapM_ abs2canonical canonical
mapM_ cnc2canonical (snd output) mapM_ cnc2canonical canonical
when (FmtCanonicalJson `elem` ofmts) $ mapM_ grammar2json canonical
when (FmtCanonicalYaml `elem` ofmts) $ mapM_ grammar2yaml canonical
where where
ofmts = flag optOutputFormats opts ofmts = flag optOutputFormats opts
@@ -79,6 +82,14 @@ compileSourceFiles opts fs =
mapM_ (writeExport.fmap render80) $ mapM_ (writeExport.fmap render80) $
concretes2canonical opts (srcAbsName gr cnc) gr concretes2canonical opts (srcAbsName gr cnc) gr
grammar2json (cnc,gr) = encodeJSON (render absname ++ ".json") gr_canon
where absname = srcAbsName gr cnc
gr_canon = grammar2canonical opts absname gr
grammar2yaml (cnc,gr) = encodeYAML (render absname ++ ".yaml") gr_canon
where absname = srcAbsName gr cnc
gr_canon = grammar2canonical opts absname gr
writeExport (path,s) = writing opts path $ writeUTF8File path s writeExport (path,s) = writing opts path $ writeUTF8File path s

View File

@@ -88,6 +88,8 @@ data Phase = Preproc | Convert | Compile | Link
data OutputFormat = FmtPGFPretty data OutputFormat = FmtPGFPretty
| FmtCanonicalGF | FmtCanonicalGF
| FmtCanonicalJson
| FmtCanonicalYaml
| FmtJavaScript | FmtJavaScript
| FmtPython | FmtPython
| FmtHaskell | FmtHaskell
@@ -470,6 +472,8 @@ outputFormatsExpl :: [((String,OutputFormat),String)]
outputFormatsExpl = outputFormatsExpl =
[(("pgf_pretty", FmtPGFPretty),"human-readable pgf"), [(("pgf_pretty", FmtPGFPretty),"human-readable pgf"),
(("canonical_gf", FmtCanonicalGF),"Canonical GF source files"), (("canonical_gf", FmtCanonicalGF),"Canonical GF source files"),
(("canonical_json", FmtCanonicalJson),"Canonical JSON source files"),
(("canonical_yaml", FmtCanonicalYaml),"Canonical YAML source files"),
(("js", FmtJavaScript),"JavaScript (whole grammar)"), (("js", FmtJavaScript),"JavaScript (whole grammar)"),
(("python", FmtPython),"Python (whole grammar)"), (("python", FmtPython),"Python (whole grammar)"),
(("haskell", FmtHaskell),"Haskell (abstract syntax)"), (("haskell", FmtHaskell),"Haskell (abstract syntax)"),