mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-18 01:09:32 -06:00
GF/src is now for 2.9, and the new sources are in src-3.0 - keep it this way until the release of GF 3
This commit is contained in:
58
src-3.0/GF/Visualization/VisualizeTree.hs
Normal file
58
src-3.0/GF/Visualization/VisualizeTree.hs
Normal file
@@ -0,0 +1,58 @@
|
||||
----------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : VisualizeTree
|
||||
-- Maintainer : AR
|
||||
-- Stability : (stable)
|
||||
-- Portability : (portable)
|
||||
--
|
||||
-- > CVS $Date:
|
||||
-- > CVS $Author:
|
||||
-- > CVS $Revision:
|
||||
--
|
||||
-- Print a graph of an abstract syntax tree in Graphviz DOT format
|
||||
-- Based on BB's VisualizeGrammar
|
||||
-- FIXME: change this to use GF.Visualization.Graphviz,
|
||||
-- instead of rolling its own.
|
||||
-----------------------------------------------------------------------------
|
||||
|
||||
module GF.Visualization.VisualizeTree ( visualizeTrees
|
||||
) where
|
||||
|
||||
import GF.Infra.Ident
|
||||
import GF.Infra.Option
|
||||
import GF.Grammar.Abstract
|
||||
import GF.Data.Zipper
|
||||
import GF.Grammar.PrGrammar
|
||||
|
||||
import Data.List (intersperse, nub)
|
||||
import Data.Maybe (maybeToList)
|
||||
|
||||
visualizeTrees :: Options -> [Tree] -> String
|
||||
visualizeTrees opts = unlines . map (prGraph opts . tree2graph opts)
|
||||
|
||||
tree2graph :: Options -> Tree -> [String]
|
||||
tree2graph opts = prf [] where
|
||||
prf ps t@(Tr (node, trees)) =
|
||||
let (nod,lab) = prn ps node in
|
||||
(nod ++ " [label = " ++ lab ++ ", style = \"solid\", shape = \"plaintext\"] ;") :
|
||||
[ pra (j:ps) nod t | (j,t) <- zip [0..] trees] ++
|
||||
concat [prf (j:ps) t | (j,t) <- zip [0..] trees]
|
||||
prn ps (N (bi,at,val,_,_)) =
|
||||
let
|
||||
lab =
|
||||
"\"" ++
|
||||
prb bi ++
|
||||
prc at val ++
|
||||
"\""
|
||||
in if oElem (iOpt "g") opts then (lab,lab) else (show(show (ps :: [Int])),lab)
|
||||
prb [] = ""
|
||||
prb bi = "\\" ++ concat (intersperse "," (map (prt_ . fst) bi)) ++ " -> "
|
||||
pra i nod t@(Tr (node,_)) = nod ++ arr ++ fst (prn i node) ++ " [style = \"solid\"];"
|
||||
prc a v
|
||||
| oElem (iOpt "c") opts = prt_ v
|
||||
| oElem (iOpt "f") opts = prt_ a
|
||||
| otherwise = prt_ a ++ " : " ++ prt_ v
|
||||
arr = if oElem (iOpt "g") opts then " -> " else " -- "
|
||||
|
||||
prGraph opts ns = concat $ map (++"\n") $ [graph ++ "{\n"] ++ ns ++ ["}"] where
|
||||
graph = if oElem (iOpt "g") opts then "digraph" else "graph"
|
||||
Reference in New Issue
Block a user