Spaces:
Runtime error
Runtime error
File size: 6,759 Bytes
8a6cf24 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
# Use of this file is governed by the BSD 3-clause license that
# can be found in the LICENSE.txt file in the project root.
#
# need forward declaration
Token = None
Lexer = None
Parser = None
TokenStream = None
ATNConfigSet = None
ParserRulecontext = None
PredicateTransition = None
BufferedTokenStream = None
class UnsupportedOperationException(Exception):
def __init__(self, msg:str):
super().__init__(msg)
class IllegalStateException(Exception):
def __init__(self, msg:str):
super().__init__(msg)
class CancellationException(IllegalStateException):
def __init__(self, msg:str):
super().__init__(msg)
# The root of the ANTLR exception hierarchy. In general, ANTLR tracks just
# 3 kinds of errors: prediction errors, failed predicate errors, and
# mismatched input errors. In each case, the parser knows where it is
# in the input, where it is in the ATN, the rule invocation stack,
# and what kind of problem occurred.
from antlr4.InputStream import InputStream
from antlr4.ParserRuleContext import ParserRuleContext
from antlr4.Recognizer import Recognizer
class RecognitionException(Exception):
def __init__(self, message:str=None, recognizer:Recognizer=None, input:InputStream=None, ctx:ParserRulecontext=None):
super().__init__(message)
self.message = message
self.recognizer = recognizer
self.input = input
self.ctx = ctx
# The current {@link Token} when an error occurred. Since not all streams
# support accessing symbols by index, we have to track the {@link Token}
# instance itself.
self.offendingToken = None
# Get the ATN state number the parser was in at the time the error
# occurred. For {@link NoViableAltException} and
# {@link LexerNoViableAltException} exceptions, this is the
# {@link DecisionState} number. For others, it is the state whose outgoing
# edge we couldn't match.
self.offendingState = -1
if recognizer is not None:
self.offendingState = recognizer.state
# <p>If the state number is not known, this method returns -1.</p>
#
# Gets the set of input symbols which could potentially follow the
# previously matched symbol at the time this exception was thrown.
#
# <p>If the set of expected tokens is not known and could not be computed,
# this method returns {@code null}.</p>
#
# @return The set of token types that could potentially follow the current
# state in the ATN, or {@code null} if the information is not available.
#/
def getExpectedTokens(self):
if self.recognizer is not None:
return self.recognizer.atn.getExpectedTokens(self.offendingState, self.ctx)
else:
return None
class LexerNoViableAltException(RecognitionException):
def __init__(self, lexer:Lexer, input:InputStream, startIndex:int, deadEndConfigs:ATNConfigSet):
super().__init__(message=None, recognizer=lexer, input=input, ctx=None)
self.startIndex = startIndex
self.deadEndConfigs = deadEndConfigs
def __str__(self):
symbol = ""
if self.startIndex >= 0 and self.startIndex < self.input.size:
symbol = self.input.getText(self.startIndex, self.startIndex)
# TODO symbol = Utils.escapeWhitespace(symbol, false);
return "LexerNoViableAltException('" + symbol + "')"
# Indicates that the parser could not decide which of two or more paths
# to take based upon the remaining input. It tracks the starting token
# of the offending input and also knows where the parser was
# in the various paths when the error. Reported by reportNoViableAlternative()
#
class NoViableAltException(RecognitionException):
def __init__(self, recognizer:Parser, input:TokenStream=None, startToken:Token=None,
offendingToken:Token=None, deadEndConfigs:ATNConfigSet=None, ctx:ParserRuleContext=None):
if ctx is None:
ctx = recognizer._ctx
if offendingToken is None:
offendingToken = recognizer.getCurrentToken()
if startToken is None:
startToken = recognizer.getCurrentToken()
if input is None:
input = recognizer.getInputStream()
super().__init__(recognizer=recognizer, input=input, ctx=ctx)
# Which configurations did we try at input.index() that couldn't match input.LT(1)?#
self.deadEndConfigs = deadEndConfigs
# The token object at the start index; the input stream might
# not be buffering tokens so get a reference to it. (At the
# time the error occurred, of course the stream needs to keep a
# buffer all of the tokens but later we might not have access to those.)
self.startToken = startToken
self.offendingToken = offendingToken
# This signifies any kind of mismatched input exceptions such as
# when the current input does not match the expected token.
#
class InputMismatchException(RecognitionException):
def __init__(self, recognizer:Parser):
super().__init__(recognizer=recognizer, input=recognizer.getInputStream(), ctx=recognizer._ctx)
self.offendingToken = recognizer.getCurrentToken()
# A semantic predicate failed during validation. Validation of predicates
# occurs when normally parsing the alternative just like matching a token.
# Disambiguating predicate evaluation occurs when we test a predicate during
# prediction.
class FailedPredicateException(RecognitionException):
def __init__(self, recognizer:Parser, predicate:str=None, message:str=None):
super().__init__(message=self.formatMessage(predicate,message), recognizer=recognizer,
input=recognizer.getInputStream(), ctx=recognizer._ctx)
s = recognizer._interp.atn.states[recognizer.state]
trans = s.transitions[0]
from antlr4.atn.Transition import PredicateTransition
if isinstance(trans, PredicateTransition):
self.ruleIndex = trans.ruleIndex
self.predicateIndex = trans.predIndex
else:
self.ruleIndex = 0
self.predicateIndex = 0
self.predicate = predicate
self.offendingToken = recognizer.getCurrentToken()
def formatMessage(self, predicate:str, message:str):
if message is not None:
return message
else:
return "failed predicate: {" + predicate + "}?"
class ParseCancellationException(CancellationException):
pass
del Token
del Lexer
del Parser
del TokenStream
del ATNConfigSet
del ParserRulecontext
del PredicateTransition
del BufferedTokenStream
|