]> 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;{&#10;</xsl:text>
59 <xsl:apply-templates select="state"/>
60 <xsl:text>}&#10;</xsl:text>
61 </xsl:template>
62
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"/>
69 </xsl:with-param>
70 </xsl:call-template>
71 <xsl:apply-templates select="actions/transitions"/>
72 </xsl:template>
73
74 <xsl:template match="actions/transitions">
75 <xsl:apply-templates select="transition"/>
76 </xsl:template>
77
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"/>
83 </xsl:template>
84
85 <xsl:template match="rule">
86 <xsl:param name="point"/>
87 <xsl:text>&#10;</xsl:text>
88 <xsl:value-of select="lhs"/>
89 <xsl:text> -&gt;</xsl:text>
90 <xsl:if test="$point = 0">
91 <xsl:text> .</xsl:text>
92 </xsl:if>
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>
97 </xsl:if>
98 </xsl:for-each>
99 </xsl:template>
100
101 <xsl:template match="symbol">
102 <xsl:text> </xsl:text>
103 <xsl:value-of select="."/>
104 </xsl:template>
105
106 <xsl:template match="empty"/>
107
108 <xsl:template match="lookaheads">
109 <xsl:text>[</xsl:text>
110 <xsl:apply-templates select="symbol"/>
111 <xsl:text>]</xsl:text>
112 </xsl:template>
113
114 <xsl:template match="lookaheads/symbol">
115 <xsl:value-of select="."/>
116 <xsl:if test="position() != last()">
117 <xsl:text>, </xsl:text>
118 </xsl:if>
119 </xsl:template>
120
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">
126 <xsl:choose>
127 <xsl:when test="@symbol = 'error'">
128 <xsl:text>dotted</xsl:text>
129 </xsl:when>
130 <xsl:when test="@type = 'shift'">
131 <xsl:text>solid</xsl:text>
132 </xsl:when>
133 <xsl:otherwise>
134 <xsl:text>dashed</xsl:text>
135 </xsl:otherwise>
136 </xsl:choose>
137 </xsl:with-param>
138 <xsl:with-param name="label">
139 <xsl:if test="not(@symbol = 'error')">
140 <xsl:value-of select="@symbol"/>
141 </xsl:if>
142 </xsl:with-param>
143 </xsl:call-template>
144 </xsl:template>
145
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"/>
154 </xsl:call-template>
155 <xsl:text>"]&#10;</xsl:text>
156 </xsl:template>
157
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"/>
173 </xsl:call-template>
174 <xsl:text>"</xsl:text>
175 </xsl:if>
176 <xsl:text>]&#10;</xsl:text>
177 </xsl:template>
178
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="'\\'"/>
189 </xsl:call-template>
190 </xsl:with-param>
191 <xsl:with-param name="search" select="'&quot;'"/>
192 <xsl:with-param name="replace" select="'\&quot;'"/>
193 </xsl:call-template>
194 </xsl:with-param>
195 <xsl:with-param name="search" select="'&#10;'"/>
196 <xsl:with-param name="replace" select="'\n'"/>
197 </xsl:call-template>
198 </xsl:template>
199
200 <xsl:template name="string-replace">
201 <xsl:param name="subject"/>
202 <xsl:param name="search"/>
203 <xsl:param name="replace"/>
204 <xsl:choose>
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"/>
214 </xsl:call-template>
215 </xsl:when>
216 <xsl:otherwise>
217 <xsl:value-of select="$subject"/>
218 </xsl:otherwise>
219 </xsl:choose>
220 </xsl:template>
221
222 </xsl:stylesheet>