package tool.model.grammar;
import java.util.logging.Logger;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonToken;
import org.antlr.runtime.Lexer;
import org.antlr.runtime.TokenSource;
import org.antlr.runtime.UnbufferedTokenStream;
public class RemovableTokenStream extends UnbufferedTokenStream{
private static Logger log = Logger.getLogger(RemovableTokenStream.class.getName());
public RemovableTokenStream(TokenSource tokenSource){
super(tokenSource);
}
/**
delete all cached tokens that is following the given token.<br>
this method should be invoked in semantic predicates
@param k number of LookAhead
*/
public void cleanRestTokens(int k){
int curr = p + k -1;
CommonToken startToken = (CommonToken)data.get(curr);
int start = curr + 1;
if( start < data.size()){
log.info("p=" + p +
" token= [ "+ ((CommonToken)data.get(curr)).getStartIndex()
+ "-"+ ((CommonToken)data.get(curr)).getStopIndex() + "]" +
data.get(curr).getText());
data.subList(start, data.size()).clear();
CharStream cs = ((Lexer)getTokenSource()).getCharStream();
cs.seek(startToken.getStopIndex() + 1);
log.info("reset cs pos:"+ (startToken.getStopIndex() + 1) );
cs.setLine(startToken.getLine());
cs.setCharPositionInLine(startToken.getCharPositionInLine() +
(startToken.getStopIndex() + 1 - startToken.getStartIndex()));
}
}
}