1 <?xml version=
"1.0" encoding=
"UTF-8"?>
4 xml2dot.xsl - transform Bison XML Report into DOT.
6 Copyright (C) 2007-2010 Free Software Foundation, Inc.
8 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>.
23 Written by Wojciech Polak <polak@gnu.org>.
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/">
30 <xsl:import href=
"bison.xsl"/>
31 <xsl:output method=
"text" encoding=
"UTF-8" indent=
"no"/>
33 <xsl:template match=
"/">
34 <xsl:apply-templates select=
"bison-xml-report"/>
37 <xsl:template match=
"bison-xml-report">
38 <xsl:text>// Generated by GNU Bison
</xsl:text>
39 <xsl:value-of select=
"@version"/>
40 <xsl:text>.
</xsl:text>
41 <xsl:text>// Report bugs to
<</xsl:text>
42 <xsl:value-of select=
"@bug-report"/>
43 <xsl:text>>.
</xsl:text>
44 <xsl:text>// Home page:
<</xsl:text>
45 <xsl:value-of select=
"@url"/>
46 <xsl:text>>.
</xsl:text>
47 <xsl:apply-templates select=
"automaton">
48 <xsl:with-param name=
"filename" select=
"filename"/>
49 </xsl:apply-templates>
52 <xsl:template match=
"automaton">
53 <xsl:param name=
"filename"/>
54 <xsl:text>digraph "
</xsl:text>
55 <xsl:call-template name=
"escape">
56 <xsl:with-param name=
"subject" select=
"$filename"/>
58 <xsl:text>" { </xsl:text>
59 <xsl:apply-templates select="state
"/>
60 <xsl:text>} </xsl:text>
63 <xsl:template match="automaton/state
">
64 <xsl:call-template name="output-node
">
65 <xsl:with-param name="number
" select="@number
"/>
66 <xsl:with-param name="label
">
67 <xsl:value-of select="@number
"/>
68 <xsl:apply-templates select="itemset/item
"/>
71 <xsl:apply-templates select="actions/transitions
"/>
74 <xsl:template match="actions/transitions
">
75 <xsl:apply-templates select="transition
"/>
78 <xsl:template match="item
">
79 <xsl:apply-templates select="key('bison:ruleByNumber', @rule-number)
">
80 <xsl:with-param name="point
" select="@point
"/>
81 </xsl:apply-templates>
82 <xsl:apply-templates select="lookaheads
"/>
85 <xsl:template match="rule
">
86 <xsl:param name="point
"/>
87 <xsl:text> </xsl:text>
88 <xsl:value-of select="lhs
"/>
89 <xsl:text> -></xsl:text>
90 <xsl:if test="$point =
0">
91 <xsl:text> .</xsl:text>
93 <xsl:for-each select="rhs/symbol|rhs/empty
">
94 <xsl:apply-templates select=".
"/>
95 <xsl:if test="$point = position()
">
96 <xsl:text> .</xsl:text>
101 <xsl:template match="symbol
">
102 <xsl:text> </xsl:text>
103 <xsl:value-of select=".
"/>
106 <xsl:template match="empty
"/>
108 <xsl:template match="lookaheads
">
109 <xsl:text>[</xsl:text>
110 <xsl:apply-templates select="symbol
"/>
111 <xsl:text>]</xsl:text>
114 <xsl:template match="lookaheads/symbol
">
115 <xsl:value-of select=".
"/>
116 <xsl:if test="position() != last()
">
117 <xsl:text>, </xsl:text>
121 <xsl:template match="transition
">
122 <xsl:call-template name="output-edge
">
123 <xsl:with-param name="src
" select="../../../@number
"/>
124 <xsl:with-param name="dst
" select="@state
"/>
125 <xsl:with-param name="style
">
127 <xsl:when test="@symbol = 'error'
">
128 <xsl:text>dotted</xsl:text>
130 <xsl:when test="@type = 'shift'
">
131 <xsl:text>solid</xsl:text>
134 <xsl:text>dashed</xsl:text>
138 <xsl:with-param name="label
">
139 <xsl:if test="not(@symbol = 'error')
">
140 <xsl:value-of select="@symbol
"/>
146 <xsl:template name="output-node
">
147 <xsl:param name="number
"/>
148 <xsl:param name="label
"/>
149 <xsl:text> </xsl:text>
150 <xsl:value-of select="$number
"/>
151 <xsl:text> [label="</xsl:text>
152 <xsl:call-template name=
"escape">
153 <xsl:with-param name=
"subject" select=
"$label"/>
155 <xsl:text>"] </xsl:text>
158 <xsl:template name="output-edge
">
159 <xsl:param name="src
"/>
160 <xsl:param name="dst
"/>
161 <xsl:param name="style
"/>
162 <xsl:param name="label
"/>
163 <xsl:text> </xsl:text>
164 <xsl:value-of select="$src
"/>
165 <xsl:text> -> </xsl:text>
166 <xsl:value-of select="$dst
"/>
167 <xsl:text> [style=</xsl:text>
168 <xsl:value-of select="$style
"/>
169 <xsl:if test="$label and $label != ''
">
170 <xsl:text> label="</xsl:text>
171 <xsl:call-template name=
"escape">
172 <xsl:with-param name=
"subject" select=
"$label"/>
174 <xsl:text>"</xsl:text>
176 <xsl:text>] </xsl:text>
179 <xsl:template name="escape
">
180 <xsl:param name="subject
"/> <!-- required -->
181 <xsl:call-template name="string-replace
">
182 <xsl:with-param name="subject
">
183 <xsl:call-template name="string-replace
">
184 <xsl:with-param name="subject
">
185 <xsl:call-template name="string-replace
">
186 <xsl:with-param name="subject
" select="$subject
"/>
187 <xsl:with-param name="search
" select="'\'
"/>
188 <xsl:with-param name="replace
" select="'\\'
"/>
191 <xsl:with-param name="search
" select="'
"'
"/>
192 <xsl:with-param name="replace
" select="'\
"'
"/>
195 <xsl:with-param name="search
" select="'
'
"/>
196 <xsl:with-param name="replace
" select="'\n'
"/>
200 <xsl:template name="string-replace
">
201 <xsl:param name="subject
"/>
202 <xsl:param name="search
"/>
203 <xsl:param name="replace
"/>
205 <xsl:when test="contains($subject, $search)
">
206 <xsl:variable name="before
" select="substring-before($subject, $search)
"/>
207 <xsl:variable name="after
" select="substring-after($subject, $search)
"/>
208 <xsl:value-of select="$before
"/>
209 <xsl:value-of select="$replace
"/>
210 <xsl:call-template name="string-replace
">
211 <xsl:with-param name="subject
" select="$after
"/>
212 <xsl:with-param name="search
" select="$search
"/>
213 <xsl:with-param name="replace
" select="$replace
"/>
217 <xsl:value-of select="$subject
"/>