Class: Rbind::DefaultParser

Inherits:
RNamespace show all
Extended by:
Logger
Defined in:
lib/rbind/default_parser.rb

Instance Attribute Summary

Attributes included from Logger

#log

Attributes inherited from RNamespace

#consts, #operation_alias, #operations, #root, #used_namespaces

Attributes inherited from RDataType

#cdelete_method, #check_type, #extern_package_name, #invalid_value, #ptr, #ref, #typedef

Attributes inherited from RBase

#alias, #cname, #csignature, #flags, #ignore, #name, #namespace, #owner, #signature, #version

Instance Method Summary collapse

Methods included from Logger

extend_object

Methods inherited from RNamespace

#add_const, #add_default_types, #add_namespace, #add_operation, #add_simple_type, #add_simple_types, #add_type, #const, #constructor?, #container?, #delete_type, #each_const, #each_container, #each_operation, #each_type, #extern?, #method_missing, #operation, #operation?, #pretty_print, #pretty_print_name, #root?, #type, #types, #use_namespace

Methods inherited from RDataType

#==, #basic_type?, #check_type?, #cname, #container?, #delete!, #extern?, #generate_signatures, #ptr?, #ref?, #to_ptr, #to_value, #typedef?, #valid_flags

Methods inherited from RBase

#add_flag, basename, #binding, #full_name, #generate_signatures, #ignore?, #map_to_namespace, namespace, #namespace?, normalize, #pretty_print, to_cname, #valid_flags, #validate_flags

Constructor Details

#initializeDefaultParser

Returns a new instance of DefaultParser.



6
7
8
9
10
11
# File 'lib/rbind/default_parser.rb', line 6

def initialize
    super("root")
    self.root = true
    add_default_types
    @on_type_not_found
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rbind::RNamespace

Instance Method Details

#add_class_name(name) ⇒ Object



52
53
54
55
56
# File 'lib/rbind/default_parser.rb', line 52

def add_class_name(name)
    klass = RClass.new(name)
    add_type klass
    klass
end

#add_data_type_name(name) ⇒ Object



34
35
36
37
38
# File 'lib/rbind/default_parser.rb', line 34

def add_data_type_name(name)
    t = RDataType.new(name)
    add_type t
    t
end

#add_namespace_name(name) ⇒ Object



40
41
42
43
44
# File 'lib/rbind/default_parser.rb', line 40

def add_namespace_name(name)
    ns = RNamespace.new(name)
    add_type ns
    ns
end

#add_struct_name(name) ⇒ Object



46
47
48
49
50
# File 'lib/rbind/default_parser.rb', line 46

def add_struct_name(name)
    s = RStruct.new(name)
    add_type s
    s
end

#attribute(line_number, string, owner = self) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rbind/default_parser.rb', line 98

def attribute(line_number,string,owner=self)
    flags = string.split(" /")
    array = flags.shift.split(" ")
    type_name = array[0]
    name = array[1]
    type = find_type(owner,type_name)
    flags = normalize_flags(line_number,flags)
    RAttribute.new(name,type,flags)
rescue RuntimeError => e
    raise "input line #{line_number}: #{e}"
end

#find_type(owner, type_name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rbind/default_parser.rb', line 63

def find_type(owner,type_name)
    t = owner.type(type_name,false)
    return t if t
    
    normalized = type_name.split("_")
    name = normalized.shift
    while !normalized.empty?
        name += "::#{normalized.shift}"
        t = if normalized.empty?
                owner.type(name,false)
            else
                owner.type("#{name}_#{normalized.join("_")}",false)
            end
        return t if t
    end
    t = @on_type_not_found.call(owner,type_name) if @on_type_not_found
    return t if t

    #search again even if we know the type is not there to create a proper error message
    owner.type(type_name,true)
end

#normalize_default_value(value) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/rbind/default_parser.rb', line 25

def normalize_default_value(value)
    val = value.gsub(/(.?)std::vector<(.*)>/,'\1vector_\2')
    if value != val
        normalize_default_value(val)
    else
        val
    end
end

#normalize_flags(line_number, flags) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rbind/default_parser.rb', line 13

def normalize_flags(line_number,flags)
    flags.map do |flag|
        next if flag.empty?
        if flag =~ /(\w*)(.*)/
            DefaultParser.log.debug "input line #{line_number}: ignoring flag #{$2}" unless $2.empty?
            $1.to_sym
        else
            raise "cannot parse flag #{flag.inspect}"
        end
    end.compact
end

#on_type_not_found(&block) ⇒ Object



58
59
60
# File 'lib/rbind/default_parser.rb', line 58

def on_type_not_found(&block)
    @on_type_not_found = block
end

#parameter(line_number, string, owner = self) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rbind/default_parser.rb', line 85

def parameter(line_number,string,owner = self)
    flags = string.split(" /")
    array = flags.shift.split(" ")
    type_name = array.shift
    para_name = array.shift
    default = normalize_default_value(array.join(" "))
    type = find_type(owner,type_name)
    flags = normalize_flags(line_number,flags)
    RParameter.new(para_name,type,default,flags)
rescue RuntimeError => e
    raise "input line #{line_number}: #{e}"
end

#parse(string, extern_package_name = nil) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/rbind/default_parser.rb', line 235

def parse(string,extern_package_name=nil)
    @extern_package_name = extern_package_name

    a = split(string)
    a.pop #remove number at the end of the file
    line_number = 1
    a.each do |block|
        begin
        first = block.split(" ",2)[0]
        obj,lines = if first == "const"
                        parse_const(line_number,block)
                    elsif first == "class"
                        parse_class(line_number,block)
                    elsif first == "struct"
                        parse_struct(line_number,block)
                    else
                        parse_operation(line_number,block)
                    end
        line_number+=lines
        rescue RuntimeError => e
            puts "Parsing Error: #{e}"
            puts "Line #{line_number}:"
            puts "--------------------------------------------------"
            puts block
            puts "--------------------------------------------------"
            Kernel.raise
            break
        end
    end
end

#parse_class(line_number, string) ⇒ Object



110
111
112
113
114
115
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
# File 'lib/rbind/default_parser.rb', line 110

def parse_class(line_number,string)
    lines = string.split("\n")
    a = lines.shift.rstrip
    unless a =~ /class ([a-zA-Z\.\d_:]*) ?:?([a-zA-Z\.\:, \d_]*)(.*)/
        raise "cannot parse class #{a}"
    end
    name = $1
    parent_classes = $2
    flags = $3
    parent_classes = if parent_classes
                         parent_classes.gsub(" ","").split(",").map do |name|
                             t = type(RBase.normalize(name),false)
                             # auto add parent class
                             t ||= add_type(RClass.new(RBase.normalize(name)))
                         end
                     end
    flags = if flags
               normalize_flags(line_number,flags.gsub(" ","").split("/").compact)
            end
    t = RClass.new(name,*parent_classes)
    t = if t2 = type(t.full_name,false)
            if !t2.is_a?(RClass) || (!t2.parent_classes.empty? && t2.parent_classes != t.parent_classes)
                raise "Cannot add class #{t.full_name}. A different type #{t2} is already registered"
            else
                t.parent_classes.each do |p|
                    t2.add_parent p
                end
                t2
            end
        else
            add_type(t)
            t
        end
    t.flags = flags if flags
    line_counter = 1
    lines.each do |line|
        a = attribute(line_counter+line_number,line,t)
        t.add_attribute(a)
        line_counter += 1
    end
    t.extern_package_name = @extern_package_name
    [t,line_counter]
rescue RuntimeError  => e
    raise "input line #{line_number}: #{e}"
end

#parse_const(line_number, string) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rbind/default_parser.rb', line 176

def parse_const(line_number,string)
    raise "multi line const are not supported: #{string}" if string.split("\n").size > 1
    unless string =~ /const ([a-zA-Z\.\d_:]*) ?([^\/]*)(.*)/
        raise "cannot parse const #{string}"
    end
    name = $1
    value = $2.chomp("\n").chomp(" ")
    flags = $3
    flags = if flags
               normalize_flags(line_number,flags.gsub(" ","").split("/").compact)
            end

    c = RConst.new(name,value)
    c.flags = flags if flags
    c.extern_package_name = @extern_package_name
    add_const(c)
    [c,1]
end

#parse_operation(line_number, string) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rbind/default_parser.rb', line 195

def parse_operation(line_number,string)
    a = string.split("\n")
    line = a.shift
    flags = line.split(" /")
    line = flags.shift
    elements = line.split(" ")
    name = elements.shift
    return_type_name = elements.shift
    if return_type_name == "()"
        name += return_type_name
        return_type_name = elements.shift
    end
    alias_name = elements.shift
    alias_name = if alias_name
                     raise "#{line_number}: cannot parse #{string}" unless alias_name =~/^=.*/
                     alias_name.gsub("=","")
                 end

    ns = RBase.namespace(name)
    owner = type(ns,true)
    if return_type_name == "explicit"
        flags << return_type_name
        return_type_name = nil
    end
    return_type = if return_type_name && !return_type_name.empty?
                      find_type(owner,return_type_name)
                  end
    line_counter = 1
    args = a.map do |line|
        p = parameter(line_number+line_counter,line,owner)
        line_counter += 1
        p
    end
    op = ::Rbind::ROperation.new(name,return_type,*args)
    op.alias = alias_name if alias_name && !alias_name.empty?
    op.flags = normalize_flags(line_number,flags)
    type(op.namespace,true).add_operation(op)
    [op,line_counter]
end

#parse_struct(line_number, string) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rbind/default_parser.rb', line 156

def parse_struct(line_number,string)
    a = string.split("\n")
    first_line = a.shift
    flags = first_line.split(" /")
    name = flags.shift.split(" ")[1]
    flags = normalize_flags(line_number,flags)
    klass = RStruct.new(name,flags)
    add_type(klass)
    line_counter = 1
    a.each do |line|
        a = attribute(line_counter+line_number,line,klass)
        klass.add_attribute(a)
        line_counter += 1
    end
    klass.extern_package_name = @extern_package_name
    [klass,line_counter]
rescue RuntimeError  => e
    raise "input line #{line_number}: #{e}"
end

#split(string) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/rbind/default_parser.rb', line 266

def split(string)
    array = []
    string.each_line do |line|
        if !line.empty? && line[0] != " "
            array << line
        else
            array[array.size-1] = array.last + line
        end
    end
    array
end