cast-to-yaml 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/cast-to-yaml.gemspec +1 -2
- data/lib/cast-to-yaml.rb +1 -0
- data/lib/cast-to-yaml/to_h.rb +85 -0
- data/lib/cast-to-yaml/to_yaml.rb +83 -33
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be1314489cac42f6fdc88072ecd2c9d18f3ffb9b938a91a4877d64f492b82ab4
|
4
|
+
data.tar.gz: a2d1171413f5219ea3f99a62e1e706fe7173ea9ddd7850a77e4a594a99009aee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c31c63132a199291668840678b8007cab4f3ba9c6303cdded9c7e8cca1e17b923121216c0096815ba92beb422f53c634c3ca0b5cb8a11cfc5db8a3fd762f8755
|
7
|
+
data.tar.gz: 33184e329313552a6661f387bc9d679efac14e44a7b8ece742961c819a9fe980245ab49732d0e1bc8bd88414ffd110ce02c6b63be585ac681127b1adfc7f6ce8
|
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.
|
3
|
+
s.version = "0.1.1"
|
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'
|
data/lib/cast-to-yaml.rb
CHANGED
@@ -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
|
data/lib/cast-to-yaml/to_yaml.rb
CHANGED
@@ -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
|
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.
|
35
|
+
indirect_type.to_h_split(declaration)
|
9
36
|
else
|
10
|
-
declaration.type.
|
37
|
+
declaration.type.to_h_split
|
11
38
|
end
|
12
39
|
if init
|
13
40
|
res["init"] = init.to_s
|
@@ -23,14 +50,17 @@ module C
|
|
23
50
|
|
24
51
|
def to_a
|
25
52
|
declarators.collect { |d|
|
26
|
-
d.
|
53
|
+
res = d.to_h_split(self)
|
54
|
+
res["storage"] = storage.to_s if storage
|
55
|
+
res["inline"] = true if inline?
|
56
|
+
res
|
27
57
|
}
|
28
58
|
end
|
29
59
|
|
30
|
-
def extract(res = Hash::new { |h, k| h[k] = [] })
|
60
|
+
def extract(res = Hash::new { |h, k| h[k] = [] }, declarations: true)
|
31
61
|
if typedef?
|
32
62
|
declarators.each { |d|
|
33
|
-
res["typedefs"].push d.
|
63
|
+
res["typedefs"].push d.to_h_split(self)
|
34
64
|
}
|
35
65
|
else
|
36
66
|
declarators.each { |d|
|
@@ -38,17 +68,29 @@ module C
|
|
38
68
|
f = {}
|
39
69
|
f["name"] = d.name
|
40
70
|
if d.indirect_type.type
|
41
|
-
f["type"] = d.indirect_type.type.
|
71
|
+
f["type"] = d.indirect_type.type.to_h_split(self)
|
42
72
|
else
|
43
|
-
f["type"] = type.
|
73
|
+
f["type"] = type.to_h_split(self)
|
44
74
|
end
|
45
75
|
if d.indirect_type.params
|
46
|
-
f["params"] = d.indirect_type.params.collect { |p| p.
|
76
|
+
f["params"] = d.indirect_type.params.collect { |p| p.to_h_split }
|
77
|
+
end
|
78
|
+
if d.indirect_type.var_args?
|
79
|
+
f["var_args"] = true
|
80
|
+
end
|
81
|
+
if inline?
|
82
|
+
f["inline"] = true
|
83
|
+
end
|
84
|
+
if storage
|
85
|
+
f["storage"] = storage.to_s
|
47
86
|
end
|
48
87
|
|
49
88
|
res["functions"].push f
|
50
|
-
|
51
|
-
|
89
|
+
elsif declarations
|
90
|
+
r = d.to_h_split(self)
|
91
|
+
r["storage"] = storage.to_s if storage
|
92
|
+
r["inline"] = true if inline?
|
93
|
+
res["declarations"].push r
|
52
94
|
end
|
53
95
|
}
|
54
96
|
end
|
@@ -57,6 +99,7 @@ module C
|
|
57
99
|
s["name"] = type.name
|
58
100
|
m = []
|
59
101
|
type.members.each { |mem|
|
102
|
+
mem.extract(res, declarations: false)
|
60
103
|
m += mem.to_a
|
61
104
|
}
|
62
105
|
s["members"] = m
|
@@ -66,7 +109,7 @@ module C
|
|
66
109
|
s["name"] = type.name
|
67
110
|
m = []
|
68
111
|
type.members.each { |mem|
|
69
|
-
m.push mem.
|
112
|
+
m.push mem.to_h_split
|
70
113
|
}
|
71
114
|
s["members"] = m
|
72
115
|
res["enums"].push s
|
@@ -75,6 +118,7 @@ module C
|
|
75
118
|
s["name"] = type.name
|
76
119
|
m = []
|
77
120
|
type.members.each { |mem|
|
121
|
+
mem.extract(res, declarations: false)
|
78
122
|
m += mem.to_a
|
79
123
|
}
|
80
124
|
s["members"] = m
|
@@ -89,7 +133,7 @@ module C
|
|
89
133
|
entities.select { |e|
|
90
134
|
e.kind_of? Declaration
|
91
135
|
}.each { |e|
|
92
|
-
e.extract(res)
|
136
|
+
e.extract(res, declarations: true)
|
93
137
|
}
|
94
138
|
res
|
95
139
|
end
|
@@ -99,7 +143,7 @@ module C
|
|
99
143
|
float_longnesses = ['float', 'double', 'long double']
|
100
144
|
## DirectTypes
|
101
145
|
class Struct
|
102
|
-
def
|
146
|
+
def to_h_split(_ = nil)
|
103
147
|
res = {}
|
104
148
|
res["kind"] = "struct"
|
105
149
|
if name
|
@@ -119,7 +163,7 @@ module C
|
|
119
163
|
end
|
120
164
|
|
121
165
|
class Union
|
122
|
-
def
|
166
|
+
def to_h_split(_ = nil)
|
123
167
|
res = {}
|
124
168
|
res["kind"] = "union"
|
125
169
|
if name
|
@@ -139,7 +183,7 @@ module C
|
|
139
183
|
end
|
140
184
|
|
141
185
|
class Enum
|
142
|
-
def
|
186
|
+
def to_h_split
|
143
187
|
res = {}
|
144
188
|
res["kind"] = "enum"
|
145
189
|
if name
|
@@ -147,7 +191,7 @@ module C
|
|
147
191
|
else
|
148
192
|
m = []
|
149
193
|
members.each { |mem|
|
150
|
-
m.push mem.
|
194
|
+
m.push mem.to_h_split
|
151
195
|
}
|
152
196
|
res["members"] = m
|
153
197
|
end
|
@@ -159,7 +203,7 @@ module C
|
|
159
203
|
end
|
160
204
|
|
161
205
|
class Enumerator
|
162
|
-
def
|
206
|
+
def to_h_split
|
163
207
|
res = {}
|
164
208
|
res["name"] = name
|
165
209
|
if val
|
@@ -182,7 +226,7 @@ module C
|
|
182
226
|
[Complex , proc{"_Complex #{float_longnesses[longness]}"}],
|
183
227
|
[Imaginary , proc{"_Imaginary #{float_longnesses[longness]}"}]
|
184
228
|
].each do |c, x|
|
185
|
-
c.send(:define_method, :
|
229
|
+
c.send(:define_method, :to_h_split) do |_ = nil|
|
186
230
|
res = {}
|
187
231
|
if self.kind_of? CustomType
|
188
232
|
res["kind"] = "custom_type"
|
@@ -199,7 +243,7 @@ module C
|
|
199
243
|
|
200
244
|
## IndirectTypes
|
201
245
|
class Pointer
|
202
|
-
def
|
246
|
+
def to_h_split(declaration = nil)
|
203
247
|
res = {}
|
204
248
|
res["kind"] = "pointer"
|
205
249
|
res["const"] = true if const?
|
@@ -207,28 +251,29 @@ module C
|
|
207
251
|
res["volatile"] = true if volatile?
|
208
252
|
if type
|
209
253
|
if declaration
|
210
|
-
res["type"] = type.
|
254
|
+
res["type"] = type.to_h_split(declaration)
|
211
255
|
else
|
212
|
-
res["type"] = type.
|
256
|
+
res["type"] = type.to_h_split
|
213
257
|
end
|
214
258
|
else
|
215
|
-
res["type"] = declaration.type.
|
259
|
+
res["type"] = declaration.type.to_h_split
|
216
260
|
end
|
217
261
|
res
|
218
262
|
end
|
219
263
|
end
|
264
|
+
|
220
265
|
class Array
|
221
|
-
def
|
266
|
+
def to_h_split(declaration = nil)
|
222
267
|
res = {}
|
223
268
|
res["kind"] = "array"
|
224
269
|
if type
|
225
270
|
if declaration
|
226
|
-
res["type"] = type.
|
271
|
+
res["type"] = type.to_h_split(declaration)
|
227
272
|
else
|
228
|
-
res["type"] = type.
|
273
|
+
res["type"] = type.to_h_split
|
229
274
|
end
|
230
275
|
else
|
231
|
-
res["type"] = declaration.type.
|
276
|
+
res["type"] = declaration.type.to_h_split
|
232
277
|
end
|
233
278
|
if length
|
234
279
|
res["length"] = length.to_s
|
@@ -236,30 +281,35 @@ module C
|
|
236
281
|
res
|
237
282
|
end
|
238
283
|
end
|
284
|
+
|
239
285
|
class Function
|
240
|
-
def
|
286
|
+
def to_h_split(declaration, no_types=false)
|
241
287
|
res = {}
|
242
288
|
res["kind"] = "function"
|
243
289
|
if type
|
244
|
-
res["type"] = type.
|
290
|
+
res["type"] = type.to_h_split(declaration)
|
245
291
|
else
|
246
|
-
res["type"] = declaration.type.
|
292
|
+
res["type"] = declaration.type.to_h_split
|
247
293
|
end
|
248
294
|
if !params.nil?
|
249
295
|
res["params"] = if no_types
|
250
296
|
params.collect{|p| p.name }
|
251
297
|
else
|
252
|
-
params.collect{|p| p.
|
298
|
+
params.collect{|p| p.to_h_split }
|
253
299
|
end
|
254
300
|
end
|
301
|
+
if var_args?
|
302
|
+
res["var_args"] = true
|
303
|
+
end
|
255
304
|
res
|
256
305
|
end
|
257
306
|
end
|
307
|
+
|
258
308
|
class Parameter
|
259
|
-
def
|
309
|
+
def to_h_split
|
260
310
|
res = {}
|
261
311
|
res["name"] = name.to_s if name.to_s != ''
|
262
|
-
res["type"] = type.
|
312
|
+
res["type"] = type.to_h_split
|
263
313
|
res["register"] = true if register?
|
264
314
|
res
|
265
315
|
end
|
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.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brice Videau
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cast
|
@@ -39,6 +39,7 @@ 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:
|
@@ -59,8 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
60
|
- !ruby/object:Gem::Version
|
60
61
|
version: '0'
|
61
62
|
requirements: []
|
62
|
-
|
63
|
-
rubygems_version: 2.7.6.2
|
63
|
+
rubygems_version: 3.1.2
|
64
64
|
signing_key:
|
65
65
|
specification_version: 4
|
66
66
|
summary: Extract information fom a c ast
|