victor
victor

Reputation: 1676

antlr match parser rule instead of token

I'm trying to match the following sequences:

var1.x , "cssStyleName : styleValue";

cssStyleName and styleValue are ... well... css compliant styles, e.g. color: #000 !important; *width: 99.94680851063829%;" etc.

inputSpecs: modelVariable (COMMA  cssStyle )*;

modelVariable: TEXT ('.' childVar)*;

childVar : TEXT; 

cssStyle:  styleName COLON styleValue+ SEMICOLON;

styleName : ASTERISK? TEXT ;

styleValue :  TEXT | STYLE_VALUE;



COLON: ':';
SEMICOLON: ';';
ASTERISK: '*';
COMMA: ',';
TEXT : ('a'..'z'|'A'..'Z'| '_' | '-' | '0'..'9')+ ;
STYLE_VALUE: ('a'..'z'|'A'..'Z'| '_' | '-' | '0'..'9' | '%' |'#' |'.')+ '!important'?;

The problem lies in declaring the '.' at the end of the STYLE_VALUE token. When I declare it, I get NoViableAltException right after parsing var1.x, probably due to the fact that modelVariable is matched as a STYLE_VALUE and not as a TEXT.

How can I have a subset of TEXT (STYLE_VALUE) be matched as *STYLE_VALUE* and '.' TEXT to be matched as TEXT with a dot in front of it?

Upvotes: 0

Views: 151

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 99859

If you want the lexer to treat var1.x as 3 separate tokens (var1, ., and x), then you need to make sure that no lexer rule can include the . with either of the two other tokens. As a start, you'll need to remove the STYLE_VALUE lexer rule. You'll also want to create the following lexer rule, and '.' should not appear in any other lexer rule.

DOT : '.';

Instead of handling the syntax of the style value in the lexer, you'll need to handle it in the parser to ensure that DOT is always treated properly.

Upvotes: 1

Related Questions