File: dsl.rb

package info (click to toggle)
ruby-bindata 2.4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 600 kB
  • sloc: ruby: 8,566; makefile: 4
file content (484 lines) | stat: -rw-r--r-- 13,761 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
58
59
60
61
62
63
64
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
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
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
211
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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
module BinData
  # Extracts args for Records and Buffers.
  #
  # Foo.new(bar: "baz) is ambiguous as to whether :bar is a value or parameter.
  #
  # BaseArgExtractor always assumes :bar is parameter.  This extractor correctly
  # identifies it as value or parameter.
  module MultiFieldArgSeparator
    def separate_args(obj_class, obj_args)
      value, parameters, parent = super(obj_class, obj_args)

      if parameters_is_value?(obj_class, value, parameters)
        value = parameters
        parameters = {}
      end

      [value, parameters, parent]
    end

    def parameters_is_value?(obj_class, value, parameters)
      if value.nil? && !parameters.empty?
        field_names_in_parameters?(obj_class, parameters)
      else
        false
      end
    end

    def field_names_in_parameters?(obj_class, parameters)
      field_names = obj_class.fields.field_names
      param_keys = parameters.keys

      !(field_names & param_keys).empty?
    end
  end

  # BinData classes that are part of the DSL must be extended by this.
  module DSLMixin
    def dsl_parser(parser_type = nil)
      @dsl_parser ||= begin
        parser_type ||= superclass.dsl_parser.parser_type
        DSLParser.new(self, parser_type)
      end
    end

    def method_missing(symbol, *args, &block) #:nodoc:
      dsl_parser.__send__(symbol, *args, &block)
    end

    # Assert object is not an array or string.
    def to_ary; nil; end
    def to_str; nil; end

    # A DSLParser parses and accumulates field definitions of the form
    #
    #   type name, params
    #
    # where:
    #   * +type+ is the under_scored name of a registered type
    #   * +name+ is the (possible optional) name of the field
    #   * +params+ is a hash containing any parameters
    #
    class DSLParser
      def initialize(the_class, parser_type)
        raise "unknown parser type #{parser_type}" unless parser_abilities[parser_type]

        @the_class      = the_class
        @parser_type    = parser_type
        @validator      = DSLFieldValidator.new(the_class, self)
        @endian         = nil
      end

      attr_reader :parser_type

      def endian(endian = nil)
        if endian
          set_endian(endian)
        elsif @endian.nil?
          set_endian(parent_attribute(:endian))
        end
        @endian
      end

      def search_prefix(*args)
        @search_prefix ||= parent_attribute(:search_prefix, []).dup

        prefix = args.collect(&:to_sym).compact
        unless prefix.empty?
          if fields?
            dsl_raise SyntaxError, "search_prefix must be called before defining fields"
          end

          @search_prefix = prefix.concat(@search_prefix)
        end

        @search_prefix
      end

      def hide(*args)
        if option?(:hidden_fields)
          @hide ||= parent_attribute(:hide, []).dup

          hidden = args.collect(&:to_sym).compact
          @hide.concat(hidden)

          @hide
        end
      end

      def fields
        @fields ||= SanitizedFields.new(hints, parent_fields)
      end

      def dsl_params
        abilities = parser_abilities[@parser_type]
        send(abilities.at(0), abilities.at(1))
      end

      def method_missing(*args, &block)
        ensure_hints
        parse_and_append_field(*args, &block)
      end

      #-------------
      private

      def parser_abilities
        @abilities ||= {
          struct:     [:to_struct_params, :struct,      [:multiple_fields, :optional_fieldnames, :hidden_fields]],
          array:      [:to_object_params, :type,        [:multiple_fields, :optional_fieldnames]],
          buffer:     [:to_object_params, :type,        [:multiple_fields, :optional_fieldnames, :hidden_fields]],
          choice:     [:to_choice_params, :choices,     [:multiple_fields, :all_or_none_fieldnames, :fieldnames_are_values]],
          delayed_io: [:to_object_params, :type,        [:multiple_fields, :optional_fieldnames, :hidden_fields]],
          primitive:  [:to_struct_params, :struct,      [:multiple_fields, :optional_fieldnames]],
          skip:       [:to_object_params, :until_valid, [:multiple_fields, :optional_fieldnames]],
        }
      end

      def option?(opt)
        parser_abilities[@parser_type].at(2).include?(opt)
      end

      def ensure_hints
        endian
        search_prefix
      end

      def hints
        { endian: endian, search_prefix: search_prefix }
      end

      def set_endian(endian)
        if endian
          if fields?
            dsl_raise SyntaxError, "endian must be called before defining fields"
          end
          if !valid_endian?(endian)
            dsl_raise ArgumentError, "unknown value for endian '#{endian}'"
          end

          if endian == :big_and_little
            DSLBigAndLittleEndianHandler.handle(@the_class)
          end

          @endian = endian
        end
      end

      def valid_endian?(endian)
        [:big, :little, :big_and_little].include?(endian)
      end

      def parent_fields
        parent_attribute(:fields)
      end

      def fields?
        defined?(@fields) && !@fields.empty?
      end

      def parse_and_append_field(*args, &block)
        parser = DSLFieldParser.new(hints, *args, &block)
        begin
          @validator.validate_field(parser.name)
          append_field(parser.type, parser.name, parser.params)
        rescue Exception => err
          dsl_raise err.class, err.message
        end
      end

      def append_field(type, name, params)
        fields.add_field(type, name, params)
      rescue BinData::UnRegisteredTypeError => err
        raise TypeError, "unknown type '#{err.message}'"
      end

      def parent_attribute(attr, default = nil)
        parent = @the_class.superclass
        parser = parent.respond_to?(:dsl_parser) ? parent.dsl_parser : nil
        if parser && parser.respond_to?(attr)
          parser.send(attr)
        else
          default
        end
      end

      def dsl_raise(exception, msg)
        backtrace = caller
        backtrace.shift while %r{bindata/dsl.rb} =~ backtrace.first

        raise exception, "#{msg} in #{@the_class}", backtrace
      end

      def to_object_params(key)
        case fields.length
        when 0
          {}
        when 1
          {key => fields[0].prototype}
        else
          {key=> [:struct, to_struct_params]}
        end
      end

      def to_choice_params(key)
        if fields.empty?
          {}
        elsif fields.all_field_names_blank?
          {key => fields.collect(&:prototype)}
        else
          choices = {}
          fields.each { |f| choices[f.name] = f.prototype }
          {key => choices}
        end
      end

      def to_struct_params(*unused)
        result = {fields: fields}
        if !endian.nil?
          result[:endian] = endian
        end
        if !search_prefix.empty?
          result[:search_prefix] = search_prefix
        end
        if option?(:hidden_fields) && !hide.empty?
          result[:hide] = hide
        end

        result
      end
    end

    # Handles the :big_and_little endian option.
    # This option creates two subclasses, each handling
    # :big or :little endian.
    class DSLBigAndLittleEndianHandler
      class << self
        def handle(bnl_class)
          make_class_abstract(bnl_class)
          create_subclasses_with_endian(bnl_class)
          override_new_in_class(bnl_class)
          delegate_field_creation(bnl_class)
          fixup_subclass_hierarchy(bnl_class)
        end

        def make_class_abstract(bnl_class)
          bnl_class.send(:unregister_self)
        end

        def create_subclasses_with_endian(bnl_class)
          instance_eval "class ::#{bnl_class}Be < ::#{bnl_class}; endian :big; end"
          instance_eval "class ::#{bnl_class}Le < ::#{bnl_class}; endian :little; end"
        end

        def override_new_in_class(bnl_class)
          endian_classes = {
            big:    class_with_endian(bnl_class, :big),
            little: class_with_endian(bnl_class, :little),
          }
          bnl_class.define_singleton_method(:new) do |*args|
            if self == bnl_class
              _, options, _ = arg_processor.separate_args(self, args)
              delegate = endian_classes[options[:endian]]
              return delegate.new(*args) if delegate
            end

            super(*args)
          end
        end

        def delegate_field_creation(bnl_class)
          endian_classes = {
            big:    class_with_endian(bnl_class, :big),
            little: class_with_endian(bnl_class, :little),
          }

          parser = bnl_class.dsl_parser
          parser.define_singleton_method(:parse_and_append_field) do |*args, &block|
            endian_classes[:big].send(*args, &block)
            endian_classes[:little].send(*args, &block)
          end
        end

        def fixup_subclass_hierarchy(bnl_class)
          parent = bnl_class.superclass
          if obj_attribute(parent, :endian) == :big_and_little
            be_subclass = class_with_endian(bnl_class, :big)
            be_parent   = class_with_endian(parent, :big)
            be_fields   = obj_attribute(be_parent, :fields)

            le_subclass = class_with_endian(bnl_class, :little)
            le_parent   = class_with_endian(parent, :little)
            le_fields   = obj_attribute(le_parent, :fields)

            be_subclass.dsl_parser.define_singleton_method(:parent_fields) do
              be_fields
            end
            le_subclass.dsl_parser.define_singleton_method(:parent_fields) do
              le_fields
            end
          end
        end

        def class_with_endian(class_name, endian)
          hints = {
            endian: endian,
            search_prefix: class_name.dsl_parser.search_prefix,
          }
          RegisteredClasses.lookup(class_name, hints)
        end

        def obj_attribute(obj, attr)
          obj.dsl_parser.send(attr)
        end
      end
    end

    # Extracts the details from a field declaration.
    class DSLFieldParser
      def initialize(hints, symbol, *args, &block)
        @hints  = hints
        @type   = symbol
        @name   = name_from_field_declaration(args)
        @params = params_from_field_declaration(args, &block)
      end

      attr_reader :type, :name, :params

      def name_from_field_declaration(args)
        name, _ = args
        if name == "" || name.is_a?(Hash)
          nil
        else
          name
        end
      end

      def params_from_field_declaration(args, &block)
        params = params_from_args(args)

        if block_given?
          params.merge(params_from_block(&block))
        else
          params
        end
      end

      def params_from_args(args)
        name, params = args
        params = name if name.is_a?(Hash)

        params || {}
      end

      def params_from_block(&block)
        bindata_classes = {
          array:      BinData::Array,
          buffer:     BinData::Buffer,
          choice:     BinData::Choice,
          delayed_io: BinData::DelayedIO,
          skip:       BinData::Skip,
          struct:     BinData::Struct,
        }

        if bindata_classes.include?(@type)
          parser = DSLParser.new(bindata_classes[@type], @type)
          parser.endian(@hints[:endian])
          parser.search_prefix(*@hints[:search_prefix])
          parser.instance_eval(&block)

          parser.dsl_params
        else
          {}
        end
      end
    end

    # Validates a field defined in a DSLMixin.
    class DSLFieldValidator
      def initialize(the_class, parser)
        @the_class = the_class
        @dsl_parser = parser
      end

      def validate_field(name)
        if must_not_have_a_name_failed?(name)
          raise SyntaxError, "field must not have a name"
        end

        if all_or_none_names_failed?(name)
          raise SyntaxError, "fields must either all have names, or none must have names"
        end

        if must_have_a_name_failed?(name)
          raise SyntaxError, "field must have a name"
        end

        ensure_valid_name(name)
      end

      def ensure_valid_name(name)
        if name && !option?(:fieldnames_are_values)
          if malformed_name?(name)
            raise SyntaxError, "field '#{name}' is an illegal fieldname"
          end

          if duplicate_name?(name)
            raise SyntaxError, "duplicate field '#{name}'"
          end

          if name_shadows_method?(name)
            raise SyntaxError, "field '#{name}' shadows an existing method"
          end

          if name_is_reserved?(name)
            raise SyntaxError, "field '#{name}' is a reserved name"
          end
        end
      end

      def must_not_have_a_name_failed?(name)
        option?(:no_fieldnames) && !name.nil?
      end

      def must_have_a_name_failed?(name)
        option?(:mandatory_fieldnames) && name.nil?
      end

      def all_or_none_names_failed?(name)
        if option?(:all_or_none_fieldnames) && !fields.empty?
          all_names_blank = fields.all_field_names_blank?
          no_names_blank = fields.no_field_names_blank?

          (!name.nil? && all_names_blank) || (name.nil? && no_names_blank)
        else
          false
        end
      end

      def malformed_name?(name)
        /^[a-z_]\w*$/ !~ name.to_s
      end

      def duplicate_name?(name)
        fields.field_name?(name)
      end

      def name_shadows_method?(name)
        @the_class.method_defined?(name)
      end

      def name_is_reserved?(name)
        BinData::Struct::RESERVED.include?(name.to_sym)
      end

      def fields
        @dsl_parser.fields
      end

      def option?(opt)
        @dsl_parser.send(:option?, opt)
      end
    end
  end
end