cast-to-yaml 0.1.0 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e8524dd3e090d0b7183a64d0211cb576e323335ef7f74c796bf9b782d6878d0
4
- data.tar.gz: 6d477343c3f14724045a5630f755a6f00b67a0b30c2fcd5056ecda1b3fa6db26
3
+ metadata.gz: 6414c24c96b94ec4c511017a89e19e77f1555bd761f4e90429cb9144ca1cb248
4
+ data.tar.gz: 5155474197891e24f1b22cd75576e7407a7911fa447033ca3ce38bf950c02906
5
5
  SHA512:
6
- metadata.gz: c285dfe327f870bf4d5d9da514f9211784dbc3d613fb6e3fe5fbc3443bfcf02426925df7fceb670ea2e40adc7447fba126e8a72daad88d2e1b524467628cc758
7
- data.tar.gz: e9fb38a6ea74eb661e681f3d5282898cbba31b244db2b8530d1069ff09e657ed69b29f6fe910ceee38e474b9067c48518b99d1c35378f0296d5d6a69bb2accfc
6
+ metadata.gz: 0733e59623ea7d45e7d8f14772be6399b2f2f32cdf50b4b0325b9107506abf09bd76367999e0bce6a7f43af4390163ee4cb6118687fac5f6837718235ad5e393
7
+ data.tar.gz: 4793adf466fba2cf74cc6b9f19746f086b4f45308534f2312baf42fbcdd105ed281fe3045c0bca1914a94d09f146ee4cc73e1109305fc2bd7348c262841a9a7e
data/cast-to-yaml.gemspec CHANGED
@@ -1,12 +1,11 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'cast-to-yaml'
3
- s.version = "0.1.0"
3
+ s.version = "0.1.2"
4
4
  s.author = "Brice Videau"
5
5
  s.email = "bvideau@anl.gov"
6
6
  s.homepage = "https://github.com/alcf-perfengr/cast-to-yaml"
7
7
  s.summary = "Extract information fom a c ast"
8
8
  s.files = Dir[ 'cast-to-yaml.gemspec', 'LICENSE', 'lib/**/*.rb' ]
9
- s.has_rdoc = false
10
9
  s.license = 'MIT'
11
10
  s.required_ruby_version = '>= 2.3.0'
12
11
  s.add_dependency 'cast', '~> 0.3', '>=0.3.0'
@@ -0,0 +1,85 @@
1
+ module C
2
+
3
+ class Node
4
+
5
+ #
6
+ # Serialize a node to a Hash representation.
7
+ #
8
+ def to_h
9
+ res = {}
10
+ kind = self.class.kind
11
+ res["kind"] = kind
12
+ fields.each do |f|
13
+ name = f.init_key.to_s
14
+ value = self.send(f.reader)
15
+ if value && !(value == f.make_default)
16
+ res[name] =
17
+ if f.child?
18
+ if value.kind_of? C::NodeList
19
+ value.collect { |n| n.to_h }
20
+ else
21
+ value.to_h
22
+ end
23
+ else
24
+ value
25
+ end
26
+ end
27
+ end
28
+ return res
29
+ end
30
+
31
+ private
32
+
33
+ def self.kind
34
+ @kind ||= name.split('::').last.
35
+ gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
36
+ gsub(/([a-z])([A-Z])/, '\1_\2').downcase
37
+ end
38
+
39
+ #
40
+ # Deserialize a node from a given Hash representation.
41
+ #
42
+ def self.from_h(h)
43
+ params = {}
44
+ fields.each do |f|
45
+ name = f.init_key
46
+ value = h[name.to_s]
47
+ if value
48
+ params[name] =
49
+ if f.child?
50
+ default = f.make_default
51
+ if default.kind_of?(C::NodeList) || value.kind_of?(::Array)
52
+ raise ArgumentError, "node is not a list" unless value.kind_of? ::Array
53
+ default = C::NodeArray.new unless default
54
+ default.push(*(value.collect { |c| C.from_h(c) }))
55
+ else
56
+ C.from_h(value)
57
+ end
58
+ else
59
+ value
60
+ end
61
+ end
62
+ end
63
+ return self.new(params)
64
+ end
65
+
66
+ end
67
+
68
+ #
69
+ # Deserialize an AST from a given Hash representation.
70
+ #
71
+ def self.from_h(h)
72
+ kind = h["kind"]
73
+ raise ArgumentError, "missing node kind" unless kind
74
+ klass = C.const_get(class_name_from_kind(kind))
75
+ raise ArgumentError, "unknown node" unless klass
76
+ return klass.send(:from_h, h)
77
+ end
78
+
79
+ private
80
+
81
+ def self.class_name_from_kind(kind)
82
+ kind.split("_").collect(&:capitalize).join
83
+ end
84
+
85
+ end
@@ -1,13 +1,40 @@
1
1
  module C
2
2
 
3
+ class Node
4
+
5
+ def to_h_split
6
+ res = {}
7
+ kind = self.class.kind
8
+ res["kind"] = kind
9
+ fields.each do |f|
10
+ name = f.init_key.to_s
11
+ value = self.send(f.reader)
12
+ if value && !(value == f.make_default)
13
+ res[name] =
14
+ if f.child?
15
+ if value.kind_of? C::NodeList
16
+ value.collect { |n| n.to_h_split }
17
+ else
18
+ value.to_h_split
19
+ end
20
+ else
21
+ value
22
+ end
23
+ end
24
+ end
25
+ return res
26
+ end
27
+
28
+ end
29
+
3
30
  class Declarator
4
- def to_h(declaration)
31
+ def to_h_split(declaration)
5
32
  res = {}
6
33
  res["name"] = name.dup
7
34
  res["type"] = if indirect_type
8
- indirect_type.to_h(declaration)
35
+ indirect_type.to_h_split(declaration)
9
36
  else
10
- declaration.type.to_h
37
+ declaration.type.to_h_split
11
38
  end
12
39
  if init
13
40
  res["init"] = init.to_s
@@ -22,15 +49,25 @@ module C
22
49
  class Declaration
23
50
 
24
51
  def to_a
25
- declarators.collect { |d|
26
- d.to_h(self)
27
- }
52
+ # Anonymous nested composites
53
+ if declarators.empty?
54
+ if (type.kind_of?(Struct) || type.kind_of?(Union)) && type.members && !type.name
55
+ [{"type" => self.type.to_h_split}]
56
+ end
57
+ else
58
+ declarators.collect { |d|
59
+ res = d.to_h_split(self)
60
+ res["storage"] = storage.to_s if storage
61
+ res["inline"] = true if inline?
62
+ res
63
+ }
64
+ end
28
65
  end
29
66
 
30
- def extract(res = Hash::new { |h, k| h[k] = [] })
67
+ def extract(res = Hash::new { |h, k| h[k] = [] }, declarations: true)
31
68
  if typedef?
32
69
  declarators.each { |d|
33
- res["typedefs"].push d.to_h(self)
70
+ res["typedefs"].push d.to_h_split(self)
34
71
  }
35
72
  else
36
73
  declarators.each { |d|
@@ -38,47 +75,65 @@ module C
38
75
  f = {}
39
76
  f["name"] = d.name
40
77
  if d.indirect_type.type
41
- f["type"] = d.indirect_type.type.to_h(self)
78
+ f["type"] = d.indirect_type.type.to_h_split(self)
42
79
  else
43
- f["type"] = type.to_h(self)
80
+ f["type"] = type.to_h_split(self)
44
81
  end
45
82
  if d.indirect_type.params
46
- f["params"] = d.indirect_type.params.collect { |p| p.to_h }
83
+ f["params"] = d.indirect_type.params.collect { |p| p.to_h_split }
84
+ end
85
+ if d.indirect_type.var_args?
86
+ f["var_args"] = true
87
+ end
88
+ if inline?
89
+ f["inline"] = true
90
+ end
91
+ if storage
92
+ f["storage"] = storage.to_s
47
93
  end
48
94
 
49
95
  res["functions"].push f
50
- else
51
- res["declarations"].push d.to_h(self)
96
+ elsif declarations
97
+ r = d.to_h_split(self)
98
+ r["storage"] = storage.to_s if storage
99
+ r["inline"] = true if inline?
100
+ res["declarations"].push r
52
101
  end
53
102
  }
54
103
  end
55
- if type.kind_of?(Struct) && type.members && type.name
56
- s = {}
57
- s["name"] = type.name
104
+ if type.kind_of?(Struct) && type.members
58
105
  m = []
59
106
  type.members.each { |mem|
107
+ mem.extract(res, declarations: false)
60
108
  m += mem.to_a
61
109
  }
62
- s["members"] = m
63
- res["structs"].push s
64
- elsif type.kind_of?(Enum) && type.members && type.name
110
+ if type.name
111
+ s = {}
112
+ s["name"] = type.name
113
+ s["members"] = m
114
+ res["structs"].push s
115
+ end
116
+ elsif type.kind_of?(Enum) && type.members && (type.name || (declarators.empty? && declarations))
65
117
  s = {}
66
- s["name"] = type.name
118
+ s["name"] = type.name if type.name
67
119
  m = []
68
120
  type.members.each { |mem|
69
- m.push mem.to_h
121
+ m.push mem.to_h_split
70
122
  }
71
123
  s["members"] = m
72
124
  res["enums"].push s
73
- elsif type.kind_of?(Union) && type.members && type.name
74
- s = {}
75
- s["name"] = type.name
125
+ elsif type.kind_of?(Union) && type.members
76
126
  m = []
77
127
  type.members.each { |mem|
128
+ mem.extract(res, declarations: false)
78
129
  m += mem.to_a
79
130
  }
80
- s["members"] = m
81
- res["unions"].push s
131
+ if type.name
132
+ s = {}
133
+ s["name"] = type.name
134
+ s["members"] = m
135
+ res["unions"].push s
136
+ end
82
137
  end
83
138
  res
84
139
  end
@@ -89,7 +144,7 @@ module C
89
144
  entities.select { |e|
90
145
  e.kind_of? Declaration
91
146
  }.each { |e|
92
- e.extract(res)
147
+ e.extract(res, declarations: true)
93
148
  }
94
149
  res
95
150
  end
@@ -99,7 +154,7 @@ module C
99
154
  float_longnesses = ['float', 'double', 'long double']
100
155
  ## DirectTypes
101
156
  class Struct
102
- def to_h
157
+ def to_h_split(_ = nil)
103
158
  res = {}
104
159
  res["kind"] = "struct"
105
160
  if name
@@ -119,7 +174,7 @@ module C
119
174
  end
120
175
 
121
176
  class Union
122
- def to_h
177
+ def to_h_split(_ = nil)
123
178
  res = {}
124
179
  res["kind"] = "union"
125
180
  if name
@@ -139,7 +194,7 @@ module C
139
194
  end
140
195
 
141
196
  class Enum
142
- def to_h
197
+ def to_h_split
143
198
  res = {}
144
199
  res["kind"] = "enum"
145
200
  if name
@@ -147,7 +202,7 @@ module C
147
202
  else
148
203
  m = []
149
204
  members.each { |mem|
150
- m.push mem.to_h
205
+ m.push mem.to_h_split
151
206
  }
152
207
  res["members"] = m
153
208
  end
@@ -159,7 +214,7 @@ module C
159
214
  end
160
215
 
161
216
  class Enumerator
162
- def to_h
217
+ def to_h_split
163
218
  res = {}
164
219
  res["name"] = name
165
220
  if val
@@ -182,7 +237,7 @@ module C
182
237
  [Complex , proc{"_Complex #{float_longnesses[longness]}"}],
183
238
  [Imaginary , proc{"_Imaginary #{float_longnesses[longness]}"}]
184
239
  ].each do |c, x|
185
- c.send(:define_method, :to_h) do |_ = nil|
240
+ c.send(:define_method, :to_h_split) do |_ = nil|
186
241
  res = {}
187
242
  if self.kind_of? CustomType
188
243
  res["kind"] = "custom_type"
@@ -199,7 +254,7 @@ module C
199
254
 
200
255
  ## IndirectTypes
201
256
  class Pointer
202
- def to_h(declaration = nil)
257
+ def to_h_split(declaration = nil)
203
258
  res = {}
204
259
  res["kind"] = "pointer"
205
260
  res["const"] = true if const?
@@ -207,28 +262,29 @@ module C
207
262
  res["volatile"] = true if volatile?
208
263
  if type
209
264
  if declaration
210
- res["type"] = type.to_h(declaration)
265
+ res["type"] = type.to_h_split(declaration)
211
266
  else
212
- res["type"] = type.to_h
267
+ res["type"] = type.to_h_split
213
268
  end
214
269
  else
215
- res["type"] = declaration.type.to_h
270
+ res["type"] = declaration.type.to_h_split
216
271
  end
217
272
  res
218
273
  end
219
274
  end
275
+
220
276
  class Array
221
- def to_h(declaration = nil)
277
+ def to_h_split(declaration = nil)
222
278
  res = {}
223
279
  res["kind"] = "array"
224
280
  if type
225
281
  if declaration
226
- res["type"] = type.to_h(declaration)
282
+ res["type"] = type.to_h_split(declaration)
227
283
  else
228
- res["type"] = type.to_h
284
+ res["type"] = type.to_h_split
229
285
  end
230
286
  else
231
- res["type"] = declaration.type.to_h
287
+ res["type"] = declaration.type.to_h_split
232
288
  end
233
289
  if length
234
290
  res["length"] = length.to_s
@@ -236,30 +292,35 @@ module C
236
292
  res
237
293
  end
238
294
  end
295
+
239
296
  class Function
240
- def to_h(declaration, no_types=false)
297
+ def to_h_split(declaration, no_types=false)
241
298
  res = {}
242
299
  res["kind"] = "function"
243
300
  if type
244
- res["type"] = type.to_h(declaration)
301
+ res["type"] = type.to_h_split(declaration)
245
302
  else
246
- res["type"] = declaration.type.to_h
303
+ res["type"] = declaration.type.to_h_split
247
304
  end
248
305
  if !params.nil?
249
306
  res["params"] = if no_types
250
307
  params.collect{|p| p.name }
251
308
  else
252
- params.collect{|p| p.to_h }
309
+ params.collect{|p| p.to_h_split }
253
310
  end
254
311
  end
312
+ if var_args?
313
+ res["var_args"] = true
314
+ end
255
315
  res
256
316
  end
257
317
  end
318
+
258
319
  class Parameter
259
- def to_h
320
+ def to_h_split
260
321
  res = {}
261
322
  res["name"] = name.to_s if name.to_s != ''
262
- res["type"] = type.to_h
323
+ res["type"] = type.to_h_split
263
324
  res["register"] = true if register?
264
325
  res
265
326
  end
data/lib/cast-to-yaml.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'cast'
2
+ require_relative 'cast-to-yaml/to_h'
2
3
  require_relative 'cast-to-yaml/to_yaml'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cast-to-yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brice Videau
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-16 00:00:00.000000000 Z
11
+ date: 2025-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cast
@@ -30,7 +30,7 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 0.3.0
33
- description:
33
+ description:
34
34
  email: bvideau@anl.gov
35
35
  executables: []
36
36
  extensions: []
@@ -39,12 +39,13 @@ files:
39
39
  - LICENSE
40
40
  - cast-to-yaml.gemspec
41
41
  - lib/cast-to-yaml.rb
42
+ - lib/cast-to-yaml/to_h.rb
42
43
  - lib/cast-to-yaml/to_yaml.rb
43
44
  homepage: https://github.com/alcf-perfengr/cast-to-yaml
44
45
  licenses:
45
46
  - MIT
46
47
  metadata: {}
47
- post_install_message:
48
+ post_install_message:
48
49
  rdoc_options: []
49
50
  require_paths:
50
51
  - lib
@@ -59,9 +60,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
60
  - !ruby/object:Gem::Version
60
61
  version: '0'
61
62
  requirements: []
62
- rubyforge_project:
63
- rubygems_version: 2.7.6.2
64
- signing_key:
63
+ rubygems_version: 3.4.20
64
+ signing_key:
65
65
  specification_version: 4
66
66
  summary: Extract information fom a c ast
67
67
  test_files: []