1 <?xml version=
"1.0" encoding=
"UTF-8"?>
4 xml2dot.xsl - transform Bison XML Report into DOT.
6 Copyright (C) 2007-2013 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"/>
59 node [fontname = courier, shape = box, colorscheme = paired6]
60 edge [fontname = courier]
63 <xsl:apply-templates select="state
"/>
64 <xsl:text>} </xsl:text>
67 <xsl:template match="automaton/state
">
68 <xsl:call-template name="output-node
">
69 <xsl:with-param name="number
" select="@number
"/>
70 <xsl:with-param name="label
">
71 <xsl:apply-templates select="itemset/item
"/>
74 <xsl:apply-templates select="actions/transitions
"/>
75 <xsl:apply-templates select="actions/reductions
">
76 <xsl:with-param name="staten
">
77 <xsl:value-of select="@number
"/>
79 </xsl:apply-templates>
82 <xsl:template match="actions/reductions
">
83 <xsl:param name="staten
"/>
84 <xsl:for-each select='reduction'>
85 <!-- These variables are needed because the current context can't be
86 refered to directly in XPath expressions. -->
87 <xsl:variable name="rul
">
88 <xsl:value-of select="@rule
"/>
90 <xsl:variable name="ena
">
91 <xsl:value-of select="@enabled
"/>
93 <!-- The foreach's body is protected by this, so that we are actually
94 going to iterate once per reduction rule, and not per lookahead. -->
95 <xsl:if test='not(preceding-sibling::*[@rule=$rul and @enabled=$ena])'>
96 <xsl:variable name="rule
">
98 <!-- The acceptation state is refered to as 'accept' in the XML, but
99 just as '0' in the DOT. -->
100 <xsl:when test="@rule='accept'
">
101 <xsl:text>0</xsl:text>
104 <xsl:value-of select="@rule
"/>
109 <!-- The edge's beginning -->
110 <xsl:call-template name="reduction-edge-start
">
111 <xsl:with-param name="state
" select="$staten
"/>
112 <xsl:with-param name="rule
" select="$rule
"/>
113 <xsl:with-param name="enabled
" select="@enabled
"/>
116 <!-- The edge's tokens -->
117 <!-- Don't show labels for the default action. In other cases, there will
118 always be at least one token, so 'label="[]
"' will not occur. -->
119 <xsl:if test='$rule!=0 and not(../reduction[@enabled=$ena and @rule=$rule and @symbol="$default
"])'>
120 <xsl:text>label="[
</xsl:text>
121 <xsl:for-each select='../reduction[@enabled=$ena and @rule=$rule]'
>
122 <xsl:call-template name=
"escape">
123 <xsl:with-param name=
"subject" select=
"@symbol"/>
125 <xsl:if test=
"position() != last ()">
126 <xsl:text>,
</xsl:text>
129 <xsl:text>]",
</xsl:text>
132 <!-- The edge's end -->
133 <xsl:text>style=solid]
</xsl:text>
135 <!-- The diamond representing the reduction -->
136 <xsl:call-template name=
"reduction-node">
137 <xsl:with-param name=
"state" select=
"$staten"/>
138 <xsl:with-param name=
"rule" select=
"$rule"/>
139 <xsl:with-param name=
"color">
141 <xsl:when test='@
enabled=
"true"'
>
142 <xsl:text>3</xsl:text>
145 <xsl:text>5</xsl:text>
154 <xsl:template match=
"actions/transitions">
155 <xsl:apply-templates select=
"transition"/>
158 <xsl:template match=
"item">
159 <xsl:param name=
"prev-rule-number"
160 select=
"preceding-sibling::item[1]/@rule-number"/>
161 <xsl:apply-templates select=
"key('bison:ruleByNumber', @rule-number)">
162 <xsl:with-param name=
"point" select=
"@point"/>
163 <xsl:with-param name=
"num" select=
"@rule-number"/>
164 <xsl:with-param name=
"prev-lhs"
165 select=
"key('bison:ruleByNumber', $prev-rule-number)/lhs[text()]"
167 </xsl:apply-templates>
168 <xsl:apply-templates select=
"lookaheads"/>
171 <xsl:template match=
"rule">
172 <xsl:param name=
"point"/>
173 <xsl:param name=
"num"/>
174 <xsl:param name=
"prev-lhs"/>
175 <xsl:text> </xsl:text>
177 <xsl:when test=
"$num < 10">
178 <xsl:text> </xsl:text>
180 <xsl:when test=
"$num < 100">
181 <xsl:text> </xsl:text>
184 <xsl:text></xsl:text>
187 <xsl:value-of select=
"$num"/>
188 <xsl:text> </xsl:text>
190 <xsl:when test=
"$prev-lhs = lhs[text()]">
191 <xsl:call-template name=
"lpad">
192 <xsl:with-param name=
"str" select=
"'|'"/>
193 <xsl:with-param name=
"pad" select=
"number(string-length(lhs[text()])) + 1"/>
197 <xsl:value-of select=
"lhs"/>
198 <xsl:text>:
</xsl:text>
201 <xsl:if test=
"$point = 0">
202 <xsl:text> .
</xsl:text>
204 <xsl:for-each select=
"rhs/symbol|rhs/empty">
205 <xsl:apply-templates select=
"."/>
206 <xsl:if test=
"$point = position()">
207 <xsl:text> .
</xsl:text>
212 <xsl:template match=
"symbol">
213 <xsl:text> </xsl:text>
214 <xsl:value-of select=
"."/>
217 <xsl:template match=
"empty"/>
219 <xsl:template match=
"lookaheads">
220 <xsl:text> [
</xsl:text>
221 <xsl:apply-templates select=
"symbol"/>
222 <xsl:text>]
</xsl:text>
225 <xsl:template match=
"lookaheads/symbol">
226 <xsl:value-of select=
"."/>
227 <xsl:if test=
"position() != last()">
228 <xsl:text>,
</xsl:text>
232 <xsl:template name=
"reduction-edge-start">
233 <xsl:param name=
"state"/>
234 <xsl:param name=
"rule"/>
235 <xsl:param name=
"enabled"/>
237 <xsl:text> </xsl:text>
238 <xsl:value-of select=
"$state"/>
239 <xsl:text> -
> "</xsl:text>
240 <xsl:value-of select="$state
"/>
241 <xsl:text>R</xsl:text>
242 <xsl:value-of select="$rule
"/>
243 <xsl:if test='$enabled = "false
"'>
244 <xsl:text>d</xsl:text>
246 <xsl:text>" [
</xsl:text>
249 <xsl:template name=
"reduction-node">
250 <xsl:param name=
"state"/>
251 <xsl:param name=
"rule"/>
252 <xsl:param name=
"color"/>
254 <xsl:text> "</xsl:text>
255 <xsl:value-of select="$state
"/>
256 <xsl:text>R</xsl:text>
257 <xsl:value-of select="$rule
"/>
258 <xsl:if test="$color =
5">
259 <xsl:text>d</xsl:text>
261 <xsl:text>" [
label=
"</xsl:text>
263 <xsl:when test="$rule =
0">
264 <xsl:text>Acc", fillcolor=
1</xsl:text>
267 <xsl:text>R
</xsl:text>
268 <xsl:value-of select=
"$rule"/>
269 <xsl:text>", fillcolor=</xsl:text>
270 <xsl:value-of select="$color
"/>
273 <xsl:text>, shape=diamond, style=filled] </xsl:text>
276 <xsl:template match="transition
">
277 <xsl:call-template name="output-edge
">
278 <xsl:with-param name="src
" select="../../../@number
"/>
279 <xsl:with-param name="dst
" select="@state
"/>
280 <xsl:with-param name="style
">
282 <xsl:when test="@symbol = 'error'
">
283 <xsl:text>dotted</xsl:text>
285 <xsl:when test="@type = 'shift'
">
286 <xsl:text>solid</xsl:text>
289 <xsl:text>dashed</xsl:text>
293 <xsl:with-param name="label
">
294 <xsl:if test="not(@symbol = 'error')
">
295 <xsl:value-of select="@symbol
"/>
301 <xsl:template name="output-node
">
302 <xsl:param name="number
"/>
303 <xsl:param name="label
"/>
304 <xsl:text> </xsl:text>
305 <xsl:value-of select="$number
"/>
306 <xsl:text> [label="</xsl:text>
307 <xsl:text>State
</xsl:text>
308 <xsl:value-of select=
"$number"/>
309 <xsl:text>\n
</xsl:text>
310 <xsl:call-template name=
"escape">
311 <xsl:with-param name=
"subject" select=
"$label"/>
313 <xsl:text>\l"]
</xsl:text>
316 <xsl:template name=
"output-edge">
317 <xsl:param name=
"src"/>
318 <xsl:param name=
"dst"/>
319 <xsl:param name=
"style"/>
320 <xsl:param name=
"label"/>
321 <xsl:text> </xsl:text>
322 <xsl:value-of select=
"$src"/>
323 <xsl:text> -
> </xsl:text>
324 <xsl:value-of select=
"$dst"/>
325 <xsl:text> [style=
</xsl:text>
326 <xsl:value-of select=
"$style"/>
327 <xsl:if test=
"$label and $label != ''">
328 <xsl:text> label="
</xsl:text>
329 <xsl:call-template name=
"escape">
330 <xsl:with-param name=
"subject" select=
"$label"/>
332 <xsl:text>"</xsl:text>
334 <xsl:text>] </xsl:text>
337 <xsl:template name="escape
">
338 <xsl:param name="subject
"/> <!-- required -->
339 <xsl:call-template name="string-replace
">
340 <xsl:with-param name="subject
">
341 <xsl:call-template name="string-replace
">
342 <xsl:with-param name="subject
">
343 <xsl:call-template name="string-replace
">
344 <xsl:with-param name="subject
" select="$subject
"/>
345 <xsl:with-param name="search
" select="'\'
"/>
346 <xsl:with-param name="replace
" select="'\\'
"/>
349 <xsl:with-param name="search
" select="'
"'
"/>
350 <xsl:with-param name="replace
" select="'\
"'
"/>
353 <xsl:with-param name="search
" select="'
'
"/>
354 <xsl:with-param name="replace
" select="'\l'
"/>
358 <xsl:template name="string-replace
">
359 <xsl:param name="subject
"/>
360 <xsl:param name="search
"/>
361 <xsl:param name="replace
"/>
363 <xsl:when test="contains($subject, $search)
">
364 <xsl:variable name="before
" select="substring-before($subject, $search)
"/>
365 <xsl:variable name="after
" select="substring-after($subject, $search)
"/>
366 <xsl:value-of select="$before
"/>
367 <xsl:value-of select="$replace
"/>
368 <xsl:call-template name="string-replace
">
369 <xsl:with-param name="subject
" select="$after
"/>
370 <xsl:with-param name="search
" select="$search
"/>
371 <xsl:with-param name="replace
" select="$replace
"/>
375 <xsl:value-of select="$subject
"/>
380 <xsl:template name="lpad
">
381 <xsl:param name="str
" select="''
"/>
382 <xsl:param name="pad
" select="0"/>
383 <xsl:variable name="diff
" select="$pad - string-length($str)
" />
385 <xsl:when test="$diff
< 0">
386 <xsl:value-of select="$str
"/>
389 <xsl:call-template name="space
">
390 <xsl:with-param name="repeat
" select="$diff
"/>
392 <xsl:value-of select="$str
"/>