Permalink
Browse files

added a syntax for notes and timelines

[delivers #15547561] [delivers #15547549]
  • Loading branch information...
1 parent 93af6ee commit 49e0d6ca43aeb75a198fa578802ab5052aca95f8 @adamjmurray committed Aug 9, 2011
Showing with 52 additions and 2 deletions.
  1. +4 −2 lib/mtk/lang/grammar.rb
  2. +22 −0 lib/mtk/lang/mtk_grammar.citrus
  3. +26 −0 spec/mtk/lang/grammar_spec.rb
View
@@ -14,8 +14,10 @@ module Lang
# Parser for the {file:lib/mtk/lang/mtk_grammar.citrus MTK grammar}
class Grammar
- def self.parse(syntax, root=:pitch)
- MTK_Grammar.parse(syntax, :root => root).value
+ def self.parse(syntax, root=:timeline)
+ syntax = syntax.to_s.strip
+ return nil if syntax.empty?
+ MTK_Grammar.parse(syntax.strip, :root => root).value
end
end
@@ -1,6 +1,28 @@
grammar MTK_Grammar
include MTK
+ rule timeline
+ ( '{' space? number space? ':' space? note_like (space number space? ':' space? note_like)* space? '}' ) {
+ Timeline.from_a values(:number).zip(values :note_like)
+ }
+ end
+
+ rule note_like
+ note_list | note
+ end
+
+ rule note_list
+ ( '[' space? note (space note)* space? ']' ) {
+ values(:note)
+ }
+ end
+
+ rule note
+ ( '{' space? pitch space intensity space duration space? '}' ) {
+ Note(pitch.value, intensity.value, duration.value)
+ }
+ end
+
rule pitch_sequence
( left_paren pitch_like (space pitch_like)* right_paren ) {
Pattern.PitchSequence *values(:pitch_like)
@@ -9,6 +9,32 @@ def parse syntax, root
describe ".parse" do
+ context "timeline" do
+ it "should parse a Timeline" do
+ parse("
+ {
+ 0: {C4 mp q}
+ 1: {D4 f h}
+ }
+ ", :timeline).should == Timeline.from_hash({0 => Note(C4,mp,q), 1 => Note(D4,f,h)})
+ end
+
+ it "should parse a Timeline" do
+ parse("
+ {
+ 0: [{C4 mp q} {D4 f h}]
+ }
+ ", :timeline).should == Timeline.from_hash({0 => [Note(C4,mp,q), Note(D4,f,h)]})
+ end
+ end
+
+ it "should parse a list of notes in square brackets as an Array of Notes" do
+ parse("[{C4 mp q} {D4 f h}]", :note_list).should == [Note(C4,mp,q), Note(D4,f,h)]
+ end
+
+ it "should parse {pitch intensity duration} as a Note" do
+ parse("{C4 mp q}", :note).should == Note(C4,mp,q)
+ end
context "pitch_sequence" do
it "should parse pitch sequences" do

0 comments on commit 49e0d6c

Please sign in to comment.