forked from GitHub/gf-core
Add option "data" to Haskell options.
Imports Data.Data, all GF types derive Data, and uses DeriveDataTypeable.
This commit is contained in:
@@ -26,50 +26,56 @@ import Data.List --(isPrefixOf, find, intersperse)
|
|||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
|
|
||||||
type Prefix = String -> String
|
type Prefix = String -> String
|
||||||
|
type DerivingClause = String
|
||||||
|
|
||||||
-- | the main function
|
-- | the main function
|
||||||
grammar2haskell :: Options
|
grammar2haskell :: Options
|
||||||
-> String -- ^ Module name.
|
-> String -- ^ Module name.
|
||||||
-> PGF
|
-> PGF
|
||||||
-> String
|
-> String
|
||||||
grammar2haskell opts name gr = foldr (++++) [] $
|
grammar2haskell opts name gr = foldr (++++) [] $
|
||||||
pragmas ++ haskPreamble gadt name ++ [types, gfinstances gId lexical gr'] ++ compos
|
pragmas ++ haskPreamble gadt name derivingClause extraImports ++
|
||||||
|
[types, gfinstances gId lexical gr'] ++ compos
|
||||||
where gr' = hSkeleton gr
|
where gr' = hSkeleton gr
|
||||||
gadt = haskellOption opts HaskellGADT
|
gadt = haskellOption opts HaskellGADT
|
||||||
|
dataExt = haskellOption opts HaskellData
|
||||||
lexical cat = haskellOption opts HaskellLexical && isLexicalCat opts cat
|
lexical cat = haskellOption opts HaskellLexical && isLexicalCat opts cat
|
||||||
gId | haskellOption opts HaskellNoPrefix = id
|
gId | haskellOption opts HaskellNoPrefix = id
|
||||||
| otherwise = ("G"++)
|
| otherwise = ("G"++)
|
||||||
pragmas | gadt = ["{-# OPTIONS_GHC -fglasgow-exts #-}","{-# LANGUAGE GADTs #-}"]
|
pragmas | gadt = ["{-# OPTIONS_GHC -fglasgow-exts #-}","{-# LANGUAGE GADTs #-}"]
|
||||||
|
| dataExt = ["{-# LANGUAGE DeriveDataTypeable #-}"]
|
||||||
| otherwise = []
|
| otherwise = []
|
||||||
|
derivingClause
|
||||||
|
| dataExt = "deriving (Show,Data)"
|
||||||
|
| otherwise = "deriving Show"
|
||||||
|
extraImports | gadt = ["import Control.Monad.Identity",
|
||||||
|
"import Data.Monoid"]
|
||||||
|
| dataExt = ["import Data.Data"]
|
||||||
|
| otherwise = []
|
||||||
types | gadt = datatypesGADT gId lexical gr'
|
types | gadt = datatypesGADT gId lexical gr'
|
||||||
| otherwise = datatypes gId lexical gr'
|
| otherwise = datatypes gId derivingClause lexical gr'
|
||||||
compos | gadt = prCompos gId lexical gr' ++ composClass
|
compos | gadt = prCompos gId lexical gr' ++ composClass
|
||||||
| otherwise = []
|
| otherwise = []
|
||||||
|
|
||||||
haskPreamble gadt name =
|
haskPreamble gadt name derivingClause extraImports =
|
||||||
[
|
[
|
||||||
"module " ++ name ++ " where",
|
"module " ++ name ++ " where",
|
||||||
""
|
""
|
||||||
] ++
|
] ++ extraImports ++ [
|
||||||
(if gadt then [
|
|
||||||
"import Control.Monad.Identity",
|
|
||||||
"import Data.Monoid"
|
|
||||||
] else []) ++
|
|
||||||
[
|
|
||||||
"import PGF hiding (Tree)",
|
"import PGF hiding (Tree)",
|
||||||
"----------------------------------------------------",
|
"----------------------------------------------------",
|
||||||
"-- automatic translation from GF to Haskell",
|
"-- automatic translation from GF to Haskell",
|
||||||
"----------------------------------------------------",
|
"----------------------------------------------------",
|
||||||
"",
|
"",
|
||||||
"class Gf a where",
|
"class Gf a where",
|
||||||
" gf :: a -> Expr",
|
" gf :: a -> Expr",
|
||||||
" fg :: Expr -> a",
|
" fg :: Expr -> a",
|
||||||
"",
|
"",
|
||||||
predefInst gadt "GString" "String" "unStr" "mkStr",
|
predefInst gadt derivingClause "GString" "String" "unStr" "mkStr",
|
||||||
"",
|
"",
|
||||||
predefInst gadt "GInt" "Int" "unInt" "mkInt",
|
predefInst gadt derivingClause "GInt" "Int" "unInt" "mkInt",
|
||||||
"",
|
"",
|
||||||
predefInst gadt "GFloat" "Double" "unFloat" "mkFloat",
|
predefInst gadt derivingClause "GFloat" "Double" "unFloat" "mkFloat",
|
||||||
"",
|
"",
|
||||||
"----------------------------------------------------",
|
"----------------------------------------------------",
|
||||||
"-- below this line machine-generated",
|
"-- below this line machine-generated",
|
||||||
@@ -77,11 +83,11 @@ haskPreamble gadt name =
|
|||||||
""
|
""
|
||||||
]
|
]
|
||||||
|
|
||||||
predefInst gadt gtyp typ destr consr =
|
predefInst gadt derivingClause gtyp typ destr consr =
|
||||||
(if gadt
|
(if gadt
|
||||||
then []
|
then []
|
||||||
else ("newtype" +++ gtyp +++ "=" +++ gtyp +++ typ +++ " deriving Show\n\n")
|
else ("newtype" +++ gtyp +++ "=" +++ gtyp +++ typ +++ derivingClause ++ "\n\n")
|
||||||
)
|
)
|
||||||
++
|
++
|
||||||
"instance Gf" +++ gtyp +++ "where" ++++
|
"instance Gf" +++ gtyp +++ "where" ++++
|
||||||
" gf (" ++ gtyp +++ "x) =" +++ consr +++ "x" ++++
|
" gf (" ++ gtyp +++ "x) =" +++ consr +++ "x" ++++
|
||||||
@@ -94,24 +100,24 @@ type OIdent = String
|
|||||||
|
|
||||||
type HSkeleton = [(OIdent, [(OIdent, [OIdent])])]
|
type HSkeleton = [(OIdent, [(OIdent, [OIdent])])]
|
||||||
|
|
||||||
datatypes :: Prefix -> (OIdent -> Bool) -> (String,HSkeleton) -> String
|
datatypes :: Prefix -> DerivingClause -> (OIdent -> Bool) -> (String,HSkeleton) -> String
|
||||||
datatypes gId lexical = (foldr (+++++) "") . (filter (/="")) . (map (hDatatype gId lexical)) . snd
|
datatypes gId derivingClause lexical = (foldr (+++++) "") . (filter (/="")) . (map (hDatatype gId derivingClause lexical)) . snd
|
||||||
|
|
||||||
gfinstances :: Prefix -> (OIdent -> Bool) -> (String,HSkeleton) -> String
|
gfinstances :: Prefix -> (OIdent -> Bool) -> (String,HSkeleton) -> String
|
||||||
gfinstances gId lexical (m,g) = (foldr (+++++) "") $ (filter (/="")) $ (map (gfInstance gId lexical m)) g
|
gfinstances gId lexical (m,g) = (foldr (+++++) "") $ (filter (/="")) $ (map (gfInstance gId lexical m)) g
|
||||||
|
|
||||||
|
|
||||||
hDatatype :: Prefix -> (OIdent -> Bool) -> (OIdent, [(OIdent, [OIdent])]) -> String
|
hDatatype :: Prefix -> DerivingClause -> (OIdent -> Bool) -> (OIdent, [(OIdent, [OIdent])]) -> String
|
||||||
hDatatype _ _ ("Cn",_) = "" ---
|
hDatatype _ _ _ ("Cn",_) = "" ---
|
||||||
hDatatype gId _ (cat,[]) = "data" +++ gId cat
|
hDatatype gId _ _ (cat,[]) = "data" +++ gId cat
|
||||||
hDatatype gId _ (cat,rules) | isListCat (cat,rules) =
|
hDatatype gId derivingClause _ (cat,rules) | isListCat (cat,rules) =
|
||||||
"newtype" +++ gId cat +++ "=" +++ gId cat +++ "[" ++ gId (elemCat cat) ++ "]"
|
"newtype" +++ gId cat +++ "=" +++ gId cat +++ "[" ++ gId (elemCat cat) ++ "]"
|
||||||
+++ "deriving Show"
|
+++ derivingClause
|
||||||
hDatatype gId lexical (cat,rules) =
|
hDatatype gId derivingClause lexical (cat,rules) =
|
||||||
"data" +++ gId cat +++ "=" ++
|
"data" +++ gId cat +++ "=" ++
|
||||||
(if length rules == 1 then "" else "\n ") +++
|
(if length rules == 1 then "" else "\n ") +++
|
||||||
foldr1 (\x y -> x ++ "\n |" +++ y) constructors ++++
|
foldr1 (\x y -> x ++ "\n |" +++ y) constructors ++++
|
||||||
" deriving Show"
|
" " +++ derivingClause
|
||||||
where
|
where
|
||||||
constructors = [gId f +++ foldr (+++) "" (map (gId) xx) | (f,xx) <- nonLexicalRules (lexical cat) rules]
|
constructors = [gId f +++ foldr (+++) "" (map (gId) xx) | (f,xx) <- nonLexicalRules (lexical cat) rules]
|
||||||
++ if lexical cat then [lexicalConstructor cat +++ "String"] else []
|
++ if lexical cat then [lexicalConstructor cat +++ "String"] else []
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ data CFGTransform = CFGNoLR
|
|||||||
deriving (Show,Eq,Ord)
|
deriving (Show,Eq,Ord)
|
||||||
|
|
||||||
data HaskellOption = HaskellNoPrefix | HaskellGADT | HaskellLexical
|
data HaskellOption = HaskellNoPrefix | HaskellGADT | HaskellLexical
|
||||||
| HaskellConcrete | HaskellVariants
|
| HaskellConcrete | HaskellVariants | HaskellData
|
||||||
deriving (Show,Eq,Ord)
|
deriving (Show,Eq,Ord)
|
||||||
|
|
||||||
data Warning = WarnMissingLincat
|
data Warning = WarnMissingLincat
|
||||||
@@ -530,7 +530,8 @@ haskellOptionNames =
|
|||||||
("gadt", HaskellGADT),
|
("gadt", HaskellGADT),
|
||||||
("lexical", HaskellLexical),
|
("lexical", HaskellLexical),
|
||||||
("concrete", HaskellConcrete),
|
("concrete", HaskellConcrete),
|
||||||
("variants", HaskellVariants)]
|
("variants", HaskellVariants),
|
||||||
|
("data", HaskellData)]
|
||||||
|
|
||||||
-- | This is for bacward compatibility. Since GHC 6.12 we
|
-- | This is for bacward compatibility. Since GHC 6.12 we
|
||||||
-- started using the native Unicode support in GHC but it
|
-- started using the native Unicode support in GHC but it
|
||||||
|
|||||||
Reference in New Issue
Block a user