From 10ac9be0e1522701b10df8301182b58a529c808b Mon Sep 17 00:00:00 2001 From: bringert Date: Wed, 20 Dec 2006 21:50:16 +0000 Subject: [PATCH] More JSGF pretty-printing. --- src/GF/Speech/PrJSGF.hs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/GF/Speech/PrJSGF.hs b/src/GF/Speech/PrJSGF.hs index ffb5d6630..f284f941d 100644 --- a/src/GF/Speech/PrJSGF.hs +++ b/src/GF/Speech/PrJSGF.hs @@ -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 \ No newline at end of file +($++$) :: Doc -> Doc -> Doc +x $++$ y = x $$ emptyLine $$ y