-
Notifications
You must be signed in to change notification settings - Fork 47
/
Less.g
1155 lines (1023 loc) · 34.5 KB
/
Less.g
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//http://stackoverflow.com/questions/7765018/returning-list-in-antlr-for-type-checking-language-java
//
// A complete lexer and grammar for CSS 2.1 as defined by the
// W3 specification.
//
// This grammar is free to use providing you retain everyhting in this header comment
// section.
//
// Author : Jim Idle, Temporal Wave LLC.
// Contact : jimi@temporal-wave.com
// Website : http://www.temporal-wave.com
// License : ANTLR Free BSD License
//
// Please visit our Web site at http://www.temporal-wave.com and try our commercial
// parsers for SQL, C#, VB.Net and more.
//
// This grammar is free to use providing you retain everything in this header comment
// section.
//
grammar Less;
options {
// antlr will generate java lexer and parser
language = Java;
// generated parser should create abstract syntax tree
output = AST;
// parser will extend special super class
superClass = SuperLessParser;
}
tokens {
VARIABLE_DECLARATION;
ARGUMENT_DECLARATION;
ARGUMENT_COLLECTOR;
EXPRESSION;
DECLARATION;
VARIABLE_REFERENCE;
RULESET;
NESTED_APPENDER;
SELECTOR;
SIMPLE_SELECTOR;
EXPRESSION;
EXPRESSION_PARENTHESES;
STYLE_SHEET;
//ANTLR seems to generate null pointers exceptions on malformed css if
//rules match nothig and generate an empty token
EMPTY_SEPARATOR;
ELEMENT_NAME;
CSS_CLASS;
NTH;
PSEUDO;
ATTRIBUTE;
ID_SELECTOR;
ELEMENT_SUBSEQUENT;
CHARSET_DECLARATION;
TERM_FUNCTION;
TERM;
MEDIUM_DECLARATION;
MEDIA_EXPRESSION;
MEDIA_QUERY;
MEDIUM_TYPE;
BODY;
MIXIN_REFERENCE;
NAMESPACE_REFERENCE;
REUSABLE_STRUCTURE;
MIXIN_PATTERN;
GUARD_CONDITION;
GUARD;
DUMMY_MEANINGFULL_WHITESPACE; //ANTLR, please
}
@lexer::header {
package com.github.sommeri.less4j.core.parser;
import com.github.sommeri.less4j.core.parser.AntlrException;
import com.github.sommeri.less4j.LessCompiler.Problem;
}
@parser::header {
package com.github.sommeri.less4j.core.parser;
import com.github.sommeri.less4j.core.parser.SuperLessParser;
import com.github.sommeri.less4j.LessCompiler.Problem;
}
//override some methods and add new members to generated lexer
@lexer::members {
public LessLexer(List<Problem> errors) {
this.errors = errors;
}
public LessLexer(CharStream input, List<Problem> errors) {
this(input, new RecognizerSharedState(), errors);
}
public LessLexer(CharStream input, RecognizerSharedState state, List<Problem> errors) {
super(input,state);
this.errors = errors;
}
//This trick allow Lexer to emit multiple tokens per one rule.
List tokens = new ArrayList();
public void emit(Token token) {
state.token = token;
tokens.add(token);
}
public void emitAs(Token token, int type) {
if (token==null)
return ;
token.setType(type);
emit(token);
}
public Token nextToken() {
super.nextToken();
if ( tokens.size()==0 ) {
return Token.EOF_TOKEN;
}
return (Token)tokens.remove(0);
}
//add new field
private List<Problem> errors = new ArrayList<Problem>();
//add new method
public List<Problem> getAllErrors() {
return new ArrayList<Problem>(errors);
}
//add new method
public boolean hasErrors() {
return !errors.isEmpty();
}
//override method
public void reportError(RecognitionException e) {
errors.add(new AntlrException(e, getErrorMessage(e, getTokenNames())));
}
}
//override some methods and add new members to generated parser
@parser::members {
public LessParser(TokenStream input, List<Problem> errors) {
super(input, errors);
}
public LessParser(TokenStream input, RecognizerSharedState state, List<Problem> errors) {
super(input, state, errors);
}
}
// -------------
// Main rule. This is the main entry rule for the parser, the top level
// grammar rule.
//
// A style sheet consists of an optional character set specification, an optional series
// of imports, and then the main body of style rules.
//
styleSheet
@init {enterRule(retval, RULE_STYLESHEET);}
: ( a+=charSet* //the original ? was replaced by *, because it is possible (even if it makes no sense) and less.js is able to handle such situation
a+=imports*
a+=bodylist
EOF ) -> ^(STYLE_SHEET ($a)*)
;
finally { leaveRule(); }
// -----------------
// Character set. Picks up the user specified character set, should it be present.
// Removed an empty option from this rule, ANTLR seems to have problems with this.
// https://developer.mozilla.org/en/CSS/@charset
charSet
@init {enterRule(retval, RULE_CHARSET);}
: CHARSET_SYM STRING SEMI -> ^(CHARSET_DECLARATION STRING)
;
finally { leaveRule(); }
// ---------
// Import. Location of an external style sheet to include in the ruleset.
//
imports
@init {enterRule(retval, RULE_IMPORTS);}
: IMPORT_SYM (STRING|URI) (mediaQuery (COMMA mediaQuery)*)? SEMI
;
finally { leaveRule(); }
// ---------
// Media. Introduce a set of rules that are to be used if the consumer indicates
// it belongs to the signified medium.
// Media can also have a ruleset in them.
//
//TODO media part of the grammar is throwing away tokens, so comments will not work correctly. fix that
media
@init {enterRule(retval, RULE_MEDIA);}
: MEDIA_SYM (m1+=mediaQuery (n+=COMMA m+=mediaQuery)*)
q1=LBRACE
((declaration) => b+=declaration SEMI
| b+=variabledeclaration
| b+=ruleSet )*
q2=RBRACE
-> ^(MEDIA_SYM ^(MEDIUM_DECLARATION $m1 ($n $m)*) $q1 $b* $q2)
;
finally { leaveRule(); }
// ---------
// Medium. The name of a medim that are particulare set of rules applies to.
//
mediaQuery
@init {enterRule(retval, RULE_MEDIUM);}
: a+=IDENT (a+=IDENT)? (b+=IDENT c+=mediaExpression)* -> ^(MEDIA_QUERY ^(MEDIUM_TYPE $a*) ($b $c)*)
| d=mediaExpression (e+=IDENT f+=mediaExpression)* -> ^(MEDIA_QUERY $d ($e $f)*)
;
finally { leaveRule(); }
mediaExpression
: LPAREN a+=mediaFeature (b+=COLON c+=expr)? RPAREN -> ^(MEDIA_EXPRESSION $a* $b* $c*)
;
mediaFeature
: IDENT
;
bodylist
: bodyset*
;
bodyset
: (reusableStructureName LPAREN)=>reusableStructure
| ruleSet
| media
| page
| fontface
| variabledeclaration
;
variabledeclaration
@init {enterRule(retval, RULE_VARIABLE_DECLARATION);}
: VARIABLE COLON (a+=expr) SEMI -> ^(VARIABLE_DECLARATION VARIABLE COLON $a* SEMI)
;
finally { leaveRule(); }
//used in mixinReferenceArgument
variabledeclarationLimitedNoSemi
@init {enterRule(retval, RULE_VARIABLE_DECLARATION);}
: VARIABLE COLON (a+=mathExprHighPrior) -> ^(VARIABLE_DECLARATION VARIABLE COLON $a* )
;
finally { leaveRule(); }
//This looks like the declaration, but does not allow a comma.
reusableStructureParameter
: VARIABLE ((b=COLON (a+=mathExprHighPrior)) | b=DOT3)? -> ^(ARGUMENT_DECLARATION VARIABLE $b* $a*)
;
variablereference
@init {enterRule(retval, RULE_VARIABLE_REFERENCE);}
: VARIABLE | INDIRECT_VARIABLE
;
finally { leaveRule(); }
fontface
@init {enterRule(retval, RULE_FONT_FACE);}
: FONT_FACE_SYM^
LBRACE!
declaration SEMI! (declaration SEMI!)*
RBRACE!
;
finally { leaveRule(); }
page
@init {enterRule(retval, RULE_PAGE);}
: PAGE_SYM pseudoPage?
LBRACE
declaration SEMI (declaration SEMI)*
RBRACE
;
finally { leaveRule(); }
pseudoPage
@init {enterRule(retval, RULE_PSEUDO_PAGE);}
: COLON IDENT
;
finally { leaveRule(); }
topLevelOperator
: COMMA
| ( -> EMPTY_SEPARATOR)
;
//combinator
// : GREATER
// | PLUS
// | TILDE
// | ( -> EMPTY_COMBINATOR)
// ;
combinator
: GREATER
| PLUS
| TILDE
| ({predicates.onEmptyCombinator(input)}?)=> -> EMPTY_COMBINATOR
;
unaryOperator
: MINUS
| PLUS
;
property
: IDENT
;
//we need to put comma into the tree so we can collect comments to it
//TODO: this does not accurately describes the grammar. Nested and real selectors are different.
ruleSet
@init {enterRule(retval, RULE_RULESET);}
: (a+=selector ( a+=selectorSeparator a+=selector)*)?
b=ruleset_body
-> ^(RULESET $a* $b)
;
finally { leaveRule(); }
selectorSeparator
: COMMA ;
nestedAppender // this must be here because of special case & & <- the space belongs to both appenders
: ((MEANINGFULL_WHITESPACE? APPENDER MEANINGFULL_WHITESPACE APPENDER)=>a+=MEANINGFULL_WHITESPACE? a+=APPENDER) -> ^(NESTED_APPENDER $a* DUMMY_MEANINGFULL_WHITESPACE)
| (
a+=MEANINGFULL_WHITESPACE?
a+=APPENDER
(
(MEANINGFULL_WHITESPACE)=>a+=MEANINGFULL_WHITESPACE
|
)
) -> ^(NESTED_APPENDER $a*)
;
//css does not require ; in last declaration
ruleset_body
: LBRACE
( ((declarationWithSemicolon)=> (a+=declarationWithSemicolon) )
| (ruleSet)=> a+=ruleSet
| (mixinReferenceWithSemi)=>a+=mixinReferenceWithSemi
| (namespaceReferenceWithSemi)=>a+=namespaceReferenceWithSemi
| (reusableStructure)=>a+=reusableStructure
| a+=variabledeclaration
)*
(
( (declaration)=>a+=declaration RBRACE)
| ( (mixinReference)=>a+=mixinReference RBRACE)
| ( (namespaceReference)=>a+=namespaceReference RBRACE)
| RBRACE
)
//If we remove LBRACE from the tree, a ruleset with an empty selector will report wrong line number in the warning.
-> ^(BODY LBRACE $a*);
selector
@init {enterRule(retval, RULE_SELECTOR);}
: ((combinator)=>a+=combinator | )
(a+=simpleSelector | a+=nestedAppender)
(
((combinator)=>a+=combinator | )
(a+=simpleSelector | a+=nestedAppender)
)*
-> ^(SELECTOR $a* )
;
finally { leaveRule(); }
simpleSelector
: ( (a+=elementName ( {!predicates.onEmptyCombinator(input)}?=>a+=elementSubsequent)*)
| (a+=elementSubsequent ( {!predicates.onEmptyCombinator(input)}?=>a+=elementSubsequent)*)
)
-> ^(SIMPLE_SELECTOR $a*)
;
hashOrCssClass
: HASH -> ^(ELEMENT_SUBSEQUENT ^(ID_SELECTOR HASH))
| cssClass -> ^(ELEMENT_SUBSEQUENT cssClass)
;
attribOrPseudo
: attrib -> ^(ELEMENT_SUBSEQUENT attrib)
| pseudo -> ^(ELEMENT_SUBSEQUENT pseudo)
;
elementSubsequent
: hashOrCssClass
| attribOrPseudo
;
//A class name can be also a number e.g., .56 or .5cm or anything else that starts with ..
//unfortunately, those can be turned into numbers by lexer. This feels like an ugly hack,
//but I do not know how to solve the problem otherwise.
cssClass
: DOT IDENT -> ^(CSS_CLASS IDENT)
| DOT NUMBER -> ^(CSS_CLASS NUMBER)
| DOT EMS -> ^(CSS_CLASS EMS)
| DOT EXS -> ^(CSS_CLASS EXS)
| DOT LENGTH -> ^(CSS_CLASS LENGTH)
| DOT ANGLE -> ^(CSS_CLASS ANGLE)
| DOT TIME -> ^(CSS_CLASS TIME)
| DOT FREQ -> ^(CSS_CLASS FREQ)
| DOT REPEATER -> ^(CSS_CLASS REPEATER)
| DOT PERCENTAGE -> ^(CSS_CLASS PERCENTAGE)
| DOT UNKNOWN_DIMENSION -> ^(CSS_CLASS UNKNOWN_DIMENSION)
;
elementName
: (IDENT -> ^(ELEMENT_NAME IDENT))
| (STAR -> ^(ELEMENT_NAME STAR))
| (NUMBER -> ^(ELEMENT_NAME NUMBER))
| (EMS -> ^(ELEMENT_NAME EMS))
| (EXS -> ^(ELEMENT_NAME EXS))
| (LENGTH -> ^(ELEMENT_NAME LENGTH))
| (ANGLE -> ^(ELEMENT_NAME ANGLE))
| (TIME -> ^(ELEMENT_NAME TIME))
| (FREQ -> ^(ELEMENT_NAME FREQ))
| (REPEATER -> ^(ELEMENT_NAME REPEATER))
| (PERCENTAGE -> ^(ELEMENT_NAME PERCENTAGE))
| (UNKNOWN_DIMENSION -> ^(ELEMENT_NAME UNKNOWN_DIMENSION))
;
attrib
: (LBRACKET
a+=IDENT
(
(
a+=OPEQ
| a+=INCLUDES
| a+=DASHMATCH
| a+=PREFIXMATCH
| a+=SUFFIXMATCH
| a+=SUBSTRINGMATCH
)
(
a+=IDENT
| a+=STRING
| a+=NUMBER
)
)?
RBRACKET)
-> ^(ATTRIBUTE $a* )
;
pseudo
: (c+=COLON c+=COLON? a=IDENT ((
{ predicates.insideNth(input)}?=> LPAREN (b1=nth| b2=variablereference) RPAREN
| LPAREN b3=pseudoparameters RPAREN
)?)
) -> ^(PSEUDO $c+ $a $b1* $b2* $b3*)
;
pseudoparameters:
(IDENT) => IDENT
| (NUMBER) => NUMBER
| (variablereference) => variablereference
| selector
;
//TODO: add special error message here
nth: ((a+=PLUS | a+=MINUS)? (a+=REPEATER | a+=IDENT) ((b+=PLUS | b+=MINUS) b+=NUMBER)?
| (b+=PLUS | b+=MINUS)? b+=NUMBER)
-> ^(NTH ^(TERM $a*) ^(TERM $b*));
namespaceReference
@init {enterRule(retval, RULE_NAMESPACE_REFERENCE);}
: (a+=reusableStructureName GREATER)* c+=mixinReferenceWithSemi -> ^(NAMESPACE_REFERENCE $a* $c);
finally { leaveRule(); }
namespaceReferenceWithSemi
@init {enterRule(retval, RULE_NAMESPACE_REFERENCE);}
: (a+=reusableStructureName GREATER)* c+=mixinReference SEMI -> ^(NAMESPACE_REFERENCE $a* $c);
finally { leaveRule(); }
mixinReference
@init {enterRule(retval, RULE_MIXIN_REFERENCE);}
: a=reusableStructureName (LPAREN b=mixinReferenceArguments? RPAREN)? c=IMPORTANT_SYM?
-> ^(MIXIN_REFERENCE $a $b* $c*)
;
finally { leaveRule(); }
mixinReferenceWithSemi
@init {enterRule(retval, RULE_MIXIN_REFERENCE);}
: a=reusableStructureName (LPAREN b=mixinReferenceArguments? RPAREN)? c=IMPORTANT_SYM? SEMI
-> ^(MIXIN_REFERENCE $a $b* $c*)
;
finally { leaveRule(); }
mixinReferenceArguments
: a+=mixinReferenceArgument ( COMMA a+=mixinReferenceArgument)*
-> $a*
;
mixinReferenceArgument
: mathExprHighPrior | variabledeclarationLimitedNoSemi
;
reusableStructureName
: hashOrCssClass;
//we can loose parentheses, because comments inside mixin definition are going to be lost anyway
reusableStructure
@init {enterRule(retval, RULE_ABSTRACT_MIXIN_OR_NAMESPACE);}
: a=reusableStructureName LPAREN c=reusableStructureArguments? RPAREN e=reusableStructureGuards? f=ruleset_body
-> ^(REUSABLE_STRUCTURE $a $c* $e* $f)
;
finally { leaveRule(); }
reusableStructureGuards
: ({predicates.isWhenKeyword(input.LT(1))}?) IDENT b+=guard (COMMA d+=guard)*
-> $b $d*
;
guard
: a=guardCondition (b+=IDENT c+=guardCondition)*
-> ^(GUARD $a* ($b $c)*)
;
guardCondition
: a+=IDENT? LPAREN b+=mathExprHighPrior (b+=compareOperator b+=mathExprHighPrior)? RPAREN
-> ^(GUARD_CONDITION $a* $b*)
;
compareOperator
: GREATER | GREATER_OR_EQUAL | OPEQ | LOWER_OR_EQUAL | LOWER
;
//It is OK to loose commas, because mixins are going to be lost anyway
reusableStructureArguments
: a+=reusableStructureArgument ( COMMA a+=reusableStructureArgument)* (COMMA b+=DOT3)?
-> $a* $b*
| DOT3
;
reusableStructureArgument
: reusableStructureParameter
| reusableStructurePattern
;
reusableStructurePattern
: (( a+=unaryOperator? (a+=value_term)
) | (
a+=unsigned_value_term
| a+=hexColor
))
-> ^(MIXIN_PATTERN ^(TERM $a*))
;
//The expr is optional, because less.js supports this: "margin: ;" I do not know why, but they have it in
//their unit tests (so it was intentional)
declaration
@init {enterRule(retval, RULE_DECLARATION);}
: property COLON expr? prio? -> ^(DECLARATION property expr? prio?)
;
finally { leaveRule(); }
//I had to do this to put semicolon as a last member of the declaration subtree - comments would be messed up otherwise.
declarationWithSemicolon
@init {enterRule(retval, RULE_DECLARATION);}
: property COLON expr? prio? SEMI -> ^(DECLARATION property expr? prio?)
;
finally { leaveRule(); }
prio
: IMPORTANT_SYM
;
operator
: COMMA
| ({predicates.onEmptySeparator(input)}?=> -> EMPTY_SEPARATOR)
;
mathOperatorHighPrior
: SOLIDUS //ratio in pure CSS
| STAR
;
mathOperatorLowPrior
: PLUS
| MINUS
;
expr
@init {enterRule(retval, RULE_EXPRESSION);}
: a=mathExprHighPrior (b+=operator c+=mathExprHighPrior)* -> ^(EXPRESSION $a ($b $c)*)
;
finally { leaveRule(); }
mathExprHighPrior
@init {enterRule(retval, RULE_EXPRESSION);}
: a=mathExprLowPrior (b+=mathOperatorLowPrior c+=mathExprLowPrior)* -> ^(EXPRESSION $a ($b $c)*)
;
finally { leaveRule(); }
mathExprLowPrior
@init {enterRule(retval, RULE_EXPRESSION);}
: a=term (b+=mathOperatorHighPrior c+=term)* -> ^(EXPRESSION $a ($b $c)*)
;
finally { leaveRule(); }
term
: (( a+=unaryOperator? (a+=value_term
| ({predicates.onFunctionStart(input)}?=> a+=function)
| a+=expr_in_parentheses
| a+=variablereference
)
) | (a+=unsigned_value_term
| a+=hexColor
| a+=special_function))
-> ^(TERM $a*)
;
expr_in_parentheses
: LPAREN expr RPAREN -> ^(EXPRESSION_PARENTHESES LPAREN expr RPAREN );
value_term
: NUMBER
| PERCENTAGE
| LENGTH
| EMS
| EXS
| ANGLE
| UNKNOWN_DIMENSION
| TIME
| FREQ
;
unsigned_value_term
: STRING
| IDENT
;
special_function
: URI
;
hexColor
: HASH -> HASH
;
function
: a=IDENT LPAREN b=functionParameter RPAREN -> ^(TERM_FUNCTION $a $b*)
;
functionParameter
: IDENT OPEQ term -> ^(OPEQ IDENT term)
| expr;
// ==============================================================
// LEXER
//
// The lexer follows the normative section of WWW standard as closely
// as it can. For instance, where the ANTLR lexer returns a token that
// is unambiguous for both ANTLR and lex (the standard defines tokens
// in lex notation), then the token names are equivalent.
//
// Note however that lex has a match order defined as top to bottom
// with longest match first. This results in a fairly inefficent, match,
// REJECT, match REJECT set of operations. ANTLR lexer grammars are actaully
// LL grammars (and hence LL recognizers), which means that we must
// specifically disambiguate longest matches and so on, when the lex
// like normative grammar results in ambiguities as far as ANTLR is concerned.
//
// This means that some tokens will either be combined compared to the
// normative spec, and the paresr will recognize them for what they are.
// In this case, the token will named as XXX_YYY where XXX and YYY are the
// token names used in the specification.
//
// Lex style macro names used in the spec may sometimes be used (in upper case
// version) as fragment rules in this grammar. However ANTLR fragment rules
// are not quite the same as lex macros, in that they generate actual
// methods in the recognizer class, and so may not be as effecient. In
// some cases then, the macro contents are embedded. Annotation indicate when
// this is the case.
//
// See comments in the rules for specific details.
// --------------------------------------------------------------
//
// N.B. CSS 2.1 is defined as case insensitive, but because each character
// is allowed to be written as in escaped form we basically define each
// character as a fragment and reuse it in all other rules.
// ==============================================================
fragment EMPTY_COMBINATOR: ;
// --------------------------------------------------------------
// Define all the fragments of the lexer. These rules neither recognize
// nor create tokens, but must be called from non-fragment rules, which
// do create tokens, using these fragments to either purely define the
// token number, or by calling them to match a certain portion of
// the token string.
//
fragment HEXCHAR : ('a'..'f'|'A'..'F'|'0'..'9') ;
fragment NONASCII : '\u0080'..'\uFFFF' ; // NB: Upper bound should be \u4177777
fragment UNICODE : '\\' HEXCHAR
(HEXCHAR
(HEXCHAR
(HEXCHAR
(HEXCHAR HEXCHAR?)?
)?
)?
)?
('\r'|'\n'|'\t'|'\f'|' ')* ;
fragment ESCAPE : UNICODE | '\\' ~('\r'|'\n'|'\f'|HEXCHAR) ;
fragment NMSTART : '_'
| 'a'..'z'
| 'A'..'Z'
| NONASCII
| ESCAPE
;
fragment NMCHAR : '_'
| 'a'..'z'
| 'A'..'Z'
| '0'..'9'
| '-'
| NONASCII
| ESCAPE
;
fragment NAME : NMCHAR+ ;
fragment UNKNOWN_DIMENSION : NMSTART NMCHAR* ;
// The original URL did not allowed characters, '.', '=', ':', ';', ',' and so on
// I added those characters that appear in less.js css test case.
fragment URL : (
'['|'!'|'#'|'$'|'%'|'&'|'*'|'~'|'/'|'.'|'='|':'|';'|','|'\r'|'\n'|'\t'|' '|'+'
| NMCHAR
)*
;
// Basic Alpha characters in upper, lower and escaped form. Note that
// whitespace and newlines are unimportant even within keywords. We do not
// however call a further fragment rule to consume these characters for
// reasons of performance - the rules are still eminently readable.
//
fragment A : ('a'|'A') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\' ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')'1'
;
fragment B : ('b'|'B') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\' ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')'2'
;
fragment C : ('c'|'C') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\' ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')'3'
;
fragment D : ('d'|'D') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\' ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')'4'
;
fragment E : ('e'|'E') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\' ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')'5'
;
fragment F : ('f'|'F') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\' ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')'6'
;
fragment G : ('g'|'G') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'g'
| 'G'
| ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')'7'
)
;
fragment H : ('h'|'H') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'h'
| 'H'
| ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')'8'
)
;
fragment I : ('i'|'I') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'i'
| 'I'
| ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')'9'
)
;
fragment J : ('j'|'J') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'j'
| 'J'
| ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')('A'|'a')
)
;
fragment K : ('k'|'K') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'k'
| 'K'
| ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')('B'|'b')
)
;
fragment L : ('l'|'L') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'l'
| 'L'
| ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')('C'|'c')
)
;
fragment M : ('m'|'M') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'm'
| 'M'
| ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')('D'|'d')
)
;
fragment N : ('n'|'N') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'n'
| 'N'
| ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')('E'|'e')
)
;
fragment O : ('o'|'O') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'o'
| 'O'
| ('0' ('0' ('0' '0'?)?)?)? ('4'|'6')('F'|'f')
)
;
fragment P : ('p'|'P') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'p'
| 'P'
| ('0' ('0' ('0' '0'?)?)?)? ('5'|'7')('0')
)
;
fragment Q : ('q'|'Q') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'q'
| 'Q'
| ('0' ('0' ('0' '0'?)?)?)? ('5'|'7')('1')
)
;
fragment R : ('r'|'R') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'r'
| 'R'
| ('0' ('0' ('0' '0'?)?)?)? ('5'|'7')('2')
)
;
fragment S : ('s'|'S') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
's'
| 'S'
| ('0' ('0' ('0' '0'?)?)?)? ('5'|'7')('3')
)
;
fragment T : ('t'|'T') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
't'
| 'T'
| ('0' ('0' ('0' '0'?)?)?)? ('5'|'7')('4')
)
;
fragment U : ('u'|'U') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'u'
| 'U'
| ('0' ('0' ('0' '0'?)?)?)? ('5'|'7')('5')
)
;
fragment V : ('v'|'V') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
( 'v'
| 'V'
| ('0' ('0' ('0' '0'?)?)?)? ('5'|'7')('6')
)
;
fragment W : ('w'|'W') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'w'
| 'W'
| ('0' ('0' ('0' '0'?)?)?)? ('5'|'7')('7')
)
;
fragment X : ('x'|'X') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'x'
| 'X'
| ('0' ('0' ('0' '0'?)?)?)? ('5'|'7')('8')
)
;
fragment Y : ('y'|'Y') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'y'
| 'Y'
| ('0' ('0' ('0' '0'?)?)?)? ('5'|'7')('9')
)
;
fragment Z : ('z'|'Z') ('\r'|'\n'|'\t'|'\f'|' ')*
| '\\'
(
'z'
| 'Z'
| ('0' ('0' ('0' '0'?)?)?)? ('5'|'7')('A'|'a')
)
;
// -------------
// Comments. Comments may not be nested, may be multilined and are delimited
// like C comments: /* ..... */
// COMMENTS are hidden from the parser which simplifies the parser
// grammar a lot.
//
COMMENT : '/*' ( options { greedy=false; } : .*) '*/'
{
$channel = HIDDEN; // Comments on channel 2 in case we want to find them
}
;
//TODO: document: annoying - causes antlr null pointer exception during generation
//catch in lexer is impossible
//COMMENT_LITTLE : '//' ~(NL)* { $channel = HIDDEN; };
//COMMENT_LITTLE : '//' (( options { greedy=false; } : .* ) NL) { $channel = HIDDEN; };
COMMENT_LITTLE : '//' (~('\r\n'|'\n'))* { $channel = HIDDEN; };
//COMMENT_LITTLE : '//' (
// (( options { greedy=false; } : .* ) NL)=>(( options { greedy=false; } : .* ) NL)
// | (( options { greedy=false; } : .* ) EOF)=>(( options { greedy=false; } : .* ) EOF)
//) { $channel = HIDDEN; };
// ---------------------
// HTML comment open. HTML/XML comments may be placed around style sheets so that they
// are hidden from higher scope parsing engines such as HTML parsers.
// They comment open is therfore ignored by the CSS parser and we hide
// it from the ANLTR parser.
//
CDO : '<!--'
{
$channel = 3; // CDO on channel 3 in case we want it later
}
;
// ---------------------
// HTML comment close. HTML/XML comments may be placed around style sheets so that they
// are hidden from higher scope parsing engines such as HTML parsers.
// They comment close is therfore ignored by the CSS parser and we hide
// it from the ANLTR parser.
//
CDC : '-->'
{
$channel = 4; // CDC on channel 4 in case we want it later
}
;
INCLUDES : '~=' ;
DASHMATCH : '|=' ;
PREFIXMATCH : '^=' ;
SUFFIXMATCH : '$=' ;
SUBSTRINGMATCH : '*=' ;
TILDE : '~' ;
GREATER : '>' ;
GREATER_OR_EQUAL : '>=' ;
LOWER : '<' ;
LOWER_OR_EQUAL : '=<' ;
LBRACE : '{' ;
RBRACE : '}' ;
LBRACKET : '[' ;
RBRACKET : ']' ;
OPEQ : '=' ;
SEMI : ';' ;
COLON : ':' ;
SOLIDUS : '/' ;
MINUS : '-' ;
PLUS : '+' ;
STAR : '*' ;
LPAREN : '(' ;
RPAREN : ')' ;
COMMA : ',' ;
DOT : '.' ;
DOT3 : '...' ;
//TODO: this trick could be used in other places too. I may be able to avoid some predicates and token position comparisons!
fragment APPENDER_FRAGMENT: '&';