-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapi.js
443 lines (394 loc) · 11.6 KB
/
api.js
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* @license
Copyright (c) 2010 Mihai Bazon <[email protected]>
Copyright (c) 2011 Lauri Paimen <[email protected]>
Copyright (c) 2013 Anton Kreuzkamp <[email protected]>
Copyright (c) 2016 qmlweb-parser contributors
Based on parse-js (http://marijn.haverbeke.nl/parse-js/).
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/*
* QML parser and parsetree'er.
*
* Exports:
*
* - qmlweb_parse(src, type) -- parses QML source and returns it as output
* tree expected by the QML engine
*/
// Object cloning for debug prints.
function clone(obj) {
if (obj == null || typeof obj !== 'object')
return obj;
var temp = {}; // changed
for (var key in obj)
temp[key] = clone(obj[key]);
return temp;
}
function QMLParseError(message, line, col, pos, source) {
JS_Parse_Error.call(this, message, line, col, pos);
var comment = extractLinesForErrorDiag(source, line);
this.comment = comment ? comment : "";
this.message += " (line: " + this.line + ", col: " + col + ", pos: " + pos + ")" + "\n" + comment + "\n";
this.file = qmlweb_parse.nowParsingFile;
}
QMLParseError.prototype = new Error();
function extractLinesForErrorDiag(text, line) {
var r = "";
var lines = text.split("\n");
for (var i = line - 3; i <= line + 3; i++) {
if (i >= 0 && i < lines.length ) {
var mark = i === line ? ">>" : " ";
r += mark + (i + 1) + " " + lines[i] + "\n";
}
}
return r;
}
function qmlweb_tokenizer($TEXT) {
// Override UglifyJS methods
parse_error = function(err) {
throw new QMLParseError(err, S.tokline, S.tokcol, S.tokpos, S.text);
};
// WARNING: Here the original tokenizer() code gets embedded
return tokenizer($TEXT);
}
function qmlweb_parse($TEXT, document_type, exigent_mode) {
var embed_tokens = false; // embed_tokens option is not supported
var TEXT = $TEXT.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, '');
$TEXT = qmlweb_tokenizer($TEXT, true);
// WARNING: Here the original parse() code gets embedded
parse($TEXT,exigent_mode,false);
// NOTE: Don't insert spaces between arguments!
// Override UglifyJS methods
croak = function(msg, line, col, pos) {
var ctx = S.input.context();
throw new QMLParseError(msg,
line != null ? line : ctx.tokline,
col != null ? col : ctx.tokcol,
pos != null ? pos : ctx.tokpos,
TEXT
);
};
expect_token = function(type, val) {
if (is(type, val)) {
return next();
}
token_error(S.token, "Unexpected token " + S.token.type + " " + S.token.val + ", expected " + type + " " + val);
};
function qml_get_attached_object(type, val) {
if (type === "name" && qml_is_element(val)) {
return "$QmlWebGetAttachedObject('" + val + "')";
}
return val
}
var subscripts_js = subscripts;
subscripts = function(expr, allow_calls) {
if (is("punc", ".")) {
next();
expr[1] = qml_get_attached_object(expr[0], expr[1]);
S.token.value = qml_get_attached_object(S.token.type, S.token.value);
return subscripts(as("dot", expr, as_name()), allow_calls);
}
return subscripts_js(expr, allow_calls);
};
var statement_js = statement;
statement = function() {
var in_qmlpropdef = !!statement.in_qmlpropdef;
statement.in_qmlpropdef = false;
switch (S.token.type) {
case "punc":
switch (S.token.value) {
case ".":
return is_token(peek(), "name", "pragma") ? qml_pragma_statement() : unexpected();
}
case "keyword":
switch (S.token.value) {
case "function":
if (in_qmlpropdef) {
next();
return function_(false);
}
}
}
return statement_js();
};
array_ = function() {
var from = S.token.pos;
var stat = expr_list("]", !exigent_mode, true);
var to = S.token.pos;
return as("array", stat, "[" + TEXT.substr(from, to - from));
};
expression = function(commas, no_in) {
if (arguments.length == 0)
commas = true;
var expr = maybe_qmlelem(no_in);
if (commas && is("punc", ",")) {
next();
return as("seq", expr, expression(true, no_in));
}
return expr;
};
// QML-specific methods
function as_statement() {
var res = slice(arguments);
var src = "";
S.in_function++;
var start = S.token.pos;
var s = statement();
res.push(s);
src += gen_code(s);
var end = S.token.pos;
S.in_function--;
res.push(src);
return res;
}
function maybe_qmlelem(no_in) {
var expr = maybe_assign(no_in);
if (is("punc", "{"))
return as("qmlelem", expr[1], undefined, qmlblock());
return expr;
}
function qml_is_element(str) {
return str[0].toUpperCase() == str[0];
}
function qmlblock() {
expect("{");
var a = [];
while (!is("punc", "}")) {
if (is("eof"))
unexpected();
a.push(qmlstatement());
}
expect("}");
return a;
}
function qmlpropdef() {
var type = S.token.value;
next();
var name = S.token.value;
next();
if (type == "alias") {
expect(":");
if (!is("name"))
unexpected();
var objName = S.token.value;
next();
if (is("punc", ".")) {
next();
if (!is("name"))
unexpected();
var propName = S.token.value;
next();
}
return as("qmlaliasdef", name, objName, propName);
}
if (is("punc", ":")) {
next();
statement.in_qmlpropdef = true;
return as_statement("qmlpropdef", name, type);
} else if (is("punc", ";"))
next();
return as("qmlpropdef", name, type);
}
function qmldefaultprop() {
next();
expect_token("name", "property");
return as("qmldefaultprop", qmlpropdef());
}
function qmlsignaldef() {
var name = S.token.value;
next();
var args = [];
if (is("punc", "(")) {
next();
var first = true;
while (!is("punc", ")")) {
if (first)
first = false;
else
expect(",");
if (!is("name") && !is('keyword', 'var'))
unexpected();
var type = S.token.value;
next();
if (!is("name"))
unexpected();
args.push({ type: type, name: S.token.value });
next();
}
next();
}
if (is("punc", ";"))
next();
return as("qmlsignaldef", name, args);
}
function qmlstatement() {
if (is("keyword", "function")) {
var from = S.token.pos;
next();
var stat = function_(true);
var to = S.token.pos;
var name = stat[1];
return as("qmlmethod", name, stat, TEXT.substr(from, to - from));
} else if (is("name", "signal")) {
next();
if (is("punc", ":")) {
next();
return as_statement("qmlprop", "signal");
} else {
return qmlsignaldef();
}
} else if (S.token.type == "name") {
var propname = S.token.value;
next();
if (propname == "property" && (S.token.type == "name" || S.token.value == "var")) {
return qmlpropdef();
} else if (is("punc", ".")) { // property statement
// anchors, fonts etc, a.b: statement;
// Can also be Component.onCompleted: ...
// Assume only one subproperty
next();
var subname = S.token.value;
next();
/* Check for ModuleQualifier.QMLElement */
if (qml_is_element(subname)) {
return as("qmlelem", propname + "." + subname, undefined, qmlblock());
}
expect(":");
return as_statement("qmlobjdef", propname, subname);
} else if (qml_is_element(propname)) {
// Element
var onProp;
if (is("name", "on")) {
next();
onProp = S.token.value;
next();
}
return as("qmlelem", propname, onProp, qmlblock());
} else if (is("punc", "{")) {
return as("qmlobj", propname, qmlblock());
} else {
// Evaluatable item
expect(":");
return as_statement("qmlprop", propname);
}
} else if (is("keyword", "default")) {
return qmldefaultprop();
} else {
todo();
}
}
function qml_pragma_statement() {
next();
next();
var pragma = S.token.value;
next();
return as("qmlpragma", pragma);
}
function qmlimport() {
// todo
next();
var moduleName = S.token.value;
var isDottedNotation = S.token.type == "name";
next();
while (is("punc", ".")) {
next();
moduleName += "." + S.token.value;
next();
}
if (is("num")) {
var version = S.token.value;
next();
}
var namespace = "";
if (is("name", "as")) {
next();
namespace = S.token.value;
next();
}
return as("qmlimport", moduleName, version, namespace, isDottedNotation);
}
function qmldocument() {
var imports = [];
while (is("name", "import")) {
imports.push(qmlimport());
}
var root = qmlstatement();
if (!is("eof"))
unexpected();
return as("toplevel", imports, root);
}
function jsdocument() {
var statements = [];
while (!is("eof")) {
statements.push(statement());
}
return as("jsresource", statements);
}
function amIn(s) {
console && console.log(s, clone(S), S.token.type, S.token.value);
}
function todo() {
amIn("todo parse:");
next();
}
if (document_type === qmlweb_parse.JSResource) {
return jsdocument();
} else {
return qmldocument();
}
}
qmlweb_parse.nowParsingFile = ''; // TODO: make a parameter of qmlweb_parse
qmlweb_parse.QMLDocument = 1;
qmlweb_parse.JSResource = 2;
function qmlweb_jsparse(source) {
var obj = { exports: [], source: source };
var AST_Tree = qmlweb_parse(source, qmlweb_parse.JSResource);
var main_scope = AST_Tree[1];
for (var i = 0 ; i < main_scope.length ; ++i) {
var item = main_scope[i];
switch (item[0]) {
case "var":
obj.exports.push(item[1][0][0]);
break ;
case "defun":
obj.exports.push(item[1]);
break ;
}
}
return obj;
}
if (typeof window !== 'undefined') {
// Browser: export only QmlWeb.parse and QmlWeb.jsparse
if (typeof QmlWeb === 'undefined') {
window.QmlWeb = {};
}
QmlWeb.parse = qmlweb_parse;
QmlWeb.jsparse = qmlweb_jsparse;
} else {
// Node.js
module.exports.parse = qmlweb_parse;
module.exports.jsparse = qmlweb_jsparse;
// Legacy
module.exports.qmlweb_parse = qmlweb_parse;
module.exports.qmlweb_jsparse = qmlweb_jsparse;
}