1
0
forked from GitHub/gf-core

added linearize -all

This commit is contained in:
aarne
2007-11-08 17:01:09 +00:00
parent 009e3cdec1
commit bf353c1a2e
2 changed files with 30 additions and 11 deletions

View File

@@ -103,7 +103,7 @@ allCommands mgr = Map.fromAscList [
}),
("l", emptyCommandInfo {
exec = \opts -> return . fromStrings . map (optLin opts),
options = ["record","table","term"],
options = ["all","record","table","term"],
flags = ["lang"]
}),
("p", emptyCommandInfo {
@@ -117,6 +117,7 @@ allCommands mgr = Map.fromAscList [
optLin opts t = unlines [linea lang t | lang <- optLangs opts] where
linea lang = case opts of
_ | isOpt "all" opts -> allLinearize gr (cid lang)
_ | isOpt "table" opts -> tableLinearize gr (cid lang)
_ | isOpt "term" opts -> termLinearize gr (cid lang)
_ | isOpt "record" opts -> recordLinearize gr (cid lang)

View File

@@ -1,7 +1,8 @@
module GF.GFCC.ShowLinearize (
tableLinearize,
recordLinearize,
termLinearize
termLinearize,
allLinearize
) where
import GF.GFCC.Linearize
@@ -13,8 +14,9 @@ import GF.GFCC.PrintGFCC ----
import GF.Data.Operations
import Data.List
-- printing linearizations with parameters
-- printing linearizations in different ways with source parameters
-- internal representation, only used internally in this module
data Record =
RR [(String,Record)]
| RT [(String,Record)]
@@ -36,6 +38,7 @@ prRecord = prr where
RS s -> prQuotedString s
RCon s -> s
-- uses the encoding of record types in GFCC.paramlincat
mkRecord :: Term -> Term -> Record
mkRecord typ trm = case (typ,trm) of
(R rs, R ts) -> RR [(str lab, mkRecord ty t) | (P lab ty, t) <- zip rs ts]
@@ -47,23 +50,38 @@ mkRecord typ trm = case (typ,trm) of
where
str = realize
-- show all branches, without labels and params
allLinearize :: GFCC -> CId -> Exp -> String
allLinearize gfcc lang = concat . map pr . tabularLinearize gfcc lang where
pr (p,vs) = unlines vs
-- show all branches, with labels and params
tableLinearize :: GFCC -> CId -> Exp -> String
tableLinearize gfcc lang = unlines . branches . recLinearize gfcc lang where
tableLinearize gfcc lang = unlines . map pr . tabularLinearize gfcc lang where
pr (p,vs) = p +++ ":" +++ unwords (intersperse "|" vs)
-- create a table from labels+params to variants
tabularLinearize :: GFCC -> CId -> Exp -> [(String,[String])]
tabularLinearize gfcc lang = branches . recLinearize gfcc lang where
branches r = case r of
RR fs -> [lab +++ b | (lab,t) <- fs, b <- branches t]
RT fs -> [lab +++ b | (lab,t) <- fs, b <- branches t]
RFV rs -> intersperse "|" (concatMap branches rs)
RS s -> [" : " ++ s]
RR fs -> [(lab +++ b,s) | (lab,t) <- fs, (b,s) <- branches t]
RT fs -> [(lab +++ b,s) | (lab,t) <- fs, (b,s) <- branches t]
RFV rs -> [([], ss) | (_,ss) <- concatMap branches rs]
RS s -> [([], [s])]
RCon _ -> []
-- show record in GF-source-like syntax
recordLinearize :: GFCC -> CId -> Exp -> String
recordLinearize gfcc lang = prRecord . recLinearize gfcc lang
termLinearize :: GFCC -> CId -> Exp -> String
termLinearize gfcc lang = printTree . linExp gfcc lang
-- create a GF-like record, forming the basis of all functions above
recLinearize :: GFCC -> CId -> Exp -> Record
recLinearize gfcc lang exp = mkRecord typ $ linExp gfcc lang exp where
typ = case exp of
DTr _ (AC f) _ -> lookParamLincat gfcc lang $ valCat $ lookType gfcc f
-- show GFCC term
termLinearize :: GFCC -> CId -> Exp -> String
termLinearize gfcc lang = printTree . linExp gfcc lang