diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ArrayPredictionContext.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ArrayPredictionContext.java index b7ae777eb..96f9d215c 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ArrayPredictionContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ArrayPredictionContext.java @@ -8,49 +8,49 @@ import java.util.Iterator; public class ArrayPredictionContext extends PredictionContext { /** Parent can be null only if full ctx mode and we make an array * from EMPTY and non-empty. We merge EMPTY by using null parent and - * invokingState == EMPTY_FULL_INVOKING_STATE + * returnState == EMPTY_FULL_RETURN_STATE */ public final PredictionContext[] parents; /** Sorted for merge, no duplicates; if present, - * EMPTY_FULL_INVOKING_STATE is always first + * EMPTY_FULL_RETURN_STATE is always first */ - public final int[] invokingStates; + public final int[] returnStates; public ArrayPredictionContext(SingletonPredictionContext a) { - this(new PredictionContext[] {a.parent}, new int[] {a.invokingState}); + this(new PredictionContext[] {a.parent}, new int[] {a.returnState}); } - public ArrayPredictionContext(PredictionContext[] parents, int[] invokingStates) { - super(calculateHashCode(parents, invokingStates)); + public ArrayPredictionContext(PredictionContext[] parents, int[] returnStates) { + super(calculateHashCode(parents, returnStates)); assert parents!=null && parents.length>0; - assert invokingStates!=null && invokingStates.length>0; -// System.err.println("CREATE ARRAY: "+Arrays.toString(parents)+", "+Arrays.toString(invokingStates)); + assert returnStates!=null && returnStates.length>0; +// System.err.println("CREATE ARRAY: "+Arrays.toString(parents)+", "+Arrays.toString(returnStates)); this.parents = parents; - this.invokingStates = invokingStates; + this.returnStates = returnStates; } -//ArrayPredictionContext(@NotNull PredictionContext[] parents, int[] invokingStates, int parentHashCode, int invokingStateHashCode) { -// super(calculateHashCode(parentHashCode, invokingStateHashCode)); -// assert parents.length == invokingStates.length; -// assert invokingStates.length > 1 || invokingStates[0] != EMPTY_FULL_STATE_KEY : "Should be using PredictionContext.EMPTY instead."; +//ArrayPredictionContext(@NotNull PredictionContext[] parents, int[] returnStates, int parentHashCode, int returnStateHashCode) { +// super(calculateHashCode(parentHashCode, returnStateHashCode)); +// assert parents.length == returnStates.length; +// assert returnStates.length > 1 || returnStates[0] != EMPTY_FULL_STATE_KEY : "Should be using PredictionContext.EMPTY instead."; // // this.parents = parents; -// this.invokingStates = invokingStates; +// this.returnStates = returnStates; // } // -//ArrayPredictionContext(@NotNull PredictionContext[] parents, int[] invokingStates, int hashCode) { +//ArrayPredictionContext(@NotNull PredictionContext[] parents, int[] returnStates, int hashCode) { // super(hashCode); -// assert parents.length == invokingStates.length; -// assert invokingStates.length > 1 || invokingStates[0] != EMPTY_FULL_STATE_KEY : "Should be using PredictionContext.EMPTY instead."; +// assert parents.length == returnStates.length; +// assert returnStates.length > 1 || returnStates[0] != EMPTY_FULL_STATE_KEY : "Should be using PredictionContext.EMPTY instead."; // // this.parents = parents; -// this.invokingStates = invokingStates; +// this.returnStates = returnStates; // } - protected static int calculateHashCode(PredictionContext[] parents, int[] invokingStates) { + protected static int calculateHashCode(PredictionContext[] parents, int[] returnStates) { return calculateHashCode(calculateParentHashCode(parents), - calculateInvokingStatesHashCode(invokingStates)); + calculateReturnStatesHashCode(returnStates)); } protected static int calculateParentHashCode(PredictionContext[] parents) { @@ -64,9 +64,9 @@ public class ArrayPredictionContext extends PredictionContext { return hashCode; } - protected static int calculateInvokingStatesHashCode(int[] invokingStates) { + protected static int calculateReturnStatesHashCode(int[] returnStates) { int hashCode = 1; - for (int state : invokingStates) { + for (int state : returnStates) { hashCode = hashCode * 31 ^ state; } @@ -83,7 +83,7 @@ public class ArrayPredictionContext extends PredictionContext { @Override public SingletonPredictionContext next() { SingletonPredictionContext ctx = - SingletonPredictionContext.create(parents[i], invokingStates[i]); + SingletonPredictionContext.create(parents[i], returnStates[i]); i++; return ctx; } @@ -96,12 +96,12 @@ public class ArrayPredictionContext extends PredictionContext { @Override public boolean isEmpty() { return size()==1 && - invokingStates[0]==EMPTY_INVOKING_STATE; + returnStates[0]==EMPTY_RETURN_STATE; } @Override public int size() { - return invokingStates.length; + return returnStates.length; } @Override @@ -110,49 +110,15 @@ public class ArrayPredictionContext extends PredictionContext { } @Override - public int getInvokingState(int index) { - return invokingStates[index]; + public int getReturnState(int index) { + return returnStates[index]; } // @Override -// public int findInvokingState(int invokingState) { -// return Arrays.binarySearch(invokingStates, invokingState); +// public int findReturnState(int returnState) { +// return Arrays.binarySearch(returnStates, returnState); // } - /** Find invokingState parameter (call it x) in this.invokingStates, - * if present. Call pop on all x's parent(s) and then pull other - * elements from this context and merge into new context. - */ - @Override - public PredictionContext popAll( - int invokingState, - boolean fullCtx, - DoubleKeyMap mergeCache) - { - int index = Arrays.binarySearch(this.invokingStates, invokingState); - if ( index < 0 ) { - return this; - } - - PredictionContext newCtx = - this.parents[index].popAll(invokingState, fullCtx, mergeCache); - for (int i = 0; i < this.invokingStates.length; i++) { - if (i == index) continue; - PredictionContext next; - if ( this.invokingStates[i] == EMPTY_INVOKING_STATE ) { - next = PredictionContext.EMPTY; - } - else { - next = SingletonPredictionContext.create(this.parents[i], - this.invokingStates[i]); - } - boolean rootIsWildcard = fullCtx; - newCtx = merge(newCtx, next, rootIsWildcard, mergeCache); - } - - return newCtx; - } - @Override public boolean equals(Object o) { if (this == o) { @@ -167,7 +133,7 @@ public class ArrayPredictionContext extends PredictionContext { } ArrayPredictionContext a = (ArrayPredictionContext)o; - return Arrays.equals(invokingStates, a.invokingStates) && + return Arrays.equals(returnStates, a.returnStates) && Arrays.equals(parents, a.parents); } @@ -176,13 +142,13 @@ public class ArrayPredictionContext extends PredictionContext { if ( isEmpty() ) return "[]"; StringBuilder buf = new StringBuilder(); buf.append("["); - for (int i=0; i0 ) buf.append(", "); - if ( invokingStates[i]==EMPTY_INVOKING_STATE ) { + if ( returnStates[i]==EMPTY_RETURN_STATE ) { buf.append("$"); continue; } - buf.append(invokingStates[i]); + buf.append(returnStates[i]); if ( parents[i]!=null ) { buf.append(' '); buf.append(parents[i].toString()); diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/EmptyPredictionContext.java b/runtime/Java/src/org/antlr/v4/runtime/atn/EmptyPredictionContext.java index 690b32f02..0cd3d0ba1 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/EmptyPredictionContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/EmptyPredictionContext.java @@ -2,7 +2,7 @@ package org.antlr.v4.runtime.atn; public class EmptyPredictionContext extends SingletonPredictionContext { public EmptyPredictionContext() { - super(null, EMPTY_INVOKING_STATE); + super(null, EMPTY_RETURN_STATE); } public boolean isEmpty() { return true; } @@ -18,8 +18,8 @@ public class EmptyPredictionContext extends SingletonPredictionContext { } @Override - public int getInvokingState(int index) { - return invokingState; + public int getReturnState(int index) { + return returnState; } @Override diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java index c040bdf68..caa7c4f05 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java @@ -109,7 +109,7 @@ public class LL1Analyzer { if ( ctx != PredictionContext.EMPTY ) { // run thru all possible stack tops in ctx for (SingletonPredictionContext p : ctx) { - ATNState returnState = atn.states.get(p.invokingState); + ATNState returnState = atn.states.get(p.returnState); // System.out.println("popping back to "+retState); _LOOK(returnState, p.parent, look, lookBusy, seeThruPreds); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java index 00f35d6d6..0b24707d0 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java @@ -407,8 +407,8 @@ public class LexerATNSimulator extends ATNSimulator { if ( config.context!=null && !config.context.isEmpty() ) { for (SingletonPredictionContext ctx : config.context) { if ( !ctx.isEmpty() ) { - PredictionContext newContext = ctx.parent; // "pop" invoking state - if ( ctx.invokingState==PredictionContext.EMPTY_INVOKING_STATE ) { + PredictionContext newContext = ctx.parent; // "pop" return state + if ( ctx.returnState==PredictionContext.EMPTY_RETURN_STATE ) { // we have no context info. Don't pursue but // record a config that indicates how we hit end LexerATNConfig c = new LexerATNConfig(config, config.state, ctx); @@ -418,7 +418,7 @@ public class LexerATNSimulator extends ATNSimulator { configs.add(c); continue; } - ATNState returnState = atn.states.get(ctx.invokingState); + ATNState returnState = atn.states.get(ctx.returnState); LexerATNConfig c = new LexerATNConfig(returnState, config.alt, newContext); currentAltReachedAcceptState = closure(input, c, configs, currentAltReachedAcceptState, speculative); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java index 2727f0543..231057bdd 100755 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java @@ -1071,7 +1071,7 @@ public class ParserATNSimulator extends ATNSimulator { // run thru all possible stack tops in ctx if ( config.context!=null && !config.context.isEmpty() ) { for (SingletonPredictionContext ctx : config.context) { - if ( ctx.invokingState==PredictionContext.EMPTY_INVOKING_STATE ) { + if ( ctx.returnState==PredictionContext.EMPTY_RETURN_STATE ) { // we have no context info, just chase follow links (if greedy) if ( debug ) System.out.println("FALLING off rule "+ getRuleName(config.state.ruleIndex)); @@ -1079,8 +1079,8 @@ public class ParserATNSimulator extends ATNSimulator { fullCtx, depth); continue; } - ATNState returnState = atn.states.get(ctx.invokingState); - PredictionContext newContext = ctx.parent; // "pop" invoking state + ATNState returnState = atn.states.get(ctx.returnState); + PredictionContext newContext = ctx.parent; // "pop" return state ATNConfig c = new ATNConfig(returnState, config.alt, newContext, config.semanticContext); // While we have context to pop back from, we may have diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/PredictionContext.java b/runtime/Java/src/org/antlr/v4/runtime/atn/PredictionContext.java index 543495990..4e8771147 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/PredictionContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/PredictionContext.java @@ -22,9 +22,9 @@ public abstract class PredictionContext implements Iterable mergeCache); - @Override public int compareTo(PredictionContext o) { // used for toDotString to print nodes in order return id - o.id; @@ -91,8 +86,8 @@ public abstract class PredictionContext implements Iterable b.invokingState ) { - payloads[0] = b.invokingState; - payloads[1] = a.invokingState; + int[] payloads = {a.returnState, b.returnState}; + if ( a.returnState > b.returnState ) { + payloads[0] = b.returnState; + payloads[1] = a.returnState; } PredictionContext[] parents = {singleParent, singleParent}; PredictionContext a_ = new ArrayPredictionContext(parents, payloads); @@ -210,11 +205,11 @@ public abstract class PredictionContext implements Iterable b.invokingState ) { // sort by payload - payloads[0] = b.invokingState; - payloads[1] = a.invokingState; + if ( a.returnState > b.returnState ) { // sort by payload + payloads[0] = b.returnState; + payloads[1] = a.returnState; parents = new PredictionContext[] {b.parent, a.parent}; } PredictionContext a_ = new ArrayPredictionContext(parents, payloads); @@ -237,14 +232,14 @@ public abstract class PredictionContext implements Iterable ax if ( both$ || ax_ax ) { mergedParents[k] = a_parent; // choose left - mergedInvokingStates[k] = payload; + mergedReturnStates[k] = payload; } else { // ax+ay -> a'[x,y] PredictionContext mergedParent = merge(a_parent, b_parent, rootIsWildcard, mergeCache); mergedParents[k] = mergedParent; - mergedInvokingStates[k] = payload; + mergedReturnStates[k] = payload; } i++; // hop over left one as usual j++; // but also skip one in right side since we merge } - else if ( a.invokingStates[i] a, copy b[j] to M mergedParents[k] = b_parent; - mergedInvokingStates[k] = b.invokingStates[j]; + mergedReturnStates[k] = b.returnStates[j]; j++; } k++; } // copy over any payloads remaining in either array - if (i < a.invokingStates.length) { - for (int p = i; p < a.invokingStates.length; p++) { + if (i < a.returnStates.length) { + for (int p = i; p < a.returnStates.length; p++) { mergedParents[k] = a.parents[p]; - mergedInvokingStates[k] = a.invokingStates[p]; + mergedReturnStates[k] = a.returnStates[p]; k++; } } else { - for (int p = j; p < b.invokingStates.length; p++) { + for (int p = j; p < b.returnStates.length; p++) { mergedParents[k] = b.parents[p]; - mergedInvokingStates[k] = b.invokingStates[p]; + mergedReturnStates[k] = b.returnStates[p]; k++; } } @@ -336,16 +331,16 @@ public abstract class PredictionContext implements Iterable 1) { // first char is '[', if more than that this isn't the first rule localBuffer.append(' '); } - localBuffer.append(p.getInvokingState(index)); + localBuffer.append(p.getReturnState(index)); } } - stateNumber = p.getInvokingState(index); + stateNumber = p.getReturnState(index); p = p.getParent(index); } localBuffer.append("]"); diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/SingletonPredictionContext.java b/runtime/Java/src/org/antlr/v4/runtime/atn/SingletonPredictionContext.java index 1d0f86ade..73f25eb27 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/SingletonPredictionContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/SingletonPredictionContext.java @@ -6,22 +6,22 @@ import java.util.Iterator; public class SingletonPredictionContext extends PredictionContext { public final PredictionContext parent; - public final int invokingState; + public final int returnState; - SingletonPredictionContext(PredictionContext parent, int invokingState) { + SingletonPredictionContext(PredictionContext parent, int returnState) { super(calculateHashCode(parent!=null ? 31 ^ parent.hashCode() : 1, - 31 ^ invokingState)); - assert invokingState!=ATNState.INVALID_STATE_NUMBER; + 31 ^ returnState)); + assert returnState!=ATNState.INVALID_STATE_NUMBER; this.parent = parent; - this.invokingState = invokingState; + this.returnState = returnState; } - public static SingletonPredictionContext create(PredictionContext parent, int invokingState) { - if ( invokingState == EMPTY_INVOKING_STATE && parent == null ) { + public static SingletonPredictionContext create(PredictionContext parent, int returnState) { + if ( returnState == EMPTY_RETURN_STATE && parent == null ) { // someone can pass in the bits of an array ctx that mean $ return EMPTY; } - return new SingletonPredictionContext(parent, invokingState); + return new SingletonPredictionContext(parent, returnState); } @Override @@ -52,21 +52,9 @@ public class SingletonPredictionContext extends PredictionContext { } @Override - public int getInvokingState(int index) { + public int getReturnState(int index) { assert index == 0; - return invokingState; - } - - @Override - public PredictionContext popAll( - int invokingState, - boolean fullCtx, - DoubleKeyMap mergeCache) - { - if ( invokingState == this.invokingState ) { - return parent.popAll(invokingState, fullCtx, mergeCache); - } - return this; + return returnState; } @Override @@ -83,7 +71,7 @@ public class SingletonPredictionContext extends PredictionContext { } SingletonPredictionContext s = (SingletonPredictionContext)o; - return invokingState == s.invokingState && + return returnState == s.returnState && (parent!=null && parent.equals(s.parent)); } @@ -91,11 +79,11 @@ public class SingletonPredictionContext extends PredictionContext { public String toString() { String up = parent!=null ? parent.toString() : ""; if ( up.length()==0 ) { - if ( invokingState == EMPTY_INVOKING_STATE ) { + if ( returnState == EMPTY_RETURN_STATE ) { return "$"; } - return String.valueOf(invokingState); + return String.valueOf(returnState); } - return String.valueOf(invokingState)+" "+up; + return String.valueOf(returnState)+" "+up; } } diff --git a/tool/test/org/antlr/v4/test/TestGraphNodes.java b/tool/test/org/antlr/v4/test/TestGraphNodes.java index 84b637b2a..8693303e3 100644 --- a/tool/test/org/antlr/v4/test/TestGraphNodes.java +++ b/tool/test/org/antlr/v4/test/TestGraphNodes.java @@ -795,7 +795,7 @@ public class TestGraphNodes { int[] invokingStates = new int[nodes.length]; for (int i=0; i'); - if (current.getInvokingState(i) == PredictionContext.EMPTY_INVOKING_STATE) { + if (current.getReturnState(i) == PredictionContext.EMPTY_RETURN_STATE) { nodes.append(rootIsWildcard ? '*' : '$'); } } @@ -843,7 +843,7 @@ public class TestGraphNodes { } for (int i = 0; i < current.size(); i++) { - if (current.getInvokingState(i) == PredictionContext.EMPTY_INVOKING_STATE) { + if (current.getReturnState(i) == PredictionContext.EMPTY_RETURN_STATE) { continue; } @@ -859,7 +859,7 @@ public class TestGraphNodes { edges.append("->"); edges.append('s').append(contextIds.get(current.getParent(i))); - edges.append("[label=\"").append(current.getInvokingState(i)).append("\"]"); + edges.append("[label=\"").append(current.getReturnState(i)).append("\"]"); edges.append(";\n"); } }