forked from GitHub/gf-core
Use grammar name as gsl category name prefix. Put some grammar info in GSL comments.
This commit is contained in:
@@ -43,48 +43,57 @@ import Data.Maybe (fromMaybe)
|
|||||||
import Data.FiniteMap
|
import Data.FiniteMap
|
||||||
|
|
||||||
|
|
||||||
data GSLGrammar = GSLGrammar String [GSLRule]
|
data GSLGrammar = GSLGrammar String -- ^ grammar name
|
||||||
|
String -- ^ start category name
|
||||||
|
[GSLRule]
|
||||||
data GSLRule = GSLRule String [GSLAlt]
|
data GSLRule = GSLRule String [GSLAlt]
|
||||||
type GSLAlt = [Symbol String Token]
|
type GSLAlt = [Symbol String Token]
|
||||||
|
|
||||||
type CatNames = FiniteMap String String
|
type CatNames = FiniteMap String String
|
||||||
|
|
||||||
gslPrinter :: Options -> CFGrammar -> String
|
gslPrinter :: Ident -- ^ Grammar name
|
||||||
gslPrinter opts = prGSL start
|
-> Options -> CFGrammar -> String
|
||||||
|
gslPrinter name opts = prGSL (prIdent name) start
|
||||||
where mstart = getOptVal opts gStartCat
|
where mstart = getOptVal opts gStartCat
|
||||||
start = fromMaybe "S" mstart ++ "{}.s"
|
start = fromMaybe "S" mstart ++ "{}.s"
|
||||||
|
|
||||||
prGSL :: String -- ^ startcat
|
prGSL :: String -- ^ Grammar name
|
||||||
|
-> String -- ^ startcat
|
||||||
-> CFGrammar -> String
|
-> CFGrammar -> String
|
||||||
prGSL start cfg = prGSLGrammar names gsl ""
|
prGSL name start cfg = prGSLGrammar names gsl ""
|
||||||
where
|
where
|
||||||
cfg' = makeNice cfg
|
cfg' = makeNice cfg
|
||||||
gsl = cfgToGSL start cfg'
|
gsl = cfgToGSL name start cfg'
|
||||||
names = mkCatNames "GSL_" gsl
|
names = mkCatNames gsl
|
||||||
|
|
||||||
cfgToGSL :: String -- ^ startcat
|
cfgToGSL :: String -- ^ grammar name
|
||||||
|
-> String -- ^ start category
|
||||||
-> [CFRule_] -> GSLGrammar
|
-> [CFRule_] -> GSLGrammar
|
||||||
cfgToGSL start =
|
cfgToGSL name start =
|
||||||
GSLGrammar start . map cfgRulesToGSLRule . sortAndGroupBy ruleCat
|
GSLGrammar name start . map cfgRulesToGSLRule . sortAndGroupBy ruleCat
|
||||||
where
|
where
|
||||||
ruleCat (Rule c _ _) = c
|
ruleCat (Rule c _ _) = c
|
||||||
ruleRhs (Rule _ r _) = r
|
ruleRhs (Rule _ r _) = r
|
||||||
cfgRulesToGSLRule rs@(r:_) = GSLRule (ruleCat r) (map ruleRhs rs)
|
cfgRulesToGSLRule rs@(r:_) = GSLRule (ruleCat r) (map ruleRhs rs)
|
||||||
|
|
||||||
mkCatNames :: String -- name prefix
|
mkCatNames :: GSLGrammar -> CatNames
|
||||||
-> GSLGrammar -> CatNames
|
mkCatNames (GSLGrammar name start rules) =
|
||||||
mkCatNames pref (GSLGrammar start rules) =
|
|
||||||
listToFM (zipWith dotIfStart lhsCats names)
|
listToFM (zipWith dotIfStart lhsCats names)
|
||||||
where names = [pref ++ show x | x <- [0..]]
|
where names = [name ++ "_" ++ show x | x <- [0..]]
|
||||||
lhsCats = [ c | GSLRule c _ <- rules]
|
lhsCats = [ c | GSLRule c _ <- rules]
|
||||||
dotIfStart c n | c == start = (c, "." ++ n)
|
dotIfStart c n | c == start = (c, "." ++ n)
|
||||||
| otherwise = (c, n)
|
| otherwise = (c, n)
|
||||||
|
|
||||||
prGSLGrammar :: CatNames -> GSLGrammar -> ShowS
|
prGSLGrammar :: CatNames -> GSLGrammar -> ShowS
|
||||||
prGSLGrammar names (GSLGrammar start g) = header . unlinesS (map prGSLrule g)
|
prGSLGrammar names (GSLGrammar name start g) =
|
||||||
|
header . unlinesS (map prGSLrule g)
|
||||||
where
|
where
|
||||||
header = showString ";GSL2.0" . nl
|
header = showString ";GSL2.0" . nl
|
||||||
. showString ("; startcat = " ++ start ) . nl
|
. comments ["Nuance speech synthesis grammar for " ++ name,
|
||||||
|
"Generated by GF",
|
||||||
|
"Start category: " ++ start
|
||||||
|
++ " (" ++ prGSLCat start ")"]
|
||||||
|
. nl . nl
|
||||||
prGSLrule (GSLRule cat rhs) =
|
prGSLrule (GSLRule cat rhs) =
|
||||||
showString "; " . prtS cat . nl
|
showString "; " . prtS cat . nl
|
||||||
. prGSLCat cat . sp . wrap "[" (unwordsS (map prGSLAlt rhs)) "]" . nl
|
. prGSLCat cat . sp . wrap "[" (unwordsS (map prGSLAlt rhs)) "]" . nl
|
||||||
@@ -105,6 +114,9 @@ rmPunct (s:ss) = s : rmPunct ss
|
|||||||
isPunct :: Char -> Bool
|
isPunct :: Char -> Bool
|
||||||
isPunct c = c `elem` "-_.;.,?!"
|
isPunct c = c `elem` "-_.;.,?!"
|
||||||
|
|
||||||
|
comments :: [String] -> ShowS
|
||||||
|
comments = unlinesS . map (showString . ("; " ++))
|
||||||
|
|
||||||
--
|
--
|
||||||
-- * Utils
|
-- * Utils
|
||||||
--
|
--
|
||||||
|
|||||||
@@ -191,7 +191,9 @@ customGrammarPrinter =
|
|||||||
,(strCI "cf", prCF . stateCF)
|
,(strCI "cf", prCF . stateCF)
|
||||||
,(strCI "old", printGrammarOld . stateGrammarST)
|
,(strCI "old", printGrammarOld . stateGrammarST)
|
||||||
,(strCI "srg", prSRG . stateCF)
|
,(strCI "srg", prSRG . stateCF)
|
||||||
,(strCI "gsl", \s -> gslPrinter (stateOptions s) $ Cnv.cfg $ statePInfo s)
|
,(strCI "gsl", \s -> let opts = stateOptions s
|
||||||
|
name = cncId s
|
||||||
|
in gslPrinter name opts $ Cnv.cfg $ statePInfo s)
|
||||||
,(strCI "lbnf", prLBNF . stateCF)
|
,(strCI "lbnf", prLBNF . stateCF)
|
||||||
,(strCI "haskell", grammar2haskell . stateGrammarST)
|
,(strCI "haskell", grammar2haskell . stateGrammarST)
|
||||||
,(strCI "morpho", prMorpho . stateMorpho)
|
,(strCI "morpho", prMorpho . stateMorpho)
|
||||||
|
|||||||
Reference in New Issue
Block a user