1
0
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:
hallgren
2014-07-28 11:58:00 +00:00
parent 59172ce9c5
commit 7a91afc02a
20 changed files with 100 additions and 100 deletions

View File

@@ -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

View File

@@ -23,7 +23,7 @@ import PGF
import Data.Char
import Data.List
--import Data.Maybe
import Text.PrettyPrint.HughesPJ
import GF.Text.Pretty
--import Debug.Trace
width :: Int
@@ -40,46 +40,46 @@ prJSGF :: Maybe SISRFormat -> SRG -> Doc
prJSGF sisr srg
= header $++$ mainCat $++$ foldr ($++$) empty (map prRule (srgRules srg))
where
header = text "#JSGF" <+> text "V1.0" <+> text "UTF-8" <+> lang <> char ';' $$
header = "#JSGF" <+> "V1.0" <+> "UTF-8" <+> lang <> ';' $$
comment ("JSGF speech recognition grammar for " ++ srgName srg) $$
comment "Generated by GF" $$
text ("grammar " ++ srgName srg ++ ";")
lang = maybe empty text (srgLanguage srg)
("grammar " ++ srgName srg ++ ";")
lang = maybe empty pp (srgLanguage srg)
mainCat = rule True "MAIN" [prCat (srgStartCat srg)]
prRule (SRGRule cat rhs) = rule (isExternalCat srg cat) cat (map prAlt rhs)
prAlt (SRGAlt mp n rhs) = sep [initTag, p (prItem sisr n rhs), finalTag]
where initTag | isEmpty t = empty
| otherwise = text "<NULL>" <+> t
| otherwise = "<NULL>" <+> t
where t = tag sisr (profileInitSISR n)
finalTag = tag sisr (profileFinalSISR n)
p = if isEmpty initTag && isEmpty finalTag then id else parens
prCat :: Cat -> Doc
prCat c = char '<' <> text c <> char '>'
prCat c = '<' <> c <> '>'
prItem :: Maybe SISRFormat -> CFTerm -> SRGItem -> Doc
prItem sisr t = f 0
where
f _ (REUnion []) = text "<VOID>"
f _ (REUnion []) = pp "<VOID>"
f p (REUnion xs)
| not (null es) = brackets (f 0 (REUnion nes))
| otherwise = (if p >= 1 then parens else id) (alts (map (f 1) xs))
where (es,nes) = partition isEpsilon xs
f _ (REConcat []) = text "<NULL>"
f _ (REConcat []) = pp "<NULL>"
f p (REConcat xs) = (if p >= 3 then parens else id) (fsep (map (f 2) xs))
f p (RERepeat x) = f 3 x <> char '*'
f p (RERepeat x) = f 3 x <> '*'
f _ (RESymbol s) = prSymbol sisr t s
prSymbol :: Maybe SISRFormat -> CFTerm -> SRGSymbol -> Doc
prSymbol sisr cn (NonTerminal n@(c,_)) = prCat c <+> tag sisr (catSISR cn n)
prSymbol _ cn (Terminal t) | all isPunct t = empty -- removes punctuation
| otherwise = text t -- FIXME: quote if there is whitespace or odd chars
| otherwise = pp t -- FIXME: quote if there is whitespace or odd chars
tag :: Maybe SISRFormat -> (SISRFormat -> SISRTag) -> Doc
tag Nothing _ = empty
tag (Just fmt) t = case t fmt of
[] -> empty
ts -> char '{' <+> (text (e $ prSISR ts)) <+> char '}'
ts -> '{' <+> (e $ prSISR ts) <+> '}'
where e [] = []
e ('}':xs) = '\\':'}':e xs
e ('\n':xs) = ' ' : e (dropWhile isSpace xs)
@@ -89,21 +89,21 @@ isPunct :: Char -> Bool
isPunct c = c `elem` "-_.;.,?!"
comment :: String -> Doc
comment s = text "//" <+> text s
comment s = "//" <+> s
alts :: [Doc] -> Doc
alts = fsep . prepunctuate (text "| ")
alts = fsep . prepunctuate ("| ")
rule :: Bool -> Cat -> [Doc] -> Doc
rule pub c xs = p <+> prCat c <+> char '=' <+> nest 2 (alts xs) <+> char ';'
where p = if pub then text "public" else empty
rule pub c xs = p <+> prCat c <+> '=' <+> nest 2 (alts xs) <+> ';'
where p = if pub then pp "public" else empty
-- Pretty-printing utilities
emptyLine :: Doc
emptyLine = text ""
emptyLine = pp ""
prepunctuate :: Doc -> [Doc] -> [Doc]
--prepunctuate :: Doc -> [Doc] -> [Doc]
prepunctuate _ [] = []
prepunctuate p (x:xs) = x : map (p <>) xs

View File

@@ -30,7 +30,7 @@ import PGF (PGF, CId)
--import Data.Char
import Data.List
import Data.Maybe
import Text.PrettyPrint.HughesPJ
import GF.Text.Pretty
--import Debug.Trace
width :: Int
@@ -50,14 +50,14 @@ prABNF :: Maybe SISRFormat -> SRG -> Doc
prABNF sisr srg
= header $++$ foldr ($++$) empty (map prRule (srgRules srg))
where
header = text "#ABNF 1.0 UTF-8;" $$
header = "#ABNF 1.0 UTF-8;" $$
meta "description" ("Speech recognition grammar for " ++ srgName srg) $$
meta "generator" "Grammatical Framework" $$
language $$ tagFormat $$ mainCat
language = maybe empty (\l -> text "language" <+> text l <> char ';') (srgLanguage srg)
tagFormat | isJust sisr = text "tag-format" <+> text "<semantics/1.0>" <> char ';'
language = maybe empty (\l -> "language" <+> l <> ';') (srgLanguage srg)
tagFormat | isJust sisr = "tag-format" <+> "<semantics/1.0>" <> ';'
| otherwise = empty
mainCat = text "root" <+> prCat (srgStartCat srg) <> char ';'
mainCat = "root" <+> prCat (srgStartCat srg) <> ';'
prRule (SRGRule cat alts) = rule (isExternalCat srg cat) cat (map prAlt alts)
prAlt (SRGAlt mp n rhs) = sep [initTag, p (prItem sisr n rhs), finalTag]
where initTag = tag sisr (profileInitSISR n)
@@ -65,19 +65,19 @@ prABNF sisr srg
p = if isEmpty initTag && isEmpty finalTag then id else parens
prCat :: Cat -> Doc
prCat c = char '$' <> text c
prCat c = '$' <> c
prItem :: Maybe SISRFormat -> CFTerm -> SRGItem -> Doc
prItem sisr t = f 0
where
f _ (REUnion []) = text "$VOID"
f _ (REUnion []) = pp "$VOID"
f p (REUnion xs)
| not (null es) = brackets (f 0 (REUnion nes))
| otherwise = (if p >= 1 then parens else id) (alts (map (f 1) xs))
where (es,nes) = partition isEpsilon xs
f _ (REConcat []) = text "$NULL"
f _ (REConcat []) = pp "$NULL"
f p (REConcat xs) = (if p >= 3 then parens else id) (fsep (map (f 2) xs))
f p (RERepeat x) = f 3 x <> text "<0->"
f p (RERepeat x) = f 3 x <> "<0->"
f _ (RESymbol s) = prSymbol sisr t s
@@ -85,7 +85,7 @@ prSymbol :: Maybe SISRFormat -> CFTerm -> SRGSymbol -> Doc
prSymbol sisr cn (NonTerminal n@(c,_)) = prCat c <+> tag sisr (catSISR cn n)
prSymbol _ cn (Terminal t)
| all isPunct t = empty -- removes punctuation
| otherwise = text t -- FIXME: quote if there is whitespace or odd chars
| otherwise = pp t -- FIXME: quote if there is whitespace or odd chars
tag :: Maybe SISRFormat -> (SISRFormat -> SISRTag) -> Doc
tag Nothing _ = empty
@@ -93,32 +93,32 @@ tag (Just fmt) t =
case t fmt of
[] -> empty
-- grr, silly SRGS ABNF does not have an escaping mechanism
ts | '{' `elem` x || '}' `elem` x -> text "{!{" <+> text x <+> text "}!}"
| otherwise -> text "{" <+> text x <+> text "}"
ts | '{' `elem` x || '}' `elem` x -> "{!{" <+> x <+> "}!}"
| otherwise -> "{" <+> x <+> "}"
where x = prSISR ts
isPunct :: Char -> Bool
isPunct c = c `elem` "-_.;.,?!"
comment :: String -> Doc
comment s = text "//" <+> text s
comment s = "//" <+> s
alts :: [Doc] -> Doc
alts = fsep . prepunctuate (text "| ")
alts = fsep . prepunctuate ("| ")
rule :: Bool -> Cat -> [Doc] -> Doc
rule pub c xs = p <+> prCat c <+> char '=' <+> nest 2 (alts xs) <+> char ';'
where p = if pub then text "public" else empty
rule pub c xs = p <+> prCat c <+> '=' <+> nest 2 (alts xs) <+> ';'
where p = if pub then pp "public" else empty
meta :: String -> String -> Doc
meta n v = text "meta" <+> text (show n) <+> text "is" <+> text (show v) <> char ';'
meta n v = "meta" <+> show n <+> "is" <+> show v <> ';'
-- Pretty-printing utilities
emptyLine :: Doc
emptyLine = text ""
emptyLine = pp ""
prepunctuate :: Doc -> [Doc] -> [Doc]
--prepunctuate :: Doc -> [Doc] -> [Doc]
prepunctuate _ [] = []
prepunctuate p (x:xs) = x : map (p <>) xs