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

2
patacrep/songs/chordpro/__init__.py

@ -31,7 +31,7 @@ class ChordproSong(Song):
song = parse_song(song.read(), self.fullpath) song = parse_song(song.read(), self.fullpath)
self.authors = song.authors self.authors = song.authors
self.titles = song.titles 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.data = song.meta
self.errors = [error(song=self) for error in song.error_builders] self.errors = [error(song=self) for error in song.error_builders]
self.cached = { self.cached = {

27
patacrep/songs/chordpro/syntax.py

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

Loading…
Cancel
Save