Project

rbnf

0.0
No commit activity in last 3 years
No release in over 3 years
Extended Backus-Naur Form implementation for Ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0.0.1
 Project Readme

RBNF

RBNF is an Extended Backus-Naur Form implementation for Ruby.

Installation

  gem install rbnf

Usage

  form  = RBNF[?a] / ?b / ?c         #=> '"a" | "b" | "c"'
  form2 = RBNF[?a].cat('b').alt('c') #=> '( "a" , "b" ) | "c"'

RBNF objects can be used to perform regex-like matches on strings. The first form will match the same strings as /[abc]/ :

  form =~ 'a'  #=> true
  form =~ 'b'  #=> true
  form =~ 'd'  #=> false
  form =~ 'ab' #=> false

RBNFs can also accomplish matches that regexes can't:

  RBNF.define :parens do
    RBNF[?(] + RBNF.parens.opt + ?)
  end

  RBNF.parens =~ '()'    #=> true
  RBNF.parens =~ '(())'  #=> true
  RBNF.parens =~ '(()))' #=> false

RBNF objects memoize matches, but otherwise make no attempt at optimizing for performance.