GammaVega
GammaVega

Reputation: 779

ANTLR - Handle whitespace in identifier

I am trying to build simple search expression, and couldn't get right answer to below grammar.

Here are my sample search text 
LOB WHERE 
Line of Business WHERE 
Line of     Business WHERE 

As you can see in above search, first few words reflect search keyword followed by where condition, i want to capture search keyword that can include whitespace. Sharing following sample grammar but doesn't seems to parse properly

sqlsyntax : identifierws 'WHERE';  
identifierws :  (WSID)+;
WSID: [a-zA-Z0-9 ] ;      // match identifiers with space
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

Any help in this regard is appreciated.

This is what is happening when I try to parse Line of Business WHERE

I get following error line 1:0 no viable alternative at input 'Line' I get back LineofBusiness text but whitespace got trimmed, i want exact text Line of Business, that is where I am struggling a bit.

Upvotes: 0

Views: 462

Answers (2)

Mike Lischke
Mike Lischke

Reputation: 53307

For such a simple case I wouldn't use a parser. That's just overkill. All you need to do is to get the current position in the input then search for WHERE (a simple boyer-moore search). Then take the text between start position and WHERE position as your input. Jump over WHERE and set the start position to where you are then. After that do the same search again etc.

Upvotes: 0

GRosenberg
GRosenberg

Reputation: 5991

The identeriferws rule is consuming all text. Better to prioritize identification of keywords in the lexer:

sqlsyntax : identifierws WHERE identifierws EQ STRING EOF ;  
identifierws :  (WSID)+;
WHERE: 'WHERE';
EQ : '=' ;
STRING : '\'' .? '\'' ;
WSID: [a-zA-Z0-9 ] ;      
WS : [ \t\r\n]+ -> skip ;

Upvotes: 1

Related Questions