amiika / ziffers

Numbered musical notation for composing algorithmic and generative melodies
MIT License
81 stars 5 forks source link

Rewrite zparse #28

Closed amiika closed 2 years ago

amiika commented 4 years ago

Current string parsing function zparse is an accumulated piece of steaming spaghetti and meatballs. Refactor parsing function to produce plain parsed hash array and create separate functions for sliding, chord parsing etc etc.

Plain hash arrays (without parsed notes and chords) would work better with loops as they could be resolved on the fly.

amiika commented 3 years ago

Tried out treetop for parsing the syntax, looks promising:

Treetop.load_from_string("
grammar Ziffers

  rule line
    (repeat / s / bar / chord / group / parens / control)*
  {
   def value
    elements.collect {|v| 
     v.value }
   end
  }
  end

  rule chord
  s [0-9]+ s
  {
   def value
    text_value
   end
  }
  end

  rule group
  [\-a-z0-9]+
  {
   def value
    text_value
   end
  }
  end

  rule parens
    '(' line ')'
  {
   def value
    line.elements.collect { |v|
     v.value
   } 
   end
  }
end

 rule control
 [A-Z] [0-9\.0-9]*
{
   def value
    text_value
   end
  }
end

  rule repeat
   '|:' line ':' num '|'
{
   def value
    text_value
   end
  }
end

  rule num
   [0-9]? {
   def value
    text_value
   end }
  end

  rule s
    [\s]
{
   def value
    ''
   end
  }
  end

  rule bar
   '|' {
def value
    '|'
   end
}
  end

end
")
amiika commented 2 years ago

Done in Ziffers 2