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, #invalid_value, #ptr, #ref, #typedef

Attributes inherited from RBase

#alias, #cname, #csignature, #flags, #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_type, #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!, #generate_signatures, #ptr?, #ref?, #to_ptr, #to_value, #typedef?

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



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

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

#add_data_type_name(name) ⇒ Object



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

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

#add_namespace_name(name) ⇒ Object



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

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

#add_struct_name(name) ⇒ Object



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

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

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



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

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



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

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



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

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
# File 'lib/rbind/default_parser.rb', line 13

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

#on_type_not_found(&block) ⇒ Object



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

def on_type_not_found(&block)
    @on_type_not_found = block
end

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



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

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) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rbind/default_parser.rb', line 212

def parse(string)
    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



109
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
# File 'lib/rbind/default_parser.rb', line 109

def parse_class(line_number,string)
    lines = string.split("\n")
    a = lines.shift.rstrip.split(" : ")
    name = a[0].split(" ")[1]
    parents = a[1]
    parent_classes = if parents
                         parents.split(", ").map do |name|
                             type(RBase.normalize(name),true)
                         end
                     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
                t2.parent_classes = t.instance_variable_get(:@parent_classes)
                t2
            end
        else
            add_type(t)
            t
        end
    line_counter = 1
    lines.each do |line|
        a = attribute(line_counter+line_number,line,t)
        t.add_attribute(a)
        line_counter += 1
    end
    [t,line_counter]
rescue RuntimeError  => e
    raise "input line #{line_number}: #{e}"
end

#parse_const(line_number, string) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/rbind/default_parser.rb', line 161

def parse_const(line_number,string)
    raise "multi line const are not supported: #{string}" if string.split("\n").size > 1
    a = string.split(" ")
    raise "not a constant: #{string}" unless a.shift == "const"
    name = a.shift
    value = a.join(" ")
    c = RConst.new(name,value)
    add_const(c)
    [c,1]
end

#parse_operation(line_number, string) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rbind/default_parser.rb', line 172

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rbind/default_parser.rb', line 142

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,line_counter]
rescue RuntimeError  => e
    raise "input line #{line_number}: #{e}"
end

#split(string) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/rbind/default_parser.rb', line 241

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