Skip to content

Commit

Permalink
Update on Antlr grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-dean committed Apr 4, 2023
1 parent a2bbb1d commit 0b6dc34
Showing 1 changed file with 92 additions and 22 deletions.
114 changes: 92 additions & 22 deletions packages/less/src/parser-antlr/CssLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,10 @@ fragment Newline
: '\\n' | '\\r' '\\n'? | '\\f'
;

fragment WS
fragment Whitespace
: ' ' | '\\t' | Newline
;

/**
* Uses nongreedy wildcard
* @see https://github.com/antlr/antlr4/blob/master/doc/wildcard.md
*/
fragment Comment
: '/*' .*? '*/'
;

fragment Digit
: '0'..'9'
;
Expand All @@ -34,7 +26,7 @@ fragment Hex
;

fragment Unicode
: '\\' Hex (Hex Hex Hex Hex Hex)? WS? // 1 or 6 hex digits
: '\\' Hex (Hex Hex Hex Hex Hex)? Whitespace? // 1 or 6 hex digits
;

fragment Escape
Expand All @@ -50,10 +42,6 @@ fragment SingleString
: '\'' ('\\' | ~[\n\r\f'] | Newline | Escape) '\''
;

fragment String
: DoubleString | SingleString
;

fragment NonAscii
: '\u0240'..'\uffff'
;
Expand Down Expand Up @@ -87,7 +75,7 @@ fragment UrlFragment
;

fragment Integer
: ('+' | '-')? ('0'..'9')+
: ('+' | '-')? Digit+
;

fragment Exp
Expand All @@ -103,30 +91,112 @@ fragment Number
)
;

fragment WSorComment
: (WS | Comment)*
fragment Gt : '>';

fragment NthFunctions
: 'nth-child'
| 'nth-last-child'
| 'nth-of-type'
| 'nth-last-of-type'
;
/**
* TOKENS
* - default mode should look for selectors and at-rule starts
* - selector mode should look for selectors and '{'
* - at-rule mode should look for any token and blocks and outer '{'
* - declaration list mode should look for idents and at-rule starts and selectors (nesting)
* - declaration mode should look for valid value tokens and ';'
* - custom property mode should look for any token and blocks and outer ';'
*/

fragment Gt : '>';
/**
* Uses nongreedy wildcard
* @see https://github.com/antlr/antlr4/blob/master/doc/wildcard.md
*/
Comment : '/*' .*? '*/' -> skip;

CompareOp : (Gt | '<') '='?;
/**
* Aliased because Less will not skip CSS comments
* e.g. (Whitespace | Comment)
*/
WS : Whitespace;
Comma : ',';
String : DoubleString | SingleString;
LCurly : '{';
RCurly : '}';
LParen : '(';
RParen : ')';
LSquare : '[';
RSquare : ']';

/**
* CSS syntax says we should identify integers as separate from numbers,
* probably because there are parts of the syntax where one is allowed but not the other?
*/
UnitlessNum : Number;
UnsignedInt : Digit+;
SignedInt : Integer;

/** Simple Selectors */
Star : '*' -> mode(Selector);
Ampersand : '&' -> mode(Selector);
ID : '#' Ident -> mode(Selector);
Class : '.' Ident -> mode(Selector);
Element : Ident -> mode(Selector);

NthSyntax : 'odd' | 'even' | Integer | Integer? [nN] (WS* [+-] WS* Digit)?;
NthChild : ':' NthFunctions '(' WS* NthSyntax WS* ')';
Pseudo : ':' ':'? Ident ('(' .*? ')')? -> mode(Selector);
/** @todo - lookup */
Attribute : LSquare WS* Ident ('=') WS* (Ident | String) WS* () RSquare -> mode(Selector);


mode Selector;
Selector_WS : WS -> type(WS);
Selector_Comma : Comma -> type(Comma);

Selector_ID : ID -> type(ID);
Selector_Class : Class -> type(Class);
Selector_Element : Element -> type(Element);
Selector_Pseudo : Pseudo -> type(Pseudo);
Selector_Star : Star -> type(Star);
Selector_Amp : Ampersand -> type(Ampersand);
Selector_Attribute: Attribute -> type(Attribute);
Selector_LCurly : LCurly -> type(LCurly), pushMode(DeclarationList);

mode DeclarationList;


ColorIdentStart : '#' [a-f];

CompareOp : (Gt | '<') '='?;

Semi : ';';
Colon : ':';
PropAssign : Colon;

Plus : '+';
Minus : '-';
Comma : ',';
Divide : '/';
Eq : '=';
Star : '*';
Tilde : '~';
/** a namespace or column combinator */
Pipe : '|';

Combinator : Plus | Gt | Tilde | Pipe;
AttrMatch : [*~|^$] '=';
PlainIdent : Ident;
CustomProp : '--' Ident;

Cdo : '<!--' -> skip;
Cdc : '-->' -> skip;

/** Ignore BOM */
UnicodeBOM : '\uFFFE' -> skip;

AttrFlag : [is];
Function : Ident '(';

Combinator : Plus | Gt | Tilde;



Expand Down

0 comments on commit 0b6dc34

Please sign in to comment.