printing options for source GF terms defined in API and used in cc command

This commit is contained in:
aarne
2008-05-26 14:50:17 +00:00
parent 089b930d7c
commit a253fa2375
3 changed files with 38 additions and 4 deletions

View File

@@ -4,7 +4,8 @@ module GF.Grammar.API (
pTerm,
prTerm,
checkTerm,
computeTerm
computeTerm,
showTerm
) where
import GF.Source.ParGF
@@ -21,6 +22,8 @@ import GF.Compile.CheckGrammar (justCheckLTerm)
import GF.Compile.Compute (computeConcrete)
import GF.Data.Operations
import GF.Infra.Option
import qualified Data.ByteString.Char8 as BS
type Grammar = SourceGrammar
@@ -49,3 +52,9 @@ checkTermAny gr m t = do
computeTerm :: Grammar -> Term -> Err Term
computeTerm = computeConcrete
showTerm :: Options -> Term -> String
showTerm opts t
| oElem (iOpt "table") opts = unlines [p +++ s | (p,s) <- prTermTabular t]
| oElem (iOpt "all") opts = unlines [ s | (p,s) <- prTermTabular t]
| oElem (iOpt "unqual") opts = prt_ t
| otherwise = prt t

View File

@@ -30,7 +30,8 @@ module GF.Grammar.PrGrammar (Print(..),
prConstrs, prConstraints,
prMetaSubst, prEnv, prMSubst,
prExp, prOperSignature,
lookupIdent, lookupIdentInfo
lookupIdent, lookupIdentInfo,
prTermTabular
) where
import GF.Data.Operations
@@ -89,6 +90,12 @@ instance Print Ident where
instance Print Patt where
prt = pprintTree . trp
prt_ = prt . unqual where
unqual p = case p of
PP _ c [] -> PV c --- to remove curlies
PP _ c ps -> PC c (map unqual ps)
PC c ps -> PC c (map unqual ps)
_ -> p ---- records
instance Print Label where
prt = pprintTree . trLabel
@@ -247,3 +254,20 @@ lookupIdent c t = case lookupTree prt c t of
lookupIdentInfo :: Module Ident a -> Ident -> Err a
lookupIdentInfo mo i = lookupIdent i (jments mo)
--- printing cc command output AR 26/5/2008
prTermTabular :: Term -> [(String,String)]
prTermTabular = pr where
pr t = case t of
R rs ->
[(prt_ lab +++ "." +++ path, str) | (lab,(_,val)) <- rs, (path,str) <- pr val]
T _ cs ->
[(prt_ lab +++"=>" +++ path, str) | (lab, val) <- cs, (path,str) <- pr val]
_ -> [([],ps t)]
ps t = case t of
K s -> s
C s u -> ps s +++ ps u
FV ts -> unwords (intersperse "/" (map ps ts))
_ -> prt_ t

View File

@@ -34,8 +34,9 @@ loop gfenv0 = do
-- special commands, requiring source grammar in env
"cc":ws -> do
let t = pTerm (unwords ws) >>= checkTerm sgr >>= computeTerm sgr
err putStrLn (putStrLn . prTerm) t ---- make pipable
let (opts,term) = getOptions "-" ws
let t = pTerm (unwords term) >>= checkTerm sgr >>= computeTerm sgr
err putStrLn (putStrLn . showTerm opts) t ---- make pipable
loopNewCPU gfenv
"i":args -> do