Class: IcAgent::Ast::Assembler
- Inherits:
-
Object
- Object
- IcAgent::Ast::Assembler
- Defined in:
- lib/ic_agent/ast/assembler.rb
Constant Summary collapse
- TYPE_MAPPING =
{}
Class Method Summary collapse
- .build_blob ⇒ Object
- .build_opt(child_type, key_types = {}) ⇒ Object
- .build_record(child_hash, multi_types = {}, key_types = {}) ⇒ Object
- .build_single_type(child_type) ⇒ Object
- .build_type(type_str, key_types = {}, multi_types = {}) ⇒ Object
- .build_variant(child_hash, multi_types = {}, key_types = {}) ⇒ Object
- .build_vec(child_type, key_types = {}) ⇒ Object
- .get_child_code(item_str, index_str) ⇒ Object
- .get_opt_code(item_str) ⇒ Object
- .get_params_refer_values(type_str) ⇒ Object
- .get_record_content(record_str) ⇒ Object
- .get_record_key_value(item_str, index_str, key_index = 0) ⇒ Object
- .get_variant_content(variant_str) ⇒ Object
- .recover_type(type_str, multi_types) ⇒ Object
- .replace_last_occurrence(string, pattern, replacement) ⇒ Object
- .replace_multi_type(type_str) ⇒ Object
Class Method Details
.build_blob ⇒ Object
12 13 14 |
# File 'lib/ic_agent/ast/assembler.rb', line 12 def self.build_blob IcAgent::Candid::BaseTypes.vec(IcAgent::Candid::BaseTypes.nat8) end |
.build_opt(child_type, key_types = {}) ⇒ Object
16 17 18 19 |
# File 'lib/ic_agent/ast/assembler.rb', line 16 def self.build_opt(child_type, key_types = {}) child_type = key_types[child_type].nil? ? build_type(child_type, key_types) : key_types[child_type] IcAgent::Candid::BaseTypes.opt(child_type) end |
.build_record(child_hash, multi_types = {}, key_types = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ic_agent/ast/assembler.rb', line 26 def self.build_record(child_hash, multi_types = {}, key_types = {}) child_types = {} child_hash.each_key do |key| if multi_types[child_hash[key].strip] multi_type = build_type(multi_types[child_hash[key].strip], key_types, multi_types) child_types[key] = multi_type elsif key_types[child_hash[key].strip] child_types[key] = key_types[child_hash[key].strip] else child_types[key] = build_type(child_hash[key], key_types, multi_types) end end IcAgent::Candid::BaseTypes.record(child_types) end |
.build_single_type(child_type) ⇒ Object
8 9 10 |
# File 'lib/ic_agent/ast/assembler.rb', line 8 def self.build_single_type(child_type) IcAgent::Candid::BaseTypes.send(child_type) end |
.build_type(type_str, key_types = {}, multi_types = {}) ⇒ Object
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 |
# File 'lib/ic_agent/ast/assembler.rb', line 56 def self.build_type(type_str, key_types = {}, multi_types = {}) opt_code = get_opt_code(type_str) if IcAgent::Candid::SINGLE_TYPES.include? opt_code build_single_type(opt_code) elsif opt_code == 'blob' build_blob elsif opt_code == 'opt' type_str = recover_type(type_str, multi_types) child_code = get_child_code(type_str, ' ') build_opt(child_code, key_types) elsif opt_code == 'vec' type_str = recover_type(type_str, multi_types) child_code = get_child_code(type_str, ' ') build_vec(child_code, key_types) elsif opt_code == 'record' child_code = get_record_content(type_str) pure_child_code, multi_types = replace_multi_type(child_code) child_hash = {} key_index = 0 pure_child_code.split(';').each do |item| item_key, item_value = get_record_key_value(item, ' : ', key_index) child_hash[item_key] = item_value key_index += 1 end build_record(child_hash, multi_types, key_types) elsif opt_code == 'variant' child_code = get_variant_content(type_str) pure_child_code, multi_types = replace_multi_type(child_code) child_hash = {} pure_child_code.split(';').each do |item| item_arr = item.strip.split(' : ') child_hash[item_arr[0]] = (item_arr.size > 1 ? item_arr[1] : 'null') end build_variant(child_hash, multi_types, key_types) end end |
.build_variant(child_hash, multi_types = {}, key_types = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ic_agent/ast/assembler.rb', line 41 def self.build_variant(child_hash, multi_types = {}, key_types = {}) child_types = {} child_hash.each_key do |key| if multi_types[child_hash[key].strip] multi_type = build_type(multi_types[child_hash[key].strip], multi_types) child_types[key] = multi_type elsif key_types[child_hash[key].strip] child_types[key] = key_types[child_hash[key].strip] else child_types[key] = build_type(child_hash[key], key_types, multi_types) end end IcAgent::Candid::BaseTypes.variant(child_types) end |
.build_vec(child_type, key_types = {}) ⇒ Object
21 22 23 24 |
# File 'lib/ic_agent/ast/assembler.rb', line 21 def self.build_vec(child_type, key_types = {}) child_type = key_types[child_type].nil? ? build_type(child_type, key_types) : key_types[child_type] IcAgent::Candid::BaseTypes.vec(child_type) end |
.get_child_code(item_str, index_str) ⇒ Object
131 132 133 134 |
# File 'lib/ic_agent/ast/assembler.rb', line 131 def self.get_child_code(item_str, index_str) first_index = item_str.index(index_str) item_str[(first_index + index_str.size)..].strip end |
.get_opt_code(item_str) ⇒ Object
126 127 128 129 |
# File 'lib/ic_agent/ast/assembler.rb', line 126 def self.get_opt_code(item_str) opt_code = item_str.strip opt_code.split(' ')[0] end |
.get_params_refer_values(type_str) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/ic_agent/ast/assembler.rb', line 155 def self.get_params_refer_values(type_str) parser = IcAgent::Ast::StatementParser.new parser.parse(type_str) refer_type = parser.source_tree.content[:refer_type] refer_type end |
.get_record_content(record_str) ⇒ Object
102 103 104 105 106 |
# File 'lib/ic_agent/ast/assembler.rb', line 102 def self.get_record_content(record_str) record_str = record_str.sub('record', '').sub('{', '') record_str = replace_last_occurrence(record_str, '}', '') record_str.strip end |
.get_record_key_value(item_str, index_str, key_index = 0) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ic_agent/ast/assembler.rb', line 114 def self.get_record_key_value(item_str, index_str, key_index = 0) first_index = item_str.index(index_str) if first_index key = item_str[0..first_index].strip value = item_str[(first_index + index_str.size)..].strip else key = key_index.to_s value = item_str.strip end return key, value end |
.get_variant_content(variant_str) ⇒ Object
108 109 110 111 112 |
# File 'lib/ic_agent/ast/assembler.rb', line 108 def self.get_variant_content(variant_str) variant_str = variant_str.sub('variant', '').sub('{', '') variant_str = replace_last_occurrence(variant_str, '}', '') variant_str.strip end |
.recover_type(type_str, multi_types) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/ic_agent/ast/assembler.rb', line 162 def self.recover_type(type_str, multi_types) multi_types.each_key do |key| type_str = type_str.gsub(key, multi_types[key]) end type_str end |
.replace_last_occurrence(string, pattern, replacement) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/ic_agent/ast/assembler.rb', line 94 def self.replace_last_occurrence(string, pattern, replacement) last_index = string.rindex(pattern) return string unless last_index string[last_index..-1] = replacement string end |
.replace_multi_type(type_str) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/ic_agent/ast/assembler.rb', line 136 def self.replace_multi_type(type_str) replaced_hash = {} modified_str = type_str.gsub(/record\s*{[^{}]*}/) do |match| rad_id = rand(100000..999999) type_name = "record_#{rad_id}" replaced_hash[type_name] = match type_name end modified_str = modified_str.gsub(/variant\s*{[^{}]*}/) do |match| rad_id = rand(100000..999999) type_name = "variant_#{rad_id}" replaced_hash[type_name] = match type_name end return modified_str, replaced_hash end |