TreeSL is my effort to define a flexible domain specific language (DSL). I think it’s similar to a recursive descent parser.
TreeSL will try a number of alternative matches for a given node until it finds a matching alternative. This is incredibly inefficient, but the goal is flexibility, not on-the-fly DSL speed.
#install gemcutter if you haven't
gem install gemcutter
gem tumble
#install treesl
gem install treesl
require 'treesl'
parser = TreeSL.define do
root 'person'
person 'title firstname lastname'
person 'title lastname'
person 'firstname lastname'
title /^Mr|Mrs|Ms$/
firstname 'propernoun'
lastname 'propernoun'
propernoun /^[A-Z][a-z]+/
end
p parser.match("Sam Jones") #=> {"firstname lastname"=>{"lastname"=>"Jones", "firstname"=>"Sam"}}
p parser.match("Mr Sam Jones") #=> {"title firstname lastname"=>{"title"=>"Mr", "lastname"=>"Jones", "firstname"=>"Sam"}}
p parser.match("Mr Jones") #=> {"title lastname"=>{"title"=>"Mr", "lastname"=>"Jones"}}
Copyright © 2009 Jacob Rothstein. See LICENSE for details.