mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-24 03:52:50 -06:00
added option -output-format=java for producing code for embedded grammars in Java
This commit is contained in:
1
gf.cabal
1
gf.cabal
@@ -195,6 +195,7 @@ Library
|
|||||||
GF.Compile.Multi
|
GF.Compile.Multi
|
||||||
GF.Compile.Optimize
|
GF.Compile.Optimize
|
||||||
GF.Compile.PGFtoHaskell
|
GF.Compile.PGFtoHaskell
|
||||||
|
GF.Compile.PGFtoJava
|
||||||
GF.Haskell
|
GF.Haskell
|
||||||
GF.Compile.ConcreteToHaskell
|
GF.Compile.ConcreteToHaskell
|
||||||
GF.Compile.PGFtoJS
|
GF.Compile.PGFtoJS
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ module GF.Compile.Export where
|
|||||||
import PGF
|
import PGF
|
||||||
import PGF.Internal(ppPGF)
|
import PGF.Internal(ppPGF)
|
||||||
import GF.Compile.PGFtoHaskell
|
import GF.Compile.PGFtoHaskell
|
||||||
|
import GF.Compile.PGFtoJava
|
||||||
import GF.Compile.PGFtoProlog
|
import GF.Compile.PGFtoProlog
|
||||||
import GF.Compile.PGFtoLProlog
|
import GF.Compile.PGFtoLProlog
|
||||||
import GF.Compile.PGFtoJS
|
import GF.Compile.PGFtoJS
|
||||||
@@ -37,6 +38,7 @@ exportPGF opts fmt pgf =
|
|||||||
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)
|
||||||
|
FmtJava -> multi "java" (grammar2java opts name)
|
||||||
FmtProlog -> multi "pl" grammar2prolog
|
FmtProlog -> multi "pl" grammar2prolog
|
||||||
FmtLambdaProlog -> multi "mod" grammar2lambdaprolog_mod ++ multi "sig" grammar2lambdaprolog_sig
|
FmtLambdaProlog -> multi "mod" grammar2lambdaprolog_mod ++ multi "sig" grammar2lambdaprolog_sig
|
||||||
FmtBNF -> single "bnf" bnfPrinter
|
FmtBNF -> single "bnf" bnfPrinter
|
||||||
|
|||||||
44
src/compiler/GF/Compile/PGFtoJava.hs
Normal file
44
src/compiler/GF/Compile/PGFtoJava.hs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
module GF.Compile.PGFtoJava (grammar2java) where
|
||||||
|
|
||||||
|
import PGF
|
||||||
|
import Data.Maybe(maybe)
|
||||||
|
import Data.List(intercalate)
|
||||||
|
import GF.Infra.Option
|
||||||
|
|
||||||
|
-- | the main function
|
||||||
|
grammar2java :: Options
|
||||||
|
-> String -- ^ Module name.
|
||||||
|
-> PGF
|
||||||
|
-> String
|
||||||
|
grammar2java opts name gr = unlines $
|
||||||
|
javaPreamble name ++ methods ++ javaEnding
|
||||||
|
where
|
||||||
|
methods = [javaMethod gr fun | fun <- functions gr]
|
||||||
|
|
||||||
|
javaPreamble name =
|
||||||
|
[
|
||||||
|
"import org.grammaticalframework.pgf.*;",
|
||||||
|
"",
|
||||||
|
"public class " ++ name ++ " {",
|
||||||
|
""
|
||||||
|
]
|
||||||
|
|
||||||
|
javaMethod gr fun =
|
||||||
|
" public static Expr "++name++"("++arg_decls++") { return new Expr("++show name++args++"); }"
|
||||||
|
where
|
||||||
|
name = showCId fun
|
||||||
|
arity = maybe 0 getArrity (functionType gr fun)
|
||||||
|
vars = ['e':show i | i <- [1..arity]]
|
||||||
|
|
||||||
|
arg_decls = intercalate "," ["Expr "++v | v <- vars]
|
||||||
|
args = if null vars then ",new Expr[] {}" else ","++intercalate "," vars
|
||||||
|
|
||||||
|
getArrity ty = length hypos
|
||||||
|
where
|
||||||
|
(hypos,_,_) = unType ty
|
||||||
|
|
||||||
|
javaEnding =
|
||||||
|
[
|
||||||
|
"",
|
||||||
|
"}"
|
||||||
|
]
|
||||||
@@ -90,6 +90,7 @@ data OutputFormat = FmtPGFPretty
|
|||||||
| FmtJavaScript
|
| FmtJavaScript
|
||||||
| FmtPython
|
| FmtPython
|
||||||
| FmtHaskell
|
| FmtHaskell
|
||||||
|
| FmtJava
|
||||||
| FmtProlog
|
| FmtProlog
|
||||||
| FmtLambdaProlog
|
| FmtLambdaProlog
|
||||||
| FmtByteCode
|
| FmtByteCode
|
||||||
@@ -475,6 +476,7 @@ outputFormatsExpl =
|
|||||||
(("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)"),
|
||||||
|
(("java", FmtJava),"Java (abstract syntax)"),
|
||||||
(("prolog", FmtProlog),"Prolog (whole grammar)"),
|
(("prolog", FmtProlog),"Prolog (whole grammar)"),
|
||||||
(("lambda_prolog",FmtLambdaProlog),"LambdaProlog (abstract syntax)"),
|
(("lambda_prolog",FmtLambdaProlog),"LambdaProlog (abstract syntax)"),
|
||||||
(("lp_byte_code", FmtByteCode),"Bytecode for Teyjus (abstract syntax, experimental)"),
|
(("lp_byte_code", FmtByteCode),"Bytecode for Teyjus (abstract syntax, experimental)"),
|
||||||
|
|||||||
Reference in New Issue
Block a user