How Do You Skip Whitespace In ANTLR Parser Rules?

32 Views Asked by At

I'm implementing an ANTLR grammar in which I want to completely ignore whitespace (other than newlines) in every context. Given that, I've included the following rule:

WS: [ \t]+ -> skip ;

However, when the rule fieldX: 'X:' INT NEWLINE; attempts to parse "X: 1", I still get the following error:

extraneous input ' ' expecting INT

My understanding is that I'm getting this error because fieldX is a parser rule, and my WS rule will only be effective for lexer rules. I looked at a few similar questions on here and the only concrete solution I could find was to just convert fieldX into a lexer rule. However, I have a number of similar rules that I would need to convert. Is there any way to get a parser rule to skip whitespace other than just converting it to a lexer rule?

1

There are 1 best solutions below

0
Coder1913 On

As sepp2k suggested, I had the following rule elsewhere in my grammar: TEXT: .?+;. I didn't think the wildcard character would match whitespace, but it does. By moving this rule to come after my WS rule, I was able to solve the issue.