| 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 LilyPond input. |
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import unicode_literals |
| 25 |
|
| 26 |
import itertools |
| 27 |
|
| 28 |
from . import _token |
| 29 |
from . import Parser, FallthroughParser |
| 30 |
|
| 31 |
# an identifier allowing letters and single hyphens in between |
| 32 |
re_identifier = r"[^\W\d_]+([_-][^\W\d_]+)*" |
| 33 |
|
| 34 |
# the lookahead pattern for the end of an identifier (ref) |
| 35 |
re_identifier_end = r"(?![_-]?[^\W\d])" |
| 36 |
|
| 37 |
re_articulation = r"[-_^][_.>|!+^-]" |
| 38 |
re_dynamic = ( |
| 39 |
r"\\[<!>]|" |
| 40 |
r"\\(f{1,5}|p{1,5}" |
| 41 |
r"|mf|mp|fp|spp?|sff?|sfz|rfz|n" |
| 42 |
r"|cresc|decresc|dim|cr|decr" |
| 43 |
r")(?![A-Za-z])") |
| 44 |
|
| 45 |
re_duration = rf"(\\(maxima|longa|breve){re_identifier_end}|(1|2|4|8|16|32|64|128|256|512|1024|2048)(?!\d))" |
| 46 |
|
| 47 |
re_dot = r"\." |
| 48 |
re_scaling = r"\*[\t ]*\d+(/\d+)?" |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
class Identifier(_token.Token): |
| 53 |
"""A variable name, like ``some-variable``.""" |
| 54 |
rx = r"(?<![^\W\d])" + re_identifier + re_identifier_end |
| 55 |
|
| 56 |
|
| 57 |
class IdentifierRef(_token.Token): |
| 58 |
r"""A reference to an identifier, e.g. ``\some-variable``.""" |
| 59 |
rx = r"\\" + re_identifier + re_identifier_end |
| 60 |
|
| 61 |
|
| 62 |
class Variable(Identifier): |
| 63 |
pass |
| 64 |
|
| 65 |
|
| 66 |
class UserVariable(Identifier): |
| 67 |
pass |
| 68 |
|
| 69 |
|
| 70 |
class Value(_token.Item, _token.Numeric): |
| 71 |
pass |
| 72 |
|
| 73 |
|
| 74 |
class DecimalValue(Value): |
| 75 |
rx = r"-?\d+(\.\d+)?" |
| 76 |
|
| 77 |
|
| 78 |
class IntegerValue(DecimalValue): |
| 79 |
rx = r"\d+" |
| 80 |
|
| 81 |
|
| 82 |
class Fraction(Value): |
| 83 |
rx = r"\d+/\d+" |
| 84 |
|
| 85 |
|
| 86 |
class Delimiter(_token.Token): |
| 87 |
pass |
| 88 |
|
| 89 |
|
| 90 |
class DotPath(Delimiter): |
| 91 |
"""A dot in dotted path notation.""" |
| 92 |
rx = r"\." |
| 93 |
|
| 94 |
|
| 95 |
class Error(_token.Error): |
| 96 |
pass |
| 97 |
|
| 98 |
|
| 99 |
class Comment(_token.Comment): |
| 100 |
pass |
| 101 |
|
| 102 |
|
| 103 |
class BlockCommentStart(Comment, _token.BlockCommentStart): |
| 104 |
rx = r"%{" |
| 105 |
def update_state(self, state): |
| 106 |
state.enter(ParseBlockComment()) |
| 107 |
|
| 108 |
|
| 109 |
class BlockCommentEnd(Comment, _token.BlockCommentEnd, _token.Leaver): |
| 110 |
rx = r"%}" |
| 111 |
|
| 112 |
|
| 113 |
class BlockComment(Comment, _token.BlockComment): |
| 114 |
pass |
| 115 |
|
| 116 |
|
| 117 |
class LineComment(Comment, _token.LineComment): |
| 118 |
rx = r"%.*$" |
| 119 |
|
| 120 |
|
| 121 |
class String(_token.String): |
| 122 |
pass |
| 123 |
|
| 124 |
|
| 125 |
class StringQuotedStart(String, _token.StringStart): |
| 126 |
rx = r'"' |
| 127 |
def update_state(self, state): |
| 128 |
state.enter(ParseString()) |
| 129 |
|
| 130 |
|
| 131 |
class StringQuotedEnd(String, _token.StringEnd): |
| 132 |
rx = r'"' |
| 133 |
def update_state(self, state): |
| 134 |
state.leave() |
| 135 |
state.endArgument() |
| 136 |
|
| 137 |
|
| 138 |
class StringQuoteEscape(_token.Character): |
| 139 |
rx = r'\\[\\"]' |
| 140 |
|
| 141 |
|
| 142 |
class MusicItem(_token.Token): |
| 143 |
r"""A note, rest, spacer, ``\skip`` or ``q``.""" |
| 144 |
|
| 145 |
|
| 146 |
class Skip(MusicItem): |
| 147 |
rx = r"\\skip" + re_identifier_end |
| 148 |
|
| 149 |
|
| 150 |
class Spacer(MusicItem): |
| 151 |
rx = r"s(?![A-Za-z])" |
| 152 |
|
| 153 |
|
| 154 |
class Rest(MusicItem): |
| 155 |
rx = r"[Rr](?![A-Za-z])" |
| 156 |
|
| 157 |
|
| 158 |
class Note(MusicItem): |
| 159 |
rx = r"[a-x]+(?![A-Za-z])" |
| 160 |
|
| 161 |
|
| 162 |
class Q(MusicItem): |
| 163 |
rx = r"q(?![A-Za-z])" |
| 164 |
|
| 165 |
|
| 166 |
class DrumNote(MusicItem): |
| 167 |
rx = r"[a-z]+(?![A-Za-z])" |
| 168 |
|
| 169 |
|
| 170 |
class Octave(_token.Token): |
| 171 |
rx = r",+|'+" |
| 172 |
|
| 173 |
|
| 174 |
class OctaveCheck(_token.Token): |
| 175 |
rx = r"=(,+|'+)?" |
| 176 |
|
| 177 |
|
| 178 |
class Accidental(_token.Token): |
| 179 |
pass |
| 180 |
|
| 181 |
|
| 182 |
class AccidentalReminder(Accidental): |
| 183 |
rx = r"!" |
| 184 |
|
| 185 |
|
| 186 |
class AccidentalCautionary(Accidental): |
| 187 |
rx = r"\?" |
| 188 |
|
| 189 |
|
| 190 |
class Duration(_token.Token): |
| 191 |
pass |
| 192 |
|
| 193 |
|
| 194 |
class Length(Duration): |
| 195 |
rx = re_duration |
| 196 |
def update_state(self, state): |
| 197 |
state.enter(ParseDuration()) |
| 198 |
|
| 199 |
|
| 200 |
class Dot(Duration): |
| 201 |
rx = re_dot |
| 202 |
|
| 203 |
|
| 204 |
class Scaling(Duration): |
| 205 |
rx = re_scaling |
| 206 |
|
| 207 |
|
| 208 |
class OpenBracket(Delimiter, _token.MatchStart, _token.Indent): |
| 209 |
"""An open bracket, does not enter different parser, subclass or reimplement Parser.update_state().""" |
| 210 |
rx = r"\{" |
| 211 |
matchname = "bracket" |
| 212 |
|
| 213 |
|
| 214 |
class CloseBracket(Delimiter, _token.MatchEnd, _token.Dedent): |
| 215 |
rx = r"\}" |
| 216 |
matchname = "bracket" |
| 217 |
def update_state(self, state): |
| 218 |
state.leave() |
| 219 |
state.endArgument() |
| 220 |
|
| 221 |
|
| 222 |
class OpenSimultaneous(Delimiter, _token.MatchStart, _token.Indent): |
| 223 |
"""An open double French quote, does not enter different parser, subclass or reimplement Parser.update_state().""" |
| 224 |
rx = r"<<" |
| 225 |
matchname = "simultaneous" |
| 226 |
|
| 227 |
|
| 228 |
class CloseSimultaneous(Delimiter, _token.MatchEnd, _token.Dedent): |
| 229 |
rx = r">>" |
| 230 |
matchname = "simultaneous" |
| 231 |
def update_state(self, state): |
| 232 |
state.leave() |
| 233 |
state.endArgument() |
| 234 |
|
| 235 |
|
| 236 |
class SequentialStart(OpenBracket): |
| 237 |
def update_state(self, state): |
| 238 |
state.enter(ParseMusic()) |
| 239 |
|
| 240 |
|
| 241 |
class SequentialEnd(CloseBracket): |
| 242 |
pass |
| 243 |
|
| 244 |
|
| 245 |
class SimultaneousStart(OpenSimultaneous): |
| 246 |
def update_state(self, state): |
| 247 |
state.enter(ParseMusic()) |
| 248 |
|
| 249 |
|
| 250 |
class SimultaneousEnd(CloseSimultaneous): |
| 251 |
pass |
| 252 |
|
| 253 |
|
| 254 |
class PipeSymbol(Delimiter): |
| 255 |
rx = r"\|" |
| 256 |
|
| 257 |
|
| 258 |
class Articulation(_token.Token): |
| 259 |
"""Base class for articulation things.""" |
| 260 |
|
| 261 |
|
| 262 |
class ArticulationCommand(Articulation, IdentifierRef): |
| 263 |
@classmethod |
| 264 |
def test_match(cls, match): |
| 265 |
s = match.group()[1:] |
| 266 |
if '-' not in s: |
| 267 |
from .. import words |
| 268 |
for l in ( |
| 269 |
words.articulations, |
| 270 |
words.ornaments, |
| 271 |
words.fermatas, |
| 272 |
words.instrument_scripts, |
| 273 |
words.repeat_scripts, |
| 274 |
words.ancient_scripts, |
| 275 |
): |
| 276 |
if s in l: |
| 277 |
return True |
| 278 |
return False |
| 279 |
|
| 280 |
|
| 281 |
class Direction(_token.Token): |
| 282 |
rx = r"[-_^]" |
| 283 |
def update_state(self, state): |
| 284 |
state.enter(ParseScriptAbbreviationOrFingering()) |
| 285 |
|
| 286 |
|
| 287 |
class ScriptAbbreviation(Articulation, _token.Leaver): |
| 288 |
rx = r"[+|!>._^-]" |
| 289 |
|
| 290 |
|
| 291 |
class Fingering(Articulation, _token.Leaver): |
| 292 |
rx = r"\d+" |
| 293 |
|
| 294 |
|
| 295 |
class StringNumber(Articulation): |
| 296 |
rx = r"\\\d+" |
| 297 |
|
| 298 |
|
| 299 |
class Slur(_token.Token): |
| 300 |
pass |
| 301 |
|
| 302 |
|
| 303 |
class SlurStart(Slur, _token.MatchStart): |
| 304 |
rx = r"\(" |
| 305 |
matchname = "slur" |
| 306 |
|
| 307 |
|
| 308 |
class SlurEnd(Slur, _token.MatchEnd): |
| 309 |
rx = r"\)" |
| 310 |
matchname = "slur" |
| 311 |
|
| 312 |
|
| 313 |
class PhrasingSlurStart(SlurStart): |
| 314 |
rx = r"\\\(" |
| 315 |
matchname = "phrasingslur" |
| 316 |
|
| 317 |
|
| 318 |
class PhrasingSlurEnd(SlurEnd): |
| 319 |
rx = r"\\\)" |
| 320 |
matchname = "phrasingslur" |
| 321 |
|
| 322 |
|
| 323 |
class Tie(Slur): |
| 324 |
rx = r"~" |
| 325 |
|
| 326 |
|
| 327 |
class Beam(_token.Token): |
| 328 |
pass |
| 329 |
|
| 330 |
|
| 331 |
class BeamStart(Beam, _token.MatchStart): |
| 332 |
rx = r"\[" |
| 333 |
matchname = "beam" |
| 334 |
|
| 335 |
|
| 336 |
class BeamEnd(Beam, _token.MatchEnd): |
| 337 |
rx = r"\]" |
| 338 |
matchname = "beam" |
| 339 |
|
| 340 |
|
| 341 |
class Ligature(_token.Token): |
| 342 |
pass |
| 343 |
|
| 344 |
|
| 345 |
class LigatureStart(Ligature, _token.MatchStart): |
| 346 |
rx = r"\\\[" |
| 347 |
matchname = "ligature" |
| 348 |
|
| 349 |
|
| 350 |
class LigatureEnd(Ligature, _token.MatchEnd): |
| 351 |
rx = r"\\\]" |
| 352 |
matchname = "ligature" |
| 353 |
|
| 354 |
|
| 355 |
class Tremolo(_token.Token): |
| 356 |
pass |
| 357 |
|
| 358 |
|
| 359 |
class TremoloColon(Tremolo): |
| 360 |
rx = r":" |
| 361 |
def update_state(self, state): |
| 362 |
state.enter(ParseTremolo()) |
| 363 |
|
| 364 |
|
| 365 |
class TremoloDuration(Tremolo, _token.Leaver): |
| 366 |
rx = r"\b(8|16|32|64|128|256|512|1024|2048)(?!\d)" |
| 367 |
|
| 368 |
|
| 369 |
class ChordItem(_token.Token): |
| 370 |
"""Base class for chordmode items.""" |
| 371 |
|
| 372 |
|
| 373 |
class ChordModifier(ChordItem): |
| 374 |
rx = r"((?<![a-z])|^)(aug|dim|sus|min|maj|m)(?![a-z])" |
| 375 |
|
| 376 |
|
| 377 |
class ChordSeparator(ChordItem): |
| 378 |
rx = r":|\^|/\+?" |
| 379 |
|
| 380 |
|
| 381 |
class ChordStepNumber(ChordItem): |
| 382 |
rx = r"\d+[-+]?" |
| 383 |
|
| 384 |
|
| 385 |
class DotChord(ChordItem): |
| 386 |
rx = r"\." |
| 387 |
|
| 388 |
|
| 389 |
class VoiceSeparator(Delimiter): |
| 390 |
rx = r"\\\\" |
| 391 |
|
| 392 |
|
| 393 |
class Dynamic(_token.Token): |
| 394 |
rx = re_dynamic |
| 395 |
|
| 396 |
|
| 397 |
class Command(_token.Item, IdentifierRef): |
| 398 |
@classmethod |
| 399 |
def test_match(cls, match): |
| 400 |
s = match.group()[1:] |
| 401 |
if '-' not in s: |
| 402 |
from .. import words |
| 403 |
return s in words.lilypond_music_commands |
| 404 |
return False |
| 405 |
|
| 406 |
|
| 407 |
class Keyword(_token.Item, IdentifierRef): |
| 408 |
@classmethod |
| 409 |
def test_match(cls, match): |
| 410 |
s = match.group()[1:] |
| 411 |
if '-' not in s: |
| 412 |
from .. import words |
| 413 |
return s in words.lilypond_keywords |
| 414 |
return False |
| 415 |
|
| 416 |
|
| 417 |
class Specifier(_token.Token): |
| 418 |
# a specifier of a command e.g. the name of clef or repeat style. |
| 419 |
pass |
| 420 |
|
| 421 |
|
| 422 |
class Score(Keyword): |
| 423 |
rx = r"\\score" + re_identifier_end |
| 424 |
def update_state(self, state): |
| 425 |
state.enter(ExpectScore()) |
| 426 |
|
| 427 |
|
| 428 |
class Book(Keyword): |
| 429 |
rx = r"\\book" + re_identifier_end |
| 430 |
def update_state(self, state): |
| 431 |
state.enter(ExpectBook()) |
| 432 |
|
| 433 |
|
| 434 |
class BookPart(Keyword): |
| 435 |
rx = r"\\bookpart" + re_identifier_end |
| 436 |
def update_state(self, state): |
| 437 |
state.enter(ExpectBookPart()) |
| 438 |
|
| 439 |
|
| 440 |
class Paper(Keyword): |
| 441 |
rx = r"\\paper" + re_identifier_end |
| 442 |
def update_state(self, state): |
| 443 |
state.enter(ExpectPaper()) |
| 444 |
|
| 445 |
|
| 446 |
class Header(Keyword): |
| 447 |
rx = r"\\header" + re_identifier_end |
| 448 |
def update_state(self, state): |
| 449 |
state.enter(ExpectHeader()) |
| 450 |
|
| 451 |
|
| 452 |
class Layout(Keyword): |
| 453 |
rx = r"\\layout" + re_identifier_end |
| 454 |
def update_state(self, state): |
| 455 |
state.enter(ExpectLayout()) |
| 456 |
|
| 457 |
|
| 458 |
class Midi(Keyword): |
| 459 |
rx = r"\\midi" + re_identifier_end |
| 460 |
def update_state(self, state): |
| 461 |
state.enter(ExpectMidi()) |
| 462 |
|
| 463 |
|
| 464 |
class With(Keyword): |
| 465 |
rx = r"\\with" + re_identifier_end |
| 466 |
def update_state(self, state): |
| 467 |
state.enter(ExpectWith()) |
| 468 |
|
| 469 |
|
| 470 |
class LayoutContext(Keyword): |
| 471 |
rx = r"\\context" + re_identifier_end |
| 472 |
def update_state(self, state): |
| 473 |
state.enter(ExpectContext()) |
| 474 |
|
| 475 |
|
| 476 |
class Markup(_token.Item): |
| 477 |
"""Base class for all markup commands.""" |
| 478 |
|
| 479 |
|
| 480 |
class MarkupStart(Markup, Command): |
| 481 |
rx = r"\\markup" + re_identifier_end |
| 482 |
def update_state(self, state): |
| 483 |
state.enter(ParseMarkup(1)) |
| 484 |
|
| 485 |
|
| 486 |
class MarkupLines(Markup): |
| 487 |
rx = r"\\markuplines" + re_identifier_end |
| 488 |
def update_state(self, state): |
| 489 |
state.enter(ParseMarkup(1)) |
| 490 |
|
| 491 |
|
| 492 |
class MarkupList(Markup): |
| 493 |
rx = r"\\markuplist" + re_identifier_end |
| 494 |
def update_state(self, state): |
| 495 |
state.enter(ParseMarkup(1)) |
| 496 |
|
| 497 |
|
| 498 |
class MarkupCommand(Markup, IdentifierRef): |
| 499 |
"""A markup command.""" |
| 500 |
@classmethod |
| 501 |
def test_match(cls, match): |
| 502 |
from .. import words |
| 503 |
return match.group()[1:] in words.markupcommands |
| 504 |
|
| 505 |
def update_state(self, state): |
| 506 |
from .. import words |
| 507 |
command = self[1:] |
| 508 |
if command in words.markupcommands_nargs[0]: |
| 509 |
state.endArgument() |
| 510 |
else: |
| 511 |
for argcount in 2, 3, 4, 5: |
| 512 |
if command in words.markupcommands_nargs[argcount]: |
| 513 |
break |
| 514 |
else: |
| 515 |
argcount = 1 |
| 516 |
state.enter(ParseMarkup(argcount)) |
| 517 |
|
| 518 |
|
| 519 |
class MarkupScore(Markup): |
| 520 |
rx = r"\\score" + re_identifier_end |
| 521 |
def update_state(self, state): |
| 522 |
state.enter(ExpectScore()) |
| 523 |
|
| 524 |
|
| 525 |
class MarkupUserCommand(Markup, IdentifierRef): |
| 526 |
"""A user-defined markup (i.e. not in the words markupcommands list).""" |
| 527 |
def update_state(self, state): |
| 528 |
state.endArgument() |
| 529 |
|
| 530 |
|
| 531 |
class MarkupWord(_token.Item): |
| 532 |
rx = r'[^{}"\\\s#%]+' |
| 533 |
|
| 534 |
|
| 535 |
class OpenBracketMarkup(OpenBracket): |
| 536 |
def update_state(self, state): |
| 537 |
state.enter(ParseMarkup()) |
| 538 |
|
| 539 |
|
| 540 |
class CloseBracketMarkup(CloseBracket): |
| 541 |
def update_state(self, state): |
| 542 |
# go back to the opening bracket, this is the ParseMarkup |
| 543 |
# parser with the 0 argcount |
| 544 |
while state.parser().argcount > 0: |
| 545 |
state.leave() |
| 546 |
state.leave() |
| 547 |
state.endArgument() |
| 548 |
|
| 549 |
|
| 550 |
class Repeat(Command): |
| 551 |
rx = r"\\repeat" + re_identifier_end |
| 552 |
def update_state(self, state): |
| 553 |
state.enter(ParseRepeat()) |
| 554 |
|
| 555 |
|
| 556 |
class RepeatSpecifier(Specifier): |
| 557 |
@_token.patternproperty |
| 558 |
def rx(): |
| 559 |
from .. import words |
| 560 |
return r"\b({0})(?![A-Za-z])".format("|".join(words.repeat_types)) |
| 561 |
|
| 562 |
|
| 563 |
class RepeatCount(IntegerValue, _token.Leaver): |
| 564 |
pass |
| 565 |
|
| 566 |
|
| 567 |
class Tempo(Command): |
| 568 |
rx = r"\\tempo" + re_identifier_end |
| 569 |
def update_state(self, state): |
| 570 |
state.enter(ParseTempo()) |
| 571 |
|
| 572 |
|
| 573 |
class TempoSeparator(Delimiter): |
| 574 |
rx = r"[-~](?=\s*\d)" |
| 575 |
|
| 576 |
|
| 577 |
class Partial(Command): |
| 578 |
rx = r"\\partial" + re_identifier_end |
| 579 |
|
| 580 |
|
| 581 |
class Override(Keyword): |
| 582 |
rx = r"\\override" + re_identifier_end |
| 583 |
def update_state(self, state): |
| 584 |
state.enter(ParseOverride()) |
| 585 |
|
| 586 |
|
| 587 |
class Set(Override): |
| 588 |
rx = r"\\set" + re_identifier_end |
| 589 |
def update_state(self, state): |
| 590 |
state.enter(ParseSet()) |
| 591 |
|
| 592 |
|
| 593 |
class Revert(Override): |
| 594 |
rx = r"\\revert" + re_identifier_end |
| 595 |
def update_state(self, state): |
| 596 |
state.enter(ParseRevert()) |
| 597 |
|
| 598 |
|
| 599 |
class Unset(Keyword): |
| 600 |
rx = r"\\unset" + re_identifier_end |
| 601 |
def update_state(self, state): |
| 602 |
state.enter(ParseUnset()) |
| 603 |
|
| 604 |
|
| 605 |
class Tweak(Keyword): |
| 606 |
rx = r"\\tweak" + re_identifier_end |
| 607 |
def update_state(self, state): |
| 608 |
state.enter(ParseTweak()) |
| 609 |
|
| 610 |
|
| 611 |
class Translator(Command): |
| 612 |
def update_state(self, state): |
| 613 |
state.enter(ParseTranslator()) |
| 614 |
|
| 615 |
|
| 616 |
class New(Translator): |
| 617 |
rx = r"\\new" + re_identifier_end |
| 618 |
|
| 619 |
|
| 620 |
class Context(Translator): |
| 621 |
rx = r"\\context" + re_identifier_end |
| 622 |
|
| 623 |
|
| 624 |
class Change(Translator): |
| 625 |
rx = r"\\change" + re_identifier_end |
| 626 |
|
| 627 |
|
| 628 |
class AccidentalStyle(Command): |
| 629 |
rx = r"\\accidentalStyle" + re_identifier_end |
| 630 |
def update_state(self, state): |
| 631 |
state.enter(ParseAccidentalStyle()) |
| 632 |
|
| 633 |
|
| 634 |
class AccidentalStyleSpecifier(Specifier): |
| 635 |
@_token.patternproperty |
| 636 |
def rx(): |
| 637 |
from .. import words |
| 638 |
return r"\b({0})(?!-?\w)".format("|".join(words.accidentalstyles)) |
| 639 |
|
| 640 |
|
| 641 |
class AlterBroken(Command): |
| 642 |
rx = r"\\alterBroken" + re_identifier_end |
| 643 |
def update_state(self, state): |
| 644 |
state.enter(ParseAlterBroken()) |
| 645 |
|
| 646 |
|
| 647 |
class Clef(Command): |
| 648 |
rx = r"\\clef" + re_identifier_end |
| 649 |
def update_state(self, state): |
| 650 |
state.enter(ParseClef()) |
| 651 |
|
| 652 |
|
| 653 |
class ClefSpecifier(Specifier): |
| 654 |
@_token.patternproperty |
| 655 |
def rx(): |
| 656 |
from .. import words |
| 657 |
return r"\b({0})\b".format("|".join(words.clefs_plain)) |
| 658 |
|
| 659 |
def update_state(self, state): |
| 660 |
state.leave() |
| 661 |
|
| 662 |
|
| 663 |
class PitchCommand(Command): |
| 664 |
rx = r"\\(relative|transpose|transposition|key|octaveCheck)" + re_identifier_end |
| 665 |
def update_state(self, state): |
| 666 |
argcount = 2 if self == '\\transpose' else 1 |
| 667 |
state.enter(ParsePitchCommand(argcount)) |
| 668 |
|
| 669 |
|
| 670 |
class KeySignatureMode(Command): |
| 671 |
@_token.patternproperty |
| 672 |
def rx(): |
| 673 |
from .. import words |
| 674 |
return r"\\({0})(?![A-Za-z])".format("|".join(words.modes)) |
| 675 |
|
| 676 |
|
| 677 |
class Hide(Keyword): |
| 678 |
rx = r"\\hide" + re_identifier_end |
| 679 |
def update_state(self, state): |
| 680 |
state.enter(ParseHideOmit()) |
| 681 |
|
| 682 |
|
| 683 |
class Omit(Keyword): |
| 684 |
rx = r"\\omit" + re_identifier_end |
| 685 |
def update_state(self, state): |
| 686 |
state.enter(ParseHideOmit()) |
| 687 |
|
| 688 |
|
| 689 |
class Unit(Command): |
| 690 |
rx = r"\\(mm|cm|in|pt|bp)" + re_identifier_end |
| 691 |
|
| 692 |
|
| 693 |
class InputMode(Command): |
| 694 |
pass |
| 695 |
|
| 696 |
|
| 697 |
class LyricMode(InputMode): |
| 698 |
rx = r"\\(lyricmode|((old)?add)?lyrics|lyricsto)" + re_identifier_end |
| 699 |
def update_state(self, state): |
| 700 |
state.enter(ExpectLyricMode()) |
| 701 |
|
| 702 |
|
| 703 |
class Lyric(_token.Item): |
| 704 |
"""Base class for Lyric items.""" |
| 705 |
|
| 706 |
|
| 707 |
class LyricText(Lyric): |
| 708 |
rx = r"[^\\\s\d\"]+" |
| 709 |
|
| 710 |
|
| 711 |
class LyricHyphen(Lyric): |
| 712 |
rx = r"--(?=($|[\s\\]))" |
| 713 |
|
| 714 |
|
| 715 |
class LyricExtender(Lyric): |
| 716 |
rx = r"__(?=($|[\s\\]))" |
| 717 |
|
| 718 |
|
| 719 |
class LyricSkip(Lyric): |
| 720 |
rx = r"_(?=($|[\s\\]))" |
| 721 |
|
| 722 |
|
| 723 |
class Figure(_token.Token): |
| 724 |
"""Base class for Figure items.""" |
| 725 |
|
| 726 |
|
| 727 |
class FigureStart(Figure): |
| 728 |
rx = r"<" |
| 729 |
def update_state(self, state): |
| 730 |
state.enter(ParseFigure()) |
| 731 |
|
| 732 |
|
| 733 |
class FigureEnd(Figure, _token.Leaver): |
| 734 |
rx = r">" |
| 735 |
|
| 736 |
|
| 737 |
class FigureBracket(Figure): |
| 738 |
rx = r"[][]" |
| 739 |
|
| 740 |
|
| 741 |
class FigureStep(Figure): |
| 742 |
"""A step figure number or the underscore.""" |
| 743 |
rx = r"_|\d+" |
| 744 |
|
| 745 |
|
| 746 |
class FigureAccidental(Figure): |
| 747 |
"""A figure accidental.""" |
| 748 |
rx = r"[-+!]+" |
| 749 |
|
| 750 |
|
| 751 |
class FigureModifier(Figure): |
| 752 |
"""A figure modifier.""" |
| 753 |
rx = r"\\[\\!+]|/" |
| 754 |
|
| 755 |
|
| 756 |
class NoteMode(InputMode): |
| 757 |
rx = r"\\(notes|notemode)" + re_identifier_end |
| 758 |
def update_state(self, state): |
| 759 |
state.enter(ExpectNoteMode()) |
| 760 |
|
| 761 |
|
| 762 |
class ChordMode(InputMode): |
| 763 |
rx = r"\\(chords|chordmode)" + re_identifier_end |
| 764 |
def update_state(self, state): |
| 765 |
state.enter(ExpectChordMode()) |
| 766 |
|
| 767 |
|
| 768 |
class DrumMode(InputMode): |
| 769 |
rx = r"\\(drums|drummode)" + re_identifier_end |
| 770 |
def update_state(self, state): |
| 771 |
state.enter(ExpectDrumMode()) |
| 772 |
|
| 773 |
|
| 774 |
class FigureMode(InputMode): |
| 775 |
rx = r"\\(figures|figuremode)"+ re_identifier_end |
| 776 |
def update_state(self, state): |
| 777 |
state.enter(ExpectFigureMode()) |
| 778 |
|
| 779 |
|
| 780 |
class UserCommand(IdentifierRef): |
| 781 |
pass |
| 782 |
|
| 783 |
|
| 784 |
class SimultaneousOrSequentialCommand(Keyword): |
| 785 |
rx = r"\\(simultaneous|sequential)" + re_identifier_end |
| 786 |
|
| 787 |
|
| 788 |
class SchemeStart(_token.Item): |
| 789 |
rx = "[#$](?![{}])" |
| 790 |
def update_state(self, state): |
| 791 |
from . import scheme |
| 792 |
state.enter(scheme.ParseScheme(1)) |
| 793 |
|
| 794 |
|
| 795 |
class ContextName(_token.Token): |
| 796 |
@_token.patternproperty |
| 797 |
def rx(): |
| 798 |
from .. import words |
| 799 |
return r"\b({0})\b".format("|".join(words.contexts)) |
| 800 |
|
| 801 |
|
| 802 |
class BackSlashedContextName(ContextName): |
| 803 |
@_token.patternproperty |
| 804 |
def rx(): |
| 805 |
from .. import words |
| 806 |
return r"\\({0})\b".format("|".join(words.contexts)) |
| 807 |
|
| 808 |
|
| 809 |
class GrobName(_token.Token): |
| 810 |
@_token.patternproperty |
| 811 |
def rx(): |
| 812 |
from .. import data |
| 813 |
return r"\b({0})\b".format("|".join(data.grobs())) |
| 814 |
|
| 815 |
|
| 816 |
class GrobProperty(Variable): |
| 817 |
rx = r"\b([a-z]+|[XY])(-([a-z]+|[XY]))*(?![\w])" |
| 818 |
|
| 819 |
|
| 820 |
class ContextProperty(Variable): |
| 821 |
@_token.patternproperty |
| 822 |
def rx(): |
| 823 |
from .. import data |
| 824 |
return r"\b({0})\b".format("|".join(data.context_properties())) |
| 825 |
|
| 826 |
|
| 827 |
class PaperVariable(Variable): |
| 828 |
"""A variable inside Paper. Always follow this one by UserVariable.""" |
| 829 |
@classmethod |
| 830 |
def test_match(cls, match): |
| 831 |
from .. import words |
| 832 |
return match.group() in words.papervariables |
| 833 |
|
| 834 |
|
| 835 |
class HeaderVariable(Variable): |
| 836 |
"""A variable inside Header. Always follow this one by UserVariable.""" |
| 837 |
@classmethod |
| 838 |
def test_match(cls, match): |
| 839 |
from .. import words |
| 840 |
return match.group() in words.headervariables |
| 841 |
|
| 842 |
|
| 843 |
class LayoutVariable(Variable): |
| 844 |
"""A variable inside Header. Always follow this one by UserVariable.""" |
| 845 |
@classmethod |
| 846 |
def test_match(cls, match): |
| 847 |
from .. import words |
| 848 |
return match.group() in words.layoutvariables |
| 849 |
|
| 850 |
|
| 851 |
class Chord(_token.Token): |
| 852 |
"""Base class for Chord delimiters.""" |
| 853 |
pass |
| 854 |
|
| 855 |
|
| 856 |
class ChordStart(Chord): |
| 857 |
rx = r"<" |
| 858 |
def update_state(self, state): |
| 859 |
state.enter(ParseChord()) |
| 860 |
|
| 861 |
|
| 862 |
class ChordEnd(Chord, _token.Leaver): |
| 863 |
rx = r">" |
| 864 |
|
| 865 |
|
| 866 |
class DrumChordStart(ChordStart): |
| 867 |
def update_state(self, state): |
| 868 |
state.enter(ParseDrumChord()) |
| 869 |
|
| 870 |
|
| 871 |
class DrumChordEnd(ChordEnd): |
| 872 |
pass |
| 873 |
|
| 874 |
|
| 875 |
class ErrorInChord(Error): |
| 876 |
rx = "|".join(( |
| 877 |
re_articulation, # articulation |
| 878 |
r"<<|>>", # double french quotes |
| 879 |
r"\\[\\\]\[\(\)()]", # slurs beams |
| 880 |
re_duration, # duration |
| 881 |
re_scaling, # scaling |
| 882 |
)) |
| 883 |
|
| 884 |
|
| 885 |
class Name(UserVariable): |
| 886 |
r"""A variable name without \ prefix.""" |
| 887 |
|
| 888 |
|
| 889 |
class EqualSign(_token.Token): |
| 890 |
rx = r"=" |
| 891 |
|
| 892 |
|
| 893 |
# Parsers |
| 894 |
class ParseLilyPond(Parser): |
| 895 |
mode = 'lilypond' |
| 896 |
|
| 897 |
# basic stuff that can appear everywhere |
| 898 |
space_items = ( |
| 899 |
_token.Space, |
| 900 |
BlockCommentStart, |
| 901 |
LineComment, |
| 902 |
) |
| 903 |
|
| 904 |
|
| 905 |
base_items = space_items + ( |
| 906 |
SchemeStart, |
| 907 |
StringQuotedStart, |
| 908 |
) |
| 909 |
|
| 910 |
|
| 911 |
# items that represent commands in both toplevel and music mode |
| 912 |
command_items = ( |
| 913 |
Repeat, |
| 914 |
PitchCommand, |
| 915 |
Override, Revert, |
| 916 |
Set, Unset, |
| 917 |
Hide, Omit, |
| 918 |
Tweak, |
| 919 |
New, Context, Change, |
| 920 |
With, |
| 921 |
Clef, |
| 922 |
Tempo, |
| 923 |
Partial, |
| 924 |
KeySignatureMode, |
| 925 |
AccidentalStyle, |
| 926 |
AlterBroken, |
| 927 |
SimultaneousOrSequentialCommand, |
| 928 |
ChordMode, DrumMode, FigureMode, LyricMode, NoteMode, |
| 929 |
MarkupStart, MarkupLines, MarkupList, |
| 930 |
ArticulationCommand, |
| 931 |
Keyword, |
| 932 |
Command, |
| 933 |
SimultaneousOrSequentialCommand, |
| 934 |
UserCommand, |
| 935 |
) |
| 936 |
|
| 937 |
|
| 938 |
# items that occur in toplevel, book, bookpart or score |
| 939 |
# no Leave-tokens! |
| 940 |
toplevel_base_items = base_items + ( |
| 941 |
SequentialStart, |
| 942 |
SimultaneousStart, |
| 943 |
) + command_items |
| 944 |
|
| 945 |
|
| 946 |
# items that occur in music expressions |
| 947 |
music_items = base_items + ( |
| 948 |
Dynamic, |
| 949 |
Skip, |
| 950 |
Spacer, |
| 951 |
Q, |
| 952 |
Rest, |
| 953 |
Note, |
| 954 |
Fraction, |
| 955 |
Length, |
| 956 |
Octave, |
| 957 |
OctaveCheck, |
| 958 |
AccidentalCautionary, |
| 959 |
AccidentalReminder, |
| 960 |
PipeSymbol, |
| 961 |
VoiceSeparator, |
| 962 |
SequentialStart, SequentialEnd, |
| 963 |
SimultaneousStart, SimultaneousEnd, |
| 964 |
ChordStart, |
| 965 |
ContextName, |
| 966 |
GrobName, |
| 967 |
SlurStart, SlurEnd, |
| 968 |
PhrasingSlurStart, PhrasingSlurEnd, |
| 969 |
Tie, |
| 970 |
BeamStart, BeamEnd, |
| 971 |
LigatureStart, LigatureEnd, |
| 972 |
Direction, |
| 973 |
StringNumber, |
| 974 |
IntegerValue, |
| 975 |
) + command_items |
| 976 |
|
| 977 |
|
| 978 |
# items that occur inside chords |
| 979 |
music_chord_items = ( |
| 980 |
ErrorInChord, |
| 981 |
ChordEnd, |
| 982 |
) + music_items |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
class ParseGlobal(ParseLilyPond): |
| 987 |
"""Parses LilyPond from the toplevel of a file.""" |
| 988 |
items = ( |
| 989 |
Book, |
| 990 |
BookPart, |
| 991 |
Score, |
| 992 |
MarkupStart, MarkupLines, MarkupList, |
| 993 |
Paper, Header, Layout, |
| 994 |
) + toplevel_base_items + ( |
| 995 |
Name, |
| 996 |
DotPath, |
| 997 |
EqualSign, |
| 998 |
Fraction, |
| 999 |
DecimalValue, |
| 1000 |
) |
| 1001 |
def update_state(self, state, token): |
| 1002 |
if isinstance(token, EqualSign): |
| 1003 |
state.enter(ParseGlobalAssignment()) |
| 1004 |
|
| 1005 |
|
| 1006 |
class ParseGlobalAssignment(FallthroughParser, ParseLilyPond): |
| 1007 |
items = space_items + ( |
| 1008 |
Skip, |
| 1009 |
Spacer, |
| 1010 |
Q, |
| 1011 |
Rest, |
| 1012 |
Note, |
| 1013 |
Length, |
| 1014 |
Fraction, |
| 1015 |
DecimalValue, |
| 1016 |
Direction, |
| 1017 |
StringNumber, |
| 1018 |
Dynamic, |
| 1019 |
) |
| 1020 |
|
| 1021 |
|
| 1022 |
class ExpectOpenBracket(FallthroughParser, ParseLilyPond): |
| 1023 |
"""Waits for an OpenBracket and then replaces the parser with the class set in the replace attribute. |
| 1024 |
|
| 1025 |
Subclass this to set the destination for the OpenBracket. |
| 1026 |
|
| 1027 |
""" |
| 1028 |
default = Error |
| 1029 |
items = space_items + ( |
| 1030 |
OpenBracket, |
| 1031 |
) |
| 1032 |
def update_state(self, state, token): |
| 1033 |
if isinstance(token, OpenBracket): |
| 1034 |
state.replace(self.replace()) |
| 1035 |
|
| 1036 |
|
| 1037 |
class ExpectMusicList(FallthroughParser, ParseLilyPond): |
| 1038 |
"""Waits for an OpenBracket or << and then replaces the parser with the class set in the replace attribute. |
| 1039 |
|
| 1040 |
Subclass this to set the destination for the OpenBracket. |
| 1041 |
|
| 1042 |
""" |
| 1043 |
items = space_items + ( |
| 1044 |
OpenBracket, |
| 1045 |
OpenSimultaneous, |
| 1046 |
SimultaneousOrSequentialCommand, |
| 1047 |
) |
| 1048 |
def update_state(self, state, token): |
| 1049 |
if isinstance(token, (OpenBracket, OpenSimultaneous)): |
| 1050 |
state.replace(self.replace()) |
| 1051 |
|
| 1052 |
|
| 1053 |
class ParseScore(ParseLilyPond): |
| 1054 |
r"""Parses the expression after ``\score {``, leaving at ``}`` """ |
| 1055 |
items = ( |
| 1056 |
CloseBracket, |
| 1057 |
Header, Layout, Midi, With, |
| 1058 |
) + toplevel_base_items |
| 1059 |
|
| 1060 |
|
| 1061 |
class ExpectScore(ExpectOpenBracket): |
| 1062 |
replace = ParseScore |
| 1063 |
|
| 1064 |
|
| 1065 |
class ParseBook(ParseLilyPond): |
| 1066 |
r"""Parses the expression after ``\book {``, leaving at ``}`` """ |
| 1067 |
items = ( |
| 1068 |
CloseBracket, |
| 1069 |
MarkupStart, MarkupLines, MarkupList, |
| 1070 |
BookPart, |
| 1071 |
Score, |
| 1072 |
Paper, Header, Layout, |
| 1073 |
) + toplevel_base_items |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
class ExpectBook(ExpectOpenBracket): |
| 1078 |
replace = ParseBook |
| 1079 |
|
| 1080 |
|
| 1081 |
class ParseBookPart(ParseLilyPond): |
| 1082 |
r"""Parses the expression after ``\bookpart {``, leaving at ``}`` """ |
| 1083 |
items = ( |
| 1084 |
CloseBracket, |
| 1085 |
MarkupStart, MarkupLines, MarkupList, |
| 1086 |
Score, |
| 1087 |
Paper, Header, Layout, |
| 1088 |
) + toplevel_base_items |
| 1089 |
|
| 1090 |
|
| 1091 |
class ExpectBookPart(ExpectOpenBracket): |
| 1092 |
replace = ParseBookPart |
| 1093 |
|
| 1094 |
|
| 1095 |
class ParsePaper(ParseLilyPond): |
| 1096 |
r"""Parses the expression after ``\paper {``, leaving at ``}`` """ |
| 1097 |
items = base_items + ( |
| 1098 |
CloseBracket, |
| 1099 |
MarkupStart, MarkupLines, MarkupList, |
| 1100 |
PaperVariable, |
| 1101 |
UserVariable, |
| 1102 |
EqualSign, |
| 1103 |
DotPath, |
| 1104 |
DecimalValue, |
| 1105 |
Unit, |
| 1106 |
) |
| 1107 |
|
| 1108 |
|
| 1109 |
class ExpectPaper(ExpectOpenBracket): |
| 1110 |
replace = ParsePaper |
| 1111 |
|
| 1112 |
|
| 1113 |
class ParseHeader(ParseLilyPond): |
| 1114 |
r"""Parses the expression after ``\header {``, leaving at ``}`` """ |
| 1115 |
items = ( |
| 1116 |
CloseBracket, |
| 1117 |
MarkupStart, MarkupLines, MarkupList, |
| 1118 |
HeaderVariable, |
| 1119 |
UserVariable, |
| 1120 |
EqualSign, |
| 1121 |
DotPath, |
| 1122 |
) + toplevel_base_items |
| 1123 |
|
| 1124 |
|
| 1125 |
class ExpectHeader(ExpectOpenBracket): |
| 1126 |
replace = ParseHeader |
| 1127 |
|
| 1128 |
|
| 1129 |
class ParseLayout(ParseLilyPond): |
| 1130 |
r"""Parses the expression after ``\layout {``, leaving at ``}`` """ |
| 1131 |
items = base_items + ( |
| 1132 |
CloseBracket, |
| 1133 |
LayoutContext, |
| 1134 |
LayoutVariable, |
| 1135 |
UserVariable, |
| 1136 |
EqualSign, |
| 1137 |
DotPath, |
| 1138 |
DecimalValue, |
| 1139 |
Unit, |
| 1140 |
ContextName, |
| 1141 |
GrobName, |
| 1142 |
) + command_items |
| 1143 |
|
| 1144 |
|
| 1145 |
class ExpectLayout(ExpectOpenBracket): |
| 1146 |
replace = ParseLayout |
| 1147 |
|
| 1148 |
|
| 1149 |
class ParseMidi(ParseLilyPond): |
| 1150 |
r"""Parses the expression after ``\midi {``, leaving at ``}`` """ |
| 1151 |
items = base_items + ( |
| 1152 |
CloseBracket, |
| 1153 |
LayoutContext, |
| 1154 |
LayoutVariable, |
| 1155 |
UserVariable, |
| 1156 |
EqualSign, |
| 1157 |
DotPath, |
| 1158 |
DecimalValue, |
| 1159 |
Unit, |
| 1160 |
ContextName, |
| 1161 |
GrobName, |
| 1162 |
) + command_items |
| 1163 |
|
| 1164 |
|
| 1165 |
class ExpectMidi(ExpectOpenBracket): |
| 1166 |
replace = ParseMidi |
| 1167 |
|
| 1168 |
|
| 1169 |
class ParseWith(ParseLilyPond): |
| 1170 |
r"""Parses the expression after ``\with {``, leaving at ``}`` """ |
| 1171 |
items = ( |
| 1172 |
CloseBracket, |
| 1173 |
ContextName, |
| 1174 |
GrobName, |
| 1175 |
ContextProperty, |
| 1176 |
EqualSign, |
| 1177 |
DotPath, |
| 1178 |
) + toplevel_base_items |
| 1179 |
|
| 1180 |
|
| 1181 |
class ExpectWith(ExpectOpenBracket): |
| 1182 |
replace = ParseWith |
| 1183 |
|
| 1184 |
|
| 1185 |
class ParseContext(ParseLilyPond): |
| 1186 |
r"""Parses the expression after (``\layout {``) ``\context {``, leaving at ``}`` """ |
| 1187 |
items = ( |
| 1188 |
CloseBracket, |
| 1189 |
BackSlashedContextName, |
| 1190 |
ContextProperty, |
| 1191 |
EqualSign, |
| 1192 |
DotPath, |
| 1193 |
) + toplevel_base_items |
| 1194 |
|
| 1195 |
|
| 1196 |
class ExpectContext(ExpectOpenBracket): |
| 1197 |
replace = ParseContext |
| 1198 |
|
| 1199 |
|
| 1200 |
class ParseMusic(ParseLilyPond): |
| 1201 |
"""Parses LilyPond music expressions.""" |
| 1202 |
items = music_items + ( |
| 1203 |
TremoloColon, |
| 1204 |
) |
| 1205 |
|
| 1206 |
|
| 1207 |
class ParseChord(ParseMusic): |
| 1208 |
"""LilyPond inside chords ``< >``""" |
| 1209 |
items = music_chord_items |
| 1210 |
|
| 1211 |
|
| 1212 |
class ParseString(Parser): |
| 1213 |
default = String |
| 1214 |
items = ( |
| 1215 |
StringQuotedEnd, |
| 1216 |
StringQuoteEscape, |
| 1217 |
) |
| 1218 |
|
| 1219 |
|
| 1220 |
class ParseBlockComment(Parser): |
| 1221 |
default = BlockComment |
| 1222 |
items = ( |
| 1223 |
BlockCommentEnd, |
| 1224 |
) |
| 1225 |
|
| 1226 |
|
| 1227 |
class ParseMarkup(Parser): |
| 1228 |
items = ( |
| 1229 |
MarkupScore, |
| 1230 |
MarkupCommand, |
| 1231 |
MarkupUserCommand, |
| 1232 |
OpenBracketMarkup, |
| 1233 |
CloseBracketMarkup, |
| 1234 |
MarkupWord, |
| 1235 |
) + base_items |
| 1236 |
|
| 1237 |
|
| 1238 |
class ParseRepeat(FallthroughParser): |
| 1239 |
items = space_items + ( |
| 1240 |
RepeatSpecifier, |
| 1241 |
StringQuotedStart, |
| 1242 |
RepeatCount, |
| 1243 |
) |
| 1244 |
|
| 1245 |
|
| 1246 |
class ParseTempo(FallthroughParser): |
| 1247 |
items = space_items + ( |
| 1248 |
MarkupStart, |
| 1249 |
StringQuotedStart, |
| 1250 |
SchemeStart, |
| 1251 |
Length, |
| 1252 |
EqualSign, |
| 1253 |
) |
| 1254 |
def update_state(self, state, token): |
| 1255 |
if isinstance(token, EqualSign): |
| 1256 |
state.replace(ParseTempoAfterEqualSign()) |
| 1257 |
|
| 1258 |
|
| 1259 |
class ParseTempoAfterEqualSign(FallthroughParser): |
| 1260 |
items = space_items + ( |
| 1261 |
IntegerValue, |
| 1262 |
TempoSeparator, |
| 1263 |
) |
| 1264 |
|
| 1265 |
|
| 1266 |
class ParseDuration(FallthroughParser): |
| 1267 |
items = space_items + ( |
| 1268 |
Dot, |
| 1269 |
) |
| 1270 |
def fallthrough(self, state): |
| 1271 |
state.replace(ParseDurationScaling()) |
| 1272 |
|
| 1273 |
|
| 1274 |
class ParseDurationScaling(ParseDuration): |
| 1275 |
items = space_items + ( |
| 1276 |
Scaling, |
| 1277 |
) |
| 1278 |
def fallthrough(self, state): |
| 1279 |
state.leave() |
| 1280 |
|
| 1281 |
|
| 1282 |
class ParseOverride(ParseLilyPond): |
| 1283 |
argcount = 0 |
| 1284 |
items = ( |
| 1285 |
ContextName, |
| 1286 |
DotPath, |
| 1287 |
GrobName, |
| 1288 |
GrobProperty, |
| 1289 |
EqualSign, |
| 1290 |
) + base_items |
| 1291 |
def update_state(self, state, token): |
| 1292 |
if isinstance(token, EqualSign): |
| 1293 |
state.replace(ParseDecimalValue()) |
| 1294 |
|
| 1295 |
|
| 1296 |
class ParseRevert(FallthroughParser): |
| 1297 |
r"""parse the arguments of ``\revert``""" |
| 1298 |
# allow both the old scheme syntax but also the dotted 2.18+ syntax |
| 1299 |
# allow either a dot between the GrobName and the property path or not |
| 1300 |
# correctly fall through when one property path has been parsed |
| 1301 |
# (uses ParseGrobPropertyPath and ExpectGrobProperty) |
| 1302 |
# (When the old scheme syntax is used this parser also falls through, |
| 1303 |
# assuming that the previous parser will handle it) |
| 1304 |
items = space_items + ( |
| 1305 |
ContextName, |
| 1306 |
DotPath, |
| 1307 |
GrobName, |
| 1308 |
GrobProperty, |
| 1309 |
) |
| 1310 |
def update_state(self, state, token): |
| 1311 |
if isinstance(token, GrobProperty): |
| 1312 |
state.replace(ParseGrobPropertyPath()) |
| 1313 |
|
| 1314 |
|
| 1315 |
class ParseGrobPropertyPath(FallthroughParser): |
| 1316 |
items = space_items + ( |
| 1317 |
DotPath, |
| 1318 |
) |
| 1319 |
def update_state(self, state, token): |
| 1320 |
if isinstance(token, DotPath): |
| 1321 |
state.enter(ExpectGrobProperty()) |
| 1322 |
|
| 1323 |
|
| 1324 |
class ExpectGrobProperty(FallthroughParser): |
| 1325 |
items = space_items + ( |
| 1326 |
GrobProperty, |
| 1327 |
) |
| 1328 |
def update_state(self, state, token): |
| 1329 |
if isinstance(token, GrobProperty): |
| 1330 |
state.leave() |
| 1331 |
|
| 1332 |
|
| 1333 |
class ParseSet(ParseLilyPond): |
| 1334 |
argcount = 0 |
| 1335 |
items = ( |
| 1336 |
ContextName, |
| 1337 |
DotPath, |
| 1338 |
ContextProperty, |
| 1339 |
EqualSign, |
| 1340 |
Name, |
| 1341 |
) + base_items |
| 1342 |
def update_state(self, state, token): |
| 1343 |
if isinstance(token, EqualSign): |
| 1344 |
state.replace(ParseDecimalValue()) |
| 1345 |
|
| 1346 |
|
| 1347 |
class ParseUnset(FallthroughParser): |
| 1348 |
items = space_items + ( |
| 1349 |
ContextName, |
| 1350 |
DotPath, |
| 1351 |
ContextProperty, |
| 1352 |
Name, |
| 1353 |
) |
| 1354 |
def update_state(self, state, token): |
| 1355 |
if isinstance(token, ContextProperty) or token[:1].islower(): |
| 1356 |
state.leave() |
| 1357 |
|
| 1358 |
|
| 1359 |
class ParseTweak(FallthroughParser): |
| 1360 |
items = space_items + ( |
| 1361 |
GrobName, |
| 1362 |
DotPath, |
| 1363 |
GrobProperty, |
| 1364 |
) |
| 1365 |
def update_state(self, state, token): |
| 1366 |
if isinstance(token, GrobProperty): |
| 1367 |
state.replace(ParseTweakGrobProperty()) |
| 1368 |
|
| 1369 |
|
| 1370 |
class ParseTweakGrobProperty(FallthroughParser): |
| 1371 |
items = space_items + ( |
| 1372 |
DotPath, |
| 1373 |
DecimalValue, |
| 1374 |
) |
| 1375 |
def update_state(self, state, token): |
| 1376 |
if isinstance(token, DotPath): |
| 1377 |
state.enter(ExpectGrobProperty()) |
| 1378 |
elif isinstance(token, DecimalValue): |
| 1379 |
state.leave() |
| 1380 |
|
| 1381 |
|
| 1382 |
class ParseTranslator(FallthroughParser): |
| 1383 |
items = space_items + ( |
| 1384 |
ContextName, |
| 1385 |
Name, |
| 1386 |
) |
| 1387 |
|
| 1388 |
def update_state(self, state, token): |
| 1389 |
if isinstance(token, (Name, ContextName)): |
| 1390 |
state.replace(ExpectTranslatorId()) |
| 1391 |
|
| 1392 |
|
| 1393 |
class ExpectTranslatorId(FallthroughParser): |
| 1394 |
items = space_items + ( |
| 1395 |
EqualSign, |
| 1396 |
) |
| 1397 |
|
| 1398 |
def update_state(self, state, token): |
| 1399 |
if token == '=': |
| 1400 |
state.replace(ParseTranslatorId()) |
| 1401 |
|
| 1402 |
|
| 1403 |
class ParseTranslatorId(FallthroughParser): |
| 1404 |
argcount = 1 |
| 1405 |
items = space_items + ( |
| 1406 |
Name, |
| 1407 |
StringQuotedStart, |
| 1408 |
) |
| 1409 |
|
| 1410 |
def update_state(self, state, token): |
| 1411 |
if isinstance(token, Name): |
| 1412 |
state.leave() |
| 1413 |
|
| 1414 |
|
| 1415 |
class ParseClef(FallthroughParser): |
| 1416 |
argcount = 1 |
| 1417 |
items = space_items + ( |
| 1418 |
ClefSpecifier, |
| 1419 |
StringQuotedStart, |
| 1420 |
) |
| 1421 |
|
| 1422 |
|
| 1423 |
class ParseHideOmit(FallthroughParser): |
| 1424 |
items = space_items + ( |
| 1425 |
ContextName, |
| 1426 |
DotPath, |
| 1427 |
GrobName, |
| 1428 |
) |
| 1429 |
def update_state(self, state, token): |
| 1430 |
if isinstance(token, GrobName): |
| 1431 |
state.leave() |
| 1432 |
|
| 1433 |
|
| 1434 |
class ParseAccidentalStyle(FallthroughParser): |
| 1435 |
items = space_items + ( |
| 1436 |
ContextName, |
| 1437 |
DotPath, |
| 1438 |
AccidentalStyleSpecifier, |
| 1439 |
) |
| 1440 |
def update_state(self, state, token): |
| 1441 |
if isinstance(token, AccidentalStyleSpecifier): |
| 1442 |
state.leave() |
| 1443 |
|
| 1444 |
|
| 1445 |
class ParseAlterBroken(FallthroughParser): |
| 1446 |
items = space_items + ( |
| 1447 |
GrobProperty, |
| 1448 |
) |
| 1449 |
def update_state(self, state, token): |
| 1450 |
if isinstance(token, GrobProperty): |
| 1451 |
state.replace(ParseGrobPropertyPath()) |
| 1452 |
|
| 1453 |
|
| 1454 |
class ParseScriptAbbreviationOrFingering(FallthroughParser): |
| 1455 |
argcount = 1 |
| 1456 |
items = space_items + ( |
| 1457 |
ScriptAbbreviation, |
| 1458 |
Fingering, |
| 1459 |
) |
| 1460 |
|
| 1461 |
|
| 1462 |
class ParseInputMode(ParseLilyPond): |
| 1463 |
"""Base class for parser for mode-changing music commands.""" |
| 1464 |
@classmethod |
| 1465 |
def update_state(cls, state, token): |
| 1466 |
if isinstance(token, (OpenSimultaneous, OpenBracket)): |
| 1467 |
state.enter(cls()) |
| 1468 |
|
| 1469 |
|
| 1470 |
class ParseLyricMode(ParseInputMode): |
| 1471 |
r"""Parser for ``\lyrics``, ``\lyricmode``, ``\addlyrics``, etc.""" |
| 1472 |
items = base_items + ( |
| 1473 |
CloseBracket, |
| 1474 |
CloseSimultaneous, |
| 1475 |
OpenBracket, |
| 1476 |
OpenSimultaneous, |
| 1477 |
PipeSymbol, |
| 1478 |
LyricHyphen, |
| 1479 |
LyricExtender, |
| 1480 |
LyricSkip, |
| 1481 |
LyricText, |
| 1482 |
Dynamic, |
| 1483 |
Skip, |
| 1484 |
Length, |
| 1485 |
MarkupStart, MarkupLines, MarkupList, |
| 1486 |
) + command_items |
| 1487 |
|
| 1488 |
|
| 1489 |
class ExpectLyricMode(ExpectMusicList): |
| 1490 |
replace = ParseLyricMode |
| 1491 |
items = space_items + ( |
| 1492 |
OpenBracket, |
| 1493 |
OpenSimultaneous, |
| 1494 |
SchemeStart, |
| 1495 |
StringQuotedStart, |
| 1496 |
Name, |
| 1497 |
SimultaneousOrSequentialCommand, |
| 1498 |
) |
| 1499 |
|
| 1500 |
|
| 1501 |
class ParseChordMode(ParseInputMode, ParseMusic): |
| 1502 |
r"""Parser for ``\chords`` and ``\chordmode``.""" |
| 1503 |
items = ( |
| 1504 |
OpenBracket, |
| 1505 |
OpenSimultaneous, |
| 1506 |
) + music_items + ( # TODO: specify items exactly, e.g. < > is not allowed |
| 1507 |
ChordSeparator, |
| 1508 |
) |
| 1509 |
def update_state(self, state, token): |
| 1510 |
if isinstance(token, ChordSeparator): |
| 1511 |
state.enter(ParseChordItems()) |
| 1512 |
else: |
| 1513 |
super(ParseChordMode, self).update_state(state, token) |
| 1514 |
|
| 1515 |
|
| 1516 |
class ExpectChordMode(ExpectMusicList): |
| 1517 |
replace = ParseChordMode |
| 1518 |
|
| 1519 |
|
| 1520 |
class ParseNoteMode(ParseMusic): |
| 1521 |
r"""Parser for ``\notes`` and ``\notemode``. Same as Music itself.""" |
| 1522 |
|
| 1523 |
|
| 1524 |
class ExpectNoteMode(ExpectMusicList): |
| 1525 |
replace = ParseNoteMode |
| 1526 |
|
| 1527 |
|
| 1528 |
class ParseDrumChord(ParseMusic): |
| 1529 |
"""LilyPond inside chords in drummode ``< >``""" |
| 1530 |
items = base_items + ( |
| 1531 |
ErrorInChord, |
| 1532 |
DrumChordEnd, |
| 1533 |
Dynamic, |
| 1534 |
Skip, |
| 1535 |
Spacer, |
| 1536 |
Q, |
| 1537 |
Rest, |
| 1538 |
DrumNote, |
| 1539 |
Fraction, |
| 1540 |
Length, |
| 1541 |
PipeSymbol, |
| 1542 |
VoiceSeparator, |
| 1543 |
SequentialStart, SequentialEnd, |
| 1544 |
SimultaneousStart, SimultaneousEnd, |
| 1545 |
ChordStart, |
| 1546 |
ContextName, |
| 1547 |
GrobName, |
| 1548 |
SlurStart, SlurEnd, |
| 1549 |
PhrasingSlurStart, PhrasingSlurEnd, |
| 1550 |
Tie, |
| 1551 |
BeamStart, BeamEnd, |
| 1552 |
LigatureStart, LigatureEnd, |
| 1553 |
Direction, |
| 1554 |
StringNumber, |
| 1555 |
IntegerValue, |
| 1556 |
) + command_items |
| 1557 |
|
| 1558 |
|
| 1559 |
class ParseDrumMode(ParseInputMode, ParseMusic): |
| 1560 |
r"""Parser for ``\drums`` and ``\drummode``.""" |
| 1561 |
items = ( |
| 1562 |
OpenBracket, |
| 1563 |
OpenSimultaneous, |
| 1564 |
) + base_items + ( |
| 1565 |
Dynamic, |
| 1566 |
Skip, |
| 1567 |
Spacer, |
| 1568 |
Q, |
| 1569 |
Rest, |
| 1570 |
DrumNote, |
| 1571 |
Fraction, |
| 1572 |
Length, |
| 1573 |
PipeSymbol, |
| 1574 |
VoiceSeparator, |
| 1575 |
SequentialStart, SequentialEnd, |
| 1576 |
SimultaneousStart, SimultaneousEnd, |
| 1577 |
DrumChordStart, |
| 1578 |
ContextName, |
| 1579 |
GrobName, |
| 1580 |
SlurStart, SlurEnd, |
| 1581 |
PhrasingSlurStart, PhrasingSlurEnd, |
| 1582 |
Tie, |
| 1583 |
BeamStart, BeamEnd, |
| 1584 |
LigatureStart, LigatureEnd, |
| 1585 |
Direction, |
| 1586 |
StringNumber, |
| 1587 |
IntegerValue, |
| 1588 |
) + command_items |
| 1589 |
|
| 1590 |
|
| 1591 |
class ExpectDrumMode(ExpectMusicList): |
| 1592 |
replace = ParseDrumMode |
| 1593 |
|
| 1594 |
|
| 1595 |
class ParseFigureMode(ParseInputMode, ParseMusic): |
| 1596 |
r"""Parser for ``\figures`` and ``\figuremode``.""" |
| 1597 |
items = base_items + ( |
| 1598 |
CloseBracket, |
| 1599 |
CloseSimultaneous, |
| 1600 |
OpenBracket, |
| 1601 |
OpenSimultaneous, |
| 1602 |
PipeSymbol, |
| 1603 |
FigureStart, |
| 1604 |
Skip, Spacer, Rest, |
| 1605 |
Length, |
| 1606 |
) + command_items |
| 1607 |
|
| 1608 |
|
| 1609 |
class ParseFigure(Parser): |
| 1610 |
"""Parse inside ``< >`` in figure mode.""" |
| 1611 |
items = base_items + ( |
| 1612 |
FigureEnd, |
| 1613 |
FigureBracket, |
| 1614 |
FigureStep, |
| 1615 |
FigureAccidental, |
| 1616 |
FigureModifier, |
| 1617 |
MarkupStart, MarkupLines, MarkupList, |
| 1618 |
) |
| 1619 |
|
| 1620 |
|
| 1621 |
class ExpectFigureMode(ExpectMusicList): |
| 1622 |
replace = ParseFigureMode |
| 1623 |
|
| 1624 |
|
| 1625 |
class ParsePitchCommand(FallthroughParser): |
| 1626 |
argcount = 1 |
| 1627 |
items = space_items + ( |
| 1628 |
Note, |
| 1629 |
Octave, |
| 1630 |
) |
| 1631 |
def update_state(self, state, token): |
| 1632 |
if isinstance(token, Note): |
| 1633 |
self.argcount -= 1 |
| 1634 |
elif isinstance(token, _token.Space) and self.argcount <= 0: |
| 1635 |
state.leave() |
| 1636 |
|
| 1637 |
|
| 1638 |
class ParseTremolo(FallthroughParser): |
| 1639 |
items = (TremoloDuration,) |
| 1640 |
|
| 1641 |
|
| 1642 |
class ParseChordItems(FallthroughParser): |
| 1643 |
items = ( |
| 1644 |
ChordSeparator, |
| 1645 |
ChordModifier, |
| 1646 |
ChordStepNumber, |
| 1647 |
DotChord, |
| 1648 |
Note, |
| 1649 |
) |
| 1650 |
|
| 1651 |
|
| 1652 |
class ParseDecimalValue(FallthroughParser): |
| 1653 |
"""Parses a decimal value without a # before it (if present).""" |
| 1654 |
items = space_items + ( |
| 1655 |
Fraction, |
| 1656 |
DecimalValue, |
| 1657 |
) |