]> git.saurik.com Git - bison.git/blob - data/xslt/xml2dot.xsl
d1d2691ac908ea40c6ad0480aa7da9dde7f4d422
[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 xmlns:bison="http://www.gnu.org/software/bison/">
30
31 <xsl:import href="bison.xsl"/>
32 <xsl:output method="text" encoding="UTF-8" indent="no"/>
33
34 <xsl:template match="/">
35 <xsl:apply-templates select="bison-xml-report"/>
36 </xsl:template>
37
38 <xsl:template match="bison-xml-report">
39 <xsl:apply-templates select="automaton"/>
40 </xsl:template>
41
42 <xsl:template match="automaton">
43 <xsl:text>digraph Automaton {&#10;</xsl:text>
44 <xsl:apply-templates select="state"/>
45 <xsl:text>}&#10;</xsl:text>
46 </xsl:template>
47
48 <xsl:template match="automaton/state">
49 <xsl:call-template name="output-node">
50 <xsl:with-param name="number" select="@number"/>
51 <xsl:with-param name="label">
52 <xsl:value-of select="@number"/>
53 <xsl:apply-templates select="itemset/item"/>
54 </xsl:with-param>
55 </xsl:call-template>
56 <xsl:apply-templates select="actions/transitions"/>
57 </xsl:template>
58
59 <xsl:template match="actions/transitions">
60 <xsl:apply-templates select="transition"/>
61 </xsl:template>
62
63 <xsl:template match="item">
64 <xsl:apply-templates select="key('bison:ruleNumber', @rule-number)">
65 <xsl:with-param name="point" select="@point"/>
66 </xsl:apply-templates>
67 <xsl:apply-templates select="lookaheads"/>
68 </xsl:template>
69
70 <xsl:template match="rule">
71 <xsl:param name="point"/>
72 <xsl:text>&#10;</xsl:text>
73 <xsl:value-of select="lhs"/>
74 <xsl:text> -&gt;</xsl:text>
75 <xsl:if test="$point = 0">
76 <xsl:text> .</xsl:text>
77 </xsl:if>
78 <xsl:for-each select="rhs/symbol|rhs/empty">
79 <xsl:apply-templates select="."/>
80 <xsl:if test="$point = position()">
81 <xsl:text> .</xsl:text>
82 </xsl:if>
83 </xsl:for-each>
84 </xsl:template>
85
86 <xsl:template match="symbol">
87 <xsl:text> </xsl:text>
88 <xsl:value-of select="."/>
89 </xsl:template>
90
91 <xsl:template match="empty"/>
92
93 <xsl:template match="lookaheads">
94 <xsl:text>[</xsl:text>
95 <xsl:apply-templates select="symbol"/>
96 <xsl:text>]</xsl:text>
97 </xsl:template>
98
99 <xsl:template match="lookaheads/symbol">
100 <xsl:value-of select="."/>
101 <xsl:if test="position() != last()">
102 <xsl:text>, </xsl:text>
103 </xsl:if>
104 </xsl:template>
105
106 <xsl:template match="transition">
107 <xsl:call-template name="output-edge">
108 <xsl:with-param name="src" select="../../../@number"/>
109 <xsl:with-param name="dst" select="@state"/>
110 <xsl:with-param name="style">
111 <xsl:choose>
112 <xsl:when test="@symbol = 'error'">
113 <xsl:text>dotted</xsl:text>
114 </xsl:when>
115 <xsl:when test="@type = 'shift'">
116 <xsl:text>solid</xsl:text>
117 </xsl:when>
118 <xsl:otherwise>
119 <xsl:text>dashed</xsl:text>
120 </xsl:otherwise>
121 </xsl:choose>
122 </xsl:with-param>
123 <xsl:with-param name="label">
124 <xsl:if test="not(@symbol = 'error')">
125 <xsl:value-of select="@symbol"/>
126 </xsl:if>
127 </xsl:with-param>
128 </xsl:call-template>
129 </xsl:template>
130
131 <xsl:template name="output-node">
132 <xsl:param name="number"/>
133 <xsl:param name="label"/>
134 <xsl:text> </xsl:text>
135 <xsl:value-of select="$number"/>
136 <xsl:text> [label="</xsl:text>
137 <xsl:call-template name="escape">
138 <xsl:with-param name="subject" select="$label"/>
139 </xsl:call-template>
140 <xsl:text>"]&#10;</xsl:text>
141 </xsl:template>
142
143 <xsl:template name="output-edge">
144 <xsl:param name="src"/>
145 <xsl:param name="dst"/>
146 <xsl:param name="style"/>
147 <xsl:param name="label"/>
148 <xsl:text> </xsl:text>
149 <xsl:value-of select="$src"/>
150 <xsl:text> -> </xsl:text>
151 <xsl:value-of select="$dst"/>
152 <xsl:text> [style=</xsl:text>
153 <xsl:value-of select="$style"/>
154 <xsl:if test="$label and $label != ''">
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>"</xsl:text>
160 </xsl:if>
161 <xsl:text>]&#10;</xsl:text>
162 </xsl:template>
163
164 <xsl:template name="escape">
165 <xsl:param name="subject"/> <!-- required -->
166 <xsl:call-template name="string-replace">
167 <xsl:with-param name="subject">
168 <xsl:call-template name="string-replace">
169 <xsl:with-param name="subject">
170 <xsl:call-template name="string-replace">
171 <xsl:with-param name="subject" select="$subject"/>
172 <xsl:with-param name="search" select="'\'"/>
173 <xsl:with-param name="replace" select="'\\'"/>
174 </xsl:call-template>
175 </xsl:with-param>
176 <xsl:with-param name="search" select="'&quot;'"/>
177 <xsl:with-param name="replace" select="'\&quot;'"/>
178 </xsl:call-template>
179 </xsl:with-param>
180 <xsl:with-param name="search" select="'&#10;'"/>
181 <xsl:with-param name="replace" select="'\n'"/>
182 </xsl:call-template>
183 </xsl:template>
184
185 <xsl:template name="string-replace">
186 <xsl:param name="subject"/>
187 <xsl:param name="search"/>
188 <xsl:param name="replace"/>
189 <xsl:choose>
190 <xsl:when test="contains($subject, $search)">
191 <xsl:variable name="before" select="substring-before($subject, $search)"/>
192 <xsl:variable name="after" select="substring-after($subject, $search)"/>
193 <xsl:value-of select="$before"/>
194 <xsl:value-of select="$replace"/>
195 <xsl:call-template name="string-replace">
196 <xsl:with-param name="subject" select="$after"/>
197 <xsl:with-param name="search" select="$search"/>
198 <xsl:with-param name="replace" select="$replace"/>
199 </xsl:call-template>
200 </xsl:when>
201 <xsl:otherwise>
202 <xsl:value-of select="$subject"/>
203 </xsl:otherwise>
204 </xsl:choose>
205 </xsl:template>
206
207 </xsl:stylesheet>