Class: HeadMusic::Notation::ABC::Parser
- Inherits:
-
Object
- Object
- HeadMusic::Notation::ABC::Parser
show all
- 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.
Defined Under Namespace
Classes: PendingNote, VoiceState
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
Instance Method Summary
collapse
-
#apply_repeat_flags(state, style) ⇒ Object
private
-
#bar(bar_number) ⇒ Object
private
-
#build_composition ⇒ 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_input_present ⇒ Object
private
-
#ensure_not_awaiting_note(token, state: current_state) ⇒ Object
private
-
#finish ⇒ Object
private
-
#flush_pending_note(state) ⇒ Object
private
-
#handle(token) ⇒ Object
private
-
#handle_bar_line(token) ⇒ Object
private
-
#handle_broken_rhythm(token) ⇒ Object
private
-
#handle_note(token) ⇒ Object
private
-
#handle_rest(token) ⇒ Object
private
-
#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
-
#place(state, length, pitch, scale: Rational(1)) ⇒ Object
private
-
#reject_content_after_tune ⇒ Object
private
The lexer treats a blank line as the end of the tune, so anything after it would be silently dropped — most likely another tune.
-
#reject_unsupported_tokens(tokens) ⇒ Object
private
-
#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.
-
#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
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
|
Returns the value of attribute header.
35
36
37
|
# File 'lib/head_music/notation/abc/parser.rb', line 35
def
@header
end
|
Instance Method Details
#apply_repeat_flags(state, style) ⇒ Object
247
248
249
250
251
252
253
254
255
|
# File 'lib/head_music/notation/abc/parser.rb', line 247
def apply_repeat_flags(state, style)
if REPEAT_ENDING_STYLES.include?(style)
completed = state.completed_bar_number
bar(completed).ends_repeat_after_num_plays = 2 if completed
end
return unless REPEAT_STARTING_STYLES.include?(style)
bar(state.entered_bar_number).starts_repeat = true
end
|
#bar(bar_number) ⇒ Object
275
276
277
|
# File 'lib/head_music/notation/abc/parser.rb', line 275
def bar(bar_number)
@building.bars(bar_number).last
end
|
#build_composition ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/head_music/notation/abc/parser.rb', line 63
def build_composition
ensure_input_present
@header = Header.new(@abc_string, start_line: @start_line)
reject_content_after_tune
tokens = BodyLexer.new(.body, start_line: .body_start_line).tokens
reject_unsupported_tokens(tokens)
@duration_resolver = DurationResolver.new(.unit_note_length)
interpret(tokens)
end
|
#clear_passes_if_over(state, style) ⇒ Object
268
269
270
271
272
273
|
# File 'lib/head_music/notation/abc/parser.rb', line 268
def clear_passes_if_over(state, style)
return unless REPEAT_ENDING_STYLES.include?(style) || SECTION_ENDING_STYLES.include?(style)
state.active_passes = nil
state.volta_start_bar = 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
Body music before any V: line falls into a default unnamed voice.
142
143
144
|
# File 'lib/head_music/notation/abc/parser.rb', line 142
def current_state
@current_state ||= voice_state(nil)
end
|
73
74
75
76
77
|
# File 'lib/head_music/notation/abc/parser.rb', line 73
def ensure_input_present
return unless @abc_string.to_s.strip.empty?
raise ParseError, "ABC input is blank"
end
|
#ensure_not_awaiting_note(token, state: current_state) ⇒ Object
225
226
227
228
229
230
231
232
|
# File 'lib/head_music/notation/abc/parser.rb', line 225
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
|
#finish ⇒ Object
217
218
219
220
221
222
223
|
# File 'lib/head_music/notation/abc/parser.rb', line 217
def finish
@voice_states.each_value do |state|
ensure_not_awaiting_note(nil, state: state)
flush_pending_note(state)
tag_completed_bar(state)
end
end
|
#flush_pending_note(state) ⇒ Object
234
235
236
237
238
239
240
|
# File 'lib/head_music/notation/abc/parser.rb', line 234
def flush_pending_note(state)
pending = state.pending_note
return unless pending
state.pending_note = nil
place(state, pending.length, pending.pitch, scale: pending.scale)
end
|
#handle(token) ⇒ Object
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/head_music/notation/abc/parser.rb', line 146
def handle(token)
case token.type
when :note then handle_note(token)
when :rest then handle_rest(token)
when :broken_rhythm then handle_broken_rhythm(token)
when :bar_line then handle_bar_line(token)
when :volta then handle_volta(token)
when :voice_change then handle_voice_change(token)
end
end
|
#handle_bar_line(token) ⇒ Object
187
188
189
190
191
192
193
194
195
|
# File 'lib/head_music/notation/abc/parser.rb', line 187
def handle_bar_line(token)
ensure_not_awaiting_note(token)
state = current_state
flush_pending_note(state)
tag_completed_bar(state)
apply_repeat_flags(state, token.style)
clear_passes_if_over(state, token.style)
state.pitch_builder.start_new_bar
end
|
#handle_broken_rhythm(token) ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/head_music/notation/abc/parser.rb', line 173
def handle_broken_rhythm(token)
state = current_state
if state.awaiting_scale || state.pending_note.nil?
raise ParseError.new(
"Broken rhythm must appear between two notes",
line_number: token.line, snippet: token.direction.to_s
)
end
left_scale, right_scale = BROKEN_RHYTHM_SCALES.fetch(token.direction)
state.pending_note = state.pending_note.with(scale: state.pending_note.scale * left_scale)
state.awaiting_scale = right_scale
state.broken_line = token.line
end
|
#handle_note(token) ⇒ Object
157
158
159
160
161
162
163
164
|
# File 'lib/head_music/notation/abc/parser.rb', line 157
def handle_note(token)
state = current_state
pitch = state.pitch_builder.pitch(token.letter, token.octave_marks, token.accidental)
scale = state.awaiting_scale || Rational(1)
state.awaiting_scale = nil
flush_pending_note(state)
state.pending_note = PendingNote.new(pitch: pitch, length: token.length, scale: scale)
end
|
#handle_rest(token) ⇒ Object
166
167
168
169
170
171
|
# File 'lib/head_music/notation/abc/parser.rb', line 166
def handle_rest(token)
ensure_not_awaiting_note(token)
state = current_state
flush_pending_note(state)
place(state, token.length, nil)
end
|
#handle_voice_change(token) ⇒ Object
208
209
210
211
212
213
214
215
|
# File 'lib/head_music/notation/abc/parser.rb', line 208
def handle_voice_change(token)
if @current_state
ensure_not_awaiting_note(token, state: @current_state)
flush_pending_note(@current_state)
end
@current_state = voice_state(token.voice_id)
end
|
#handle_volta(token) ⇒ Object
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/head_music/notation/abc/parser.rb', line 197
def handle_volta(token)
ensure_not_awaiting_note(token)
if token.passes.empty?
raise ParseError.new("Volta has no passes", line_number: token.line)
end
state = current_state
flush_pending_note(state)
state.active_passes = token.passes
state.volta_start_bar = state.entered_bar_number
end
|
#interpret(tokens) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/head_music/notation/abc/parser.rb', line 110
def interpret(tokens)
@building = HeadMusic::Content::Composition.new(
name: .title,
key_signature: .key_signature,
meter: .meter,
composer: .composer,
origin: .origin,
comments: .annotations
)
setup_voices(tokens)
tokens.each { |token| handle(token) }
finish
@building
end
|
#place(state, length, pitch, scale: Rational(1)) ⇒ Object
242
243
244
245
|
# File 'lib/head_music/notation/abc/parser.rb', line 242
def place(state, length, pitch, scale: Rational(1))
rhythmic_value = duration_resolver.rhythmic_value(length, scale: scale)
state.voice.place(state.voice.next_position, rhythmic_value, pitch)
end
|
#reject_content_after_tune ⇒ Object
The lexer treats a blank line as the end of the tune, so anything
after it would be silently dropped — most likely another tune.
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/head_music/notation/abc/parser.rb', line 81
def reject_content_after_tune
lines = .body.lines
blank_index = lines.find_index { |line| line.strip.empty? }
return unless blank_index
= lines[(blank_index + 1)..]
= .find_index do |line|
stripped = line.strip
!stripped.empty? && !stripped.start_with?("%")
end
return unless
raise ParseError.new(
"Content after the tune body; parse a book of tunes with ABC.parse_book",
line_number: .body_start_line + blank_index + 1 + ,
snippet: [].strip[0, BodyLexer::SNIPPET_LENGTH]
)
end
|
#reject_unsupported_tokens(tokens) ⇒ Object
100
101
102
103
104
105
106
107
108
|
# File 'lib/head_music/notation/abc/parser.rb', line 100
def reject_unsupported_tokens(tokens)
token = tokens.find { |candidate| candidate.type == :unsupported }
return unless token
raise UnsupportedFeatureError.new(
"Unsupported ABC feature #{token.lexeme.inspect}",
line_number: token.line, snippet: token.lexeme
)
end
|
#setup_voices(tokens) ⇒ Object
125
126
127
128
129
130
131
132
|
# File 'lib/head_music/notation/abc/parser.rb', line 125
def setup_voices(tokens)
@voice_states = {}
.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
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.
259
260
261
262
263
264
265
266
|
# File 'lib/head_music/notation/abc/parser.rb', line 259
def tag_completed_bar(state)
return unless state.active_passes
completed = state.completed_bar_number
return unless completed && completed >= state.volta_start_bar
bar(completed).plays_on_passes = state.active_passes
end
|
#voice_state(role) ⇒ Object
134
135
136
137
138
139
|
# File 'lib/head_music/notation/abc/parser.rb', line 134
def voice_state(role)
@voice_states[role] ||= VoiceState.new(
@building.add_voice(role: role),
PitchBuilder.new(.key_signature)
)
end
|