What

Rsec is a dynamic parser generator for Ruby 1.9.

Install

gem in rsec

Use

Here's a simple arithmetic example:

require "rsec"
include Rsec::Helpers

# evaluate binary arithmetic series calculate = proc do |(p, *ps)| ps.each_slice(2).inject(p) do |left, (op, right)| left.send op, right end end
# build the parser num = prim(:double).fail 'number' paren = '('.r >> lazy{expr} << ')' factor = num | paren term = factor.join(one_of_('*/%').fail 'operator').map &calculate expr = term.join(one_of_('+-').fail 'operator').map &calculate arithmetic = expr.eof
# test it! print '1+ 2*4 = ' p arithmetic.parse! '1+ 2*4' #=> 9 print '1+ 2*/4 = ' p arithmetic.parse! '1+ 2*/4' #=> should raise syntax error

Code

Hosted on github.