reduce memory usage by creating LexerATNConfig a subclass with the lexer action index. saves about 2M on java.* with javalr.
This commit is contained in:
parent
3c7b4c2a33
commit
0e24a66f07
|
@ -68,9 +68,6 @@ public class ATNConfig {
|
||||||
*/
|
*/
|
||||||
public int reachesIntoOuterContext;
|
public int reachesIntoOuterContext;
|
||||||
|
|
||||||
/** Capture lexer action we traverse */
|
|
||||||
public int lexerActionIndex = -1; // TOOD: move to subclass
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public final SemanticContext semanticContext;
|
public final SemanticContext semanticContext;
|
||||||
|
|
||||||
|
@ -112,7 +109,6 @@ public class ATNConfig {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.semanticContext = semanticContext;
|
this.semanticContext = semanticContext;
|
||||||
this.reachesIntoOuterContext = c.reachesIntoOuterContext;
|
this.reachesIntoOuterContext = c.reachesIntoOuterContext;
|
||||||
this.lexerActionIndex = c.lexerActionIndex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** An ATN configuration is equal to another if both have
|
/** An ATN configuration is equal to another if both have
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
package org.antlr.v4.runtime.atn;
|
||||||
|
|
||||||
|
import org.antlr.v4.runtime.misc.NotNull;
|
||||||
|
import org.antlr.v4.runtime.misc.Nullable;
|
||||||
|
|
||||||
|
public class LexerATNConfig extends ATNConfig {
|
||||||
|
/** Capture lexer action we traverse */
|
||||||
|
public int lexerActionIndex = -1; // TOOD: move to subclass
|
||||||
|
|
||||||
|
public LexerATNConfig(@NotNull ATNState state,
|
||||||
|
int alt,
|
||||||
|
@Nullable PredictionContext context)
|
||||||
|
{
|
||||||
|
super(state, alt, context, SemanticContext.NONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LexerATNConfig(@NotNull LexerATNConfig c, @NotNull ATNState state) {
|
||||||
|
super(c, state, c.context, c.semanticContext);
|
||||||
|
this.lexerActionIndex = c.lexerActionIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LexerATNConfig(@NotNull LexerATNConfig c, @NotNull ATNState state,
|
||||||
|
@NotNull SemanticContext semanticContext) {
|
||||||
|
super(c, state, c.context, semanticContext);
|
||||||
|
this.lexerActionIndex = c.lexerActionIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LexerATNConfig(@NotNull LexerATNConfig c, @NotNull ATNState state,
|
||||||
|
int actionIndex)
|
||||||
|
{
|
||||||
|
super(c, state, c.context, c.semanticContext);
|
||||||
|
this.lexerActionIndex = actionIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LexerATNConfig(@NotNull LexerATNConfig c, @NotNull ATNState state,
|
||||||
|
@Nullable PredictionContext context) {
|
||||||
|
super(c, state, context, c.semanticContext);
|
||||||
|
this.lexerActionIndex = c.lexerActionIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -73,7 +73,7 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
protected int line = 0;
|
protected int line = 0;
|
||||||
protected int charPos = -1;
|
protected int charPos = -1;
|
||||||
protected DFAState dfaState;
|
protected DFAState dfaState;
|
||||||
protected ATNConfig config;
|
protected LexerATNConfig config;
|
||||||
|
|
||||||
protected void reset() {
|
protected void reset() {
|
||||||
index = -1;
|
index = -1;
|
||||||
|
@ -384,7 +384,7 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
Transition trans = c.state.transition(ti);
|
Transition trans = c.state.transition(ti);
|
||||||
ATNState target = getReachableTarget(trans, t);
|
ATNState target = getReachableTarget(trans, t);
|
||||||
if ( target!=null ) {
|
if ( target!=null ) {
|
||||||
closure(new ATNConfig(c, target), reach);
|
closure(new LexerATNConfig((LexerATNConfig)c, target), reach);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -396,7 +396,7 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
reach, prevAccept.config, prevAccept.index);
|
reach, prevAccept.config, prevAccept.index);
|
||||||
}
|
}
|
||||||
for (int ci=0; ci<reach.size(); ci++) {
|
for (int ci=0; ci<reach.size(); ci++) {
|
||||||
ATNConfig c = reach.get(ci);
|
LexerATNConfig c = (LexerATNConfig)reach.get(ci);
|
||||||
if ( c.state instanceof RuleStopState) {
|
if ( c.state instanceof RuleStopState) {
|
||||||
if ( debug ) {
|
if ( debug ) {
|
||||||
System.out.format("processAcceptConfigs: hit accept config %s index %d\n",
|
System.out.format("processAcceptConfigs: hit accept config %s index %d\n",
|
||||||
|
@ -416,7 +416,7 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
if ( debug ) {
|
if ( debug ) {
|
||||||
System.out.format("markExecSettings for %s @ index=%d, line %d:%d\n", c, index, prevAccept.line, prevAccept.charPos);
|
System.out.format("markExecSettings for %s @ index=%d, line %d:%d\n", c, index, prevAccept.line, prevAccept.charPos);
|
||||||
}
|
}
|
||||||
captureSimState(prevAccept, input, reach, c);
|
captureSimState(prevAccept, input, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we reach lexer accept state, toss out any configs in rest
|
// if we reach lexer accept state, toss out any configs in rest
|
||||||
|
@ -513,13 +513,13 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
ATNConfigSet configs = new ATNConfigSet();
|
ATNConfigSet configs = new ATNConfigSet();
|
||||||
for (int i=0; i<p.getNumberOfTransitions(); i++) {
|
for (int i=0; i<p.getNumberOfTransitions(); i++) {
|
||||||
ATNState target = p.transition(i).target;
|
ATNState target = p.transition(i).target;
|
||||||
ATNConfig c = new ATNConfig(target, i+1, initialContext);
|
LexerATNConfig c = new LexerATNConfig(target, i+1, initialContext);
|
||||||
closure(c, configs);
|
closure(c, configs);
|
||||||
}
|
}
|
||||||
return configs;
|
return configs;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void closure(@NotNull ATNConfig config, @NotNull ATNConfigSet configs) {
|
protected void closure(@NotNull LexerATNConfig config, @NotNull ATNConfigSet configs) {
|
||||||
if ( debug ) {
|
if ( debug ) {
|
||||||
System.out.println("closure("+config.toString(recog, true)+")");
|
System.out.println("closure("+config.toString(recog, true)+")");
|
||||||
}
|
}
|
||||||
|
@ -547,7 +547,7 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
ATNState invokingState = atn.states.get(ctx.invokingState);
|
ATNState invokingState = atn.states.get(ctx.invokingState);
|
||||||
RuleTransition rt = (RuleTransition)invokingState.transition(0);
|
RuleTransition rt = (RuleTransition)invokingState.transition(0);
|
||||||
ATNState retState = rt.followState;
|
ATNState retState = rt.followState;
|
||||||
ATNConfig c = new ATNConfig(retState, config.alt, newContext);
|
LexerATNConfig c = new LexerATNConfig(retState, config.alt, newContext);
|
||||||
closure(c, configs);
|
closure(c, configs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -563,23 +563,23 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
ATNState p = config.state;
|
ATNState p = config.state;
|
||||||
for (int i=0; i<p.getNumberOfTransitions(); i++) {
|
for (int i=0; i<p.getNumberOfTransitions(); i++) {
|
||||||
Transition t = p.transition(i);
|
Transition t = p.transition(i);
|
||||||
ATNConfig c = getEpsilonTarget(config, t, configs);
|
LexerATNConfig c = getEpsilonTarget(config, t, configs);
|
||||||
if ( c!=null ) closure(c, configs);
|
if ( c!=null ) closure(c, configs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// side-effect: can alter configs.hasSemanticContext
|
// side-effect: can alter configs.hasSemanticContext
|
||||||
@Nullable
|
@Nullable
|
||||||
public ATNConfig getEpsilonTarget(@NotNull ATNConfig config,
|
public LexerATNConfig getEpsilonTarget(@NotNull LexerATNConfig config,
|
||||||
@NotNull Transition t,
|
@NotNull Transition t,
|
||||||
@NotNull ATNConfigSet configs)
|
@NotNull ATNConfigSet configs)
|
||||||
{
|
{
|
||||||
ATNState p = config.state;
|
ATNState p = config.state;
|
||||||
ATNConfig c = null;
|
LexerATNConfig c = null;
|
||||||
if ( t.getClass() == RuleTransition.class ) {
|
if ( t.getClass() == RuleTransition.class ) {
|
||||||
PredictionContext newContext =
|
PredictionContext newContext =
|
||||||
new SingletonPredictionContext(config.context, p.stateNumber);
|
new SingletonPredictionContext(config.context, p.stateNumber);
|
||||||
c = new ATNConfig(config, t.target, newContext);
|
c = new LexerATNConfig(config, t.target, newContext);
|
||||||
}
|
}
|
||||||
else if ( t.getClass() == PredicateTransition.class ) {
|
else if ( t.getClass() == PredicateTransition.class ) {
|
||||||
if (recog == null) {
|
if (recog == null) {
|
||||||
|
@ -610,16 +610,15 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
}
|
}
|
||||||
configs.hasSemanticContext = true;
|
configs.hasSemanticContext = true;
|
||||||
if ( recog == null || recog.sempred(null, pt.ruleIndex, pt.predIndex) ) {
|
if ( recog == null || recog.sempred(null, pt.ruleIndex, pt.predIndex) ) {
|
||||||
c = new ATNConfig(config, t.target, pt.getPredicate());
|
c = new LexerATNConfig(config, t.target, pt.getPredicate());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ignore actions; just exec one per rule upon accept
|
// ignore actions; just exec one per rule upon accept
|
||||||
else if ( t.getClass() == ActionTransition.class ) {
|
else if ( t.getClass() == ActionTransition.class ) {
|
||||||
c = new ATNConfig(config, t.target);
|
c = new LexerATNConfig(config, t.target, ((ActionTransition)t).actionIndex);
|
||||||
c.lexerActionIndex = ((ActionTransition)t).actionIndex;
|
|
||||||
}
|
}
|
||||||
else if ( t.isEpsilon() ) {
|
else if ( t.isEpsilon() ) {
|
||||||
c = new ATNConfig(config, t.target);
|
c = new LexerATNConfig(config, t.target);
|
||||||
}
|
}
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
@ -659,8 +658,7 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
|
|
||||||
protected void captureSimState(@NotNull SimState settings,
|
protected void captureSimState(@NotNull SimState settings,
|
||||||
@NotNull CharStream input,
|
@NotNull CharStream input,
|
||||||
@NotNull ATNConfigSet ATNConfigs,
|
@NotNull LexerATNConfig config)
|
||||||
@NotNull ATNConfig config)
|
|
||||||
{
|
{
|
||||||
settings.index = input.index();
|
settings.index = input.index();
|
||||||
settings.line = line;
|
settings.line = line;
|
||||||
|
@ -726,7 +724,8 @@ public class LexerATNSimulator extends ATNSimulator {
|
||||||
if ( firstConfigWithRuleStopState!=null ) {
|
if ( firstConfigWithRuleStopState!=null ) {
|
||||||
newState.isAcceptState = true;
|
newState.isAcceptState = true;
|
||||||
newState.lexerRuleIndex = firstConfigWithRuleStopState.state.ruleIndex;
|
newState.lexerRuleIndex = firstConfigWithRuleStopState.state.ruleIndex;
|
||||||
newState.lexerActionIndex = firstConfigWithRuleStopState.lexerActionIndex;
|
newState.lexerActionIndex =
|
||||||
|
((LexerATNConfig)firstConfigWithRuleStopState).lexerActionIndex;
|
||||||
newState.prediction = atn.ruleToTokenType[newState.lexerRuleIndex];
|
newState.prediction = atn.ruleToTokenType[newState.lexerRuleIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue