]>
Commit | Line | Data |
---|---|---|
1 | <?xml version="1.0" encoding="UTF-8"?> | |
2 | ||
3 | <!-- | |
4 | xml2dot.xsl - transform Bison XML Report into DOT. | |
5 | ||
6 | Copyright (C) 2007-2010 Free Software Foundation, Inc. | |
7 | ||
8 | This file is part of Bison, the GNU Compiler Compiler. | |
9 | ||
10 | This program is free software: you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation, either version 3 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 | ||
23 | Written by Wojciech Polak <polak@gnu.org>. | |
24 | --> | |
25 | ||
26 | <xsl:stylesheet version="1.0" | |
27 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
28 | xmlns:bison="http://www.gnu.org/software/bison/"> | |
29 | ||
30 | <xsl:import href="bison.xsl"/> | |
31 | <xsl:output method="text" encoding="UTF-8" indent="no"/> | |
32 | ||
33 | <xsl:template match="/"> | |
34 | <xsl:apply-templates select="bison-xml-report"/> | |
35 | </xsl:template> | |
36 | ||
37 | <xsl:template match="bison-xml-report"> | |
38 | <xsl:apply-templates select="automaton"/> | |
39 | </xsl:template> | |
40 | ||
41 | <xsl:template match="automaton"> | |
42 | <xsl:text>digraph Automaton { </xsl:text> | |
43 | <xsl:apply-templates select="state"/> | |
44 | <xsl:text>} </xsl:text> | |
45 | </xsl:template> | |
46 | ||
47 | <xsl:template match="automaton/state"> | |
48 | <xsl:call-template name="output-node"> | |
49 | <xsl:with-param name="number" select="@number"/> | |
50 | <xsl:with-param name="label"> | |
51 | <xsl:value-of select="@number"/> | |
52 | <xsl:apply-templates select="itemset/item"/> | |
53 | </xsl:with-param> | |
54 | </xsl:call-template> | |
55 | <xsl:apply-templates select="actions/transitions"/> | |
56 | </xsl:template> | |
57 | ||
58 | <xsl:template match="actions/transitions"> | |
59 | <xsl:apply-templates select="transition"/> | |
60 | </xsl:template> | |
61 | ||
62 | <xsl:template match="item"> | |
63 | <xsl:apply-templates select="key('bison:ruleByNumber', @rule-number)"> | |
64 | <xsl:with-param name="point" select="@point"/> | |
65 | </xsl:apply-templates> | |
66 | <xsl:apply-templates select="lookaheads"/> | |
67 | </xsl:template> | |
68 | ||
69 | <xsl:template match="rule"> | |
70 | <xsl:param name="point"/> | |
71 | <xsl:text> </xsl:text> | |
72 | <xsl:value-of select="lhs"/> | |
73 | <xsl:text> -></xsl:text> | |
74 | <xsl:if test="$point = 0"> | |
75 | <xsl:text> .</xsl:text> | |
76 | </xsl:if> | |
77 | <xsl:for-each select="rhs/symbol|rhs/empty"> | |
78 | <xsl:apply-templates select="."/> | |
79 | <xsl:if test="$point = position()"> | |
80 | <xsl:text> .</xsl:text> | |
81 | </xsl:if> | |
82 | </xsl:for-each> | |
83 | </xsl:template> | |
84 | ||
85 | <xsl:template match="symbol"> | |
86 | <xsl:text> </xsl:text> | |
87 | <xsl:value-of select="."/> | |
88 | </xsl:template> | |
89 | ||
90 | <xsl:template match="empty"/> | |
91 | ||
92 | <xsl:template match="lookaheads"> | |
93 | <xsl:text>[</xsl:text> | |
94 | <xsl:apply-templates select="symbol"/> | |
95 | <xsl:text>]</xsl:text> | |
96 | </xsl:template> | |
97 | ||
98 | <xsl:template match="lookaheads/symbol"> | |
99 | <xsl:value-of select="."/> | |
100 | <xsl:if test="position() != last()"> | |
101 | <xsl:text>, </xsl:text> | |
102 | </xsl:if> | |
103 | </xsl:template> | |
104 | ||
105 | <xsl:template match="transition"> | |
106 | <xsl:call-template name="output-edge"> | |
107 | <xsl:with-param name="src" select="../../../@number"/> | |
108 | <xsl:with-param name="dst" select="@state"/> | |
109 | <xsl:with-param name="style"> | |
110 | <xsl:choose> | |
111 | <xsl:when test="@symbol = 'error'"> | |
112 | <xsl:text>dotted</xsl:text> | |
113 | </xsl:when> | |
114 | <xsl:when test="@type = 'shift'"> | |
115 | <xsl:text>solid</xsl:text> | |
116 | </xsl:when> | |
117 | <xsl:otherwise> | |
118 | <xsl:text>dashed</xsl:text> | |
119 | </xsl:otherwise> | |
120 | </xsl:choose> | |
121 | </xsl:with-param> | |
122 | <xsl:with-param name="label"> | |
123 | <xsl:if test="not(@symbol = 'error')"> | |
124 | <xsl:value-of select="@symbol"/> | |
125 | </xsl:if> | |
126 | </xsl:with-param> | |
127 | </xsl:call-template> | |
128 | </xsl:template> | |
129 | ||
130 | <xsl:template name="output-node"> | |
131 | <xsl:param name="number"/> | |
132 | <xsl:param name="label"/> | |
133 | <xsl:text> </xsl:text> | |
134 | <xsl:value-of select="$number"/> | |
135 | <xsl:text> [label="</xsl:text> | |
136 | <xsl:call-template name="escape"> | |
137 | <xsl:with-param name="subject" select="$label"/> | |
138 | </xsl:call-template> | |
139 | <xsl:text>"] </xsl:text> | |
140 | </xsl:template> | |
141 | ||
142 | <xsl:template name="output-edge"> | |
143 | <xsl:param name="src"/> | |
144 | <xsl:param name="dst"/> | |
145 | <xsl:param name="style"/> | |
146 | <xsl:param name="label"/> | |
147 | <xsl:text> </xsl:text> | |
148 | <xsl:value-of select="$src"/> | |
149 | <xsl:text> -> </xsl:text> | |
150 | <xsl:value-of select="$dst"/> | |
151 | <xsl:text> [style=</xsl:text> | |
152 | <xsl:value-of select="$style"/> | |
153 | <xsl:if test="$label and $label != ''"> | |
154 | <xsl:text> label="</xsl:text> | |
155 | <xsl:call-template name="escape"> | |
156 | <xsl:with-param name="subject" select="$label"/> | |
157 | </xsl:call-template> | |
158 | <xsl:text>"</xsl:text> | |
159 | </xsl:if> | |
160 | <xsl:text>] </xsl:text> | |
161 | </xsl:template> | |
162 | ||
163 | <xsl:template name="escape"> | |
164 | <xsl:param name="subject"/> <!-- required --> | |
165 | <xsl:call-template name="string-replace"> | |
166 | <xsl:with-param name="subject"> | |
167 | <xsl:call-template name="string-replace"> | |
168 | <xsl:with-param name="subject"> | |
169 | <xsl:call-template name="string-replace"> | |
170 | <xsl:with-param name="subject" select="$subject"/> | |
171 | <xsl:with-param name="search" select="'\'"/> | |
172 | <xsl:with-param name="replace" select="'\\'"/> | |
173 | </xsl:call-template> | |
174 | </xsl:with-param> | |
175 | <xsl:with-param name="search" select="'"'"/> | |
176 | <xsl:with-param name="replace" select="'\"'"/> | |
177 | </xsl:call-template> | |
178 | </xsl:with-param> | |
179 | <xsl:with-param name="search" select="' '"/> | |
180 | <xsl:with-param name="replace" select="'\n'"/> | |
181 | </xsl:call-template> | |
182 | </xsl:template> | |
183 | ||
184 | <xsl:template name="string-replace"> | |
185 | <xsl:param name="subject"/> | |
186 | <xsl:param name="search"/> | |
187 | <xsl:param name="replace"/> | |
188 | <xsl:choose> | |
189 | <xsl:when test="contains($subject, $search)"> | |
190 | <xsl:variable name="before" select="substring-before($subject, $search)"/> | |
191 | <xsl:variable name="after" select="substring-after($subject, $search)"/> | |
192 | <xsl:value-of select="$before"/> | |
193 | <xsl:value-of select="$replace"/> | |
194 | <xsl:call-template name="string-replace"> | |
195 | <xsl:with-param name="subject" select="$after"/> | |
196 | <xsl:with-param name="search" select="$search"/> | |
197 | <xsl:with-param name="replace" select="$replace"/> | |
198 | </xsl:call-template> | |
199 | </xsl:when> | |
200 | <xsl:otherwise> | |
201 | <xsl:value-of select="$subject"/> | |
202 | </xsl:otherwise> | |
203 | </xsl:choose> | |
204 | </xsl:template> | |
205 | ||
206 | </xsl:stylesheet> |