mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-22 19:22:50 -06:00
Added module graph visualization.
This commit is contained in:
@@ -52,6 +52,7 @@ import qualified PrintParser as Prt
|
|||||||
import GFC
|
import GFC
|
||||||
import qualified MkGFC as MC
|
import qualified MkGFC as MC
|
||||||
import PrintCFGrammar (prCanonAsCFGM)
|
import PrintCFGrammar (prCanonAsCFGM)
|
||||||
|
import VisualizeGrammar (visualizeGrammar)
|
||||||
|
|
||||||
import MyParser
|
import MyParser
|
||||||
|
|
||||||
@@ -229,6 +230,7 @@ customMultiGrammarPrinter =
|
|||||||
(strCI "gfcm", MC.prCanon)
|
(strCI "gfcm", MC.prCanon)
|
||||||
,(strCI "header", MC.prCanonMGr)
|
,(strCI "header", MC.prCanonMGr)
|
||||||
,(strCI "cfgm", prCanonAsCFGM)
|
,(strCI "cfgm", prCanonAsCFGM)
|
||||||
|
,(strCI "graph", visualizeGrammar)
|
||||||
]
|
]
|
||||||
++ moreCustomMultiGrammarPrinter
|
++ moreCustomMultiGrammarPrinter
|
||||||
|
|
||||||
|
|||||||
79
src/GF/Visualization/VisualizeGrammar.hs
Normal file
79
src/GF/Visualization/VisualizeGrammar.hs
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
-- Print a graph of module dependencies in Graphviz DOT format
|
||||||
|
module VisualizeGrammar where
|
||||||
|
|
||||||
|
import qualified Modules as M
|
||||||
|
import GFC
|
||||||
|
import Ident
|
||||||
|
|
||||||
|
import Data.List (intersperse)
|
||||||
|
import Data.Maybe (maybeToList)
|
||||||
|
|
||||||
|
data GrType = GrAbstract | GrConcrete | GrResource
|
||||||
|
deriving Show
|
||||||
|
|
||||||
|
data Node = Node {
|
||||||
|
label :: String,
|
||||||
|
grtype :: GrType,
|
||||||
|
extends :: [String],
|
||||||
|
opens :: [String],
|
||||||
|
implements :: Maybe String
|
||||||
|
}
|
||||||
|
deriving Show
|
||||||
|
|
||||||
|
|
||||||
|
visualizeGrammar :: CanonGrammar -> String
|
||||||
|
visualizeGrammar gr = prGraph ns
|
||||||
|
where
|
||||||
|
ns = [ toNode i m | (i,M.ModMod m) <- M.modules gr ]
|
||||||
|
|
||||||
|
toNode :: Ident -> M.Module Ident f Info -> Node
|
||||||
|
toNode i m = Node {
|
||||||
|
label = prIdent i,
|
||||||
|
grtype = t,
|
||||||
|
extends = map prIdent (M.extends m),
|
||||||
|
opens = map openName (M.opens m),
|
||||||
|
implements = is
|
||||||
|
}
|
||||||
|
where
|
||||||
|
(t,is) = case M.mtype m of
|
||||||
|
M.MTAbstract -> (GrAbstract, Nothing)
|
||||||
|
M.MTConcrete i -> (GrConcrete, Just (prIdent i))
|
||||||
|
M.MTResource -> (GrResource, Nothing)
|
||||||
|
-- FIXME: transfer and resource
|
||||||
|
|
||||||
|
openName :: M.OpenSpec Ident -> String
|
||||||
|
openName (M.OSimple q i) = prIdent i
|
||||||
|
openName (M.OQualif q i _) = prIdent i
|
||||||
|
|
||||||
|
prGraph :: [Node] -> String
|
||||||
|
prGraph ns = concat $ map (++"\n") $ ["digraph {\n"] ++ map prNode ns ++ ["}"]
|
||||||
|
|
||||||
|
prNode :: Node -> String
|
||||||
|
prNode n = concat (map (++";\n") stmts)
|
||||||
|
where
|
||||||
|
l = label n
|
||||||
|
stmts = [l ++ " [" ++ prAttributes attrs ++ "]"]
|
||||||
|
++ map (prExtend l) (extends n)
|
||||||
|
++ map (prOpen l) (opens n)
|
||||||
|
++ map (prImplement l) (maybeToList (implements n))
|
||||||
|
style = case grtype n of
|
||||||
|
GrAbstract -> "solid"
|
||||||
|
GrConcrete -> "dashed"
|
||||||
|
GrResource -> "dotted"
|
||||||
|
attrs = [("style",style)]
|
||||||
|
|
||||||
|
|
||||||
|
prExtend :: String -> String -> String
|
||||||
|
prExtend f t = prEdge f t []
|
||||||
|
|
||||||
|
prOpen :: String -> String -> String
|
||||||
|
prOpen f t = prEdge f t [("style","dotted")]
|
||||||
|
|
||||||
|
prImplement :: String -> String -> String
|
||||||
|
prImplement f t = prEdge f t [("arrowhead","empty"),("style","dashed")]
|
||||||
|
|
||||||
|
prEdge :: String -> String -> [(String,String)] -> String
|
||||||
|
prEdge f t as = f ++ " -> " ++ t ++ " [" ++ prAttributes as ++ "]"
|
||||||
|
|
||||||
|
prAttributes :: [(String,String)] -> String
|
||||||
|
prAttributes = concat . intersperse ", " . map (\ (n,v) -> n ++ " = " ++ v)
|
||||||
@@ -8,8 +8,8 @@ GHCOPTFLAGS=-O $(GHCFLAGS)
|
|||||||
GHCFUDFLAG=
|
GHCFUDFLAG=
|
||||||
JAVAFLAGS=-target 1.4 -source 1.4
|
JAVAFLAGS=-target 1.4 -source 1.4
|
||||||
|
|
||||||
HUGSINCLUDE =.:for-hugs:api:source:canonical:cf:grammar:infra:shell:useGrammar:compile:newparsing:trace:
|
HUGSINCLUDE =.:for-hugs:api:source:canonical:cf:grammar:infra:shell:useGrammar:compile:newparsing:trace:visualization:
|
||||||
BASICINCLUDE =-iapi -icompile -igrammar -iinfra -ishell -isource -icanonical -iuseGrammar -icf -inewparsing -iparsers -inotrace -icfgm -ispeech
|
BASICINCLUDE =-iapi -icompile -igrammar -iinfra -ishell -isource -icanonical -iuseGrammar -icf -inewparsing -iparsers -inotrace -icfgm -ispeech -ivisualization
|
||||||
GHCINCLUDE =-ifor-ghc $(BASICINCLUDE)
|
GHCINCLUDE =-ifor-ghc $(BASICINCLUDE)
|
||||||
GHCINCLUDENOFUD=-ifor-ghc-nofud $(BASICINCLUDE)
|
GHCINCLUDENOFUD=-ifor-ghc-nofud $(BASICINCLUDE)
|
||||||
GHCINCLUDEGFT =-ifor-gft $(BASICINCLUDE)
|
GHCINCLUDEGFT =-ifor-gft $(BASICINCLUDE)
|
||||||
|
|||||||
Reference in New Issue
Block a user