]> git.0d.be Git - empathy.git/blob - tools/c-constants-generator.xsl
Update tools/.gitignore
[empathy.git] / tools / c-constants-generator.xsl
1 <!-- Stylesheet to extract C enumerations from the Telepathy spec.
2 The master copy of this stylesheet is in telepathy-glib - please make any
3 changes there.
4
5 Copyright (C) 2006, 2007 Collabora Limited
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 -->
21
22 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
23   xmlns:tp="http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"
24   exclude-result-prefixes="tp">
25
26   <xsl:output method="text" indent="no" encoding="ascii"/>
27
28   <xsl:param name="mixed-case-prefix" select="''"/>
29
30   <xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
31   <xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
32
33   <xsl:variable name="upper-case-prefix" select="concat(translate($mixed-case-prefix, $lower, $upper), '_')"/>
34   <xsl:variable name="lower-case-prefix" select="concat(translate($mixed-case-prefix, $upper, $lower), '_')"/>
35
36
37   <xsl:template match="tp:flags">
38     <xsl:variable name="name">
39       <xsl:choose>
40         <xsl:when test="@plural">
41           <xsl:value-of select="@plural"/>
42         </xsl:when>
43         <xsl:otherwise>
44           <xsl:value-of select="@name"/>
45         </xsl:otherwise>
46       </xsl:choose>
47     </xsl:variable>
48     <xsl:variable name="value-prefix">
49       <xsl:choose>
50         <xsl:when test="@singular">
51           <xsl:value-of select="@singular"/>
52         </xsl:when>
53         <xsl:when test="@value-prefix">
54           <xsl:value-of select="@value-prefix"/>
55         </xsl:when>
56         <xsl:otherwise>
57           <xsl:value-of select="@name"/>
58         </xsl:otherwise>
59       </xsl:choose>
60     </xsl:variable>
61     <xsl:text>/**&#10;</xsl:text>
62     <xsl:text> *&#10;</xsl:text>
63     <xsl:value-of select="translate(concat($mixed-case-prefix, $name), '_', '')"/>
64     <xsl:text>:&#10;</xsl:text>
65     <xsl:apply-templates mode="flag-or-enumvalue-gtkdoc">
66       <xsl:with-param name="value-prefix" select="$value-prefix"/>
67     </xsl:apply-templates>
68     <xsl:text> *&#10;</xsl:text>
69     <xsl:if test="tp:docstring">
70       <xsl:text> * &lt;![CDATA[</xsl:text>
71       <xsl:value-of select="translate(string (tp:docstring), '&#13;&#10;', '  ')"/>
72       <xsl:text>]]&gt;&#10;</xsl:text>
73       <xsl:text> *&#10;</xsl:text>
74     </xsl:if>
75     <xsl:text> * Bitfield/set of flags generated from the Telepathy specification.&#10;</xsl:text>
76     <xsl:text> */&#10;</xsl:text>
77     <xsl:text>typedef enum {&#10;</xsl:text>
78     <xsl:apply-templates>
79       <xsl:with-param name="value-prefix" select="$value-prefix"/>
80     </xsl:apply-templates>
81     <xsl:text>} </xsl:text>
82     <xsl:value-of select="translate(concat($mixed-case-prefix, $name), '_', '')"/>
83     <xsl:text>;&#10;</xsl:text>
84     <xsl:text>&#10;</xsl:text>
85   </xsl:template>
86
87   <xsl:template match="text()" mode="flag-or-enumvalue-gtkdoc"/>
88
89   <xsl:template match="tp:enumvalue" mode="flag-or-enumvalue-gtkdoc">
90     <xsl:param name="value-prefix"/>
91     <xsl:text> * @</xsl:text>
92     <xsl:value-of select="translate(concat($upper-case-prefix, $value-prefix, '_', @suffix), $lower, $upper)"/>
93     <xsl:text>: &lt;![CDATA[</xsl:text>
94     <xsl:value-of select="translate(string(tp:docstring), '&#13;&#10;', '  ')"/>
95     <xsl:text>]]&gt;&#10;</xsl:text>
96   </xsl:template>
97
98   <xsl:template match="tp:flag" mode="flag-or-enumvalue-gtkdoc">
99     <xsl:param name="value-prefix"/>
100     <xsl:text> * @</xsl:text>
101     <xsl:value-of select="translate(concat($upper-case-prefix, $value-prefix, '_', @suffix), $lower, $upper)"/>
102     <xsl:text>: &lt;![CDATA[</xsl:text>
103     <xsl:value-of select="translate(string(tp:docstring), '&#13;&#10;', '  ')"/>
104     <xsl:text>]]&gt;&#10;</xsl:text>
105   </xsl:template>
106
107   <xsl:template match="tp:enum">
108     <xsl:variable name="name">
109       <xsl:choose>
110         <xsl:when test="@singular">
111           <xsl:value-of select="@singular"/>
112         </xsl:when>
113         <xsl:otherwise>
114           <xsl:value-of select="@name"/>
115         </xsl:otherwise>
116       </xsl:choose>
117     </xsl:variable>
118     <xsl:variable name="value-prefix">
119       <xsl:choose>
120         <xsl:when test="@singular">
121           <xsl:value-of select="@singular"/>
122         </xsl:when>
123         <xsl:when test="@value-prefix">
124           <xsl:value-of select="@value-prefix"/>
125         </xsl:when>
126         <xsl:otherwise>
127           <xsl:value-of select="@name"/>
128         </xsl:otherwise>
129       </xsl:choose>
130     </xsl:variable>
131     <xsl:variable name="name-plural">
132       <xsl:choose>
133         <xsl:when test="@plural">
134           <xsl:value-of select="@plural"/>
135         </xsl:when>
136         <xsl:otherwise>
137           <xsl:value-of select="@name"/><xsl:text>s</xsl:text>
138         </xsl:otherwise>
139       </xsl:choose>
140     </xsl:variable>
141     <xsl:text>/**&#10;</xsl:text>
142     <xsl:text> *&#10;</xsl:text>
143     <xsl:value-of select="translate(concat($mixed-case-prefix, $name), '_', '')"/>
144     <xsl:text>:&#10;</xsl:text>
145     <xsl:apply-templates mode="flag-or-enumvalue-gtkdoc">
146       <xsl:with-param name="value-prefix" select="$value-prefix"/>
147     </xsl:apply-templates>
148     <xsl:text> *&#10;</xsl:text>
149     <xsl:if test="tp:docstring">
150       <xsl:text> * &lt;![CDATA[</xsl:text>
151       <xsl:value-of select="translate(string (tp:docstring), '&#13;&#10;', '  ')"/>
152       <xsl:text>]]&gt;&#10;</xsl:text>
153       <xsl:text> *&#10;</xsl:text>
154     </xsl:if>
155     <xsl:text> * Bitfield/set of flags generated from the Telepathy specification.&#10;</xsl:text>
156     <xsl:text> */&#10;</xsl:text>
157     <xsl:text>typedef enum {&#10;</xsl:text>
158     <xsl:apply-templates>
159       <xsl:with-param name="value-prefix" select="$value-prefix"/>
160     </xsl:apply-templates>
161     <xsl:text>} </xsl:text>
162     <xsl:value-of select="translate(concat($mixed-case-prefix, $name), '_', '')"/>
163     <xsl:text>;&#10;</xsl:text>
164     <xsl:text>&#10;</xsl:text>
165     <xsl:text>/**&#10;</xsl:text>
166     <xsl:text> * NUM_</xsl:text>
167     <xsl:value-of select="translate(concat($upper-case-prefix, $name-plural), $lower, $upper)"/>
168     <xsl:text>:&#10;</xsl:text>
169     <xsl:text> *&#10;</xsl:text>
170     <xsl:text> * 1 higher than the highest valid value of #</xsl:text>
171     <xsl:value-of select="translate(concat($mixed-case-prefix, $name), '_', '')"/>
172     <xsl:text>.&#10;</xsl:text>
173     <xsl:text> */&#10;</xsl:text>
174     <xsl:text>#define NUM_</xsl:text>
175     <xsl:value-of select="translate(concat($upper-case-prefix, $name-plural), $lower, $upper)"/>
176     <xsl:text> (</xsl:text>
177     <xsl:value-of select="tp:enumvalue[position() = last()]/@value"/>
178     <xsl:text>+1)&#10;</xsl:text>
179     <xsl:text>&#10;</xsl:text>
180   </xsl:template>
181
182   <xsl:template match="tp:flags/tp:flag">
183     <xsl:param name="value-prefix"/>
184     <xsl:variable name="suffix">
185       <xsl:choose>
186         <xsl:when test="@suffix">
187           <xsl:value-of select="@suffix"/>
188         </xsl:when>
189         <xsl:otherwise>
190           <xsl:value-of select="@name"/>
191         </xsl:otherwise>
192       </xsl:choose>
193     </xsl:variable>
194     <xsl:variable name="name" select="translate(concat($upper-case-prefix, $value-prefix, '_', $suffix), $lower, $upper)"/>
195
196     <xsl:if test="@name and @suffix and @name != @suffix">
197       <xsl:message terminate="yes">
198         <xsl:text>Flag name </xsl:text>
199         <xsl:value-of select="@name"/>
200         <xsl:text> != suffix </xsl:text>
201         <xsl:value-of select="@suffix"/>
202         <xsl:text>&#10;</xsl:text>
203       </xsl:message>
204     </xsl:if>
205     <xsl:text>    </xsl:text>
206     <xsl:value-of select="$name"/>
207     <xsl:text> = </xsl:text>
208     <xsl:value-of select="@value"/>
209     <xsl:text>,&#10;</xsl:text>
210   </xsl:template>
211
212   <xsl:template match="tp:enum/tp:enumvalue">
213     <xsl:param name="value-prefix"/>
214     <xsl:variable name="suffix">
215       <xsl:choose>
216         <xsl:when test="@suffix">
217           <xsl:value-of select="@suffix"/>
218         </xsl:when>
219         <xsl:otherwise>
220           <xsl:value-of select="@name"/>
221         </xsl:otherwise>
222       </xsl:choose>
223     </xsl:variable>
224     <xsl:variable name="name" select="translate(concat($upper-case-prefix, $value-prefix, '_', $suffix), $lower, $upper)"/>
225
226     <xsl:if test="@name and @suffix and @name != @suffix">
227       <xsl:message terminate="yes">
228         <xsl:text>Enumvalue name </xsl:text>
229         <xsl:value-of select="@name"/>
230         <xsl:text> != suffix </xsl:text>
231         <xsl:value-of select="@suffix"/>
232         <xsl:text>&#10;</xsl:text>
233       </xsl:message>
234     </xsl:if>
235
236     <xsl:if test="preceding-sibling::tp:enumvalue and number(preceding-sibling::tp:enumvalue[1]/@value) > number(@value)">
237       <xsl:message terminate="yes">
238         <xsl:text>Enum values must be in ascending numeric order, but </xsl:text>
239         <xsl:value-of select="$name"/>
240         <xsl:text> is less than the previous value</xsl:text>
241       </xsl:message>
242     </xsl:if>
243
244     <xsl:text>    </xsl:text>
245     <xsl:value-of select="$name"/>
246     <xsl:text> = </xsl:text>
247     <xsl:value-of select="@value"/>
248     <xsl:text>,&#10;</xsl:text>
249   </xsl:template>
250
251   <xsl:template match="tp:flag">
252     <xsl:message terminate="yes">tp:flag found outside tp:flags&#10;</xsl:message>
253   </xsl:template>
254
255   <xsl:template match="tp:enumvalue">
256     <xsl:message terminate="yes">tp:enumvalue found outside tp:enum&#10;</xsl:message>
257   </xsl:template>
258
259   <xsl:template match="text()"/>
260
261   <xsl:template match="/tp:spec">
262     <xsl:if test="$mixed-case-prefix = ''">
263       <xsl:message terminate="yes">
264         <xsl:text>mixed-case-prefix param must be set&#10;</xsl:text>
265       </xsl:message>
266     </xsl:if>
267
268     <xsl:text>/* Generated from </xsl:text>
269     <xsl:value-of select="tp:title"/>
270     <xsl:if test="tp:version">
271       <xsl:text>, version </xsl:text>
272       <xsl:value-of select="tp:version"/>
273     </xsl:if>
274     <xsl:text>&#10;</xsl:text>
275     <xsl:text>&#10;</xsl:text>
276     <xsl:for-each select="tp:copyright">
277       <xsl:value-of select="."/>
278       <xsl:text>&#10;</xsl:text>
279     </xsl:for-each>
280     <xsl:value-of select="tp:license"/>
281     <xsl:text>&#10;</xsl:text>
282     <xsl:value-of select="tp:docstring"/>
283     <xsl:text>&#10;</xsl:text>
284     <xsl:text> */&#10;</xsl:text>
285     <xsl:text>&#10;</xsl:text>
286     <xsl:text>#ifdef __cplusplus&#10;</xsl:text>
287     <xsl:text>extern "C" {&#10;</xsl:text>
288     <xsl:text>#endif&#10;</xsl:text>
289     <xsl:text>&#10;</xsl:text>
290     <xsl:apply-templates/>
291     <xsl:text>&#10;</xsl:text>
292     <xsl:text>#ifdef __cplusplus&#10;</xsl:text>
293     <xsl:text>}&#10;</xsl:text>
294     <xsl:text>#endif&#10;</xsl:text>
295   </xsl:template>
296
297 </xsl:stylesheet>
298
299 <!-- vim:set sw=2 sts=2 et noai noci: -->