if (tree_option) System.out.println(t.toStringTree());
rewrite(g.getTreeAdaptor(),t,g.getTokenNames());
}
public void rewrite(TreeAdaptor adaptor, CommonTree t, String[] tokenNames) throws Exception {
TreeWizard wiz = new TreeWizard(adaptor, tokenNames);
// ACTIONS STUFF
wiz.visit(t, ANTLRv3Parser.ACTION,
new TreeWizard.Visitor() {
@Override
public void visit(Object t) { ACTION(tokens, (CommonTree)t); }
});
wiz.visit(t, ANTLRv3Parser.AT, // ^('@' id ACTION) rule actions
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
CommonTree a = (CommonTree)t;
CommonTree action = null;
if ( a.getChildCount()==2 ) action = (CommonTree)a.getChild(1);
else if ( a.getChildCount()==3 ) action = (CommonTree)a.getChild(2);
if ( action.getType()==ANTLRv3Parser.ACTION ) {
tokens.delete(a.getTokenStartIndex(),
a.getTokenStopIndex());
killTrailingNewline(tokens, action.getTokenStopIndex());
}
}
});
wiz.visit(t, ANTLRv3Parser.ARG, // wipe rule arguments
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
CommonTree a = (CommonTree)t;
a = (CommonTree)a.getChild(0);
tokens.delete(a.token.getTokenIndex());
killTrailingNewline(tokens, a.token.getTokenIndex());
}
});
wiz.visit(t, ANTLRv3Parser.RET, // wipe rule return declarations
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
CommonTree a = (CommonTree)t;
CommonTree ret = (CommonTree)a.getChild(0);
tokens.delete(a.token.getTokenIndex(),
ret.token.getTokenIndex());
}
});
wiz.visit(t, ANTLRv3Parser.SEMPRED, // comment out semantic predicates
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
CommonTree a = (CommonTree)t;
tokens.replace(a.token.getTokenIndex(), "/*"+a.getText()+"*/");
}
});
wiz.visit(t, ANTLRv3Parser.GATED_SEMPRED, // comment out semantic predicates
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
CommonTree a = (CommonTree)t;
String text = tokens.toString(a.getTokenStartIndex(),
a.getTokenStopIndex());
tokens.replace(a.getTokenStartIndex(),
a.getTokenStopIndex(),
"/*"+text+"*/");
}
});
wiz.visit(t, ANTLRv3Parser.SCOPE, // comment scope specs
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
CommonTree a = (CommonTree)t;
tokens.delete(a.getTokenStartIndex(),
a.getTokenStopIndex());
killTrailingNewline(tokens, a.getTokenStopIndex());
}
});
wiz.visit(t, ANTLRv3Parser.ARG_ACTION, // args r[x,y] -> ^(r [x,y])
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
CommonTree a = (CommonTree)t;
if ( a.getParent().getType()==ANTLRv3Parser.RULE_REF ) {
tokens.delete(a.getTokenStartIndex(),
a.getTokenStopIndex());
}
}
});
wiz.visit(t, ANTLRv3Parser.LABEL_ASSIGN, // ^('=' id ^(RULE_REF [arg])), ...
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
CommonTree a = (CommonTree)t;
if ( !a.hasAncestor(ANTLRv3Parser.OPTIONS) ) { // avoid options
CommonTree child = (CommonTree)a.getChild(0);
tokens.delete(a.token.getTokenIndex()); // kill "id="
tokens.delete(child.token.getTokenIndex());
}
}
});
wiz.visit(t, ANTLRv3Parser.LIST_LABEL_ASSIGN, // ^('+=' id ^(RULE_REF [arg])), ...
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
CommonTree a = (CommonTree)t;
CommonTree child = (CommonTree)a.getChild(0);
tokens.delete(a.token.getTokenIndex()); // kill "id+="
tokens.delete(child.token.getTokenIndex());
}
});
// AST STUFF
wiz.visit(t, ANTLRv3Parser.REWRITE,
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
CommonTree a = (CommonTree)t;
CommonTree child = (CommonTree)a.getChild(0);
int stop = child.getTokenStopIndex();
if ( child.getType()==ANTLRv3Parser.SEMPRED ) {
CommonTree rew = (CommonTree)a.getChild(1);
stop = rew.getTokenStopIndex();
}
tokens.delete(a.token.getTokenIndex(), stop);
killTrailingNewline(tokens, stop);
}
});
wiz.visit(t, ANTLRv3Parser.ROOT,
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
tokens.delete(((CommonTree)t).token.getTokenIndex());
}
});
wiz.visit(t, ANTLRv3Parser.BANG,
new TreeWizard.Visitor() {
@Override
public void visit(Object t) {
tokens.delete(((CommonTree)t).token.getTokenIndex());
}