The lang.lilypond module

LilyPond language and transform definition.

The LilyPond language definitions inherits from parce’s one, just like the Scheme language definition in the scheme module.

A LilyPondTransform is able to transform the parce tree to a quickly.dom document. Many contexts are transformed in corresponding nodes. Musical contents are transformed by the LilyPondTransform.create_music() method, that delegates the work to a MusicBuilder, which first creates a flat stream of nodes. Only nodes within music events are already combined, such as notes, chords and their durations, directions, articulations, tags and tweaks, etc.

Then music commands are combined with their arguments by looking at the signatures many Element types provide, which results in a sensible node tree representing the LilyPond source document. This is done by the quickly.dom.element.build_tree() function.

This quickly.dom tree can then be queried and modified at will.

class LilyPond[source]

Bases: LilyPond

LilyPond language definition.

classmethod get_scheme_target()[source]

Get our Scheme.

html_lilypond_tag = LilyPond.html_lilypond_tag
latex_lilypond_environment = LilyPond.latex_lilypond_environment
class LilyPondTransform[source]

Bases: Transform

Transform LilyPond to Music.

root(items)[source]

Concatenate all nodes in a Document object.

book(items)[source]

A Book or BookPart element.

score(items)[source]

A Score element.

header(items)[source]

A Header element.

paper(items)[source]

A Paper element.

layout(items)[source]

A Layout element.

midi(items)[source]

A Midi element.

layout_context(items)[source]

A With or LayoutContext element.

musiclist(items)[source]

A MusicList ({}) or SimultaneousMusicList (<<>>) element.

chord(items)[source]

A Chord element (<>).

repeat(items)[source]

A list of elements, contents of repeat context.

script(items)[source]

A Fingering or Articulation event.

pitch(items)[source]

A list of elements, Octave, Accidental and/or OctCheck after a note name.

duration(items)[source]

A tuple (dots, scaling).

dots is a list of Dot tokens and scaling is a DurationScaling node or None.

duration_scaling(items)[source]

A DurationScaling element (scaling after a duration).

lyricmode(items)[source]

A list of elements, contents of lyricmode context.

lyricsto(items)[source]

A list of elements, contents of lyricsto context.

lyriclist(items)[source]

A MusicList or SimultaneousMusicList element, the {} or <<>> construct in lyricmode.

lyricword = None
drummode(items)[source]

A list of elements, contents of drummode context.

drumlist(items)[source]

A MusicList or SimultaneousMusicList element, the {} or <<>> construct in drummode.

chordmode(items)[source]

A list of elements, contents of chordmode context.

chordlist(items)[source]

A MusicList or SimultaneousMusicList element, the {} or <<>> construct in chordmode.

chord_modifier(items)[source]

A list of elements, contents of chord_modifier context.

notemode(items)[source]

A list with elements, contents of notemode context.

figuremode(items)[source]

A list with elements, contents of figuremode context.

figurelist(items)[source]

A MusicList or SimultaneousMusicList element, the {} construct in figuremode.

figure(items)[source]

A Figure element.

figurebracket(items)[source]

A FigureBracket element.

list(items)[source]

A List, String, Int, Symbol, or Scheme element.

start_list = None
identifier_ref(items)[source]

An IdentifierRef element.

markup(items)[source]

A list of flattened markup contents, the markup will be constructed later.

markuplist(items)[source]

A MarkupList element.

markupscore(items)[source]

A MarkupScore element.

schemelily(items)[source]

A scm.LilyPond element, with LilyPond in Scheme.

string(items)[source]

A String element.

multiline_comment(items)[source]

A MultilineComment element.

singleline_comment(items)[source]

A SinglelineComment element.

html_lilypond_tag(items)[source]

Contents of a LilyPond tag.

There are two forms of LilyPond input in lilypond-book Html:

  • within the (self-closing) tag itself, after a colon

  • between <lilypond></lilypond> tags.

There may or may not be attributes.

Returns a two-tuple: ([list of elements], tail_origin).

latex_lilypond_environment(items)[source]

Contents of a lilypond command or environment.

Returns a three-tuple(options, contents, tail_origin). The first is the options that must be added to the environment opening command, if any; the second is the contents of the Environment node; and the third is the tail origin of the last brace in the short, \lilypond{ ... } form.

common(items)[source]

Find comment, string, scheme, markup and common tokens.

Yields Element objects.

handle_assignments(nodes)[source]

Handle assignments that occur in the Element nodes.

If a List, Symbol or String is encountered followed by an EqualSign and a third node, it is turned into an Assignment.

Needed at toplevel and in blocks that can contain variable assignments, such as layout, header and paper.

create_block(element_class, items, *, music=False, assignments=False)[source]

Return a block element for the items.

element_class is the type, e.g. lily.Score; items are the contents.

If music is set to True, the items are read as music, otherwise as common LilyPond syntax (e.g. enough for a header block). If assignments is set to True, assignments (list context followed by an equalsign) are converted into Assignment elements.

create_markup(markup, items)[source]

Yield zero or one Markup element.

markup is the result list of markup(), and items is the iterable from which more arguments are read. If there is no single argument, nothing is yielded.

read_markup_arguments(items)[source]

Read from items and yield nodes that can occur in markup.

get_markup_argument_count(command)[source]

Return the number of arguments of the markup command (without \).

Re-implement this method if you want to add your own markup commands. The default implementation consults LilyPond.get_markup_argument_count().

create_figure(items)[source]

Yield nodes to be added in a Figure.

create_music(items)[source]

Read music from items and yield Element nodes.

postprocess_lyriclist(nodes)[source]

Yields nodes, combining syllabe nodes with a Duration.

A String, Symbol, Scheme or Markup expression is wrapped in a LyricItem node. If a lyric node is followed by an Unpitched, the unpitched’s duration is added to the node, the unpitched is then discarded. Following Unpitcheds are converted to empty LyricItem nodes.

number_action(token)[source]

Called for Number.

separator_action(token)[source]

Called for Delimiter.Separator.

float_action(token)[source]

Called for Number.Float.

fraction_action(token)[source]

Called for Number.Fraction.

assignment_action(token)[source]

Called for Operator.Assignment.

keyword_action(token)[source]

Called for Keyword.

name_builtin_unit_action(token)[source]

Called for Name.Builtin.Unit (in paper or layout block).

name_type_action(token)[source]

Called for Name.Type (repeat mode).

pitch_octave_action(token)[source]

Called for Text.Music.Pitch.Octave.

pitch_octavecheck_action(token)[source]

Called for Text.Music.Pitch.Octave.OctaveCheck.

pitch_accidental_action(token)[source]

Called for Text.Music.Pitch.Accidental.

keyword_accepts(token)[source]

Called for Keyword \accepts.

keyword_denies(token)[source]

Called for Keyword \denies.

keyword_name(token)[source]

Called for Keyword \name.

keyword_alias(token)[source]

Called for Keyword \alias.

keyword_consists(token)[source]

Called for Keyword \consists.

keyword_remove(token)[source]

Called for Keyword \remove.

keyword_defaultchild(token)[source]

Called for Keyword \defaultchild.

class LilyPondAdHocTransform[source]

Bases: AdHocTransform, LilyPondTransform

LilyPondTransform that does not keep the origin tokens.

This is used to create pieces (nodes) of a LilyPond document from text, and then use that pieces to compose a larger Document or to edit an existing document. It is undesirable that origin tokens then would mistakenly be used as if they originated from the document that’s being edited.

class MusicBuilder(transform, items)[source]

Bases: object

Helper class that reads and builds music.

An instance of MusicBuilder is created and used in LilyPondTransform.create_music().

reset()[source]

Initialize all state.

pending_music()[source]

Yield pending music.

add_articulation(art)[source]

Add an articulation or script.

add_spanner_id(node)[source]

Return True if the node could be added to a spanner id that’s being built.

add_tweak(node)[source]

Return True if the node could be added to a Tweak that’s being built.

add_tag(node)[source]

Return True if the node could be added to a \tag command that’s being built.

add_shape(node)[source]

Return True if the node could be added to a \shape command that’s being built.

add_event_argument(node)[source]

Try to add the node as argument to a spanner_id, tweak, tag, or shape.

skip_token(token)[source]

Called for \skip.

after_token(token)[source]

Called for \after.

rest_token(token)[source]

Called for \rest.

tweak_token(token)[source]

Called for \tweak.

nobeam_token(token)[source]

Called for \noBeam.

tag_token(token)[source]

Called for \tag.

shape_token(token)[source]

Called for \shape.

vshape_token(token)[source]

Called for \vshape.

pitch_action(token)[source]

Called for Text.Music.Pitch (or Name.Pitch).

rest_action(token)[source]

Called for Text.Music.Rest.

drum_action(token)[source]

Called for Text.Music.Pitch.Drum.

duration_action(token)[source]

Called for Number.Duration.

direction_action(token)[source]

Called for Delimiter.Direction.

dynamic_action(token)[source]

Called for Name.Builtin.Dynamic.

articulation_action(token)[source]

Called for Name.Script.Articulation.

spanner_action(token)[source]

Called for Name.Symbol.Spanner.\*.

name_type_action(token)[source]

Called for Name.Type, e.g. a key signature mode.

tremolo_action(token)[source]

Called for Delimiter.Tremolo.

pipe_symbol_action(token)[source]

Called for Delimiter.Separator.PipeSymbol.

voice_separator_action(token)[source]

Called for Delimiter.Separator.VoiceSeparator.

chord_modifier_action(token)[source]

Called for Delimiter.Separator.Chord, chordmode.

number_action(token)[source]

Called for Number.

float_action(token)[source]

Called for Number.Float.

fraction_action(token)[source]

Called for Number.Fraction.

symbol_action(token)[source]

Called for Name.Symbol.*.

assignment_action(token)[source]

Called for Operator.Assignment.

lyric_text_action(token)[source]

Called for Text.Lyric.LyricText.*.

lyric_extender_action(token)[source]

Called for Delimiter.Lyric.LyricExtender.

lyric_hyphen_action(token)[source]

Called for Delimiter.Lyric.LyricHyphen.

lyric_skip_action(token)[source]

Called for Delimiter.Lyric.LyricSkip.

name_builtin_action(token)[source]

Called for any Name.Builtin token.

keyword_action(token)[source]

Called for any Keyword token.

keyword_lyricmode(token)[source]

Called for Keyword \lyricmode, \lyrics and \lyricsto.

keyword_addlyrics(token)[source]

Called for Keyword \addlyrics.

keyword_chordmode(token)[source]

Called for Keyword \chordmode and \chords.

keyword_figure(token)[source]

Called for Keyword \figuremode and \figures.

keyword_drummode(token)[source]

Called for Keyword \drummode and \drums.

chord_modifier(obj)[source]

Called with the result of the chord_modifier context.

flatten_elements(obj)[source]

Called for context that yield lists of Elements; flatten them.

pitch(obj)[source]

Called for pitch context: octave, accidental, octavecheck and/or comment.

duration(obj)[source]

Called for duration context: dots, scaling.

music_element(obj)[source]

An element node that is a music item (chord, figure).

script(obj)[source]

Called for script context: an articulation.

string_scheme(obj)[source]

Called for string, scheme or list context.

markup(obj)[source]

Called for markup context: read arguments from items.

identifier_ref(obj)[source]

Called for identifier_ref context: maybe articulation.

comment(obj)[source]

Called for singleline_comment and multiline_comment context.

Comments are preserved as good as possible.