Module: RBNF

Included in:
Binary, Unary
Defined in:
lib/rbnf.rb,
lib/rbnf/nodes.rb,
lib/rbnf/version.rb

Defined Under Namespace

Classes: Alt, Binary, Cat, Def, Except, Group, Opt, Rep, RepN, Term, Unary

Constant Summary collapse

DEFS =

Namespace for defined forms.

{}
VERSION =
'0.0.3'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](s) ⇒ Object



70
71
72
# File 'lib/rbnf.rb', line 70

def [](s)
  Term.new s
end

.def(name) ⇒ Object

Takes a symbol <name> and a block and defines a new form <name> to be the contents of the block. Note that this method can’t handle recursive form definition; use ::define for that instead.



77
78
79
# File 'lib/rbnf.rb', line 77

def def(name)
  DEFS[name] = Def.new name, yield
end

.define(name, &b) ⇒ Object

Takes a symbol <name> and a block and defines a new form <name> to be the contents of the block. In contrast to ::def this method can handle recursive definition, at the cost of building a new AST each time a match is attempted on the new form.



85
86
87
# File 'lib/rbnf.rb', line 85

def define(name,&b)
  DEFS[name] = Def.new name, ->(s){b.call=~s}
end

.dememoizeObject

Empties the memos of all defined (with ::def or ::define) forms.



90
91
92
# File 'lib/rbnf.rb', line 90

def dememoize
  DEFS.values.each {|v| v.instance_variable_set :@memo, {}}
end

.method_missing(sym, *as) ⇒ Object



94
95
96
# File 'lib/rbnf.rb', line 94

def method_missing(sym,*as)
  DEFS.has_key?(sym) ? DEFS[sym] : super
end

Instance Method Details

#=~(s) ⇒ Object Also known as: []

String match with memoization.



43
44
45
# File 'lib/rbnf.rb', line 43

def =~(s)
  @memo.has_key?(s) ? @memo[s] : (@memo[s] = match s)
end

#alt(f) ⇒ Object Also known as: /

“a” | “b”



18
19
20
# File 'lib/rbnf.rb', line 18

def alt(f)
  Alt.new self, ebnify(f)
end

#cat(f) ⇒ Object Also known as: +

“a” , “b”



13
14
15
# File 'lib/rbnf.rb', line 13

def cat(f)
  Cat.new self, ebnify(f)
end

#comps(s, e = heads(s)) ⇒ Object

:nodoc:



47
48
49
# File 'lib/rbnf.rb', line 47

def comps(s, e=heads(s)) #:nodoc:
  Enumerator.new {|y| e.each {|h| self=~h ? (y<<s.slice(h.size..-1)) : next}}
end

#except(f) ⇒ Object Also known as: -

a - “b”



23
24
25
# File 'lib/rbnf.rb', line 23

def except(f)
  Except.new self, ebnify(f)
end

#groupObject

( “a” )



38
39
40
# File 'lib/rbnf.rb', line 38

def group
  Group.new self
end

#optObject Also known as: -@

“a”


8
9
10
# File 'lib/rbnf.rb', line 8

def opt
  Opt.new self
end

#repObject Also known as: +@

{ “a” }



28
29
30
# File 'lib/rbnf.rb', line 28

def rep
  Rep.new self
end

#rep_n(n) ⇒ Object Also known as: *

b * “a”



33
34
35
# File 'lib/rbnf.rb', line 33

def rep_n(n)
  RepN.new self, n
end