def sync(self, recognizer: Parser): # If already recovering, don't try to sync if self.inErrorRecoveryMode(recognizer): return s = recognizer._interp.atn.states[recognizer.state] la = recognizer.getTokenStream().LA(1) # try cheaper subset first; might get lucky. seems to shave a wee bit off if la == Token.EOF or la in recognizer.atn.nextTokens(s): return # Return but don't end recovery. only do that upon valid token match if recognizer.isExpectedToken(la): return if s.stateType in [ ATNState.BLOCK_START, ATNState.STAR_BLOCK_START, ATNState.PLUS_BLOCK_START, ATNState.STAR_LOOP_ENTRY ]: # report error and recover if possible if self.singleTokenDeletion(recognizer) is not None: return else: raise InputMismatchException(recognizer) elif s.stateType in [ATNState.PLUS_LOOP_BACK, ATNState.STAR_LOOP_BACK]: self.reportUnwantedToken(recognizer) expecting = recognizer.getExpectedTokens() whatFollowsLoopIterationOrRule = expecting.addSet( self.getErrorRecoverySet(recognizer)) self.consumeUntil(recognizer, whatFollowsLoopIterationOrRule) else: # do nothing if we can't identify the exact kind of ATN state pass
def reportInputMismatch(self, recognizer: Parser, e: InputMismatchException, line, column): msg = "Mismatched input {0}. Expected {1}".format( self.getTokenErrorDisplay(e.offendingToken), e.getExpectedTokens().toString(recognizer.literalNames, recognizer.symbolicNames)) self.printSyntaxError(msg, line, column)
def messageInputMismatch(self, recognizer: Parser, e: InputMismatchException) -> str: msg: str = ("mismatched input " + self.getTokenErrorDisplay(e.offendingToken) + " expecting " + e.getExpectedTokens().toString( recognizer.literalNames, recognizer.symbolicNames)) return msg
def recoverInline(self, recognizer: Parser): # SINGLE TOKEN DELETION matchedSymbol = self.singleTokenDeletion(recognizer) if matchedSymbol is not None: # we have deleted the extra token. # now, move past ttype token as if all were ok recognizer.consume() return matchedSymbol # SINGLE TOKEN INSERTION if self.singleTokenInsertion(recognizer): return self.getMissingSymbol(recognizer) # even that didn't work; must throw the exception raise InputMismatchException(recognizer)
def sync(self, recognizer): # If already recovering, don't try to sync if self.inErrorRecoveryMode(recognizer): return s = recognizer._interp.atn.states[recognizer.state] 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 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, ATNState.PLUS_BLOCK_START, ATNState.STAR_LOOP_ENTRY ]: # report error and recover if possible if self.singleTokenDeletion(recognizer) is not None: return else: raise InputMismatchException(recognizer) elif s.stateType in [ATNState.PLUS_LOOP_BACK, ATNState.STAR_LOOP_BACK]: self.reportUnwantedToken(recognizer) expecting = recognizer.getExpectedTokens() whatFollowsLoopIterationOrRule = expecting.addSet( self.getErrorRecoverySet(recognizer)) self.consumeUntil(recognizer, whatFollowsLoopIterationOrRule) else: # do nothing if we can't identify the exact kind of ATN state pass
def recoverInline(self, recognizer: Parser): self.recover(recognizer, InputMismatchException(recognizer))
def reportInputMismatch(self, recognizer: Parser, e: InputMismatchException): msg = "mismatched input " + self.getTokenErrorDisplay(e.offendingToken) \ + " expecting " + e.getExpectedTokens().toString(recognizer.literalNames, recognizer.symbolicNames) recognizer.notifyErrorListeners(msg, e.offendingToken, e)
def reportInputMismatch(self, recognizer:Parser, e:InputMismatchException): msg = "mismatched input " + self.getTokenErrorDisplay(e.offendingToken) \ + " expecting " + e.getExpectedTokens().toString(recognizer.literalNames, recognizer.symbolicNames) recognizer.notifyErrorListeners(msg, e.offendingToken, e)
def recoverInline(self, recognizer): raise RuntimeError(InputMismatchException(recognizer))
def recoverInline(self, recognizer: Parser): raise (InputMismatchException(recognizer))
def recover(self, recognizer: Parser, e: RecognitionException): raise (InputMismatchException(recognizer))