0
|
1 |
----------------------------------------------------------------------------
|
|
2 |
--
|
|
3 |
-- Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
-- All rights reserved.
|
|
5 |
-- Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
--
|
|
7 |
-- This file is part of the QtCore module of the Qt Toolkit.
|
|
8 |
--
|
|
9 |
-- $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
-- No Commercial Usage
|
|
11 |
-- This file contains pre-release code and may not be distributed.
|
|
12 |
-- You may use this file in accordance with the terms and conditions
|
|
13 |
-- contained in the Technology Preview License Agreement accompanying
|
|
14 |
-- this package.
|
|
15 |
--
|
|
16 |
-- GNU Lesser General Public License Usage
|
|
17 |
-- Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
-- General Public License version 2.1 as published by the Free Software
|
|
19 |
-- Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
-- packaging of this file. Please review the following information to
|
|
21 |
-- ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
-- will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
--
|
|
24 |
-- In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
-- rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
-- version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
--
|
|
28 |
-- If you have questions regarding the use of this file, please contact
|
|
29 |
-- Nokia at qt-info@nokia.com.
|
|
30 |
--
|
|
31 |
--
|
|
32 |
--
|
|
33 |
--
|
|
34 |
--
|
|
35 |
--
|
|
36 |
--
|
|
37 |
--
|
|
38 |
-- $QT_END_LICENSE$
|
|
39 |
--
|
|
40 |
----------------------------------------------------------------------------
|
|
41 |
|
|
42 |
%parser XMLTable
|
|
43 |
|
|
44 |
%impl xmlreader.cpp
|
|
45 |
|
|
46 |
%token LEFT_ANGLE
|
|
47 |
%token RIGHT_ANGLE
|
|
48 |
%token ANY
|
|
49 |
|
|
50 |
%start XmlStream
|
|
51 |
|
|
52 |
/.
|
|
53 |
#ifndef XMLREADER_H
|
|
54 |
#define XMLREADER_H
|
|
55 |
|
|
56 |
#include <QtCore>
|
|
57 |
#include <cstdio>
|
|
58 |
#include <cstdlib>
|
|
59 |
#include <cstring>
|
|
60 |
#include "$header"
|
|
61 |
|
|
62 |
class XMLReader: protected $table
|
|
63 |
{
|
|
64 |
public:
|
|
65 |
XMLReader(const QByteArray &bytes);
|
|
66 |
~XMLReader();
|
|
67 |
|
|
68 |
bool parse();
|
|
69 |
|
|
70 |
inline int nextToken()
|
|
71 |
{
|
|
72 |
switch (*bufptr++) {
|
|
73 |
case '\0':
|
|
74 |
return EOF_SYMBOL;
|
|
75 |
|
|
76 |
case '<':
|
|
77 |
in_tag = true;
|
|
78 |
return LEFT_ANGLE;
|
|
79 |
|
|
80 |
case '>':
|
|
81 |
if (! in_tag)
|
|
82 |
break;
|
|
83 |
in_tag = false;
|
|
84 |
return RIGHT_ANGLE;
|
|
85 |
break;
|
|
86 |
|
|
87 |
} // switch
|
|
88 |
|
|
89 |
return ANY;
|
|
90 |
}
|
|
91 |
|
|
92 |
protected:
|
|
93 |
inline void reallocateStack();
|
|
94 |
|
|
95 |
inline int &sym(int index)
|
|
96 |
{ return stack [tos + index - 1].ival; }
|
|
97 |
|
|
98 |
protected:
|
|
99 |
int tos;
|
|
100 |
int stack_size;
|
|
101 |
|
|
102 |
struct StackItem {
|
|
103 |
int state;
|
|
104 |
int ival;
|
|
105 |
};
|
|
106 |
|
|
107 |
QVarLengthArray<StackItem> stack;
|
|
108 |
unsigned in_tag: 1;
|
|
109 |
QByteArray bytes;
|
|
110 |
const char *bufptr;
|
|
111 |
};
|
|
112 |
|
|
113 |
inline void XMLReader::reallocateStack()
|
|
114 |
{
|
|
115 |
if (! stack_size)
|
|
116 |
stack_size = 128;
|
|
117 |
else
|
|
118 |
stack_size <<= 1;
|
|
119 |
|
|
120 |
stack.resize (stack_size);
|
|
121 |
}
|
|
122 |
|
|
123 |
#endif // XMLREADER_H
|
|
124 |
|
|
125 |
XMLReader::XMLReader(const QByteArray &bytes):
|
|
126 |
tos(0),
|
|
127 |
stack_size(0),
|
|
128 |
bytes(bytes)
|
|
129 |
{
|
|
130 |
bufptr = bytes.constData();
|
|
131 |
}
|
|
132 |
|
|
133 |
XMLReader::~XMLReader()
|
|
134 |
{
|
|
135 |
}
|
|
136 |
|
|
137 |
bool XMLReader::parse()
|
|
138 |
{
|
|
139 |
const int INITIAL_STATE = 0;
|
|
140 |
|
|
141 |
in_tag = 0;
|
|
142 |
bufptr = bytes.constData();
|
|
143 |
|
|
144 |
int yytoken = -1;
|
|
145 |
reallocateStack();
|
|
146 |
|
|
147 |
tos = 0;
|
|
148 |
stack [++tos].state = INITIAL_STATE;
|
|
149 |
|
|
150 |
while (true)
|
|
151 |
{
|
|
152 |
const int state = stack [tos].state;
|
|
153 |
|
|
154 |
if (yytoken == -1 && - TERMINAL_COUNT != action_index [state])
|
|
155 |
yytoken = nextToken();
|
|
156 |
|
|
157 |
int act = t_action (state, yytoken);
|
|
158 |
|
|
159 |
if (act == ACCEPT_STATE)
|
|
160 |
return true;
|
|
161 |
|
|
162 |
else if (act > 0)
|
|
163 |
{
|
|
164 |
if (++tos == stack_size)
|
|
165 |
reallocateStack();
|
|
166 |
|
|
167 |
stack [tos].ival = *bufptr; // ### save the token value here
|
|
168 |
stack [tos].state = act;
|
|
169 |
yytoken = -1;
|
|
170 |
}
|
|
171 |
|
|
172 |
else if (act < 0)
|
|
173 |
{
|
|
174 |
int r = - act - 1;
|
|
175 |
|
|
176 |
tos -= rhs [r];
|
|
177 |
act = stack [tos++].state;
|
|
178 |
|
|
179 |
switch (r) {
|
|
180 |
./
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
XmlStream: TagOrWord ;
|
|
186 |
XmlStream: XmlStream TagOrWord ;
|
|
187 |
|
|
188 |
TagOrWord: Tag ;
|
|
189 |
TagOrWord: ANY ;
|
|
190 |
|
|
191 |
Tag: LEFT_ANGLE TagName RIGHT_ANGLE ;
|
|
192 |
/.
|
|
193 |
case $rule_number: {
|
|
194 |
fprintf (stderr, "*** found a tag\n");
|
|
195 |
} break;
|
|
196 |
./
|
|
197 |
|
|
198 |
TagName: ANY ;
|
|
199 |
TagName: TagName ANY ;
|
|
200 |
|
|
201 |
|
|
202 |
/.
|
|
203 |
} // switch
|
|
204 |
|
|
205 |
stack [tos].state = nt_action (act, lhs [r] - TERMINAL_COUNT);
|
|
206 |
}
|
|
207 |
|
|
208 |
else
|
|
209 |
{
|
|
210 |
// ### ERROR RECOVERY HERE
|
|
211 |
break;
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
return false;
|
|
216 |
}
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
/////////////////////////////
|
|
221 |
// entry point
|
|
222 |
/////////////////////////////
|
|
223 |
int main(int, char *argv[])
|
|
224 |
{
|
|
225 |
QFile f (argv[1]);
|
|
226 |
|
|
227 |
if (f.open(QFile::ReadOnly)) {
|
|
228 |
QByteArray contents = f.readAll();
|
|
229 |
XMLReader parser (contents);
|
|
230 |
|
|
231 |
if (parser.parse())
|
|
232 |
printf ("OK\n");
|
|
233 |
else
|
|
234 |
printf ("KO\n");
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
./
|
|
242 |
|