Class: FFIDB::LibraryParser

Inherits:
Object
  • Object
show all
Includes:
SymbolTable
Defined in:
lib/ffidb/library_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SymbolTable

#each_type, #types

Constructor Details

#initialize(path) ⇒ LibraryParser

Returns a new instance of LibraryParser.

Parameters:

  • path (Pathname, #to_s)


16
17
18
# File 'lib/ffidb/library_parser.rb', line 16

def initialize(path)
  @path = Pathname(path).freeze
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/ffidb/library_parser.rb', line 12

def path
  @path
end

Instance Method Details

#each_enum {|enum| ... } ⇒ Enumerator

Yields:

  • (enum)

Yield Parameters:

  • Enum (enum)

Returns:

  • (Enumerator)


33
34
35
36
# File 'lib/ffidb/library_parser.rb', line 33

def each_enum(&block)
  return self.to_enum(:each_enum) unless block_given?
  self.parse_yaml_dir(self.path, [:enum], &block)
end

#each_function {|function| ... } ⇒ Enumerator

Yields:

  • (function)

Yield Parameters:

  • Function (function)

Returns:

  • (Enumerator)


60
61
62
63
# File 'lib/ffidb/library_parser.rb', line 60

def each_function(&block)
  return self.to_enum(:each_function) unless block_given?
  self.parse_yaml_dir(self.path, [:function], &block)
end

#each_struct {|struct| ... } ⇒ Enumerator

Yields:

  • (struct)

Yield Parameters:

  • Struct (struct)

Returns:

  • (Enumerator)


42
43
44
45
# File 'lib/ffidb/library_parser.rb', line 42

def each_struct(&block)
  return self.to_enum(:each_struct) unless block_given?
  self.parse_yaml_dir(self.path, [:struct], &block)
end

#each_symbol {|symbol| ... } ⇒ Enumerator

Yields:

  • (symbol)

Yield Parameters:

  • Symbolic (symbol)

Returns:

  • (Enumerator)


69
70
71
72
# File 'lib/ffidb/library_parser.rb', line 69

def each_symbol(&block)
  return self.to_enum(:each_symbol) unless block_given?
  self.parse_yaml_dir(self.path, nil, &block)
end

#each_typedef {|typedef| ... } ⇒ Enumerator

Yields:

  • (typedef)

Yield Parameters:

  • Symbolic (symbol)

Returns:

  • (Enumerator)


24
25
26
27
# File 'lib/ffidb/library_parser.rb', line 24

def each_typedef(&block)
  return self.to_enum(:each_typedef) unless block_given?
  self.parse_yaml_dir(self.path, [:typedef], &block)
end

#each_union {|union| ... } ⇒ Enumerator

Yields:

  • (union)

Yield Parameters:

  • Union (union)

Returns:

  • (Enumerator)


51
52
53
54
# File 'lib/ffidb/library_parser.rb', line 51

def each_union(&block)
  return self.to_enum(:each_union) unless block_given?
  self.parse_yaml_dir(self.path, [:union], &block)
end

#parse_yaml_dir(path, kind_filter = nil) {|symbol| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • path (Pathname)
  • kind_filter (Array<Symbol>) (defaults to: nil)

Yields:

  • (symbol)

Yield Parameters:

  • Symbolic (symbol)


80
81
82
83
84
85
86
# File 'lib/ffidb/library_parser.rb', line 80

def parse_yaml_dir(path, kind_filter = nil, &block)
  self.path.glob('*.yaml') do |path|
    path.open do |file|
      self.parse_yaml_file(file, kind_filter, &block)
    end
  end
end

#parse_yaml_file(file, kind_filter = nil) {|symbol| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • file (IO)
  • kind_filter (Array<Symbol>) (defaults to: nil)

Yields:

  • (symbol)

Yield Parameters:

  • Symbolic (symbol)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ffidb/library_parser.rb', line 94

def parse_yaml_file(file, kind_filter = nil, &block)
  Psych.parse_stream(file) do |yaml_doc|
    kind = yaml_doc.children.first.tag[1..-1].to_sym
    next if kind_filter && !kind_filter.include?(kind)
    yaml = yaml_doc.to_ruby.transform_keys!(&:to_sym)
    case kind
      when :typedef
        yield Typedef.new(yaml[:name], Type.for(yaml[:type]), yaml[:comment])
      when :enum
        yield Enum.new(yaml[:name], yaml[:values] || {}, yaml[:comment])
      when :struct
        fields = (yaml[:fields] || {}).inject({}) do |fs, (k, v)|
          fs[k.to_sym] = Type.for(v)
          fs
        end
        yield Struct.new(yaml[:name], fields, yaml[:comment])
      when :union
        yield Union.new(yaml[:name], yaml[:fields] || {}, yaml[:comment])
      when :function
        parameters = (yaml[:parameters] || {}).inject({}) do |ps, (k, v)|
          k = k.to_sym
          ps[k] = Parameter.new(k, Type.for(v))
          ps
        end
        yield Function.new(
          name: yaml[:name],
          type: Type.for(yaml[:type]),
          parameters: parameters,
          definition: !yaml.has_key?(:definition) ? nil : Location.new(
            file: yaml.dig(:definition, 'file'),
            line: yaml.dig(:definition, 'line'),
          ),
          comment: yaml[:comment],
        )
    end
  end
end