Module: Fiddle::CParser

Included in:
Importer
Defined in:
lib/fiddle/cparser.rb

Overview

A mixin that provides methods for parsing C struct and prototype signatures.

Example

require 'fiddle/import'

include Fiddle::CParser
  #=> Object

parse_ctype('int increment(int)')
  #=> ["increment", Fiddle::TYPE_INT, [Fiddle::TYPE_INT]]

Instance Method Summary collapse

Instance Method Details

#parse_ctype(ty, tymap = nil) ⇒ Object

Given a String of C type ty, returns the corresponding Fiddle constant.

ty can also accept an Array of C type Strings, and will be returned in a corresponding Array.

If Hash tymap is provided, ty is expected to be the key, and the value will be the C type to be looked up.

Example:

include Fiddle::CParser
  #=> Object

parse_ctype('int')
  #=> Fiddle::TYPE_INT

parse_ctype('double')
  #=> Fiddle::TYPE_DOUBLE

parse_ctype('unsigned char')
  #=> -Fiddle::TYPE_CHAR


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/fiddle/cparser.rb', line 116

def parse_ctype(ty, tymap=nil)
  tymap ||= {}
  case ty
  when Array
    return [parse_ctype(ty[0], tymap), ty[1]]
  when "void"
    return TYPE_VOID
  when "char"
    return TYPE_CHAR
  when "unsigned char"
    return  -TYPE_CHAR
  when "short"
    return TYPE_SHORT
  when "unsigned short"
    return -TYPE_SHORT
  when "int"
    return TYPE_INT
  when "unsigned int", 'uint'
    return -TYPE_INT
  when "long"
    return TYPE_LONG
  when "unsigned long"
    return -TYPE_LONG
  when "long long"
    if( defined?(TYPE_LONG_LONG) )
      return TYPE_LONG_LONG
    else
      raise(RuntimeError, "unsupported type: #{ty}")
    end
  when "unsigned long long"
    if( defined?(TYPE_LONG_LONG) )
      return -TYPE_LONG_LONG
    else
      raise(RuntimeError, "unsupported type: #{ty}")
    end
  when "float"
    return TYPE_FLOAT
  when "double"
    return TYPE_DOUBLE
  when "size_t"
    return TYPE_SIZE_T
  when "ssize_t"
    return TYPE_SSIZE_T
  when "ptrdiff_t"
    return TYPE_PTRDIFF_T
  when "intptr_t"
    return TYPE_INTPTR_T
  when "uintptr_t"
    return TYPE_UINTPTR_T
  when /\*/, /\[\s*\]/
    return TYPE_VOIDP
  else
    if( tymap[ty] )
      return parse_ctype(tymap[ty], tymap)
    else
      raise(DLError, "unknown type: #{ty}")
    end
  end
end

#parse_signature(signature, tymap = nil) ⇒ Object

Parses a C prototype signature

If Hash tymap is provided, the return value and the arguments from the signature are expected to be keys, and the value will be the C type to be looked up.

Example:

include Fiddle::CParser
  #=> Object

parse_signature('double sum(double, double)')
  #=> ["sum", Fiddle::TYPE_DOUBLE, [Fiddle::TYPE_DOUBLE, Fiddle::TYPE_DOUBLE]]


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fiddle/cparser.rb', line 73

def parse_signature(signature, tymap=nil)
  tymap ||= {}
  signature = signature.gsub(/\s+/, " ").strip
  case signature
  when /^([\w@\*\s]+)\(([\w\*\s\,\[\]]*)\)$/
    ret = $1
    (args = $2).strip!
    ret = ret.split(/\s+/)
    args = args.split(/\s*,\s*/)
    func = ret.pop
    if( func =~ /^\*/ )
      func.gsub!(/^\*+/,"")
      ret.push("*")
    end
    ret  = ret.join(" ")
    return [func, parse_ctype(ret, tymap), args.collect{|arg| parse_ctype(arg, tymap)}]
  else
    raise(RuntimeError,"can't parse the function prototype: #{signature}")
  end
end

#parse_struct_signature(signature, tymap = nil) ⇒ Object

Parses a C struct’s members

Example:

include Fiddle::CParser
  #=> Object

parse_struct_signature(['int i', 'char c'])
  #=> [[Fiddle::TYPE_INT, Fiddle::TYPE_CHAR], ["i", "c"]]


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fiddle/cparser.rb', line 24

def parse_struct_signature(signature, tymap=nil)
  if( signature.is_a?(String) )
    signature = signature.split(/\s*,\s*/)
  end
  mems = []
  tys  = []
  signature.each{|msig|
    tks = msig.split(/\s+(\*)?/)
    ty = tks[0..-2].join(" ")
    member = tks[-1]

    case ty
    when /\[(\d+)\]/
      n = $1.to_i
      ty.gsub!(/\s*\[\d+\]/,"")
      ty = [ty, n]
    when /\[\]/
      ty.gsub!(/\s*\[\]/, "*")
    end

    case member
    when /\[(\d+)\]/
      ty = [ty, $1.to_i]
      member.gsub!(/\s*\[\d+\]/,"")
    when /\[\]/
      ty = ty + "*"
      member.gsub!(/\s*\[\]/, "")
    end

    mems.push(member)
    tys.push(parse_ctype(ty,tymap))
  }
  return tys, mems
end