Module: RBNF
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
- .[](s) ⇒ Object
-
.def(name) ⇒ Object
Takes a symbol <name> and a block and defines a new form <name> to be the contents of the block.
-
.define(name, &b) ⇒ Object
Takes a symbol <name> and a block and defines a new form <name> to be the contents of the block.
-
.dememoize ⇒ Object
Empties the memos of all defined (with ::def or ::define) forms.
- .method_missing(sym, *as) ⇒ Object
Instance Method Summary collapse
-
#=~(s) ⇒ Object
(also: #[])
String match with memoization.
-
#alt(f) ⇒ Object
(also: #/)
“a” | “b”.
-
#cat(f) ⇒ Object
(also: #+)
“a” , “b”.
-
#comps(s, e = heads(s)) ⇒ Object
:nodoc:.
-
#except(f) ⇒ Object
(also: #-)
a - “b”.
-
#group ⇒ Object
( “a” ).
-
#opt ⇒ Object
(also: #-@)
[ “a” ].
-
#rep ⇒ Object
(also: #+@)
{ “a” }.
-
#rep_n(n) ⇒ Object
(also: #*)
b * “a”.
Class Method Details
.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 |
.dememoize ⇒ Object
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 |
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 |