| 1 |
# This file is part of python-ly, https://pypi.python.org/pypi/python-ly |
| 2 |
# |
| 3 |
# Copyright (c) 2008 - 2015 by Wilbert Berendsen |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or |
| 6 |
# modify it under the terms of the GNU General Public License |
| 7 |
# as published by the Free Software Foundation; either version 2 |
| 8 |
# of the License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
# See http://www.gnu.org/licenses/ for more information. |
| 19 |
|
| 20 |
""" |
| 21 |
Parses and tokenizes Texinfo input, recognizing LilyPond in Texinfo. |
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import unicode_literals |
| 25 |
|
| 26 |
from . import _token |
| 27 |
from . import Parser, FallthroughParser |
| 28 |
|
| 29 |
|
| 30 |
class Comment(_token.Comment): |
| 31 |
pass |
| 32 |
|
| 33 |
|
| 34 |
class LineComment(Comment, _token.LineComment): |
| 35 |
rx = r"@c\b.*$" |
| 36 |
|
| 37 |
|
| 38 |
class BlockCommentStart(Comment, _token.BlockCommentStart): |
| 39 |
rx = r"@ignore\b" |
| 40 |
def update_state(self, state): |
| 41 |
state.enter(ParseComment()) |
| 42 |
|
| 43 |
|
| 44 |
class BlockCommentEnd(Comment, _token.Leaver, _token.BlockCommentEnd): |
| 45 |
rx = r"@end\s+ignore\b" |
| 46 |
|
| 47 |
|
| 48 |
class Attribute(_token.Token): |
| 49 |
pass |
| 50 |
|
| 51 |
|
| 52 |
class Keyword(_token.Token): |
| 53 |
rx = r"@[a-zA-Z]+" |
| 54 |
|
| 55 |
|
| 56 |
class Block(_token.Token): |
| 57 |
pass |
| 58 |
|
| 59 |
|
| 60 |
class BlockStart(Block): |
| 61 |
rx = r"@[a-zA-Z]+\{" |
| 62 |
def update_state(self, state): |
| 63 |
state.enter(ParseBlock()) |
| 64 |
|
| 65 |
|
| 66 |
class BlockEnd(Block, _token.Leaver): |
| 67 |
rx = r"\}" |
| 68 |
|
| 69 |
|
| 70 |
class EscapeChar(_token.Character): |
| 71 |
rx = r"@[@{}]" |
| 72 |
|
| 73 |
|
| 74 |
class Accent(EscapeChar): |
| 75 |
rx = "@['\"',=^`~](\\{[a-zA-Z]\\}|[a-zA-Z]\\b)" |
| 76 |
|
| 77 |
|
| 78 |
class Verbatim(_token.Token): |
| 79 |
pass |
| 80 |
|
| 81 |
|
| 82 |
class VerbatimStart(Keyword): |
| 83 |
rx = r"@verbatim\b" |
| 84 |
def update_state(self, state): |
| 85 |
state.enter(ParseVerbatim()) |
| 86 |
|
| 87 |
|
| 88 |
class VerbatimEnd(Keyword, _token.Leaver): |
| 89 |
rx = r"@end\s+verbatim\b" |
| 90 |
|
| 91 |
|
| 92 |
class LilyPondBlockStart(Block): |
| 93 |
rx = r"@lilypond(?=(\[[a-zA-Z,=0-9\\\s]+\])?\{)" |
| 94 |
def update_state(self, state): |
| 95 |
state.enter(ParseLilyPondBlockAttr()) |
| 96 |
|
| 97 |
|
| 98 |
class LilyPondBlockStartBrace(Block): |
| 99 |
rx = r"\{" |
| 100 |
def update_state(self, state): |
| 101 |
state.replace(ParseLilyPondBlock()) |
| 102 |
|
| 103 |
|
| 104 |
class LilyPondBlockEnd(Block, _token.Leaver): |
| 105 |
rx = r"\}" |
| 106 |
|
| 107 |
|
| 108 |
class LilyPondEnvStart(Keyword): |
| 109 |
rx = r"@lilypond\b" |
| 110 |
def update_state(self, state): |
| 111 |
state.enter(ParseLilyPondEnvAttr()) |
| 112 |
|
| 113 |
|
| 114 |
class LilyPondEnvEnd(Keyword, _token.Leaver): |
| 115 |
rx = r"@end\s+lilypond\b" |
| 116 |
|
| 117 |
|
| 118 |
class LilyPondFileStart(Block): |
| 119 |
rx = r"@lilypondfile\b" |
| 120 |
def update_state(self, state): |
| 121 |
state.enter(ParseLilyPondFile()) |
| 122 |
|
| 123 |
|
| 124 |
class LilyPondFileStartBrace(Block): |
| 125 |
rx = r"\{" |
| 126 |
def update_state(self, state): |
| 127 |
state.replace(ParseBlock()) |
| 128 |
|
| 129 |
|
| 130 |
class LilyPondAttrStart(Attribute): |
| 131 |
rx = r"\[" |
| 132 |
def update_state(self, state): |
| 133 |
state.enter(ParseLilyPondAttr()) |
| 134 |
|
| 135 |
|
| 136 |
class LilyPondAttrEnd(Attribute, _token.Leaver): |
| 137 |
rx = r"\]" |
| 138 |
|
| 139 |
|
| 140 |
# Parsers: |
| 141 |
|
| 142 |
class ParseTexinfo(Parser): |
| 143 |
mode = "texinfo" |
| 144 |
items = ( |
| 145 |
LineComment, |
| 146 |
BlockCommentStart, |
| 147 |
Accent, |
| 148 |
EscapeChar, |
| 149 |
LilyPondBlockStart, |
| 150 |
LilyPondEnvStart, |
| 151 |
LilyPondFileStart, |
| 152 |
BlockStart, |
| 153 |
VerbatimStart, |
| 154 |
Keyword, |
| 155 |
) |
| 156 |
|
| 157 |
|
| 158 |
class ParseComment(Parser): |
| 159 |
default = Comment |
| 160 |
items = ( |
| 161 |
BlockCommentEnd, |
| 162 |
) |
| 163 |
|
| 164 |
|
| 165 |
class ParseBlock(Parser): |
| 166 |
items = ( |
| 167 |
BlockEnd, |
| 168 |
Accent, |
| 169 |
EscapeChar, |
| 170 |
BlockStart, |
| 171 |
Keyword, |
| 172 |
) |
| 173 |
|
| 174 |
|
| 175 |
class ParseVerbatim(Parser): |
| 176 |
default = Verbatim |
| 177 |
items = ( |
| 178 |
VerbatimEnd, |
| 179 |
) |
| 180 |
|
| 181 |
|
| 182 |
class ParseLilyPondBlockAttr(Parser): |
| 183 |
items = ( |
| 184 |
LilyPondAttrStart, |
| 185 |
LilyPondBlockStartBrace, |
| 186 |
) |
| 187 |
|
| 188 |
|
| 189 |
class ParseLilyPondEnvAttr(FallthroughParser): |
| 190 |
items = ( |
| 191 |
LilyPondAttrStart, |
| 192 |
) |
| 193 |
def fallthrough(self, state): |
| 194 |
state.replace(ParseLilyPondEnv()) |
| 195 |
|
| 196 |
|
| 197 |
class ParseLilyPondAttr(Parser): |
| 198 |
default = Attribute |
| 199 |
items = ( |
| 200 |
LilyPondAttrEnd, |
| 201 |
) |
| 202 |
|
| 203 |
|
| 204 |
class ParseLilyPondFile(Parser): |
| 205 |
items = ( |
| 206 |
LilyPondAttrStart, |
| 207 |
LilyPondFileStartBrace, |
| 208 |
) |
| 209 |
|
| 210 |
|
| 211 |
from . import lilypond |
| 212 |
|
| 213 |
class ParseLilyPondBlock(lilypond.ParseGlobal): |
| 214 |
items = ( |
| 215 |
LilyPondBlockEnd, |
| 216 |
) + lilypond.ParseGlobal.items |
| 217 |
|
| 218 |
|
| 219 |
class ParseLilyPondEnv(lilypond.ParseGlobal): |
| 220 |
items = ( |
| 221 |
LilyPondEnvEnd, |
| 222 |
) + lilypond.ParseGlobal.items |
| 223 |
|
| 224 |
|