Class: RubyQt6::RSpec::QlassFileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/qt6/rspec/qlass_file_parser.rb

Defined Under Namespace

Classes: MissingLine

Constant Summary collapse

NESTED_QLASSES =
[
  "QMetaObjectConnection",
  "QPainterPathElement",
  "QFormLayoutTakeRowResult",
  "QTextEditExtraSelection"
]

Instance Method Summary collapse

Constructor Details

#initialize(cppfile, qmod) ⇒ QlassFileParser

Returns a new instance of QlassFileParser.



19
20
21
22
23
# File 'lib/qt6/rspec/qlass_file_parser.rb', line 19

def initialize(cppfile, qmod)
  @qmod = qmod
  @qlasses = []
  @lines, @lineno = File.read(cppfile).lines, 0
end

Instance Method Details

#lineObject



250
251
252
# File 'lib/qt6/rspec/qlass_file_parser.rb', line 250

def line
  @lines[@lineno].strip
end

#parseObject

Raises:



25
26
27
28
29
30
# File 'lib/qt6/rspec/qlass_file_parser.rb', line 25

def parse
  parse_qlass_variants_declaration
  parse_qlass_definitions
  raise MissingLine.new("}", line) unless line == "}"
  @qlasses
end

#parse_qlass_definition(qlass) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
131
132
133
134
135
136
137
# File 'lib/qt6/rspec/qlass_file_parser.rb', line 65

def parse_qlass_definition(qlass)
  qmod_name = @qmod.name
  name = qlass.name

  expected = "rb_c#{name} ="
  if line == expected
    take_next_line
  else
    raise MissingLine.new(expected, line)
  end

  expected = "// RubyQt6::#{qmod_name}::#{name}"
  if line == expected
    take_next_line
  else
    raise MissingLine.new(expected, line)
  end

  if line.start_with?(/define_class_under<#{name}.*rb_mQt6#{qmod_name}, "#{name}"/)
    take_next_line
  else
    raise MissingLine.new("define_class_under<#{name}...", line)
  end

  loop do
    case line
    when /\/\/ RubyQt6-Defined Functions/
      qlass.methods.concat parse_qlass_definition_methods(qlass, :rubyqt6_defined_functions)
    when /\/\/ Constructor/
      qlass.methods.concat parse_qlass_definition_methods(qlass, :constructor)
    when /\/\/ Inherits/
      qlass.methods.concat parse_qlass_definition_methods(qlass, :inherits)
    when /\/\/ Public Functions/
      qlass.methods.concat parse_qlass_definition_methods(qlass, :public_functions)
    when /\/\/ Public Slots/
      qlass.methods.concat parse_qlass_definition_methods(qlass, :public_slots)
    when /\/\/ Signals/
      qlass.methods.concat parse_qlass_definition_methods(qlass, :signals)
    when /\/\/ Static Public Members/
      qlass.methods.concat parse_qlass_definition_methods(qlass, :static_public_members)
    when /.define_attr/
      take_next_line
      next
    when "", "}"
      break
    else
      raise MissingLine.new("// Constructor", line)
    end
  end

  while line == "" || line == "}"
    return if line == "}"
    take_next_line
  end

  while (matched = line.match(/^Data_Type<(.*)rb_c#{name}(.*) =/))
    break if matched[1].start_with?("QFlags")
    enum = Struct.new(:name).new(matched[2])
    qlass.enums << enum
    parse_qlass_definition_enum(qlass, enum)
  end

  while line == "" || line == "}"
    return if line == "}"
    take_next_line
  end

  while (matched = line.match(/^Data_Type<QFlags.*rb_c#{name}(.*) =/))
    flag = Struct.new(:name, :enum_name).new(matched[1], "")
    qlass.flags << flag
    parse_qlass_definition_flag(qlass, flag)
  end
end

#parse_qlass_definition_enum(qlass, enum) ⇒ Object



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
211
212
# File 'lib/qt6/rspec/qlass_file_parser.rb', line 183

def parse_qlass_definition_enum(qlass, enum)
  qmod_name = @qmod.name
  name = qlass.name
  enum_name = enum.name

  expected = "Data_Type<#{name}::#{enum_name}> rb_c#{name}#{enum_name} ="
  if line == expected
    take_next_line
  else
    raise MissingLine.new(expected, line)
  end

  expected = "// RubyQt6::#{qmod_name}::#{name}::#{enum_name}"
  if line == expected
    take_next_line
  else
    raise MissingLine.new(expected, line)
  end

  expected = "define_qenum_under<#{name}::#{enum_name}>(rb_c#{name}, \"#{enum_name}\");"
  if line == expected
    take_next_line
  else
    raise MissingLine.new(expected, line)
  end

  while line == "" || line.start_with?("define_qenum_value_under")
    take_next_line
  end
end

#parse_qlass_definition_flag(qlass, flag) ⇒ Object



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
240
241
242
243
244
# File 'lib/qt6/rspec/qlass_file_parser.rb', line 214

def parse_qlass_definition_flag(qlass, flag)
  qmod_name = @qmod.name
  name = qlass.name
  flag_name = flag.name

  expected = /Data_Type<QFlags<#{name}::(.*)>> rb_c#{name}#{flag_name} =/
  if (matched = line.match(expected))
    flag.enum_name = matched[1]
    take_next_line
  else
    raise MissingLine.new(expected, line)
  end

  expected = "// RubyQt6::#{qmod_name}::#{name}::#{flag_name}"
  if line == expected
    take_next_line
  else
    raise MissingLine.new(expected, line)
  end

  expected = "define_qflags_under<#{name}::#{flag.enum_name}>(rb_c#{name}, \"#{flag_name}\");"
  if line == expected
    take_next_line
  else
    raise MissingLine.new(expected, line)
  end

  while line == ""
    take_next_line
  end
end

#parse_qlass_definition_method(line, qlass, type) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/qt6/rspec/qlass_file_parser.rb', line 156

def parse_qlass_definition_method(line, qlass, type)
  rawline = line
  line = line.sub(".define_constructor", "")
  line = line.sub(".define_singleton_function", "")
  line = line.sub(".define_method", "")
  case line
  when /Constructor<#{qlass.name}.*>/
    rbname = "initialize"
  when /(<.*>)?\("([\w?]+)", \[\]\(/
    rbname = $2
  when /(<.*>)?\("([\w?]+)", [A-Z]/
    rbname = $2
  when /(<.*>)?\("([\w?]+)", &#{qlass.name}::([a-zA-Z0-9_]+)/
    rbname = $2
    cppname = $3
  when /(<.*>)?\("(\W+)", \[\]/
    rbname = $2
  when /(<.*>)?\("(\W+)", [A-Z]/
    rbname = $2
  when /(<.*>)?\("(\W+)", &#{qlass.name}::operator/
    rbname = $2
  else
    raise "Invalid method line: #{line}"
  end
  Struct.new(:type, :rbname, :cppname, :rawline).new(type, rbname, cppname, rawline)
end

#parse_qlass_definition_methods(qlass, type) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/qt6/rspec/qlass_file_parser.rb', line 139

def parse_qlass_definition_methods(qlass, type)
  [].tap do |methods|
    loop do
      take_next_line
      break if line.start_with?(/\/\/ (RubyQt6|Constructor|Inherits|Public|Signals|Static)/) || line == "" || line == "}"

      if line.start_with?("// .define_")
        next
      elsif line.start_with?(".define_")
        methods << parse_qlass_definition_method(line, qlass, type)
      else
        raise MissingLine.new(".define_...", line)
      end
    end
  end
end

#parse_qlass_definitionsObject



55
56
57
58
59
60
61
62
63
# File 'lib/qt6/rspec/qlass_file_parser.rb', line 55

def parse_qlass_definitions
  @qlasses.each do |qlass|
    if NESTED_QLASSES.include?(qlass.name)
      take_next_line until line == "" || line == "}"
      next
    end
    parse_qlass_definition(qlass)
  end
end

#parse_qlass_variants_declarationObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/qt6/rspec/qlass_file_parser.rb', line 32

def parse_qlass_variants_declaration
  while line == "" || line.start_with?("#include") || line == "using namespace Rice;"
    take_next_line
  end

  while (matched = line.match(/^Rice::Class rb_c(\w+)/))
    @qlasses << Struct.new(:name, :methods, :enums, :flags).new(matched[1], [], [], [])
    take_next_line
  end

  if @qlasses.length.zero?
    raise MissingLine.new("Rice::Class rb_c...", line)
  end

  until line.start_with?("void Init_q")
    take_next_line
  end

  while line == "" || line.start_with?("void Init_q") || line == "{"
    take_next_line
  end
end

#take_next_lineObject



246
247
248
# File 'lib/qt6/rspec/qlass_file_parser.rb', line 246

def take_next_line
  @lineno += 1
end