Browse Source

Factorize the error recording

pull/176/head
Oliverpool 9 years ago
parent
commit
198db1a0e1
  1. 16
      patacrep/songs/chordpro/syntax.py
  2. 22
      patacrep/songs/syntax.py

16
patacrep/songs/chordpro/syntax.py

@ -5,7 +5,6 @@ import ply.yacc as yacc
import re import re
from patacrep.songs.syntax import Parser from patacrep.songs.syntax import Parser
from patacrep.songs import errors
from patacrep.songs.chordpro import ast from patacrep.songs.chordpro import ast
from patacrep.songs.chordpro.lexer import tokens, ChordProLexer from patacrep.songs.chordpro.lexer import tokens, ChordProLexer
@ -145,29 +144,24 @@ class ChordproParser(Parser):
if match is None: if match is None:
if argument.strip(): if argument.strip():
error = errors.SongSyntaxError( self.error(
line=symbols.lexer.lineno, line=symbols.lexer.lineno,
message="Invalid chord definition '{}'.".format(argument), message="Invalid chord definition '{}'.".format(argument),
) )
self.error(line=error.line, message=error.message)
else: else:
error = errors.SongSyntaxError( self.error(
line=symbols.lexer.lineno, line=symbols.lexer.lineno,
message="Invalid empty chord definition.", message="Invalid empty chord definition.",
) )
self.error(line=error.line, message=error.message)
self._errors.append(error)
symbols[0] = ast.Error() symbols[0] = ast.Error()
return return
define = self._parse_define(match.groupdict()) define = self._parse_define(match.groupdict())
if define is None: if define is None:
error = errors.SongSyntaxError( self.error(
line=symbols.lexer.lineno, line=symbols.lexer.lineno,
message="Invalid chord definition '{}'.".format(argument), message="Invalid chord definition '{}'.".format(argument),
) )
self.error(line=error.line, message=error.message)
self._errors.append(error)
symbols[0] = ast.Error() symbols[0] = ast.Error()
return return
self._directives.append(define) self._directives.append(define)
@ -198,12 +192,10 @@ class ChordproParser(Parser):
def p_line_error(self, symbols): def p_line_error(self, symbols):
"""line_error : error directive""" """line_error : error directive"""
error = errors.SongSyntaxError( self.error(
line=symbols.lexer.lineno, line=symbols.lexer.lineno,
message="Directive can only be preceded or followed by spaces", message="Directive can only be preceded or followed by spaces",
) )
self._errors.append(error)
LOGGER.error(error.message)
symbols[0] = ast.Line() symbols[0] = ast.Line()
@staticmethod @staticmethod

22
patacrep/songs/syntax.py

@ -24,7 +24,14 @@ class Parser:
return column return column
def error(self, *, line=None, column=None, message=""): 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 = [] coordinates = []
if line is not None: if line is not None:
coordinates.append("line {}".format(line)) coordinates.append("line {}".format(line))
@ -45,17 +52,10 @@ class Parser:
def p_error(self, token): def p_error(self, token):
"""Manage parsing errors.""" """Manage parsing errors."""
if token is None: if token is None:
error = errors.SongSyntaxError( self.error(message="Unexpected end of file.")
line=None,
message="Unexpected end of file.",
)
self.error(message=error.message)
else: else:
error = errors.SongSyntaxError(
line=token.lineno,
message="Syntax error",
)
self.error( self.error(
line=error.line, message="Syntax error",
line=token.lineno,
column=self.__find_column(token), column=self.__find_column(token),
) )

Loading…
Cancel
Save