More JSGF pretty-printing.

This commit is contained in:
bringert
2006-12-20 21:50:16 +00:00
parent 642809fd3b
commit 514a8d6afa

View File

@@ -47,18 +47,18 @@ jsgfPrinter name start opts sisr probs cfg = show (prJSGF srg sisr)
prJSGF :: SRG -> Maybe SISRFormat -> Doc
prJSGF srg@(SRG{grammarName=name,startCat=start,origStartCat=origStart,rules=rs}) sisr
= header $$ mainCat $$ vcat topCatRules $$ vcat (map prRule rs)
= header $++$ mainCat $++$ vcat topCatRules $++$ foldr ($++$) empty (map prRule rs)
where
header = text "#JSGF V1.0 UTF-8;" $$
comment ("JSGF speech recognition grammar for " ++ name) $$
comment "Generated by GF" $$
text ("grammar " ++ name ++ ";") $$ emptyLine
text ("grammar " ++ name ++ ";")
mainCat = comment ("Start category: " ++ origStart) $$
rule True "MAIN" [prCat start] $$ emptyLine
rule True "MAIN" [prCat start]
prRule (SRGRule cat origCat rhs) =
comment origCat $$
-- rule False cat (map prAlt (ebnfSRGAlts rhs))
rule False cat (map prAlt rhs) $$ emptyLine
rule False cat (map prAlt rhs)
-- FIXME: use the probability
-- prAlt (EBnfSRGAlt mp n rhs) = tag sisr (profileInitSISR n) . showChar ' '. prItem sisr rhs
prAlt (SRGAlt mp n rhs) = initTag <+> prItem sisr n rhs <+> finalTag
@@ -120,6 +120,15 @@ isPunct c = c `elem` "-_.;.,?!"
comment :: String -> Doc
comment s = text "//" <+> text s
rule :: Bool -> SRGCat -> [Doc] -> Doc
rule pub c xs = p <+> prCat c <+> char '='
$$ nest 2 (sep (prepunctuate (text "| ") xs) <+> char ';')
where p = if pub then text "public" else empty
-- Pretty-printing utilities
emptyLine :: Doc
emptyLine = text ""
@@ -127,7 +136,5 @@ prepunctuate :: Doc -> [Doc] -> [Doc]
prepunctuate _ [] = []
prepunctuate p (x:xs) = x : map (p <>) xs
rule :: Bool -> SRGCat -> [Doc] -> Doc
rule pub c xs = p <+> prCat c <+> char '='
$$ nest 2 (sep (prepunctuate (text "| ") xs) <+> char ';')
where p = if pub then text "public" else empty
($++$) :: Doc -> Doc -> Doc
x $++$ y = x $$ emptyLine $$ y