diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java index 733fb5a39..0ad61f917 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNSimulator.java @@ -236,6 +236,20 @@ public abstract class ATNSimulator { } for (ATNState state : atn.states) { + if (state instanceof BlockStartState) { + // we need to know the end state to set its start state + if (((BlockStartState)state).endState == null) { + throw new IllegalStateException(); + } + + // block end states can only be associated to a single block start state + if (((BlockStartState)state).endState.startState != null) { + throw new IllegalStateException(); + } + + ((BlockStartState)state).endState.startState = (BlockStartState)state; + } + if (state instanceof PlusLoopbackState) { PlusLoopbackState loopbackState = (PlusLoopbackState)state; for (int i = 0; i < loopbackState.getNumberOfTransitions(); i++) { @@ -310,6 +324,12 @@ public abstract class ATNSimulator { } } + if (state instanceof BlockEndState) { + if (((BlockEndState)state).startState == null) { + throw new IllegalStateException(); + } + } + if (state instanceof DecisionState) { DecisionState decisionState = (DecisionState)state; if (decisionState.getNumberOfTransitions() > 1 && decisionState.decision < 0) { diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/BlockEndState.java b/runtime/Java/src/org/antlr/v4/runtime/atn/BlockEndState.java index 474f94e7a..f82ed4dbf 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/BlockEndState.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/BlockEndState.java @@ -31,4 +31,5 @@ package org.antlr.v4.runtime.atn; /** Terminal node of a simple (a|b|c) block */ public class BlockEndState extends ATNState { + public BlockStartState startState; }