| 1 | package Music::Harmonica::TabsCreator::TabParser; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use 5.036; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 4 | our $VERSION = '0.05'; | |||||||||
| 11 | |||||||||||
| 12 | # This class converts a tab into tones (degrees) relative to the key of C4. | ||||||||||
| 13 | # It accepts an input specifying the tuning of an harmonica. | ||||||||||
| 14 | |||||||||||
| 15 | 2 | ▶ | sub new ($class, $tab_to_tones) { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 16 | # We order the keys by length, so that we match the longest ones first. | ||||||||||
| 17 | 2 | my $re = join('|', map { quotemeta } sort { length $b <=> length $a } keys %{$tab_to_tones}); | |||||||||
| 18 | 2 | my $self = bless { | |||||||||
| 19 | tab_to_tones => $tab_to_tones, | ||||||||||
| 20 | tab_re => qr/$re/, | ||||||||||
| 21 | }, $class; | ||||||||||
| 22 | 2 | return $self; | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 2 | ▶ | sub parse ($self, $tab) { | ||||||||
|
Subroutine
parse: called CC=7 SCAR=23.2
Pod
undocumented
| |||||||||||
| 26 | 2 | my @out; | |||||||||
| 27 | 2 | pos($tab) = 0; | |||||||||
| 28 | 2 | ▶ | while (pos($tab) < length($tab)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 16 | ▶ | next if $tab =~ m/\G\h+/gc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | |||||||||||
| 31 | 9 | ▶ | if ($tab =~ m/\G(\v+)/gc) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | push @out, $1; | |||||||||
| 33 | 0 | next; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 9 | ▶ | if ($tab =~ m/ \G \# \s* ( .*? (?:\r\n|\n|\r|\v|\z) )/xgc) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | push @out, $1; | |||||||||
| 38 | 0 | next; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 9 | ▶ | if ($tab =~ m/\G($self->{tab_re})/gc) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 9 | push @out, $self->{tab_to_tones}{$1}; | |||||||||
| 43 | 9 | next; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 0 | my $pos = pos($tab); | |||||||||
| 47 | 0 | substr $tab, $pos, 0, '-->'; | |||||||||
| 48 | 0 | $pos++; | |||||||||
| 49 | 0 | die "Invalid syntax in the input tab at character ${pos}: ${tab}\n"; | |||||||||
| 50 | } | ||||||||||
| 51 | 2 | ▶ | return wantarray ? @out : \@out; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 4 | 1; | |||||||||