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.

Raises:

  • (ArgumentError)


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

def initialize(name:nil, character_encoding:nil, locale:nil, private_rules:{}, public_rules:{}, version:nil)
    raise ArgumentError, "Grammar requires a name" unless name
    @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

#rootsObject



22
23
24
25
26
27
28
29
30
# File 'lib/jsgf/grammar.rb', line 22

def roots
    # Descend through the public rule trees to see if they reference each other
    root_rules = public_rules.dup
    public_rules.each do |(name, rule)|
  names = find_rule_names(rule).compact
  root_rules.delete_if {|k,v| names.include?(k)}
    end
    root_rules.empty? ? nil : root_rules
end

#rulesObject



34
35
36
# File 'lib/jsgf/grammar.rb', line 34

def rules
    @public_rules.merge(@private_rules)
end

#versionObject



40
41
42
# File 'lib/jsgf/grammar.rb', line 40

def version
  @version
end

Instance Method Details

#to_sObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jsgf/grammar.rb', line 44

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