From da9215aacd4036d2750e698de5221c9e0f011265 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 23 Mar 2016 07:18:23 +0100 Subject: [PATCH] [WIP] Escaped characters are correctly parsed. They now have to be correctly rendered [ci skip] --- patacrep/songs/chordpro/lexer.py | 22 ++++++++++++++-------- patacrep/songs/chordpro/syntax.py | 15 ++++++++++++--- test/test_song/latex.csg | 10 ++++++++++ 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/patacrep/songs/chordpro/lexer.py b/patacrep/songs/chordpro/lexer.py index ffeec7d4..c63049aa 100644 --- a/patacrep/songs/chordpro/lexer.py +++ b/patacrep/songs/chordpro/lexer.py @@ -30,8 +30,6 @@ tokens = ( 'EE', ) -literals = [ '{', '}', "\\", ' ' ] - class ChordProLexer: """ChordPro Lexer class""" # pylint: disable=too-many-public-methods @@ -51,7 +49,7 @@ class ChordProLexer: t_directive_SPACE = r'[ \t]+' t_directive_KEYWORD = r'[a-zA-Z_]+' - t_directiveargument_TEXT = r'[^}]+' + t_directiveargument_TEXT = r'[^\\}]+' @staticmethod def t_SOC(token): @@ -120,7 +118,7 @@ class ChordProLexer: @staticmethod def t_WORD(token): - r'[^{}\r\n\]\[\t ]+' + r'[^{}\\\r\n\]\[\t ]+' return token def t_LBRACKET(self, __token): @@ -153,10 +151,18 @@ class ChordProLexer: return token @staticmethod - def t_literal(token): - r'\[{} \#]' - t.type = t.type[1] - return t + def t_ESCAPED(token): + r'\\[{} #\\]' + token.value = token.value[1] + token.type = "WORD" + return token + + @staticmethod + def t_directiveargument_ESCAPED(token): + r'\\[{} #\\]' + token.value = token.value[1] + token.type = "TEXT" + return token def error(self, token, more=""): """Display error message, and skip illegal token.""" diff --git a/patacrep/songs/chordpro/syntax.py b/patacrep/songs/chordpro/syntax.py index b4c28cc7..9a7b9feb 100644 --- a/patacrep/songs/chordpro/syntax.py +++ b/patacrep/songs/chordpro/syntax.py @@ -182,9 +182,8 @@ class ChordproParser(Parser): @staticmethod def p_directive_next(symbols): - """directive_next : SPACE COLON TEXT - | COLON TEXT - | COLON + """directive_next : SPACE COLON directive_argument + | COLON directive_argument | empty """ if len(symbols) == 3: @@ -196,6 +195,16 @@ class ChordproParser(Parser): else: symbols[0] = None + @staticmethod + def p_directive_argument(symbols): + """directive_argument : TEXT directive_argument + | empty + """ + if len(symbols) == 3: + symbols[0] = symbols[1] + symbols[2] + else: + symbols[0] = "" + def p_line_error(self, symbols): """line_error : error directive""" self.error( diff --git a/test/test_song/latex.csg b/test/test_song/latex.csg index e69de29b..7fd9cf9f 100644 --- a/test/test_song/latex.csg +++ b/test/test_song/latex.csg @@ -0,0 +1,10 @@ +{lang: en} +{title: & $ % # _ \} \{ ~ ^ \\} +{artist: & $ % # _ \} \{ ~ ^ \\} +{album: & $ % # _ \} \{ ~ ^ \\} + +& $ % \# _ \} \{ ~ ^ \\ + +{start_of_chorus} + & $ % \# _ \} \{ ~ ^ \\ +{end_of_chorus}