Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-ulex/BackEnds/Dot/dot-output.sml

raw
1 (* dot-output.sml
2 *
3 * COPYRIGHT (c) 2005
4 * John Reppy (http://www.cs.uchicago.edu/~jhr)
5 * Aaron Turon ([email protected])
6 * All rights reserved.
7 *
8 * Produce a .dot file from a DFA.
9 * (See www.graphviz.org for details about DOT)
10 *)
11
12 structure DotOutput : OUTPUT =
13 struct
14
15 structure RE = RegExp
16 structure Lex = LexGen
17 structure LO = LexOutputSpec
18
19 datatype attribute = ATTR of string * string
20 datatype node = NODE of string * attribute list
21 datatype di_edge = EDGE of string * string * attribute list
22 datatype di_graph = GRAPH of string * node list * di_edge list * attribute list
23
24 fun replBS str =
25 String.translate
26 (fn #"\\" => "\\\\" | #"\"" => "\\\"" | c => String.str c)
27 str
28
29 fun writeGraph (out, graph) = let
30 (* output a string *)
31 fun wr s = TextIO.output (out, s)
32 (* output a string list *)
33 fun wrs ss = wr (String.concat ss)
34 (* indent to some level *)
35 fun wrIndent 0 = ()
36 | wrIndent lvl = (wr " "; wrIndent (lvl - 1))
37 (* apply output functions, indenting each time *)
38 fun app indent f list =
39 List.app (fn x => (wrIndent indent; f x)) list
40 fun wrAttr (ATTR (name, value)) = wrs ([
41 "[ ", name, " = \"", value, "\" ]", "\n"
42 ])
43 fun wrNode (NODE (name, atts)) =
44 (wr name;
45 wr "\n";
46 app 2 wrAttr atts)
47 fun wrEdge (EDGE (no1, no2, atts)) =
48 (wrs ([no1, " -> ", no2, "\n"]);
49 app 2 wrAttr atts)
50 fun wrGraphAttr attr =
51 (wr "graph\n";
52 wrIndent 2;
53 wrAttr attr)
54 fun wrGraph (GRAPH (name, nodes, edges, atts)) =
55 (wrs (["digraph ", name, " {\n"]);
56 app 1 wrGraphAttr atts;
57 app 1 wrNode nodes;
58 app 1 wrEdge edges;
59 wr "}")
60 in wrGraph graph
61 end
62
63 fun mkGraph states = let
64 (* node id -> node name *)
65 fun name id = "Q" ^ Int.toString id
66 fun mkNode (LO.State{id, label, final = [], ...}) =
67 NODE (name id, [ATTR ("shape", "circle")])
68 | mkNode (LO.State{id, label, final = i::_, ...}) =
69 NODE (name id,
70 [ATTR ("shape", "doublecircle"),
71 ATTR ("label", (name id) ^ "/" ^ (Int.toString i))])
72 fun mkEdge fromID (symSet, LO.State{id, ...}) =
73 EDGE (name fromID, name id,
74 [ATTR ("label", replBS (RE.toString (RE.mkSymSet symSet)))])
75 fun mkEdges (LO.State{id, next, ...}) =
76 List.map (mkEdge id) (List.rev (!next))
77 fun mkRule (i, re) = String.concat (
78 ["Rule ",
79 Int.toString i,
80 ": ",
81 replBS (RE.toString re),
82 "\\n"])
83 (* node for input REs *)
84 fun mkRules res =
85 NODE ("Rules",
86 [ATTR ("label", Vector.foldli
87 (fn (i, r, s) => s ^ (mkRule (i, r)))
88 "" res),
89 ATTR ("shape", "plaintext"),
90 ATTR ("fontname", "Courier")])
91 val nodes' = List.map mkNode states
92 val nodes = nodes'
93 val edges = List.concat (List.map mkEdges states)
94 in GRAPH ("DFA", nodes, edges,
95 [ATTR ("size", "7,10"),
96 ATTR ("rankdir", "LR")])
97 end
98
99 fun output (spec, fname) = let
100 val LO.Spec {dfa, startStates, ...} = spec
101 val out = TextIO.openOut (fname ^ ".dot")
102 val graph = mkGraph dfa
103 in
104 print (" writing " ^ fname ^ ".dot\n");
105 writeGraph (out, graph)
106 before TextIO.closeOut out
107 end
108
109 end