]> git.saurik.com Git - bison.git/blob - data/xslt/xml2dot.xsl
Merge remote-tracking branch 'origin/maint'
[bison.git] / data / xslt / xml2dot.xsl
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!--
4 xml2dot.xsl - transform Bison XML Report into DOT.
5
6 Copyright (C) 2007-2012 Free Software Foundation, Inc.
7
8 This file is part of Bison, the GNU Compiler Compiler.
9
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.
14
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.
19
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/>.
22
23 Written by Wojciech Polak <polak@gnu.org>.
24 -->
25
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/">
29
30 <xsl:import href="bison.xsl"/>
31 <xsl:output method="text" encoding="UTF-8" indent="no"/>
32
33 <xsl:template match="/">
34 <xsl:apply-templates select="bison-xml-report"/>
35 </xsl:template>
36
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>.&#10;</xsl:text>
41 <xsl:text>// Report bugs to &lt;</xsl:text>
42 <xsl:value-of select="@bug-report"/>
43 <xsl:text>&gt;.&#10;</xsl:text>
44 <xsl:text>// Home page: &lt;</xsl:text>
45 <xsl:value-of select="@url"/>
46 <xsl:text>&gt;.&#10;&#10;</xsl:text>
47 <xsl:apply-templates select="automaton">
48 <xsl:with-param name="filename" select="filename"/>
49 </xsl:apply-templates>
50 </xsl:template>
51
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"/>
57 </xsl:call-template>
58 <xsl:text>&#10;{
59 node [fontname = courier, shape = box, colorscheme = paired6]
60 edge [fontname = courier]
61
62 </xsl:text>
63 <xsl:apply-templates select="state"/>
64 <xsl:text>}&#10;</xsl:text>
65 </xsl:template>
66
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:value-of select="@number"/>
72 <xsl:apply-templates select="itemset/item"/>
73 </xsl:with-param>
74 </xsl:call-template>
75 <xsl:apply-templates select="actions/transitions"/>
76 </xsl:template>
77
78 <xsl:template match="actions/transitions">
79 <xsl:apply-templates select="transition"/>
80 </xsl:template>
81
82 <xsl:template match="item">
83 <xsl:apply-templates select="key('bison:ruleByNumber', @rule-number)">
84 <xsl:with-param name="point" select="@point"/>
85 </xsl:apply-templates>
86 <xsl:apply-templates select="lookaheads"/>
87 </xsl:template>
88
89 <xsl:template match="rule">
90 <xsl:param name="point"/>
91 <xsl:text>&#10;</xsl:text>
92 <xsl:value-of select="lhs"/>
93 <xsl:text> -&gt;</xsl:text>
94 <xsl:if test="$point = 0">
95 <xsl:text> .</xsl:text>
96 </xsl:if>
97 <xsl:for-each select="rhs/symbol|rhs/empty">
98 <xsl:apply-templates select="."/>
99 <xsl:if test="$point = position()">
100 <xsl:text> .</xsl:text>
101 </xsl:if>
102 </xsl:for-each>
103 </xsl:template>
104
105 <xsl:template match="symbol">
106 <xsl:text> </xsl:text>
107 <xsl:value-of select="."/>
108 </xsl:template>
109
110 <xsl:template match="empty"/>
111
112 <xsl:template match="lookaheads">
113 <xsl:text>[</xsl:text>
114 <xsl:apply-templates select="symbol"/>
115 <xsl:text>]</xsl:text>
116 </xsl:template>
117
118 <xsl:template match="lookaheads/symbol">
119 <xsl:value-of select="."/>
120 <xsl:if test="position() != last()">
121 <xsl:text>, </xsl:text>
122 </xsl:if>
123 </xsl:template>
124
125 <xsl:template match="transition">
126 <xsl:call-template name="output-edge">
127 <xsl:with-param name="src" select="../../../@number"/>
128 <xsl:with-param name="dst" select="@state"/>
129 <xsl:with-param name="style">
130 <xsl:choose>
131 <xsl:when test="@symbol = 'error'">
132 <xsl:text>dotted</xsl:text>
133 </xsl:when>
134 <xsl:when test="@type = 'shift'">
135 <xsl:text>solid</xsl:text>
136 </xsl:when>
137 <xsl:otherwise>
138 <xsl:text>dashed</xsl:text>
139 </xsl:otherwise>
140 </xsl:choose>
141 </xsl:with-param>
142 <xsl:with-param name="label">
143 <xsl:if test="not(@symbol = 'error')">
144 <xsl:value-of select="@symbol"/>
145 </xsl:if>
146 </xsl:with-param>
147 </xsl:call-template>
148 </xsl:template>
149
150 <xsl:template name="output-node">
151 <xsl:param name="number"/>
152 <xsl:param name="label"/>
153 <xsl:text> </xsl:text>
154 <xsl:value-of select="$number"/>
155 <xsl:text> [label="</xsl:text>
156 <xsl:call-template name="escape">
157 <xsl:with-param name="subject" select="$label"/>
158 </xsl:call-template>
159 <xsl:text>"]&#10;</xsl:text>
160 </xsl:template>
161
162 <xsl:template name="output-edge">
163 <xsl:param name="src"/>
164 <xsl:param name="dst"/>
165 <xsl:param name="style"/>
166 <xsl:param name="label"/>
167 <xsl:text> </xsl:text>
168 <xsl:value-of select="$src"/>
169 <xsl:text> -> </xsl:text>
170 <xsl:value-of select="$dst"/>
171 <xsl:text> [style=</xsl:text>
172 <xsl:value-of select="$style"/>
173 <xsl:if test="$label and $label != ''">
174 <xsl:text> label="</xsl:text>
175 <xsl:call-template name="escape">
176 <xsl:with-param name="subject" select="$label"/>
177 </xsl:call-template>
178 <xsl:text>"</xsl:text>
179 </xsl:if>
180 <xsl:text>]&#10;</xsl:text>
181 </xsl:template>
182
183 <xsl:template name="escape">
184 <xsl:param name="subject"/> <!-- required -->
185 <xsl:call-template name="string-replace">
186 <xsl:with-param name="subject">
187 <xsl:call-template name="string-replace">
188 <xsl:with-param name="subject">
189 <xsl:call-template name="string-replace">
190 <xsl:with-param name="subject" select="$subject"/>
191 <xsl:with-param name="search" select="'\'"/>
192 <xsl:with-param name="replace" select="'\\'"/>
193 </xsl:call-template>
194 </xsl:with-param>
195 <xsl:with-param name="search" select="'&quot;'"/>
196 <xsl:with-param name="replace" select="'\&quot;'"/>
197 </xsl:call-template>
198 </xsl:with-param>
199 <xsl:with-param name="search" select="'&#10;'"/>
200 <xsl:with-param name="replace" select="'\n'"/>
201 </xsl:call-template>
202 </xsl:template>
203
204 <xsl:template name="string-replace">
205 <xsl:param name="subject"/>
206 <xsl:param name="search"/>
207 <xsl:param name="replace"/>
208 <xsl:choose>
209 <xsl:when test="contains($subject, $search)">
210 <xsl:variable name="before" select="substring-before($subject, $search)"/>
211 <xsl:variable name="after" select="substring-after($subject, $search)"/>
212 <xsl:value-of select="$before"/>
213 <xsl:value-of select="$replace"/>
214 <xsl:call-template name="string-replace">
215 <xsl:with-param name="subject" select="$after"/>
216 <xsl:with-param name="search" select="$search"/>
217 <xsl:with-param name="replace" select="$replace"/>
218 </xsl:call-template>
219 </xsl:when>
220 <xsl:otherwise>
221 <xsl:value-of select="$subject"/>
222 </xsl:otherwise>
223 </xsl:choose>
224 </xsl:template>
225
226 </xsl:stylesheet>