From 59676745319b864fe3b8fd61710e730e760f5afd Mon Sep 17 00:00:00 2001 From: Michael Peyton Jones Date: Mon, 22 Feb 2016 11:55:34 +0000 Subject: [PATCH] Track 'prefix' and 'other' alternatives together These alternatives are not treated differently after they are created, and we want to retain their alternative ordering. --- .../analysis/LeftRecursiveRuleAnalyzer.java | 19 +++++++++---------- .../LeftRecursiveRuleTransformer.java | 1 - 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java index 5b6ba058f..0bf3708fb 100644 --- a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java +++ b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java @@ -30,6 +30,12 @@ package org.antlr.v4.analysis; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + import org.antlr.runtime.CommonToken; import org.antlr.runtime.Token; import org.antlr.runtime.TokenStream; @@ -51,12 +57,6 @@ import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.STGroupFile; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - /** Using a tree walker on the rules, determine if a rule is directly left-recursive and if it follows * our pattern. */ @@ -69,7 +69,6 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { public LinkedHashMap ternaryAlts = new LinkedHashMap(); public LinkedHashMap suffixAlts = new LinkedHashMap(); public List prefixAlts = new ArrayList(); - public List otherAlts = new ArrayList(); /** Pointer to ID node of ^(= ID element) */ public List> leftRecursiveRuleRefLabels = @@ -222,7 +221,9 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { String altLabel = altTree.altLabel!=null ? altTree.altLabel.getText() : null; LeftRecursiveRuleAltInfo a = new LeftRecursiveRuleAltInfo(alt, altText, null, altLabel, false, originalAltTree); - otherAlts.add(a); + // We keep other alts with prefix alts since they are all added to the start of the generated rule, and + // we want to retain any prior ordering between them + prefixAlts.add(a); // System.out.println("otherAlt " + alt + ": " + altText); } @@ -255,7 +256,6 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { } ruleST.add("primaryAlts", prefixAlts); - ruleST.add("primaryAlts", otherAlts); tool.log("left-recursion", ruleST.render()); @@ -440,7 +440,6 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { ", ternaryAlts=" + ternaryAlts + ", suffixAlts=" + suffixAlts + ", prefixAlts=" + prefixAlts + - ", otherAlts=" + otherAlts + '}'; } } diff --git a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java index ea67493f9..0df860807 100644 --- a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java +++ b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java @@ -169,7 +169,6 @@ public class LeftRecursiveRuleTransformer { // track recursive alt info for codegen r.recPrimaryAlts = new ArrayList(); r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAlts); - r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.otherAlts); if (r.recPrimaryAlts.isEmpty()) { tool.errMgr.grammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.getChild(0)).getToken(), r.name); }