vscode-css-languageservice 6.0.1 → 6.1.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 (87) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/SECURITY.md +41 -0
  3. package/lib/esm/beautify/beautify-css.js +11 -4
  4. package/lib/esm/cssLanguageService.d.ts +38 -37
  5. package/lib/esm/cssLanguageService.js +73 -72
  6. package/lib/esm/cssLanguageTypes.d.ts +238 -238
  7. package/lib/esm/cssLanguageTypes.js +42 -42
  8. package/lib/esm/data/webCustomData.js +22089 -21959
  9. package/lib/esm/languageFacts/builtinData.js +142 -142
  10. package/lib/esm/languageFacts/colors.js +469 -469
  11. package/lib/esm/languageFacts/dataManager.js +88 -88
  12. package/lib/esm/languageFacts/dataProvider.js +73 -73
  13. package/lib/esm/languageFacts/entry.js +137 -137
  14. package/lib/esm/languageFacts/facts.js +8 -8
  15. package/lib/esm/parser/cssErrors.js +48 -48
  16. package/lib/esm/parser/cssNodes.js +1511 -1502
  17. package/lib/esm/parser/cssParser.js +1606 -1534
  18. package/lib/esm/parser/cssScanner.js +592 -592
  19. package/lib/esm/parser/cssSymbolScope.js +311 -311
  20. package/lib/esm/parser/lessParser.js +715 -714
  21. package/lib/esm/parser/lessScanner.js +57 -57
  22. package/lib/esm/parser/scssErrors.js +18 -18
  23. package/lib/esm/parser/scssParser.js +806 -796
  24. package/lib/esm/parser/scssScanner.js +95 -95
  25. package/lib/esm/services/cssCodeActions.js +77 -77
  26. package/lib/esm/services/cssCompletion.js +1054 -1054
  27. package/lib/esm/services/cssFolding.js +190 -190
  28. package/lib/esm/services/cssFormatter.js +136 -136
  29. package/lib/esm/services/cssHover.js +148 -148
  30. package/lib/esm/services/cssNavigation.js +441 -378
  31. package/lib/esm/services/cssSelectionRange.js +47 -47
  32. package/lib/esm/services/cssValidation.js +41 -41
  33. package/lib/esm/services/lessCompletion.js +378 -378
  34. package/lib/esm/services/lint.js +518 -518
  35. package/lib/esm/services/lintRules.js +76 -76
  36. package/lib/esm/services/lintUtil.js +196 -196
  37. package/lib/esm/services/pathCompletion.js +157 -157
  38. package/lib/esm/services/scssCompletion.js +354 -354
  39. package/lib/esm/services/scssNavigation.js +82 -82
  40. package/lib/esm/services/selectorPrinting.js +492 -492
  41. package/lib/esm/utils/arrays.js +40 -40
  42. package/lib/esm/utils/objects.js +11 -11
  43. package/lib/esm/utils/resources.js +11 -11
  44. package/lib/esm/utils/strings.js +102 -102
  45. package/lib/umd/beautify/beautify-css.js +11 -4
  46. package/lib/umd/cssLanguageService.d.ts +38 -37
  47. package/lib/umd/cssLanguageService.js +104 -99
  48. package/lib/umd/cssLanguageTypes.d.ts +238 -238
  49. package/lib/umd/cssLanguageTypes.js +89 -89
  50. package/lib/umd/data/webCustomData.js +22102 -21972
  51. package/lib/umd/languageFacts/builtinData.js +154 -154
  52. package/lib/umd/languageFacts/colors.js +492 -492
  53. package/lib/umd/languageFacts/dataManager.js +101 -101
  54. package/lib/umd/languageFacts/dataProvider.js +86 -86
  55. package/lib/umd/languageFacts/entry.js +152 -152
  56. package/lib/umd/languageFacts/facts.js +33 -29
  57. package/lib/umd/parser/cssErrors.js +61 -61
  58. package/lib/umd/parser/cssNodes.js +1597 -1587
  59. package/lib/umd/parser/cssParser.js +1619 -1547
  60. package/lib/umd/parser/cssScanner.js +606 -606
  61. package/lib/umd/parser/cssSymbolScope.js +328 -328
  62. package/lib/umd/parser/lessParser.js +728 -727
  63. package/lib/umd/parser/lessScanner.js +70 -70
  64. package/lib/umd/parser/scssErrors.js +31 -31
  65. package/lib/umd/parser/scssParser.js +819 -809
  66. package/lib/umd/parser/scssScanner.js +108 -108
  67. package/lib/umd/services/cssCodeActions.js +90 -90
  68. package/lib/umd/services/cssCompletion.js +1067 -1067
  69. package/lib/umd/services/cssFolding.js +203 -203
  70. package/lib/umd/services/cssFormatter.js +150 -150
  71. package/lib/umd/services/cssHover.js +161 -161
  72. package/lib/umd/services/cssNavigation.js +454 -391
  73. package/lib/umd/services/cssSelectionRange.js +60 -60
  74. package/lib/umd/services/cssValidation.js +54 -54
  75. package/lib/umd/services/lessCompletion.js +391 -391
  76. package/lib/umd/services/lint.js +531 -531
  77. package/lib/umd/services/lintRules.js +91 -91
  78. package/lib/umd/services/lintUtil.js +210 -210
  79. package/lib/umd/services/pathCompletion.js +171 -171
  80. package/lib/umd/services/scssCompletion.js +367 -367
  81. package/lib/umd/services/scssNavigation.js +95 -95
  82. package/lib/umd/services/selectorPrinting.js +510 -510
  83. package/lib/umd/utils/arrays.js +55 -55
  84. package/lib/umd/utils/objects.js +25 -25
  85. package/lib/umd/utils/resources.js +26 -26
  86. package/lib/umd/utils/strings.js +120 -120
  87. package/package.json +13 -12
@@ -1,714 +1,715 @@
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
- }
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._parseLayer() // @layer
276
+ || this._parseDetachedRuleSetMixin() // less detached ruleset mixin
277
+ || this._parseVariableDeclaration() // Variable declarations
278
+ || super._parseRuleSetDeclarationAtStatement();
279
+ }
280
+ return this._tryParseMixinDeclaration()
281
+ || this._tryParseRuleset(true) // nested ruleset
282
+ || this._tryParseMixinReference() // less mixin reference
283
+ || this._parseFunction()
284
+ || this._parseExtend() // less extend declaration
285
+ || super._parseRuleSetDeclaration(); // try css ruleset declaration as the last option
286
+ }
287
+ _parseKeyframeIdent() {
288
+ return this._parseIdent([nodes.ReferenceType.Keyframe]) || this._parseVariable();
289
+ }
290
+ _parseKeyframeSelector() {
291
+ return this._parseDetachedRuleSetMixin() // less detached ruleset mixin
292
+ || super._parseKeyframeSelector();
293
+ }
294
+ _parseSimpleSelectorBody() {
295
+ return this._parseSelectorCombinator() || super._parseSimpleSelectorBody();
296
+ }
297
+ _parseSelector(isNested) {
298
+ // CSS Guards
299
+ const node = this.create(nodes.Selector);
300
+ let hasContent = false;
301
+ if (isNested) {
302
+ // nested selectors can start with a combinator
303
+ hasContent = node.addChild(this._parseCombinator());
304
+ }
305
+ while (node.addChild(this._parseSimpleSelector())) {
306
+ hasContent = true;
307
+ const mark = this.mark();
308
+ if (node.addChild(this._parseGuard()) && this.peek(TokenType.CurlyL)) {
309
+ break;
310
+ }
311
+ this.restoreAtMark(mark);
312
+ node.addChild(this._parseCombinator()); // optional
313
+ }
314
+ return hasContent ? this.finish(node) : null;
315
+ }
316
+ _parseSelectorCombinator() {
317
+ if (this.peekDelim('&')) {
318
+ const node = this.createNode(nodes.NodeType.SelectorCombinator);
319
+ this.consumeToken();
320
+ while (!this.hasWhitespace() && (this.acceptDelim('-') || this.accept(TokenType.Num) || this.accept(TokenType.Dimension) || node.addChild(this._parseIdent()) || this.acceptDelim('&'))) {
321
+ // support &-foo
322
+ }
323
+ return this.finish(node);
324
+ }
325
+ return null;
326
+ }
327
+ _parseSelectorIdent() {
328
+ if (!this.peekInterpolatedIdent()) {
329
+ return null;
330
+ }
331
+ const node = this.createNode(nodes.NodeType.SelectorInterpolation);
332
+ const hasContent = this._acceptInterpolatedIdent(node);
333
+ return hasContent ? this.finish(node) : null;
334
+ }
335
+ _parsePropertyIdentifier(inLookup = false) {
336
+ const propertyRegex = /^[\w-]+/;
337
+ if (!this.peekInterpolatedIdent() && !this.peekRegExp(this.token.type, propertyRegex)) {
338
+ return null;
339
+ }
340
+ const mark = this.mark();
341
+ const node = this.create(nodes.Identifier);
342
+ node.isCustomProperty = this.acceptDelim('-') && this.acceptDelim('-');
343
+ let childAdded = false;
344
+ if (!inLookup) {
345
+ if (node.isCustomProperty) {
346
+ childAdded = this._acceptInterpolatedIdent(node);
347
+ }
348
+ else {
349
+ childAdded = this._acceptInterpolatedIdent(node, propertyRegex);
350
+ }
351
+ }
352
+ else {
353
+ if (node.isCustomProperty) {
354
+ childAdded = node.addChild(this._parseIdent());
355
+ }
356
+ else {
357
+ childAdded = node.addChild(this._parseRegexp(propertyRegex));
358
+ }
359
+ }
360
+ if (!childAdded) {
361
+ this.restoreAtMark(mark);
362
+ return null;
363
+ }
364
+ if (!inLookup && !this.hasWhitespace()) {
365
+ this.acceptDelim('+');
366
+ if (!this.hasWhitespace()) {
367
+ this.acceptIdent('_');
368
+ }
369
+ }
370
+ return this.finish(node);
371
+ }
372
+ peekInterpolatedIdent() {
373
+ return this.peek(TokenType.Ident) ||
374
+ this.peekDelim('@') ||
375
+ this.peekDelim('$') ||
376
+ this.peekDelim('-');
377
+ }
378
+ _acceptInterpolatedIdent(node, identRegex) {
379
+ let hasContent = false;
380
+ const indentInterpolation = () => {
381
+ const pos = this.mark();
382
+ if (this.acceptDelim('-')) {
383
+ if (!this.hasWhitespace()) {
384
+ this.acceptDelim('-');
385
+ }
386
+ if (this.hasWhitespace()) {
387
+ this.restoreAtMark(pos);
388
+ return null;
389
+ }
390
+ }
391
+ return this._parseInterpolation();
392
+ };
393
+ const accept = identRegex ?
394
+ () => this.acceptRegexp(identRegex) :
395
+ () => this.accept(TokenType.Ident);
396
+ while (accept() ||
397
+ node.addChild(this._parseInterpolation() ||
398
+ this.try(indentInterpolation))) {
399
+ hasContent = true;
400
+ if (this.hasWhitespace()) {
401
+ break;
402
+ }
403
+ }
404
+ return hasContent;
405
+ }
406
+ _parseInterpolation() {
407
+ // @{name} Variable or
408
+ // ${name} Property
409
+ const mark = this.mark();
410
+ if (this.peekDelim('@') || this.peekDelim('$')) {
411
+ const node = this.createNode(nodes.NodeType.Interpolation);
412
+ this.consumeToken();
413
+ if (this.hasWhitespace() || !this.accept(TokenType.CurlyL)) {
414
+ this.restoreAtMark(mark);
415
+ return null;
416
+ }
417
+ if (!node.addChild(this._parseIdent())) {
418
+ return this.finish(node, ParseError.IdentifierExpected);
419
+ }
420
+ if (!this.accept(TokenType.CurlyR)) {
421
+ return this.finish(node, ParseError.RightCurlyExpected);
422
+ }
423
+ return this.finish(node);
424
+ }
425
+ return null;
426
+ }
427
+ _tryParseMixinDeclaration() {
428
+ const mark = this.mark();
429
+ const node = this.create(nodes.MixinDeclaration);
430
+ if (!node.setIdentifier(this._parseMixinDeclarationIdentifier()) || !this.accept(TokenType.ParenthesisL)) {
431
+ this.restoreAtMark(mark);
432
+ return null;
433
+ }
434
+ if (node.getParameters().addChild(this._parseMixinParameter())) {
435
+ while (this.accept(TokenType.Comma) || this.accept(TokenType.SemiColon)) {
436
+ if (this.peek(TokenType.ParenthesisR)) {
437
+ break;
438
+ }
439
+ if (!node.getParameters().addChild(this._parseMixinParameter())) {
440
+ this.markError(node, ParseError.IdentifierExpected, [], [TokenType.ParenthesisR]);
441
+ }
442
+ }
443
+ }
444
+ if (!this.accept(TokenType.ParenthesisR)) {
445
+ this.restoreAtMark(mark);
446
+ return null;
447
+ }
448
+ node.setGuard(this._parseGuard());
449
+ if (!this.peek(TokenType.CurlyL)) {
450
+ this.restoreAtMark(mark);
451
+ return null;
452
+ }
453
+ return this._parseBody(node, this._parseMixInBodyDeclaration.bind(this));
454
+ }
455
+ _parseMixInBodyDeclaration() {
456
+ return this._parseFontFace() || this._parseRuleSetDeclaration();
457
+ }
458
+ _parseMixinDeclarationIdentifier() {
459
+ let identifier;
460
+ if (this.peekDelim('#') || this.peekDelim('.')) {
461
+ identifier = this.create(nodes.Identifier);
462
+ this.consumeToken(); // # or .
463
+ if (this.hasWhitespace() || !identifier.addChild(this._parseIdent())) {
464
+ return null;
465
+ }
466
+ }
467
+ else if (this.peek(TokenType.Hash)) {
468
+ identifier = this.create(nodes.Identifier);
469
+ this.consumeToken(); // TokenType.Hash
470
+ }
471
+ else {
472
+ return null;
473
+ }
474
+ identifier.referenceTypes = [nodes.ReferenceType.Mixin];
475
+ return this.finish(identifier);
476
+ }
477
+ _parsePseudo() {
478
+ if (!this.peek(TokenType.Colon)) {
479
+ return null;
480
+ }
481
+ const mark = this.mark();
482
+ const node = this.create(nodes.ExtendsReference);
483
+ this.consumeToken(); // :
484
+ if (this.acceptIdent('extend')) {
485
+ return this._completeExtends(node);
486
+ }
487
+ this.restoreAtMark(mark);
488
+ return super._parsePseudo();
489
+ }
490
+ _parseExtend() {
491
+ if (!this.peekDelim('&')) {
492
+ return null;
493
+ }
494
+ const mark = this.mark();
495
+ const node = this.create(nodes.ExtendsReference);
496
+ this.consumeToken(); // &
497
+ if (this.hasWhitespace() || !this.accept(TokenType.Colon) || !this.acceptIdent('extend')) {
498
+ this.restoreAtMark(mark);
499
+ return null;
500
+ }
501
+ return this._completeExtends(node);
502
+ }
503
+ _completeExtends(node) {
504
+ if (!this.accept(TokenType.ParenthesisL)) {
505
+ return this.finish(node, ParseError.LeftParenthesisExpected);
506
+ }
507
+ const selectors = node.getSelectors();
508
+ if (!selectors.addChild(this._parseSelector(true))) {
509
+ return this.finish(node, ParseError.SelectorExpected);
510
+ }
511
+ while (this.accept(TokenType.Comma)) {
512
+ if (!selectors.addChild(this._parseSelector(true))) {
513
+ return this.finish(node, ParseError.SelectorExpected);
514
+ }
515
+ }
516
+ if (!this.accept(TokenType.ParenthesisR)) {
517
+ return this.finish(node, ParseError.RightParenthesisExpected);
518
+ }
519
+ return this.finish(node);
520
+ }
521
+ _parseDetachedRuleSetMixin() {
522
+ if (!this.peek(TokenType.AtKeyword)) {
523
+ return null;
524
+ }
525
+ const mark = this.mark();
526
+ const node = this.create(nodes.MixinReference);
527
+ if (node.addChild(this._parseVariable(true)) && (this.hasWhitespace() || !this.accept(TokenType.ParenthesisL))) {
528
+ this.restoreAtMark(mark);
529
+ return null;
530
+ }
531
+ if (!this.accept(TokenType.ParenthesisR)) {
532
+ return this.finish(node, ParseError.RightParenthesisExpected);
533
+ }
534
+ return this.finish(node);
535
+ }
536
+ _tryParseMixinReference(atRoot = true) {
537
+ const mark = this.mark();
538
+ const node = this.create(nodes.MixinReference);
539
+ let identifier = this._parseMixinDeclarationIdentifier();
540
+ while (identifier) {
541
+ this.acceptDelim('>');
542
+ const nextId = this._parseMixinDeclarationIdentifier();
543
+ if (nextId) {
544
+ node.getNamespaces().addChild(identifier);
545
+ identifier = nextId;
546
+ }
547
+ else {
548
+ break;
549
+ }
550
+ }
551
+ if (!node.setIdentifier(identifier)) {
552
+ this.restoreAtMark(mark);
553
+ return null;
554
+ }
555
+ let hasArguments = false;
556
+ if (this.accept(TokenType.ParenthesisL)) {
557
+ hasArguments = true;
558
+ if (node.getArguments().addChild(this._parseMixinArgument())) {
559
+ while (this.accept(TokenType.Comma) || this.accept(TokenType.SemiColon)) {
560
+ if (this.peek(TokenType.ParenthesisR)) {
561
+ break;
562
+ }
563
+ if (!node.getArguments().addChild(this._parseMixinArgument())) {
564
+ return this.finish(node, ParseError.ExpressionExpected);
565
+ }
566
+ }
567
+ }
568
+ if (!this.accept(TokenType.ParenthesisR)) {
569
+ return this.finish(node, ParseError.RightParenthesisExpected);
570
+ }
571
+ identifier.referenceTypes = [nodes.ReferenceType.Mixin];
572
+ }
573
+ else {
574
+ identifier.referenceTypes = [nodes.ReferenceType.Mixin, nodes.ReferenceType.Rule];
575
+ }
576
+ if (this.peek(TokenType.BracketL)) {
577
+ if (!atRoot) {
578
+ this._addLookupChildren(node);
579
+ }
580
+ }
581
+ else {
582
+ node.addChild(this._parsePrio());
583
+ }
584
+ if (!hasArguments && !this.peek(TokenType.SemiColon) && !this.peek(TokenType.CurlyR) && !this.peek(TokenType.EOF)) {
585
+ this.restoreAtMark(mark);
586
+ return null;
587
+ }
588
+ return this.finish(node);
589
+ }
590
+ _parseMixinArgument() {
591
+ // [variableName ':'] expression | variableName '...'
592
+ const node = this.create(nodes.FunctionArgument);
593
+ const pos = this.mark();
594
+ const argument = this._parseVariable();
595
+ if (argument) {
596
+ if (!this.accept(TokenType.Colon)) {
597
+ this.restoreAtMark(pos);
598
+ }
599
+ else {
600
+ node.setIdentifier(argument);
601
+ }
602
+ }
603
+ if (node.setValue(this._parseDetachedRuleSet() || this._parseExpr(true))) {
604
+ return this.finish(node);
605
+ }
606
+ this.restoreAtMark(pos);
607
+ return null;
608
+ }
609
+ _parseMixinParameter() {
610
+ const node = this.create(nodes.FunctionParameter);
611
+ // special rest variable: @rest...
612
+ if (this.peekKeyword('@rest')) {
613
+ const restNode = this.create(nodes.Node);
614
+ this.consumeToken();
615
+ if (!this.accept(lessScanner.Ellipsis)) {
616
+ return this.finish(node, ParseError.DotExpected, [], [TokenType.Comma, TokenType.ParenthesisR]);
617
+ }
618
+ node.setIdentifier(this.finish(restNode));
619
+ return this.finish(node);
620
+ }
621
+ // special const args: ...
622
+ if (this.peek(lessScanner.Ellipsis)) {
623
+ const varargsNode = this.create(nodes.Node);
624
+ this.consumeToken();
625
+ node.setIdentifier(this.finish(varargsNode));
626
+ return this.finish(node);
627
+ }
628
+ let hasContent = false;
629
+ // default variable declaration: @param: 12 or @name
630
+ if (node.setIdentifier(this._parseVariable())) {
631
+ this.accept(TokenType.Colon);
632
+ hasContent = true;
633
+ }
634
+ if (!node.setDefaultValue(this._parseDetachedRuleSet() || this._parseExpr(true)) && !hasContent) {
635
+ return null;
636
+ }
637
+ return this.finish(node);
638
+ }
639
+ _parseGuard() {
640
+ if (!this.peekIdent('when')) {
641
+ return null;
642
+ }
643
+ const node = this.create(nodes.LessGuard);
644
+ this.consumeToken(); // when
645
+ node.isNegated = this.acceptIdent('not');
646
+ if (!node.getConditions().addChild(this._parseGuardCondition())) {
647
+ return this.finish(node, ParseError.ConditionExpected);
648
+ }
649
+ while (this.acceptIdent('and') || this.accept(TokenType.Comma)) {
650
+ if (!node.getConditions().addChild(this._parseGuardCondition())) {
651
+ return this.finish(node, ParseError.ConditionExpected);
652
+ }
653
+ }
654
+ return this.finish(node);
655
+ }
656
+ _parseGuardCondition() {
657
+ if (!this.peek(TokenType.ParenthesisL)) {
658
+ return null;
659
+ }
660
+ const node = this.create(nodes.GuardCondition);
661
+ this.consumeToken(); // ParenthesisL
662
+ if (!node.addChild(this._parseExpr())) {
663
+ // empty (?)
664
+ }
665
+ if (!this.accept(TokenType.ParenthesisR)) {
666
+ return this.finish(node, ParseError.RightParenthesisExpected);
667
+ }
668
+ return this.finish(node);
669
+ }
670
+ _parseFunction() {
671
+ const pos = this.mark();
672
+ const node = this.create(nodes.Function);
673
+ if (!node.setIdentifier(this._parseFunctionIdentifier())) {
674
+ return null;
675
+ }
676
+ if (this.hasWhitespace() || !this.accept(TokenType.ParenthesisL)) {
677
+ this.restoreAtMark(pos);
678
+ return null;
679
+ }
680
+ if (node.getArguments().addChild(this._parseMixinArgument())) {
681
+ while (this.accept(TokenType.Comma) || this.accept(TokenType.SemiColon)) {
682
+ if (this.peek(TokenType.ParenthesisR)) {
683
+ break;
684
+ }
685
+ if (!node.getArguments().addChild(this._parseMixinArgument())) {
686
+ return this.finish(node, ParseError.ExpressionExpected);
687
+ }
688
+ }
689
+ }
690
+ if (!this.accept(TokenType.ParenthesisR)) {
691
+ return this.finish(node, ParseError.RightParenthesisExpected);
692
+ }
693
+ return this.finish(node);
694
+ }
695
+ _parseFunctionIdentifier() {
696
+ if (this.peekDelim('%')) {
697
+ const node = this.create(nodes.Identifier);
698
+ node.referenceTypes = [nodes.ReferenceType.Function];
699
+ this.consumeToken();
700
+ return this.finish(node);
701
+ }
702
+ return super._parseFunctionIdentifier();
703
+ }
704
+ _parseURLArgument() {
705
+ const pos = this.mark();
706
+ const node = super._parseURLArgument();
707
+ if (!node || !this.peek(TokenType.ParenthesisR)) {
708
+ this.restoreAtMark(pos);
709
+ const node = this.create(nodes.Node);
710
+ node.addChild(this._parseBinaryExpr());
711
+ return this.finish(node);
712
+ }
713
+ return node;
714
+ }
715
+ }