]> git.saurik.com Git - bison.git/blob - data/xslt/xml2dot.xsl
(xsl:template name="escape"): Write xsl:param "required" attribute as comment.
[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 $Id$
6
7 Copyright (C) 2007 Free Software Foundation, Inc.
8
9 This file is part of Bison, the GNU Compiler Compiler.
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24 Written by Wojciech Polak <polak@gnu.org>.
25 -->
26
27 <xsl:stylesheet version="1.0"
28 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
29
30 <xsl:output method="text" encoding="UTF-8" indent="no"/>
31
32 <xsl:template match="/">
33 <xsl:apply-templates select="bison-xml-report"/>
34 </xsl:template>
35
36 <xsl:template match="bison-xml-report">
37 <xsl:apply-templates select="automaton"/>
38 </xsl:template>
39
40 <xsl:template match="automaton">
41 <xsl:text>digraph Automaton {&#10;</xsl:text>
42 <xsl:apply-templates select="state"/>
43 <xsl:text>}&#10;</xsl:text>
44 </xsl:template>
45
46 <xsl:template match="automaton/state">
47 <xsl:call-template name="output-node">
48 <xsl:with-param name="number" select="@number"/>
49 <xsl:with-param name="label">
50 <xsl:value-of select="@number"/>
51 <xsl:apply-templates select="itemset/rule"/>
52 </xsl:with-param>
53 </xsl:call-template>
54 <xsl:apply-templates select="actions/transitions"/>
55 </xsl:template>
56
57 <xsl:template match="actions/transitions">
58 <xsl:apply-templates select="transition"/>
59 </xsl:template>
60
61 <xsl:template match="rule">
62 <xsl:text>&#10;</xsl:text>
63 <xsl:value-of select="lhs"/>
64 <xsl:text> -&gt;</xsl:text>
65 <xsl:apply-templates select="rhs/symbol|rhs/point|rhs/empty"/>
66 <xsl:apply-templates select="lookaheads"/>
67 </xsl:template>
68
69 <xsl:template match="symbol">
70 <xsl:text> </xsl:text>
71 <xsl:value-of select="."/>
72 </xsl:template>
73
74 <xsl:template match="point">
75 <xsl:text> .</xsl:text>
76 </xsl:template>
77
78 <xsl:template match="empty">
79 <xsl:text> /* empty */</xsl:text>
80 </xsl:template>
81
82 <xsl:template match="lookaheads">
83 <xsl:text>[</xsl:text>
84 <xsl:apply-templates select="symbol"/>
85 <xsl:text>]</xsl:text>
86 </xsl:template>
87
88 <xsl:template match="lookaheads/symbol">
89 <xsl:value-of select="."/>
90 <xsl:if test="position() != last()">
91 <xsl:text>, </xsl:text>
92 </xsl:if>
93 </xsl:template>
94
95 <xsl:template match="transition">
96 <xsl:call-template name="output-edge">
97 <xsl:with-param name="src" select="../../../@number"/>
98 <xsl:with-param name="dst" select="@state"/>
99 <xsl:with-param name="style">
100 <xsl:choose>
101 <xsl:when test="@symbol = 'error'">
102 <xsl:text>dotted</xsl:text>
103 </xsl:when>
104 <xsl:when test="@type = 'shift'">
105 <xsl:text>solid</xsl:text>
106 </xsl:when>
107 <xsl:otherwise>
108 <xsl:text>dashed</xsl:text>
109 </xsl:otherwise>
110 </xsl:choose>
111 </xsl:with-param>
112 <xsl:with-param name="label">
113 <xsl:if test="not(@symbol = 'error')">
114 <xsl:value-of select="@symbol"/>
115 </xsl:if>
116 </xsl:with-param>
117 </xsl:call-template>
118 </xsl:template>
119
120 <xsl:template name="output-node">
121 <xsl:param name="number"/>
122 <xsl:param name="label"/>
123 <xsl:text> </xsl:text>
124 <xsl:value-of select="$number"/>
125 <xsl:text> [label="</xsl:text>
126 <xsl:call-template name="escape">
127 <xsl:with-param name="subject" select="$label"/>
128 </xsl:call-template>
129 <xsl:text>"]&#10;</xsl:text>
130 </xsl:template>
131
132 <xsl:template name="output-edge">
133 <xsl:param name="src"/>
134 <xsl:param name="dst"/>
135 <xsl:param name="style"/>
136 <xsl:param name="label"/>
137 <xsl:text> </xsl:text>
138 <xsl:value-of select="$src"/>
139 <xsl:text> -> </xsl:text>
140 <xsl:value-of select="$dst"/>
141 <xsl:text> [style=</xsl:text>
142 <xsl:value-of select="$style"/>
143 <xsl:if test="$label and $label != ''">
144 <xsl:text> label="</xsl:text>
145 <xsl:call-template name="escape">
146 <xsl:with-param name="subject" select="$label"/>
147 </xsl:call-template>
148 <xsl:text>"</xsl:text>
149 </xsl:if>
150 <xsl:text>]&#10;</xsl:text>
151 </xsl:template>
152
153 <xsl:template name="escape">
154 <xsl:param name="subject"/> <!-- required -->
155 <xsl:call-template name="string-replace">
156 <xsl:with-param name="subject">
157 <xsl:call-template name="string-replace">
158 <xsl:with-param name="subject">
159 <xsl:call-template name="string-replace">
160 <xsl:with-param name="subject" select="$subject"/>
161 <xsl:with-param name="search" select="'\'"/>
162 <xsl:with-param name="replace" select="'\\'"/>
163 </xsl:call-template>
164 </xsl:with-param>
165 <xsl:with-param name="search" select="'&quot;'"/>
166 <xsl:with-param name="replace" select="'\&quot;'"/>
167 </xsl:call-template>
168 </xsl:with-param>
169 <xsl:with-param name="search" select="'&#10;'"/>
170 <xsl:with-param name="replace" select="'\n'"/>
171 </xsl:call-template>
172 </xsl:template>
173
174 <xsl:template name="string-replace">
175 <xsl:param name="subject"/>
176 <xsl:param name="search"/>
177 <xsl:param name="replace"/>
178 <xsl:choose>
179 <xsl:when test="contains($subject, $search)">
180 <xsl:variable name="before" select="substring-before($subject, $search)"/>
181 <xsl:variable name="after" select="substring-after($subject, $search)"/>
182 <xsl:value-of select="$before"/>
183 <xsl:value-of select="$replace"/>
184 <xsl:call-template name="string-replace">
185 <xsl:with-param name="subject" select="$after"/>
186 <xsl:with-param name="search" select="$search"/>
187 <xsl:with-param name="replace" select="$replace"/>
188 </xsl:call-template>
189 </xsl:when>
190 <xsl:otherwise>
191 <xsl:value-of select="$subject"/>
192 </xsl:otherwise>
193 </xsl:choose>
194 </xsl:template>
195
196 </xsl:stylesheet>