vscode-css-languageservice 5.4.2 → 6.0.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.
Files changed (84) hide show
  1. package/CHANGELOG.md +5 -1
  2. package/lib/esm/cssLanguageService.d.ts +37 -37
  3. package/lib/esm/cssLanguageService.js +72 -75
  4. package/lib/esm/cssLanguageTypes.d.ts +238 -238
  5. package/lib/esm/cssLanguageTypes.js +42 -42
  6. package/lib/esm/data/webCustomData.js +21965 -21965
  7. package/lib/esm/languageFacts/builtinData.js +142 -142
  8. package/lib/esm/languageFacts/colors.js +469 -472
  9. package/lib/esm/languageFacts/dataManager.js +88 -92
  10. package/lib/esm/languageFacts/dataProvider.js +73 -79
  11. package/lib/esm/languageFacts/entry.js +137 -138
  12. package/lib/esm/languageFacts/facts.js +8 -8
  13. package/lib/esm/parser/cssErrors.js +48 -50
  14. package/lib/esm/parser/cssNodes.js +1502 -2019
  15. package/lib/esm/parser/cssParser.js +1534 -1563
  16. package/lib/esm/parser/cssScanner.js +592 -599
  17. package/lib/esm/parser/cssSymbolScope.js +311 -341
  18. package/lib/esm/parser/lessParser.js +714 -740
  19. package/lib/esm/parser/lessScanner.js +57 -78
  20. package/lib/esm/parser/scssErrors.js +18 -20
  21. package/lib/esm/parser/scssParser.js +796 -818
  22. package/lib/esm/parser/scssScanner.js +95 -116
  23. package/lib/esm/services/cssCodeActions.js +77 -81
  24. package/lib/esm/services/cssCompletion.js +1054 -1149
  25. package/lib/esm/services/cssFolding.js +190 -193
  26. package/lib/esm/services/cssFormatter.js +136 -136
  27. package/lib/esm/services/cssHover.js +148 -151
  28. package/lib/esm/services/cssNavigation.js +378 -470
  29. package/lib/esm/services/cssSelectionRange.js +47 -47
  30. package/lib/esm/services/cssValidation.js +41 -44
  31. package/lib/esm/services/lessCompletion.js +378 -397
  32. package/lib/esm/services/lint.js +518 -532
  33. package/lib/esm/services/lintRules.js +76 -83
  34. package/lib/esm/services/lintUtil.js +196 -205
  35. package/lib/esm/services/pathCompletion.js +157 -231
  36. package/lib/esm/services/scssCompletion.js +354 -378
  37. package/lib/esm/services/scssNavigation.js +82 -154
  38. package/lib/esm/services/selectorPrinting.js +492 -536
  39. package/lib/esm/utils/arrays.js +40 -46
  40. package/lib/esm/utils/objects.js +11 -11
  41. package/lib/esm/utils/resources.js +11 -24
  42. package/lib/esm/utils/strings.js +102 -104
  43. package/lib/umd/cssLanguageService.d.ts +37 -37
  44. package/lib/umd/cssLanguageService.js +99 -102
  45. package/lib/umd/cssLanguageTypes.d.ts +238 -238
  46. package/lib/umd/cssLanguageTypes.js +89 -88
  47. package/lib/umd/data/webCustomData.js +21978 -21978
  48. package/lib/umd/languageFacts/builtinData.js +154 -154
  49. package/lib/umd/languageFacts/colors.js +492 -495
  50. package/lib/umd/languageFacts/dataManager.js +101 -104
  51. package/lib/umd/languageFacts/dataProvider.js +86 -91
  52. package/lib/umd/languageFacts/entry.js +152 -153
  53. package/lib/umd/languageFacts/facts.js +29 -29
  54. package/lib/umd/parser/cssErrors.js +61 -62
  55. package/lib/umd/parser/cssNodes.js +1587 -2034
  56. package/lib/umd/parser/cssParser.js +1547 -1575
  57. package/lib/umd/parser/cssScanner.js +606 -611
  58. package/lib/umd/parser/cssSymbolScope.js +328 -353
  59. package/lib/umd/parser/lessParser.js +727 -752
  60. package/lib/umd/parser/lessScanner.js +70 -90
  61. package/lib/umd/parser/scssErrors.js +31 -32
  62. package/lib/umd/parser/scssParser.js +809 -830
  63. package/lib/umd/parser/scssScanner.js +108 -128
  64. package/lib/umd/services/cssCodeActions.js +90 -93
  65. package/lib/umd/services/cssCompletion.js +1067 -1161
  66. package/lib/umd/services/cssFolding.js +203 -206
  67. package/lib/umd/services/cssFormatter.js +150 -150
  68. package/lib/umd/services/cssHover.js +161 -163
  69. package/lib/umd/services/cssNavigation.js +391 -482
  70. package/lib/umd/services/cssSelectionRange.js +60 -60
  71. package/lib/umd/services/cssValidation.js +54 -56
  72. package/lib/umd/services/lessCompletion.js +391 -409
  73. package/lib/umd/services/lint.js +531 -544
  74. package/lib/umd/services/lintRules.js +91 -95
  75. package/lib/umd/services/lintUtil.js +210 -218
  76. package/lib/umd/services/pathCompletion.js +171 -244
  77. package/lib/umd/services/scssCompletion.js +367 -390
  78. package/lib/umd/services/scssNavigation.js +95 -166
  79. package/lib/umd/services/selectorPrinting.js +510 -550
  80. package/lib/umd/utils/arrays.js +55 -61
  81. package/lib/umd/utils/objects.js +25 -25
  82. package/lib/umd/utils/resources.js +26 -39
  83. package/lib/umd/utils/strings.js +120 -122
  84. package/package.json +10 -10
@@ -1,740 +1,714 @@
1
- /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License. See License.txt in the project root for license information.
4
- *--------------------------------------------------------------------------------------------*/
5
- 'use strict';
6
- var __extends = (this && this.__extends) || (function () {
7
- var extendStatics = function (d, b) {
8
- extendStatics = Object.setPrototypeOf ||
9
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
10
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
11
- return extendStatics(d, b);
12
- };
13
- return function (d, b) {
14
- if (typeof b !== "function" && b !== null)
15
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
16
- extendStatics(d, b);
17
- function __() { this.constructor = d; }
18
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
19
- };
20
- })();
21
- import * as lessScanner from './lessScanner';
22
- import { TokenType } from './cssScanner';
23
- import * as cssParser from './cssParser';
24
- import * as nodes from './cssNodes';
25
- import { ParseError } from './cssErrors';
26
- /// <summary>
27
- /// A parser for LESS
28
- /// http://lesscss.org/
29
- /// </summary>
30
- var LESSParser = /** @class */ (function (_super) {
31
- __extends(LESSParser, _super);
32
- function LESSParser() {
33
- return _super.call(this, new lessScanner.LESSScanner()) || this;
34
- }
35
- LESSParser.prototype._parseStylesheetStatement = function (isNested) {
36
- if (isNested === void 0) { isNested = false; }
37
- if (this.peek(TokenType.AtKeyword)) {
38
- return this._parseVariableDeclaration()
39
- || this._parsePlugin()
40
- || _super.prototype._parseStylesheetAtStatement.call(this, isNested);
41
- }
42
- return this._tryParseMixinDeclaration()
43
- || this._tryParseMixinReference()
44
- || this._parseFunction()
45
- || this._parseRuleset(true);
46
- };
47
- LESSParser.prototype._parseImport = function () {
48
- if (!this.peekKeyword('@import') && !this.peekKeyword('@import-once') /* deprecated in less 1.4.1 */) {
49
- return null;
50
- }
51
- var node = this.create(nodes.Import);
52
- this.consumeToken();
53
- // less 1.4.1: @import (css) "lib"
54
- if (this.accept(TokenType.ParenthesisL)) {
55
- if (!this.accept(TokenType.Ident)) {
56
- return this.finish(node, ParseError.IdentifierExpected, [TokenType.SemiColon]);
57
- }
58
- do {
59
- if (!this.accept(TokenType.Comma)) {
60
- break;
61
- }
62
- } while (this.accept(TokenType.Ident));
63
- if (!this.accept(TokenType.ParenthesisR)) {
64
- return this.finish(node, ParseError.RightParenthesisExpected, [TokenType.SemiColon]);
65
- }
66
- }
67
- if (!node.addChild(this._parseURILiteral()) && !node.addChild(this._parseStringLiteral())) {
68
- return this.finish(node, ParseError.URIOrStringExpected, [TokenType.SemiColon]);
69
- }
70
- if (!this.peek(TokenType.SemiColon) && !this.peek(TokenType.EOF)) {
71
- node.setMedialist(this._parseMediaQueryList());
72
- }
73
- return this.finish(node);
74
- };
75
- LESSParser.prototype._parsePlugin = function () {
76
- if (!this.peekKeyword('@plugin')) {
77
- return null;
78
- }
79
- var node = this.createNode(nodes.NodeType.Plugin);
80
- this.consumeToken(); // @import
81
- if (!node.addChild(this._parseStringLiteral())) {
82
- return this.finish(node, ParseError.StringLiteralExpected);
83
- }
84
- if (!this.accept(TokenType.SemiColon)) {
85
- return this.finish(node, ParseError.SemiColonExpected);
86
- }
87
- return this.finish(node);
88
- };
89
- LESSParser.prototype._parseMediaQuery = function () {
90
- var node = _super.prototype._parseMediaQuery.call(this);
91
- if (!node) {
92
- var node_1 = this.create(nodes.MediaQuery);
93
- if (node_1.addChild(this._parseVariable())) {
94
- return this.finish(node_1);
95
- }
96
- return null;
97
- }
98
- return node;
99
- };
100
- LESSParser.prototype._parseMediaDeclaration = function (isNested) {
101
- if (isNested === void 0) { isNested = false; }
102
- return this._tryParseRuleset(isNested)
103
- || this._tryToParseDeclaration()
104
- || this._tryParseMixinDeclaration()
105
- || this._tryParseMixinReference()
106
- || this._parseDetachedRuleSetMixin()
107
- || this._parseStylesheetStatement(isNested);
108
- };
109
- LESSParser.prototype._parseMediaFeatureName = function () {
110
- return this._parseIdent() || this._parseVariable();
111
- };
112
- LESSParser.prototype._parseVariableDeclaration = function (panic) {
113
- if (panic === void 0) { panic = []; }
114
- var node = this.create(nodes.VariableDeclaration);
115
- var mark = this.mark();
116
- if (!node.setVariable(this._parseVariable(true))) {
117
- return null;
118
- }
119
- if (this.accept(TokenType.Colon)) {
120
- if (this.prevToken) {
121
- node.colonPosition = this.prevToken.offset;
122
- }
123
- if (node.setValue(this._parseDetachedRuleSet())) {
124
- node.needsSemicolon = false;
125
- }
126
- else if (!node.setValue(this._parseExpr())) {
127
- return this.finish(node, ParseError.VariableValueExpected, [], panic);
128
- }
129
- node.addChild(this._parsePrio());
130
- }
131
- else {
132
- this.restoreAtMark(mark);
133
- return null; // at keyword, but no ':', not a variable declaration but some at keyword
134
- }
135
- if (this.peek(TokenType.SemiColon)) {
136
- node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
137
- }
138
- return this.finish(node);
139
- };
140
- LESSParser.prototype._parseDetachedRuleSet = function () {
141
- var mark = this.mark();
142
- // "Anonymous mixin" used in each() and possibly a generic type in the future
143
- if (this.peekDelim('#') || this.peekDelim('.')) {
144
- this.consumeToken();
145
- if (!this.hasWhitespace() && this.accept(TokenType.ParenthesisL)) {
146
- var node = this.create(nodes.MixinDeclaration);
147
- if (node.getParameters().addChild(this._parseMixinParameter())) {
148
- while (this.accept(TokenType.Comma) || this.accept(TokenType.SemiColon)) {
149
- if (this.peek(TokenType.ParenthesisR)) {
150
- break;
151
- }
152
- if (!node.getParameters().addChild(this._parseMixinParameter())) {
153
- this.markError(node, ParseError.IdentifierExpected, [], [TokenType.ParenthesisR]);
154
- }
155
- }
156
- }
157
- if (!this.accept(TokenType.ParenthesisR)) {
158
- this.restoreAtMark(mark);
159
- return null;
160
- }
161
- }
162
- else {
163
- this.restoreAtMark(mark);
164
- return null;
165
- }
166
- }
167
- if (!this.peek(TokenType.CurlyL)) {
168
- return null;
169
- }
170
- var content = this.create(nodes.BodyDeclaration);
171
- this._parseBody(content, this._parseDetachedRuleSetBody.bind(this));
172
- return this.finish(content);
173
- };
174
- LESSParser.prototype._parseDetachedRuleSetBody = function () {
175
- return this._tryParseKeyframeSelector() || this._parseRuleSetDeclaration();
176
- };
177
- LESSParser.prototype._addLookupChildren = function (node) {
178
- if (!node.addChild(this._parseLookupValue())) {
179
- return false;
180
- }
181
- var expectsValue = false;
182
- while (true) {
183
- if (this.peek(TokenType.BracketL)) {
184
- expectsValue = true;
185
- }
186
- if (!node.addChild(this._parseLookupValue())) {
187
- break;
188
- }
189
- expectsValue = false;
190
- }
191
- return !expectsValue;
192
- };
193
- LESSParser.prototype._parseLookupValue = function () {
194
- var node = this.create(nodes.Node);
195
- var mark = this.mark();
196
- if (!this.accept(TokenType.BracketL)) {
197
- this.restoreAtMark(mark);
198
- return null;
199
- }
200
- if (((node.addChild(this._parseVariable(false, true)) ||
201
- node.addChild(this._parsePropertyIdentifier())) &&
202
- this.accept(TokenType.BracketR)) || this.accept(TokenType.BracketR)) {
203
- return node;
204
- }
205
- this.restoreAtMark(mark);
206
- return null;
207
- };
208
- LESSParser.prototype._parseVariable = function (declaration, insideLookup) {
209
- if (declaration === void 0) { declaration = false; }
210
- if (insideLookup === void 0) { insideLookup = false; }
211
- var isPropertyReference = !declaration && this.peekDelim('$');
212
- if (!this.peekDelim('@') && !isPropertyReference && !this.peek(TokenType.AtKeyword)) {
213
- return null;
214
- }
215
- var node = this.create(nodes.Variable);
216
- var mark = this.mark();
217
- while (this.acceptDelim('@') || (!declaration && this.acceptDelim('$'))) {
218
- if (this.hasWhitespace()) {
219
- this.restoreAtMark(mark);
220
- return null;
221
- }
222
- }
223
- if (!this.accept(TokenType.AtKeyword) && !this.accept(TokenType.Ident)) {
224
- this.restoreAtMark(mark);
225
- return null;
226
- }
227
- if (!insideLookup && this.peek(TokenType.BracketL)) {
228
- if (!this._addLookupChildren(node)) {
229
- this.restoreAtMark(mark);
230
- return null;
231
- }
232
- }
233
- return node;
234
- };
235
- LESSParser.prototype._parseTermExpression = function () {
236
- return this._parseVariable() ||
237
- this._parseEscaped() ||
238
- _super.prototype._parseTermExpression.call(this) || // preference for colors before mixin references
239
- this._tryParseMixinReference(false);
240
- };
241
- LESSParser.prototype._parseEscaped = function () {
242
- if (this.peek(TokenType.EscapedJavaScript) ||
243
- this.peek(TokenType.BadEscapedJavaScript)) {
244
- var node = this.createNode(nodes.NodeType.EscapedValue);
245
- this.consumeToken();
246
- return this.finish(node);
247
- }
248
- if (this.peekDelim('~')) {
249
- var node = this.createNode(nodes.NodeType.EscapedValue);
250
- this.consumeToken();
251
- if (this.accept(TokenType.String) || this.accept(TokenType.EscapedJavaScript)) {
252
- return this.finish(node);
253
- }
254
- else {
255
- return this.finish(node, ParseError.TermExpected);
256
- }
257
- }
258
- return null;
259
- };
260
- LESSParser.prototype._parseOperator = function () {
261
- var node = this._parseGuardOperator();
262
- if (node) {
263
- return node;
264
- }
265
- else {
266
- return _super.prototype._parseOperator.call(this);
267
- }
268
- };
269
- LESSParser.prototype._parseGuardOperator = function () {
270
- if (this.peekDelim('>')) {
271
- var node = this.createNode(nodes.NodeType.Operator);
272
- this.consumeToken();
273
- this.acceptDelim('=');
274
- return node;
275
- }
276
- else if (this.peekDelim('=')) {
277
- var node = this.createNode(nodes.NodeType.Operator);
278
- this.consumeToken();
279
- this.acceptDelim('<');
280
- return node;
281
- }
282
- else if (this.peekDelim('<')) {
283
- var node = this.createNode(nodes.NodeType.Operator);
284
- this.consumeToken();
285
- this.acceptDelim('=');
286
- return node;
287
- }
288
- return null;
289
- };
290
- LESSParser.prototype._parseRuleSetDeclaration = function () {
291
- if (this.peek(TokenType.AtKeyword)) {
292
- return this._parseKeyframe()
293
- || this._parseMedia(true)
294
- || this._parseImport()
295
- || this._parseSupports(true) // @supports
296
- || this._parseDetachedRuleSetMixin() // less detached ruleset mixin
297
- || this._parseVariableDeclaration() // Variable declarations
298
- || _super.prototype._parseRuleSetDeclarationAtStatement.call(this);
299
- }
300
- return this._tryParseMixinDeclaration()
301
- || this._tryParseRuleset(true) // nested ruleset
302
- || this._tryParseMixinReference() // less mixin reference
303
- || this._parseFunction()
304
- || this._parseExtend() // less extend declaration
305
- || _super.prototype._parseRuleSetDeclaration.call(this); // try css ruleset declaration as the last option
306
- };
307
- LESSParser.prototype._parseKeyframeIdent = function () {
308
- return this._parseIdent([nodes.ReferenceType.Keyframe]) || this._parseVariable();
309
- };
310
- LESSParser.prototype._parseKeyframeSelector = function () {
311
- return this._parseDetachedRuleSetMixin() // less detached ruleset mixin
312
- || _super.prototype._parseKeyframeSelector.call(this);
313
- };
314
- LESSParser.prototype._parseSimpleSelectorBody = function () {
315
- return this._parseSelectorCombinator() || _super.prototype._parseSimpleSelectorBody.call(this);
316
- };
317
- LESSParser.prototype._parseSelector = function (isNested) {
318
- // CSS Guards
319
- var node = this.create(nodes.Selector);
320
- var hasContent = false;
321
- if (isNested) {
322
- // nested selectors can start with a combinator
323
- hasContent = node.addChild(this._parseCombinator());
324
- }
325
- while (node.addChild(this._parseSimpleSelector())) {
326
- hasContent = true;
327
- var mark = this.mark();
328
- if (node.addChild(this._parseGuard()) && this.peek(TokenType.CurlyL)) {
329
- break;
330
- }
331
- this.restoreAtMark(mark);
332
- node.addChild(this._parseCombinator()); // optional
333
- }
334
- return hasContent ? this.finish(node) : null;
335
- };
336
- LESSParser.prototype._parseSelectorCombinator = function () {
337
- if (this.peekDelim('&')) {
338
- var node = this.createNode(nodes.NodeType.SelectorCombinator);
339
- this.consumeToken();
340
- while (!this.hasWhitespace() && (this.acceptDelim('-') || this.accept(TokenType.Num) || this.accept(TokenType.Dimension) || node.addChild(this._parseIdent()) || this.acceptDelim('&'))) {
341
- // support &-foo
342
- }
343
- return this.finish(node);
344
- }
345
- return null;
346
- };
347
- LESSParser.prototype._parseSelectorIdent = function () {
348
- if (!this.peekInterpolatedIdent()) {
349
- return null;
350
- }
351
- var node = this.createNode(nodes.NodeType.SelectorInterpolation);
352
- var hasContent = this._acceptInterpolatedIdent(node);
353
- return hasContent ? this.finish(node) : null;
354
- };
355
- LESSParser.prototype._parsePropertyIdentifier = function (inLookup) {
356
- if (inLookup === void 0) { inLookup = false; }
357
- var propertyRegex = /^[\w-]+/;
358
- if (!this.peekInterpolatedIdent() && !this.peekRegExp(this.token.type, propertyRegex)) {
359
- return null;
360
- }
361
- var mark = this.mark();
362
- var node = this.create(nodes.Identifier);
363
- node.isCustomProperty = this.acceptDelim('-') && this.acceptDelim('-');
364
- var childAdded = false;
365
- if (!inLookup) {
366
- if (node.isCustomProperty) {
367
- childAdded = this._acceptInterpolatedIdent(node);
368
- }
369
- else {
370
- childAdded = this._acceptInterpolatedIdent(node, propertyRegex);
371
- }
372
- }
373
- else {
374
- if (node.isCustomProperty) {
375
- childAdded = node.addChild(this._parseIdent());
376
- }
377
- else {
378
- childAdded = node.addChild(this._parseRegexp(propertyRegex));
379
- }
380
- }
381
- if (!childAdded) {
382
- this.restoreAtMark(mark);
383
- return null;
384
- }
385
- if (!inLookup && !this.hasWhitespace()) {
386
- this.acceptDelim('+');
387
- if (!this.hasWhitespace()) {
388
- this.acceptIdent('_');
389
- }
390
- }
391
- return this.finish(node);
392
- };
393
- LESSParser.prototype.peekInterpolatedIdent = function () {
394
- return this.peek(TokenType.Ident) ||
395
- this.peekDelim('@') ||
396
- this.peekDelim('$') ||
397
- this.peekDelim('-');
398
- };
399
- LESSParser.prototype._acceptInterpolatedIdent = function (node, identRegex) {
400
- var _this = this;
401
- var hasContent = false;
402
- var indentInterpolation = function () {
403
- var pos = _this.mark();
404
- if (_this.acceptDelim('-')) {
405
- if (!_this.hasWhitespace()) {
406
- _this.acceptDelim('-');
407
- }
408
- if (_this.hasWhitespace()) {
409
- _this.restoreAtMark(pos);
410
- return null;
411
- }
412
- }
413
- return _this._parseInterpolation();
414
- };
415
- var accept = identRegex ?
416
- function () { return _this.acceptRegexp(identRegex); } :
417
- function () { return _this.accept(TokenType.Ident); };
418
- while (accept() ||
419
- node.addChild(this._parseInterpolation() ||
420
- this.try(indentInterpolation))) {
421
- hasContent = true;
422
- if (this.hasWhitespace()) {
423
- break;
424
- }
425
- }
426
- return hasContent;
427
- };
428
- LESSParser.prototype._parseInterpolation = function () {
429
- // @{name} Variable or
430
- // ${name} Property
431
- var mark = this.mark();
432
- if (this.peekDelim('@') || this.peekDelim('$')) {
433
- var node = this.createNode(nodes.NodeType.Interpolation);
434
- this.consumeToken();
435
- if (this.hasWhitespace() || !this.accept(TokenType.CurlyL)) {
436
- this.restoreAtMark(mark);
437
- return null;
438
- }
439
- if (!node.addChild(this._parseIdent())) {
440
- return this.finish(node, ParseError.IdentifierExpected);
441
- }
442
- if (!this.accept(TokenType.CurlyR)) {
443
- return this.finish(node, ParseError.RightCurlyExpected);
444
- }
445
- return this.finish(node);
446
- }
447
- return null;
448
- };
449
- LESSParser.prototype._tryParseMixinDeclaration = function () {
450
- var mark = this.mark();
451
- var node = this.create(nodes.MixinDeclaration);
452
- if (!node.setIdentifier(this._parseMixinDeclarationIdentifier()) || !this.accept(TokenType.ParenthesisL)) {
453
- this.restoreAtMark(mark);
454
- return null;
455
- }
456
- if (node.getParameters().addChild(this._parseMixinParameter())) {
457
- while (this.accept(TokenType.Comma) || this.accept(TokenType.SemiColon)) {
458
- if (this.peek(TokenType.ParenthesisR)) {
459
- break;
460
- }
461
- if (!node.getParameters().addChild(this._parseMixinParameter())) {
462
- this.markError(node, ParseError.IdentifierExpected, [], [TokenType.ParenthesisR]);
463
- }
464
- }
465
- }
466
- if (!this.accept(TokenType.ParenthesisR)) {
467
- this.restoreAtMark(mark);
468
- return null;
469
- }
470
- node.setGuard(this._parseGuard());
471
- if (!this.peek(TokenType.CurlyL)) {
472
- this.restoreAtMark(mark);
473
- return null;
474
- }
475
- return this._parseBody(node, this._parseMixInBodyDeclaration.bind(this));
476
- };
477
- LESSParser.prototype._parseMixInBodyDeclaration = function () {
478
- return this._parseFontFace() || this._parseRuleSetDeclaration();
479
- };
480
- LESSParser.prototype._parseMixinDeclarationIdentifier = function () {
481
- var identifier;
482
- if (this.peekDelim('#') || this.peekDelim('.')) {
483
- identifier = this.create(nodes.Identifier);
484
- this.consumeToken(); // # or .
485
- if (this.hasWhitespace() || !identifier.addChild(this._parseIdent())) {
486
- return null;
487
- }
488
- }
489
- else if (this.peek(TokenType.Hash)) {
490
- identifier = this.create(nodes.Identifier);
491
- this.consumeToken(); // TokenType.Hash
492
- }
493
- else {
494
- return null;
495
- }
496
- identifier.referenceTypes = [nodes.ReferenceType.Mixin];
497
- return this.finish(identifier);
498
- };
499
- LESSParser.prototype._parsePseudo = function () {
500
- if (!this.peek(TokenType.Colon)) {
501
- return null;
502
- }
503
- var mark = this.mark();
504
- var node = this.create(nodes.ExtendsReference);
505
- this.consumeToken(); // :
506
- if (this.acceptIdent('extend')) {
507
- return this._completeExtends(node);
508
- }
509
- this.restoreAtMark(mark);
510
- return _super.prototype._parsePseudo.call(this);
511
- };
512
- LESSParser.prototype._parseExtend = function () {
513
- if (!this.peekDelim('&')) {
514
- return null;
515
- }
516
- var mark = this.mark();
517
- var node = this.create(nodes.ExtendsReference);
518
- this.consumeToken(); // &
519
- if (this.hasWhitespace() || !this.accept(TokenType.Colon) || !this.acceptIdent('extend')) {
520
- this.restoreAtMark(mark);
521
- return null;
522
- }
523
- return this._completeExtends(node);
524
- };
525
- LESSParser.prototype._completeExtends = function (node) {
526
- if (!this.accept(TokenType.ParenthesisL)) {
527
- return this.finish(node, ParseError.LeftParenthesisExpected);
528
- }
529
- var selectors = node.getSelectors();
530
- if (!selectors.addChild(this._parseSelector(true))) {
531
- return this.finish(node, ParseError.SelectorExpected);
532
- }
533
- while (this.accept(TokenType.Comma)) {
534
- if (!selectors.addChild(this._parseSelector(true))) {
535
- return this.finish(node, ParseError.SelectorExpected);
536
- }
537
- }
538
- if (!this.accept(TokenType.ParenthesisR)) {
539
- return this.finish(node, ParseError.RightParenthesisExpected);
540
- }
541
- return this.finish(node);
542
- };
543
- LESSParser.prototype._parseDetachedRuleSetMixin = function () {
544
- if (!this.peek(TokenType.AtKeyword)) {
545
- return null;
546
- }
547
- var mark = this.mark();
548
- var node = this.create(nodes.MixinReference);
549
- if (node.addChild(this._parseVariable(true)) && (this.hasWhitespace() || !this.accept(TokenType.ParenthesisL))) {
550
- this.restoreAtMark(mark);
551
- return null;
552
- }
553
- if (!this.accept(TokenType.ParenthesisR)) {
554
- return this.finish(node, ParseError.RightParenthesisExpected);
555
- }
556
- return this.finish(node);
557
- };
558
- LESSParser.prototype._tryParseMixinReference = function (atRoot) {
559
- if (atRoot === void 0) { atRoot = true; }
560
- var mark = this.mark();
561
- var node = this.create(nodes.MixinReference);
562
- var identifier = this._parseMixinDeclarationIdentifier();
563
- while (identifier) {
564
- this.acceptDelim('>');
565
- var nextId = this._parseMixinDeclarationIdentifier();
566
- if (nextId) {
567
- node.getNamespaces().addChild(identifier);
568
- identifier = nextId;
569
- }
570
- else {
571
- break;
572
- }
573
- }
574
- if (!node.setIdentifier(identifier)) {
575
- this.restoreAtMark(mark);
576
- return null;
577
- }
578
- var hasArguments = false;
579
- if (this.accept(TokenType.ParenthesisL)) {
580
- hasArguments = true;
581
- if (node.getArguments().addChild(this._parseMixinArgument())) {
582
- while (this.accept(TokenType.Comma) || this.accept(TokenType.SemiColon)) {
583
- if (this.peek(TokenType.ParenthesisR)) {
584
- break;
585
- }
586
- if (!node.getArguments().addChild(this._parseMixinArgument())) {
587
- return this.finish(node, ParseError.ExpressionExpected);
588
- }
589
- }
590
- }
591
- if (!this.accept(TokenType.ParenthesisR)) {
592
- return this.finish(node, ParseError.RightParenthesisExpected);
593
- }
594
- identifier.referenceTypes = [nodes.ReferenceType.Mixin];
595
- }
596
- else {
597
- identifier.referenceTypes = [nodes.ReferenceType.Mixin, nodes.ReferenceType.Rule];
598
- }
599
- if (this.peek(TokenType.BracketL)) {
600
- if (!atRoot) {
601
- this._addLookupChildren(node);
602
- }
603
- }
604
- else {
605
- node.addChild(this._parsePrio());
606
- }
607
- if (!hasArguments && !this.peek(TokenType.SemiColon) && !this.peek(TokenType.CurlyR) && !this.peek(TokenType.EOF)) {
608
- this.restoreAtMark(mark);
609
- return null;
610
- }
611
- return this.finish(node);
612
- };
613
- LESSParser.prototype._parseMixinArgument = function () {
614
- // [variableName ':'] expression | variableName '...'
615
- var node = this.create(nodes.FunctionArgument);
616
- var pos = this.mark();
617
- var argument = this._parseVariable();
618
- if (argument) {
619
- if (!this.accept(TokenType.Colon)) {
620
- this.restoreAtMark(pos);
621
- }
622
- else {
623
- node.setIdentifier(argument);
624
- }
625
- }
626
- if (node.setValue(this._parseDetachedRuleSet() || this._parseExpr(true))) {
627
- return this.finish(node);
628
- }
629
- this.restoreAtMark(pos);
630
- return null;
631
- };
632
- LESSParser.prototype._parseMixinParameter = function () {
633
- var node = this.create(nodes.FunctionParameter);
634
- // special rest variable: @rest...
635
- if (this.peekKeyword('@rest')) {
636
- var restNode = this.create(nodes.Node);
637
- this.consumeToken();
638
- if (!this.accept(lessScanner.Ellipsis)) {
639
- return this.finish(node, ParseError.DotExpected, [], [TokenType.Comma, TokenType.ParenthesisR]);
640
- }
641
- node.setIdentifier(this.finish(restNode));
642
- return this.finish(node);
643
- }
644
- // special const args: ...
645
- if (this.peek(lessScanner.Ellipsis)) {
646
- var varargsNode = this.create(nodes.Node);
647
- this.consumeToken();
648
- node.setIdentifier(this.finish(varargsNode));
649
- return this.finish(node);
650
- }
651
- var hasContent = false;
652
- // default variable declaration: @param: 12 or @name
653
- if (node.setIdentifier(this._parseVariable())) {
654
- this.accept(TokenType.Colon);
655
- hasContent = true;
656
- }
657
- if (!node.setDefaultValue(this._parseDetachedRuleSet() || this._parseExpr(true)) && !hasContent) {
658
- return null;
659
- }
660
- return this.finish(node);
661
- };
662
- LESSParser.prototype._parseGuard = function () {
663
- if (!this.peekIdent('when')) {
664
- return null;
665
- }
666
- var node = this.create(nodes.LessGuard);
667
- this.consumeToken(); // when
668
- node.isNegated = this.acceptIdent('not');
669
- if (!node.getConditions().addChild(this._parseGuardCondition())) {
670
- return this.finish(node, ParseError.ConditionExpected);
671
- }
672
- while (this.acceptIdent('and') || this.accept(TokenType.Comma)) {
673
- if (!node.getConditions().addChild(this._parseGuardCondition())) {
674
- return this.finish(node, ParseError.ConditionExpected);
675
- }
676
- }
677
- return this.finish(node);
678
- };
679
- LESSParser.prototype._parseGuardCondition = function () {
680
- if (!this.peek(TokenType.ParenthesisL)) {
681
- return null;
682
- }
683
- var node = this.create(nodes.GuardCondition);
684
- this.consumeToken(); // ParenthesisL
685
- if (!node.addChild(this._parseExpr())) {
686
- // empty (?)
687
- }
688
- if (!this.accept(TokenType.ParenthesisR)) {
689
- return this.finish(node, ParseError.RightParenthesisExpected);
690
- }
691
- return this.finish(node);
692
- };
693
- LESSParser.prototype._parseFunction = function () {
694
- var pos = this.mark();
695
- var node = this.create(nodes.Function);
696
- if (!node.setIdentifier(this._parseFunctionIdentifier())) {
697
- return null;
698
- }
699
- if (this.hasWhitespace() || !this.accept(TokenType.ParenthesisL)) {
700
- this.restoreAtMark(pos);
701
- return null;
702
- }
703
- if (node.getArguments().addChild(this._parseMixinArgument())) {
704
- while (this.accept(TokenType.Comma) || this.accept(TokenType.SemiColon)) {
705
- if (this.peek(TokenType.ParenthesisR)) {
706
- break;
707
- }
708
- if (!node.getArguments().addChild(this._parseMixinArgument())) {
709
- return this.finish(node, ParseError.ExpressionExpected);
710
- }
711
- }
712
- }
713
- if (!this.accept(TokenType.ParenthesisR)) {
714
- return this.finish(node, ParseError.RightParenthesisExpected);
715
- }
716
- return this.finish(node);
717
- };
718
- LESSParser.prototype._parseFunctionIdentifier = function () {
719
- if (this.peekDelim('%')) {
720
- var node = this.create(nodes.Identifier);
721
- node.referenceTypes = [nodes.ReferenceType.Function];
722
- this.consumeToken();
723
- return this.finish(node);
724
- }
725
- return _super.prototype._parseFunctionIdentifier.call(this);
726
- };
727
- LESSParser.prototype._parseURLArgument = function () {
728
- var pos = this.mark();
729
- var node = _super.prototype._parseURLArgument.call(this);
730
- if (!node || !this.peek(TokenType.ParenthesisR)) {
731
- this.restoreAtMark(pos);
732
- var node_2 = this.create(nodes.Node);
733
- node_2.addChild(this._parseBinaryExpr());
734
- return this.finish(node_2);
735
- }
736
- return node;
737
- };
738
- return LESSParser;
739
- }(cssParser.Parser));
740
- export { LESSParser };
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ 'use strict';
6
+ import * as lessScanner from './lessScanner';
7
+ import { TokenType } from './cssScanner';
8
+ import * as cssParser from './cssParser';
9
+ import * as nodes from './cssNodes';
10
+ import { ParseError } from './cssErrors';
11
+ /// <summary>
12
+ /// A parser for LESS
13
+ /// http://lesscss.org/
14
+ /// </summary>
15
+ export class LESSParser extends cssParser.Parser {
16
+ constructor() {
17
+ super(new lessScanner.LESSScanner());
18
+ }
19
+ _parseStylesheetStatement(isNested = false) {
20
+ if (this.peek(TokenType.AtKeyword)) {
21
+ return this._parseVariableDeclaration()
22
+ || this._parsePlugin()
23
+ || super._parseStylesheetAtStatement(isNested);
24
+ }
25
+ return this._tryParseMixinDeclaration()
26
+ || this._tryParseMixinReference()
27
+ || this._parseFunction()
28
+ || this._parseRuleset(true);
29
+ }
30
+ _parseImport() {
31
+ if (!this.peekKeyword('@import') && !this.peekKeyword('@import-once') /* deprecated in less 1.4.1 */) {
32
+ return null;
33
+ }
34
+ const node = this.create(nodes.Import);
35
+ this.consumeToken();
36
+ // less 1.4.1: @import (css) "lib"
37
+ if (this.accept(TokenType.ParenthesisL)) {
38
+ if (!this.accept(TokenType.Ident)) {
39
+ return this.finish(node, ParseError.IdentifierExpected, [TokenType.SemiColon]);
40
+ }
41
+ do {
42
+ if (!this.accept(TokenType.Comma)) {
43
+ break;
44
+ }
45
+ } while (this.accept(TokenType.Ident));
46
+ if (!this.accept(TokenType.ParenthesisR)) {
47
+ return this.finish(node, ParseError.RightParenthesisExpected, [TokenType.SemiColon]);
48
+ }
49
+ }
50
+ if (!node.addChild(this._parseURILiteral()) && !node.addChild(this._parseStringLiteral())) {
51
+ return this.finish(node, ParseError.URIOrStringExpected, [TokenType.SemiColon]);
52
+ }
53
+ if (!this.peek(TokenType.SemiColon) && !this.peek(TokenType.EOF)) {
54
+ node.setMedialist(this._parseMediaQueryList());
55
+ }
56
+ return this.finish(node);
57
+ }
58
+ _parsePlugin() {
59
+ if (!this.peekKeyword('@plugin')) {
60
+ return null;
61
+ }
62
+ const node = this.createNode(nodes.NodeType.Plugin);
63
+ this.consumeToken(); // @import
64
+ if (!node.addChild(this._parseStringLiteral())) {
65
+ return this.finish(node, ParseError.StringLiteralExpected);
66
+ }
67
+ if (!this.accept(TokenType.SemiColon)) {
68
+ return this.finish(node, ParseError.SemiColonExpected);
69
+ }
70
+ return this.finish(node);
71
+ }
72
+ _parseMediaQuery() {
73
+ const node = super._parseMediaQuery();
74
+ if (!node) {
75
+ const node = this.create(nodes.MediaQuery);
76
+ if (node.addChild(this._parseVariable())) {
77
+ return this.finish(node);
78
+ }
79
+ return null;
80
+ }
81
+ return node;
82
+ }
83
+ _parseMediaDeclaration(isNested = false) {
84
+ return this._tryParseRuleset(isNested)
85
+ || this._tryToParseDeclaration()
86
+ || this._tryParseMixinDeclaration()
87
+ || this._tryParseMixinReference()
88
+ || this._parseDetachedRuleSetMixin()
89
+ || this._parseStylesheetStatement(isNested);
90
+ }
91
+ _parseMediaFeatureName() {
92
+ return this._parseIdent() || this._parseVariable();
93
+ }
94
+ _parseVariableDeclaration(panic = []) {
95
+ const node = this.create(nodes.VariableDeclaration);
96
+ const mark = this.mark();
97
+ if (!node.setVariable(this._parseVariable(true))) {
98
+ return null;
99
+ }
100
+ if (this.accept(TokenType.Colon)) {
101
+ if (this.prevToken) {
102
+ node.colonPosition = this.prevToken.offset;
103
+ }
104
+ if (node.setValue(this._parseDetachedRuleSet())) {
105
+ node.needsSemicolon = false;
106
+ }
107
+ else if (!node.setValue(this._parseExpr())) {
108
+ return this.finish(node, ParseError.VariableValueExpected, [], panic);
109
+ }
110
+ node.addChild(this._parsePrio());
111
+ }
112
+ else {
113
+ this.restoreAtMark(mark);
114
+ return null; // at keyword, but no ':', not a variable declaration but some at keyword
115
+ }
116
+ if (this.peek(TokenType.SemiColon)) {
117
+ node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
118
+ }
119
+ return this.finish(node);
120
+ }
121
+ _parseDetachedRuleSet() {
122
+ let mark = this.mark();
123
+ // "Anonymous mixin" used in each() and possibly a generic type in the future
124
+ if (this.peekDelim('#') || this.peekDelim('.')) {
125
+ this.consumeToken();
126
+ if (!this.hasWhitespace() && this.accept(TokenType.ParenthesisL)) {
127
+ let node = this.create(nodes.MixinDeclaration);
128
+ if (node.getParameters().addChild(this._parseMixinParameter())) {
129
+ while (this.accept(TokenType.Comma) || this.accept(TokenType.SemiColon)) {
130
+ if (this.peek(TokenType.ParenthesisR)) {
131
+ break;
132
+ }
133
+ if (!node.getParameters().addChild(this._parseMixinParameter())) {
134
+ this.markError(node, ParseError.IdentifierExpected, [], [TokenType.ParenthesisR]);
135
+ }
136
+ }
137
+ }
138
+ if (!this.accept(TokenType.ParenthesisR)) {
139
+ this.restoreAtMark(mark);
140
+ return null;
141
+ }
142
+ }
143
+ else {
144
+ this.restoreAtMark(mark);
145
+ return null;
146
+ }
147
+ }
148
+ if (!this.peek(TokenType.CurlyL)) {
149
+ return null;
150
+ }
151
+ const content = this.create(nodes.BodyDeclaration);
152
+ this._parseBody(content, this._parseDetachedRuleSetBody.bind(this));
153
+ return this.finish(content);
154
+ }
155
+ _parseDetachedRuleSetBody() {
156
+ return this._tryParseKeyframeSelector() || this._parseRuleSetDeclaration();
157
+ }
158
+ _addLookupChildren(node) {
159
+ if (!node.addChild(this._parseLookupValue())) {
160
+ return false;
161
+ }
162
+ let expectsValue = false;
163
+ while (true) {
164
+ if (this.peek(TokenType.BracketL)) {
165
+ expectsValue = true;
166
+ }
167
+ if (!node.addChild(this._parseLookupValue())) {
168
+ break;
169
+ }
170
+ expectsValue = false;
171
+ }
172
+ return !expectsValue;
173
+ }
174
+ _parseLookupValue() {
175
+ const node = this.create(nodes.Node);
176
+ const mark = this.mark();
177
+ if (!this.accept(TokenType.BracketL)) {
178
+ this.restoreAtMark(mark);
179
+ return null;
180
+ }
181
+ if (((node.addChild(this._parseVariable(false, true)) ||
182
+ node.addChild(this._parsePropertyIdentifier())) &&
183
+ this.accept(TokenType.BracketR)) || this.accept(TokenType.BracketR)) {
184
+ return node;
185
+ }
186
+ this.restoreAtMark(mark);
187
+ return null;
188
+ }
189
+ _parseVariable(declaration = false, insideLookup = false) {
190
+ const isPropertyReference = !declaration && this.peekDelim('$');
191
+ if (!this.peekDelim('@') && !isPropertyReference && !this.peek(TokenType.AtKeyword)) {
192
+ return null;
193
+ }
194
+ const node = this.create(nodes.Variable);
195
+ const mark = this.mark();
196
+ while (this.acceptDelim('@') || (!declaration && this.acceptDelim('$'))) {
197
+ if (this.hasWhitespace()) {
198
+ this.restoreAtMark(mark);
199
+ return null;
200
+ }
201
+ }
202
+ if (!this.accept(TokenType.AtKeyword) && !this.accept(TokenType.Ident)) {
203
+ this.restoreAtMark(mark);
204
+ return null;
205
+ }
206
+ if (!insideLookup && this.peek(TokenType.BracketL)) {
207
+ if (!this._addLookupChildren(node)) {
208
+ this.restoreAtMark(mark);
209
+ return null;
210
+ }
211
+ }
212
+ return node;
213
+ }
214
+ _parseTermExpression() {
215
+ return this._parseVariable() ||
216
+ this._parseEscaped() ||
217
+ super._parseTermExpression() || // preference for colors before mixin references
218
+ this._tryParseMixinReference(false);
219
+ }
220
+ _parseEscaped() {
221
+ if (this.peek(TokenType.EscapedJavaScript) ||
222
+ this.peek(TokenType.BadEscapedJavaScript)) {
223
+ const node = this.createNode(nodes.NodeType.EscapedValue);
224
+ this.consumeToken();
225
+ return this.finish(node);
226
+ }
227
+ if (this.peekDelim('~')) {
228
+ const node = this.createNode(nodes.NodeType.EscapedValue);
229
+ this.consumeToken();
230
+ if (this.accept(TokenType.String) || this.accept(TokenType.EscapedJavaScript)) {
231
+ return this.finish(node);
232
+ }
233
+ else {
234
+ return this.finish(node, ParseError.TermExpected);
235
+ }
236
+ }
237
+ return null;
238
+ }
239
+ _parseOperator() {
240
+ const node = this._parseGuardOperator();
241
+ if (node) {
242
+ return node;
243
+ }
244
+ else {
245
+ return super._parseOperator();
246
+ }
247
+ }
248
+ _parseGuardOperator() {
249
+ if (this.peekDelim('>')) {
250
+ const node = this.createNode(nodes.NodeType.Operator);
251
+ this.consumeToken();
252
+ this.acceptDelim('=');
253
+ return node;
254
+ }
255
+ else if (this.peekDelim('=')) {
256
+ const node = this.createNode(nodes.NodeType.Operator);
257
+ this.consumeToken();
258
+ this.acceptDelim('<');
259
+ return node;
260
+ }
261
+ else if (this.peekDelim('<')) {
262
+ const node = this.createNode(nodes.NodeType.Operator);
263
+ this.consumeToken();
264
+ this.acceptDelim('=');
265
+ return node;
266
+ }
267
+ return null;
268
+ }
269
+ _parseRuleSetDeclaration() {
270
+ if (this.peek(TokenType.AtKeyword)) {
271
+ return this._parseKeyframe()
272
+ || this._parseMedia(true)
273
+ || this._parseImport()
274
+ || this._parseSupports(true) // @supports
275
+ || this._parseDetachedRuleSetMixin() // less detached ruleset mixin
276
+ || this._parseVariableDeclaration() // Variable declarations
277
+ || super._parseRuleSetDeclarationAtStatement();
278
+ }
279
+ return this._tryParseMixinDeclaration()
280
+ || this._tryParseRuleset(true) // nested ruleset
281
+ || this._tryParseMixinReference() // less mixin reference
282
+ || this._parseFunction()
283
+ || this._parseExtend() // less extend declaration
284
+ || super._parseRuleSetDeclaration(); // try css ruleset declaration as the last option
285
+ }
286
+ _parseKeyframeIdent() {
287
+ return this._parseIdent([nodes.ReferenceType.Keyframe]) || this._parseVariable();
288
+ }
289
+ _parseKeyframeSelector() {
290
+ return this._parseDetachedRuleSetMixin() // less detached ruleset mixin
291
+ || super._parseKeyframeSelector();
292
+ }
293
+ _parseSimpleSelectorBody() {
294
+ return this._parseSelectorCombinator() || super._parseSimpleSelectorBody();
295
+ }
296
+ _parseSelector(isNested) {
297
+ // CSS Guards
298
+ const node = this.create(nodes.Selector);
299
+ let hasContent = false;
300
+ if (isNested) {
301
+ // nested selectors can start with a combinator
302
+ hasContent = node.addChild(this._parseCombinator());
303
+ }
304
+ while (node.addChild(this._parseSimpleSelector())) {
305
+ hasContent = true;
306
+ const mark = this.mark();
307
+ if (node.addChild(this._parseGuard()) && this.peek(TokenType.CurlyL)) {
308
+ break;
309
+ }
310
+ this.restoreAtMark(mark);
311
+ node.addChild(this._parseCombinator()); // optional
312
+ }
313
+ return hasContent ? this.finish(node) : null;
314
+ }
315
+ _parseSelectorCombinator() {
316
+ if (this.peekDelim('&')) {
317
+ const node = this.createNode(nodes.NodeType.SelectorCombinator);
318
+ this.consumeToken();
319
+ while (!this.hasWhitespace() && (this.acceptDelim('-') || this.accept(TokenType.Num) || this.accept(TokenType.Dimension) || node.addChild(this._parseIdent()) || this.acceptDelim('&'))) {
320
+ // support &-foo
321
+ }
322
+ return this.finish(node);
323
+ }
324
+ return null;
325
+ }
326
+ _parseSelectorIdent() {
327
+ if (!this.peekInterpolatedIdent()) {
328
+ return null;
329
+ }
330
+ const node = this.createNode(nodes.NodeType.SelectorInterpolation);
331
+ const hasContent = this._acceptInterpolatedIdent(node);
332
+ return hasContent ? this.finish(node) : null;
333
+ }
334
+ _parsePropertyIdentifier(inLookup = false) {
335
+ const propertyRegex = /^[\w-]+/;
336
+ if (!this.peekInterpolatedIdent() && !this.peekRegExp(this.token.type, propertyRegex)) {
337
+ return null;
338
+ }
339
+ const mark = this.mark();
340
+ const node = this.create(nodes.Identifier);
341
+ node.isCustomProperty = this.acceptDelim('-') && this.acceptDelim('-');
342
+ let childAdded = false;
343
+ if (!inLookup) {
344
+ if (node.isCustomProperty) {
345
+ childAdded = this._acceptInterpolatedIdent(node);
346
+ }
347
+ else {
348
+ childAdded = this._acceptInterpolatedIdent(node, propertyRegex);
349
+ }
350
+ }
351
+ else {
352
+ if (node.isCustomProperty) {
353
+ childAdded = node.addChild(this._parseIdent());
354
+ }
355
+ else {
356
+ childAdded = node.addChild(this._parseRegexp(propertyRegex));
357
+ }
358
+ }
359
+ if (!childAdded) {
360
+ this.restoreAtMark(mark);
361
+ return null;
362
+ }
363
+ if (!inLookup && !this.hasWhitespace()) {
364
+ this.acceptDelim('+');
365
+ if (!this.hasWhitespace()) {
366
+ this.acceptIdent('_');
367
+ }
368
+ }
369
+ return this.finish(node);
370
+ }
371
+ peekInterpolatedIdent() {
372
+ return this.peek(TokenType.Ident) ||
373
+ this.peekDelim('@') ||
374
+ this.peekDelim('$') ||
375
+ this.peekDelim('-');
376
+ }
377
+ _acceptInterpolatedIdent(node, identRegex) {
378
+ let hasContent = false;
379
+ const indentInterpolation = () => {
380
+ const pos = this.mark();
381
+ if (this.acceptDelim('-')) {
382
+ if (!this.hasWhitespace()) {
383
+ this.acceptDelim('-');
384
+ }
385
+ if (this.hasWhitespace()) {
386
+ this.restoreAtMark(pos);
387
+ return null;
388
+ }
389
+ }
390
+ return this._parseInterpolation();
391
+ };
392
+ const accept = identRegex ?
393
+ () => this.acceptRegexp(identRegex) :
394
+ () => this.accept(TokenType.Ident);
395
+ while (accept() ||
396
+ node.addChild(this._parseInterpolation() ||
397
+ this.try(indentInterpolation))) {
398
+ hasContent = true;
399
+ if (this.hasWhitespace()) {
400
+ break;
401
+ }
402
+ }
403
+ return hasContent;
404
+ }
405
+ _parseInterpolation() {
406
+ // @{name} Variable or
407
+ // ${name} Property
408
+ const mark = this.mark();
409
+ if (this.peekDelim('@') || this.peekDelim('$')) {
410
+ const node = this.createNode(nodes.NodeType.Interpolation);
411
+ this.consumeToken();
412
+ if (this.hasWhitespace() || !this.accept(TokenType.CurlyL)) {
413
+ this.restoreAtMark(mark);
414
+ return null;
415
+ }
416
+ if (!node.addChild(this._parseIdent())) {
417
+ return this.finish(node, ParseError.IdentifierExpected);
418
+ }
419
+ if (!this.accept(TokenType.CurlyR)) {
420
+ return this.finish(node, ParseError.RightCurlyExpected);
421
+ }
422
+ return this.finish(node);
423
+ }
424
+ return null;
425
+ }
426
+ _tryParseMixinDeclaration() {
427
+ const mark = this.mark();
428
+ const node = this.create(nodes.MixinDeclaration);
429
+ if (!node.setIdentifier(this._parseMixinDeclarationIdentifier()) || !this.accept(TokenType.ParenthesisL)) {
430
+ this.restoreAtMark(mark);
431
+ return null;
432
+ }
433
+ if (node.getParameters().addChild(this._parseMixinParameter())) {
434
+ while (this.accept(TokenType.Comma) || this.accept(TokenType.SemiColon)) {
435
+ if (this.peek(TokenType.ParenthesisR)) {
436
+ break;
437
+ }
438
+ if (!node.getParameters().addChild(this._parseMixinParameter())) {
439
+ this.markError(node, ParseError.IdentifierExpected, [], [TokenType.ParenthesisR]);
440
+ }
441
+ }
442
+ }
443
+ if (!this.accept(TokenType.ParenthesisR)) {
444
+ this.restoreAtMark(mark);
445
+ return null;
446
+ }
447
+ node.setGuard(this._parseGuard());
448
+ if (!this.peek(TokenType.CurlyL)) {
449
+ this.restoreAtMark(mark);
450
+ return null;
451
+ }
452
+ return this._parseBody(node, this._parseMixInBodyDeclaration.bind(this));
453
+ }
454
+ _parseMixInBodyDeclaration() {
455
+ return this._parseFontFace() || this._parseRuleSetDeclaration();
456
+ }
457
+ _parseMixinDeclarationIdentifier() {
458
+ let identifier;
459
+ if (this.peekDelim('#') || this.peekDelim('.')) {
460
+ identifier = this.create(nodes.Identifier);
461
+ this.consumeToken(); // # or .
462
+ if (this.hasWhitespace() || !identifier.addChild(this._parseIdent())) {
463
+ return null;
464
+ }
465
+ }
466
+ else if (this.peek(TokenType.Hash)) {
467
+ identifier = this.create(nodes.Identifier);
468
+ this.consumeToken(); // TokenType.Hash
469
+ }
470
+ else {
471
+ return null;
472
+ }
473
+ identifier.referenceTypes = [nodes.ReferenceType.Mixin];
474
+ return this.finish(identifier);
475
+ }
476
+ _parsePseudo() {
477
+ if (!this.peek(TokenType.Colon)) {
478
+ return null;
479
+ }
480
+ const mark = this.mark();
481
+ const node = this.create(nodes.ExtendsReference);
482
+ this.consumeToken(); // :
483
+ if (this.acceptIdent('extend')) {
484
+ return this._completeExtends(node);
485
+ }
486
+ this.restoreAtMark(mark);
487
+ return super._parsePseudo();
488
+ }
489
+ _parseExtend() {
490
+ if (!this.peekDelim('&')) {
491
+ return null;
492
+ }
493
+ const mark = this.mark();
494
+ const node = this.create(nodes.ExtendsReference);
495
+ this.consumeToken(); // &
496
+ if (this.hasWhitespace() || !this.accept(TokenType.Colon) || !this.acceptIdent('extend')) {
497
+ this.restoreAtMark(mark);
498
+ return null;
499
+ }
500
+ return this._completeExtends(node);
501
+ }
502
+ _completeExtends(node) {
503
+ if (!this.accept(TokenType.ParenthesisL)) {
504
+ return this.finish(node, ParseError.LeftParenthesisExpected);
505
+ }
506
+ const selectors = node.getSelectors();
507
+ if (!selectors.addChild(this._parseSelector(true))) {
508
+ return this.finish(node, ParseError.SelectorExpected);
509
+ }
510
+ while (this.accept(TokenType.Comma)) {
511
+ if (!selectors.addChild(this._parseSelector(true))) {
512
+ return this.finish(node, ParseError.SelectorExpected);
513
+ }
514
+ }
515
+ if (!this.accept(TokenType.ParenthesisR)) {
516
+ return this.finish(node, ParseError.RightParenthesisExpected);
517
+ }
518
+ return this.finish(node);
519
+ }
520
+ _parseDetachedRuleSetMixin() {
521
+ if (!this.peek(TokenType.AtKeyword)) {
522
+ return null;
523
+ }
524
+ const mark = this.mark();
525
+ const node = this.create(nodes.MixinReference);
526
+ if (node.addChild(this._parseVariable(true)) && (this.hasWhitespace() || !this.accept(TokenType.ParenthesisL))) {
527
+ this.restoreAtMark(mark);
528
+ return null;
529
+ }
530
+ if (!this.accept(TokenType.ParenthesisR)) {
531
+ return this.finish(node, ParseError.RightParenthesisExpected);
532
+ }
533
+ return this.finish(node);
534
+ }
535
+ _tryParseMixinReference(atRoot = true) {
536
+ const mark = this.mark();
537
+ const node = this.create(nodes.MixinReference);
538
+ let identifier = this._parseMixinDeclarationIdentifier();
539
+ while (identifier) {
540
+ this.acceptDelim('>');
541
+ const nextId = this._parseMixinDeclarationIdentifier();
542
+ if (nextId) {
543
+ node.getNamespaces().addChild(identifier);
544
+ identifier = nextId;
545
+ }
546
+ else {
547
+ break;
548
+ }
549
+ }
550
+ if (!node.setIdentifier(identifier)) {
551
+ this.restoreAtMark(mark);
552
+ return null;
553
+ }
554
+ let hasArguments = false;
555
+ if (this.accept(TokenType.ParenthesisL)) {
556
+ hasArguments = true;
557
+ if (node.getArguments().addChild(this._parseMixinArgument())) {
558
+ while (this.accept(TokenType.Comma) || this.accept(TokenType.SemiColon)) {
559
+ if (this.peek(TokenType.ParenthesisR)) {
560
+ break;
561
+ }
562
+ if (!node.getArguments().addChild(this._parseMixinArgument())) {
563
+ return this.finish(node, ParseError.ExpressionExpected);
564
+ }
565
+ }
566
+ }
567
+ if (!this.accept(TokenType.ParenthesisR)) {
568
+ return this.finish(node, ParseError.RightParenthesisExpected);
569
+ }
570
+ identifier.referenceTypes = [nodes.ReferenceType.Mixin];
571
+ }
572
+ else {
573
+ identifier.referenceTypes = [nodes.ReferenceType.Mixin, nodes.ReferenceType.Rule];
574
+ }
575
+ if (this.peek(TokenType.BracketL)) {
576
+ if (!atRoot) {
577
+ this._addLookupChildren(node);
578
+ }
579
+ }
580
+ else {
581
+ node.addChild(this._parsePrio());
582
+ }
583
+ if (!hasArguments && !this.peek(TokenType.SemiColon) && !this.peek(TokenType.CurlyR) && !this.peek(TokenType.EOF)) {
584
+ this.restoreAtMark(mark);
585
+ return null;
586
+ }
587
+ return this.finish(node);
588
+ }
589
+ _parseMixinArgument() {
590
+ // [variableName ':'] expression | variableName '...'
591
+ const node = this.create(nodes.FunctionArgument);
592
+ const pos = this.mark();
593
+ const argument = this._parseVariable();
594
+ if (argument) {
595
+ if (!this.accept(TokenType.Colon)) {
596
+ this.restoreAtMark(pos);
597
+ }
598
+ else {
599
+ node.setIdentifier(argument);
600
+ }
601
+ }
602
+ if (node.setValue(this._parseDetachedRuleSet() || this._parseExpr(true))) {
603
+ return this.finish(node);
604
+ }
605
+ this.restoreAtMark(pos);
606
+ return null;
607
+ }
608
+ _parseMixinParameter() {
609
+ const node = this.create(nodes.FunctionParameter);
610
+ // special rest variable: @rest...
611
+ if (this.peekKeyword('@rest')) {
612
+ const restNode = this.create(nodes.Node);
613
+ this.consumeToken();
614
+ if (!this.accept(lessScanner.Ellipsis)) {
615
+ return this.finish(node, ParseError.DotExpected, [], [TokenType.Comma, TokenType.ParenthesisR]);
616
+ }
617
+ node.setIdentifier(this.finish(restNode));
618
+ return this.finish(node);
619
+ }
620
+ // special const args: ...
621
+ if (this.peek(lessScanner.Ellipsis)) {
622
+ const varargsNode = this.create(nodes.Node);
623
+ this.consumeToken();
624
+ node.setIdentifier(this.finish(varargsNode));
625
+ return this.finish(node);
626
+ }
627
+ let hasContent = false;
628
+ // default variable declaration: @param: 12 or @name
629
+ if (node.setIdentifier(this._parseVariable())) {
630
+ this.accept(TokenType.Colon);
631
+ hasContent = true;
632
+ }
633
+ if (!node.setDefaultValue(this._parseDetachedRuleSet() || this._parseExpr(true)) && !hasContent) {
634
+ return null;
635
+ }
636
+ return this.finish(node);
637
+ }
638
+ _parseGuard() {
639
+ if (!this.peekIdent('when')) {
640
+ return null;
641
+ }
642
+ const node = this.create(nodes.LessGuard);
643
+ this.consumeToken(); // when
644
+ node.isNegated = this.acceptIdent('not');
645
+ if (!node.getConditions().addChild(this._parseGuardCondition())) {
646
+ return this.finish(node, ParseError.ConditionExpected);
647
+ }
648
+ while (this.acceptIdent('and') || this.accept(TokenType.Comma)) {
649
+ if (!node.getConditions().addChild(this._parseGuardCondition())) {
650
+ return this.finish(node, ParseError.ConditionExpected);
651
+ }
652
+ }
653
+ return this.finish(node);
654
+ }
655
+ _parseGuardCondition() {
656
+ if (!this.peek(TokenType.ParenthesisL)) {
657
+ return null;
658
+ }
659
+ const node = this.create(nodes.GuardCondition);
660
+ this.consumeToken(); // ParenthesisL
661
+ if (!node.addChild(this._parseExpr())) {
662
+ // empty (?)
663
+ }
664
+ if (!this.accept(TokenType.ParenthesisR)) {
665
+ return this.finish(node, ParseError.RightParenthesisExpected);
666
+ }
667
+ return this.finish(node);
668
+ }
669
+ _parseFunction() {
670
+ const pos = this.mark();
671
+ const node = this.create(nodes.Function);
672
+ if (!node.setIdentifier(this._parseFunctionIdentifier())) {
673
+ return null;
674
+ }
675
+ if (this.hasWhitespace() || !this.accept(TokenType.ParenthesisL)) {
676
+ this.restoreAtMark(pos);
677
+ return null;
678
+ }
679
+ if (node.getArguments().addChild(this._parseMixinArgument())) {
680
+ while (this.accept(TokenType.Comma) || this.accept(TokenType.SemiColon)) {
681
+ if (this.peek(TokenType.ParenthesisR)) {
682
+ break;
683
+ }
684
+ if (!node.getArguments().addChild(this._parseMixinArgument())) {
685
+ return this.finish(node, ParseError.ExpressionExpected);
686
+ }
687
+ }
688
+ }
689
+ if (!this.accept(TokenType.ParenthesisR)) {
690
+ return this.finish(node, ParseError.RightParenthesisExpected);
691
+ }
692
+ return this.finish(node);
693
+ }
694
+ _parseFunctionIdentifier() {
695
+ if (this.peekDelim('%')) {
696
+ const node = this.create(nodes.Identifier);
697
+ node.referenceTypes = [nodes.ReferenceType.Function];
698
+ this.consumeToken();
699
+ return this.finish(node);
700
+ }
701
+ return super._parseFunctionIdentifier();
702
+ }
703
+ _parseURLArgument() {
704
+ const pos = this.mark();
705
+ const node = super._parseURLArgument();
706
+ if (!node || !this.peek(TokenType.ParenthesisR)) {
707
+ this.restoreAtMark(pos);
708
+ const node = this.create(nodes.Node);
709
+ node.addChild(this._parseBinaryExpr());
710
+ return this.finish(node);
711
+ }
712
+ return node;
713
+ }
714
+ }