Class: Bvh::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/bvh/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Parser

Returns a new instance of Parser.



6
7
8
9
# File 'lib/bvh/parser.rb', line 6

def initialize(file)
  @filename = file
  @source = File.read(file)
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



3
4
5
# File 'lib/bvh/parser.rb', line 3

def filename
  @filename
end

#sourceObject (readonly)

Returns the value of attribute source.



4
5
6
# File 'lib/bvh/parser.rb', line 4

def source
  @source
end

Instance Method Details

#parse(bvh) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bvh/parser.rb', line 11

def parse(bvh)
  @bvh = bvh

  # a little touch-up...
  s = source
  # it's tempting to just downcase the whole string, but that'd change the case of object names, which might be
  # bad for the end user. So we'll swap out specific keywords instead.
  s.gsub!(/^((\s*)(root|offset|channels|joint|end site|hierarchy)(([^\n\{]*)(\n|\{)))/mi) do
    match = $~
    "#{match[2]}#{match[3].downcase.gsub(/\s/, '_')} \"#{match[5].strip}\"#{match[6]}"
  end
  # make { . . . } into proper Ruby blocks
  s.gsub!(/[\n\s]*\{/m, ' do').gsub!(/[\n\s]*\}/m, "\nend")

  # Finally, handle the MOTION segment, which can be treated as a single method call.
  s.gsub!(/^((\s*)(motion)(.*))/mi) do
    "#{$~[2]}#{$~[3].downcase} <<-EOF\n#{$~[4].strip}\nEOF\n"
  end

  eval(s, binding, @filename, 1)
end