From 198db1a0e189cfd072eeb6a3d4d1ebf0cf9d5ab4 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Tue, 17 Nov 2015 14:19:53 +0100 Subject: [PATCH] Factorize the error recording --- patacrep/songs/chordpro/syntax.py | 16 ++++------------ patacrep/songs/syntax.py | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/patacrep/songs/chordpro/syntax.py b/patacrep/songs/chordpro/syntax.py index 2747d030..b0eda1ee 100644 --- a/patacrep/songs/chordpro/syntax.py +++ b/patacrep/songs/chordpro/syntax.py @@ -5,7 +5,6 @@ import ply.yacc as yacc import re from patacrep.songs.syntax import Parser -from patacrep.songs import errors from patacrep.songs.chordpro import ast from patacrep.songs.chordpro.lexer import tokens, ChordProLexer @@ -145,29 +144,24 @@ class ChordproParser(Parser): if match is None: if argument.strip(): - error = errors.SongSyntaxError( + self.error( line=symbols.lexer.lineno, message="Invalid chord definition '{}'.".format(argument), ) - self.error(line=error.line, message=error.message) else: - error = errors.SongSyntaxError( + self.error( line=symbols.lexer.lineno, message="Invalid empty chord definition.", ) - self.error(line=error.line, message=error.message) - self._errors.append(error) symbols[0] = ast.Error() return define = self._parse_define(match.groupdict()) if define is None: - error = errors.SongSyntaxError( + self.error( line=symbols.lexer.lineno, message="Invalid chord definition '{}'.".format(argument), ) - self.error(line=error.line, message=error.message) - self._errors.append(error) symbols[0] = ast.Error() return self._directives.append(define) @@ -198,12 +192,10 @@ class ChordproParser(Parser): def p_line_error(self, symbols): """line_error : error directive""" - error = errors.SongSyntaxError( + self.error( line=symbols.lexer.lineno, message="Directive can only be preceded or followed by spaces", ) - self._errors.append(error) - LOGGER.error(error.message) symbols[0] = ast.Line() @staticmethod diff --git a/patacrep/songs/syntax.py b/patacrep/songs/syntax.py index c7e933ec..d3b5c0ac 100644 --- a/patacrep/songs/syntax.py +++ b/patacrep/songs/syntax.py @@ -24,7 +24,14 @@ class Parser: return column def error(self, *, line=None, column=None, message=""): - """Display an error message""" + """Record and display an error message""" + self._errors.append( + errors.SongSyntaxError( + line=line, + message=message, + ) + ) + coordinates = [] if line is not None: coordinates.append("line {}".format(line)) @@ -45,17 +52,10 @@ class Parser: def p_error(self, token): """Manage parsing errors.""" if token is None: - error = errors.SongSyntaxError( - line=None, - message="Unexpected end of file.", - ) - self.error(message=error.message) + self.error(message="Unexpected end of file.") else: - error = errors.SongSyntaxError( - line=token.lineno, - message="Syntax error", - ) self.error( - line=error.line, + message="Syntax error", + line=token.lineno, column=self.__find_column(token), )