ic_agent 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f7c8e2c2b92f692a20ba38898f78dd76a854367369a905e8553c162c3a0238c
4
- data.tar.gz: 51885b2b8966bae6a23b4dc5e775c7fd3ee864b4452cafa2213e54aa1fdddeae
3
+ metadata.gz: 8f6dd5be4d1dbd760c09b92da6efa53590adf43cad43c6ef495ccb704deccf1a
4
+ data.tar.gz: d28f144bd132dbb0dca0a13f7f12f8749595b1055f70801ffa8dc117cc113bb0
5
5
  SHA512:
6
- metadata.gz: 3369b673094b4b7416f1a5125f440d5b5ad4fd9b8878d58564165b50bbd33b503e0653220fb92ac617fdc25890cb20efe2429c3f7f64340f8305134162f58a95
7
- data.tar.gz: 0e8e47383437dfd4a1de1d05ac8975faed0fb1cff4e56ea9c2006450fd7036dbff578cb4bbbbea2f635e4a41dbf05e0f25ce178a8978e5f6bdd2e79a7d644d22
6
+ metadata.gz: b332d5369452f62130204a09c92d24a96029a054c7e02f92ddbaed137167b7f7eccf6e7458b76e6bf4a56da43a540ae937435dae5bd5a2329f680e100f80d53f
7
+ data.tar.gz: d7ff9b8954dc25684ef50da692aeea12e000b7e146a40c803aec70bfb6ffc4e5f0348984af230fd55cb71ab7b69f1c91634d9fdd7b7f47d471d9b6efac321098
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ic_agent (0.1.1)
4
+ ic_agent (0.1.2)
5
5
  base32 (~> 0.3.4)
6
6
  bitcoin-ruby (~> 0.0.20)
7
7
  cbor (~> 0.5.9.6)
@@ -153,32 +153,10 @@ module IcAgent
153
153
  end
154
154
 
155
155
  def self.get_params_refer_values(type_str)
156
- pure_child_code, replaced_hash = replace_multi_type(type_str)
157
- key_index = 0
158
- value_arr = []
159
- pure_child_code.split(';').each do |item|
160
- _, item_value = get_record_key_value(item, ' : ', key_index)
161
- value_arr << item_value.split(' ')
162
- key_index += 1
163
- end
164
-
165
- replaced_hash.each_key do |key|
166
- item_arr = replaced_hash[key].strip.split(';')
167
- item_arr.each do |item|
168
- multi_item_arr = item.strip.split(':')
169
- if multi_item_arr.size > 1
170
- item_value_arr = multi_item_arr[1].strip.split(' ').collect { |v| v.strip.gsub(';', '') }
171
- item_value_arr.delete('{')
172
- item_value_arr.delete('}')
173
- item_value_arr.delete('{}')
174
- value_arr << item_value_arr
175
- else
176
- value_arr << multi_item_arr[1].strip
177
- end
178
- end
179
- value_arr.delete(key)
180
- end
181
- value_arr.flatten.uniq - IcAgent::Candid::ALL_TYPES - replaced_hash.keys
156
+ parser = IcAgent::Ast::StatementParser.new
157
+ parser.parse(type_str)
158
+ refer_type = parser.source_tree.content[:refer_type]
159
+ refer_type
182
160
  end
183
161
 
184
162
  def self.recover_type(type_str, multi_types)
@@ -6,7 +6,7 @@ grammar DIDGrammar
6
6
 
7
7
  rule base_type
8
8
  'bool' / 'text' / 'null' / 'reserved' / 'empty' / 'principal' / 'nat8' / 'nat16' / 'nat32' / 'nat64' / 'int8' / 'int16' / 'int32' / 'int64' / 'float32' / 'float64' /
9
- 'opt' / 'vec' / 'record' / 'variant' / 'service' / 'func' / 'nat' / 'int' / blob
9
+ 'opt' / 'vec' / 'record' / 'variant' / 'service' / 'func' / 'nat' / 'int' / 'blob'
10
10
  end
11
11
 
12
12
  rule base_type_single
@@ -90,15 +90,19 @@ grammar DIDGrammar
90
90
  end
91
91
 
92
92
  rule service_item
93
- space? service_method_name space? ":" space? "(" service_method_params? ")" space? "->" space? "(" service_method_return_type ")" space? method_query? ';' end_of_line? <IcAgent::Ast::Nodes::IcServiceItem>
93
+ space? service_method_name space? ":" space? "(" service_method_params? ")" space? "->" space? "(" service_method_return_type? ")" space? method_query? ';' end_of_line? <IcAgent::Ast::Nodes::IcServiceItem>
94
94
  end
95
95
 
96
96
  rule service_method_params
97
- param_element (", " param_element)* <IcAgent::Ast::Nodes::IcServiceMethodParams>
97
+ source_coding (", " source_coding)* <IcAgent::Ast::Nodes::IcServiceMethodParams>
98
98
  end
99
99
 
100
100
  rule service_method_return_type
101
- [a-zA-Z0-9_{}: ]* <IcAgent::Ast::Nodes::IcServiceMethodReturn>
101
+ source_coding (", " source_coding)* <IcAgent::Ast::Nodes::IcServiceMethodReturn>
102
+ end
103
+
104
+ rule source_coding
105
+ [a-zA-Z0-9_{}:;\n ]*
102
106
  end
103
107
 
104
108
  rule method_query
@@ -0,0 +1,159 @@
1
+ grammar DIDGrammar
2
+
3
+ rule body
4
+ ( type_declaration / service_declaration / comment / end_of_line )* <IcAgent::Ast::Nodes::DIDFile>
5
+ end
6
+
7
+ rule base_type
8
+ 'bool' / 'text' / 'null' / 'reserved' / 'empty' / 'principal' / 'nat8' / 'nat16' / 'nat32' / 'nat64' / 'int8' / 'int16' / 'int32' / 'int64' / 'float32' / 'float64' /
9
+ 'opt' / 'vec' / 'record' / 'variant' / 'service' / 'func' / 'nat' / 'int' / blob
10
+ end
11
+
12
+ rule base_type_single
13
+ 'bool' / 'text' / 'null' / 'reserved' / 'empty' / 'principal' / 'nat8' / 'nat16' / 'nat32' / 'nat64' / 'int8' / 'int16' / 'int32' / 'int64' / 'float32' / 'float64' / 'nat' / 'int' / 'blob'
14
+ end
15
+
16
+ rule base_type_base
17
+ base_type_single+ <IcAgent::Ast::Nodes::BaseTypeSingle>
18
+ end
19
+
20
+ rule base_type_code
21
+ [a-zA-Z0-9_]+ <IcAgent::Ast::Nodes::BaseTypeOther>
22
+ end
23
+
24
+ rule base_type_key
25
+ [a-zA-Z0-9_]+ <IcAgent::Ast::Nodes::BaseTypeKey>
26
+ end
27
+
28
+ rule base_type_vec
29
+ 'vec' space ic_all_type <IcAgent::Ast::Nodes::BaseTypeVec>
30
+ end
31
+
32
+ rule base_type_opt
33
+ 'opt' space ic_all_type <IcAgent::Ast::Nodes::BaseTypeOpt>
34
+ end
35
+
36
+ rule base_type_record
37
+ 'record' space '{' space_and_line ic_base_content space_and_line '}' <IcAgent::Ast::Nodes::BaseTypeRecord>
38
+ end
39
+
40
+ rule base_type_record_null
41
+ 'record {}' <IcAgent::Ast::Nodes::BaseTypeRecord>
42
+ end
43
+
44
+ rule base_type_variant
45
+ 'variant' space '{' space_and_line ic_base_content space_and_line '}' <IcAgent::Ast::Nodes::BaseTypeVariant>
46
+ end
47
+
48
+ rule base_type_func
49
+ 'func' space? "(" service_method_params? ")" space? "->" space? "(" service_method_return_type ")" space? method_query? <IcAgent::Ast::Nodes::BaseTypeFunc>
50
+ end
51
+
52
+ rule ic_all_type
53
+ base_type_base / base_type_vec / base_type_record / base_type_record_null / base_type_variant / base_type_opt / base_type_func / base_type_code
54
+ end
55
+
56
+ rule ic_base_content
57
+ (ic_base_type)+ <IcAgent::Ast::Nodes::BaseTypeContent>
58
+ end
59
+
60
+ rule ic_base_type
61
+ space? base_type_key space? (':' space ic_all_type)* optional_semicolon <IcAgent::Ast::Nodes::BaseTypeChild>
62
+ end
63
+
64
+ rule type_declaration
65
+ 'type' space_and_line type_name space_and_line '=' space_and_line ic_all_type ';' end_of_line <IcAgent::Ast::Nodes::TypeDeclaration>
66
+ end
67
+
68
+ rule type_name
69
+ [a-zA-Z0-9_]+ <IcAgent::Ast::Nodes::TypeName>
70
+ end
71
+
72
+ rule comment
73
+ space? '//' space? rest_of_line <IcAgent::Ast::Nodes::Comment>
74
+ end
75
+
76
+ rule service_declaration
77
+ "service" space ":" space service_name* "{" end_of_line service_methods "}" <IcAgent::Ast::Nodes::Service>
78
+ end
79
+
80
+ rule service_name
81
+ "(" ic_service_name ")" space? "->" space?
82
+ end
83
+
84
+ rule ic_service_name
85
+ [a-zA-Z0-9_]* <IcAgent::Ast::Nodes::IcServiceName>
86
+ end
87
+
88
+ rule service_methods
89
+ (service_item)+ <IcAgent::Ast::Nodes::IcServiceMethods>
90
+ end
91
+
92
+ rule service_item
93
+ space? service_method_name space? ":" space? "(" service_method_params? ")" space? "->" space? "(" service_method_return_type ")" space? method_query? ';' end_of_line? <IcAgent::Ast::Nodes::IcServiceItem>
94
+ end
95
+
96
+ rule service_method_params
97
+ param_element (", " param_element)* <IcAgent::Ast::Nodes::IcServiceMethodParams>
98
+ end
99
+
100
+ rule service_method_return_type
101
+ [a-zA-Z0-9_{}: ]* <IcAgent::Ast::Nodes::IcServiceMethodReturn>
102
+ end
103
+
104
+ rule method_query
105
+ [a-zA-Z0-9_]* <IcAgent::Ast::Nodes::IcServiceMethodQuery>
106
+ end
107
+
108
+ rule words
109
+ [a-zA-Z_] [a-zA-Z0-9_]*
110
+ end
111
+
112
+ rule param_element
113
+ [a-zA-Z0-9_ ]+
114
+ end
115
+
116
+ rule service_method_name
117
+ [a-zA-Z0-9_]* <IcAgent::Ast::Nodes::IcServiceMethodName>
118
+ end
119
+
120
+ rule string_without_spaces
121
+ [\S]* <IcAgent::Ast::Nodes::StringLiteral>
122
+ end
123
+
124
+ rule rest_of_line
125
+ [^\n]* <IcAgent::Ast::Nodes::StringLiteral>
126
+ end
127
+
128
+ rule end_of_line
129
+ [\n]+
130
+ end
131
+
132
+ rule block_record
133
+ '{}'
134
+ end
135
+
136
+ rule has_end_of_line
137
+ [\n]*
138
+ end
139
+
140
+ rule splite_code
141
+ ' : '
142
+ end
143
+
144
+ rule space_and_line
145
+ [\s\n]*
146
+ end
147
+
148
+ rule space
149
+ [\s]+
150
+ end
151
+
152
+ rule start_space
153
+ [^\s]+
154
+ end
155
+
156
+ rule optional_semicolon
157
+ [;]*
158
+ end
159
+ end
@@ -0,0 +1,118 @@
1
+ grammar TypeGrammar
2
+ rule statement_block
3
+ ic_all_type optional_semicolon <IcAgent::Ast::Nodes::StatementBlock>
4
+ end
5
+
6
+ rule ic_all_type
7
+ (ic_type_def)+ <IcAgent::Ast::Nodes::IcTypeDef>
8
+ end
9
+
10
+ rule ic_type_def
11
+ base_type_base / base_type_vec / base_type_record / base_type_record_null / base_type_variant / base_type_opt / base_type_func / base_type_code
12
+ end
13
+
14
+ rule record_statement_content
15
+ (ic_base_type_pair / ic_base_type_value)* <IcAgent::Ast::Nodes::StatementContent>
16
+ end
17
+
18
+ rule variant_statement_content
19
+ (ic_base_type_pair / ic_base_type_key)* <IcAgent::Ast::Nodes::StatementContent>
20
+ end
21
+
22
+ rule base_type
23
+ 'bool' / 'text' / 'null' / 'reserved' / 'empty' / 'principal' / 'nat8' / 'nat16' / 'nat32' / 'nat64' / 'int8' / 'int16' / 'int32' / 'int64' / 'float32' / 'float64' /
24
+ 'opt' / 'vec' / 'record' / 'variant' / 'service' / 'func' / 'nat' / 'int' / 'blob'
25
+ end
26
+
27
+ rule base_type_single
28
+ 'bool' / 'text' / 'null' / 'reserved' / 'empty' / 'principal' / 'nat8' / 'nat16' / 'nat32' / 'nat64' / 'int8' / 'int16' / 'int32' / 'int64' / 'float32' / 'float64' / 'nat' / 'int' / 'blob'
29
+ end
30
+
31
+ rule ic_base_type_pair
32
+ space? base_type_key space? ':' space? base_type_value ';'? <IcAgent::Ast::Nodes::IcBaseTypeChild>
33
+ end
34
+
35
+ rule ic_base_type_key
36
+ space? base_type_key space? ';'? <IcAgent::Ast::Nodes::IcBaseTypeChild>
37
+ end
38
+
39
+ rule ic_base_type_value
40
+ base_type_value space? ';'? <IcAgent::Ast::Nodes::IcBaseTypeChild>
41
+ end
42
+
43
+ rule base_type_base
44
+ base_type_single+ <IcAgent::Ast::Nodes::IcBaseTypeSingle>
45
+ end
46
+
47
+ rule base_type_code
48
+ [a-zA-Z0-9_]+ <IcAgent::Ast::Nodes::IcBaseTypeOther>
49
+ end
50
+
51
+ rule base_type_key
52
+ [a-zA-Z0-9_]+ <IcAgent::Ast::Nodes::IcBaseTypeKey>
53
+ end
54
+
55
+ rule base_type_value
56
+ space? ic_all_type <IcAgent::Ast::Nodes::IcBaseTypeValue>
57
+ end
58
+
59
+ rule base_type_vec
60
+ 'vec' space ic_all_type <IcAgent::Ast::Nodes::IcBaseTypeVec>
61
+ end
62
+
63
+ rule base_type_opt
64
+ 'opt' space ic_all_type <IcAgent::Ast::Nodes::IcBaseTypeOpt>
65
+ end
66
+
67
+ rule base_type_record
68
+ 'record' space '{' space_and_line? record_statement_content space_and_line? '}' <IcAgent::Ast::Nodes::IcBaseTypeRecord>
69
+ end
70
+
71
+ rule base_type_record_null
72
+ 'record {}' <IcAgent::Ast::Nodes::IcBaseTypeRecord>
73
+ end
74
+
75
+ rule base_type_variant
76
+ 'variant' space '{' space_and_line variant_statement_content space_and_line '}' <IcAgent::Ast::Nodes::IcBaseTypeVariant>
77
+ end
78
+
79
+ rule base_type_func
80
+ 'func' space? "(" service_method_params? ")" space? "->" space? "(" service_method_return_type ")" space? method_query? <IcAgent::Ast::Nodes::IcBaseTypeFunc>
81
+ end
82
+
83
+ rule service_method_params
84
+ source_coding / refer_coding
85
+ end
86
+
87
+ rule service_method_return_type
88
+ [a-zA-Z0-9_{}:;\n ]* <IcAgent::Ast::Nodes::IcServiceMethodReturn>
89
+ end
90
+
91
+ rule refer_coding
92
+ param_element (", " param_element)* <IcAgent::Ast::Nodes::IcServiceMethodParams>
93
+ end
94
+
95
+ rule source_coding
96
+ [a-zA-Z0-9_{}:;\n ]* <IcAgent::Ast::Nodes::IcServiceMethodParams>
97
+ end
98
+
99
+ rule param_element
100
+ [a-zA-Z0-9_ ]+
101
+ end
102
+
103
+ rule method_query
104
+ [a-zA-Z0-9]* <IcAgent::Ast::Nodes::IcServiceMethodQuery>
105
+ end
106
+
107
+ rule space_and_line
108
+ [\s\n]*
109
+ end
110
+
111
+ rule space
112
+ [\s]+
113
+ end
114
+
115
+ rule optional_semicolon
116
+ [;]*
117
+ end
118
+ end
@@ -19,6 +19,10 @@ module IcAgent
19
19
  def elements_to_s
20
20
  elements.map(&:to_s).join("\n")
21
21
  end
22
+
23
+ def source_content
24
+ self.text_value.strip
25
+ end
22
26
  end
23
27
 
24
28
  class Instruction < NamedNode
@@ -57,11 +61,11 @@ module IcAgent
57
61
  end
58
62
 
59
63
  def type_param_name
60
- elements[0].text_value
64
+ elements[0].source_content
61
65
  end
62
66
 
63
67
  def type_param_content
64
- elements[1].text_value.gsub("\n", '').gsub(';}', '}')
68
+ elements[1].source_content.gsub("\n", '').gsub(';}', '}')
65
69
  end
66
70
 
67
71
  def type_root_opt_code
@@ -84,59 +88,12 @@ module IcAgent
84
88
  names
85
89
  end
86
90
 
87
- def type_child_item_values
88
- values = []
89
- type_child_items.each do |ele|
90
-
91
- # get multi type value
92
- replaced_hash = {}
93
- modified_str = ele.text_value.gsub("\n", '').gsub(/record\s*{[^{}]*}/) do |match|
94
- rad_id = rand(100000..999999)
95
- type_name = "record_#{rad_id}"
96
- replaced_hash[type_name] = match
97
- type_name
98
- end
99
- modified_str = modified_str.gsub(/variant\s*{[^{}]*}/) do |match|
100
- rad_id = rand(100000..999999)
101
- type_name = "variant_#{rad_id}"
102
- replaced_hash[type_name] = match
103
- type_name
104
- end
105
- replaced_hash.each_key do |key|
106
- item_arr = replaced_hash[key].strip.split(';')
107
- item_arr.each do |item|
108
- multi_item_arr = item.strip.split(':')
109
- if multi_item_arr.size > 1
110
- item_value_arr = multi_item_arr[1].strip.split(' ').collect { |v| v.strip.gsub(';', '') }
111
- item_value_arr.delete('{')
112
- item_value_arr.delete('}')
113
- item_value_arr.delete('{}')
114
- values << item_value_arr
115
- else
116
- values << []
117
- end
118
- end
119
- end
120
-
121
- # get root type value
122
- item_arr = modified_str.strip.split(':')
123
- if item_arr.size > 1
124
- item_value_arr = item_arr[1].strip.split(' ').collect { |v| v.strip.gsub(';', '') }
125
- item_value_arr.delete('{')
126
- item_value_arr.delete('}')
127
- item_value_arr.delete('{}')
128
- item_value_arr -= replaced_hash.keys
129
- values << item_value_arr
130
- else
131
- values << []
132
- end
133
- end
134
- values
135
- end
136
-
137
- def type_child_refer_items
138
- child_args = type_child_item_values.flatten - IcAgent::Candid::ALL_TYPES
139
- child_args.uniq
91
+ def type_refer_items
92
+ source_string = self.type_param_content
93
+ parser = IcAgent::Ast::StatementParser.new
94
+ parser.parse(source_string)
95
+ refer_type = parser.source_tree.content[:refer_type]
96
+ refer_type
140
97
  end
141
98
 
142
99
  def to_s
@@ -148,7 +105,7 @@ module IcAgent
148
105
  'type_param_name' => type_param_name,
149
106
  'type_root_opt_code' => type_root_opt_code,
150
107
  'type_child_item_keys' => type_child_item_keys,
151
- 'type_child_item_values' => type_child_item_values
108
+ 'type_child_item_values' => type_refer_items
152
109
  }
153
110
  end
154
111
  end
@@ -349,7 +306,7 @@ module IcAgent
349
306
  def to_obj
350
307
  obj = {}
351
308
  elements.each do |element|
352
- obj[element.title.to_s] = element.text_value
309
+ obj[element.title.to_s] = element.text_value.gsub("\n", '')
353
310
  end
354
311
  obj
355
312
  end
@@ -404,6 +361,16 @@ module IcAgent
404
361
  elements_to_s
405
362
  end
406
363
  end
364
+
365
+ class BaseTypeContent < NamedNode
366
+ def title
367
+ :ic_service_method_query
368
+ end
369
+
370
+ def to_s
371
+ elements_to_s
372
+ end
373
+ end
407
374
  end
408
375
  end
409
376
  end
@@ -0,0 +1,224 @@
1
+ require 'treetop'
2
+
3
+ module IcAgent
4
+ module Ast
5
+ module Nodes
6
+ class StatementNode < Treetop::Runtime::SyntaxNode
7
+ attr_accessor :child_count, :depth
8
+
9
+ def title
10
+ :named_node
11
+ end
12
+
13
+ def to_array
14
+ [title] + elements.map(&:to_array)
15
+ end
16
+
17
+ def to_s
18
+ "#{title.to_s.upcase} #{elements_to_s}"
19
+ end
20
+
21
+ def elements_to_s
22
+ elements.map(&:to_s).join("\n")
23
+ end
24
+
25
+ def add_child
26
+ @child_count ||= 0 + 1
27
+ end
28
+
29
+ def source_content
30
+ self.text_value.strip
31
+ end
32
+ end
33
+
34
+ class IcBaseType < StatementNode
35
+ def title
36
+ :base_type
37
+ end
38
+
39
+ def to_s
40
+ elements_to_s
41
+ end
42
+ end
43
+
44
+ class IcBaseTypeSingle < StatementNode
45
+ def title
46
+ :base_type_single
47
+ end
48
+
49
+ def to_s
50
+ elements_to_s
51
+ end
52
+
53
+ def opt_code
54
+ 'single'
55
+ end
56
+ end
57
+
58
+ class IcBaseTypeRecord < StatementNode
59
+ def title
60
+ :base_type_record
61
+ end
62
+
63
+ def to_s
64
+ elements_to_s
65
+ end
66
+
67
+ def opt_code
68
+ 'record'
69
+ end
70
+ end
71
+
72
+ class IcBaseTypeKey < StatementNode
73
+ def title
74
+ :base_type_key
75
+ end
76
+
77
+ def to_s
78
+ elements_to_s
79
+ end
80
+ end
81
+
82
+ class IcBaseTypeValue < StatementNode
83
+ def title
84
+ :base_type_value
85
+ end
86
+
87
+ def to_s
88
+ elements_to_s
89
+ end
90
+ end
91
+
92
+ class IcTypeDef < StatementNode
93
+ def title
94
+ :base_type_def
95
+ end
96
+
97
+ def to_s
98
+ elements_to_s
99
+ end
100
+ end
101
+
102
+ class IcBaseTypeVariant < StatementNode
103
+ def title
104
+ :base_type_variant
105
+ end
106
+
107
+ def to_s
108
+ elements_to_s
109
+ end
110
+
111
+ def opt_code
112
+ 'variant'
113
+ end
114
+ end
115
+
116
+ class IcBaseTypeFunc < StatementNode
117
+ def title
118
+ :base_type_func
119
+ end
120
+
121
+ def to_s
122
+ elements_to_s
123
+ end
124
+
125
+ def opt_code
126
+ 'func'
127
+ end
128
+ end
129
+
130
+ class IcBaseTypeOpt < StatementNode
131
+ def title
132
+ :base_type_opt
133
+ end
134
+
135
+ def to_s
136
+ elements_to_s
137
+ end
138
+
139
+ def opt_code
140
+ 'opt'
141
+ end
142
+ end
143
+
144
+ class IcBaseTypeVec < StatementNode
145
+ def title
146
+ :base_type_vec
147
+ end
148
+
149
+ def to_s
150
+ elements_to_s
151
+ end
152
+
153
+ def opt_code
154
+ 'vec'
155
+ end
156
+ end
157
+
158
+ class IcBaseTypeOther < StatementNode
159
+ def title
160
+ :base_type_other
161
+ end
162
+
163
+ def to_s
164
+ elements_to_s
165
+ end
166
+
167
+ def opt_code
168
+ text_value
169
+ end
170
+ end
171
+
172
+ class IcBaseTypeContent < StatementNode
173
+ def title
174
+ :base_type_content
175
+ end
176
+
177
+ def to_s
178
+ elements_to_s
179
+ end
180
+ end
181
+
182
+ class IcBaseTypeChild < StatementNode
183
+ def title
184
+ :base_type_child
185
+ end
186
+
187
+ def to_s
188
+ elements_to_s
189
+ end
190
+ end
191
+
192
+ class IcTypeName < StatementNode
193
+ def title
194
+ :type_name
195
+ end
196
+
197
+ def to_s
198
+ elements_to_s
199
+ end
200
+ end
201
+
202
+ class StatementBlock < StatementNode
203
+ def title
204
+ :statement_block
205
+ end
206
+
207
+ def to_s
208
+ elements_to_s
209
+ end
210
+ end
211
+
212
+ class StatementContent < StatementNode
213
+ def title
214
+ :statement_content
215
+ end
216
+
217
+ def to_s
218
+ elements_to_s
219
+ end
220
+ end
221
+ end
222
+ end
223
+ end
224
+
@@ -0,0 +1,22 @@
1
+ grammar RecordParser
2
+ # record { key1: text; key2: 42; value1; value2; }
3
+ rule record
4
+ 'record' space '{' space? (pair / value)* space? '}' <IcAgent::Ast::Nodes::IcBaseTypeRecord>
5
+ end
6
+
7
+ rule pair
8
+ space? identifier space? ':' space? identifier ';' <IcAgent::Ast::Nodes::IcBaseTypeChild>
9
+ end
10
+
11
+ rule value
12
+ space? identifier ';' <IcAgent::Ast::Nodes::IcBaseTypeChild>
13
+ end
14
+
15
+ rule identifier
16
+ [a-zA-Z0-9_]*
17
+ end
18
+
19
+ rule space
20
+ [\s]+
21
+ end
22
+ end
@@ -0,0 +1,101 @@
1
+ require 'treetop'
2
+
3
+ module IcAgent
4
+ module Ast
5
+ class StatementParser
6
+ attr_accessor :parser, :tree, :source_tree
7
+
8
+ REFER_TYPE_CLASS = ['IcAgent::Ast::Nodes::IcBaseTypeRecord',
9
+ 'IcAgent::Ast::Nodes::IcBaseTypeVariant',
10
+ 'IcAgent::Ast::Nodes::IcBaseTypeVec',
11
+ 'IcAgent::Ast::Nodes::IcBaseTypeOpt']
12
+
13
+ TREE_TYPE_CLASS = ['IcAgent::Ast::Nodes::IcBaseTypeRecord',
14
+ 'IcAgent::Ast::Nodes::IcBaseTypeVariant',
15
+ 'IcAgent::Ast::Nodes::IcBaseTypeVec',
16
+ 'IcAgent::Ast::Nodes::IcBaseTypeFunc',
17
+ 'IcAgent::Ast::Nodes::IcBaseTypeOpt',
18
+ 'IcAgent::Ast::Nodes::IcBaseTypeSingle',
19
+ 'IcAgent::Ast::Nodes::IcBaseTypeOther']
20
+
21
+ REFER_TYPE_KEYS = ['record', 'variant']
22
+
23
+ def initialize
24
+ Treetop.load(File.expand_path(File.join(File.dirname(__FILE__), 'nested_type_grammar.treetop')))
25
+ @parser = TypeGrammarParser.new
26
+ end
27
+
28
+ def parse(data, return_type = :string)
29
+ tree = @parser.parse(data)
30
+ raise Exception, "Parse error at offset: #{@parser.index} #{@parser.failure_reason}" if tree.nil?
31
+
32
+ # this edits the tree in place
33
+ clean_tree(tree)
34
+
35
+ # generate soure tree
36
+ gen_source_tree(tree)
37
+ @tree = tree
38
+ tree
39
+ end
40
+
41
+ def clean_tree(root_node)
42
+ return if root_node.elements.nil?
43
+
44
+ root_node.elements.delete_if { |node| node.class.name == 'Treetop::Runtime::SyntaxNode' and node.parent.class.name != 'IcAgent::Ast::Nodes::IcBaseTypeValue' and node.parent.class.name != "IcAgent::Ast::Nodes::IcTypeDef" }
45
+ root_node.elements.each { |node| self.clean_tree(node) }
46
+ end
47
+
48
+ # @param [Object] root_node
49
+ # @param [nil] tree_root_node
50
+ # @param [nil] tree_current_node
51
+ def gen_source_tree(root_node, tree_root_node = nil, tree_current_node = nil)
52
+ return if root_node.elements.nil?
53
+
54
+ tree_root_node = tree_root_node.nil? ? Tree::TreeNode.new('root', { 'total_child': 0, 'ic_type': nil, 'refer_type': [], 'prototype': root_node.source_content, 'content': root_node.source_content }) : tree_root_node
55
+ tree_current_node = tree_current_node.nil? ? tree_root_node : tree_current_node
56
+
57
+ root_node.elements.each do |node|
58
+ if TREE_TYPE_CLASS.include?(node.class.name) && node.source_content != tree_root_node.content[:prototype]
59
+
60
+ id = tree_root_node.content[:total_child] + 1
61
+ new_tree_node = Tree::TreeNode.new("node_#{id}", { 'total_child': 0, 'ic_type': nil, 'prototype': root_node.source_content, 'content': root_node.source_content })
62
+ tree_current_node << new_tree_node
63
+ tree_root_node.content[:total_child] = id
64
+
65
+ # set refer_type
66
+ unless Regexp.union(REFER_TYPE_KEYS) === root_node.source_content
67
+ # func type content
68
+ if root_node.source_content.index('->')
69
+ param_arr = []
70
+ temp_param_arr = root_node.source_content.strip.split(' ').collect { |v| v.strip.gsub(';', '') }
71
+ temp_param_arr.delete_if {|v| !v.index('(') && !v.index(')') }
72
+ temp_param_arr.each {|v| param_arr = param_arr + v.sub('(', '').sub(')', '').split(',')}
73
+ else
74
+ param_arr = root_node.source_content.strip.split(' ').collect { |v| v.strip.gsub(';', '') }
75
+ param_arr = param_arr - IcAgent::Candid::ALL_TYPES
76
+ end
77
+ tree_root_node.content[:refer_type] = (tree_root_node.content[:refer_type] + param_arr).uniq
78
+ end
79
+
80
+ self.source_tree = tree_root_node
81
+ self.gen_source_tree(node, tree_root_node, new_tree_node)
82
+ else
83
+ self.gen_source_tree(node, tree_root_node, tree_current_node)
84
+ end
85
+ end
86
+
87
+ self.source_tree = tree_root_node
88
+ end
89
+
90
+ def ic_statement_root
91
+ tree.elements[0]
92
+ end
93
+
94
+ def ic_statement_childs
95
+ if tree.elements[0] && tree.elements[0].elements[0].elements[0]
96
+ tree.elements[0].elements[0].elements[0].elements
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,21 @@
1
+ grammar VariantParser
2
+ rule variant
3
+ 'variant' space? '{' space? (pair / key)* space? '}' <IcAgent::Ast::Nodes::IcBaseTypeVariant>
4
+ end
5
+
6
+ rule pair
7
+ space? identifier space? ':' space? identifier ';' <IcAgent::Ast::Nodes::IcBaseTypeChild>
8
+ end
9
+
10
+ rule key
11
+ space? identifier ';' <IcAgent::Ast::Nodes::IcBaseTypeChild>
12
+ end
13
+
14
+ rule identifier
15
+ [a-zA-Z0-9_]*
16
+ end
17
+
18
+ rule space
19
+ [\s]+
20
+ end
21
+ end
@@ -33,9 +33,14 @@ module IcAgent
33
33
  args_arr.each do |arg|
34
34
  args_type_arrs << get_param_to_ic_type(parser, arg)
35
35
  end
36
- ret_type = get_param_to_ic_type(parser, rets)
37
36
 
38
- add_caniter_method(method_name, args, args_type_arrs, ret_type, anno)
37
+ rets_arr = rets.nil? ? [] : rets.split(',').map(&:strip)
38
+ rets_type_arr = []
39
+ rets_arr.each do |ret|
40
+ rets_type_arr << get_param_to_ic_type(parser, ret)
41
+ end
42
+
43
+ add_caniter_method(method_name, args, args_type_arrs, rets_type_arr, anno)
39
44
  end
40
45
  end
41
46
 
@@ -69,7 +74,7 @@ module IcAgent
69
74
  def build_param_tree(parser, type_name, current_node = nil, tree_root_node = nil)
70
75
  if current_node.nil?
71
76
  root_type = parser.ic_type_by_name(type_name)
72
- refer_nodes = root_type.type_child_refer_items.nil? ? [] : root_type.type_child_refer_items
77
+ refer_nodes = root_type.type_refer_items.nil? ? [] : root_type.type_refer_items
73
78
  root_node = Tree::TreeNode.new(type_name,
74
79
  { 'total_child': refer_nodes.size,
75
80
  'all_child': refer_nodes,
@@ -119,7 +124,7 @@ module IcAgent
119
124
  else
120
125
  # non self refer
121
126
  child_type = parser.ic_type_by_name(refer_node)
122
- child_refer_nodes = child_type.type_child_refer_items
127
+ child_refer_nodes = child_type.type_refer_items
123
128
  if child_refer_nodes.size == 0
124
129
  # set ic type
125
130
  ic_type = IcAgent::Ast::Assembler.build_type(child_type.type_param_content)
@@ -183,14 +188,11 @@ module IcAgent
183
188
  refer_type.empty? ? nil : refer_type[0]
184
189
  end
185
190
 
186
- def only_refer_type?(param)
187
- refer_types = IcAgent::Ast::Assembler.get_params_refer_values(param)
188
- param.index(' ').nil? && refer_types.size == 1
189
- end
190
-
191
191
  def get_param_to_ic_type(parser, param)
192
192
  ic_refer_types = {}
193
193
  refer_types = IcAgent::Ast::Assembler.get_params_refer_values(param)
194
+ # param self is refer_values
195
+ refer_types << param unless param.index(' ') || IcAgent::Candid::ALL_TYPES.any?(param)
194
196
  refer_types.each do |refer_code|
195
197
  ic_refer_types[refer_code] = build_param_tree(parser, refer_code, nil, nil).content[:ic_type]
196
198
  end
@@ -235,14 +237,14 @@ module IcAgent
235
237
  new_root_text
236
238
  end
237
239
 
238
- def add_caniter_method(method_name, type_args, args_types, rets_type, anno = nil)
240
+ def add_caniter_method(method_name, type_args, args_types, rets_types, anno = nil)
239
241
  self.class.class_eval do
240
242
  define_method(method_name) do |*args|
241
243
  init_method_name = method_name
242
244
  init_method_args = type_args.nil? ? [] : type_args.split(',').map(&:strip)
243
245
  init_method_anno = anno
244
246
  init_method_types = args_types
245
- init_method_ret_type = rets_type
247
+ init_method_ret_type = rets_types
246
248
 
247
249
  if init_method_args.length != args.length
248
250
  raise ArgumentError, 'Arguments length not match'
@@ -228,7 +228,7 @@ module IcAgent
228
228
  wallet_balance128: () -> (record { amount: nat }) query;
229
229
  wallet_send: (record { canister: principal; amount: nat64 }) -> (WalletResult);
230
230
  wallet_send128: (record { canister: principal; amount: nat }) -> (WalletResult);
231
- wallet_receive: (opt ReceiveOptions) -> (); // Endpoint for receiving cycles.
231
+ wallet_receive: (opt ReceiveOptions) -> ();
232
232
  wallet_create_canister: (CreateCanisterArgs) -> (WalletResultCreate);
233
233
  wallet_create_canister128: (CreateCanisterArgs128) -> (WalletResultCreate);
234
234
  wallet_create_wallet: (CreateCanisterArgs) -> (WalletResultCreate);
@@ -248,9 +248,7 @@ module IcAgent
248
248
  args: blob;
249
249
  cycles: nat;
250
250
  }) -> (WalletResultCall);
251
- add_address: (address: AddressEntry) -> ();
252
251
  list_addresses: () -> (vec AddressEntry) query;
253
- remove_address: (address: principal) -> (WalletResult);
254
252
  get_events: (opt record { from: opt nat32; to: opt nat32; }) -> (vec Event) query;
255
253
  get_events128: (opt record { from: opt nat32; to: opt nat32; }) -> (vec Event128) query;
256
254
  get_chart: (opt record { count: opt nat32; precision: opt nat64; } ) -> (vec record { nat64; nat64; }) query;
@@ -258,7 +256,6 @@ module IcAgent
258
256
  get_managed_canister_events: (record { canister: principal; from: opt nat32; to: opt nat32; }) -> (opt vec ManagedCanisterEvent) query;
259
257
  get_managed_canister_events128: (record { canister: principal; from: opt nat32; to: opt nat32; }) -> (opt vec ManagedCanisterEvent128) query;
260
258
  set_short_name: (principal, opt text) -> (opt ManagedCanisterInfo);
261
- http_request: (request: HttpRequest) -> (HttpResponse) query;
262
259
  }
263
260
  DIDL_DOC
264
261
 
@@ -4,7 +4,6 @@ module IcAgent
4
4
  CANISTER_ID = 'aaaaa-aa'
5
5
  DID_FILE = <<~DIDL_DOC
6
6
  type canister_id = principal;
7
- type user_id = principal;
8
7
  type wasm_module = blob;
9
8
 
10
9
  type canister_settings = record {
@@ -21,21 +20,117 @@ module IcAgent
21
20
  freezing_threshold : nat;
22
21
  };
23
22
 
24
- service ic : {
23
+ type change_origin = variant {
24
+ from_user : record {
25
+ user_id : principal;
26
+ };
27
+ from_canister : record {
28
+ canister_id : principal;
29
+ canister_version : opt nat64;
30
+ };
31
+ };
32
+
33
+ type change_details = variant {
34
+ creation : record {
35
+ controllers : vec principal;
36
+ };
37
+ code_uninstall;
38
+ code_deployment : record {
39
+ mode : variant {install; reinstall; upgrade};
40
+ module_hash : blob;
41
+ };
42
+ controllers_change : record {
43
+ controllers : vec principal;
44
+ };
45
+ };
46
+
47
+ type change = record {
48
+ timestamp_nanos : nat64;
49
+ canister_version : nat64;
50
+ origin : change_origin;
51
+ details : change_details;
52
+ };
53
+
54
+ type http_header = record { name: text; value: text };
55
+
56
+ type ecdsa_curve = variant { secp256k1; };
57
+
58
+ type satoshi = nat64;
59
+
60
+ type bitcoin_network = variant {
61
+ mainnet;
62
+ testnet;
63
+ };
64
+
65
+ type bitcoin_address = text;
66
+
67
+ type block_hash = blob;
68
+
69
+ type outpoint = record {
70
+ txid : blob;
71
+ vout : nat32
72
+ };
73
+
74
+ type utxo = record {
75
+ outpoint: outpoint;
76
+ value: satoshi;
77
+ height: nat32;
78
+ };
79
+
80
+ type get_utxos_request = record {
81
+ address : bitcoin_address;
82
+ network: bitcoin_network;
83
+ filter: opt variant {
84
+ min_confirmations: nat32;
85
+ page: blob;
86
+ };
87
+ };
88
+
89
+ type get_current_fee_percentiles_request = record {
90
+ network: bitcoin_network;
91
+ };
92
+
93
+ type get_utxos_response = record {
94
+ utxos: vec utxo;
95
+ tip_block_hash: block_hash;
96
+ tip_height: nat32;
97
+ next_page: opt blob;
98
+ };
99
+
100
+ type get_balance_request = record {
101
+ address : bitcoin_address;
102
+ network: bitcoin_network;
103
+ min_confirmations: opt nat32;
104
+ };
105
+
106
+ type send_transaction_request = record {
107
+ transaction: blob;
108
+ network: bitcoin_network;
109
+ };
110
+
111
+ type millisatoshi_per_byte = nat64;
112
+
113
+ service : {
25
114
  create_canister : (record {
26
- settings : opt canister_settings
115
+ settings : opt canister_settings;
116
+ sender_canister_version : opt nat64;
27
117
  }) -> (record {canister_id : canister_id});
28
118
  update_settings : (record {
29
119
  canister_id : principal;
30
- settings : canister_settings
120
+ settings : canister_settings;
121
+ sender_canister_version : opt nat64;
31
122
  }) -> ();
32
123
  install_code : (record {
33
124
  mode : variant {install; reinstall; upgrade};
34
125
  canister_id : canister_id;
35
126
  wasm_module : wasm_module;
36
127
  arg : blob;
128
+ sender_canister_version : opt nat64;
129
+ }) -> ();
130
+ uninstall_code : (record {
131
+ canister_id : canister_id;
132
+ sender_canister_version : opt nat64;
37
133
  }) -> ();
38
- uninstall_code : (record {canister_id : canister_id}) -> ();
39
134
  start_canister : (record {canister_id : canister_id}) -> ();
40
135
  stop_canister : (record {canister_id : canister_id}) -> ();
41
136
  canister_status : (record {canister_id : canister_id}) -> (record {
@@ -44,12 +139,41 @@ module IcAgent
44
139
  module_hash: opt blob;
45
140
  memory_size: nat;
46
141
  cycles: nat;
142
+ idle_cycles_burned_per_day: nat;
143
+ });
144
+ canister_info : (record {
145
+ canister_id : canister_id;
146
+ num_requested_changes : opt nat64;
147
+ }) -> (record {
148
+ total_num_changes : nat64;
149
+ recent_changes : vec change;
150
+ module_hash : opt blob;
151
+ controllers : vec principal;
47
152
  });
48
153
  delete_canister : (record {canister_id : canister_id}) -> ();
49
154
  deposit_cycles : (record {canister_id : canister_id}) -> ();
155
+ raw_rand : () -> (blob);
156
+
157
+ ecdsa_public_key : (record {
158
+ canister_id : opt canister_id;
159
+ derivation_path : vec blob;
160
+ key_id : record { curve: ecdsa_curve; name: text };
161
+ }) -> (record { public_key : blob; chain_code : blob; });
162
+ sign_with_ecdsa : (record {
163
+ message_hash : blob;
164
+ derivation_path : vec blob;
165
+ key_id : record { curve: ecdsa_curve; name: text };
166
+ }) -> (record { signature : blob });
167
+
168
+ bitcoin_get_balance: (get_balance_request) -> (satoshi);
169
+ bitcoin_get_utxos: (get_utxos_request) -> (get_utxos_response);
170
+ bitcoin_send_transaction: (send_transaction_request) -> ();
171
+ bitcoin_get_current_fee_percentiles: (get_current_fee_percentiles_request) -> (vec millisatoshi_per_byte);
172
+
50
173
  provisional_create_canister_with_cycles : (record {
51
174
  amount: opt nat;
52
- settings : opt canister_settings
175
+ settings : opt canister_settings;
176
+ specified_id: opt canister_id;
53
177
  }) -> (record {canister_id : canister_id});
54
178
  provisional_top_up_canister :
55
179
  (record { canister_id: canister_id; amount: nat }) -> ();
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IcAgent
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.3'
5
5
  end
data/lib/ic_agent.rb CHANGED
@@ -14,8 +14,10 @@ require_relative 'ic_agent/system_state'
14
14
  require_relative 'ic_agent/canister'
15
15
 
16
16
  require_relative 'ic_agent/ast/nodes/named_nodes'
17
+ require_relative 'ic_agent/ast/nodes/statement_nodes'
17
18
  require_relative 'ic_agent/ast/nodes/string_literal'
18
19
  require_relative 'ic_agent/ast/parser'
20
+ require_relative 'ic_agent/ast/statement_parser'
19
21
  require_relative 'ic_agent/ast/writer'
20
22
  require_relative 'ic_agent/ast/assembler'
21
23
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ic_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Terry.Tu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-08 00:00:00.000000000 Z
11
+ date: 2023-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base32
@@ -241,9 +241,15 @@ files:
241
241
  - lib/ic_agent/ast/assembler.rb
242
242
  - lib/ic_agent/ast/did_grammar.treetop
243
243
  - lib/ic_agent/ast/did_grammar_v1.treetop
244
+ - lib/ic_agent/ast/did_grammar_v2.treetop
245
+ - lib/ic_agent/ast/nested_type_grammar.treetop
244
246
  - lib/ic_agent/ast/nodes/named_nodes.rb
247
+ - lib/ic_agent/ast/nodes/statement_nodes.rb
245
248
  - lib/ic_agent/ast/nodes/string_literal.rb
246
249
  - lib/ic_agent/ast/parser.rb
250
+ - lib/ic_agent/ast/record_parser.treetop
251
+ - lib/ic_agent/ast/statement_parser.rb
252
+ - lib/ic_agent/ast/variant_parser.treetop
247
253
  - lib/ic_agent/ast/writer.rb
248
254
  - lib/ic_agent/candid.rb
249
255
  - lib/ic_agent/canister.rb