Class: BNF::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/bnf/tree.rb

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Tree

Returns a new instance of Tree.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bnf/tree.rb', line 8

def initialize(version)
  if version == :bnf
    filename = File.expand_path('../../../data/bnf.json.gz', __FILE__)
  elsif version == :bnfc
    filename = File.expand_path('../../../data/bnfc.json.gz', __FILE__)
  else
    raise 'Must use :bnf or :bnfc to choose BNF version to load'
  end
  gzipped_file = File.open(filename)
  file = Zlib::GzipReader.new(gzipped_file)
  @tree = JSON.parse(file.read)
end

Instance Method Details

#eachObject



21
22
23
24
25
# File 'lib/bnf/tree.rb', line 21

def each
  @tree['entries'].each do |h|
    yield h
  end
end

#find(match) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/bnf/tree.rb', line 27

def find(match)
  if match.is_a? String
    return find_by_string(match)
  elsif match.is_a? Regexp
    return find_by_regex(match)
  else
    raise 'Tree.find(match) accepts a string or a regex'
  end
end