don't reduce SETs if elements ref'd in -> rewrite

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8864]
This commit is contained in:
parrt 2011-07-16 11:49:14 -08:00
parent 43f4ef59f7
commit 65396e6d7c
2 changed files with 19 additions and 15 deletions

View File

@ -2,6 +2,7 @@ grammar T;
options {output=AST;}
tokens {DUH;}
a : (A|B) ;
b : (A|B) -> A ;
/*
type returns [int i] : ID;
ID : 'a'..'z'+ ;

View File

@ -12,6 +12,8 @@ package org.antlr.v4.parse;
import org.antlr.v4.misc.Utils;
import org.antlr.v4.tool.*;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.ArrayList;
import org.antlr.v4.runtime.misc.IntervalSet;
}
@ -19,6 +21,7 @@ import org.antlr.v4.runtime.misc.IntervalSet;
@members {
public String currentRuleName;
public GrammarAST currentAlt;
public Set<String> rewriteElems = new HashSet<String>();
}
topdown
@ -30,10 +33,19 @@ topdown
setAlt
: {inContext("RULE BLOCK")}?
( ALT {currentAlt = (AltAST)$start;}
| ALT_REWRITE {currentAlt = (AltAST)$start.getChild(0);}
( ALT {currentAlt = $start; rewriteElems.clear();}
| ALT_REWRITE {currentAlt = $start;}
{
IntervalSet s = new IntervalSet();
s.add(RULE_REF);
s.add(STRING_LITERAL);
s.add(TOKEN_REF);
List<GrammarAST> nodes = ((GrammarAST)(currentAlt.getChild(1))).getNodesWithType(s);
for (GrammarAST n : nodes) {rewriteElems.add(n.getText());}
System.out.println("stuff in rewrite: "+rewriteElems);
}
)
;
// (BLOCK (ALT (+ (BLOCK (ALT INT) (ALT ID)))))
@ -50,22 +62,13 @@ ebnfSuffix
blockSet
@init {
if ( currentAlt!=null && currentAlt.getType()==ANTLRParser.ALT_REWRITE ) {
IntervalSet s = new IntervalSet();
s.add(RULE_REF);
s.add(STRING_LITERAL);
s.add(TOKEN_REF);
List<GrammarAST> elems = currentAlt.getNodesWithType(s);
System.out.println("stuff in rewrite: "+elems);
}
}
: {Character.isLowerCase(currentRuleName.charAt(0)) &&
!inContext("ALT_REWRITE ...") && !inContext("RULE")}?
!inContext("RULE")}? // if non-lexer rule and not rule block
^(BLOCK ( ^(ALT setElement) )+) -> ^(SET[$BLOCK.token, "SET"] setElement+)
;
setElement
@after {$tree = new TerminalAST($start);}
: STRING_LITERAL
| TOKEN_REF
@after {$tree = new TerminalAST($start);} // elem can't be to right of ->
: {!rewriteElems.contains($start.getText())}? (STRING_LITERAL|TOKEN_REF)
;