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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
#include "qmlscriptparser_p.h"
#include "parser/javascriptengine_p.h"
#include "parser/javascriptparser_p.h"
#include "parser/javascriptlexer_p.h"
#include "parser/javascriptnodepool_p.h"
#include "parser/javascriptastvisitor_p.h"
#include "parser/javascriptast_p.h"
#include "parser/javascriptprettypretty_p.h"
#include <QStack>
#include <QtDebug>
QT_BEGIN_NAMESPACE
using namespace JavaScript;
using namespace QmlParser;
namespace {
class ProcessAST: protected AST::Visitor
{
struct State {
State() : object(0), property(0) {}
State(Object *o) : object(o), property(0) {}
State(Object *o, Property *p) : object(o), property(p) {}
Object *object;
Property *property;
};
struct StateStack : public QStack<State>
{
void pushObject(Object *obj)
{
push(State(obj));
}
void pushProperty(const QString &name, int lineNumber)
{
const State &state = top();
if (state.property) {
State s(state.property->getValue(),
state.property->getValue()->getProperty(name.toLatin1()));
s.property->line = lineNumber;
push(s);
} else {
State s(state.object,
state.object->getProperty(name.toLatin1()));
s.property->line = lineNumber;
push(s);
}
}
};
public:
ProcessAST(QmlScriptParser *parser);
virtual ~ProcessAST();
void operator()(AST::Node *node);
protected:
using AST::Visitor::visit;
using AST::Visitor::endVisit;
virtual bool visit(AST::UiObjectDefinition *node);
virtual void endVisit(AST::UiObjectDefinition *node);
virtual bool visit(AST::UiPublicMember *node);
virtual bool visit(AST::UiObjectBinding *node);
virtual bool visit(AST::UiScriptBinding *node);
virtual bool visit(AST::UiArrayBinding *node);
void accept(AST::Node *node);
QString asString(AST::UiQualifiedId *node) const;
const State state() const;
Object *currentObject() const;
Property *currentProperty() const;
QString qualifiedNameId() const;
private:
QmlScriptParser *_parser;
StateStack _stateStack;
QStringList _scope;
};
ProcessAST::ProcessAST(QmlScriptParser *parser)
: _parser(parser)
{
}
ProcessAST::~ProcessAST()
{
}
void ProcessAST::operator()(AST::Node *node)
{
accept(node);
}
void ProcessAST::accept(AST::Node *node)
{
AST::Node::acceptChild(node, this);
}
const ProcessAST::State ProcessAST::state() const
{
if (_stateStack.isEmpty())
return State();
return _stateStack.back();
}
Object *ProcessAST::currentObject() const
{
return state().object;
}
Property *ProcessAST::currentProperty() const
{
return state().property;
}
QString ProcessAST::qualifiedNameId() const
{
return _scope.join(QLatin1String("/"));
}
QString ProcessAST::asString(AST::UiQualifiedId *node) const
{
QString s;
for (AST::UiQualifiedId *it = node; it; it = it->next) {
s.append(it->name->asString());
if (it->next)
s.append(QLatin1Char('.'));
}
return s;
}
// UiObjectMember: T_PUBLIC T_IDENTIFIER T_IDENTIFIER T_COLON Expression UiObjectInitializer ;
bool ProcessAST::visit(AST::UiPublicMember *node)
{
qWarning() << Q_FUNC_INFO << "not implemented";
return false;
}
// UiObjectMember: T_IDENTIFIER UiObjectInitializer ;
bool ProcessAST::visit(AST::UiObjectDefinition *node)
{
const QString name = node->name->asString();
bool isType = name.at(0).isUpper() && !name.contains(QLatin1Char('.'));
_scope.append(name);
if (! isType) {
qWarning() << "bad name for a class"; // ### FIXME
return false;
}
// Class
const int typeId = _parser->findOrCreateTypeId(name);
int line = node->identifierToken.startLine;
Object *obj = new Object;
obj->type = typeId;
obj->typeName = qualifiedNameId().toLatin1();
obj->line = line;
if (! _parser->tree()) {
_parser->setTree(obj);
_stateStack.pushObject(obj);
} else {
const State state = _stateStack.top();
Value *v = new Value;
v->object = obj;
v->line = line;
if(state.property)
state.property->addValue(v);
else
state.object->getDefaultProperty()->addValue(v);
_stateStack.pushObject(obj);
}
return true;
}
// UiObjectMember: T_IDENTIFIER UiObjectInitializer ;
void ProcessAST::endVisit(AST::UiObjectDefinition *)
{
_stateStack.pop();
_scope.removeLast();
}
// UiObjectMember: UiQualifiedId T_COLON T_IDENTIFIER UiObjectInitializer ;
bool ProcessAST::visit(AST::UiObjectBinding *node)
{
qWarning() << Q_FUNC_INFO << "not implemented";
return false;
}
// UiObjectMember: UiQualifiedId T_COLON Statement ;
bool ProcessAST::visit(AST::UiScriptBinding *node)
{
const QString qualifiedId = asString(node->qualifiedId);
const QStringList str = qualifiedId.split(QLatin1Char('.'));
int line = node->colonToken.startLine;
for(int ii = 0; ii < str.count(); ++ii) {
const QString s = str.at(ii);
_stateStack.pushProperty(s, line);
}
QString primitive;
QTextStream out(&primitive);
PrettyPretty pp(out);
Property *prop = currentProperty();
if (node->statement->kind == AST::Node::Kind_ExpressionStatement) {
AST::ExpressionStatement *stmt = static_cast<AST::ExpressionStatement *>(node->statement);
if(prop->name.length() >= 3 && prop->name.startsWith("on") &&
('A' <= prop->name.at(2) && 'Z' >= prop->name.at(2))) {
pp(stmt->expression);
// here comes a cruel hack until we support functions properly with arguments for signal properties
if (primitive.startsWith(QLatin1String("function("))) {
int brace = 0;
for (;brace < primitive.size(); ++brace)
if (primitive.at(brace) == QLatin1Char('{'))
break;
primitive = primitive.mid(brace + 1, primitive.size() - brace - 2);
}
//end of hack
} else if (prop->name == "id" && stmt->expression && stmt->expression->kind == AST::Node::Kind_IdentifierExpression) {
primitive = static_cast<AST::IdentifierExpression *>(stmt->expression)->name->asString();
} else if (stmt->expression->kind == AST::Node::Kind_StringLiteral) {
// hack: emulate weird XML feature that string literals are not quoted.
//This needs to be fixed in the qmlcompiler once xml goes away.
primitive = static_cast<AST::StringLiteral *>(stmt->expression)->value->asString();
} else if (stmt->expression->kind == AST::Node::Kind_TrueLiteral
|| stmt->expression->kind == AST::Node::Kind_FalseLiteral
|| stmt->expression->kind == AST::Node::Kind_NumericLiteral
) {
pp(stmt->expression);
} else {
// create a binding
out << "{";
pp(stmt->expression);
out << "}";
}
} else {
pp(node->statement);
}
Value *v = new Value;
v->primitive = primitive;
v->line = line;
prop->addValue(v);
for(int ii = str.count() - 1; ii >= 0; --ii)
_stateStack.pop();
return false;
}
// UiObjectMember: UiQualifiedId T_COLON T_LBRACKET UiObjectMemberList T_RBRACKET ;
bool ProcessAST::visit(AST::UiArrayBinding *node)
{
qWarning() << Q_FUNC_INFO << "not implemented";
return false;
}
} // end of anonymous namespace
QmlScriptParser::QmlScriptParser()
: root(0)
{
}
QmlScriptParser::~QmlScriptParser()
{
}
bool QmlScriptParser::parse(const QByteArray &data, const QUrl &url)
{
const QString fileName = url.toString();
const QString code = QString::fromUtf8(data); // ### FIXME
JavaScriptParser parser;
JavaScriptEnginePrivate driver;
NodePool nodePool(fileName, &driver);
driver.setNodePool(&nodePool);
Lexer lexer(&driver);
lexer.setCode(code, /*line = */ 1);
driver.setLexer(&lexer);
if (! parser.parse(&driver)) {
_error = parser.errorMessage();
return false;
}
ProcessAST process(this);
process(parser.ast());
return true;
}
QString QmlScriptParser::errorDescription() const
{
return _error;
}
QMap<QString,QString> QmlScriptParser::nameSpacePaths() const
{
qWarning() << Q_FUNC_INFO << "not implemented";
return _nameSpacePaths;
}
QStringList QmlScriptParser::types() const
{
return _typeNames;
}
Object *QmlScriptParser::tree() const
{
return root;
}
int QmlScriptParser::findOrCreateTypeId(const QString &name)
{
int index = _typeNames.indexOf(name);
if (index == -1) {
index = _typeNames.size();
_typeNames.append(name);
}
return index;
}
void QmlScriptParser::setTree(Object *tree)
{
Q_ASSERT(! root);
root = tree;
}
QT_END_NAMESPACE
|