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 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
35×
1×
154×
1×
114×
114×
105×
9×
7×
1×
6×
9×
4×
6×
1×
7×
1×
6×
6×
1×
4×
4×
1×
1×
1×
3×
1×
6×
1×
112×
112×
112×
112×
63×
49×
49×
49×
49×
49×
35×
49×
1×
72×
69×
3×
3×
3×
1×
577×
577×
577×
4×
577×
386×
386×
191×
1×
600×
600×
252×
348×
348×
73×
275×
1×
277×
277×
1×
276×
1×
275×
1×
327×
327×
17×
310×
1×
327×
327×
9×
9×
318×
5×
5×
313×
3×
3×
310×
1×
170×
19×
151×
1×
778×
1×
333×
333×
333×
3×
330×
330×
1×
778×
778×
9×
9×
769×
1×
913×
1×
609×
609×
4×
2×
2×
605×
5×
1×
4×
1×
3×
1×
2×
600×
1×
328×
328×
328×
274×
274×
54×
41×
13×
1×
12×
1×
53×
1×
1×
52×
52×
7×
7×
45×
1×
641×
641×
97×
97×
97×
544×
9×
9×
535×
106×
106×
1×
769×
613×
156×
156×
156×
156×
8×
148×
148×
1×
127×
125×
2×
2×
2×
2×
2×
1×
10×
10×
10×
10×
1×
19×
2×
17×
17×
17×
15×
15×
2×
2×
1×
278×
278×
278×
278×
278×
35×
35×
35×
3×
3×
32×
278×
246×
246×
32×
15×
14×
9×
1×
8×
1×
14×
14×
17×
31×
31×
12×
12×
19×
1×
23×
23×
23×
23×
4×
4×
23×
21×
2×
2×
1×
74×
74×
3×
3×
3×
3×
3×
71×
1×
1×
1×
1×
70×
70×
43×
43×
70×
14×
56×
1×
207×
196×
11×
11×
11×
11×
11×
3×
11×
1×
14×
14×
14×
14×
14×
1×
604×
3×
3×
3×
3×
601×
1×
144×
144×
80×
64×
64×
64×
64×
64×
1×
1×
| var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
(function (factory) {
Eif (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
Iif (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./lessScanner", "./cssScanner", "./cssParser", "./cssNodes", "./cssErrors"], factory);
}
})(function (require, exports) {
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
var lessScanner = require("./lessScanner");
var cssScanner_1 = require("./cssScanner");
var cssParser = require("./cssParser");
var nodes = require("./cssNodes");
var cssErrors_1 = require("./cssErrors");
/// <summary>
/// A parser for LESS
/// http://lesscss.org/
/// </summary>
var LESSParser = (function (_super) {
__extends(LESSParser, _super);
function LESSParser() {
return _super.call(this, new lessScanner.LESSScanner()) || this;
}
LESSParser.prototype._parseStylesheetStatement = function () {
return this._tryParseMixinDeclaration()
|| this._tryParseMixinReference(true)
|| _super.prototype._parseStylesheetStatement.call(this)
|| this._parseVariableDeclaration();
};
LESSParser.prototype._parseImport = function () {
var node = this.create(nodes.Import);
if (!this.accept(cssScanner_1.TokenType.AtKeyword, '@import') && !this.accept(cssScanner_1.TokenType.AtKeyword, '@import-once') /* deprecated in less 1.4.1 */) {
return null;
}
// less 1.4.1: @import (css) "lib"
if (this.accept(cssScanner_1.TokenType.ParenthesisL)) {
if (!this.accept(cssScanner_1.TokenType.Ident)) {
return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [cssScanner_1.TokenType.SemiColon]);
}
do {
if (!this.accept(cssScanner_1.TokenType.Comma)) {
break;
}
} while (this.accept(cssScanner_1.TokenType.Ident));
if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [cssScanner_1.TokenType.SemiColon]);
}
}
if (!this.accept(cssScanner_1.TokenType.URI) && !this.accept(cssScanner_1.TokenType.String)) {
return this.finish(node, cssErrors_1.ParseError.URIOrStringExpected, [cssScanner_1.TokenType.SemiColon]);
}
node.setMedialist(this._parseMediaList());
return this.finish(node);
};
LESSParser.prototype._parseMediaQuery = function (resyncStopToken) {
var node = _super.prototype._parseMediaQuery.call(this, resyncStopToken);
if (!node) {
var node_1 = this.create(nodes.MediaQuery);
Eif (node_1.addChild(this._parseVariable())) {
return this.finish(node_1);
}
return null;
}
return node;
};
LESSParser.prototype._parseMediaDeclaration = function () {
return this._tryParseRuleset(false)
|| this._tryToParseDeclaration()
|| this._tryParseMixinDeclaration()
|| this._tryParseMixinReference()
|| this._parseDetachedRuleSetMixin()
|| this._parseStylesheetStatement();
};
LESSParser.prototype._parseVariableDeclaration = function (panic) {
Eif (panic === void 0) { panic = []; }
var node = this.create(nodes.VariableDeclaration);
var mark = this.mark();
if (!node.setVariable(this._parseVariable())) {
return null;
}
Eif (this.accept(cssScanner_1.TokenType.Colon, ':')) {
node.colonPosition = this.prevToken.offset;
Iif (!node.setValue(this._parseDetachedRuleSet() || this._parseExpr())) {
return this.finish(node, cssErrors_1.ParseError.VariableValueExpected, [], panic);
}
node.addChild(this._parsePrio());
}
else {
this.restoreAtMark(mark);
return null; // at keyword, but no ':', not a variable declaration but some at keyword
}
if (this.peek(cssScanner_1.TokenType.SemiColon)) {
node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
}
return this.finish(node);
};
LESSParser.prototype._parseDetachedRuleSet = function () {
if (!this.peek(cssScanner_1.TokenType.CurlyL)) {
return null;
}
var content = this.create(nodes.BodyDeclaration);
this._parseBody(content, this._parseRuleSetDeclaration.bind(this));
return this.finish(content);
};
LESSParser.prototype._parseVariable = function () {
var node = this.create(nodes.Variable);
var mark = this.mark();
while (this.accept(cssScanner_1.TokenType.Delim, '@')) {
Iif (this.hasWhitespace()) {
this.restoreAtMark(mark);
return null;
}
}
if (!this.accept(cssScanner_1.TokenType.AtKeyword)) {
this.restoreAtMark(mark);
return null;
}
return node;
};
LESSParser.prototype._parseTerm = function () {
var term = _super.prototype._parseTerm.call(this);
if (term) {
return term;
}
term = this.create(nodes.Term);
if (term.setExpression(this._parseVariable()) ||
term.setExpression(this._parseEscaped())) {
return this.finish(term);
}
return null;
};
LESSParser.prototype._parseEscaped = function () {
var node = this.createNode(nodes.NodeType.EscapedValue);
if (this.accept(cssScanner_1.TokenType.EscapedJavaScript) ||
this.accept(cssScanner_1.TokenType.BadEscapedJavaScript)) {
return this.finish(node);
}
if (this.accept(cssScanner_1.TokenType.Delim, '~')) {
return this.finish(node, this.accept(cssScanner_1.TokenType.String) ? null : cssErrors_1.ParseError.TermExpected);
}
return null;
};
LESSParser.prototype._parseOperator = function () {
var node = this._parseGuardOperator();
if (node) {
return node;
}
else {
return _super.prototype._parseOperator.call(this);
}
};
LESSParser.prototype._parseGuardOperator = function () {
var node = this.createNode(nodes.NodeType.Operator);
if (this.accept(cssScanner_1.TokenType.Delim, '>')) {
this.accept(cssScanner_1.TokenType.Delim, '=');
return node;
}
else if (this.accept(cssScanner_1.TokenType.Delim, '=')) {
this.accept(cssScanner_1.TokenType.Delim, '<');
return node;
}
else if (this.accept(cssScanner_1.TokenType.Delim, '<')) {
this.accept(cssScanner_1.TokenType.Delim, '=');
return node;
}
return null;
};
LESSParser.prototype._parseRuleSetDeclaration = function () {
if (this.peek(cssScanner_1.TokenType.AtKeyword)) {
return this._parseKeyframe()
|| this._parseMedia()
|| this._parseImport()
|| this._parseDetachedRuleSetMixin() // less detached ruleset mixin
|| this._parseVariableDeclaration(); // Variable declarations
}
return this._tryParseMixinDeclaration()
|| this._tryParseRuleset(true) // nested ruleset
|| this._tryParseMixinReference() // less mixin reference
|| this._parseExtend() // less extend declaration
|| _super.prototype._parseRuleSetDeclaration.call(this); // try css ruleset declaration as the last option
};
LESSParser.prototype._parseSimpleSelectorBody = function () {
return this._parseSelectorCombinator() || _super.prototype._parseSimpleSelectorBody.call(this);
};
LESSParser.prototype._parseSelector = function (isNested) {
// CSS Guards
var mark = this.mark();
var node = this.create(nodes.Selector);
if (node.addChild(this._parseSimpleSelector()) && node.addChild(this._parseGuard())) {
return this.finish(node);
}
this.restoreAtMark(mark);
return _super.prototype._parseSelector.call(this, isNested);
};
LESSParser.prototype._parseSelectorCombinator = function () {
var node = this.createNode(nodes.NodeType.SelectorCombinator);
if (this.accept(cssScanner_1.TokenType.Delim, '&')) {
while (!this.hasWhitespace() && (this.accept(cssScanner_1.TokenType.Delim, '-') || node.addChild(this._parseIdent()) || this.accept(cssScanner_1.TokenType.Delim, '&'))) {
// support &-foo
}
return this.finish(node);
}
return null;
};
LESSParser.prototype._parseSelectorIdent = function () {
return this._parseIdent() || this._parseSelectorInterpolation();
};
LESSParser.prototype._parseSelectorInterpolation = function () {
// Selector interpolation; old: ~"@{name}", new: @{name}
var node = this.createNode(nodes.NodeType.SelectorInterpolation);
if (this.accept(cssScanner_1.TokenType.Delim, '~')) {
if (!this.hasWhitespace() && (this.accept(cssScanner_1.TokenType.String) || this.accept(cssScanner_1.TokenType.BadString))) {
return this.finish(node);
}
return this.finish(node, cssErrors_1.ParseError.StringLiteralExpected);
}
else if (this.accept(cssScanner_1.TokenType.Delim, '@')) {
if (this.hasWhitespace() || !this.accept(cssScanner_1.TokenType.CurlyL)) {
return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected);
}
if (!node.addChild(this._parseIdent())) {
return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
}
if (!this.accept(cssScanner_1.TokenType.CurlyR)) {
return this.finish(node, cssErrors_1.ParseError.RightCurlyExpected);
}
return this.finish(node);
}
return null;
};
LESSParser.prototype._tryParseMixinDeclaration = function () {
var mark = this.mark();
var node = this.create(nodes.MixinDeclaration);
if (!node.setIdentifier(this._parseMixinDeclarationIdentifier()) || !this.accept(cssScanner_1.TokenType.ParenthesisL)) {
this.restoreAtMark(mark);
return null;
}
if (node.getParameters().addChild(this._parseMixinParameter())) {
while (this.accept(cssScanner_1.TokenType.Comma) || this.accept(cssScanner_1.TokenType.SemiColon)) {
if (this.peek(cssScanner_1.TokenType.ParenthesisR)) {
break;
}
if (!node.getParameters().addChild(this._parseMixinParameter())) {
return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
}
}
}
if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
this.restoreAtMark(mark);
return null;
}
node.setGuard(this._parseGuard());
if (!this.peek(cssScanner_1.TokenType.CurlyL)) {
this.restoreAtMark(mark);
return null;
}
return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
};
LESSParser.prototype._parseMixinDeclarationIdentifier = function () {
var identifier;
if (this.peek(cssScanner_1.TokenType.Delim, '#') || this.peek(cssScanner_1.TokenType.Delim, '.')) {
identifier = this.create(nodes.Identifier);
this.consumeToken(); // # or .
Iif (this.hasWhitespace() || !identifier.addChild(this._parseIdent())) {
return null;
}
}
else if (this.peek(cssScanner_1.TokenType.Hash)) {
identifier = this.create(nodes.Identifier);
this.consumeToken(); // TokenType.Hash
}
else {
return null;
}
identifier.referenceTypes = [nodes.ReferenceType.Mixin];
return this.finish(identifier);
};
LESSParser.prototype._parsePseudo = function () {
if (!this.peek(cssScanner_1.TokenType.Colon)) {
return null;
}
var mark = this.mark();
var node = this.create(nodes.ExtendsReference);
this.consumeToken(); // :
if (this.accept(cssScanner_1.TokenType.Ident, 'extend')) {
return this._completeExtends(node);
}
this.restoreAtMark(mark);
return _super.prototype._parsePseudo.call(this);
};
LESSParser.prototype._parseExtend = function () {
if (!this.peek(cssScanner_1.TokenType.Delim, '&')) {
return null;
}
var mark = this.mark();
var node = this.create(nodes.ExtendsReference);
this.consumeToken(); // &
Iif (this.hasWhitespace() || !this.accept(cssScanner_1.TokenType.Colon) || !this.accept(cssScanner_1.TokenType.Ident, 'extend')) {
this.restoreAtMark(mark);
return null;
}
return this._completeExtends(node);
};
LESSParser.prototype._completeExtends = function (node) {
Iif (!this.accept(cssScanner_1.TokenType.ParenthesisL)) {
return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected);
}
Iif (!node.setSelector(this._parseSelector(true))) {
return this.finish(node, cssErrors_1.ParseError.SelectorExpected);
}
Iif (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
}
return this.finish(node);
};
LESSParser.prototype._parseDetachedRuleSetMixin = function () {
if (!this.peek(cssScanner_1.TokenType.AtKeyword)) {
return null;
}
var mark = this.mark();
var node = this.create(nodes.MixinReference);
if (!node.addChild(this._parseVariable()) || !this.accept(cssScanner_1.TokenType.ParenthesisL)) {
this.restoreAtMark(mark);
return null;
}
Iif (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
}
return this.finish(node);
};
LESSParser.prototype._tryParseMixinReference = function (atRoot) {
if (atRoot === void 0) { atRoot = false; }
var mark = this.mark();
var node = this.create(nodes.MixinReference);
var identifier = this._parseMixinDeclarationIdentifier();
while (identifier) {
this.accept(cssScanner_1.TokenType.Delim, '>');
var nextId = this._parseMixinDeclarationIdentifier();
if (nextId) {
node.getNamespaces().addChild(identifier);
identifier = nextId;
}
else {
break;
}
}
if (!node.setIdentifier(identifier)) {
this.restoreAtMark(mark);
return null;
}
if (!this.hasWhitespace() && this.accept(cssScanner_1.TokenType.ParenthesisL)) {
if (node.getArguments().addChild(this._parseMixinArgument())) {
while (this.accept(cssScanner_1.TokenType.Comma) || this.accept(cssScanner_1.TokenType.SemiColon)) {
if (this.peek(cssScanner_1.TokenType.ParenthesisR)) {
break;
}
if (!node.getArguments().addChild(this._parseMixinArgument())) {
return this.finish(node, cssErrors_1.ParseError.ExpressionExpected);
}
}
}
Iif (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
}
identifier.referenceTypes = [nodes.ReferenceType.Mixin];
}
else {
identifier.referenceTypes = [nodes.ReferenceType.Mixin, nodes.ReferenceType.Rule];
}
node.addChild(this._parsePrio());
if (atRoot && !this.peek(cssScanner_1.TokenType.SemiColon)) {
this.restoreAtMark(mark);
return null;
}
return this.finish(node);
};
LESSParser.prototype._parseMixinArgument = function () {
// [variableName ':'] expression | variableName '...'
var node = this.create(nodes.FunctionArgument);
var pos = this.mark();
var argument = this._parseVariable();
if (argument) {
Iif (!this.accept(cssScanner_1.TokenType.Colon)) {
this.restoreAtMark(pos);
}
else {
node.setIdentifier(argument);
}
}
if (node.setValue(this._parseDetachedRuleSet() || this._parseExpr(true))) {
return this.finish(node);
}
this.restoreAtMark(pos);
return null;
};
LESSParser.prototype._parseMixinParameter = function () {
var node = this.create(nodes.FunctionParameter);
// special rest variable: @rest...
if (this.peek(cssScanner_1.TokenType.AtKeyword, '@rest')) {
var restNode = this.create(nodes.Node);
this.consumeToken();
Iif (!this.accept(lessScanner.Ellipsis)) {
return this.finish(node, cssErrors_1.ParseError.DotExpected, [], [cssScanner_1.TokenType.Comma, cssScanner_1.TokenType.ParenthesisR]);
}
node.setIdentifier(this.finish(restNode));
return this.finish(node);
}
// special let args: ...
if (this.peek(lessScanner.Ellipsis)) {
var varargsNode = this.create(nodes.Node);
this.consumeToken();
node.setIdentifier(this.finish(varargsNode));
return this.finish(node);
}
var hasContent = false;
// default variable declaration: @param: 12 or @name
if (node.setIdentifier(this._parseVariable())) {
this.accept(cssScanner_1.TokenType.Colon);
hasContent = true;
}
if (!node.setDefaultValue(this._parseExpr(true)) && !hasContent) {
return null;
}
return this.finish(node);
};
LESSParser.prototype._parseGuard = function () {
if (!this.peek(cssScanner_1.TokenType.Ident, 'when')) {
return null;
}
var node = this.create(nodes.LessGuard);
this.consumeToken(); // when
node.isNegated = this.accept(cssScanner_1.TokenType.Ident, 'not');
Iif (!node.getConditions().addChild(this._parseGuardCondition())) {
return this.finish(node, cssErrors_1.ParseError.ConditionExpected);
}
while (this.accept(cssScanner_1.TokenType.Ident, 'and') || this.accept(cssScanner_1.TokenType.Comma, ',')) {
Iif (!node.getConditions().addChild(this._parseGuardCondition())) {
return this.finish(node, cssErrors_1.ParseError.ConditionExpected);
}
}
return this.finish(node);
};
LESSParser.prototype._parseGuardCondition = function () {
var node = this.create(nodes.GuardCondition);
Iif (!this.accept(cssScanner_1.TokenType.ParenthesisL)) {
return null;
}
Iif (!node.addChild(this._parseExpr())) {
// empty (?)
}
Iif (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
}
return this.finish(node);
};
LESSParser.prototype._parseFunctionIdentifier = function () {
if (this.peek(cssScanner_1.TokenType.Delim, '%')) {
var node = this.create(nodes.Identifier);
node.referenceTypes = [nodes.ReferenceType.Function];
this.consumeToken();
return this.finish(node);
}
return _super.prototype._parseFunctionIdentifier.call(this);
};
LESSParser.prototype._parsePropertyIdentifier = function () {
var identifier = this._parseIdent();
if (!identifier) {
return null;
}
Eif (!this.hasWhitespace()) {
this.accept(cssScanner_1.TokenType.Delim, '+');
Eif (!this.hasWhitespace()) {
this.accept(cssScanner_1.TokenType.Ident, '_');
}
}
return this.finish(identifier);
};
return LESSParser;
}(cssParser.Parser));
exports.LESSParser = LESSParser;
});
//# sourceMappingURL=lessParser.js.map |