Skip to content

Commit

Permalink
Merge pull request #1150 from clinssen/friendlier_lexer_parser_errors
Browse files Browse the repository at this point in the history
  • Loading branch information
clinssen authored Dec 11, 2024
2 parents 461c5c3 + dcee582 commit c60b5b0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pynestml/grammars/generate_lexer_parser
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

# Do not change the position of this file! In order to generate the corresponding code into `pynestml/generated`, this script has to be executed from `pynestml/grammars`.

antlr4 -Dlanguage=Python3 *.g4 -visitor -no-listener -o ../generated
antlr4 -encoding UTF8 -Dlanguage=Python3 *.g4 -visitor -no-listener -o ../generated
4 changes: 4 additions & 0 deletions pynestml/utils/error_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ def error_occurred(self):

def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
self._error_occurred = True
self.offendingSymbol = offendingSymbol
self.line = line
self.column = column
self.msg = msg
8 changes: 4 additions & 4 deletions pynestml/utils/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ def get_no_code_generated(cls):
return MessageCode.NO_CODE_GENERATED, message

@classmethod
def get_lexer_error(cls):
message = 'Error occurred during lexing: abort'
def get_lexer_error(cls, msg):
message = 'Error occurred during lexing: ' + msg
return MessageCode.LEXER_ERROR, message

@classmethod
Expand All @@ -192,8 +192,8 @@ def get_could_not_determine_cond_based(cls, type_str, name):
return MessageCode.LEXER_ERROR, message

@classmethod
def get_parser_error(cls):
message = 'Error occurred during parsing: abort'
def get_parser_error(cls, msg):
message = 'Error occurred during parsing: ' + msg
return MessageCode.PARSER_ERROR, message

@classmethod
Expand Down
21 changes: 14 additions & 7 deletions pynestml/utils/model_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def parse_file(cls, file_path=None):
:rtype: ASTNestMLCompilationUnit
"""
try:
input_file = FileStream(file_path)
input_file = FileStream(file_path, encoding='utf-8')
except IOError:
code, message = Messages.get_input_path_not_found(path=file_path)
Logger.log_message(node=None, code=None, message=message,
Expand All @@ -101,7 +101,6 @@ def parse_file(cls, file_path=None):
# create a lexer and hand over the input
lexer = PyNestMLLexer()
lexer.removeErrorListeners()
lexer.addErrorListener(ConsoleErrorListener())
lexerErrorListener = NestMLErrorListener()
lexer.addErrorListener(lexerErrorListener)
lexer._errHandler = BailErrorStrategy() # halt immediately on lexer errors
Expand All @@ -111,24 +110,32 @@ def parse_file(cls, file_path=None):
stream = CommonTokenStream(lexer)
stream.fill()
if lexerErrorListener._error_occurred:
code, message = Messages.get_lexer_error()
error_location = ASTSourceLocation(lexerErrorListener.line,
lexerErrorListener.column,
lexerErrorListener.line,
lexerErrorListener.column)
code, message = Messages.get_lexer_error(lexerErrorListener.msg)
Logger.log_message(node=None, code=None, message=message,
error_position=None, log_level=LoggingLevel.ERROR)
error_position=error_location, log_level=LoggingLevel.ERROR)
return

# parse the file
parser = PyNestMLParser(None)
parser.removeErrorListeners()
parser.addErrorListener(ConsoleErrorListener())
parserErrorListener = NestMLErrorListener()
parser.addErrorListener(parserErrorListener)
# parser._errHandler = BailErrorStrategy() # N.B. uncomment this line and the next to halt immediately on parse errors
# parser._errHandler.reset(parser)
parser.setTokenStream(stream)
compilation_unit = parser.nestMLCompilationUnit()
if parserErrorListener._error_occurred:
code, message = Messages.get_parser_error()
error_location = ASTSourceLocation(parserErrorListener.line,
parserErrorListener.column,
parserErrorListener.line,
parserErrorListener.column)
code, message = Messages.get_parser_error(parserErrorListener.msg)
Logger.log_message(node=None, code=None, message=message,
error_position=None, log_level=LoggingLevel.ERROR)
error_position=error_location, log_level=LoggingLevel.ERROR)
return

# create a new visitor and return the new AST
Expand Down

0 comments on commit c60b5b0

Please sign in to comment.