[antlr-interest] Detecting a space as a token

Jim Idle jimi at temporal-wave.com
Mon Jan 18 14:45:29 PST 2010


All you need do is use a predicate at the DOT, which is where the esPred rule is. You can change the syntactic predicate to a semantic predicate and check for #, ., ( and : via input.LT() but can also look at the previous token even if off channel to make sure it is not a space:

simpleSelector
	: elementName 
		({ mySemPred() }?=>elementSubsequent)*
		
	| ({ mySemPred() }?=>elementSubsequent)+
	;
@parser:members {

boolean mySemPred() {
switch (input.LA(1)) {
   case DOT:
	// Only if no preceding spaces (but is that correct for CSS?
	//
	if ((TokenStream)input).get( input.index()-1 ).getType() != WS) { return true; } else {return false; }
	break;
   case HASH:
   case LBRACKET:
   case COLON:
      return true;
   default:
      return false;
 }
}   

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of David Grieve
> Sent: Monday, January 18, 2010 12:29 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Detecting a space as a token
> 
> In CSS, a selector is (roughly) a sequence of simple selectors joined
> by a combinator. http://www.antlr.org/grammar/1240941192304/css21.g has
> the following rules which correspond to this.
> 
> combinator
> 	: PLUS
> 	| GREATER
> 	|
> 	;
> 
> selector
> 	: simpleSelector (combinator simpleSelector)*
> 	;
> 
> The issue I'm having is how to handle the combinator which is a space
> in the selector rule. Specifically, I should be able to parse
> 
> 	A .b
> 
> as two simple selectors: A and .b. However, since whitespace is
> ignored, this is getting parsed as one selector. The following parses
> as desired:
> 
> 	A *.b
> 
> Using the universal selector as part of the second simple selector is a
> workaround that I shouldn't have to employ.
> 
> How can I parse "A<space>.b" such that the space is recognized as a
> combinator? Thanks in advance for any help!
> David Grieve
> Sun Microsystems, Inc.
> 
> 
> 
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address





More information about the antlr-interest mailing list