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>
206 <xsl:for-each select=
"rhs/symbol|rhs/empty">
207 <xsl:apply-templates select=
"."/>
208 <xsl:if test=
"$point = position()">
209 <xsl:text> .
</xsl:text>
214 <xsl:template match=
"symbol">
215 <xsl:text> </xsl:text>
216 <xsl:value-of select=
"."/>
219 <xsl:template match=
"empty">
220 <xsl:text> %empty
</xsl:text>
223 <xsl:template match=
"lookaheads">
224 <xsl:text> [
</xsl:text>
225 <xsl:apply-templates select=
"symbol"/>
226 <xsl:text>]
</xsl:text>
229 <xsl:template match=
"lookaheads/symbol">
230 <xsl:value-of select=
"."/>
231 <xsl:if test=
"position() != last()">
232 <xsl:text>,
</xsl:text>
236 <xsl:template name=
"reduction-edge-start">
237 <xsl:param name=
"state"/>
238 <xsl:param name=
"rule"/>
239 <xsl:param name=
"enabled"/>
241 <xsl:text> </xsl:text>
242 <xsl:value-of select=
"$state"/>
243 <xsl:text> -
> "</xsl:text>
244 <xsl:value-of select="$state
"/>
245 <xsl:text>R</xsl:text>
246 <xsl:value-of select="$rule
"/>
247 <xsl:if test='$enabled = "false
"'>
248 <xsl:text>d</xsl:text>
250 <xsl:text>" [
</xsl:text>
253 <xsl:template name=
"reduction-node">
254 <xsl:param name=
"state"/>
255 <xsl:param name=
"rule"/>
256 <xsl:param name=
"color"/>
258 <xsl:text> "</xsl:text>
259 <xsl:value-of select="$state
"/>
260 <xsl:text>R</xsl:text>
261 <xsl:value-of select="$rule
"/>
262 <xsl:if test="$color =
5">
263 <xsl:text>d</xsl:text>
265 <xsl:text>" [
label=
"</xsl:text>
267 <xsl:when test="$rule =
0">
268 <xsl:text>Acc", fillcolor=
1</xsl:text>
271 <xsl:text>R
</xsl:text>
272 <xsl:value-of select=
"$rule"/>
273 <xsl:text>", fillcolor=</xsl:text>
274 <xsl:value-of select="$color
"/>
277 <xsl:text>, shape=diamond, style=filled] </xsl:text>
280 <xsl:template match="transition
">
281 <xsl:call-template name="output-edge
">
282 <xsl:with-param name="src
" select="../../../@number
"/>
283 <xsl:with-param name="dst
" select="@state
"/>
284 <xsl:with-param name="style
">
286 <xsl:when test="@symbol = 'error'
">
287 <xsl:text>dotted</xsl:text>
289 <xsl:when test="@type = 'shift'
">
290 <xsl:text>solid</xsl:text>
293 <xsl:text>dashed</xsl:text>
297 <xsl:with-param name="label
">
298 <xsl:if test="not(@symbol = 'error')
">
299 <xsl:value-of select="@symbol
"/>
305 <xsl:template name="output-node
">
306 <xsl:param name="number
"/>
307 <xsl:param name="label
"/>
308 <xsl:text> </xsl:text>
309 <xsl:value-of select="$number
"/>
310 <xsl:text> [label="</xsl:text>
311 <xsl:text>State
</xsl:text>
312 <xsl:value-of select=
"$number"/>
313 <xsl:text>\n
</xsl:text>
314 <xsl:call-template name=
"escape">
315 <xsl:with-param name=
"subject" select=
"$label"/>
317 <xsl:text>\l"]
</xsl:text>
320 <xsl:template name=
"output-edge">
321 <xsl:param name=
"src"/>
322 <xsl:param name=
"dst"/>
323 <xsl:param name=
"style"/>
324 <xsl:param name=
"label"/>
325 <xsl:text> </xsl:text>
326 <xsl:value-of select=
"$src"/>
327 <xsl:text> -
> </xsl:text>
328 <xsl:value-of select=
"$dst"/>
329 <xsl:text> [style=
</xsl:text>
330 <xsl:value-of select=
"$style"/>
331 <xsl:if test=
"$label and $label != ''">
332 <xsl:text> label="
</xsl:text>
333 <xsl:call-template name=
"escape">
334 <xsl:with-param name=
"subject" select=
"$label"/>
336 <xsl:text>"</xsl:text>
338 <xsl:text>] </xsl:text>
341 <xsl:template name="escape
">
342 <xsl:param name="subject
"/> <!-- required -->
343 <xsl:call-template name="string-replace
">
344 <xsl:with-param name="subject
">
345 <xsl:call-template name="string-replace
">
346 <xsl:with-param name="subject
">
347 <xsl:call-template name="string-replace
">
348 <xsl:with-param name="subject
" select="$subject
"/>
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="'\
"'
"/>
357 <xsl:with-param name="search
" select="'
'
"/>
358 <xsl:with-param name="replace
" select="'\l'
"/>
362 <xsl:template name="string-replace
">
363 <xsl:param name="subject
"/>
364 <xsl:param name="search
"/>
365 <xsl:param name="replace
"/>
367 <xsl:when test="contains($subject, $search)
">
368 <xsl:variable name="before
" select="substring-before($subject, $search)
"/>
369 <xsl:variable name="after
" select="substring-after($subject, $search)
"/>
370 <xsl:value-of select="$before
"/>
371 <xsl:value-of select="$replace
"/>
372 <xsl:call-template name="string-replace
">
373 <xsl:with-param name="subject
" select="$after
"/>
374 <xsl:with-param name="search
" select="$search
"/>
375 <xsl:with-param name="replace
" select="$replace
"/>
379 <xsl:value-of select="$subject
"/>
384 <xsl:template name="lpad
">
385 <xsl:param name="str
" select="''
"/>
386 <xsl:param name="pad
" select="0"/>
387 <xsl:variable name="diff
" select="$pad - string-length($str)
" />
389 <xsl:when test="$diff
< 0">
390 <xsl:value-of select="$str
"/>
393 <xsl:call-template name="space
">
394 <xsl:with-param name="repeat
" select="$diff
"/>
396 <xsl:value-of select="$str
"/>