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"
29 xmlns:
bison=
"http://www.gnu.org/software/bison/">
31 <xsl:import href=
"bison.xsl"/>
32 <xsl:output method=
"text" encoding=
"UTF-8" indent=
"no"/>
34 <xsl:template match=
"/">
35 <xsl:apply-templates select=
"bison-xml-report"/>
38 <xsl:template match=
"bison-xml-report">
39 <xsl:apply-templates select=
"automaton"/>
42 <xsl:template match=
"automaton">
43 <xsl:text>digraph Automaton {
</xsl:text>
44 <xsl:apply-templates select=
"state"/>
45 <xsl:text>}
</xsl:text>
48 <xsl:template match=
"automaton/state">
49 <xsl:call-template name=
"output-node">
50 <xsl:with-param name=
"number" select=
"@number"/>
51 <xsl:with-param name=
"label">
52 <xsl:value-of select=
"@number"/>
53 <xsl:apply-templates select=
"itemset/item"/>
56 <xsl:apply-templates select=
"actions/transitions"/>
59 <xsl:template match=
"actions/transitions">
60 <xsl:apply-templates select=
"transition"/>
63 <xsl:template match=
"item">
64 <xsl:apply-templates select=
"key('bison:ruleNumber', @rule-number)">
65 <xsl:with-param name=
"point" select=
"@point"/>
66 </xsl:apply-templates>
67 <xsl:apply-templates select=
"lookaheads"/>
70 <xsl:template match=
"rule">
71 <xsl:param name=
"point"/>
72 <xsl:text> </xsl:text>
73 <xsl:value-of select=
"lhs"/>
74 <xsl:text> -
></xsl:text>
75 <xsl:if test=
"$point = 0">
76 <xsl:text> .
</xsl:text>
78 <xsl:for-each select=
"rhs/symbol|rhs/empty">
79 <xsl:apply-templates select=
"."/>
80 <xsl:if test=
"$point = position()">
81 <xsl:text> .
</xsl:text>
86 <xsl:template match=
"symbol">
87 <xsl:text> </xsl:text>
88 <xsl:value-of select=
"."/>
91 <xsl:template match=
"empty"/>
93 <xsl:template match=
"lookaheads">
94 <xsl:text>[
</xsl:text>
95 <xsl:apply-templates select=
"symbol"/>
96 <xsl:text>]
</xsl:text>
99 <xsl:template match=
"lookaheads/symbol">
100 <xsl:value-of select=
"."/>
101 <xsl:if test=
"position() != last()">
102 <xsl:text>,
</xsl:text>
106 <xsl:template match=
"transition">
107 <xsl:call-template name=
"output-edge">
108 <xsl:with-param name=
"src" select=
"../../../@number"/>
109 <xsl:with-param name=
"dst" select=
"@state"/>
110 <xsl:with-param name=
"style">
112 <xsl:when test=
"@symbol = 'error'">
113 <xsl:text>dotted
</xsl:text>
115 <xsl:when test=
"@type = 'shift'">
116 <xsl:text>solid
</xsl:text>
119 <xsl:text>dashed
</xsl:text>
123 <xsl:with-param name=
"label">
124 <xsl:if test=
"not(@symbol = 'error')">
125 <xsl:value-of select=
"@symbol"/>
131 <xsl:template name=
"output-node">
132 <xsl:param name=
"number"/>
133 <xsl:param name=
"label"/>
134 <xsl:text> </xsl:text>
135 <xsl:value-of select=
"$number"/>
136 <xsl:text> [
label="
</xsl:text>
137 <xsl:call-template name=
"escape">
138 <xsl:with-param name=
"subject" select=
"$label"/>
140 <xsl:text>"] </xsl:text>
143 <xsl:template name="output-edge
">
144 <xsl:param name="src
"/>
145 <xsl:param name="dst
"/>
146 <xsl:param name="style
"/>
147 <xsl:param name="label
"/>
148 <xsl:text> </xsl:text>
149 <xsl:value-of select="$src
"/>
150 <xsl:text> -> </xsl:text>
151 <xsl:value-of select="$dst
"/>
152 <xsl:text> [style=</xsl:text>
153 <xsl:value-of select="$style
"/>
154 <xsl:if test="$label and $label != ''
">
155 <xsl:text> label="</xsl:text>
156 <xsl:call-template name=
"escape">
157 <xsl:with-param name=
"subject" select=
"$label"/>
159 <xsl:text>"</xsl:text>
161 <xsl:text>] </xsl:text>
164 <xsl:template name="escape
">
165 <xsl:param name="subject
"/> <!-- required -->
166 <xsl:call-template name="string-replace
">
167 <xsl:with-param name="subject
">
168 <xsl:call-template name="string-replace
">
169 <xsl:with-param name="subject
">
170 <xsl:call-template name="string-replace
">
171 <xsl:with-param name="subject
" select="$subject
"/>
172 <xsl:with-param name="search
" select="'\'
"/>
173 <xsl:with-param name="replace
" select="'\\'
"/>
176 <xsl:with-param name="search
" select="'
"'
"/>
177 <xsl:with-param name="replace
" select="'\
"'
"/>
180 <xsl:with-param name="search
" select="'
'
"/>
181 <xsl:with-param name="replace
" select="'\n'
"/>
185 <xsl:template name="string-replace
">
186 <xsl:param name="subject
"/>
187 <xsl:param name="search
"/>
188 <xsl:param name="replace
"/>
190 <xsl:when test="contains($subject, $search)
">
191 <xsl:variable name="before
" select="substring-before($subject, $search)
"/>
192 <xsl:variable name="after
" select="substring-after($subject, $search)
"/>
193 <xsl:value-of select="$before
"/>
194 <xsl:value-of select="$replace
"/>
195 <xsl:call-template name="string-replace
">
196 <xsl:with-param name="subject
" select="$after
"/>
197 <xsl:with-param name="search
" select="$search
"/>
198 <xsl:with-param name="replace
" select="$replace
"/>
202 <xsl:value-of select="$subject
"/>