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
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

22
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),
)

Loading…
Cancel
Save