From 65396e6d7c4813051fabf8f6aff1353907280581 Mon Sep 17 00:00:00 2001 From: parrt Date: Sat, 16 Jul 2011 11:49:14 -0800 Subject: [PATCH] don't reduce SETs if elements ref'd in -> rewrite [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8864] --- tool/playground/T.g | 1 + .../org/antlr/v4/parse/BlockSetTransformer.g | 33 ++++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/tool/playground/T.g b/tool/playground/T.g index 716d09e3f..9bf5d38f8 100644 --- a/tool/playground/T.g +++ b/tool/playground/T.g @@ -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'+ ; diff --git a/tool/src/org/antlr/v4/parse/BlockSetTransformer.g b/tool/src/org/antlr/v4/parse/BlockSetTransformer.g index a8cb38e7b..36833b36e 100644 --- a/tool/src/org/antlr/v4/parse/BlockSetTransformer.g +++ b/tool/src/org/antlr/v4/parse/BlockSetTransformer.g @@ -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 rewriteElems = new HashSet(); } 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 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 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) ; \ No newline at end of file