• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14require 'ostruct'
15require 'delegate'
16
17class String
18  def snakecase
19    gsub(/::/, '/')
20      .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
21      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
22      .tr('-', '_')
23      .downcase
24  end
25end
26
27class Option < SimpleDelegator
28  def initialize(data)
29    super
30    if has_sub_options
31      raise "Compound option should not have `type` field, it is always bool" if respond_to?(:type)
32      raise "Compound option should not have `default` field, it is always `false``" if respond_to?(:default)
33      self[:type] = 'bool'
34      self[:default] = 'false'
35    end
36  end
37
38  def fix_references(options)
39    if respond_to?(:possible_values) && possible_values.is_a?(String)
40      raise "Wrong reference syntax: #{possible_values}" if possible_values[0] != '$'
41      p = possible_values[1..-1]
42      target = options.find {|x| x.name == p }
43      raise "Invalid reference #{possible_values}" if target.nil?
44      self["possible_values"] = target["possible_values"]
45    end
46  end
47
48  def field_name
49    name.tr('-', '_').tr('.', '_') + '_'
50  end
51
52  def camel_name
53    n = name.split(Regexp.union(['-', '.'])).map(&:capitalize).join
54    sub_option? ? (self.parent.camel_name + n) : n
55  end
56
57  def getter_name
58    type == 'bool' ? 'Is' + camel_name : 'Get' + camel_name
59  end
60
61  def setter_name
62    'Set' + camel_name
63  end
64
65  def sub_option?
66    respond_to?(:parent)
67  end
68
69  def has_sub_options
70    respond_to?(:sub_options)
71  end
72
73  def deprecated?
74    respond_to?(:deprecated) && deprecated
75  end
76
77  def lang_specific?
78    respond_to?(:lang_specific)
79  end
80
81  def has_lang_suboptions?
82    respond_to?(:lang)
83  end
84
85  def lang_field_name(lang)
86    "#{lang}.#{name}".tr('-', '_').tr('.', '_') + '_'
87  end
88
89  def lang_camel_name(lang)
90    n = "#{lang}.#{name}".split(Regexp.union(['-', '.'])).map(&:capitalize).join
91    sub_option? ? (self.parent.camel_name + n) : n
92  end
93
94  def default_value
95    return default_constant_name if need_default_constant
96    return '{' + default.map { |e| expand_string(e) }.join(', ') + '}' if type == 'arg_list_t'
97    return expand_string(default) if type == 'std::string'
98    default
99  end
100
101  def full_description
102    full_desc = description
103    full_desc.prepend("[DEPRECATED] ") if deprecated?
104    if defined? possible_values
105      full_desc += '. Possible values: ' + possible_values.inspect
106    end
107    Common::to_raw(full_desc + '. Default: ' + default.inspect)
108  end
109
110  def expand_string(s)
111    @expansion_map ||= {
112      '$ORIGIN' => 'exe_dir_'
113    }
114    @expansion_map.each do |k, v|
115      ret = ""
116      if s.include?(k)
117        split = s.split(k);
118        for i in 1..split.length() - 1
119          ret += v + ' + ' + Common::to_raw(split[i])+ ' + '
120        end
121        return ret.delete_suffix(' + ')
122      end
123    end
124    Common::to_raw(s)
125  end
126
127  def need_default_constant
128    type == 'int' || type == 'double' || type == 'uint32_t' || type == 'uint64_t'
129  end
130
131  def default_constant_name
132    name.tr('-', '_').tr('.', '_').upcase + '_DEFAULT'
133  end
134end
135
136class Event < SimpleDelegator
137  def method_name
138    'Event' + name.split('-').map(&:capitalize).join
139  end
140
141  def args_list
142    args = ''
143    delim = ''
144    fields.each do |field|
145      args.concat(delim, field.type, ' ', field.name)
146      delim = ', '
147    end
148    return args
149  end
150
151  def print_line
152    qoute = '"'
153    line = 'events_file'
154    delim = ' << '
155    fields.each do |field|
156      line.concat(delim, qoute, field.name, qoute)
157      delim = ' << ":" << '
158      line.concat(delim, field.name)
159      delim = ' << "," << '
160    end
161    return line
162  end
163
164end
165
166module Common
167  module_function
168
169  def create_sub_options(option, options)
170    return unless option.has_sub_options
171    raise "Only boolean option can have sub options: #{option.name}" unless option.type == 'bool'
172    sub_options = []
173    option.sub_options.each_with_object(sub_options) do |sub_option, sub_options|
174      sub_option[:parent] = option
175      sub_options << Option.new(sub_option)
176    end
177    options.concat sub_options
178    option[:sub_options] = sub_options
179  end
180
181  def options
182    @options ||= @data.options.each_with_object([]) do |option, options|
183      option_hash = option.to_h
184      if option_hash.include?(:lang)
185        lang_spec_option = option_hash.clone
186        lang_spec_option.delete(:lang)
187        lang_spec_option[:lang_specific] = true
188        option.lang.each do |lang|
189          lang_spec_option[:description] = "#{option.description}. Only for #{lang}"
190          lang_spec_option[:name] = "#{lang}.#{option.name}"
191          options << Option.new(OpenStruct.new(lang_spec_option))
192          create_sub_options(options[-1], options)
193        end
194        main_option = option_hash.clone
195        main_option[:name] = "#{option.name}"
196        options << Option.new(OpenStruct.new(main_option))
197        create_sub_options(options[-1], options)
198      else
199        options << Option.new(option)
200        create_sub_options(options[-1], options)
201      end
202      options.last.fix_references(@data.options)
203    end
204    @options
205  end
206
207  def events
208    @data.events.map do |op|
209      Event.new(op)
210    end
211  end
212
213  def module
214    @data.module
215  end
216
217  def to_raw(s)
218    'R"(' + s + ')"'
219  end
220
221  def wrap_data(data)
222    @data = data
223  end
224end
225
226def Gen.on_require(data)
227  Common.wrap_data(data)
228end
229