Browse files

Switch from ABC to ABC::Grammar in preparation for further goodness.

  • Loading branch information...
1 parent c2fe3ce commit 961319501798a8bbd918bd68aaddfc5980d1091e @colomon committed Sep 16, 2010
Showing with 77 additions and 77 deletions.
  1. +5 −5 lib/{ABC.pm → ABC/Grammar.pm}
  2. +2 −2 playing.pl
  3. +49 −49 t/01-regexes.t
  4. +19 −19 t/02-key.t
  5. +2 −2 t/03-file.t
View
10 lib/ABC.pm → lib/ABC/Grammar.pm
@@ -1,6 +1,6 @@
use v6;
-grammar ABC
+grammar ABC::Grammar
{
regex header_field_name { \w }
regex header_field_data { \N* }
@@ -14,13 +14,13 @@ grammar ABC
regex tie { '-' }
regex note_length { [\d* ['/' \d*]? ] | '/' }
- regex note { <pitch> <note_length>? <tie>? }
- regex stem { <note> | [ '[' <note>+ ']' ] }
+ regex mnote { <pitch> <note_length>? <tie>? }
+ regex stem { <mnote> | [ '[' <mnote>+ ']' ] }
regex rest_type { <[x..z]> }
regex rest { <rest_type> <note_length>? }
- regex grace_note { <pitch> <note_length>? } # as note, but without tie
+ regex grace_note { <pitch> <note_length>? } # as mnote, but without tie
regex grace_note_stem { <grace_note> | [ '[' <grace_note>+ ']' ] }
regex acciaccatura { '/' }
regex grace_notes { '{' <acciaccatura>? <grace_note_stem>+ '}' }
@@ -79,7 +79,7 @@ grammar ABC
# <[a..g]+[A..G]> should be <ABC::basenote
- my $match = ABC.parse($key_signature_name, :rule<key_sig>);
+ my $match = ABC::Grammar.parse($key_signature_name, :rule<key_sig>);
# say :$match.perl;
die "Illegal key signature\n" unless $match ~~ Match;
my $lookup = [~] $match<basenote>.uc, $match[0];
View
4 playing.pl
@@ -24,8 +24,8 @@
{
for $bar<element>
{
- when .<broken_rhythm> { take .<broken_rhythm><note>[0]; take .<broken_rhythm><note>[1]; }
- when .<note> { take .<note>; }
+ when .<broken_rhythm> { take .<broken_rhythm><mnote>[0]; take .<broken_rhythm><mnote>[1]; }
+ when .<mnote> { take .<mnote>; }
}
}
}
View
98 t/01-regexes.t
@@ -1,43 +1,43 @@
use v6;
use Test;
-use ABC;
+use ABC::Grammar;
plan *;
{
- my $match = ABC.parse("^A,", :rule<pitch>);
+ my $match = ABC::Grammar.parse("^A,", :rule<pitch>);
isa_ok $match, Match, '"^A," is a pitch';
is $match<basenote>, "A", '"^A," has base note A';
is $match<octave>, ",", '"^A," has octave ","';
is $match<accidental>, "^", '"^A," has accidental "#"';
}
{
- my $match = ABC.parse("_B", :rule<pitch>);
+ my $match = ABC::Grammar.parse("_B", :rule<pitch>);
isa_ok $match, Match, '"_B" is a pitch';
is $match<basenote>, "B", '"_B" has base note B';
is $match<octave>, "", '"_B" has octave ""';
is $match<accidental>, "_", '"_B" has accidental "_"';
}
{
- my $match = ABC.parse("C''", :rule<pitch>);
+ my $match = ABC::Grammar.parse("C''", :rule<pitch>);
isa_ok $match, Match, '"note" is a pitch';
is $match<basenote>, "C", '"note" has base note C';
is $match<octave>, "''", '"note" has octave two-upticks';
is $match<accidental>, "", '"note" has accidental ""';
}
{
- my $match = ABC.parse("=d,,,", :rule<pitch>);
+ my $match = ABC::Grammar.parse("=d,,,", :rule<pitch>);
isa_ok $match, Match, '"=d,,," is a pitch';
is $match<basenote>, "d", '"=d,,," has base note d';
is $match<octave>, ",,,", '"=d,,," has octave ",,,"';
is $match<accidental>, "=", '"=d,,," has accidental "="';
}
{
- my $match = ABC.parse("^^e2", :rule<note>);
+ my $match = ABC::Grammar.parse("^^e2", :rule<mnote>);
isa_ok $match, Match, '"^^e2" is a note';
is $match<pitch><basenote>, "e", '"^^e2" has base note e';
is $match<pitch><octave>, "", '"^^e2" has octave ""';
@@ -46,7 +46,7 @@ plan *;
}
{
- my $match = ABC.parse("__f'/", :rule<note>);
+ my $match = ABC::Grammar.parse("__f'/", :rule<mnote>);
isa_ok $match, Match, '"__f/" is a note';
is $match<pitch><basenote>, "f", '"__f/" has base note f';
is $match<pitch><octave>, "'", '"__f/" has octave tick';
@@ -55,7 +55,7 @@ plan *;
}
{
- my $match = ABC.parse("G,2/3", :rule<note>);
+ my $match = ABC::Grammar.parse("G,2/3", :rule<mnote>);
isa_ok $match, Match, '"G,2/3" is a note';
is $match<pitch><basenote>, "G", '"G,2/3" has base note G';
is $match<pitch><octave>, ",", '"G,2/3" has octave ","';
@@ -64,104 +64,104 @@ plan *;
}
{
- my $match = ABC.parse("z2/3", :rule<rest>);
+ my $match = ABC::Grammar.parse("z2/3", :rule<rest>);
isa_ok $match, Match, '"z2/3" is a rest';
is $match<rest_type>, "z", '"z2/3" has base rest z';
is $match<note_length>, "2/3", '"z2/3" has note length 2/3';
}
{
- my $match = ABC.parse("y/3", :rule<rest>);
+ my $match = ABC::Grammar.parse("y/3", :rule<rest>);
isa_ok $match, Match, '"y/3" is a rest';
is $match<rest_type>, "y", '"y/3" has base rest y';
is $match<note_length>, "/3", '"y/3" has note length 2/3';
}
{
- my $match = ABC.parse("x", :rule<rest>);
+ my $match = ABC::Grammar.parse("x", :rule<rest>);
isa_ok $match, Match, '"x" is a rest';
is $match<rest_type>, "x", '"x" has base rest x';
is $match<note_length>, "", '"x" has no note length';
}
{
- my $match = ABC.parse("+trill+", :rule<element>);
+ my $match = ABC::Grammar.parse("+trill+", :rule<element>);
isa_ok $match, Match, '"+trill+" is an element';
is $match<gracing>, "+trill+", '"+trill+" gracing is +trill+';
}
{
- my $match = ABC.parse("~", :rule<element>);
+ my $match = ABC::Grammar.parse("~", :rule<element>);
isa_ok $match, Match, '"~" is an element';
is $match<gracing>, "~", '"~" gracing is ~';
}
{
- my $match = ABC.parse("z/", :rule<element>);
+ my $match = ABC::Grammar.parse("z/", :rule<element>);
isa_ok $match, Match, '"z/" is an element';
is $match<rest><rest_type>, "z", '"z/" has base rest z';
is $match<rest><note_length>, "/", '"z/" has length "/"';
}
{
- my $match = ABC.parse("_D,5/4", :rule<element>);
+ my $match = ABC::Grammar.parse("_D,5/4", :rule<element>);
isa_ok $match, Match, '"_D,5/4" is an element';
- is $match<stem><note>[0]<pitch><basenote>, "D", '"_D,5/4" has base note D';
- is $match<stem><note>[0]<pitch><octave>, ",", '"_D,5/4" has octave ","';
- is $match<stem><note>[0]<pitch><accidental>, "_", '"_D,5/4" is flat';
- is $match<stem><note>[0]<note_length>, "5/4", '"_D,5/4" has note length 5/4';
+ is $match<stem><mnote>[0]<pitch><basenote>, "D", '"_D,5/4" has base note D';
+ is $match<stem><mnote>[0]<pitch><octave>, ",", '"_D,5/4" has octave ","';
+ is $match<stem><mnote>[0]<pitch><accidental>, "_", '"_D,5/4" is flat';
+ is $match<stem><mnote>[0]<note_length>, "5/4", '"_D,5/4" has note length 5/4';
}
{
- my $match = ABC.parse("A>^C'", :rule<broken_rhythm>);
+ my $match = ABC::Grammar.parse("A>^C'", :rule<broken_rhythm>);
isa_ok $match, Match, '"A>^C" is a broken rhythm';
- is $match<stem>[0]<note>[0]<pitch><basenote>, "A", 'first note is A';
- is $match<stem>[0]<note>[0]<pitch><octave>, "", 'first note has no octave';
- is $match<stem>[0]<note>[0]<pitch><accidental>, "", 'first note has no accidental';
- is $match<stem>[0]<note>[0]<note_length>, "", 'first note has no length';
+ is $match<stem>[0]<mnote>[0]<pitch><basenote>, "A", 'first note is A';
+ is $match<stem>[0]<mnote>[0]<pitch><octave>, "", 'first note has no octave';
+ is $match<stem>[0]<mnote>[0]<pitch><accidental>, "", 'first note has no accidental';
+ is $match<stem>[0]<mnote>[0]<note_length>, "", 'first note has no length';
is $match<broken_rhythm_bracket>, ">", 'angle is >';
- is $match<stem>[1]<note>[0]<pitch><basenote>, "C", 'second note is C';
- is $match<stem>[1]<note>[0]<pitch><octave>, "'", 'second note has octave tick';
- is $match<stem>[1]<note>[0]<pitch><accidental>, "^", 'second note is sharp';
- is $match<stem>[1]<note>[0]<note_length>, "", 'second note has no length';
+ is $match<stem>[1]<mnote>[0]<pitch><basenote>, "C", 'second note is C';
+ is $match<stem>[1]<mnote>[0]<pitch><octave>, "'", 'second note has octave tick';
+ is $match<stem>[1]<mnote>[0]<pitch><accidental>, "^", 'second note is sharp';
+ is $match<stem>[1]<mnote>[0]<note_length>, "", 'second note has no length';
}
{
- my $match = ABC.parse("d'+p+<<<+accent+_B", :rule<broken_rhythm>);
+ my $match = ABC::Grammar.parse("d'+p+<<<+accent+_B", :rule<broken_rhythm>);
isa_ok $match, Match, '"d+p+<<<+accent+_B" is a broken rhythm';
given $match
{
- is .<stem>[0]<note>[0]<pitch><basenote>, "d", 'first note is d';
- is .<stem>[0]<note>[0]<pitch><octave>, "'", 'first note has an octave tick';
- is .<stem>[0]<note>[0]<pitch><accidental>, "", 'first note has no accidental';
- is .<stem>[0]<note>[0]<note_length>, "", 'first note has no length';
+ is .<stem>[0]<mnote>[0]<pitch><basenote>, "d", 'first note is d';
+ is .<stem>[0]<mnote>[0]<pitch><octave>, "'", 'first note has an octave tick';
+ is .<stem>[0]<mnote>[0]<pitch><accidental>, "", 'first note has no accidental';
+ is .<stem>[0]<mnote>[0]<note_length>, "", 'first note has no length';
is .<g1>[0], "+p+", 'first gracing is +p+';
is .<broken_rhythm_bracket>, "<<<", 'angle is <<<';
is .<g2>[0], "+accent+", 'second gracing is +accent+';
- is .<stem>[1]<note>[0]<pitch><basenote>, "B", 'second note is B';
- is .<stem>[1]<note>[0]<pitch><octave>, "", 'second note has no octave';
- is .<stem>[1]<note>[0]<pitch><accidental>, "_", 'second note is flat';
- is .<stem>[1]<note>[0]<note_length>, "", 'second note has no length';
+ is .<stem>[1]<mnote>[0]<pitch><basenote>, "B", 'second note is B';
+ is .<stem>[1]<mnote>[0]<pitch><octave>, "", 'second note has no octave';
+ is .<stem>[1]<mnote>[0]<pitch><accidental>, "_", 'second note is flat';
+ is .<stem>[1]<mnote>[0]<note_length>, "", 'second note has no length';
}
}
for ':|:', '|:', '|', ':|', '::'
{
- my $match = ABC.parse($_, :rule<barline>);
+ my $match = ABC::Grammar.parse($_, :rule<barline>);
isa_ok $match, Match, "barline $_ recognized";
is $match, $_, "barline $_ is correct";
}
{
- my $match = ABC.parse("g>ecgece/f/g/e/|", :rule<bar>);
+ my $match = ABC::Grammar.parse("g>ecgece/f/g/e/|", :rule<bar>);
isa_ok $match, Match, 'bar recognized';
is $match, "g>ecgece/f/g/e/|", "Entire bar was matched";
is $match<element>.map(~*), "g>e c g e c e/ f/ g/ e/", "Each element was matched";
is $match<barline>, "|", "Barline was matched";
}
{
- my $match = ABC.parse("g>ecg ec e/f/g/e/ |", :rule<bar>);
+ my $match = ABC::Grammar.parse("g>ecg ec e/f/g/e/ |", :rule<bar>);
isa_ok $match, Match, 'bar recognized';
is $match, "g>ecg ec e/f/g/e/ |", "Entire bar was matched";
is $match<element>.map(~*), "g>e c g e c e/ f/ g/ e/ ", "Each element was matched";
@@ -170,41 +170,41 @@ for ':|:', '|:', '|', ':|', '::'
{
my $line = "g>ecg ec e/f/g/e/ | d/c/B/A/ Gd BG B/c/d/B/ |";
- my $match = ABC.parse($line, :rule<line_of_music>);
+ my $match = ABC::Grammar.parse($line, :rule<line_of_music>);
isa_ok $match, Match, 'line of music recognized';
is $match, $line, "Entire line was matched";
is $match<bar>[0], "g>ecg ec e/f/g/e/ |", "First bar is correct";
is $match<bar>[1], " d/c/B/A/ Gd BG B/c/d/B/ |", "Second bar is correct";
- # say $match<ABC::line_of_music>.perl;
+ # say $match<ABC::Grammar::line_of_music>.perl;
}
{
my $line = "|A/B/c/A/ c>d e>deg | dB/A/ gB +trill+A2 +trill+e2 ::";
- my $match = ABC.parse($line, :rule<line_of_music>);
+ my $match = ABC::Grammar.parse($line, :rule<line_of_music>);
isa_ok $match, Match, 'line of music recognized';
is $match, $line, "Entire line was matched";
is $match<bar>[0], "A/B/c/A/ c>d e>deg |", "First bar is correct";
is $match<bar>[1], " dB/A/ gB +trill+A2 +trill+e2 ::", "Second bar is correct";
is $match<barline>, "|", "Initial barline matched";
- # say $match<ABC::line_of_music>.perl;
+ # say $match<ABC::Grammar::line_of_music>.perl;
}
{
my $line = 'g>ecg ec e/f/g/e/ |[2-3 d/c/B/A/ {Gd} BG B/c/d/B/ |';
- my $match = ABC.parse($line, :rule<line_of_music>);
+ my $match = ABC::Grammar.parse($line, :rule<line_of_music>);
isa_ok $match, Match, 'line of music recognized';
is $match, $line, "Entire line was matched";
is $match<bar>[0], "g>ecg ec e/f/g/e/ |", "First bar is correct";
is $match<bar>[1], '[2-3 d/c/B/A/ {Gd} BG B/c/d/B/ |', "Second bar is correct";
- # say $match<ABC::line_of_music>.perl;
+ # say $match<ABC::Grammar::line_of_music>.perl;
}
{
my $music = q«A/B/c/A/ +trill+c>d e>deg | GG +trill+B>c d/B/A/G/ B/c/d/B/ |
A/B/c/A/ c>d e>deg | dB/A/ gB +trill+A2 +trill+e2 ::
g>ecg ec e/f/g/e/ | d/c/B/A/ Gd BG B/c/d/B/ |
g/f/e/d/ c/d/e/f/ gc e/f/g/e/ | dB/A/ gB +trill+A2 +trill+e2 :|»;
- my $match = ABC.parse($music, :rule<music>);
+ my $match = ABC::Grammar.parse($music, :rule<music>);
isa_ok $match, Match, 'music recognized';
is $match<line_of_music>.elems, 4, "Four lines matched";
}
@@ -217,7 +217,7 @@ M:4/4
L:1/8
K:D
»;
- my $match = ABC.parse($music, :rule<header>);
+ my $match = ABC::Grammar.parse($music, :rule<header>);
isa_ok $match, Match, 'header recognized';
is $match<header_field>.elems, 6, "Six fields matched";
is $match<header_field>.flat.map({ .<header_field_name> }), "X T S M L K", "Got the right field names";
@@ -235,7 +235,7 @@ A/B/c/A/ c>d e>deg | dB/A/ gB +trill+A2 +trill+e2 ::
g>ecg ec e/f/g/e/ | d/c/B/A/ Gd BG B/c/d/B/ |
g/f/e/d/ c/d/e/f/ gc e/f/g/e/ | dB/A/ gB +trill+A2 +trill+e2 :|
»;
- my $match = ABC.parse($music, :rule<tune>);
+ my $match = ABC::Grammar.parse($music, :rule<tune>);
isa_ok $match, Match, 'tune recognized';
given $match<header>
{
View
38 t/02-key.t
@@ -1,47 +1,47 @@
use v6;
use Test;
-use ABC;
+use ABC::Grammar;
plan *;
{
- my %key = ABC::key_signature("D");
+ my %key = ABC::Grammar::key_signature("D");
is %key.elems, 2, "D has two sharps";
is %key<F>, "^", "F is sharp";
is %key<C>, "^", "C is sharp";
}
{
- my %key = ABC::key_signature("Dmix");
+ my %key = ABC::Grammar::key_signature("Dmix");
is %key.elems, 1, "Dmix has one sharp";
is %key<F>, "^", "F is sharp";
}
{
- my %key = ABC::key_signature("Am");
+ my %key = ABC::Grammar::key_signature("Am");
is %key.elems, 0, "Am has no sharps or flats";
}
{
- my %key = ABC::key_signature("Ddor");
+ my %key = ABC::Grammar::key_signature("Ddor");
is %key.elems, 0, "Ddor has no sharps or flats";
}
{
- my %key = ABC::key_signature("Ador");
+ my %key = ABC::Grammar::key_signature("Ador");
is %key.elems, 1, "Ador has one sharp";
is %key<F>, "^", "F is sharp";
}
{
- my %key = ABC::key_signature("Amix");
+ my %key = ABC::Grammar::key_signature("Amix");
is %key.elems, 2, "Amix has two sharps";
is %key<F>, "^", "F is sharp";
is %key<C>, "^", "C is sharp";
}
{
- my %key = ABC::key_signature("C#m");
+ my %key = ABC::Grammar::key_signature("C#m");
is %key.elems, 4, "C#m has four sharps";
is %key<F>, "^", "F is sharp";
is %key<C>, "^", "C is sharp";
@@ -50,7 +50,7 @@ plan *;
}
{
- my %key = ABC::key_signature("C#");
+ my %key = ABC::Grammar::key_signature("C#");
is %key.elems, 7, "C# has seven sharps";
is %key<F>, "^", "F is sharp";
is %key<C>, "^", "C is sharp";
@@ -62,16 +62,16 @@ plan *;
}
{
- my %key = ABC::key_signature("C#m");
- is ABC::apply_key_signature(%key, ABC.parse("f", :rule<pitch>)), "^f", "f => ^f";
- is ABC::apply_key_signature(%key, ABC.parse("C", :rule<pitch>)), "^C", "C => ^C";
- is ABC::apply_key_signature(%key, ABC.parse("G", :rule<pitch>)), "^G", "G => ^G";
- is ABC::apply_key_signature(%key, ABC.parse("d", :rule<pitch>)), "^d", "d => ^d";
- is ABC::apply_key_signature(%key, ABC.parse("_f", :rule<pitch>)), "_f", "_f => _f";
- is ABC::apply_key_signature(%key, ABC.parse("=C", :rule<pitch>)), "=C", "=C => =C";
- is ABC::apply_key_signature(%key, ABC.parse("^G", :rule<pitch>)), "^G", "^G => ^G";
- is ABC::apply_key_signature(%key, ABC.parse("^^d", :rule<pitch>)), "^^d", "^^d => ^^d";
- is ABC::apply_key_signature(%key, ABC.parse("b'", :rule<pitch>)), "b'", "b' => b'";
+ my %key = ABC::Grammar::key_signature("C#m");
+ is ABC::Grammar::apply_key_signature(%key, ABC::Grammar.parse("f", :rule<pitch>)), "^f", "f => ^f";
+ is ABC::Grammar::apply_key_signature(%key, ABC::Grammar.parse("C", :rule<pitch>)), "^C", "C => ^C";
+ is ABC::Grammar::apply_key_signature(%key, ABC::Grammar.parse("G", :rule<pitch>)), "^G", "G => ^G";
+ is ABC::Grammar::apply_key_signature(%key, ABC::Grammar.parse("d", :rule<pitch>)), "^d", "d => ^d";
+ is ABC::Grammar::apply_key_signature(%key, ABC::Grammar.parse("_f", :rule<pitch>)), "_f", "_f => _f";
+ is ABC::Grammar::apply_key_signature(%key, ABC::Grammar.parse("=C", :rule<pitch>)), "=C", "=C => =C";
+ is ABC::Grammar::apply_key_signature(%key, ABC::Grammar.parse("^G", :rule<pitch>)), "^G", "^G => ^G";
+ is ABC::Grammar::apply_key_signature(%key, ABC::Grammar.parse("^^d", :rule<pitch>)), "^^d", "^^d => ^^d";
+ is ABC::Grammar::apply_key_signature(%key, ABC::Grammar.parse("b'", :rule<pitch>)), "b'", "b' => b'";
}
View
4 t/03-file.t
@@ -1,11 +1,11 @@
use v6;
use Test;
-use ABC;
+use ABC::Grammar;
plan *;
{
- my $match = ABC.parse(slurp("samples.abc"), :rule<tune_file>);
+ my $match = ABC::Grammar.parse(slurp("samples.abc"), :rule<tune_file>);
isa_ok $match, Match, 'samples.abc is a valid tune file';
is @( $match<tune> ).elems, 3, "Three tunes were found";

0 comments on commit 9613195

Please sign in to comment.