java - Traversal of tokens using ParserRuleContext in listener - ANTLR4 -
while iterating on tokens using listener, know how use parserrulecontext peek @ next token or next few tokens in token stream?
in code below trying peek @ tokens after current token till eof:
@override public void entersemicolon(javaparser.semicoloncontext ctx) { token tok, semicolon = ctx.getstart(); int currentindex = semicolon.getstartindex(); int reqind = currentindex+1; tokensource toksrc= semicolon.gettokensource(); charstream srcstream = semicolon.getinputstream(); srcstream.seek(currentindex); while(true){ tok = toksrc.nexttoken() ; system.out.println(tok); if(tok.gettext()=="<eof>"){break;} srcstream.seek(reqind++); } }
but output is:
. . . . . [@-1,131:130='',<-1>,13:0] [@-1,132:131='',<-1>,13:0] [@-1,133:132='',<-1>,13:0] [@-1,134:133='',<-1>,13:0] [@-1,135:134='',<-1>,13:0] [@-1,136:135='',<-1>,13:0] [@-1,137:136='',<-1>,13:0] [@-1,138:137='',<-1>,13:0] [@-1,139:138='',<-1>,13:0] [@-1,140:139='',<-1>,13:0] [@-1,141:140='',<-1>,13:0] [@-1,142:141='',<-1>,13:0] [@-1,143:142='',<-1>,13:0] [@-1,144:143='',<-1>,13:0] [@-1,145:144='',<-1>,13:0] [@-1,146:145='',<-1>,13:0] [@-1,147:146='',<-1>,13:0] [@-1,148:147='',<-1>,13:0] [@-1,149:148='',<-1>,13:0] [@-1,150:149='',<-1>,13:0] [@-1,151:150='',<-1>,13:0] [@-1,152:151='',<-1>,13:0] [@-1,153:152='',<-1>,13:0] [@-1,154:153='',<-1>,13:0] [@-1,155:154='',<-1>,13:0] [@-1,156:155='',<-1>,13:0] [@-1,157:156='',<-1>,13:0] [@-1,158:157='',<-1>,13:0] [@-1,159:158='',<-1>,13:0] [@-1,160:159='',<-1>,13:0] [@-1,161:160='<eof>',<-1>,13:0] [@-1,137:136='',<-1>,13:0] [@-1,138:137='',<-1>,13:0] [@-1,139:138='',<-1>,13:0] [@-1,140:139='',<-1>,13:0] [@-1,141:140='',<-1>,13:0] [@-1,142:141='',<-1>,13:0] [@-1,143:142='',<-1>,13:0] [@-1,144:143='',<-1>,13:0] [@-1,145:144='',<-1>,13:0] [@-1,146:145='',<-1>,13:0] [@-1,147:146='',<-1>,13:0] [@-1,148:147='',<-1>,13:0] [@-1,149:148='',<-1>,13:0] [@-1,150:149='',<-1>,13:0] [@-1,151:150='',<-1>,13:0] [@-1,152:151='',<-1>,13:0] [@-1,153:152='',<-1>,13:0] [@-1,154:153='',<-1>,13:0] [@-1,155:154='',<-1>,13:0] [@-1,156:155='',<-1>,13:0] [@-1,157:156='',<-1>,13:0] [@-1,158:157='',<-1>,13:0] [@-1,159:158='',<-1>,13:0] [@-1,160:159='',<-1>,13:0] [@-1,161:160='<eof>',<-1>,13:0] . . . .
we see although able traverse through tokens till eof, unable actual content or type of tokens. know if there neat way of doing using listener traversing.
hard certain,
tok = toksrc.nexttoken() ;
appears rerunning lexer, starting @ presumed proper token boundary, without having reset lexer. lexer throwing errors might explain observed behavior.
still, better approach recover existing token stream:
public class walker implements yourjavalistener { commontokenstream tokens; public walker(javaparser parser) { tokens = (commontokenstream) parser.gettokenstream() }
then access stream desired tokens:
@override public void entersemicolon(javaparser.semicoloncontext ctx) { terminalnode semi = ctx.semicolon(); // adjust needed impl. token tok = semi.getsymbol(); int idx = tok.gettokenindex(); while(tok.gettype() != intstream.eof) { system.out.println(tok); tok = tokens.get(idx++); } }
an entirely different approach might serve ultimate purpose limited set of tokens directly parent context:
parserrulecontext pctx = ctx.getparent(); list<terminalnode> nodes = pctx.gettokens(pctx.getstart(), pctx.getstop());
Comments
Post a Comment