]> git.saurik.com Git - bison.git/blame - data/xslt/xml2dot.xsl
TODO: statistics.
[bison.git] / data / xslt / xml2dot.xsl
CommitLineData
dc6025ff
WP
1<?xml version="1.0" encoding="UTF-8"?>
2
3<!--
4 xml2dot.xsl - transform Bison XML Report into DOT.
dc6025ff 5
41976786 6 Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
dc6025ff
WP
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"
255a6b90
JD
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"/>
dc6025ff
WP
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:apply-templates select="automaton"/>
39</xsl:template>
40
41<xsl:template match="automaton">
42 <xsl:text>digraph Automaton {&#10;</xsl:text>
43 <xsl:apply-templates select="state"/>
44 <xsl:text>}&#10;</xsl:text>
45</xsl:template>
46
47<xsl:template match="automaton/state">
48 <xsl:call-template name="output-node">
49 <xsl:with-param name="number" select="@number"/>
50 <xsl:with-param name="label">
51 <xsl:value-of select="@number"/>
255a6b90 52 <xsl:apply-templates select="itemset/item"/>
dc6025ff
WP
53 </xsl:with-param>
54 </xsl:call-template>
55 <xsl:apply-templates select="actions/transitions"/>
56</xsl:template>
57
58<xsl:template match="actions/transitions">
59 <xsl:apply-templates select="transition"/>
60</xsl:template>
61
255a6b90 62<xsl:template match="item">
d4a26c48 63 <xsl:apply-templates select="key('bison:ruleByNumber', @rule-number)">
255a6b90
JD
64 <xsl:with-param name="point" select="@point"/>
65 </xsl:apply-templates>
66 <xsl:apply-templates select="lookaheads"/>
67</xsl:template>
68
dc6025ff 69<xsl:template match="rule">
255a6b90 70 <xsl:param name="point"/>
21f1b063 71 <xsl:text>&#10;</xsl:text>
dc6025ff
WP
72 <xsl:value-of select="lhs"/>
73 <xsl:text> -&gt;</xsl:text>
255a6b90
JD
74 <xsl:if test="$point = 0">
75 <xsl:text> .</xsl:text>
76 </xsl:if>
77 <xsl:for-each select="rhs/symbol|rhs/empty">
78 <xsl:apply-templates select="."/>
79 <xsl:if test="$point = position()">
80 <xsl:text> .</xsl:text>
81 </xsl:if>
82 </xsl:for-each>
dc6025ff
WP
83</xsl:template>
84
85<xsl:template match="symbol">
86 <xsl:text> </xsl:text>
87 <xsl:value-of select="."/>
88</xsl:template>
89
255a6b90 90<xsl:template match="empty"/>
dc6025ff
WP
91
92<xsl:template match="lookaheads">
93 <xsl:text>[</xsl:text>
94 <xsl:apply-templates select="symbol"/>
95 <xsl:text>]</xsl:text>
96</xsl:template>
97
98<xsl:template match="lookaheads/symbol">
99 <xsl:value-of select="."/>
100 <xsl:if test="position() != last()">
101 <xsl:text>, </xsl:text>
102 </xsl:if>
103</xsl:template>
104
105<xsl:template match="transition">
106 <xsl:call-template name="output-edge">
107 <xsl:with-param name="src" select="../../../@number"/>
108 <xsl:with-param name="dst" select="@state"/>
109 <xsl:with-param name="style">
110 <xsl:choose>
111 <xsl:when test="@symbol = 'error'">
112 <xsl:text>dotted</xsl:text>
113 </xsl:when>
114 <xsl:when test="@type = 'shift'">
115 <xsl:text>solid</xsl:text>
116 </xsl:when>
117 <xsl:otherwise>
118 <xsl:text>dashed</xsl:text>
119 </xsl:otherwise>
120 </xsl:choose>
121 </xsl:with-param>
122 <xsl:with-param name="label">
123 <xsl:if test="not(@symbol = 'error')">
124 <xsl:value-of select="@symbol"/>
125 </xsl:if>
126 </xsl:with-param>
127 </xsl:call-template>
128</xsl:template>
129
130<xsl:template name="output-node">
131 <xsl:param name="number"/>
132 <xsl:param name="label"/>
133 <xsl:text> </xsl:text>
134 <xsl:value-of select="$number"/>
135 <xsl:text> [label="</xsl:text>
21f1b063 136 <xsl:call-template name="escape">
dc6025ff 137 <xsl:with-param name="subject" select="$label"/>
dc6025ff
WP
138 </xsl:call-template>
139 <xsl:text>"]&#10;</xsl:text>
140</xsl:template>
141
142<xsl:template name="output-edge">
143 <xsl:param name="src"/>
144 <xsl:param name="dst"/>
145 <xsl:param name="style"/>
146 <xsl:param name="label"/>
147 <xsl:text> </xsl:text>
148 <xsl:value-of select="$src"/>
149 <xsl:text> -> </xsl:text>
150 <xsl:value-of select="$dst"/>
151 <xsl:text> [style=</xsl:text>
152 <xsl:value-of select="$style"/>
153 <xsl:if test="$label and $label != ''">
154 <xsl:text> label="</xsl:text>
21f1b063 155 <xsl:call-template name="escape">
dc6025ff 156 <xsl:with-param name="subject" select="$label"/>
dc6025ff
WP
157 </xsl:call-template>
158 <xsl:text>"</xsl:text>
159 </xsl:if>
160 <xsl:text>]&#10;</xsl:text>
161</xsl:template>
162
21f1b063 163<xsl:template name="escape">
85a6eb13 164 <xsl:param name="subject"/> <!-- required -->
21f1b063
JD
165 <xsl:call-template name="string-replace">
166 <xsl:with-param name="subject">
167 <xsl:call-template name="string-replace">
168 <xsl:with-param name="subject">
169 <xsl:call-template name="string-replace">
170 <xsl:with-param name="subject" select="$subject"/>
171 <xsl:with-param name="search" select="'\'"/>
172 <xsl:with-param name="replace" select="'\\'"/>
173 </xsl:call-template>
174 </xsl:with-param>
175 <xsl:with-param name="search" select="'&quot;'"/>
176 <xsl:with-param name="replace" select="'\&quot;'"/>
177 </xsl:call-template>
178 </xsl:with-param>
179 <xsl:with-param name="search" select="'&#10;'"/>
180 <xsl:with-param name="replace" select="'\n'"/>
181 </xsl:call-template>
182</xsl:template>
183
184<xsl:template name="string-replace">
185 <xsl:param name="subject"/>
186 <xsl:param name="search"/>
187 <xsl:param name="replace"/>
188 <xsl:choose>
189 <xsl:when test="contains($subject, $search)">
190 <xsl:variable name="before" select="substring-before($subject, $search)"/>
191 <xsl:variable name="after" select="substring-after($subject, $search)"/>
192 <xsl:value-of select="$before"/>
193 <xsl:value-of select="$replace"/>
194 <xsl:call-template name="string-replace">
195 <xsl:with-param name="subject" select="$after"/>
196 <xsl:with-param name="search" select="$search"/>
197 <xsl:with-param name="replace" select="$replace"/>
198 </xsl:call-template>
41976786 199 </xsl:when>
21f1b063 200 <xsl:otherwise>
41976786 201 <xsl:value-of select="$subject"/>
21f1b063 202 </xsl:otherwise>
41976786 203 </xsl:choose>
21f1b063 204</xsl:template>
dc6025ff
WP
205
206</xsl:stylesheet>