1 <?xml version=
"1.0" encoding=
"UTF-8"?>
4 xml2dot.xsl - transform Bison XML Report into DOT.
7 Copyright (C) 2007 Free Software Foundation, Inc.
9 This file is part of Bison, the GNU Compiler Compiler.
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 Written by Wojciech Polak <polak@gnu.org>.
27 <xsl:stylesheet version=
"1.0"
28 xmlns:
xsl=
"http://www.w3.org/1999/XSL/Transform">
30 <xsl:output method=
"text" encoding=
"UTF-8" indent=
"no"/>
32 <xsl:template match=
"/">
33 <xsl:apply-templates select=
"bison-xml-report"/>
36 <xsl:template match=
"bison-xml-report">
37 <xsl:apply-templates select=
"automaton"/>
40 <xsl:template match=
"automaton">
41 <xsl:text>digraph Automaton {
</xsl:text>
42 <xsl:apply-templates select=
"state"/>
43 <xsl:text>}
</xsl:text>
46 <xsl:template match=
"automaton/state">
47 <xsl:call-template name=
"output-node">
48 <xsl:with-param name=
"number" select=
"@number"/>
49 <xsl:with-param name=
"label">
50 <xsl:value-of select=
"@number"/>
51 <xsl:apply-templates select=
"itemset/rule"/>
54 <xsl:apply-templates select=
"actions/transitions"/>
57 <xsl:template match=
"actions/transitions">
58 <xsl:apply-templates select=
"transition"/>
61 <xsl:template match=
"rule">
62 <xsl:text> </xsl:text>
63 <xsl:value-of select=
"lhs"/>
64 <xsl:text> -
></xsl:text>
65 <xsl:apply-templates select=
"rhs/symbol|rhs/point|rhs/empty"/>
66 <xsl:apply-templates select=
"lookaheads"/>
69 <xsl:template match=
"symbol">
70 <xsl:text> </xsl:text>
71 <xsl:value-of select=
"."/>
74 <xsl:template match=
"point">
75 <xsl:text> .
</xsl:text>
78 <xsl:template match=
"empty">
79 <xsl:text> /* empty */
</xsl:text>
82 <xsl:template match=
"lookaheads">
83 <xsl:text>[
</xsl:text>
84 <xsl:apply-templates select=
"symbol"/>
85 <xsl:text>]
</xsl:text>
88 <xsl:template match=
"lookaheads/symbol">
89 <xsl:value-of select=
"."/>
90 <xsl:if test=
"position() != last()">
91 <xsl:text>,
</xsl:text>
95 <xsl:template match=
"transition">
96 <xsl:call-template name=
"output-edge">
97 <xsl:with-param name=
"src" select=
"../../../@number"/>
98 <xsl:with-param name=
"dst" select=
"@state"/>
99 <xsl:with-param name=
"style">
101 <xsl:when test=
"@symbol = 'error'">
102 <xsl:text>dotted
</xsl:text>
104 <xsl:when test=
"@type = 'shift'">
105 <xsl:text>solid
</xsl:text>
108 <xsl:text>dashed
</xsl:text>
112 <xsl:with-param name=
"label">
113 <xsl:if test=
"not(@symbol = 'error')">
114 <xsl:value-of select=
"@symbol"/>
120 <xsl:template name=
"output-node">
121 <xsl:param name=
"number"/>
122 <xsl:param name=
"label"/>
123 <xsl:text> </xsl:text>
124 <xsl:value-of select=
"$number"/>
125 <xsl:text> [
label="
</xsl:text>
126 <xsl:call-template name=
"escape">
127 <xsl:with-param name=
"subject" select=
"$label"/>
129 <xsl:text>"] </xsl:text>
132 <xsl:template name="output-edge
">
133 <xsl:param name="src
"/>
134 <xsl:param name="dst
"/>
135 <xsl:param name="style
"/>
136 <xsl:param name="label
"/>
137 <xsl:text> </xsl:text>
138 <xsl:value-of select="$src
"/>
139 <xsl:text> -> </xsl:text>
140 <xsl:value-of select="$dst
"/>
141 <xsl:text> [style=</xsl:text>
142 <xsl:value-of select="$style
"/>
143 <xsl:if test="$label and $label != ''
">
144 <xsl:text> label="</xsl:text>
145 <xsl:call-template name=
"escape">
146 <xsl:with-param name=
"subject" select=
"$label"/>
148 <xsl:text>"</xsl:text>
150 <xsl:text>] </xsl:text>
153 <xsl:template name="escape
">
154 <xsl:param name="subject
"/> <!-- required -->
155 <xsl:call-template name="string-replace
">
156 <xsl:with-param name="subject
">
157 <xsl:call-template name="string-replace
">
158 <xsl:with-param name="subject
">
159 <xsl:call-template name="string-replace
">
160 <xsl:with-param name="subject
" select="$subject
"/>
161 <xsl:with-param name="search
" select="'\'
"/>
162 <xsl:with-param name="replace
" select="'\\'
"/>
165 <xsl:with-param name="search
" select="'
"'
"/>
166 <xsl:with-param name="replace
" select="'\
"'
"/>
169 <xsl:with-param name="search
" select="'
'
"/>
170 <xsl:with-param name="replace
" select="'\n'
"/>
174 <xsl:template name="string-replace
">
175 <xsl:param name="subject
"/>
176 <xsl:param name="search
"/>
177 <xsl:param name="replace
"/>
179 <xsl:when test="contains($subject, $search)
">
180 <xsl:variable name="before
" select="substring-before($subject, $search)
"/>
181 <xsl:variable name="after
" select="substring-after($subject, $search)
"/>
182 <xsl:value-of select="$before
"/>
183 <xsl:value-of select="$replace
"/>
184 <xsl:call-template name="string-replace
">
185 <xsl:with-param name="subject
" select="$after
"/>
186 <xsl:with-param name="search
" select="$search
"/>
187 <xsl:with-param name="replace
" select="$replace
"/>
191 <xsl:value-of select="$subject
"/>