ic_agent 0.1.4 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,217 +3,280 @@ require 'treetop'
3
3
  module IcAgent
4
4
  module Ast
5
5
  module Nodes
6
+ # Represents a statement node in the abstract syntax tree with additional attributes.
6
7
  class StatementNode < Treetop::Runtime::SyntaxNode
8
+ # Additional attributes to store child count and depth.
7
9
  attr_accessor :child_count, :depth
8
10
 
11
+ # The title of the statement node, by default :named_node.
9
12
  def title
10
13
  :named_node
11
14
  end
12
15
 
16
+ # Converts the statement node and its children to an array representation.
13
17
  def to_array
14
18
  [title] + elements.map(&:to_array)
15
19
  end
16
20
 
21
+ # Converts the statement node and its children to a string representation.
17
22
  def to_s
18
23
  "#{title.to_s.upcase} #{elements_to_s}"
19
24
  end
20
25
 
26
+ # Converts the children of the statement node to a string.
21
27
  def elements_to_s
22
28
  elements.map(&:to_s).join("\n")
23
29
  end
24
30
 
31
+ # Adds a child to the statement node.
25
32
  def add_child
26
33
  @child_count ||= 0 + 1
27
34
  end
28
35
 
36
+ # Returns the source content of the statement node by removing leading and trailing whitespaces.
29
37
  def source_content
30
38
  self.text_value.strip
31
39
  end
32
40
  end
33
41
 
42
+ # Represents an IC base type node in the abstract syntax tree, a subclass of StatementNode.
34
43
  class IcBaseType < StatementNode
44
+ # The title of the IC base type node.
35
45
  def title
36
46
  :base_type
37
47
  end
38
48
 
49
+ # Converts the IC base type node to a string representation.
39
50
  def to_s
40
51
  elements_to_s
41
52
  end
42
53
  end
43
54
 
55
+ # Represents an IC base type single node in the abstract syntax tree, a subclass of StatementNode.
44
56
  class IcBaseTypeSingle < StatementNode
57
+ # The title of the IC base type single node.
45
58
  def title
46
59
  :base_type_single
47
60
  end
48
61
 
62
+ # Converts the IC base type single node to a string representation.
49
63
  def to_s
50
64
  elements_to_s
51
65
  end
52
66
 
67
+ # Returns the opt code for the IC base type single node, which is 'single'.
53
68
  def opt_code
54
69
  'single'
55
70
  end
56
71
  end
57
72
 
73
+ # Represents an IC base type record node in the abstract syntax tree, a subclass of StatementNode.
58
74
  class IcBaseTypeRecord < StatementNode
75
+ # The title of the IC base type record node.
59
76
  def title
60
77
  :base_type_record
61
78
  end
62
79
 
80
+ # Converts the IC base type record node to a string representation.
63
81
  def to_s
64
82
  elements_to_s
65
83
  end
66
84
 
85
+ # Returns the opt code for the IC base type record node, which is 'record'.
67
86
  def opt_code
68
87
  'record'
69
88
  end
70
89
  end
71
90
 
91
+ # Represents an IC base type key node in the abstract syntax tree, a subclass of StatementNode.
72
92
  class IcBaseTypeKey < StatementNode
93
+ # The title of the IC base type key node.
73
94
  def title
74
95
  :base_type_key
75
96
  end
76
97
 
98
+ # Converts the IC base type key node to a string representation.
77
99
  def to_s
78
100
  elements_to_s
79
101
  end
80
102
  end
81
103
 
104
+ # Represents an IC base type value node in the abstract syntax tree, a subclass of StatementNode.
82
105
  class IcBaseTypeValue < StatementNode
106
+ # The title of the IC base type value node.
83
107
  def title
84
108
  :base_type_value
85
109
  end
86
110
 
111
+ # Converts the IC base type value node to a string representation.
87
112
  def to_s
88
113
  elements_to_s
89
114
  end
90
115
  end
91
116
 
117
+ # Represents an IC type definition node in the abstract syntax tree, a subclass of StatementNode.
92
118
  class IcTypeDef < StatementNode
119
+ # The title of the IC type definition node.
93
120
  def title
94
121
  :base_type_def
95
122
  end
96
123
 
124
+ # Converts the IC type definition node to a string representation.
97
125
  def to_s
98
126
  elements_to_s
99
127
  end
100
128
  end
101
129
 
130
+ # Represents an IC base type variant node in the abstract syntax tree, a subclass of StatementNode.
102
131
  class IcBaseTypeVariant < StatementNode
132
+ # The title of the IC base type variant node.
103
133
  def title
104
134
  :base_type_variant
105
135
  end
106
136
 
137
+ # Converts the IC base type variant node to a string representation.
107
138
  def to_s
108
139
  elements_to_s
109
140
  end
110
141
 
142
+ # Returns the opt code for the IC base type variant node, which is 'variant'.
111
143
  def opt_code
112
144
  'variant'
113
145
  end
114
146
  end
115
147
 
148
+ # Represents an IC base type function node in the abstract syntax tree, a subclass of StatementNode.
116
149
  class IcBaseTypeFunc < StatementNode
150
+ # The title of the IC base type function node.
117
151
  def title
118
152
  :base_type_func
119
153
  end
120
154
 
155
+ # Converts the IC base type function node to a string representation.
121
156
  def to_s
122
157
  elements_to_s
123
158
  end
124
159
 
160
+ # Returns the opt code for the IC base type function node, which is 'func'.
125
161
  def opt_code
126
162
  'func'
127
163
  end
128
164
  end
129
165
 
166
+ # Represents an IC base type optional node in the abstract syntax tree, a subclass of StatementNode.
130
167
  class IcBaseTypeOpt < StatementNode
168
+ # The title of the IC base type optional node.
131
169
  def title
132
170
  :base_type_opt
133
171
  end
134
172
 
173
+ # Converts the IC base type optional node to a string representation.
135
174
  def to_s
136
175
  elements_to_s
137
176
  end
138
177
 
178
+ # Returns the opt code for the IC base type optional node, which is 'opt'.
139
179
  def opt_code
140
180
  'opt'
141
181
  end
142
182
  end
143
183
 
184
+ # Represents an IC base type vector node in the abstract syntax tree, a subclass of StatementNode.
144
185
  class IcBaseTypeVec < StatementNode
186
+ # The title of the IC base type vector node.
145
187
  def title
146
188
  :base_type_vec
147
189
  end
148
190
 
191
+ # Converts the IC base type vector node to a string representation.
149
192
  def to_s
150
193
  elements_to_s
151
194
  end
152
195
 
196
+ # Returns the opt code for the IC base type vector node, which is 'vec'.
153
197
  def opt_code
154
198
  'vec'
155
199
  end
156
200
  end
157
201
 
202
+ # Represents an IC base type other node in the abstract syntax tree, a subclass of StatementNode.
158
203
  class IcBaseTypeOther < StatementNode
204
+ # The title of the IC base type other node.
159
205
  def title
160
206
  :base_type_other
161
207
  end
162
208
 
209
+ # Converts the IC base type other node to a string representation.
163
210
  def to_s
164
211
  elements_to_s
165
212
  end
166
213
 
214
+ # Returns the opt code for the IC base type other node.
167
215
  def opt_code
168
216
  text_value
169
217
  end
170
218
  end
171
219
 
220
+ # Represents the content of an IC base type node in the abstract syntax tree, a subclass of StatementNode.
172
221
  class IcBaseTypeContent < StatementNode
222
+ # The title of the IC base type content node.
173
223
  def title
174
224
  :base_type_content
175
225
  end
176
226
 
227
+ # Converts the IC base type content node to a string representation.
177
228
  def to_s
178
229
  elements_to_s
179
230
  end
180
231
  end
181
232
 
233
+ # Represents a child of an IC base type node in the abstract syntax tree, a subclass of StatementNode.
182
234
  class IcBaseTypeChild < StatementNode
235
+ # The title of the IC base type child node.
183
236
  def title
184
237
  :base_type_child
185
238
  end
186
239
 
240
+ # Converts the IC base type child node to a string representation.
187
241
  def to_s
188
242
  elements_to_s
189
243
  end
190
244
  end
191
245
 
246
+ # Represents an IC type name node in the abstract syntax tree, a subclass of StatementNode.
192
247
  class IcTypeName < StatementNode
248
+ # The title of the IC type name node.
193
249
  def title
194
250
  :type_name
195
251
  end
196
252
 
253
+ # Converts the IC type name node to a string representation.
197
254
  def to_s
198
255
  elements_to_s
199
256
  end
200
257
  end
201
258
 
259
+ # Represents a statement block node in the abstract syntax tree, a subclass of StatementNode.
202
260
  class StatementBlock < StatementNode
261
+ # The title of the statement block node.
203
262
  def title
204
263
  :statement_block
205
264
  end
206
265
 
266
+ # Converts the statement block node to a string representation.
207
267
  def to_s
208
268
  elements_to_s
209
269
  end
210
270
  end
211
271
 
272
+ # Represents the content of a statement node in the abstract syntax tree, a subclass of StatementNode.
212
273
  class StatementContent < StatementNode
274
+ # The title of the statement content node.
213
275
  def title
214
276
  :statement_content
215
277
  end
216
278
 
279
+ # Converts the statement content node to a string representation.
217
280
  def to_s
218
281
  elements_to_s
219
282
  end
@@ -221,4 +284,3 @@ module IcAgent
221
284
  end
222
285
  end
223
286
  end
224
-
@@ -3,11 +3,16 @@ require 'treetop'
3
3
  module IcAgent
4
4
  module Ast
5
5
  module Nodes
6
+ # Represents a string literal node in the abstract syntax tree.
6
7
  class StringLiteral < Treetop::Runtime::SyntaxNode
8
+ # Converts the string literal node to an array representation.
9
+ # In this case, the array contains the text value of the string literal.
7
10
  def to_array
8
11
  self.text_value
9
12
  end
10
13
 
14
+ # Converts the string literal node to a string representation.
15
+ # In this case, it returns the text value of the string literal.
11
16
  def to_s
12
17
  self.text_value
13
18
  end
@@ -2,26 +2,41 @@ require 'treetop'
2
2
 
3
3
  module IcAgent
4
4
  module Ast
5
+ # The Parser class provides methods to parse data using a Treetop grammar and work with the resulting Abstract Syntax Tree (AST).
5
6
  class Parser
6
7
  attr_accessor :parser, :tree
7
8
 
9
+ # Initializes the Parser by loading the Treetop grammar file.
8
10
  def initialize
9
11
  Treetop.load(File.expand_path(File.join(File.dirname(__FILE__), 'did_grammar.treetop')))
10
12
  @parser = DIDGrammarParser.new
11
13
  end
12
14
 
15
+ # Parses the given data using the Treetop parser and returns the AST.
16
+ #
17
+ # Parameters:
18
+ # - data: The data to be parsed.
19
+ # - return_type: The desired return type for the parse result (:string by default).
20
+ #
21
+ # Returns:
22
+ # - The root node of the Abstract Syntax Tree (AST).
23
+ #
24
+ # Raises:
25
+ # - Exception if there is a parse error.
13
26
  def parse(data, return_type = :string)
14
27
  tree = @parser.parse(data)
15
28
 
16
29
  raise Exception, "Parse error at offset: #{@parser.index} #{@parser.failure_reason}" if tree.nil?
17
30
 
18
- # this edits the tree in place
31
+ # This method edits the tree in place to remove unnecessary syntax nodes.
19
32
  clean_tree(tree)
20
33
 
21
34
  @tree = tree
22
35
  tree
23
36
  end
24
37
 
38
+ # Recursively cleans the syntax tree by removing nodes of class 'Treetop::Runtime::SyntaxNode'.
39
+ # This method is used to remove unnecessary nodes generated during parsing.
25
40
  def clean_tree(root_node)
26
41
  return if root_node.elements.nil?
27
42
 
@@ -29,6 +44,10 @@ module IcAgent
29
44
  root_node.elements.each { |node| self.clean_tree(node) }
30
45
  end
31
46
 
47
+ # Retrieves the root node of the IC service from the AST.
48
+ #
49
+ # Returns:
50
+ # - The root node representing the IC service, or nil if not found.
32
51
  def ic_service
33
52
  tree.elements.each do |ele|
34
53
  return ele if ele.title == :ic_service
@@ -36,6 +55,7 @@ module IcAgent
36
55
  nil
37
56
  end
38
57
 
58
+ # Retrieves an array of AST nodes representing IC type declarations.
39
59
  def ic_types
40
60
  type_arr = []
41
61
  tree.elements.each do |ele|
@@ -44,6 +64,7 @@ module IcAgent
44
64
  type_arr
45
65
  end
46
66
 
67
+ # Converts the IC type declarations to an array of corresponding objects.
47
68
  def ic_types_obj
48
69
  obj_arr = []
49
70
  ic_types.each do |ic_type|
@@ -52,6 +73,10 @@ module IcAgent
52
73
  obj_arr
53
74
  end
54
75
 
76
+ # Retrieves the root node of the IC service methods from the AST.
77
+ #
78
+ # Returns:
79
+ # - The root node representing the IC service methods, or nil if not found.
55
80
  def ic_service_methods
56
81
  ic_service_tree = ic_service
57
82
  unless ic_service_tree.empty?
@@ -62,10 +87,12 @@ module IcAgent
62
87
  nil
63
88
  end
64
89
 
90
+ # Retrieves the name of an IC type from its AST node.
65
91
  def ic_type_name(ic_type)
66
92
  ic_type.type_param_name
67
93
  end
68
94
 
95
+ # Retrieves an array of names of all IC types from the AST.
69
96
  def ic_type_names
70
97
  names_arr = []
71
98
  ic_types.each do |ic_type|
@@ -74,6 +101,13 @@ module IcAgent
74
101
  names_arr
75
102
  end
76
103
 
104
+ # Retrieves the AST node of an IC type by its name.
105
+ #
106
+ # Parameters:
107
+ # - type_name: The name of the IC type to retrieve.
108
+ #
109
+ # Returns:
110
+ # - The AST node representing the IC type, or nil if not found.
77
111
  def ic_type_by_name(type_name)
78
112
  ic_types.each do |ic_type|
79
113
  return ic_type if type_name == ic_type_name(ic_type)
@@ -2,6 +2,8 @@ require 'treetop'
2
2
 
3
3
  module IcAgent
4
4
  module Ast
5
+ # The StatementParser class provides methods to parse data and generate an Abstract Syntax Tree (AST)
6
+ # for nested types based on the specified grammar.
5
7
  class StatementParser
6
8
  attr_accessor :parser, :tree, :source_tree
7
9
 
@@ -21,10 +23,19 @@ module IcAgent
21
23
  REFER_TYPE_KEYS = ['record', 'variant']
22
24
 
23
25
  def initialize
26
+ # Loads the Treetop grammar from the specified file.
24
27
  Treetop.load(File.expand_path(File.join(File.dirname(__FILE__), 'nested_type_grammar.treetop')))
25
28
  @parser = TypeGrammarParser.new
26
29
  end
27
30
 
31
+ # Parses the input data and generates the Abstract Syntax Tree (AST) for nested types based on the grammar.
32
+ #
33
+ # Parameters:
34
+ # - data: The input data to be parsed.
35
+ # - return_type: The desired format for the parsed AST (:string by default).
36
+ #
37
+ # Returns:
38
+ # - The generated AST in the specified format.
28
39
  def parse(data, return_type = :string)
29
40
  tree = @parser.parse(data)
30
41
  raise Exception, "Parse error at offset: #{@parser.index} #{@parser.failure_reason}" if tree.nil?
@@ -38,6 +49,7 @@ module IcAgent
38
49
  tree
39
50
  end
40
51
 
52
+ # Cleans up the AST tree by removing unnecessary nodes.
41
53
  def clean_tree(root_node)
42
54
  return if root_node.elements.nil?
43
55
 
@@ -45,6 +57,7 @@ module IcAgent
45
57
  root_node.elements.each { |node| self.clean_tree(node) }
46
58
  end
47
59
 
60
+ # Generates the source tree from the AST tree.
48
61
  # @param [Object] root_node
49
62
  # @param [nil] tree_root_node
50
63
  # @param [nil] tree_current_node
@@ -87,10 +100,12 @@ module IcAgent
87
100
  self.source_tree = tree_root_node
88
101
  end
89
102
 
103
+ # Returns the root node of the AST statement.
90
104
  def ic_statement_root
91
105
  tree.elements[0]
92
106
  end
93
107
 
108
+ # Returns the child nodes of the AST statement.
94
109
  def ic_statement_childs
95
110
  if tree.elements[0] && tree.elements[0].elements[0].elements[0]
96
111
  tree.elements[0].elements[0].elements[0].elements
@@ -1,10 +1,25 @@
1
1
  module IcAgent
2
2
  module Ast
3
+ # The Writer class provides methods to represent an Abstract Syntax Tree (AST) in different formats.
3
4
  class Writer
5
+ # Initializes the Writer with an Abstract Syntax Tree (AST).
6
+ #
7
+ # Parameters:
8
+ # - tree: The Abstract Syntax Tree (AST) to be represented.
4
9
  def initialize(tree)
5
10
  @tree = tree
6
11
  end
7
12
 
13
+ # Writes the AST in the desired format.
14
+ #
15
+ # Parameters:
16
+ # - return_type: The desired format to represent the AST (:string by default).
17
+ #
18
+ # Returns:
19
+ # - The AST in the specified format:
20
+ # - :tree: Returns the original AST.
21
+ # - :array: Returns the AST as an array.
22
+ # - :string: Returns the AST as a string.
8
23
  def write(return_type = :string)
9
24
  if return_type == :tree
10
25
  @tree
@@ -174,6 +174,8 @@ module IcAgent
174
174
  end
175
175
  end
176
176
 
177
+ # Represents an IDL Null
178
+ # check None == Null ?
177
179
  class NullClass < PrimitiveType
178
180
  def initialize()
179
181
  super
@@ -205,6 +207,7 @@ module IcAgent
205
207
  end
206
208
  end
207
209
 
210
+ # Represents an IDL Empty, a type which has no inhabitants.
208
211
  class EmptyClass < PrimitiveType
209
212
  def initialize
210
213
  super
@@ -235,6 +238,7 @@ module IcAgent
235
238
  end
236
239
  end
237
240
 
241
+ # Represents an IDL Bool
238
242
  class BoolClass < PrimitiveType
239
243
  def initialize
240
244
  super
@@ -275,6 +279,7 @@ module IcAgent
275
279
  end
276
280
  end
277
281
 
282
+ # Represents an IDL Reserved
278
283
  class ReservedClass < PrimitiveType
279
284
  def initialize
280
285
  super
@@ -308,6 +313,7 @@ module IcAgent
308
313
  end
309
314
  end
310
315
 
316
+ # Represents an IDL Text
311
317
  class TextClass < PrimitiveType
312
318
  def initialize
313
319
  super
@@ -343,6 +349,7 @@ module IcAgent
343
349
  end
344
350
  end
345
351
 
352
+ # Represents an IDL Int
346
353
  class IntClass < PrimitiveType
347
354
  def initialize
348
355
  super
@@ -374,6 +381,7 @@ module IcAgent
374
381
  end
375
382
  end
376
383
 
384
+ # Represents an IDL Nat
377
385
  class NatClass < PrimitiveType
378
386
  def initialize
379
387
  super
@@ -405,6 +413,7 @@ module IcAgent
405
413
  end
406
414
  end
407
415
 
416
+ # Represents an IDL Float
408
417
  class FloatClass < PrimitiveType
409
418
  def initialize(bits)
410
419
  super()
@@ -460,6 +469,7 @@ module IcAgent
460
469
  end
461
470
  end
462
471
 
472
+ # Represents an IDL fixed-width Int(n)
463
473
  class FixedIntClass < PrimitiveType
464
474
  def initialize(bits)
465
475
  super()
@@ -535,6 +545,7 @@ module IcAgent
535
545
  end
536
546
  end
537
547
 
548
+ # Represents an IDL fixed-width Nat(n)
538
549
  class FixedNatClass < PrimitiveType
539
550
  def initialize(bits)
540
551
  super()
@@ -605,6 +616,7 @@ module IcAgent
605
616
  end
606
617
  end
607
618
 
619
+ # Represents an IDL principal reference
608
620
  class PrincipalClass < PrimitiveType
609
621
  def initialize
610
622
  super
@@ -658,6 +670,7 @@ module IcAgent
658
670
  end
659
671
  end
660
672
 
673
+ # Represents an IDL Array
661
674
  class VecClass < ConstructType
662
675
  def initialize(_type)
663
676
  super()
@@ -704,6 +717,7 @@ module IcAgent
704
717
  end
705
718
  end
706
719
 
720
+ # Represents an IDL Option
707
721
  class OptClass < ConstructType
708
722
  def initialize(_type)
709
723
  super()
@@ -756,6 +770,7 @@ module IcAgent
756
770
  end
757
771
  end
758
772
 
773
+ # Represents an IDL Record
759
774
  class RecordClass < ConstructType
760
775
  def initialize(field)
761
776
  super()
@@ -845,6 +860,7 @@ module IcAgent
845
860
  end
846
861
  end
847
862
 
863
+ # Represents Tuple, a syntactic sugar for Record.
848
864
  class TupleClass < RecordClass
849
865
  attr_accessor :components
850
866
 
@@ -908,6 +924,7 @@ module IcAgent
908
924
  end
909
925
  end
910
926
 
927
+ # Represents an IDL Variant
911
928
  class VariantClass < ConstructType
912
929
  attr_accessor :fields
913
930
 
@@ -996,6 +1013,7 @@ module IcAgent
996
1013
  end
997
1014
  end
998
1015
 
1016
+ # Represents a reference to an IDL type, used for defining recursive data types.
999
1017
  class RecClass < ConstructType
1000
1018
  @@counter = 0
1001
1019
 
@@ -1073,6 +1091,7 @@ module IcAgent
1073
1091
  end
1074
1092
  end
1075
1093
 
1094
+ #Represents an IDL Func reference
1076
1095
  class FuncClass < ConstructType
1077
1096
  attr_accessor :arg_types, :ret_types, :annotations
1078
1097
 
@@ -1170,6 +1189,7 @@ module IcAgent
1170
1189
  end
1171
1190
  end
1172
1191
 
1192
+ # Represents an IDL Service reference
1173
1193
  class ServiceClass < ConstructType
1174
1194
  def initialize(field)
1175
1195
  super()
@@ -1344,6 +1364,7 @@ module IcAgent
1344
1364
  end
1345
1365
  end
1346
1366
 
1367
+ # through Pipe to decode bytes
1347
1368
  def self.leb128u_decode(pipe)
1348
1369
  res = StringIO.new
1349
1370
  loop do
@@ -1575,8 +1596,8 @@ module IcAgent
1575
1596
  return table[t]
1576
1597
  end
1577
1598
 
1578
- # params = [{type, value}]
1579
- # data = b'DIDL' + len(params) + encoded types + encoded values
1599
+ # @param [Object] params = [{type, value}]
1600
+ # @return data = b'DIDL' + len(params) + encoded types + encoded values
1580
1601
  def self.encode(params)
1581
1602
  arg_types = []
1582
1603
  args = []
@@ -1617,7 +1638,8 @@ module IcAgent
1617
1638
  return pre + table + length + typs + vals
1618
1639
  end
1619
1640
 
1620
- # decode a bytes value
1641
+ # @param [Object] data: decode a bytes value
1642
+ # @param [nil] ret_types
1621
1643
  # def decode(retTypes, data):
1622
1644
  def self.decode(data, ret_types = nil)
1623
1645
  pipe = Pipe.new(data)
@@ -1664,8 +1686,7 @@ module IcAgent
1664
1686
  'value' => t.decode_value(pipe, types[i])
1665
1687
  })
1666
1688
  end
1667
-
1668
- return outputs
1689
+ outputs
1669
1690
  end
1670
1691
  end
1671
1692
  end
@@ -2,6 +2,12 @@ require 'rubytree'
2
2
 
3
3
  module IcAgent
4
4
  class Canister
5
+ # Constructor for the Canister class.
6
+ #
7
+ # Parameters:
8
+ # - agent: An instance of the agent.
9
+ # - canister_id: ID of the canister.
10
+ # - candid: (Optional) The Candid description of the canister. If not provided, it will be queried.
5
11
  def initialize(agent, canister_id, candid=nil)
6
12
  @agent = agent
7
13
  @canister_id = canister_id
@@ -17,6 +23,7 @@ module IcAgent
17
23
  raise BaseException, "canister #{@canister_id} has no __get_candid_interface_tmp_hack method."
18
24
  end
19
25
 
26
+ # Parse the Candid description to extract information about service methods.
20
27
  parser = IcAgent::Ast::Parser.new
21
28
  parser.parse(@candid)
22
29