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