Class: JSGF::Grammar

Inherits:
Object
  • Object
show all
Defined in:
lib/jsgf/grammar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, character_encoding: nil, locale: nil, private_rules: {}, public_rules: {}, version: nil) ⇒ Grammar

Returns a new instance of Grammar.



10
11
12
13
14
15
16
17
# File 'lib/jsgf/grammar.rb', line 10

def initialize(name:nil, character_encoding:nil, locale:nil, private_rules:{}, public_rules:{}, version:nil)
    @character_encoding = character_encoding
    @locale = locale
    @grammar_name = name
    @private_rules = private_rules
    @public_rules = public_rules
    @version = version
end

Instance Attribute Details

#character_encodingObject (readonly)

Returns the value of attribute character_encoding.



3
4
5
# File 'lib/jsgf/grammar.rb', line 3

def character_encoding
  @character_encoding
end

#grammar_nameObject (readonly)

Returns the value of attribute grammar_name.



5
6
7
# File 'lib/jsgf/grammar.rb', line 5

def grammar_name
  @grammar_name
end

#localeObject (readonly)

Returns the value of attribute locale.



4
5
6
# File 'lib/jsgf/grammar.rb', line 4

def locale
  @locale
end

#private_rulesObject (readonly)

Returns the value of attribute private_rules.



6
7
8
# File 'lib/jsgf/grammar.rb', line 6

def private_rules
  @private_rules
end

#public_rulesObject (readonly)

Returns the value of attribute public_rules.



7
8
9
# File 'lib/jsgf/grammar.rb', line 7

def public_rules
  @public_rules
end

#rulesObject



21
22
23
# File 'lib/jsgf/grammar.rb', line 21

def rules
    @public_rules.merge(@private_rules)
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#to_sObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jsgf/grammar.rb', line 25

def to_s
    private_rule_array = @private_rules.map do |(name, rule)|
	atoms = rule.map {|a| unparse_atom(a) }.join(' ')
	"<#{name}> = #{atoms};"
    end
    public_rule_array = @public_rules.map do |(name, rule)|
	atoms = rule.map {|a| unparse_atom(a) }.join(' ')
	"public <#{name}> = #{atoms};"
    end

    [header, grammar_header, *public_rule_array, *private_rule_array].join("\n")
end