summaryrefslogtreecommitdiffstats
path: root/src/xmlpatterns/qtokenautomaton/qautomaton2cpp.xsl
blob: 16084a0a4bae6dc7ce67ea8c8197d98b3978c322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
                xml:lang="en"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:local="http://www.w3.org/2005/xquery-local-functions"
                xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xsl:variable name="className" as="xs:string" select="tokenAutomaton/@className"/>
    <xsl:variable name="defaultToken" as="xs:string" select="tokenAutomaton/@defaultToken"/>
    <xsl:variable name="tokens" as="element(token)+" select="tokenAutomaton/tokens/token"/>
    <xsl:variable name="tokenEnum" as="xs:string" select="tokenAutomaton/@tokenEnum"/>

    <xsl:variable name="warningGenerated" as="xs:string">/* NOTE: This file is AUTO GENERATED by qautomaton2cpp.xsl. */&#xA;</xsl:variable>

    <xsl:template match="tokenAutomaton">

        <xsl:variable name="uniqueLengths" as="xs:integer+" select="distinct-values(tokens/token/string-length())"/>

        <xsl:result-document method="text" href="{@headerFile}">

            <xsl:variable name="includeGuardName" select="string(@includeGuardName)"/>

            <xsl:value-of select="boilerplate/prolog"/>

            <xsl:value-of select="$warningGenerated"/>

            <xsl:text>&#xA;#ifndef </xsl:text>
            <xsl:value-of select="$includeGuardName"/>
            <xsl:text>&#xA;#define </xsl:text>
            <xsl:value-of select="$includeGuardName"/>
            <xsl:text>&#xA;&#xA;</xsl:text>

            <xsl:text>#include &lt;QtCore/QString>&#xA;</xsl:text>
            <xsl:text>&#xA;</xsl:text>
            <xsl:text>QT_BEGIN_NAMESPACE&#xA;</xsl:text>
            <xsl:text>&#xA;</xsl:text>

            <xsl:if test="@namespace">
                <xsl:text>namespace </xsl:text>
                <xsl:value-of select="@namespace"/>
                {
            </xsl:if>

            <xsl:text>class </xsl:text>
            <xsl:value-of select="@className"/>
                {
                <xsl:value-of select="@scope"/>:
                <xsl:text>enum </xsl:text>
                <xsl:value-of select="$tokenEnum"/>
                <xsl:text>&#xA;</xsl:text>
                {
                <xsl:value-of separator=",&#xA;">
                    <xsl:sequence select="@defaultToken"/>
                    <xsl:perform-sort select="tokens/token/local:tokenToEnumName(.)">
                        <xsl:sort select="."/>
                    </xsl:perform-sort>
                </xsl:value-of>
                };

                <xsl:text>static inline </xsl:text>
                <xsl:value-of select="$tokenEnum"/>
                <xsl:text> toToken(const QString &amp;value);&#xA;</xsl:text>
                <xsl:text>static inline </xsl:text>
                <xsl:value-of select="$tokenEnum"/>
                <xsl:text> toToken(const QStringRef &amp;value);&#xA;</xsl:text>
                <xsl:text>static </xsl:text>
                <xsl:value-of select="$tokenEnum"/>
                <xsl:text> toToken(const QChar *data, int length);&#xA;</xsl:text>
                    <xsl:if test="xs:boolean(@hasToString)">
                        <xsl:text>static QString toString(</xsl:text>
                        <xsl:value-of select="$tokenEnum"/>
                        <xsl:text> token);&#xA;</xsl:text>
                    </xsl:if>

                private:
                    <xsl:for-each select="$uniqueLengths">
                        <xsl:sort select="."/>
                        <xsl:text>static inline </xsl:text>
                        <xsl:value-of select="$tokenEnum"/>
                        <xsl:text> classifier</xsl:text>
                        <xsl:value-of select="."/>
                        <xsl:text>(const QChar *data);&#xA;</xsl:text>
                    </xsl:for-each>
                };

                <xsl:text>inline </xsl:text>
                <xsl:value-of select="@className"/>::<xsl:value-of select="$tokenEnum"/>
                <xsl:text> </xsl:text>
                <xsl:value-of select="@className"/>::toToken(const QString &amp;value)
                {
                    return toToken(value.constData(), value.length());
                }

                <xsl:text>inline </xsl:text>
                <xsl:value-of select="@className"/>::<xsl:value-of select="$tokenEnum"/>
                <xsl:text> </xsl:text>
                <xsl:value-of select="@className"/>::toToken(const QStringRef &amp;value)
                {
                    return toToken(value.constData(), value.length());
                }

                <xsl:if test="@namespace">
                    <xsl:text>}&#xA;</xsl:text>
                </xsl:if>

                <xsl:text>&#xA;</xsl:text>
                <xsl:text>QT_END_NAMESPACE&#xA;</xsl:text>
                <xsl:text>&#xA;</xsl:text>

                <xsl:text>#endif&#xA;</xsl:text>
        </xsl:result-document>

        <xsl:result-document method="text" href="{@sourceFile}">
            <xsl:value-of select="boilerplate/prolog"/>

            <xsl:value-of select="$warningGenerated"/>

            <xsl:text>&#xA;#include "</xsl:text>
            <xsl:value-of select="@headerFile"/>
            <xsl:text>"&#xA;</xsl:text>
            <xsl:text>&#xA;</xsl:text>
            <xsl:text>QT_BEGIN_NAMESPACE&#xA;</xsl:text>

            <xsl:if test="@namespace">
                <xsl:text>&#xA;</xsl:text>
                <xsl:text>using namespace </xsl:text>
                <xsl:value-of select="@namespace"/>
                <xsl:text>;&#xA;</xsl:text>
            </xsl:if>

            <xsl:text>&#xA;</xsl:text>
            <xsl:variable name="tokens" select="tokens/token"/>

            <xsl:for-each select="$uniqueLengths">
                <xsl:sort select="."/>
                <xsl:call-template name="generate-classifier">
                    <xsl:with-param name="strings" select="$tokens[string-length() eq current()]"/>
                </xsl:call-template>
            </xsl:for-each>

            <xsl:value-of select="@className"/>::<xsl:value-of select="$tokenEnum"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="@className"/>::toToken(const QChar *data, int length)
            {
                switch(length)
                {
                    <xsl:for-each select="$uniqueLengths">
                        <xsl:sort data-type="number" select="."/>
                        case <xsl:value-of select="."/>:
                            return classifier<xsl:value-of select="."/>(data);

                    </xsl:for-each>
                        default:
                            return <xsl:value-of select="@defaultToken"/>;
                }
            }

            <xsl:if test="xs:boolean(@hasToString)">
                QString <xsl:value-of select="@className"/>::toString(<xsl:value-of select="$tokenEnum"/> token)
                {
                    const unsigned short *data = 0;
                    int length = 0;

                    switch(token)
                    {
                    <xsl:for-each select="tokens/token">
                        <xsl:sort select="local:tokenToEnumName(.)"/>
                        case <xsl:sequence select="local:tokenToEnumName(.)"/>:
                        {<!-- Without these braces, the code doesn't compile on MSVC 2008. -->
                            static const unsigned short staticallyStored<xsl:value-of select="local:tokenToEnumName(.)"/>[] =
                            {
                            <xsl:value-of separator=", " select="string-to-codepoints(.), 0"/>
                            };
                            data = staticallyStored<xsl:value-of select="local:tokenToEnumName(.)"/>;
                            length = <xsl:value-of select="string-length(.)"/>;
                            break;
                        }
                    </xsl:for-each>
                        default:
                            /* It's either the default token, or an undefined enum
                             * value. We silence a compiler warning, and return the
                             * empty string. */
                            ;
                    }

                    union
                    {
                        const unsigned short *data;
                        const QChar *asQChar;
                    } converter;
                    converter.data = data;

                    return QString::fromRawData(converter.asQChar, length);
                }
            </xsl:if>

            <xsl:text>&#xA;</xsl:text>
            <xsl:text>QT_END_NAMESPACE&#xA;</xsl:text>
            <xsl:text>&#xA;</xsl:text>
        </xsl:result-document>

    </xsl:template>

    <xsl:template name="generate-classifier">
        <xsl:param name="strings" as="xs:string+"/>

        <xsl:value-of select="$className"/>::<xsl:value-of select="$tokenEnum"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="$className"/>::classifier<xsl:value-of select="."/>(const QChar *data)

        {
            <xsl:sequence select="local:generateBranching($strings, 1, 1)"/>

            return <xsl:value-of select="$defaultToken"/>;
        }
    </xsl:template>

    <xsl:function name="local:generateBranching" ><!--as="xs:string+">-->
        <xsl:param name="strings" as="xs:string+"/>
        <xsl:param name="depth" as="xs:integer"/>
        <xsl:param name="currentPos" as="xs:integer"/>

        <xsl:choose>
            <xsl:when test="count($strings) eq 1">
                <xsl:variable name="remainingLength" as="xs:integer" select="(string-length($strings) - $currentPos) + 1"/>
                <xsl:variable name="toMatch" as="xs:integer+" select="string-to-codepoints(substring($strings, $currentPos))"/>

                <xsl:if test="$remainingLength ne 0">
                    <xsl:choose>
                        <xsl:when test="$remainingLength eq 1">
                            if(data[<xsl:sequence select="$depth - 1"/>] == <xsl:sequence select="$toMatch"/>)
                        </xsl:when>
                        <xsl:when test="$remainingLength &gt; 1">
                            static const unsigned short string[] =
                            {
                                <xsl:value-of separator=", " select="string-to-codepoints(substring($strings, $currentPos))"/>
                            };
                            if(memcmp(&amp;data[<xsl:sequence select="$depth - 1"/>], &amp;string, sizeof(QChar) * <xsl:value-of select="$remainingLength"/>) == 0)
                        </xsl:when>
                    </xsl:choose>
                </xsl:if>

                return <xsl:value-of select="local:tokenToEnumName($strings)"/>;
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="distinct-values(for $i in $strings return substring($i, $currentPos, 1))">
                    <xsl:if test="position() &gt; 1">
                        <xsl:text>else </xsl:text>
                    </xsl:if>

                    <xsl:text>if (data[</xsl:text>
                    <xsl:sequence select="string($depth - 1)"/>
                    <xsl:text>] == </xsl:text>
                    <xsl:sequence select="string-to-codepoints(.)"/>
                    <xsl:text>)&#xA;</xsl:text>

                    {
                    <xsl:sequence select="local:generateBranching($strings[substring(., $currentPos, 1) eq current()], $depth + 1, $currentPos + 1)"/>
                    }

                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>

    <xsl:function name="local:toCamelCase" as="xs:string">
        <xsl:param name="arg" as="xs:string"/>

        <xsl:sequence select="string-join((for $word in tokenize($arg,'[:-]+')
                                           return concat(upper-case(substring($word,1,1)),
                                                         substring($word, 2))) ,'')"/>

    </xsl:function>

    <xsl:function name="local:tokenToEnumName" as="xs:string">
        <xsl:param name="string" as="xs:string"/>

        <xsl:variable name="token" select="$tokens[. eq $string]"/>

        <xsl:choose>
            <xsl:when test="$token/@name">
                <xsl:sequence select="$token/@name"/>
            </xsl:when>
            <xsl:otherwise>
                <!-- We take the token's string value, and coerces into a C++
                     name. So get rid of invalid characters. Also do basic camel casing. -->
                <xsl:variable name="normalized" select="translate($string, 'ABCDEFGHIJKLMNOPQRSTYXZabcdefghijklmnopqrstyxz1234567890_', 'ABCDEFGHIJKLMNOPQRSTYXZabcdefghijklmnopqrstyxz1234567890_')"/>
                <xsl:value-of select="local:toCamelCase($normalized)"/>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:function>

</xsl:stylesheet>

<!--
vim: et:ts=4:sw=4:sts=4
-->