forked from GitHub/gf-core
Convert from Text.PrettyPrint to GF.Text.Pretty
All compiler modules now use GF.Text.Pretty instead of Text.PrettyPrint
This commit is contained in:
@@ -18,7 +18,7 @@ import PGF
|
||||
|
||||
import Data.Char (toUpper,toLower)
|
||||
import Data.List (partition)
|
||||
import Text.PrettyPrint.HughesPJ
|
||||
import GF.Text.Pretty
|
||||
|
||||
width :: Int
|
||||
width = 75
|
||||
@@ -30,10 +30,10 @@ gslPrinter opts pgf cnc = renderStyle st $ prGSL $ makeNonLeftRecursiveSRG opts
|
||||
prGSL :: SRG -> Doc
|
||||
prGSL srg = header $++$ mainCat $++$ foldr ($++$) empty (map prRule (srgRules srg))
|
||||
where
|
||||
header = text ";GSL2.0" $$
|
||||
header = ";GSL2.0" $$
|
||||
comment ("Nuance speech recognition grammar for " ++ srgName srg) $$
|
||||
comment ("Generated by GF")
|
||||
mainCat = text ".MAIN" <+> prCat (srgStartCat srg)
|
||||
mainCat = ".MAIN" <+> prCat (srgStartCat srg)
|
||||
prRule (SRGRule cat rhs) = prCat cat <+> union (map prAlt rhs)
|
||||
-- FIXME: use the probability
|
||||
prAlt (SRGAlt mp _ rhs) = prItem rhs
|
||||
@@ -42,23 +42,23 @@ prGSL srg = header $++$ mainCat $++$ foldr ($++$) empty (map prRule (srgRules sr
|
||||
prItem :: SRGItem -> Doc
|
||||
prItem = f
|
||||
where
|
||||
f (REUnion xs) = (if null es then empty else text "?") <> union (map f nes)
|
||||
f (REUnion xs) = (if null es then empty else pp "?") <> union (map f nes)
|
||||
where (es,nes) = partition isEpsilon xs
|
||||
f (REConcat [x]) = f x
|
||||
f (REConcat xs) = text "(" <> fsep (map f xs) <> text ")"
|
||||
f (RERepeat x) = text "*" <> f x
|
||||
f (REConcat xs) = "(" <> fsep (map f xs) <> ")"
|
||||
f (RERepeat x) = "*" <> f x
|
||||
f (RESymbol s) = prSymbol s
|
||||
|
||||
union :: [Doc] -> Doc
|
||||
union [x] = x
|
||||
union xs = text "[" <> fsep xs <> text "]"
|
||||
union xs = "[" <> fsep xs <> "]"
|
||||
|
||||
prSymbol :: Symbol SRGNT Token -> Doc
|
||||
prSymbol = symbol (prCat . fst) (doubleQuotes . showToken)
|
||||
|
||||
-- GSL requires an upper case letter in category names
|
||||
prCat :: Cat -> Doc
|
||||
prCat = text . firstToUpper
|
||||
prCat = pp . firstToUpper
|
||||
|
||||
|
||||
firstToUpper :: String -> String
|
||||
@@ -76,19 +76,19 @@ keepSymbol _ = True
|
||||
|
||||
-- Nuance does not like upper case characters in tokens
|
||||
showToken :: Token -> Doc
|
||||
showToken = text . map toLower
|
||||
showToken = pp . map toLower
|
||||
|
||||
isPunct :: Char -> Bool
|
||||
isPunct c = c `elem` "-_.:;.,?!()[]{}"
|
||||
|
||||
comment :: String -> Doc
|
||||
comment s = text ";" <+> text s
|
||||
comment s = ";" <+> s
|
||||
|
||||
|
||||
-- Pretty-printing utilities
|
||||
|
||||
emptyLine :: Doc
|
||||
emptyLine = text ""
|
||||
emptyLine = pp ""
|
||||
|
||||
($++$) :: Doc -> Doc -> Doc
|
||||
x $++$ y = x $$ emptyLine $$ y
|
||||
|
||||
Reference in New Issue
Block a user