Class: VhdlDoctest::TestParser

Inherits:
Object
  • Object
show all
Defined in:
lib/vhdl_doctest/test_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(vhdl) ⇒ TestParser

Returns a new instance of TestParser.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vhdl_doctest/test_parser.rb', line 9

def initialize(vhdl)
  @vhdl = vhdl
  d = lambda do |x|
    assert_in_range(("0".."9").to_a + %w{ - }, x)
    x.to_i
  end

  h = lambda do |x|
    assert_in_range(("0".."9").to_a + ("a".."f").to_a, x)
    x.to_i(16)
  end

  b = lambda do |x|
    assert_in_range(%w{ 0 1 }, x)
    x.to_i(2)
  end
  @decoders = { 'd' => d, 'h' => h, 'x' => h, 'b' => b }
end

Instance Method Details

#decode(decoder, value) ⇒ Object

find decoder to decode given value



35
36
37
38
39
40
41
# File 'lib/vhdl_doctest/test_parser.rb', line 35

def decode(decoder, value)
  if fun = @decoders[decoder || 'd']
    fun.call(value)
  else
    raise "Cannot decode #{value} with decoder #{decoder}.  Unknown decoder."
  end
end

#def_to_lambda(def_string) ⇒ Object

convert decoder definition string to lambda Example:

def f { |x| x.include?(".") ? [x.to_f].pack('f').unpack('I').first : x.to_i }
=> #<Proc>


47
48
49
50
51
# File 'lib/vhdl_doctest/test_parser.rb', line 47

def def_to_lambda(def_string)
  if def_string.match(/def\s+([[:alnum:]]*)\s+{([^}]*)}/)
    [$1, eval("lambda { #{ $2 } }")]
  end
end

#parse(ports) ⇒ Object



28
29
30
31
32
# File 'lib/vhdl_doctest/test_parser.rb', line 28

def parse(ports)
  names, vectors = extract_values(@vhdl)
  defined_ports = names.map { |name| ports.find { |p| p.name == name } }
  vectors.map { |v| TestCase.new(Hash[defined_ports.zip(v)]) }
end

#register_decoder(defs) ⇒ Object



53
54
55
# File 'lib/vhdl_doctest/test_parser.rb', line 53

def register_decoder(defs)
  @decoders.merge! Hash[defs.map{ |d| def_to_lambda(d) }]
end