Browse Source

Raise a SongSyntaxError on fatal error

pull/176/head
Oliverpool 9 years ago
parent
commit
9afd8f7b61
  1. 13
      patacrep/songs/chordpro/syntax.py
  2. 3
      patacrep/songs/errors.py
  3. 3
      test/test_chordpro/test_parser.py

13
patacrep/songs/chordpro/syntax.py

@ -7,6 +7,7 @@ import re
from patacrep.songs.syntax import Parser from patacrep.songs.syntax import Parser
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
from patacrep.songs import errors
LOGGER = logging.getLogger() LOGGER = logging.getLogger()
@ -331,5 +332,15 @@ def parse_song(content, filename=None):
# TODO: Provide a nicer error (an empty song?) # TODO: Provide a nicer error (an empty song?)
# TODO: Add an error to the song.errors list. # TODO: Add an error to the song.errors list.
# using parser._errors # using parser._errors
raise SyntaxError('Fatal error during song parsing: {}'.format(filename))
# pylint: disable=protected-access
if parser._errors:
# The line where it started to go wrong
lineno = parser._errors[-1].line
else:
lineno = None
raise errors.SongSyntaxError(
message='Fatal error during song parsing: {}'.format(filename),
line=lineno,
)
return parsed_content return parsed_content

3
patacrep/songs/errors.py

@ -1,12 +1,13 @@
"""Errors in song definition (syntax errors, and so on)""" """Errors in song definition (syntax errors, and so on)"""
class SongError: class SongError(Exception):
"""Generic song error""" """Generic song error"""
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
type = "generic" type = "generic"
def __init__(self, message): def __init__(self, message):
super().__init__()
self.message = message self.message = message
def __str__(self): def __str__(self):

3
test/test_chordpro/test_parser.py

@ -11,6 +11,7 @@ from pkg_resources import resource_filename
from patacrep import files from patacrep import files
from patacrep.build import DEFAULT_CONFIG from patacrep.build import DEFAULT_CONFIG
from patacrep.encoding import open_read from patacrep.encoding import open_read
from patacrep.songs import errors
from .. import disable_logging from .. import disable_logging
from .. import dynamic # pylint: disable=unused-import from .. import dynamic # pylint: disable=unused-import
@ -119,7 +120,7 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
sourcename = "{}.{}.source".format(base, in_format) sourcename = "{}.{}.source".format(base, in_format)
with self.chdir('errors'): with self.chdir('errors'):
parser = self.song_plugins[out_format][in_format] parser = self.song_plugins[out_format][in_format]
self.assertRaises(SyntaxError, parser, sourcename, self.config) self.assertRaises(errors.SongSyntaxError, parser, sourcename, self.config)
test_parse_failure.__doc__ = ( test_parse_failure.__doc__ = (
"Test that '{base}' parsing fails." "Test that '{base}' parsing fails."

Loading…
Cancel
Save