source: trunk/src/xmlpatterns/qtokenautomaton/README@ 134

Last change on this file since 134 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 2.3 KB
Line 
1
2qtokenautomaton is a token generator, that generates a simple, Unicode aware
3tokenizer for C++ that uses the Qt API.
4
5Introduction
6=====================
7QTokenAutomaton generates a C++ class that essentially has this interface:
8
9 class YourTokenizer
10 {
11 protected:
12 enum Token
13 {
14 A,
15 B,
16 C,
17 NoKeyword
18 };
19
20 static inline Token toToken(const QString &string);
21 static inline Token toToken(const QStringRef &string);
22 static Token toToken(const QChar *data, int length);
23 static QString toString(Token token);
24 };
25
26When calling toToken(), the tokenizer returns the enum value corresponding to
27the string. This is done with O(N) time complexity, where N is the length of
28the string. The returned value can then subsequently be efficiently switched
29over. The alternatives, either a long chain of if statements comparing one
30QString to several other QStrings; or inserting all strings first into a hash,
31are less efficient.
32
33For instance, the latter case of using a hash would involve when excluding the
34initial populating of the hash, O(N) + O(1) where 0(1) is assumed to be a
35non-conflicting hash lookup.
36
37toString(), which returns the string for the token that an enum value
38represents, is implemented to store the strings in an efficient manner.
39
40A typical usage scenario is in combination with QXmlStreamReader. When parsing
41a certain format, for instance XHTML, each element name, body, span, table and
42so forth, typically needs special treatment. QTokenAutomaton conceptually cuts
43the string comparisons down to one.
44
45Beyond efficiency, QTokenAutomaton also increases type safety, since C++
46identifiers are used instead of string literals.
47
48Usage
49=====================
50Using it is approached as follows:
51
521. Create a token file. Use exampleFile.xml as a template.
53
542. Make sure it is valid by validating against qtokenautomaton.xsd. On
55 Linux, this can be achieved by running `xmllint --noout
56 --schema qtokenautomaton.xsd yourFile.xml`
57
583. Produce the C++ files by invoking the stylesheet with an XSL-T 2.0
59 processor[1]. For instance, with the implementation Saxon, this would be:
60 `java net.sf.saxon.Transform -xsl:qautomaton2cpp.xsl yourFile.xml`
61
624. Include the produced C++ files with your build system.
63
64
651.
66In Qt there is as of 4.4 no support for XSL-T.
Note: See TracBrowser for help on using the repository browser.