Skip to content

Commit dd0dece

Browse files
committed
qmlweb_tokenizer: support multiline strings in QML mode
Fixes: #29 PR-URL: #31
1 parent 59233bf commit dd0dece

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

src/api.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,44 @@ function extractLinesForErrorDiag(text, line) {
7777
return r;
7878
}
7979

80-
function qmlweb_tokenizer($TEXT) {
80+
function qmlweb_tokenizer($TEXT, document_type) {
8181
// Override UglifyJS methods
8282

8383
parse_error = function(err) {
8484
throw new QMLParseError(err, S.tokline, S.tokcol, S.tokpos, S.text);
8585
};
8686

87+
if (document_type === qmlweb_parse.QMLDocument) {
88+
// We need to support multiline strings in QML mode, allow newline chars
89+
// We don't need to support octal escape sequences, as those are not
90+
// supported in QML
91+
read_string = function() {
92+
return with_eof_error("Unterminated string constant", function(){
93+
var quote = next(), ret = "";
94+
for (;;) {
95+
var ch = next(true);
96+
if (ch == "\\") {
97+
ch = read_escaped_char(true);
98+
} else if (ch == quote) {
99+
break;
100+
}
101+
ret += ch;
102+
}
103+
return token("string", ret);
104+
});
105+
}
106+
}
107+
87108
// WARNING: Here the original tokenizer() code gets embedded
88109
return tokenizer($TEXT);
89110
}
90111

91112
function qmlweb_parse($TEXT, document_type, exigent_mode) {
92113
var embed_tokens = false; // embed_tokens option is not supported
114+
document_type = document_type || qmlweb_parse.QMLDocument;
93115

94116
var TEXT = $TEXT.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, '');
95-
$TEXT = qmlweb_tokenizer($TEXT, true);
117+
$TEXT = qmlweb_tokenizer($TEXT, document_type);
96118

97119
// WARNING: Here the original parse() code gets embedded
98120
parse($TEXT,exigent_mode,false);

tests/qml/MultilineString.qml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import QtQuick 2.0
2+
3+
Item {
4+
property string foo: "foo\""
5+
property string bar: "foo
6+
hello
7+
world
8+
"
9+
property string test: '
10+
\'
11+
'
12+
}

tests/qml/MultilineString.qml.json

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[
2+
"toplevel",
3+
[
4+
[
5+
"qmlimport",
6+
"QtQuick",
7+
2,
8+
"",
9+
true
10+
]
11+
],
12+
[
13+
"qmlelem",
14+
"Item",
15+
null,
16+
[
17+
[
18+
"qmlpropdef",
19+
"foo",
20+
"string",
21+
[
22+
"stat",
23+
[
24+
"string",
25+
"foo\""
26+
]
27+
],
28+
"\"foo\\\"\"\n "
29+
],
30+
[
31+
"qmlpropdef",
32+
"bar",
33+
"string",
34+
[
35+
"stat",
36+
[
37+
"string",
38+
"foo\n hello\n world\n "
39+
]
40+
],
41+
"\"foo\n hello\n world\n \"\n "
42+
],
43+
[
44+
"qmlpropdef",
45+
"test",
46+
"string",
47+
[
48+
"stat",
49+
[
50+
"string",
51+
"\n '\n "
52+
]
53+
],
54+
"'\n \\'\n '\n"
55+
]
56+
]
57+
]
58+
]

0 commit comments

Comments
 (0)