Not skip whitespace

72 views
Skip to first unread message

Mohan Radhakrishnan

unread,
Jun 24, 2016, 5:11:21 AM6/24/16
to antlr-discussion
Hi,

          The Java grammar skips whitespaces ? I am losing comments and code formatting. How can I keep them and use them ?

So, for example, I want to keep the empty lines in this code.

package com.nationwide.pc.is.tpi.web.action;

import java.util.Enumeration;

Java.g4 has this.

WS  :  [ \t\r\n\u000C]+ -> skip
;

COMMENT
: '/*' .*? '*/' -> skip
;

LINE_COMMENT
: '//' ~[\r\n]* -> skip
;

Thanks,
Mohan

Jim Idle

unread,
Jun 24, 2016, 6:18:35 AM6/24/16
to antlr-discussion
Look in to lexer channels. You can send the whitespace et al to a different channel that the Parser will ignore but are available to you to render. 





--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mohan Radhakrishnan

unread,
Jun 24, 2016, 7:44:11 AM6/24/16
to antlr-discussion
WS  :  [ \t\r\n\u000C]+ -> channel(HIDDEN)
;

I tried this.
The standard code I find by searching is this. Whether I use DEFAULT_CHANNEL or HIDDEN_CHANNEL I still see the code. I thought DEFAULT_CHANNEL 
should have only code without whitespaces and HIDDEN_CHANNEL should have only spaces.

public void enterMethodDeclaration( JavaParser.MethodDeclarationContext ctx){

ParserRuleContext parent = ctx.getParent();

while (parent!=null && !(parent instanceof JavaParser.ClassBodyDeclarationContext)) {
parent = parent.getParent();
}
if (parent == null) {
parent = ctx;
}
Token start = parent.start;
// work backwards in token stream to include all preceding whitespace and comments
int tokenIndex = start.getTokenIndex();
while ( tokenIndex > 0 && tokens.get(tokenIndex-1).getChannel() != Token.DEFAULT_CHANNEL ) {
tokenIndex--;
}
int startIndex = start.getStartIndex();
int endIndex = ctx.stop.getStopIndex();
Interval interval = new Interval(startIndex,endIndex);
}

Mohan

Mohan Radhakrishnan

unread,
Jul 8, 2016, 2:24:29 AM7/8/16
to antlr-di...@googlegroups.com
Hi,

      Is my approach right ? Can anyone nudge in the right direction ?

Update : This works.

((CommonTokenStream) tokens).getHiddenTokensToLeft( classOrInterfaceModifierContext.getStart().getTokenIndex(),
Token.HIDDEN_CHANNEL);

I get the comments and whitespaces before each method.

Thanks,
Mohan

Niels Basjes

unread,
Jul 9, 2016, 9:09:10 AM7/9/16
to antlr-discussion
Have a look at what I did https://github.com/nielsbasjes/yauaa/blob/SPACE_Skip/src/main/java/nl/basjes/parse/useragent/utils/AntlrUtils.java#L32
Perhaps this helps ?

Niels

Op vrijdag 8 juli 2016 08:24:29 UTC+2 schreef Mohan Radhakrishnan:

miwor...@gmail.com

unread,
Aug 15, 2016, 5:31:25 AM8/15/16
to antlr-discussion
Page not found,can you fix the address,thanks.

在 2016年7月9日星期六 UTC+8下午9:09:10,Niels Basjes写道:

Niels Basjes

unread,
Aug 15, 2016, 9:56:20 AM8/15/16
to antlr-di...@googlegroups.com

--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/YWsVT-ntD1U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussion+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Best regards / Met vriendelijke groeten,

Niels Basjes
Reply all
Reply to author
Forward
0 new messages