]> git.saurik.com Git - bison.git/blob - data/xslt/xml2dot.xsl
Improve C++ namespace support. Discussed starting at
[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>\n</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="string-replace">
127 <xsl:with-param name="subject" select="$label"/>
128 <xsl:with-param name="search" select="'&quot;'"/>
129 <xsl:with-param name="replace" select="'\&quot;'"/>
130 </xsl:call-template>
131 <xsl:text>"]&#10;</xsl:text>
132 </xsl:template>
133
134 <xsl:template name="output-edge">
135 <xsl:param name="src"/>
136 <xsl:param name="dst"/>
137 <xsl:param name="style"/>
138 <xsl:param name="label"/>
139 <xsl:text> </xsl:text>
140 <xsl:value-of select="$src"/>
141 <xsl:text> -> </xsl:text>
142 <xsl:value-of select="$dst"/>
143 <xsl:text> [style=</xsl:text>
144 <xsl:value-of select="$style"/>
145 <xsl:if test="$label and $label != ''">
146 <xsl:text> label="</xsl:text>
147 <xsl:call-template name="string-replace">
148 <xsl:with-param name="subject" select="$label"/>
149 <xsl:with-param name="search" select="'&quot;'"/>
150 <xsl:with-param name="replace" select="'\&quot;'"/>
151 </xsl:call-template>
152 <xsl:text>"</xsl:text>
153 </xsl:if>
154 <xsl:text>]&#10;</xsl:text>
155 </xsl:template>
156
157 <xsl:template name="string-replace">
158 <xsl:param name="subject"/>
159 <xsl:param name="search"/>
160 <xsl:param name="replace"/>
161 <xsl:choose>
162 <xsl:when test="contains($subject, $search)">
163 <xsl:variable name="before" select="substring-before($subject, $search)"/>
164 <xsl:variable name="after" select="substring-after($subject, $search)"/>
165 <xsl:value-of select="$before"/>
166 <xsl:value-of select="$replace"/>
167 <xsl:call-template name="string-replace">
168 <xsl:with-param name="subject" select="$after"/>
169 <xsl:with-param name="search" select="$search"/>
170 <xsl:with-param name="replace" select="$replace"/>
171 </xsl:call-template>
172 </xsl:when>
173 <xsl:otherwise>
174 <xsl:value-of select="$subject"/>
175 </xsl:otherwise>
176 </xsl:choose>
177 </xsl:template>
178
179 </xsl:stylesheet>