def parse_rtm_syntax(rtm): r""" Parses RTM syntax. .. container:: example Parses tuplet: >>> rtm = '(1 (1 (1 (1 1)) 1))' >>> tuplet = abjad.rhythmtrees.parse_rtm_syntax(rtm) >>> abjad.show(tuplet) # doctest: +SKIP .. docs:: >>> abjad.f(tuplet) \times 2/3 { c'8 c'16 c'16 c'8 } .. container:: example Also supports fractional durations: >>> rtm = '(3/4 (1 1/2 (4/3 (1 -1/2 1))))' >>> tuplet = abjad.rhythmtrees.parse_rtm_syntax(rtm) >>> abjad.show(tuplet) # doctest: +SKIP .. docs:: >>> abjad.f(tuplet) \tweak text #tuplet-number::calc-fraction-text \times 9/17 { c'8 c'16 \tweak edge-height #'(0.7 . 0) \times 8/15 { c'8 r16 c'8 } } Returns tuplet or container. """ result = RhythmTreeParser()(rtm) container = core.Container() for node in result: tuplet = node((1, 4)) # following line added 2012-08-01. tb. tuplet = tuplet[0] if tuplet.trivial(): container.extend(tuplet[:]) else: container.append(tuplet) if len(container) == 1: return container[0] return container
def _construct_simultaneous_music(self, music): from abjad import parser as abjad_parser def is_separator(x): if isinstance(x, abjad_parser.LilyPondEvent): if x.name == "VoiceSeparator": return True return False container = core.Container() container.simultaneous = True # check for voice separators groups = [] for value, group in itertools.groupby(music, is_separator): if not value: groups.append(list(group)) # without voice separators if 1 == len(groups): # assert all(isinstance(x, core.Context) for x in groups[0]) container.extend(groups[0]) # with voice separators else: for group in groups: container.append(core.Voice(self._construct_sequential_music(group)[:])) return container
def p_measure__PIPE__FRACTION__component_list__PIPE(self, p): """ measure : PIPE FRACTION component_list PIPE """ measure = core.Container() for x in p[3]: measure.append(x) leaf = inspect(measure).leaf(0) time_signature = indicators.TimeSignature(p[2].pair) attach(time_signature, leaf) p[0] = measure
def construct(self): """ Constructs sequential music. Returns Abjad container. """ container = core.Container() for x in self.music: if isinstance(x, core.Component): container.append(x) elif isinstance(x, type(self)): container.extend(x.construct()) return container
def _cleanup(self, parsed): container = core.Container() for x in parsed: container.append(x) parsed = container leaves = select(parsed).leaves() if leaves: self._attach_indicators(leaves) for leaf in leaves: detach(dict, leaf) if 1 < self._toplevel_component_count: return parsed return parsed[0]
def _cleanup(self, parsed): import abjad container = core.Container() for x in parsed: container.append(x) parsed = container leaves = abjad.select(parsed).leaves() if leaves: self._apply_spanners(leaves) for leaf in leaves: detach(dict, leaf) if 1 < self._toplevel_component_count: return parsed return parsed[0]
def _construct_sequential_music(self, music): # indicator sorting could be rewritten into a single list using tuplets # with t[0] being 'forward' or 'backward' and t[1] being the indicator # as this better preserves attachment order. Not clear if we need it. container = core.Container() previous_leaf = None apply_forward = [] apply_backward = [] # sort events into forward or backwards attaching # and attach them to the proper leaf for x in music: if isinstance(x, core.Component) and not isinstance( x, core.BeforeGraceContainer ): for indicator in apply_forward: attach(indicator, x) if previous_leaf: for indicator in apply_backward: attach(indicator, previous_leaf) else: for indicator in apply_backward: attach(indicator, x) apply_forward[:] = [] apply_backward[:] = [] previous_leaf = x container.append(x) else: if isinstance(x, (abjad_indicators.BarLine,)): apply_backward.append(x) elif isinstance(x, abjad_indicators.LilyPondLiteral) and x.argument in ( r"\break", r"\breathe", r"\pageBreak", ): apply_backward.append(x) else: apply_forward.append(x) # attach remaining events to last leaf # or to the container itself if there were no leaves if previous_leaf: for indicator in apply_forward: attach(indicator, previous_leaf) for indicator in apply_backward: attach(indicator, previous_leaf) else: for indicator in apply_forward: attach(indicator, container) for indicator in apply_backward: attach(indicator, container) return container
def p_container__BRACE_L__component_list__BRACE_R(self, p): r"""container : BRACE_L component_list BRACE_R """ p[0] = core.Container() for component in p[2]: p[0].append(component)