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
'use strict';
var SyntaxReferenceError = require('./error').SyntaxReferenceError;
var MatchError = require('./error').MatchError;
var names = require('../utils/names');
var generic = require('./generic');
var parse = require('./grammar/parse');
var generate = require('./grammar/generate');
var walk = require('./grammar/walk');
var match = require('./match');
var trace = require('./trace');
var search = require('./search');
var getStructureFromConfig = require('./structure').getStructureFromConfig;
var cssWideKeywords = parse('inherit | initial | unset');
var cssWideKeywordsWithExpression = parse('inherit | initial | unset | <expression>');
function dumpMapSyntax(map, syntaxAsAst) {
var result = {};
for (var name in map) {
if (map[name].syntax) {
result[name] = syntaxAsAst ? map[name].syntax : generate(map[name].syntax);
}
}
return result;
}
function unwrapNode(item) {
return item && item.data;
}
function valueHasVar(value) {
var hasVar = false;
this.syntax.walk(value, function(node) {
if (node.type === 'Function' && node.name.toLowerCase() === 'var') {
hasVar = true;
}
});
return hasVar;
}
// check node is \0 or \9 hack
function isHack(node) {
return node.type === 'Identifier' && /^\\[09]/.test(node.name);
}
// white spaces, comments and some hacks can to be ignored at the end of value
function isNextMayToBeIgnored(cursor) {
while (cursor !== null) {
if (cursor.data.type !== 'WhiteSpace' &&
cursor.data.type !== 'Comment' &&
!isHack(cursor.data)) {
return false;
}
cursor = cursor.next;
}
return true;
}
function buildMatchResult(match, error) {
return {
matched: match,
error: error,
getTrace: trace.getTrace,
isType: trace.isType,
isProperty: trace.isProperty,
isKeyword: trace.isKeyword
};
}
function matchSyntax(lexer, syntax, node, useCommon) {
var result;
if (!node || !node.children) {
return buildMatchResult(null, new Error('Node has no children'));
}
if (valueHasVar.call(lexer, node)) {
return buildMatchResult(null, new Error('Matching for a tree with var() is not supported'));
}
if (useCommon) {
result = match(lexer, lexer.valueCommonSyntax, node.children.head);
}
if (!useCommon || !result.match) {
result = syntax.match(node.children.head);
if (!result.match) {
return buildMatchResult(null, new MatchError('Mismatch', lexer, syntax.syntax, node, result.badNode || unwrapNode(result.next) || node));
}
}
// enhance top-level match wrapper
if (result.match.type === 'ASTNode') {
result.match = {
syntax: {
type: syntax.type,
name: syntax.name
},
match: [result.match]
};
} else if (result.match.syntax.type === 'Group') {
result.match.syntax = {
type: syntax.type,
name: syntax.name
};
}
if (result.next && !isNextMayToBeIgnored(result.next)) {
return buildMatchResult(null, new MatchError('Uncomplete match', lexer, syntax.syntax, node, result.badNode || unwrapNode(result.next) || node));
}
return buildMatchResult(result.match, null);
}
var Lexer = function(config, syntax, structure) {
this.valueCommonSyntax = cssWideKeywords;
this.syntax = syntax;
this.generic = false;
this.properties = {};
this.types = {};
this.structure = structure || getStructureFromConfig(config);
if (config) {
if (config.generic) {
this.generic = true;
for (var name in generic) {
this.addType_(name, generic[name]);
}
}
if (config.types) {
for (var name in config.types) {
this.addType_(name, config.types[name]);
}
}
if (config.properties) {
for (var name in config.properties) {
this.addProperty_(name, config.properties[name]);
}
}
}
};
Lexer.prototype = {
structure: {},
checkStructure: function(ast) {
function collectWarning(node, message) {
warns.push({
node: node,
message: message
});
}
var structure = this.structure;
var warns = [];
this.syntax.walk(ast, function(node) {
if (structure.hasOwnProperty(node.type)) {
structure[node.type].check(node, collectWarning);
} else {
collectWarning(node, 'Unknown node type `' + node.type + '`');
}
});
return warns.length ? warns : false;
},
createDescriptor: function(syntax, type, name) {
var self = this;
var descriptor = {
type: type,
name: name,
syntax: null,
match: null
};
if (typeof syntax === 'function') {
// convert syntax to pseudo syntax node
// NOTE: that's not a part of match result tree
syntax = {
type: 'ASTNode',
match: syntax
};
descriptor.match = function(item) {
return match(self, syntax, item);
};
} else {
if (typeof syntax === 'string') {
// lazy parsing on first access
Object.defineProperty(descriptor, 'syntax', {
get: function() {
Object.defineProperty(descriptor, 'syntax', {
value: parse(syntax)
});
return descriptor.syntax;
}
});
} else {
descriptor.syntax = syntax;
}
descriptor.match = function(item) {
return match(self, descriptor.syntax, item);
};
}
return descriptor;
},
addProperty_: function(name, syntax) {
this.properties[name] = this.createDescriptor(syntax, 'Property', name);
},
addType_: function(name, syntax) {
this.types[name] = this.createDescriptor(syntax, 'Type', name);
if (syntax === generic.expression) {
this.valueCommonSyntax = cssWideKeywordsWithExpression;
}
},
matchDeclaration: function(node) {
if (node.type !== 'Declaration') {
return buildMatchResult(null, new Error('Not a Declaration node'));
}
return this.matchProperty(node.property, node.value);
},
matchProperty: function(propertyName, value) {
var property = names.property(propertyName);
// don't match syntax for a custom property
if (property.custom) {
return buildMatchResult(null, new Error('Lexer matching doesn\'t applicable for custom properties'));
}
var propertySyntax = property.vendor
? this.getProperty(property.name) || this.getProperty(property.basename)
: this.getProperty(property.name);
if (!propertySyntax) {
return buildMatchResult(null, new SyntaxReferenceError('Unknown property', propertyName));
}
return matchSyntax(this, propertySyntax, value, true);
},
matchType: function(typeName, value) {
var typeSyntax = this.getType(typeName);
if (!typeSyntax) {
return buildMatchResult(null, new SyntaxReferenceError('Unknown type', typeName));
}
return matchSyntax(this, typeSyntax, value, false);
},
match: function(syntax, value) {
if (!syntax || !syntax.type) {
return buildMatchResult(null, new SyntaxReferenceError('Bad syntax'));
}
if (!syntax.match) {
syntax = this.createDescriptor(syntax);
}
return matchSyntax(this, syntax, value, false);
},
findValueFragments: function(propertyName, value, type, name) {
return search.matchFragments(this, value, this.matchProperty(propertyName, value), type, name);
},
findDeclarationValueFragments: function(declaration, type, name) {
return search.matchFragments(this, declaration.value, this.matchDeclaration(declaration), type, name);
},
findAllFragments: function(ast, type, name) {
var result = [];
this.syntax.walk(ast, {
visit: 'Declaration',
enter: function(declaration) {
result.push.apply(result, this.findDeclarationValueFragments(declaration, type, name));
}.bind(this)
});
return result;
},
getProperty: function(name) {
return this.properties.hasOwnProperty(name) ? this.properties[name] : null;
},
getType: function(name) {
return this.types.hasOwnProperty(name) ? this.types[name] : null;
},
validate: function() {
function validate(syntax, name, broken, descriptor) {
if (broken.hasOwnProperty(name)) {
return broken[name];
}
broken[name] = false;
if (descriptor.syntax !== null) {
walk(descriptor.syntax, function(node) {
if (node.type !== 'Type' && node.type !== 'Property') {
return;
}
var map = node.type === 'Type' ? syntax.types : syntax.properties;
var brokenMap = node.type === 'Type' ? brokenTypes : brokenProperties;
if (!map.hasOwnProperty(node.name) || validate(syntax, node.name, brokenMap, map[node.name])) {
broken[name] = true;
}
}, this);
}
}
var brokenTypes = {};
var brokenProperties = {};
for (var key in this.types) {
validate(this, key, brokenTypes, this.types[key]);
}
for (var key in this.properties) {
validate(this, key, brokenProperties, this.properties[key]);
}
brokenTypes = Object.keys(brokenTypes).filter(function(name) {
return brokenTypes[name];
});
brokenProperties = Object.keys(brokenProperties).filter(function(name) {
return brokenProperties[name];
});
if (brokenTypes.length || brokenProperties.length) {
return {
types: brokenTypes,
properties: brokenProperties
};
}
return null;
},
dump: function(syntaxAsAst) {
return {
generic: this.generic,
types: dumpMapSyntax(this.types, syntaxAsAst),
properties: dumpMapSyntax(this.properties, syntaxAsAst)
};
},
toString: function() {
return JSON.stringify(this.dump());
}
};
module.exports = Lexer;