@zackad/prettier-plugin-twig 0.7.0-alpha.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -7
- package/package.json +10 -8
- package/src/index.js +53 -53
- package/src/melody/melody-code-frame/index.js +44 -0
- package/src/melody/melody-code-frame/lineNumbers.js +45 -0
- package/src/melody/melody-extension-core/index.js +167 -0
- package/src/melody/melody-extension-core/operators.js +414 -0
- package/src/melody/melody-extension-core/parser/autoescape.js +92 -0
- package/src/melody/melody-extension-core/parser/block.js +93 -0
- package/src/melody/melody-extension-core/parser/do.js +32 -0
- package/src/melody/melody-extension-core/parser/embed.js +76 -0
- package/src/melody/melody-extension-core/parser/extends.js +35 -0
- package/src/melody/melody-extension-core/parser/filter.js +63 -0
- package/src/melody/melody-extension-core/parser/flush.js +33 -0
- package/src/melody/melody-extension-core/parser/for.js +110 -0
- package/src/melody/melody-extension-core/parser/from.js +63 -0
- package/src/melody/melody-extension-core/parser/if.js +100 -0
- package/src/melody/melody-extension-core/parser/import.js +44 -0
- package/src/melody/melody-extension-core/parser/include.js +48 -0
- package/src/melody/melody-extension-core/parser/macro.js +94 -0
- package/src/melody/melody-extension-core/parser/mount.js +147 -0
- package/src/melody/melody-extension-core/parser/set.js +103 -0
- package/src/melody/melody-extension-core/parser/spaceless.js +56 -0
- package/src/melody/melody-extension-core/parser/use.js +58 -0
- package/src/melody/melody-extension-core/types.js +402 -0
- package/src/melody/melody-extension-core/visitors/filters.js +146 -0
- package/src/melody/melody-extension-core/visitors/for.js +391 -0
- package/src/melody/melody-extension-core/visitors/functions.js +100 -0
- package/src/melody/melody-extension-core/visitors/tests.js +127 -0
- package/src/melody/melody-parser/Associativity.js +17 -0
- package/src/melody/melody-parser/CharStream.js +82 -0
- package/src/melody/melody-parser/GenericMultiTagParser.js +66 -0
- package/src/melody/melody-parser/GenericTagParser.js +53 -0
- package/src/melody/melody-parser/Lexer.js +708 -0
- package/src/melody/melody-parser/Parser.js +954 -0
- package/src/melody/melody-parser/TokenStream.js +183 -0
- package/src/melody/melody-parser/TokenTypes.js +74 -0
- package/src/melody/melody-parser/elementInfo.js +43 -0
- package/src/melody/melody-parser/index.js +94 -0
- package/src/melody/melody-parser/util.js +100 -0
- package/src/melody/melody-traverse/Binding.js +61 -0
- package/src/melody/melody-traverse/Path.js +427 -0
- package/src/melody/melody-traverse/Scope.js +218 -0
- package/src/melody/melody-traverse/TraversalContext.js +166 -0
- package/src/melody/melody-traverse/index.js +21 -0
- package/src/melody/melody-traverse/traverse.js +48 -0
- package/src/melody/melody-traverse/visitors.js +112 -0
- package/src/melody/melody-types/index.js +506 -0
- package/src/parser.js +7 -2
- package/src/print/Attribute.js +1 -1
- package/src/print/BinaryExpression.js +2 -2
- package/src/print/BlockStatement.js +1 -1
- package/src/print/CallExpression.js +1 -1
- package/src/print/ExpressionStatement.js +1 -1
- package/src/print/FilterExpression.js +1 -1
- package/src/print/GenericTwigTag.js +1 -1
- package/src/print/IfStatement.js +1 -1
- package/src/print/ObjectProperty.js +1 -1
- package/src/print/SetStatement.js +1 -1
- package/src/print/SwitchTag.js +1 -1
- package/src/print/UnarySubclass.js +1 -1
- package/src/util/printFunctions.js +1 -1
- package/src/util/publicFunctions.js +1 -1
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2017 trivago N.V.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import * as TokenTypes from "./TokenTypes.js";
|
|
17
|
+
import { EOF } from "./CharStream.js";
|
|
18
|
+
|
|
19
|
+
const State = {
|
|
20
|
+
TEXT: "TEXT",
|
|
21
|
+
EXPRESSION: "EXPRESSION",
|
|
22
|
+
TAG: "TAG",
|
|
23
|
+
INTERPOLATION: "INTERPOLATION",
|
|
24
|
+
STRING_SINGLE: "STRING_SINGLE",
|
|
25
|
+
STRING_DOUBLE: "STRING_DOUBLE",
|
|
26
|
+
ELEMENT: "ELEMENT",
|
|
27
|
+
ATTRIBUTE_VALUE: "ATTRIBUTE_VALUE",
|
|
28
|
+
DECLARATION: "DECLARATION"
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const STATE = Symbol("STATE");
|
|
32
|
+
const OPERATORS = Symbol("OPERATORS");
|
|
33
|
+
const STRING_START = Symbol("STRING_START");
|
|
34
|
+
|
|
35
|
+
const CHAR_TO_TOKEN = {
|
|
36
|
+
"[": TokenTypes.LBRACE,
|
|
37
|
+
"]": TokenTypes.RBRACE,
|
|
38
|
+
"(": TokenTypes.LPAREN,
|
|
39
|
+
")": TokenTypes.RPAREN,
|
|
40
|
+
"{": TokenTypes.LBRACKET,
|
|
41
|
+
"}": TokenTypes.RBRACKET,
|
|
42
|
+
":": TokenTypes.COLON,
|
|
43
|
+
".": TokenTypes.DOT,
|
|
44
|
+
"|": TokenTypes.PIPE,
|
|
45
|
+
",": TokenTypes.COMMA,
|
|
46
|
+
"?": TokenTypes.QUESTION_MARK,
|
|
47
|
+
"=": TokenTypes.ASSIGNMENT,
|
|
48
|
+
//'<': TokenTypes.ELEMENT_START,
|
|
49
|
+
//'>': TokenTypes.ELEMENT_END,
|
|
50
|
+
"/": TokenTypes.SLASH
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export default class Lexer {
|
|
54
|
+
constructor(input, { preserveSourceLiterally = false } = {}) {
|
|
55
|
+
this.input = input;
|
|
56
|
+
this[STATE] = [State.TEXT];
|
|
57
|
+
this[OPERATORS] = [];
|
|
58
|
+
this[STRING_START] = null;
|
|
59
|
+
this.options = {
|
|
60
|
+
preserveSourceLiterally: preserveSourceLiterally === true
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
applyExtension(ext) {
|
|
65
|
+
if (ext.unaryOperators) {
|
|
66
|
+
this.addOperators(...ext.unaryOperators.map(op => op.text));
|
|
67
|
+
}
|
|
68
|
+
if (ext.binaryOperators) {
|
|
69
|
+
this.addOperators(...ext.binaryOperators.map(op => op.text));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
reset() {
|
|
74
|
+
this.input.reset();
|
|
75
|
+
this[STATE] = [State.TEXT];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get source() {
|
|
79
|
+
return this.input.source;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
addOperators(...ops) {
|
|
83
|
+
this[OPERATORS].push(...ops);
|
|
84
|
+
this[OPERATORS].sort((a, b) => (a.length > b.length ? -1 : 1));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get state() {
|
|
88
|
+
return this[STATE][this[STATE].length - 1];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
pushState(state) {
|
|
92
|
+
this[STATE].push(state);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
popState() {
|
|
96
|
+
this[STATE].length--;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
createToken(type, pos) {
|
|
100
|
+
const input = this.input;
|
|
101
|
+
const endPos = input.mark();
|
|
102
|
+
const end = endPos.index;
|
|
103
|
+
return {
|
|
104
|
+
type,
|
|
105
|
+
pos,
|
|
106
|
+
endPos,
|
|
107
|
+
end,
|
|
108
|
+
length: end - pos.index,
|
|
109
|
+
source: input.input,
|
|
110
|
+
text: input.input.substr(pos.index, end - pos.index),
|
|
111
|
+
toString: function () {
|
|
112
|
+
return this.text;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
next() {
|
|
118
|
+
const input = this.input;
|
|
119
|
+
let pos;
|
|
120
|
+
let c;
|
|
121
|
+
while ((c = input.la(0)) !== EOF) {
|
|
122
|
+
pos = input.mark();
|
|
123
|
+
if (
|
|
124
|
+
this.state !== State.TEXT &&
|
|
125
|
+
this.state !== State.STRING_DOUBLE &&
|
|
126
|
+
this.state !== State.STRING_SINGLE &&
|
|
127
|
+
this.state !== State.ATTRIBUTE_VALUE &&
|
|
128
|
+
isWhitespace(c)
|
|
129
|
+
) {
|
|
130
|
+
input.next();
|
|
131
|
+
while ((c = input.la(0)) !== EOF && isWhitespace(c)) {
|
|
132
|
+
input.next();
|
|
133
|
+
}
|
|
134
|
+
return this.createToken(TokenTypes.WHITESPACE, pos);
|
|
135
|
+
}
|
|
136
|
+
if (c === "{" && input.la(1) === "#") {
|
|
137
|
+
input.next();
|
|
138
|
+
input.next();
|
|
139
|
+
if (input.la(0) === "-") {
|
|
140
|
+
input.next();
|
|
141
|
+
}
|
|
142
|
+
while ((c = input.la(0)) !== EOF) {
|
|
143
|
+
if (
|
|
144
|
+
(c === "#" && input.la(1) === "}") ||
|
|
145
|
+
(c === "-" &&
|
|
146
|
+
input.la(1) === "#" &&
|
|
147
|
+
input.la(2) === "}")
|
|
148
|
+
) {
|
|
149
|
+
if (c === "-") {
|
|
150
|
+
input.next();
|
|
151
|
+
}
|
|
152
|
+
input.next();
|
|
153
|
+
input.next();
|
|
154
|
+
return this.createToken(TokenTypes.COMMENT, pos);
|
|
155
|
+
}
|
|
156
|
+
input.next();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (this.state === State.TEXT) {
|
|
160
|
+
let entityToken;
|
|
161
|
+
if (c === "<") {
|
|
162
|
+
if (
|
|
163
|
+
input.la(1) === "{" ||
|
|
164
|
+
isAlpha(input.lac(1)) ||
|
|
165
|
+
input.la(1) === "/"
|
|
166
|
+
) {
|
|
167
|
+
input.next();
|
|
168
|
+
this.pushState(State.ELEMENT);
|
|
169
|
+
return this.createToken(TokenTypes.ELEMENT_START, pos);
|
|
170
|
+
} else if (
|
|
171
|
+
input.la(1) === "!" &&
|
|
172
|
+
input.la(2) === "-" &&
|
|
173
|
+
input.la(3) === "-"
|
|
174
|
+
) {
|
|
175
|
+
// match HTML comment
|
|
176
|
+
input.next(); // <
|
|
177
|
+
input.next(); // !
|
|
178
|
+
input.next(); // -
|
|
179
|
+
input.next(); // -
|
|
180
|
+
while ((c = input.la(0)) !== EOF) {
|
|
181
|
+
if (c === "-" && input.la(1) === "-") {
|
|
182
|
+
input.next();
|
|
183
|
+
input.next();
|
|
184
|
+
if (!(c = input.next()) === ">") {
|
|
185
|
+
this.error(
|
|
186
|
+
"Unexpected end for HTML comment",
|
|
187
|
+
input.mark(),
|
|
188
|
+
`Expected comment to end with '>' but found '${c}' instead.`
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
input.next();
|
|
194
|
+
}
|
|
195
|
+
return this.createToken(TokenTypes.HTML_COMMENT, pos);
|
|
196
|
+
} else if (
|
|
197
|
+
input.la(1) === "!" &&
|
|
198
|
+
(isAlpha(input.lac(2)) || isWhitespace(input.la(2)))
|
|
199
|
+
) {
|
|
200
|
+
input.next();
|
|
201
|
+
input.next();
|
|
202
|
+
this.pushState(State.DECLARATION);
|
|
203
|
+
return this.createToken(
|
|
204
|
+
TokenTypes.DECLARATION_START,
|
|
205
|
+
pos
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
return this.matchText(pos);
|
|
209
|
+
} else if (c === "{") {
|
|
210
|
+
return this.matchExpressionToken(pos);
|
|
211
|
+
} else if (c === "&" && (entityToken = this.matchEntity(pos))) {
|
|
212
|
+
return entityToken;
|
|
213
|
+
}
|
|
214
|
+
return this.matchText(pos);
|
|
215
|
+
} else if (this.state === State.EXPRESSION) {
|
|
216
|
+
if (
|
|
217
|
+
(c === "}" && input.la(1) === "}") ||
|
|
218
|
+
(c === "-" && input.la(1) === "}" && input.la(2) === "}")
|
|
219
|
+
) {
|
|
220
|
+
if (c === "-") {
|
|
221
|
+
input.next();
|
|
222
|
+
}
|
|
223
|
+
input.next();
|
|
224
|
+
input.next();
|
|
225
|
+
this.popState();
|
|
226
|
+
return this.createToken(TokenTypes.EXPRESSION_END, pos);
|
|
227
|
+
}
|
|
228
|
+
return this.matchExpression(pos);
|
|
229
|
+
} else if (this.state === State.TAG) {
|
|
230
|
+
if (
|
|
231
|
+
(c === "%" && input.la(1) === "}") ||
|
|
232
|
+
(c === "-" && input.la(1) === "%" && input.la(2) === "}")
|
|
233
|
+
) {
|
|
234
|
+
if (c === "-") {
|
|
235
|
+
input.next();
|
|
236
|
+
}
|
|
237
|
+
input.next();
|
|
238
|
+
input.next();
|
|
239
|
+
this.popState();
|
|
240
|
+
return this.createToken(TokenTypes.TAG_END, pos);
|
|
241
|
+
}
|
|
242
|
+
return this.matchExpression(pos);
|
|
243
|
+
} else if (
|
|
244
|
+
this.state === State.STRING_SINGLE ||
|
|
245
|
+
this.state === State.STRING_DOUBLE
|
|
246
|
+
) {
|
|
247
|
+
return this.matchString(pos, true);
|
|
248
|
+
} else if (this.state === State.INTERPOLATION) {
|
|
249
|
+
if (c === "}") {
|
|
250
|
+
input.next();
|
|
251
|
+
this.popState(); // pop interpolation
|
|
252
|
+
return this.createToken(TokenTypes.INTERPOLATION_END, pos);
|
|
253
|
+
}
|
|
254
|
+
return this.matchExpression(pos);
|
|
255
|
+
} else if (this.state === State.ELEMENT) {
|
|
256
|
+
switch (c) {
|
|
257
|
+
case "/":
|
|
258
|
+
input.next();
|
|
259
|
+
return this.createToken(TokenTypes.SLASH, pos);
|
|
260
|
+
case "{":
|
|
261
|
+
return this.matchExpressionToken(pos);
|
|
262
|
+
case ">":
|
|
263
|
+
input.next();
|
|
264
|
+
this.popState();
|
|
265
|
+
return this.createToken(TokenTypes.ELEMENT_END, pos);
|
|
266
|
+
case '"':
|
|
267
|
+
input.next();
|
|
268
|
+
this.pushState(State.ATTRIBUTE_VALUE);
|
|
269
|
+
return this.createToken(TokenTypes.STRING_START, pos);
|
|
270
|
+
case "=":
|
|
271
|
+
input.next();
|
|
272
|
+
return this.createToken(TokenTypes.ASSIGNMENT, pos);
|
|
273
|
+
default:
|
|
274
|
+
return this.matchSymbol(pos);
|
|
275
|
+
}
|
|
276
|
+
} else if (this.state === State.ATTRIBUTE_VALUE) {
|
|
277
|
+
if (c === '"') {
|
|
278
|
+
input.next();
|
|
279
|
+
this.popState();
|
|
280
|
+
return this.createToken(TokenTypes.STRING_END, pos);
|
|
281
|
+
}
|
|
282
|
+
return this.matchAttributeValue(pos);
|
|
283
|
+
} else if (this.state === State.DECLARATION) {
|
|
284
|
+
switch (c) {
|
|
285
|
+
case ">":
|
|
286
|
+
input.next();
|
|
287
|
+
this.popState();
|
|
288
|
+
return this.createToken(TokenTypes.ELEMENT_END, pos);
|
|
289
|
+
case '"':
|
|
290
|
+
input.next();
|
|
291
|
+
this.pushState(State.STRING_DOUBLE);
|
|
292
|
+
return this.createToken(TokenTypes.STRING_START, pos);
|
|
293
|
+
case "{":
|
|
294
|
+
return this.matchExpressionToken(pos);
|
|
295
|
+
default:
|
|
296
|
+
return this.matchSymbol(pos);
|
|
297
|
+
}
|
|
298
|
+
} else {
|
|
299
|
+
return this.error(`Invalid state ${this.state}`, pos);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return TokenTypes.EOF_TOKEN;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
matchExpressionToken(pos) {
|
|
306
|
+
const input = this.input;
|
|
307
|
+
switch (input.la(1)) {
|
|
308
|
+
case "{":
|
|
309
|
+
input.next();
|
|
310
|
+
input.next();
|
|
311
|
+
this.pushState(State.EXPRESSION);
|
|
312
|
+
if (input.la(0) === "-") {
|
|
313
|
+
input.next();
|
|
314
|
+
}
|
|
315
|
+
return this.createToken(TokenTypes.EXPRESSION_START, pos);
|
|
316
|
+
case "%":
|
|
317
|
+
input.next();
|
|
318
|
+
input.next();
|
|
319
|
+
this.pushState(State.TAG);
|
|
320
|
+
if (input.la(0) === "-") {
|
|
321
|
+
input.next();
|
|
322
|
+
}
|
|
323
|
+
return this.createToken(TokenTypes.TAG_START, pos);
|
|
324
|
+
case "#":
|
|
325
|
+
input.next();
|
|
326
|
+
input.next();
|
|
327
|
+
if (input.la(0) === "-") {
|
|
328
|
+
input.next();
|
|
329
|
+
}
|
|
330
|
+
return this.matchComment(pos);
|
|
331
|
+
default:
|
|
332
|
+
return this.matchText(pos);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
matchExpression(pos) {
|
|
337
|
+
const input = this.input;
|
|
338
|
+
const c = input.la(0);
|
|
339
|
+
switch (c) {
|
|
340
|
+
case "'":
|
|
341
|
+
this.pushState(State.STRING_SINGLE);
|
|
342
|
+
input.next();
|
|
343
|
+
return this.createToken(TokenTypes.STRING_START, pos);
|
|
344
|
+
case '"':
|
|
345
|
+
this.pushState(State.STRING_DOUBLE);
|
|
346
|
+
input.next();
|
|
347
|
+
return this.createToken(TokenTypes.STRING_START, pos);
|
|
348
|
+
default: {
|
|
349
|
+
if (isDigit(input.lac(0))) {
|
|
350
|
+
input.next();
|
|
351
|
+
return this.matchNumber(pos);
|
|
352
|
+
}
|
|
353
|
+
if (
|
|
354
|
+
(c === "t" && input.match("true")) ||
|
|
355
|
+
(c === "T" && input.match("TRUE"))
|
|
356
|
+
) {
|
|
357
|
+
return this.createToken(TokenTypes.TRUE, pos);
|
|
358
|
+
}
|
|
359
|
+
if (
|
|
360
|
+
(c === "f" && input.match("false")) ||
|
|
361
|
+
(c === "F" && input.match("FALSE"))
|
|
362
|
+
) {
|
|
363
|
+
return this.createToken(TokenTypes.FALSE, pos);
|
|
364
|
+
}
|
|
365
|
+
if (
|
|
366
|
+
(c === "n" &&
|
|
367
|
+
(input.match("null") || input.match("none"))) ||
|
|
368
|
+
(c === "N" && (input.match("NULL") || input.match("NONE")))
|
|
369
|
+
) {
|
|
370
|
+
return this.createToken(TokenTypes.NULL, pos);
|
|
371
|
+
}
|
|
372
|
+
const { longestMatchingOperator, longestMatchEndPos } =
|
|
373
|
+
this.findLongestMatchingOperator();
|
|
374
|
+
const cc = input.lac(0);
|
|
375
|
+
if (cc === 95 /* _ */ || isAlpha(cc) || isDigit(cc)) {
|
|
376
|
+
// okay... this could be either a symbol or an operator
|
|
377
|
+
input.next();
|
|
378
|
+
const sym = this.matchSymbol(pos);
|
|
379
|
+
if (sym.text.length <= longestMatchingOperator.length) {
|
|
380
|
+
// the operator was longer so let's use that
|
|
381
|
+
input.rewind(longestMatchEndPos);
|
|
382
|
+
return this.createToken(TokenTypes.OPERATOR, pos);
|
|
383
|
+
}
|
|
384
|
+
// found a symbol
|
|
385
|
+
return sym;
|
|
386
|
+
} else if (longestMatchingOperator) {
|
|
387
|
+
input.rewind(longestMatchEndPos);
|
|
388
|
+
return this.createToken(TokenTypes.OPERATOR, pos);
|
|
389
|
+
} else if (
|
|
390
|
+
Object.prototype.hasOwnProperty.call(CHAR_TO_TOKEN, c)
|
|
391
|
+
) {
|
|
392
|
+
input.next();
|
|
393
|
+
return this.createToken(CHAR_TO_TOKEN[c], pos);
|
|
394
|
+
} else if (c === "\xa0") {
|
|
395
|
+
return this.error(
|
|
396
|
+
"Unsupported token: Non-breaking space",
|
|
397
|
+
pos
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
return this.error(`Unknown token ${c}`, pos);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
findLongestMatchingOperator() {
|
|
406
|
+
const input = this.input;
|
|
407
|
+
const start = input.mark();
|
|
408
|
+
let longestMatchingOperator = "";
|
|
409
|
+
let longestMatchEndPos = null;
|
|
410
|
+
for (let i = 0, ops = this[OPERATORS], len = ops.length; i < len; i++) {
|
|
411
|
+
const op = ops[i];
|
|
412
|
+
if (op.length > longestMatchingOperator.length && input.match(op)) {
|
|
413
|
+
const cc = input.lac(0);
|
|
414
|
+
|
|
415
|
+
// prevent mixing up operators with symbols (e.g. matching
|
|
416
|
+
// 'not in' in 'not invalid').
|
|
417
|
+
if (op.indexOf(" ") === -1 || !(isAlpha(cc) || isDigit(cc))) {
|
|
418
|
+
longestMatchingOperator = op;
|
|
419
|
+
longestMatchEndPos = input.mark();
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
input.rewind(start);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
input.rewind(start);
|
|
426
|
+
return { longestMatchingOperator, longestMatchEndPos };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
error(message, pos, advice = "") {
|
|
430
|
+
const errorToken = this.createToken(TokenTypes.ERROR, pos);
|
|
431
|
+
errorToken.message = message;
|
|
432
|
+
errorToken.advice = advice;
|
|
433
|
+
return errorToken;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
matchEntity(pos) {
|
|
437
|
+
const input = this.input;
|
|
438
|
+
input.next(); // &
|
|
439
|
+
if (input.la(0) === "#") {
|
|
440
|
+
input.next(); // #
|
|
441
|
+
if (input.la(0) === "x") {
|
|
442
|
+
// hexadecimal numeric character reference
|
|
443
|
+
input.next(); // x
|
|
444
|
+
let c = input.la(0);
|
|
445
|
+
while (
|
|
446
|
+
("a" <= c && c <= "f") ||
|
|
447
|
+
("A" <= c && c <= "F") ||
|
|
448
|
+
isDigit(input.lac(0))
|
|
449
|
+
) {
|
|
450
|
+
input.next();
|
|
451
|
+
c = input.la(0);
|
|
452
|
+
}
|
|
453
|
+
if (input.la(0) === ";") {
|
|
454
|
+
input.next();
|
|
455
|
+
} else {
|
|
456
|
+
input.rewind(pos);
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
} else if (isDigit(input.lac(0))) {
|
|
460
|
+
// decimal numeric character reference
|
|
461
|
+
// consume decimal numbers
|
|
462
|
+
do {
|
|
463
|
+
input.next();
|
|
464
|
+
} while (isDigit(input.lac(0)));
|
|
465
|
+
// check for final ";"
|
|
466
|
+
if (input.la(0) === ";") {
|
|
467
|
+
input.next();
|
|
468
|
+
} else {
|
|
469
|
+
input.rewind(pos);
|
|
470
|
+
return null;
|
|
471
|
+
}
|
|
472
|
+
} else {
|
|
473
|
+
input.rewind(pos);
|
|
474
|
+
return null;
|
|
475
|
+
}
|
|
476
|
+
} else {
|
|
477
|
+
// match named character reference
|
|
478
|
+
while (isAlpha(input.lac(0))) {
|
|
479
|
+
input.next();
|
|
480
|
+
}
|
|
481
|
+
if (input.la(0) === ";") {
|
|
482
|
+
input.next();
|
|
483
|
+
} else {
|
|
484
|
+
input.rewind(pos);
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return this.createToken(TokenTypes.ENTITY, pos);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
matchSymbol(pos) {
|
|
492
|
+
const input = this.input;
|
|
493
|
+
const inElement = this.state === State.ELEMENT;
|
|
494
|
+
let c;
|
|
495
|
+
while (
|
|
496
|
+
(c = input.lac(0)) &&
|
|
497
|
+
(c === 95 ||
|
|
498
|
+
isAlpha(c) ||
|
|
499
|
+
isDigit(c) ||
|
|
500
|
+
(inElement && isValidAttributeName(c)))
|
|
501
|
+
) {
|
|
502
|
+
input.next();
|
|
503
|
+
}
|
|
504
|
+
const end = input.mark();
|
|
505
|
+
if (pos.index === end.index) {
|
|
506
|
+
return this.error(
|
|
507
|
+
"Expected an Identifier",
|
|
508
|
+
pos,
|
|
509
|
+
inElement
|
|
510
|
+
? `Expected a valid attribute name, but instead found "${input.la(
|
|
511
|
+
0
|
|
512
|
+
)}", which is not part of a valid attribute name.`
|
|
513
|
+
: `Expected letter, digit or underscore but found ${input.la(
|
|
514
|
+
0
|
|
515
|
+
)} instead.`
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
return this.createToken(TokenTypes.SYMBOL, pos);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
matchString(pos, allowInterpolation = true) {
|
|
522
|
+
const input = this.input;
|
|
523
|
+
const start = this.state === State.STRING_SINGLE ? "'" : '"';
|
|
524
|
+
let c;
|
|
525
|
+
// string starts with an interpolation
|
|
526
|
+
if (allowInterpolation && input.la(0) === "#" && input.la(1) === "{") {
|
|
527
|
+
this.pushState(State.INTERPOLATION);
|
|
528
|
+
input.next();
|
|
529
|
+
input.next();
|
|
530
|
+
return this.createToken(TokenTypes.INTERPOLATION_START, pos);
|
|
531
|
+
}
|
|
532
|
+
if (input.la(0) === start) {
|
|
533
|
+
input.next();
|
|
534
|
+
this.popState();
|
|
535
|
+
return this.createToken(TokenTypes.STRING_END, pos);
|
|
536
|
+
}
|
|
537
|
+
while ((c = input.la(0)) !== start && c !== EOF) {
|
|
538
|
+
if (c === "\\" && input.la(1) === start) {
|
|
539
|
+
// escape sequence for string start
|
|
540
|
+
input.next();
|
|
541
|
+
input.next();
|
|
542
|
+
} else if (allowInterpolation && c === "#" && input.la(1) === "{") {
|
|
543
|
+
// found interpolation start, string part matched
|
|
544
|
+
// next iteration will match the interpolation
|
|
545
|
+
break;
|
|
546
|
+
} else {
|
|
547
|
+
input.next();
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
const result = this.createToken(TokenTypes.STRING, pos);
|
|
551
|
+
// Replace double backslash before escaped quotes
|
|
552
|
+
if (!this.options.preserveSourceLiterally) {
|
|
553
|
+
result.text = result.text.replace(
|
|
554
|
+
new RegExp("(?:\\\\)(" + start + ")", "g"),
|
|
555
|
+
"$1"
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
return result;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
matchAttributeValue(pos) {
|
|
562
|
+
const input = this.input;
|
|
563
|
+
const start = this.state === State.STRING_SINGLE ? "'" : '"';
|
|
564
|
+
let c = input.la(0);
|
|
565
|
+
const c2 = input.la(1);
|
|
566
|
+
if (c === "{" && (c2 === "{" || c2 === "#" || c2 === "%")) {
|
|
567
|
+
return this.matchExpressionToken(pos);
|
|
568
|
+
}
|
|
569
|
+
while ((c = input.la(0)) !== start && c !== EOF) {
|
|
570
|
+
if (c === "\\" && input.la(1) === start) {
|
|
571
|
+
input.next();
|
|
572
|
+
input.next();
|
|
573
|
+
} else if (c === start) {
|
|
574
|
+
break;
|
|
575
|
+
} else {
|
|
576
|
+
input.next();
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
const result = this.createToken(TokenTypes.STRING, pos);
|
|
580
|
+
// Replace double backslash before escaped quotes
|
|
581
|
+
if (!this.options.preserveSourceLiterally) {
|
|
582
|
+
result.text = result.text.replace(
|
|
583
|
+
new RegExp("(?:\\\\)(" + start + ")", "g"),
|
|
584
|
+
"$1"
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
return result;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
matchNumber(pos) {
|
|
591
|
+
const input = this.input;
|
|
592
|
+
let c;
|
|
593
|
+
while ((c = input.lac(0)) !== EOF) {
|
|
594
|
+
if (!isDigit(c)) {
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
input.next();
|
|
598
|
+
}
|
|
599
|
+
if (input.la(0) === "." && isDigit(input.lac(1))) {
|
|
600
|
+
input.next();
|
|
601
|
+
while ((c = input.lac(0)) !== EOF) {
|
|
602
|
+
if (!isDigit(c)) {
|
|
603
|
+
break;
|
|
604
|
+
}
|
|
605
|
+
input.next();
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
return this.createToken(TokenTypes.NUMBER, pos);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
matchText(pos) {
|
|
612
|
+
const input = this.input;
|
|
613
|
+
let c;
|
|
614
|
+
while ((c = input.la(0)) && c !== EOF) {
|
|
615
|
+
if (c === "{") {
|
|
616
|
+
const c2 = input.la(1);
|
|
617
|
+
if (c2 === "{" || c2 === "#" || c2 === "%") {
|
|
618
|
+
break;
|
|
619
|
+
}
|
|
620
|
+
} else if (c === "<") {
|
|
621
|
+
const nextChar = input.la(1);
|
|
622
|
+
if (
|
|
623
|
+
nextChar === "/" || // closing tag
|
|
624
|
+
nextChar === "!" || // HTML comment
|
|
625
|
+
isAlpha(input.lac(1)) // opening tag
|
|
626
|
+
) {
|
|
627
|
+
break;
|
|
628
|
+
} else if (input.la(1) === "{") {
|
|
629
|
+
const c2 = input.la(1);
|
|
630
|
+
if (c2 === "{" || c2 === "#" || c2 === "%") {
|
|
631
|
+
break;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
input.next();
|
|
636
|
+
}
|
|
637
|
+
return this.createToken(TokenTypes.TEXT, pos);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
matchComment(pos) {
|
|
641
|
+
const input = this.input;
|
|
642
|
+
let c;
|
|
643
|
+
while ((c = input.next()) !== EOF) {
|
|
644
|
+
if (c === "#" && input.la(0) === "}") {
|
|
645
|
+
input.next(); // consume '}'
|
|
646
|
+
break;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
return this.createToken(TokenTypes.COMMENT, pos);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
function isWhitespace(c) {
|
|
654
|
+
return c === "\n" || c === " " || c === "\t";
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function isAlpha(c) {
|
|
658
|
+
return (65 <= c && c <= 90) || (97 <= c && c <= 122);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
function isDigit(c) {
|
|
662
|
+
return c >= 48 && c <= 57;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
|
|
666
|
+
function isValidAttributeName(c) {
|
|
667
|
+
const is_c0_control = c >= 0 && c <= 31;
|
|
668
|
+
const is_control = is_c0_control || (c >= 127 && c <= 159);
|
|
669
|
+
const is_invalid =
|
|
670
|
+
c === 32 || c === 34 || c === 39 || c === 62 || c === 47 || c === 61;
|
|
671
|
+
const is_noncharacter =
|
|
672
|
+
(c >= 64976 && c <= 65007) ||
|
|
673
|
+
c === 65534 ||
|
|
674
|
+
c === 65535 ||
|
|
675
|
+
c === 131070 ||
|
|
676
|
+
c === 131071 ||
|
|
677
|
+
c === 196606 ||
|
|
678
|
+
c === 196607 ||
|
|
679
|
+
c === 262142 ||
|
|
680
|
+
c === 262143 ||
|
|
681
|
+
c === 327678 ||
|
|
682
|
+
c === 327679 ||
|
|
683
|
+
c === 393214 ||
|
|
684
|
+
c === 393215 ||
|
|
685
|
+
c === 458750 ||
|
|
686
|
+
c === 458751 ||
|
|
687
|
+
c === 524286 ||
|
|
688
|
+
c === 524287 ||
|
|
689
|
+
c === 589822 ||
|
|
690
|
+
c === 589823 ||
|
|
691
|
+
c === 655358 ||
|
|
692
|
+
c === 655359 ||
|
|
693
|
+
c === 720894 ||
|
|
694
|
+
c === 720895 ||
|
|
695
|
+
c === 786430 ||
|
|
696
|
+
c === 786431 ||
|
|
697
|
+
c === 851966 ||
|
|
698
|
+
c === 851967 ||
|
|
699
|
+
c === 917502 ||
|
|
700
|
+
c === 917503 ||
|
|
701
|
+
c === 983038 ||
|
|
702
|
+
c === 983039 ||
|
|
703
|
+
c === 1048574 ||
|
|
704
|
+
c === 1048575 ||
|
|
705
|
+
c === 1114106 ||
|
|
706
|
+
c === 1114107;
|
|
707
|
+
return !is_control && !is_invalid && !is_noncharacter;
|
|
708
|
+
}
|