fix #3042 in Python 2 runtime

This commit is contained in:
Eric Vergnaud 2021-01-23 17:23:45 +08:00
parent 7a9a26c7ec
commit a9f11612dd
2 changed files with 25 additions and 14 deletions

View File

@ -129,16 +129,16 @@ class LL1Analyzer (object):
return
if ctx != PredictionContext.EMPTY:
# run thru all possible stack tops in ctx
for i in range(0, len(ctx)):
returnState = self.atn.states[ctx.getReturnState(i)]
removed = returnState.ruleIndex in calledRuleStack
try:
calledRuleStack.discard(returnState.ruleIndex)
removed = s.ruleIndex in calledRuleStack
try:
calledRuleStack.discard(s.ruleIndex)
# run thru all possible stack tops in ctx
for i in range(0, len(ctx)):
returnState = self.atn.states[ctx.getReturnState(i)]
self._LOOK(returnState, stopState, ctx.getParent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
finally:
if removed:
calledRuleStack.add(returnState.ruleIndex)
finally:
if removed:
calledRuleStack.add(s.ruleIndex)
return
for t in s.transitions:
@ -163,8 +163,8 @@ class LL1Analyzer (object):
elif type(t) == WildcardTransition:
look.addRange( Interval(Token.MIN_USER_TOKEN_TYPE, self.atn.maxTokenType + 1) )
else:
set = t.label
if set is not None:
set_ = t.label
if set_ is not None:
if isinstance(t, NotSetTransition):
set = set.complement(Token.MIN_USER_TOKEN_TYPE, self.atn.maxTokenType)
look.addSet(set)
set_ = set_.complement(Token.MIN_USER_TOKEN_TYPE, self.atn.maxTokenType)
look.addSet(set_)

View File

@ -53,6 +53,8 @@ class DefaultErrorStrategy(ErrorStrategy):
#
self.lastErrorIndex = -1
self.lastErrorStates = None
self.nextTokensContext = None
self.nextTokenState = 0
# <p>The default implementation simply calls {@link #endErrorCondition} to
# ensure that the handler is not in error recovery mode.</p>
@ -203,7 +205,16 @@ class DefaultErrorStrategy(ErrorStrategy):
la = recognizer.getTokenStream().LA(1)
# try cheaper subset first; might get lucky. seems to shave a wee bit off
nextTokens = recognizer.atn.nextTokens(s)
if Token.EPSILON in nextTokens or la in nextTokens:
if la in nextTokens:
self.nextTokensContext = None
self.nextTokenState = ATNState.INVALID_STATE_NUMBER
return
elif Token.EPSILON in nextTokens:
if self.nextTokensContext is None:
# It's possible the next token won't match information tracked
# by sync is restricted for performance.
self.nextTokensContext = recognizer._ctx
self.nextTokensState = recognizer._stateNumber
return
if s.stateType in [ATNState.BLOCK_START, ATNState.STAR_BLOCK_START,