Skip to content

Python LexerNoViableAltException should not be created with None mgs #4095

@Sofyan-SU

Description

@Sofyan-SU

Grammar bug :
Identifier: [A-Za-z][A-Za-z0-9_]* ;
expr: Identifier ;

Trigger :

  • Input to expr is given an illegal character e.g : "id@"

  • one has overriden the recover function of the lexer. e.g. :
    `class ErrorLexer(bugLexer):
    """
    New Lexer class that overrides the bugLexer and crashes in case of lexer error
    """

    def recover(self, r):
    raise r # Raise the error for the upper level`

Analysis
LexerNoViableAltException is created with message = None
Exception is caught by ErrorStrategy that reportError with a None message
print("unknown recognition error type: " + type(e).__name__) recognizer.notifyErrorListeners(e.message, e.offendingToken, e)
Then the ConsoleErrorListener will display the syntaxError as follow:
print("line " + str(line) + ":" + str(column) + " " + msg, file=sys.stderr)
This raise an error as msg is None.

Proposal is to create the LexerNoViableAltException with a message
e.g. :
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 self.message = self.__str__()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions