Browse Source

[song.chordpro] Catch fatal syntax error when a block is not closed

pull/176/head
Louis 9 years ago
parent
commit
d40fd9bb15
  1. 7
      patacrep/songs/__init__.py
  2. 2
      patacrep/songs/chordpro/__init__.py
  3. 27
      patacrep/songs/chordpro/syntax.py

7
patacrep/songs/__init__.py

@ -7,8 +7,10 @@ import os
import pickle
import re
from patacrep import errors as book_errors
from patacrep import files, encoding
from patacrep.authors import process_listauthors
from patacrep.songs import errors as song_errors
LOGGER = logging.getLogger(__name__)
@ -106,7 +108,7 @@ class Song:
self.subpath = subpath
self._filehash = None
self.encoding = config["encoding"]
self.default_lang = config["lang"]
self.lang = config["lang"]
self.config = config
self.errors = []
@ -116,8 +118,7 @@ class Song:
# Data extraction from the latex song
self.titles = []
self.data = {}
self.cached = None
self.lang = None
self.cached = {}
self._parse()
# Post processing of data

2
patacrep/songs/chordpro/__init__.py

@ -31,7 +31,7 @@ class ChordproSong(Song):
song = parse_song(song.read(), self.fullpath)
self.authors = song.authors
self.titles = song.titles
self.lang = song.get_data_argument('language', self.default_lang)
self.lang = song.get_data_argument('language', self.lang)
self.data = song.meta
self.errors = [error(song=self) for error in song.error_builders]
self.cached = {

27
patacrep/songs/chordpro/syntax.py

@ -1,13 +1,14 @@
"""ChordPro parser"""
import functools
import logging
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
from patacrep.songs import errors
from patacrep.songs.syntax import Parser
LOGGER = logging.getLogger()
@ -328,19 +329,13 @@ def parse_song(content, filename=None):
lexer=ChordProLexer(filename=filename).lexer,
)
if parsed_content is None:
# pylint: disable=fixme
# TODO: Provide a nicer error (an empty song?)
# TODO: Add an error to the song.errors list.
# using parser._errors
# 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 ast.Song(
filename,
[],
errors=[functools.partial(
errors.SongSyntaxError,
line=parser._errors[-1].keywords['line'],
message='Fatal error during song parsing.',
)]
)
return parsed_content

Loading…
Cancel
Save