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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
'use strict';
var names = require('../utils/names');
var MULTIPLIER_DEFAULT = {
comma: false,
min: 1,
max: 1
};
function skipSpaces(node) {
while (node !== null && (node.data.type === 'WhiteSpace' || node.data.type === 'Comment')) {
node = node.next;
}
return node;
}
function putResult(buffer, match) {
var type = match.type || match.syntax.type;
// ignore groups
if (type === 'Group') {
buffer.push.apply(buffer, match.match);
} else {
buffer.push(match);
}
}
function matchToJSON() {
return {
type: this.syntax.type,
name: this.syntax.name,
match: this.match,
node: this.node
};
}
function buildMatchNode(badNode, lastNode, next, match) {
if (badNode) {
return {
badNode: badNode,
lastNode: null,
next: null,
match: null
};
}
return {
badNode: null,
lastNode: lastNode,
next: next,
match: match
};
}
function matchGroup(lexer, syntaxNode, node) {
var result = [];
var buffer;
var multiplier = syntaxNode.multiplier || MULTIPLIER_DEFAULT;
var min = multiplier.min;
var max = multiplier.max === 0 ? Infinity : multiplier.max;
var lastCommaTermCount;
var lastComma;
var matchCount = 0;
var lastNode = null;
var badNode = null;
mismatch:
while (matchCount < max) {
node = skipSpaces(node);
buffer = [];
switch (syntaxNode.combinator) {
case '|':
for (var i = 0; i < syntaxNode.terms.length; i++) {
var term = syntaxNode.terms[i];
var res = matchSyntax(lexer, term, node);
if (res.match) {
putResult(buffer, res.match);
node = res.next;
break; // continue matching
} else if (res.badNode) {
badNode = res.badNode;
break mismatch;
} else if (res.lastNode) {
lastNode = res.lastNode;
}
}
if (buffer.length === 0) {
break mismatch; // nothing found -> stop matching
}
break;
case ' ':
var beforeMatchNode = node;
var lastMatchedTerm = null;
var hasTailMatch = false;
var commaMissed = false;
for (var i = 0; i < syntaxNode.terms.length; i++) {
var term = syntaxNode.terms[i];
var res = matchSyntax(lexer, term, node);
if (res.match) {
if (term.type === 'Comma' && i !== 0 && !hasTailMatch) {
// recover cursor to state before last match and stop matching
lastNode = node && node.data;
node = beforeMatchNode;
break mismatch;
}
// non-empty match (res.next will refer to another node)
if (res.next !== node) {
// match should be preceded by a comma
if (commaMissed) {
lastNode = node && node.data;
node = beforeMatchNode;
break mismatch;
}
hasTailMatch = term.type !== 'Comma';
lastMatchedTerm = term;
}
putResult(buffer, res.match);
node = skipSpaces(res.next);
} else if (res.badNode) {
badNode = res.badNode;
break mismatch;
} else {
if (res.lastNode) {
lastNode = res.lastNode;
}
// it's ok when comma doesn't match when no matches yet
// but only if comma is not first or last term
if (term.type === 'Comma' && i !== 0 && i !== syntaxNode.terms.length - 1) {
if (hasTailMatch) {
commaMissed = true;
}
continue;
}
// recover cursor to state before last match and stop matching
lastNode = res.lastNode || (node && node.data);
node = beforeMatchNode;
break mismatch;
}
}
// don't allow empty match when [ ]!
if (!lastMatchedTerm && syntaxNode.disallowEmpty) {
// empty match but shouldn't
// recover cursor to state before last match and stop matching
lastNode = node && node.data;
node = beforeMatchNode;
break mismatch;
}
// don't allow comma at the end but only if last term isn't a comma
if (lastMatchedTerm && lastMatchedTerm.type === 'Comma' && term.type !== 'Comma') {
lastNode = node && node.data;
node = beforeMatchNode;
break mismatch;
}
break;
case '&&':
var beforeMatchNode = node;
var lastMatchedTerm = null;
var terms = syntaxNode.terms.slice();
while (terms.length) {
var wasMatch = false;
var emptyMatched = 0;
for (var i = 0; i < terms.length; i++) {
var term = terms[i];
var res = matchSyntax(lexer, term, node);
if (res.match) {
// non-empty match (res.next will refer to another node)
if (res.next !== node) {
lastMatchedTerm = term;
} else {
emptyMatched++;
continue;
}
wasMatch = true;
terms.splice(i--, 1);
putResult(buffer, res.match);
node = skipSpaces(res.next);
break;
} else if (res.badNode) {
badNode = res.badNode;
break mismatch;
} else if (res.lastNode) {
lastNode = res.lastNode;
}
}
if (!wasMatch) {
// terms left, but they all are optional
if (emptyMatched === terms.length) {
break;
}
// not ok
lastNode = node && node.data;
node = beforeMatchNode;
break mismatch;
}
}
if (!lastMatchedTerm && syntaxNode.disallowEmpty) { // don't allow empty match when [ ]!
// empty match but shouldn't
// recover cursor to state before last match and stop matching
lastNode = node && node.data;
node = beforeMatchNode;
break mismatch;
}
break;
case '||':
var beforeMatchNode = node;
var lastMatchedTerm = null;
var terms = syntaxNode.terms.slice();
while (terms.length) {
var wasMatch = false;
var emptyMatched = 0;
for (var i = 0; i < terms.length; i++) {
var term = terms[i];
var res = matchSyntax(lexer, term, node);
if (res.match) {
// non-empty match (res.next will refer to another node)
if (res.next !== node) {
lastMatchedTerm = term;
} else {
emptyMatched++;
continue;
}
wasMatch = true;
terms.splice(i--, 1);
putResult(buffer, res.match);
node = skipSpaces(res.next);
break;
} else if (res.badNode) {
badNode = res.badNode;
break mismatch;
} else if (res.lastNode) {
lastNode = res.lastNode;
}
}
if (!wasMatch) {
break;
}
}
// don't allow empty match
if (!lastMatchedTerm && (emptyMatched !== terms.length || syntaxNode.disallowEmpty)) {
// empty match but shouldn't
// recover cursor to state before last match and stop matching
lastNode = node && node.data;
node = beforeMatchNode;
break mismatch;
}
break;
}
// flush buffer
result.push.apply(result, buffer);
matchCount++;
if (!node) {
break;
}
if (multiplier.comma) {
if (lastComma && lastCommaTermCount === result.length) {
// nothing match after comma
break mismatch;
}
node = skipSpaces(node);
if (node !== null && node.data.type === 'Operator' && node.data.value === ',') {
result.push({
syntax: syntaxNode,
match: [{
type: 'ASTNode',
node: node.data,
childrenMatch: null
}]
});
lastCommaTermCount = result.length;
lastComma = node;
node = node.next;
} else {
lastNode = node !== null ? node.data : null;
break mismatch;
}
}
}
// console.log(syntaxNode.type, badNode, lastNode);
if (lastComma && lastCommaTermCount === result.length) {
// nothing match after comma
node = lastComma;
result.pop();
}
return buildMatchNode(badNode, lastNode, node, matchCount < min ? null : {
syntax: syntaxNode,
match: result,
toJSON: matchToJSON
});
}
function matchSyntax(lexer, syntaxNode, node) {
var badNode = null;
var lastNode = null;
var match = null;
switch (syntaxNode.type) {
case 'Group':
return matchGroup(lexer, syntaxNode, node);
case 'Function':
// expect a function node
if (!node || node.data.type !== 'Function') {
break;
}
var keyword = names.keyword(node.data.name);
var name = syntaxNode.name.toLowerCase();
// check function name with vendor consideration
if (name !== keyword.name) {
break;
}
var res = matchSyntax(lexer, syntaxNode.children, node.data.children.head);
if (!res.match || res.next) {
badNode = res.badNode || res.lastNode || (res.next ? res.next.data : null) || node.data;
break;
}
match = [{
type: 'ASTNode',
node: node.data,
childrenMatch: res.match.match
}];
// Use node.next instead of res.next here since syntax is matching
// for internal list and it should be completelly matched (res.next is null at this point).
// Therefore function is matched and we are going to next node
node = node.next;
break;
case 'Parentheses':
if (!node || node.data.type !== 'Parentheses') {
break;
}
var res = matchSyntax(lexer, syntaxNode.children, node.data.children.head);
if (!res.match || res.next) {
badNode = res.badNode || res.lastNode || (res.next ? res.next.data : null) || node.data; // TODO: case when res.next === null
break;
}
match = [{
type: 'ASTNode',
node: node.data,
childrenMatch: res.match.match
}];
node = res.next;
break;
case 'Type':
var typeSyntax = lexer.getType(syntaxNode.name);
if (!typeSyntax) {
throw new Error('Unknown syntax type `' + syntaxNode.name + '`');
}
var res = typeSyntax.match(node);
if (!res.match) {
badNode = res && res.badNode; // TODO: case when res.next === null
lastNode = (res && res.lastNode) || (node && node.data);
break;
}
node = res.next;
putResult(match = [], res.match);
if (match.length === 0) {
match = null;
}
break;
case 'Property':
var propertySyntax = lexer.getProperty(syntaxNode.name);
if (!propertySyntax) {
throw new Error('Unknown property `' + syntaxNode.name + '`');
}
var res = propertySyntax.match(node);
if (!res.match) {
badNode = res && res.badNode; // TODO: case when res.next === null
lastNode = (res && res.lastNode) || (node && node.data);
break;
}
node = res.next;
putResult(match = [], res.match);
if (match.length === 0) {
match = null;
}
break;
case 'Keyword':
if (!node) {
break;
}
if (node.data.type === 'Identifier') {
var keyword = names.keyword(node.data.name);
var keywordName = keyword.name;
var name = syntaxNode.name.toLowerCase();
// drop \0 and \9 hack from keyword name
if (keywordName.indexOf('\\') !== -1) {
keywordName = keywordName.replace(/\\[09].*$/, '');
}
if (name !== keywordName) {
break;
}
} else {
// keyword may to be a number (e.g. font-weight: 400 )
if (node.data.type !== 'Number' || node.data.value !== syntaxNode.name) {
break;
}
}
match = [{
type: 'ASTNode',
node: node.data,
childrenMatch: null
}];
node = node.next;
break;
case 'Slash':
case 'Comma':
if (!node || node.data.type !== 'Operator' || node.data.value !== syntaxNode.value) {
break;
}
match = [{
type: 'ASTNode',
node: node.data,
childrenMatch: null
}];
node = node.next;
break;
case 'String':
if (!node || node.data.type !== 'String') {
break;
}
match = [{
type: 'ASTNode',
node: node.data,
childrenMatch: null
}];
node = node.next;
break;
case 'ASTNode':
if (node && syntaxNode.match(node)) {
match = {
type: 'ASTNode',
node: node.data,
childrenMatch: null
};
node = node.next;
}
return buildMatchNode(badNode, lastNode, node, match);
default:
throw new Error('Not implemented yet node type: ' + syntaxNode.type);
}
return buildMatchNode(badNode, lastNode, node, match === null ? null : {
syntax: syntaxNode,
match: match,
toJSON: matchToJSON
});
};
module.exports = matchSyntax;