Class: JSGF::Builder
- Inherits:
-
Object
- Object
- JSGF::Builder
- Defined in:
- lib/jsgf/builder.rb
Class Method Summary collapse
-
.build(name = nil, &block) ⇒ Grammar
Convenience method for instantiating a builder and then building a Grammar.
Instance Method Summary collapse
-
#build(name = nil, &block) ⇒ Grammar
A new Grammar built from the block argument.
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
-
#name(arg) ⇒ Object
Set the name attribute from the DSL block.
-
#rule(**options) ⇒ JSGF::Grammar
Create a new rule using the provided name and string By default, all new rules are private, unless they’re root rules.
Constructor Details
#initialize ⇒ Builder
Returns a new instance of Builder.
6 7 8 |
# File 'lib/jsgf/builder.rb', line 6 def initialize @rules = {} end |
Class Method Details
Instance Method Details
#build(name = nil, &block) ⇒ Grammar
Returns a new Grammar built from the block argument.
19 20 21 22 23 |
# File 'lib/jsgf/builder.rb', line 19 def build(name=nil, &block) @name = name || 'DSL' instance_eval(&block) if block_given? Grammar.new(name:@name, rules:@rules) end |
#name(arg) ⇒ Object
Set the name attribute from the DSL block
26 27 28 |
# File 'lib/jsgf/builder.rb', line 26 def name(arg) @name = arg end |
#rule(**options) ⇒ JSGF::Grammar
Create a new rule using the provided name and string
By default, all new rules are private, unless they're root rules
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jsgf/builder.rb', line 37 def rule(**) .each do |name, v| @rules[name.to_s] = case v when Array then [Alternation.new(*v)] when Symbol then [{name:v.to_s, weight:1.0, tags:[]}] else v.split(' ').map do |a| case a when /\<(.*)\>/, /:(.*)/ then {name:$1, weight:1.0, tags:[]} else {atom:a, weight:1.0, tags:[]} end end end end end |