ynl 0.4.2 → 0.5.0
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 +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/ynl/family.rb +2 -1
- data/lib/ynl/generator.rb +317 -129
- data/lib/ynl/models.rb +12 -107
- data/lib/ynl/parser.rb +303 -214
- data/lib/ynl/version.rb +1 -1
- data/lib/ynl.rb +1 -51
- data/sig/generated/ynl/generator.rbs +11 -1
- data/sig/generated/ynl/models.rbs +5 -33
- data/sig/generated/ynl/parser.rbs +43 -20
- metadata +3 -3
data/lib/ynl/parser.rb
CHANGED
|
@@ -7,18 +7,11 @@ module Ynl
|
|
|
7
7
|
# @param [String, #read] source
|
|
8
8
|
# @rbs (String | _Readable source) -> void
|
|
9
9
|
def initialize(source)
|
|
10
|
-
@
|
|
11
|
-
|
|
12
|
-
@consts = {}
|
|
13
|
-
@flags = {}
|
|
14
|
-
@enums = {}
|
|
15
|
-
@structs = {}
|
|
16
|
-
|
|
17
|
-
@attribute_sets = {}
|
|
10
|
+
@document = YAML.load(source, aliases: true)
|
|
11
|
+
@attribute_declarations = {}
|
|
18
12
|
@attribute_definitions = {}
|
|
19
|
-
@
|
|
20
|
-
@
|
|
21
|
-
@mcast_groups = {}
|
|
13
|
+
@expanding_subsets = []
|
|
14
|
+
@operation_values = []
|
|
22
15
|
end
|
|
23
16
|
|
|
24
17
|
# @param [String, #to_path] path
|
|
@@ -28,25 +21,26 @@ module Ynl
|
|
|
28
21
|
File.open(path) {|f| new(f) }.parse
|
|
29
22
|
end
|
|
30
23
|
|
|
24
|
+
# Returns a complete, deeply frozen model graph.
|
|
31
25
|
# @return [Models::Family]
|
|
32
26
|
# @rbs () -> Models::Family
|
|
33
27
|
def parse
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
parse_sub_message(d)
|
|
28
|
+
return @family if @family
|
|
29
|
+
|
|
30
|
+
normalize
|
|
31
|
+
@family = materialize
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private def normalize
|
|
35
|
+
@document.fetch('attribute-sets').each do |declaration|
|
|
36
|
+
name = declaration.fetch('name')
|
|
37
|
+
if @attribute_declarations.key?(name)
|
|
38
|
+
raise ParseError, "Duplicate AttributeSet: #{name}"
|
|
39
|
+
end
|
|
40
|
+
@attribute_declarations[name] = declaration
|
|
48
41
|
end
|
|
49
|
-
|
|
42
|
+
@attribute_declarations.each_key { attribute_definitions(it) }
|
|
43
|
+
if operations = @document['operations']
|
|
50
44
|
enum_model = case operations['enum-model']
|
|
51
45
|
when 'directional'
|
|
52
46
|
:directional
|
|
@@ -55,13 +49,12 @@ module Ynl
|
|
|
55
49
|
else
|
|
56
50
|
raise ParseError, "Unknown enum model: #{operations['enum-model']}"
|
|
57
51
|
end
|
|
58
|
-
@default_fixed_header = operations['fixed-header']&.then { @structs.fetch(it) }
|
|
59
52
|
|
|
60
53
|
if enum_model == :unified
|
|
61
54
|
counter = 0
|
|
62
55
|
operations['list']&.each do |d|
|
|
63
56
|
counter = d['value'] || (counter + 1)
|
|
64
|
-
|
|
57
|
+
@operation_values << [d, counter, counter]
|
|
65
58
|
end
|
|
66
59
|
else
|
|
67
60
|
request_counter = 1
|
|
@@ -69,7 +62,7 @@ module Ynl
|
|
|
69
62
|
operations['list']&.each do |d|
|
|
70
63
|
if d.key?('notify') || d.key?('event')
|
|
71
64
|
reply_counter = d['value'] || reply_counter
|
|
72
|
-
|
|
65
|
+
@operation_values << [d, nil, reply_counter]
|
|
73
66
|
reply_counter += 1
|
|
74
67
|
else
|
|
75
68
|
primary = d['do'] || d['dump']
|
|
@@ -83,48 +76,118 @@ module Ynl
|
|
|
83
76
|
else
|
|
84
77
|
reply_id = nil
|
|
85
78
|
end
|
|
86
|
-
|
|
79
|
+
@operation_values << [d, request_id, reply_id]
|
|
87
80
|
end
|
|
88
81
|
end
|
|
89
82
|
end
|
|
90
83
|
end
|
|
91
|
-
|
|
92
|
-
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private def materialize
|
|
87
|
+
@consts = {}
|
|
88
|
+
@enums = {}
|
|
89
|
+
@flags = {}
|
|
90
|
+
@structs = {}
|
|
91
|
+
@attribute_sets = {}
|
|
92
|
+
@sub_messages = {}
|
|
93
|
+
@operations = {}
|
|
94
|
+
@mcast_groups = {}
|
|
95
|
+
@operation_declarations = {}
|
|
96
|
+
@building_operations = []
|
|
97
|
+
@operation_values.each do |entry|
|
|
98
|
+
name = entry.first.fetch('name')
|
|
99
|
+
raise ParseError, "Duplicate Operation: #{name}" if @operation_declarations.key?(name)
|
|
100
|
+
@operation_declarations[name] = entry
|
|
101
|
+
end
|
|
102
|
+
declare_models
|
|
103
|
+
|
|
104
|
+
@document.fetch('definitions', []).each { build_definition(it) }
|
|
105
|
+
@attribute_declarations.each_value { build_attribute_set(it) }
|
|
106
|
+
@document.fetch('sub-messages', []).each { build_sub_message(it) }
|
|
107
|
+
@document.dig('mcast-groups', 'list')&.each { build_mcast_group(it) }
|
|
108
|
+
@default_fixed_header = @document.dig('operations', 'fixed-header')&.then do
|
|
109
|
+
reference(@structs, it, 'struct')
|
|
110
|
+
end
|
|
111
|
+
# Construction may visit a notification source first; retain declaration
|
|
112
|
+
# order in the exposed table and keep the previously assigned wire IDs.
|
|
113
|
+
@operations = @operation_values.to_h do |declaration, request_id, reply_id|
|
|
114
|
+
operation = build_operation(declaration, request_id:, reply_id:)
|
|
115
|
+
[operation.name, operation]
|
|
93
116
|
end
|
|
94
117
|
|
|
95
118
|
Models::Family.new(
|
|
96
|
-
name
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
119
|
+
name: @document['name']&.dedup, protocol: (@document['protocol'] || 'genetlink').dedup,
|
|
120
|
+
version: @document.fetch('version', 1), protonum: @document['protonum'],
|
|
121
|
+
doc: translate_doc(@document['doc']),
|
|
122
|
+
consts: @consts.freeze, enums: @enums.freeze, flags: @flags.freeze, structs: @structs.freeze,
|
|
123
|
+
attribute_sets: @attribute_sets.freeze, sub_messages: @sub_messages.freeze,
|
|
124
|
+
operations: @operations.freeze, mcast_groups: @mcast_groups.freeze).freeze
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Establish identities through ordinary constructors. Their owned arrays
|
|
128
|
+
# remain mutable until the corresponding members, attributes, or formats
|
|
129
|
+
# have been built; references can already point to these models.
|
|
130
|
+
private def declare_models
|
|
131
|
+
@document.fetch('definitions', []).each do |d|
|
|
132
|
+
next unless d['type'] == 'struct'
|
|
133
|
+
register(@structs, Models::Struct.new(
|
|
134
|
+
name: d.fetch('name').dedup, members: [], doc: translate_doc(d['doc'])).freeze)
|
|
135
|
+
end
|
|
136
|
+
@attribute_sets = @attribute_declarations.to_h do |name, _|
|
|
137
|
+
[name, declare_attribute_set(name)]
|
|
138
|
+
end
|
|
139
|
+
@document.fetch('sub-messages', []).each do |d|
|
|
140
|
+
register(@sub_messages, Models::SubMessage.new(
|
|
141
|
+
name: d.fetch('name').dedup, formats: []).freeze)
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
private def declare_attribute_set(name)
|
|
146
|
+
return @attribute_sets.fetch(name) if @attribute_sets.key?(name)
|
|
147
|
+
|
|
148
|
+
d = reference(@attribute_declarations, name, 'attribute set')
|
|
149
|
+
model = if parent = d['subset-of']
|
|
150
|
+
Models::AttributeSubset.new(
|
|
151
|
+
name: name.dedup, superset: declare_attribute_set(parent),
|
|
152
|
+
attributes: [], doc: translate_doc(d['doc']))
|
|
153
|
+
else
|
|
154
|
+
Models::AttributeSet.new(
|
|
155
|
+
name: name.dedup, name_prefix: d['name_prefix']&.dedup,
|
|
156
|
+
attributes: [], doc: translate_doc(d['doc']))
|
|
157
|
+
end
|
|
158
|
+
@attribute_sets[name] = model.freeze
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
private def register(table, model)
|
|
162
|
+
if table.key?(model.name)
|
|
163
|
+
raise ParseError, "Duplicate #{model.class.name.split('::').last}: #{model.name}"
|
|
164
|
+
end
|
|
165
|
+
table[model.name] = model
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
private def reference(table, name, kind)
|
|
169
|
+
table.fetch(name) { raise ParseError, "Unknown #{kind}: #{name}" }
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
private def find_type(name)
|
|
173
|
+
@enums[name] || @flags[name] || @structs[name] || raise(ParseError, "Unknown type: #{name}")
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
private def build_definition(d)
|
|
113
177
|
case type = d.fetch('type')
|
|
114
178
|
when 'const'
|
|
115
|
-
v =
|
|
179
|
+
v = build_const(d)
|
|
116
180
|
@consts[v.name] = v
|
|
117
181
|
when 'enum'
|
|
118
|
-
v =
|
|
182
|
+
v = build_enum_flags(d, type: :enum)
|
|
119
183
|
reject_enum_flags_name_collision(v.name, @flags)
|
|
120
184
|
@enums[v.name] = v
|
|
121
185
|
when 'flags'
|
|
122
|
-
v =
|
|
186
|
+
v = build_enum_flags(d, type: :flags)
|
|
123
187
|
reject_enum_flags_name_collision(v.name, @enums)
|
|
124
188
|
@flags[v.name] = v
|
|
125
189
|
when 'struct'
|
|
126
|
-
|
|
127
|
-
@structs[v.name] = v
|
|
190
|
+
build_struct(d)
|
|
128
191
|
else
|
|
129
192
|
raise ParseError, "Unknown definition type: #{type}"
|
|
130
193
|
end
|
|
@@ -136,122 +199,128 @@ module Ynl
|
|
|
136
199
|
raise ParseError, "Definition #{name.inspect} cannot be both an enum and flags"
|
|
137
200
|
end
|
|
138
201
|
|
|
139
|
-
private def
|
|
140
|
-
name = "#{d['name-prefix']}#{d.fetch('name')}"
|
|
141
|
-
|
|
142
|
-
|
|
202
|
+
private def build_const(d)
|
|
203
|
+
name = "#{d['name-prefix']}#{d.fetch('name')}".dedup
|
|
204
|
+
literal = d.fetch('value')
|
|
205
|
+
value = case literal
|
|
206
|
+
when String
|
|
207
|
+
literal.dedup
|
|
208
|
+
when Integer
|
|
209
|
+
literal
|
|
210
|
+
else
|
|
143
211
|
raise ParseError, "Constant value must be a string or an integer: #{name}"
|
|
144
212
|
end
|
|
145
213
|
|
|
146
|
-
Models::Const.new(name
|
|
214
|
+
Models::Const.new(name:, value:, doc: translate_doc(d['doc'])).freeze
|
|
147
215
|
end
|
|
148
216
|
|
|
149
|
-
private def
|
|
217
|
+
private def build_enum_flags(d, type:)
|
|
150
218
|
cls = type == :enum ? Models::Enum : Models::Flags
|
|
151
|
-
result = cls.new(name: d.fetch('name'), doc: translate_doc(d['doc']))
|
|
152
|
-
|
|
153
219
|
start_value = d['value-start'] || 0
|
|
154
220
|
raw_value = start_value
|
|
155
221
|
|
|
156
|
-
d.fetch('entries').
|
|
222
|
+
entries = d.fetch('entries').map do |v|
|
|
157
223
|
explicit_value = v.is_a?(Hash) ? v['value'] : nil
|
|
158
224
|
raw_value = explicit_value unless explicit_value.nil?
|
|
159
225
|
value = type == :enum ? raw_value : 1 << raw_value
|
|
160
226
|
case v
|
|
161
227
|
when String
|
|
162
|
-
entry = cls::Entry.new(name: v, value:)
|
|
228
|
+
entry = cls::Entry.new(name: v.dedup, value:).freeze
|
|
163
229
|
when Hash
|
|
164
|
-
entry = cls::Entry.new(name: v.fetch('name'), value:, doc: translate_doc(v['doc']))
|
|
230
|
+
entry = cls::Entry.new(name: v.fetch('name').dedup, value:, doc: translate_doc(v['doc'])).freeze
|
|
165
231
|
else
|
|
166
232
|
raise ParseError, "Unknown class for enum/flags entry: #{v.class}"
|
|
167
233
|
end
|
|
168
234
|
|
|
169
|
-
result.entries << entry
|
|
170
|
-
|
|
171
235
|
raw_value += 1
|
|
236
|
+
entry
|
|
172
237
|
|
|
173
238
|
rescue
|
|
174
239
|
raise ParseError, "Failed to parse enum/flags entry: #{v.fetch('name')}"
|
|
175
240
|
end
|
|
176
241
|
|
|
177
|
-
|
|
242
|
+
cls.new(name: d.fetch('name').dedup, entries: entries.freeze, doc: translate_doc(d['doc'])).freeze
|
|
178
243
|
rescue
|
|
179
244
|
raise ParseError, "Failed to parse enum/flags: #{d.fetch('name')}"
|
|
180
245
|
end
|
|
181
246
|
|
|
182
|
-
private def
|
|
247
|
+
private def build_struct_member_type(d)
|
|
248
|
+
length = d['len']
|
|
249
|
+
length = length.dedup if length.is_a?(String)
|
|
183
250
|
type = d.fetch('type')
|
|
184
251
|
case type
|
|
185
252
|
when 'u8', 'u16', 'u32', 'u64', 's8', 's16', 's32', 's64', 'int', 'uint'
|
|
186
253
|
Types::Scalar.new(
|
|
187
|
-
type: type,
|
|
188
|
-
byte_order:
|
|
189
|
-
)
|
|
254
|
+
type: type.dedup,
|
|
255
|
+
byte_order: build_byte_order(d['byte-order']),
|
|
256
|
+
).freeze
|
|
190
257
|
when 'binary'
|
|
191
258
|
Types::Binary.new(
|
|
192
|
-
struct: d['struct'] ?
|
|
193
|
-
length:
|
|
194
|
-
display_hint: d['display-hint'],
|
|
195
|
-
)
|
|
259
|
+
struct: d['struct'] ? reference(@structs, d.fetch('struct'), 'struct') : nil,
|
|
260
|
+
length: length,
|
|
261
|
+
display_hint: d['display-hint']&.dedup,
|
|
262
|
+
).freeze
|
|
196
263
|
when 'pad'
|
|
197
264
|
Types::Pad.new(
|
|
198
|
-
length:
|
|
199
|
-
)
|
|
265
|
+
length: length,
|
|
266
|
+
).freeze
|
|
200
267
|
else
|
|
201
268
|
fail "Unknown type: #{type}"
|
|
202
269
|
end
|
|
203
270
|
end
|
|
204
271
|
|
|
205
|
-
private def
|
|
272
|
+
private def build_attribute_type(d)
|
|
206
273
|
type = d.fetch('type')
|
|
207
274
|
case type
|
|
208
275
|
when 'u8', 'u16', 'u32', 'u64', 's8', 's16', 's32', 's64', 'int', 'uint', 'sint'
|
|
209
276
|
Types::Scalar.new(
|
|
210
|
-
type: type,
|
|
211
|
-
byte_order:
|
|
212
|
-
)
|
|
277
|
+
type: type.dedup,
|
|
278
|
+
byte_order: build_byte_order(d['byte-order']),
|
|
279
|
+
).freeze
|
|
213
280
|
when 'binary'
|
|
214
281
|
if d.key?('sub-type')
|
|
215
|
-
Types::PackedArray.new(sub_type:
|
|
282
|
+
Types::PackedArray.new(sub_type: build_packed_array_sub_type(d)).freeze
|
|
216
283
|
else
|
|
217
284
|
Types::Binary.new(
|
|
218
|
-
struct: d['struct'] ?
|
|
285
|
+
struct: d['struct'] ? reference(@structs, d.fetch('struct'), 'struct') : nil,
|
|
219
286
|
length: nil,
|
|
220
|
-
display_hint: d['display-hint'],
|
|
221
|
-
)
|
|
287
|
+
display_hint: d['display-hint']&.dedup,
|
|
288
|
+
).freeze
|
|
222
289
|
end
|
|
223
290
|
when 'string'
|
|
224
|
-
Types::String.new
|
|
291
|
+
Types::String.new.freeze
|
|
225
292
|
when 'nest'
|
|
226
293
|
Types::NestedAttributes.new(
|
|
227
|
-
attribute_set:
|
|
228
|
-
)
|
|
294
|
+
attribute_set: reference(@attribute_sets, d.fetch('nested-attributes'), 'attribute set'),
|
|
295
|
+
).freeze
|
|
229
296
|
when 'indexed-array'
|
|
230
297
|
Types::IndexedArray.new(
|
|
231
|
-
sub_type:
|
|
232
|
-
)
|
|
298
|
+
sub_type: build_indexed_array_sub_type(d),
|
|
299
|
+
).freeze
|
|
233
300
|
when 'nest-type-value'
|
|
234
301
|
if d['nested-attributes']
|
|
302
|
+
type_values = d.fetch('type-value')
|
|
303
|
+
type_values.map!(&:dedup).freeze unless type_values.frozen?
|
|
235
304
|
Types::NestTypeValue.new(
|
|
236
|
-
attribute_set:
|
|
237
|
-
type_values
|
|
238
|
-
)
|
|
305
|
+
attribute_set: reference(@attribute_sets, d.fetch('nested-attributes'), 'attribute set'),
|
|
306
|
+
type_values:,
|
|
307
|
+
).freeze
|
|
239
308
|
else
|
|
240
|
-
Types::Binary.new(struct: nil, length: nil, display_hint: nil)
|
|
309
|
+
Types::Binary.new(struct: nil, length: nil, display_hint: nil).freeze
|
|
241
310
|
end
|
|
242
311
|
when 'sub-message'
|
|
243
312
|
Types::SubMessage.new(
|
|
244
|
-
sub_message:
|
|
245
|
-
selector: d.fetch('selector'),
|
|
246
|
-
)
|
|
313
|
+
sub_message: reference(@sub_messages, d.fetch('sub-message'), 'sub-message'),
|
|
314
|
+
selector: d.fetch('selector').dedup,
|
|
315
|
+
).freeze
|
|
247
316
|
when 'pad'
|
|
248
317
|
Types::Pad.new(
|
|
249
318
|
length: nil,
|
|
250
|
-
)
|
|
319
|
+
).freeze
|
|
251
320
|
when 'flag'
|
|
252
|
-
Types::Flag.new
|
|
321
|
+
Types::Flag.new.freeze
|
|
253
322
|
when 'bitfield32'
|
|
254
|
-
Types::Bitfield32.new
|
|
323
|
+
Types::Bitfield32.new.freeze
|
|
255
324
|
when 'unused'
|
|
256
325
|
nil
|
|
257
326
|
else
|
|
@@ -259,43 +328,43 @@ module Ynl
|
|
|
259
328
|
end
|
|
260
329
|
end
|
|
261
330
|
|
|
262
|
-
private def
|
|
331
|
+
private def build_indexed_array_sub_type(d)
|
|
263
332
|
sub_type = d.fetch('sub-type')
|
|
264
333
|
case sub_type
|
|
265
334
|
when 'u8', 'u16', 'u32', 'u64', 's8', 's16', 's32', 's64', 'int', 'uint', 'sint'
|
|
266
335
|
Types::Scalar.new(
|
|
267
|
-
type: sub_type,
|
|
268
|
-
byte_order:
|
|
269
|
-
)
|
|
336
|
+
type: sub_type.dedup,
|
|
337
|
+
byte_order: build_byte_order(d['byte-order']),
|
|
338
|
+
).freeze
|
|
270
339
|
when 'binary'
|
|
271
340
|
Types::Binary.new(
|
|
272
341
|
struct: nil,
|
|
273
342
|
length: nil,
|
|
274
|
-
display_hint: d['display-hint'],
|
|
275
|
-
)
|
|
343
|
+
display_hint: d['display-hint']&.dedup,
|
|
344
|
+
).freeze
|
|
276
345
|
when 'nest'
|
|
277
346
|
Types::NestedAttributes.new(
|
|
278
|
-
attribute_set:
|
|
279
|
-
)
|
|
347
|
+
attribute_set: reference(@attribute_sets, d.fetch('nested-attributes'), 'attribute set'),
|
|
348
|
+
).freeze
|
|
280
349
|
else
|
|
281
350
|
raise ParseError, "Unknown indexed-array sub-type: #{sub_type}"
|
|
282
351
|
end
|
|
283
352
|
end
|
|
284
353
|
|
|
285
|
-
private def
|
|
354
|
+
private def build_packed_array_sub_type(d)
|
|
286
355
|
sub_type = d.fetch('sub-type')
|
|
287
356
|
case sub_type
|
|
288
357
|
when 'u8', 'u16', 'u32', 'u64', 's8', 's16', 's32', 's64', 'int'
|
|
289
358
|
Types::Scalar.new(
|
|
290
|
-
type: sub_type,
|
|
291
|
-
byte_order:
|
|
292
|
-
)
|
|
359
|
+
type: sub_type.dedup,
|
|
360
|
+
byte_order: build_byte_order(d['byte-order']),
|
|
361
|
+
).freeze
|
|
293
362
|
else
|
|
294
363
|
raise ParseError, "Unknown packed-array sub-type: #{sub_type}"
|
|
295
364
|
end
|
|
296
365
|
end
|
|
297
366
|
|
|
298
|
-
private def
|
|
367
|
+
private def build_byte_order(v)
|
|
299
368
|
case v
|
|
300
369
|
when nil
|
|
301
370
|
:host
|
|
@@ -308,34 +377,33 @@ module Ynl
|
|
|
308
377
|
end
|
|
309
378
|
end
|
|
310
379
|
|
|
311
|
-
private def
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
raise ParseError, "Failed to parse struct member: #{v.fetch('name')}"
|
|
380
|
+
private def build_struct(d)
|
|
381
|
+
model = @structs.fetch(d.fetch('name'))
|
|
382
|
+
d.fetch('members').each do |member|
|
|
383
|
+
model.members << Models::Struct::Member.new(
|
|
384
|
+
name: member.fetch('name').dedup, type: build_struct_member_type(member),
|
|
385
|
+
doc: translate_doc(member['doc'])).freeze
|
|
386
|
+
rescue => error
|
|
387
|
+
raise ParseError, "Failed to parse struct member #{member.fetch('name')}: #{error.message}"
|
|
320
388
|
end
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
rescue
|
|
324
|
-
raise ParseError, "Failed to parse struct
|
|
389
|
+
model.members.freeze
|
|
390
|
+
model
|
|
391
|
+
rescue => error
|
|
392
|
+
raise ParseError, "Failed to parse struct #{d.fetch('name')}: #{error.message}"
|
|
325
393
|
end
|
|
326
394
|
|
|
327
|
-
private def
|
|
395
|
+
private def build_checks(d)
|
|
328
396
|
d.filter_map do |op, value|
|
|
329
|
-
|
|
330
|
-
end
|
|
397
|
+
build_check(op, value)
|
|
398
|
+
end.freeze
|
|
331
399
|
rescue
|
|
332
400
|
raise ParseError, "Failed to parse checks"
|
|
333
401
|
end
|
|
334
402
|
|
|
335
|
-
private def
|
|
403
|
+
private def build_check(op, value_literal)
|
|
336
404
|
case op
|
|
337
405
|
when 'max', 'min', 'min-len', 'max-len', 'exact-len'
|
|
338
|
-
Models::Check.new(operation: op, value:
|
|
406
|
+
Models::Check.new(operation: op.dedup, value: build_value(value_literal)).freeze
|
|
339
407
|
when 'unterminated-ok'
|
|
340
408
|
return nil
|
|
341
409
|
else
|
|
@@ -345,7 +413,7 @@ module Ynl
|
|
|
345
413
|
raise ParseError, "Failed to parse check: #{op}"
|
|
346
414
|
end
|
|
347
415
|
|
|
348
|
-
private def
|
|
416
|
+
private def build_value(v)
|
|
349
417
|
case v
|
|
350
418
|
when Integer
|
|
351
419
|
v
|
|
@@ -361,36 +429,51 @@ module Ynl
|
|
|
361
429
|
end
|
|
362
430
|
end
|
|
363
431
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
432
|
+
# Subsets depend on their parent's normalized contents, unlike ordinary
|
|
433
|
+
# references, which only need the target's identity. Expand that dependency
|
|
434
|
+
# separately so forward subsets work and inheritance cycles fail clearly.
|
|
435
|
+
private def attribute_definitions(name)
|
|
436
|
+
return @attribute_definitions.fetch(name) if @attribute_definitions.key?(name)
|
|
437
|
+
if @expanding_subsets.include?(name)
|
|
438
|
+
chain = (@expanding_subsets + [name]).join(' -> ')
|
|
439
|
+
raise ParseError, "Attribute subset inheritance cycle: #{chain}"
|
|
440
|
+
end
|
|
441
|
+
d = reference(@attribute_declarations, name, 'attribute set')
|
|
442
|
+
@expanding_subsets << name
|
|
443
|
+
begin
|
|
444
|
+
definitions = if parent_name = d['subset-of']
|
|
445
|
+
inherited = attribute_definitions(parent_name).to_h { [it.fetch('name'), it] }
|
|
446
|
+
d.fetch('attributes').uniq { it.fetch('name') }.map do |override|
|
|
447
|
+
parent = inherited.fetch(override.fetch('name'))
|
|
448
|
+
parent.merge(override).merge('value' => parent.fetch('value'))
|
|
449
|
+
end
|
|
450
|
+
else
|
|
451
|
+
value = 0
|
|
452
|
+
d.fetch('attributes').map do |attribute|
|
|
453
|
+
value = attribute.fetch('value', value + 1)
|
|
454
|
+
attribute.merge('value' => value)
|
|
455
|
+
end
|
|
382
456
|
end
|
|
383
|
-
|
|
457
|
+
@attribute_definitions[name] = definitions
|
|
458
|
+
ensure
|
|
459
|
+
@expanding_subsets.pop
|
|
384
460
|
end
|
|
461
|
+
end
|
|
385
462
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
463
|
+
private def build_attribute_set(d)
|
|
464
|
+
name = d.fetch('name')
|
|
465
|
+
model = @attribute_sets.fetch(name)
|
|
466
|
+
attribute_definitions(name).each do |definition|
|
|
467
|
+
attribute = build_attribute(definition)
|
|
468
|
+
model.attributes << attribute if attribute
|
|
469
|
+
end
|
|
470
|
+
model.attributes.freeze
|
|
471
|
+
model
|
|
389
472
|
rescue => error
|
|
390
473
|
raise ParseError, "Failed to parse attribute set #{d.fetch('name')}: #{error.message}"
|
|
391
474
|
end
|
|
392
475
|
|
|
393
|
-
private def
|
|
476
|
+
private def build_attribute(d)
|
|
394
477
|
multi_attr = d.fetch('multi-attr', false)
|
|
395
478
|
unless multi_attr == true || multi_attr == false
|
|
396
479
|
raise ParseError, "multi-attr must be a boolean"
|
|
@@ -404,104 +487,110 @@ module Ynl
|
|
|
404
487
|
raise ParseError, "enum-as-flags requires enum"
|
|
405
488
|
end
|
|
406
489
|
|
|
407
|
-
return unless type =
|
|
490
|
+
return unless type = build_attribute_type(d)
|
|
408
491
|
|
|
409
492
|
Models::AttributeSet::Attribute.new(
|
|
410
|
-
name: d.fetch('name'),
|
|
493
|
+
name: d.fetch('name').dedup,
|
|
411
494
|
type:,
|
|
412
495
|
value: d.fetch('value'),
|
|
413
|
-
checks: (
|
|
496
|
+
checks: (build_checks(d['checks']) if d['checks']),
|
|
414
497
|
doc: translate_doc(d['doc']),
|
|
415
498
|
multi_attr:,
|
|
416
|
-
enum: d['enum']&.then {
|
|
499
|
+
enum: d['enum']&.then { find_type(it) },
|
|
417
500
|
enum_as_flags:,
|
|
418
|
-
)
|
|
501
|
+
).freeze
|
|
419
502
|
rescue => error
|
|
420
503
|
raise ParseError, "Failed to parse attribute #{d.fetch('name')}: #{error.message}"
|
|
421
504
|
end
|
|
422
505
|
|
|
423
|
-
private def
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
attr_set = fmt['attribute-set']&.then do |ref|
|
|
431
|
-
Models::Thunk.new {|f| f.attribute_sets.fetch(ref) }
|
|
432
|
-
end
|
|
433
|
-
result.formats << Models::SubMessage::Format.new(fmt.fetch('value'), fixed_header, attr_set)
|
|
506
|
+
private def build_sub_message(d)
|
|
507
|
+
model = @sub_messages.fetch(d.fetch('name'))
|
|
508
|
+
d.fetch('formats', []).each do |format|
|
|
509
|
+
model.formats << Models::SubMessage::Format.new(
|
|
510
|
+
value: format.fetch('value').dedup,
|
|
511
|
+
fixed_header: format['fixed-header']&.then { reference(@structs, it, 'struct') },
|
|
512
|
+
attribute_set: format['attribute-set']&.then { reference(@attribute_sets, it, 'attribute set') }).freeze
|
|
434
513
|
end
|
|
435
|
-
|
|
514
|
+
model.formats.freeze
|
|
515
|
+
model
|
|
436
516
|
end
|
|
437
517
|
|
|
438
|
-
private def
|
|
439
|
-
name = d.fetch('name')
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
@
|
|
443
|
-
|
|
444
|
-
raise ParseError, "Undefined fixed header: #{it}"
|
|
445
|
-
end || @default_fixed_header
|
|
446
|
-
|
|
447
|
-
attribute_set = d['attribute-set']&.then do
|
|
448
|
-
@attribute_sets.fetch(it)
|
|
449
|
-
rescue
|
|
450
|
-
raise ParseError, "Undefined attribute set: #{it}"
|
|
518
|
+
private def build_operation(d, request_id:, reply_id:)
|
|
519
|
+
name = d.fetch('name').dedup
|
|
520
|
+
return @operations.fetch(name) if @operations.key?(name)
|
|
521
|
+
if @building_operations.include?(name)
|
|
522
|
+
chain = (@building_operations + [name]).join(' -> ')
|
|
523
|
+
raise ParseError, "Notification source cycle: #{chain}"
|
|
451
524
|
end
|
|
452
525
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
526
|
+
@building_operations << name
|
|
527
|
+
begin
|
|
528
|
+
fixed_header = d['fixed-header']&.then do
|
|
529
|
+
reference(@structs, it, 'struct')
|
|
530
|
+
end || @default_fixed_header
|
|
531
|
+
attribute_set = d['attribute-set']&.then do
|
|
532
|
+
reference(@attribute_sets, it, 'attribute set')
|
|
533
|
+
end
|
|
456
534
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
535
|
+
doit = d['do']&.then { build_request_reply(it, default_request_id: request_id, default_reply_id: reply_id) }
|
|
536
|
+
dumpit = d['dump']&.then { build_request_reply(it, default_request_id: request_id, default_reply_id: reply_id) }
|
|
537
|
+
notification = build_notification(d, default_id: reply_id)
|
|
538
|
+
flags = d.fetch('flags', [])
|
|
539
|
+
flags.map!(&:dedup).freeze unless flags.frozen?
|
|
540
|
+
|
|
541
|
+
@operations[name] = Models::Operation.new(
|
|
542
|
+
name:, doc: translate_doc(d['doc']), flags:,
|
|
543
|
+
fixed_header:, attribute_set:, doit:, dumpit:, notification:,
|
|
544
|
+
).freeze
|
|
545
|
+
ensure
|
|
546
|
+
@building_operations.pop
|
|
547
|
+
end
|
|
461
548
|
end
|
|
462
549
|
|
|
463
|
-
private def
|
|
550
|
+
private def build_notification(d, default_id:)
|
|
464
551
|
if source = d['notify']
|
|
552
|
+
declaration, request_id, reply_id = reference(@operation_declarations, source, 'operation')
|
|
553
|
+
source_operation = build_operation(declaration, request_id:, reply_id:)
|
|
465
554
|
Models::Notification.new(
|
|
466
555
|
kind: :notify,
|
|
467
|
-
message:
|
|
468
|
-
source
|
|
469
|
-
group: d['mcgrp'],
|
|
470
|
-
)
|
|
556
|
+
message: build_message({}, default_id:),
|
|
557
|
+
source: source_operation,
|
|
558
|
+
group: d['mcgrp']&.then { reference(@mcast_groups, it, 'multicast group') },
|
|
559
|
+
).freeze
|
|
471
560
|
elsif event = d['event']
|
|
472
561
|
Models::Notification.new(
|
|
473
562
|
kind: :event,
|
|
474
|
-
message:
|
|
563
|
+
message: build_message(event, default_id:),
|
|
475
564
|
source: nil,
|
|
476
|
-
group: d['mcgrp'],
|
|
477
|
-
)
|
|
565
|
+
group: d['mcgrp']&.then { reference(@mcast_groups, it, 'multicast group') },
|
|
566
|
+
).freeze
|
|
478
567
|
end
|
|
479
568
|
end
|
|
480
569
|
|
|
481
|
-
private def
|
|
570
|
+
private def build_request_reply(d, default_request_id:, default_reply_id:)
|
|
482
571
|
Models::RequestReply.new(
|
|
483
|
-
request:
|
|
484
|
-
reply: d['reply']&.then {
|
|
485
|
-
)
|
|
572
|
+
request: build_message(d.fetch('request', {}), default_id: default_request_id),
|
|
573
|
+
reply: d['reply']&.then { build_message(it, default_id: default_reply_id) },
|
|
574
|
+
).freeze
|
|
486
575
|
end
|
|
487
576
|
|
|
488
|
-
private def
|
|
489
|
-
|
|
577
|
+
private def build_message(d, default_id:)
|
|
578
|
+
attributes = d.fetch('attributes', [])
|
|
579
|
+
attributes.map!(&:dedup).freeze unless attributes.frozen?
|
|
580
|
+
Models::Message.new(value: d['value'] || default_id, attributes:).freeze
|
|
490
581
|
end
|
|
491
582
|
|
|
492
|
-
private def
|
|
493
|
-
name = d.fetch('name')
|
|
583
|
+
private def build_mcast_group(d)
|
|
584
|
+
name = d.fetch('name').dedup
|
|
494
585
|
@mcast_groups[name] = Models::McastGroup.new(
|
|
495
|
-
name,
|
|
496
|
-
|
|
497
|
-
translate_doc(d['doc']),
|
|
498
|
-
)
|
|
586
|
+
name:, value: d['value'], doc: translate_doc(d['doc']),
|
|
587
|
+
).freeze
|
|
499
588
|
end
|
|
500
589
|
|
|
501
590
|
private def translate_doc(doc)
|
|
502
591
|
return unless doc
|
|
503
592
|
|
|
504
|
-
doc.gsub(/(?:\s|^)\K@(?<ident>[\w.-]+)/i) { "`#{$~[:ident]}`" }
|
|
593
|
+
doc.gsub(/(?:\s|^)\K@(?<ident>[\w.-]+)/i) { "`#{$~[:ident]}`" }.dedup
|
|
505
594
|
end
|
|
506
595
|
end
|
|
507
596
|
end
|