summaryrefslogtreecommitdiff
path: root/src/compiler/GF/Compile
diff options
context:
space:
mode:
authorAndreas Källberg <anka.213@gmail.com>2020-09-05 21:11:12 +0200
committerAndreas Källberg <anka.213@gmail.com>2020-09-05 21:11:12 +0200
commit56f94da772566a1960d889c14c420ee832038365 (patch)
tree86737b1039dfa03b3fb289cdb309b80a5f35f759 /src/compiler/GF/Compile
parent57ce76dbc121ee554675b9ee6136441ec0bb5710 (diff)
parentbca0691cb028fe33ae1b77e71752d4e937490ff1 (diff)
Merge remote-tracking branch 'origin/master' into fix-newer-cabal
Diffstat (limited to 'src/compiler/GF/Compile')
-rw-r--r--src/compiler/GF/Compile/PGFtoHaskell.hs70
1 files changed, 39 insertions, 31 deletions
diff --git a/src/compiler/GF/Compile/PGFtoHaskell.hs b/src/compiler/GF/Compile/PGFtoHaskell.hs
index fc17e4e4e..6356c9f6d 100644
--- a/src/compiler/GF/Compile/PGFtoHaskell.hs
+++ b/src/compiler/GF/Compile/PGFtoHaskell.hs
@@ -26,50 +26,58 @@ import Data.List --(isPrefixOf, find, intersperse)
import qualified Data.Map as Map
type Prefix = String -> String
+type DerivingClause = String
-- | the main function
grammar2haskell :: Options
-> String -- ^ Module name.
-> PGF
-> String
-grammar2haskell opts name gr = foldr (++++) [] $
- pragmas ++ haskPreamble gadt name ++ [types, gfinstances gId lexical gr'] ++ compos
+grammar2haskell opts name gr = foldr (++++) [] $
+ pragmas ++ haskPreamble gadt name derivingClause extraImports ++
+ [types, gfinstances gId lexical gr'] ++ compos
where gr' = hSkeleton gr
gadt = haskellOption opts HaskellGADT
+ dataExt = haskellOption opts HaskellData
lexical cat = haskellOption opts HaskellLexical && isLexicalCat opts cat
- gId | haskellOption opts HaskellNoPrefix = id
- | otherwise = ("G"++)
- pragmas | gadt = ["{-# OPTIONS_GHC -fglasgow-exts #-}","{-# LANGUAGE GADTs #-}"]
+ gId | haskellOption opts HaskellNoPrefix = rmForbiddenChars
+ | otherwise = ("G"++) . rmForbiddenChars
+ -- GF grammars allow weird identifier names inside '', e.g. 'VP/Object'
+ rmForbiddenChars = filter (`notElem` "'!#$%&*+./<=>?@\\^|-~")
+ pragmas | gadt = ["{-# LANGUAGE GADTs, FlexibleInstances, KindSignatures, RankNTypes, TypeSynonymInstances #-}"]
+ | dataExt = ["{-# LANGUAGE DeriveDataTypeable #-}"]
| 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'
- | otherwise = datatypes gId lexical gr'
+ | otherwise = datatypes gId derivingClause lexical gr'
compos | gadt = prCompos gId lexical gr' ++ composClass
| otherwise = []
-haskPreamble gadt name =
+haskPreamble gadt name derivingClause extraImports =
[
"module " ++ name ++ " where",
""
- ] ++
- (if gadt then [
- "import Control.Monad.Identity",
- "import Data.Monoid"
- ] else []) ++
- [
+ ] ++ extraImports ++ [
"import PGF hiding (Tree)",
"----------------------------------------------------",
"-- automatic translation from GF to Haskell",
"----------------------------------------------------",
- "",
+ "",
"class Gf a where",
" gf :: a -> Expr",
" 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",
@@ -77,11 +85,11 @@ haskPreamble gadt name =
""
]
-predefInst gadt gtyp typ destr consr =
+predefInst gadt derivingClause gtyp typ destr consr =
(if gadt
- then []
- else ("newtype" +++ gtyp +++ "=" +++ gtyp +++ typ +++ " deriving Show\n\n")
- )
+ then []
+ else ("newtype" +++ gtyp +++ "=" +++ gtyp +++ typ +++ derivingClause ++ "\n\n")
+ )
++
"instance Gf" +++ gtyp +++ "where" ++++
" gf (" ++ gtyp +++ "x) =" +++ consr +++ "x" ++++
@@ -94,24 +102,24 @@ type OIdent = String
type HSkeleton = [(OIdent, [(OIdent, [OIdent])])]
-datatypes :: Prefix -> (OIdent -> Bool) -> (String,HSkeleton) -> String
-datatypes gId lexical = (foldr (+++++) "") . (filter (/="")) . (map (hDatatype gId lexical)) . snd
+datatypes :: Prefix -> DerivingClause -> (OIdent -> Bool) -> (String,HSkeleton) -> String
+datatypes gId derivingClause lexical = (foldr (+++++) "") . (filter (/="")) . (map (hDatatype gId derivingClause lexical)) . snd
gfinstances :: Prefix -> (OIdent -> Bool) -> (String,HSkeleton) -> String
gfinstances gId lexical (m,g) = (foldr (+++++) "") $ (filter (/="")) $ (map (gfInstance gId lexical m)) g
-hDatatype :: Prefix -> (OIdent -> Bool) -> (OIdent, [(OIdent, [OIdent])]) -> String
-hDatatype _ _ ("Cn",_) = "" ---
-hDatatype gId _ (cat,[]) = "data" +++ gId cat
-hDatatype gId _ (cat,rules) | isListCat (cat,rules) =
- "newtype" +++ gId cat +++ "=" +++ gId cat +++ "[" ++ gId (elemCat cat) ++ "]"
- +++ "deriving Show"
-hDatatype gId lexical (cat,rules) =
+hDatatype :: Prefix -> DerivingClause -> (OIdent -> Bool) -> (OIdent, [(OIdent, [OIdent])]) -> String
+hDatatype _ _ _ ("Cn",_) = "" ---
+hDatatype gId _ _ (cat,[]) = "data" +++ gId cat
+hDatatype gId derivingClause _ (cat,rules) | isListCat (cat,rules) =
+ "newtype" +++ gId cat +++ "=" +++ gId cat +++ "[" ++ gId (elemCat cat) ++ "]"
+ +++ derivingClause
+hDatatype gId derivingClause lexical (cat,rules) =
"data" +++ gId cat +++ "=" ++
(if length rules == 1 then "" else "\n ") +++
foldr1 (\x y -> x ++ "\n |" +++ y) constructors ++++
- " deriving Show"
+ " " +++ derivingClause
where
constructors = [gId f +++ foldr (+++) "" (map (gId) xx) | (f,xx) <- nonLexicalRules (lexical cat) rules]
++ if lexical cat then [lexicalConstructor cat +++ "String"] else []