Class: HeadMusic::Notation::ABC::Parser
- Inherits:
-
Object
- Object
- HeadMusic::Notation::ABC::Parser
- Defined in:
- lib/head_music/notation/abc/parser.rb
Overview
Interprets an ABC tune string as a HeadMusic::Content::Composition.
Everything that can be validated up front (blank input, header problems, lexing errors, unsupported features) raises before the composition is constructed, so callers never receive a reference to a partially built composition.
Constant Summary collapse
- BROKEN_RHYTHM_SCALES =
Broken-rhythm scales: the mark’s side gets the dot (x 3/2) and the other side is halved.
{ :> => [Rational(3, 2), Rational(1, 2)], :< => [Rational(1, 2), Rational(3, 2)] }.freeze
- REPEAT_ENDING_STYLES =
Bar styles that end a repeated section, terminating any volta.
[":|", "::"].freeze
- REPEAT_STARTING_STYLES =
["|:", "::"].freeze
- SECTION_ENDING_STYLES =
["||", "|]", "[|"].freeze
Instance Attribute Summary collapse
-
#duration_resolver ⇒ Object
readonly
private
Returns the value of attribute duration_resolver.
-
#header ⇒ Object
readonly
private
Returns the value of attribute header.
Instance Method Summary collapse
-
#apply_repeat_flags(state, style) ⇒ Object
private
-
#bar(bar_number) ⇒ Object
private
-
#build_composition ⇒ Object
private
Per-voice interpretation state and note-assembly live in VoiceState.
-
#chord_snippet(token) ⇒ Object
private
-
#clear_passes_if_over(state, style) ⇒ Object
private
-
#composition ⇒ Object
-
#current_state ⇒ Object
private
Body music before any V: line falls into a default unnamed voice.
-
#ensure_not_awaiting_note(token, state: current_state) ⇒ Object
private
-
#ensure_unique_chord_pitches(pitches, token) ⇒ Object
private
-
#finish ⇒ Object
private
-
#handle(token) ⇒ Object
private
-
#handle_bar_line(token) ⇒ Object
private
-
#handle_beam_break(_token) ⇒ Object
private
The lexer only emits :beam_break after a music token, so a voice state already exists; the flag is consumed by the next deferred note.
-
#handle_broken_rhythm(token) ⇒ Object
private
-
#handle_chord(token) ⇒ Object
private
Chord pitches resolve in bracket order, so an explicit accidental inside a chord persists for the rest of the bar like any other.
-
#handle_note(token) ⇒ Object
private
-
#handle_rest(token) ⇒ Object
private
-
#handle_tie(token) ⇒ Object
private
A tie (
-) after a note or chord fuses it to the next note of the same pitch. -
#handle_voice_change(token) ⇒ Object
private
-
#handle_volta(token) ⇒ Object
private
-
#initialize(abc_string, start_line: 1) ⇒ Parser
constructor
start_line offsets reported line numbers, so a tune parsed out of a larger book raises errors with book-relative line numbers.
-
#interpret(tokens) ⇒ Object
private
-
#reject_open_tie(state, line, message) ⇒ Object
private
A tie left open by a non-note terminator can never close, so each terminator rejects it.
-
#setup_voices(tokens) ⇒ Object
private
-
#tag_completed_bar(state) ⇒ Object
private
A volta covers every bar from its opening bracket through the bar line that ends it, so each completed bar in that span gets tagged.
-
#uniform_chord_length(token) ⇒ Object
private
ABC 2.1 sec.
-
#voice_state(role) ⇒ Object
private
Constructor Details
#initialize(abc_string, start_line: 1) ⇒ Parser
start_line offsets reported line numbers, so a tune parsed out of a larger book raises errors with book-relative line numbers.
24 25 26 27 |
# File 'lib/head_music/notation/abc/parser.rb', line 24 def initialize(abc_string, start_line: 1) @abc_string = abc_string @start_line = start_line end |
Instance Attribute Details
#duration_resolver ⇒ Object (readonly, private)
Returns the value of attribute duration_resolver.
35 36 37 |
# File 'lib/head_music/notation/abc/parser.rb', line 35 def duration_resolver @duration_resolver end |
#header ⇒ Object (readonly, private)
Returns the value of attribute header.
35 36 37 |
# File 'lib/head_music/notation/abc/parser.rb', line 35 def header @header end |
Instance Method Details
#apply_repeat_flags(state, style) ⇒ Object (private)
259 260 261 262 263 264 265 266 267 |
# File 'lib/head_music/notation/abc/parser.rb', line 259 def apply_repeat_flags(state, style) if REPEAT_ENDING_STYLES.include?(style) completed = state. (completed).ends_repeat_after_num_plays = 2 if completed end return unless REPEAT_STARTING_STYLES.include?(style) (state.).starts_repeat = true end |
#bar(bar_number) ⇒ Object (private)
288 289 290 |
# File 'lib/head_music/notation/abc/parser.rb', line 288 def () @building.().last end |
#build_composition ⇒ Object (private)
Per-voice interpretation state and note-assembly live in VoiceState.
39 40 41 42 43 44 45 46 47 |
# File 'lib/head_music/notation/abc/parser.rb', line 39 def build_composition Preflight.ensure_input_present(@abc_string) @header = Header.new(@abc_string, start_line: @start_line) Preflight.reject_content_after_tune(header) tokens = BodyLexer.new(header.body, start_line: header.body_start_line).tokens Preflight.reject_unsupported_tokens(tokens) @duration_resolver = DurationResolver.new(header.unit_note_length) interpret(tokens) end |
#chord_snippet(token) ⇒ Object (private)
142 143 144 145 146 147 |
# File 'lib/head_music/notation/abc/parser.rb', line 142 def chord_snippet(token) inner = token.notes.map do |note| "#{note.accidental}#{note.letter}#{note.octave_marks}#{note.length}" end.join "[#{inner}]" end |
#clear_passes_if_over(state, style) ⇒ Object (private)
281 282 283 284 285 286 |
# File 'lib/head_music/notation/abc/parser.rb', line 281 def clear_passes_if_over(state, style) return unless REPEAT_ENDING_STYLES.include?(style) || SECTION_ENDING_STYLES.include?(style) state.active_passes = nil state. = nil end |
#composition ⇒ Object
29 30 31 |
# File 'lib/head_music/notation/abc/parser.rb', line 29 def composition @composition ||= build_composition end |
#current_state ⇒ Object (private)
Body music before any V: line falls into a default unnamed voice.
82 83 84 |
# File 'lib/head_music/notation/abc/parser.rb', line 82 def current_state @current_state ||= voice_state(nil) end |
#ensure_not_awaiting_note(token, state: current_state) ⇒ Object (private)
250 251 252 253 254 255 256 257 |
# File 'lib/head_music/notation/abc/parser.rb', line 250 def ensure_not_awaiting_note(token, state: current_state) return unless state.awaiting_scale raise ParseError.new( "Broken rhythm must be followed by a note", line_number: token&.line || state.broken_line ) end |
#ensure_unique_chord_pitches(pitches, token) ⇒ Object (private)
118 119 120 121 122 123 124 125 |
# File 'lib/head_music/notation/abc/parser.rb', line 118 def ensure_unique_chord_pitches(pitches, token) return if pitches.uniq.length == pitches.length raise ParseError.new( "Chord pitches must be unique", line_number: token.line, snippet: chord_snippet(token) ) end |
#finish ⇒ Object (private)
241 242 243 244 245 246 247 248 |
# File 'lib/head_music/notation/abc/parser.rb', line 241 def finish @voice_states.each_value do |state| ensure_not_awaiting_note(nil, state: state) reject_open_tie(state, nil, "A tie must be followed by a note") state.flush_pending_note (state) end end |
#handle(token) ⇒ Object (private)
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/head_music/notation/abc/parser.rb', line 86 def handle(token) case token.type when :note then handle_note(token) when :chord then handle_chord(token) when :rest then handle_rest(token) when :tie then handle_tie(token) when :broken_rhythm then handle_broken_rhythm(token) when :bar_line then (token) when :volta then handle_volta(token) when :voice_change then handle_voice_change(token) when :beam_break then handle_beam_break(token) end end |
#handle_bar_line(token) ⇒ Object (private)
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/head_music/notation/abc/parser.rb', line 203 def (token) ensure_not_awaiting_note(token) state = current_state style = token.style reject_open_tie(state, token.line, "Ties across barlines are not yet supported") state.flush_pending_note state.reset_beam_adjacency (state) apply_repeat_flags(state, style) clear_passes_if_over(state, style) state.pitch_builder. end |
#handle_beam_break(_token) ⇒ Object (private)
The lexer only emits :beam_break after a music token, so a voice state already exists; the flag is consumed by the next deferred note.
151 152 153 |
# File 'lib/head_music/notation/abc/parser.rb', line 151 def handle_beam_break(_token) current_state.mark_beam_break end |
#handle_broken_rhythm(token) ⇒ Object (private)
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/head_music/notation/abc/parser.rb', line 185 def handle_broken_rhythm(token) state = current_state line = token.line direction = token.direction reject_open_tie(state, line, "A tie must be followed by a note") pending = state.pending_note if state.awaiting_scale || pending.nil? raise ParseError.new( "Broken rhythm must appear between two notes", line_number: line, snippet: direction.to_s ) end left_scale, right_scale = BROKEN_RHYTHM_SCALES.fetch(direction) state.pending_note = pending.with(scale: pending.scale * left_scale) state.awaiting_scale = right_scale state.broken_line = line end |
#handle_chord(token) ⇒ Object (private)
Chord pitches resolve in bracket order, so an explicit accidental inside a chord persists for the rest of the bar like any other.
108 109 110 111 112 113 114 115 116 |
# File 'lib/head_music/notation/abc/parser.rb', line 108 def handle_chord(token) state = current_state pitches = token.notes.map do |note| state.pitch_builder.pitch(note.letter, note.octave_marks, note.accidental) end ensure_unique_chord_pitches(pitches, token) inner_length = uniform_chord_length(token) state.defer_placement(pitches, token.length, inner_length) end |
#handle_note(token) ⇒ Object (private)
100 101 102 103 104 |
# File 'lib/head_music/notation/abc/parser.rb', line 100 def handle_note(token) state = current_state pitch = state.pitch_builder.pitch(token.letter, token.octave_marks, token.accidental) state.defer_placement([pitch], token.length) end |
#handle_rest(token) ⇒ Object (private)
167 168 169 170 171 172 173 174 |
# File 'lib/head_music/notation/abc/parser.rb', line 167 def handle_rest(token) ensure_not_awaiting_note(token) state = current_state reject_open_tie(state, token.line, "A tie must be followed by a note") state.flush_pending_note state.reset_beam_adjacency state.place(token.length, nil) end |
#handle_tie(token) ⇒ Object (private)
A tie (-) after a note or chord fuses it to the next note of the
same pitch. The left note stays pending; the tie is only closed once
its right note arrives.
158 159 160 161 162 163 164 165 |
# File 'lib/head_music/notation/abc/parser.rb', line 158 def handle_tie(token) state = current_state line = token.line if state.awaiting_scale || state.pending_note.nil? raise ParseError.new("A tie must follow a note", line_number: line, snippet: "-") end state.open_tie(line) end |
#handle_voice_change(token) ⇒ Object (private)
230 231 232 233 234 235 236 237 238 239 |
# File 'lib/head_music/notation/abc/parser.rb', line 230 def handle_voice_change(token) # Guarded so a leading V: line doesn't force a default voice into existence. if @current_state ensure_not_awaiting_note(token, state: @current_state) reject_open_tie(@current_state, token.line, "A tie must be followed by a note") @current_state.flush_pending_note @current_state.reset_beam_adjacency end @current_state = voice_state(token.voice_id) end |
#handle_volta(token) ⇒ Object (private)
216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/head_music/notation/abc/parser.rb', line 216 def handle_volta(token) ensure_not_awaiting_note(token) passes = token.passes line = token.line raise ParseError.new("Volta has no passes", line_number: line) if passes.empty? state = current_state reject_open_tie(state, line, "A tie must be followed by a note") state.flush_pending_note state.reset_beam_adjacency state.active_passes = passes state. = state. end |
#interpret(tokens) ⇒ Object (private)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/head_music/notation/abc/parser.rb', line 49 def interpret(tokens) @building = HeadMusic::Content::Composition.new( name: header.title, key_signature: header.key_signature, meter: header.meter, composer: header.composer, origin: header.origin, comments: header.annotations ) setup_voices(tokens) tokens.each { |token| handle(token) } finish @building end |
#reject_open_tie(state, line, message) ⇒ Object (private)
A tie left open by a non-note terminator can never close, so each terminator rejects it. A bar line gets its own message: an author tie across a barline is a real, but not-yet-supported, request.
179 180 181 182 183 |
# File 'lib/head_music/notation/abc/parser.rb', line 179 def reject_open_tie(state, line, ) return unless state&.tie_open? raise ParseError.new(, line_number: line || state.tie_line, snippet: "-") end |
#setup_voices(tokens) ⇒ Object (private)
64 65 66 67 68 69 70 71 |
# File 'lib/head_music/notation/abc/parser.rb', line 64 def setup_voices(tokens) @voice_states = {} header.voice_ids.each { |voice_id| voice_state(voice_id) } if @voice_states.empty? && tokens.none? { |token| token.type == :voice_change } voice_state(nil) end @current_state = @voice_states.values.first end |
#tag_completed_bar(state) ⇒ Object (private)
A volta covers every bar from its opening bracket through the bar line that ends it, so each completed bar in that span gets tagged.
271 272 273 274 275 276 277 278 279 |
# File 'lib/head_music/notation/abc/parser.rb', line 271 def (state) passes = state.active_passes return unless passes completed = state. return unless completed && completed >= state. (completed).plays_on_passes = passes end |
#uniform_chord_length(token) ⇒ Object (private)
ABC 2.1 sec. 4.17 allows per-note lengths only when they agree; the shared inner length then multiplies with any outer length. Unequal lengths (whose ABC meaning is “the duration of the first note”) would need silent reinterpretation to fit one rhythmic value, so we reject.
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/head_music/notation/abc/parser.rb', line 131 def uniform_chord_length(token) fractions = token.notes.map { |note| duration_resolver.length_fraction(note.length) } return fractions.first if fractions.uniq.length == 1 raise ParseError.new( 'Chord notes must share one length; write it after the bracket ("[CEG]2") ' \ 'or repeat it on every note ("[C2E2G2]")', line_number: token.line, snippet: chord_snippet(token) ) end |
#voice_state(role) ⇒ Object (private)
73 74 75 76 77 78 79 |
# File 'lib/head_music/notation/abc/parser.rb', line 73 def voice_state(role) @voice_states[role] ||= VoiceState.new( @building.add_voice(role: role), PitchBuilder.new(header.key_signature), duration_resolver ) end |