prepared visualize_parse for showing dependency labels

This commit is contained in:
aarne
2015-11-04 17:28:09 +00:00
parent e8b5b8c390
commit e39787ab88
3 changed files with 45 additions and 7 deletions

View File

@@ -597,6 +597,7 @@ pgfCommands = Map.fromList [
let gvOptions = GraphvizOptions {noLeaves = isOpt "noleaves" opts && not (isOpt "showleaves" opts), let gvOptions = GraphvizOptions {noLeaves = isOpt "noleaves" opts && not (isOpt "showleaves" opts),
noFun = isOpt "nofun" opts || not (isOpt "showfun" opts), noFun = isOpt "nofun" opts || not (isOpt "showfun" opts),
noCat = isOpt "nocat" opts && not (isOpt "showcat" opts), noCat = isOpt "nocat" opts && not (isOpt "showcat" opts),
noDep = True, ---- TODO
nodeFont = valStrOpts "nodefont" "" opts, nodeFont = valStrOpts "nodefont" "" opts,
leafFont = valStrOpts "leaffont" "" opts, leafFont = valStrOpts "leaffont" "" opts,
nodeColor = valStrOpts "nodecolor" "" opts, nodeColor = valStrOpts "nodecolor" "" opts,

View File

@@ -44,6 +44,7 @@ import Text.PrettyPrint
data GraphvizOptions = GraphvizOptions {noLeaves :: Bool, data GraphvizOptions = GraphvizOptions {noLeaves :: Bool,
noFun :: Bool, noFun :: Bool,
noCat :: Bool, noCat :: Bool,
noDep :: Bool,
nodeFont :: String, nodeFont :: String,
leafFont :: String, leafFont :: String,
nodeColor :: String, nodeColor :: String,
@@ -52,7 +53,7 @@ data GraphvizOptions = GraphvizOptions {noLeaves :: Bool,
leafEdgeStyle :: String leafEdgeStyle :: String
} }
graphvizDefaults = GraphvizOptions False False False "" "" "" "" "" "" graphvizDefaults = GraphvizOptions False False False True "" "" "" "" "" ""
-- | Renders abstract syntax tree in Graphviz format -- | Renders abstract syntax tree in Graphviz format
@@ -208,13 +209,15 @@ graphvizDependencyTree format debug mlab ms pgf lang t = render $
getDepLabels :: [String] -> Labels getDepLabels :: [String] -> Labels
getDepLabels ss = Map.fromList [(mkCId f,ls) | f:ls <- map words ss] getDepLabels ss = Map.fromList [(mkCId f,ls) | f:ls <- map words ss]
-- the old function, without dependencies
graphvizParseTree :: PGF -> Language -> GraphvizOptions -> Tree -> String graphvizParseTree :: PGF -> Language -> GraphvizOptions -> Tree -> String
graphvizParseTree pgf lang opts = graphvizBracketedString opts . bracketedLinearize pgf lang graphvizParseTree = graphvizParseTreeDep Nothing
graphvizParseTreeDep :: Maybe Labels -> PGF -> Language -> GraphvizOptions -> Tree -> String
graphvizParseTreeDep mbl pgf lang opts tree = graphvizBracketedString opts mbl tree $ bracketedLinearize pgf lang tree
graphvizBracketedString :: GraphvizOptions -> [BracketedString] -> String graphvizBracketedString :: GraphvizOptions -> Maybe Labels -> Tree -> [BracketedString] -> String
graphvizBracketedString opts bss = render graphviz_code graphvizBracketedString opts mbl tree bss = render graphviz_code
where where
graphviz_code graphviz_code
= text "graph {" $$ = text "graph {" $$
@@ -258,7 +261,7 @@ graphvizBracketedString opts bss = render graphviz_code
getInternals [] = [] getInternals [] = []
getInternals nodes getInternals nodes
= nub [(parent, fid, mkNode fun cat) | = nub [(parent, fid, mkNode fun cat) |
(parent, Bracket cat fid _ fun _ _) <- nodes] (parent, Bracket cat fid lind fun _ _) <- nodes]
: getInternals [(fid, child) | : getInternals [(fid, child) |
(_, Bracket _ fid _ _ _ children) <- nodes, (_, Bracket _ fid _ _ _ children) <- nodes,
child <- children] child <- children]
@@ -279,9 +282,42 @@ graphvizBracketedString opts bss = render graphviz_code
) $$ ) $$
text "}" $$ text "}" $$
-- the following is for the edges between parent and children: -- the following is for the edges between parent and children:
vcat [tag pid <> text " -- " <> tag id <> semi | (pid, id, _) <- nodes, pid /= nil] $$ vcat [tag pid <> text " -- " <> tag id <> text (depLabel node) | node@(pid, id, _) <- nodes, pid /= nil] $$
space space
depLabel node@(parent,id,lbl)
| noDep opts = ";"
| otherwise = case getArg id of
Just (fun,arg) -> (mkOption "" "label" (showCId fun ++ "#" ++ show arg))
_ -> ";"
getArg i = getArgumentPlace i (expr2numtree tree) Nothing
---- to restore the argument place from bracketed linearization
data NumTree = NumTree Int CId [NumTree]
getArgumentPlace :: Int -> NumTree -> Maybe (CId,Int) -> Maybe (CId,Int)
getArgumentPlace i tree@(NumTree int fun ts) mfi
| i == int = mfi
| otherwise = case [fj | (t,x) <- zip ts [0..], Just fj <- [getArgumentPlace i t (Just (fun,x))]] of
fj:_ -> Just fj
_ -> Nothing
expr2numtree :: Expr -> NumTree
expr2numtree = fst . renumber 0 . flatten where
flatten e = case e of
EApp f a -> case flatten f of
NumTree _ g ts -> NumTree 0 g (ts ++ [flatten a])
EFun f -> NumTree 0 f []
renumber i t@(NumTree _ f ts) = case renumbers i ts of
(ts',j) -> (NumTree j f ts', j+1)
renumbers i ts = case ts of
t:tt -> case renumber i t of
(t',j) -> case renumbers j tt of (tt',k) -> (t':tt',k)
_ -> ([],i)
----- end this terrible stuff
type Rel = (Int,[Int]) type Rel = (Int,[Int])
-- possibly needs changes after clearing about many-to-many on this level -- possibly needs changes after clearing about many-to-many on this level

View File

@@ -448,6 +448,7 @@ pgfMain command (t,pgf) =
PGF.GraphvizOptions # bool "noleaves" PGF.GraphvizOptions # bool "noleaves"
% bool "nofun" % bool "nofun"
% bool "nocat" % bool "nocat"
% bool "nodep"
% string "nodefont" % string "nodefont"
% string "leaffont" % string "leaffont"
% string "nodecolor" % string "nodecolor"