cddl 0.6.4 → 0.6.5

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
  SHA1:
3
- metadata.gz: 57c85df19d5a0b7d9070f233f0d4a59a3cee1bb2
4
- data.tar.gz: ac391af699e243e95ae1fce85de5fdf9c863a24d
3
+ metadata.gz: f786ac685c1f30ca4a1510ad4e40981a6403dc7f
4
+ data.tar.gz: 8d2a5a22918683e260bbd79331e1b9874ea649a1
5
5
  SHA512:
6
- metadata.gz: 121cbda92cc8112171d3b7395373fe09e502786a90887e2b54dd2f7639f8082a5197b5f64d6b8fa8c157555c46e476e7a81f463fd33b2f5f085cb5b5b74b0908
7
- data.tar.gz: 0ed8f5e200aabf211c5489cd66487bf22e5bcde1d06894795cb36341d4e848d9f479e77cbaa30d6e81d6fd28d66b9fe9cec724b460ae9fd357d3dd981b4995ee
6
+ metadata.gz: 2055bf34a4f4b366b12455723dcc86eba307064c0831dad03d864f7f6a014eab724b9283917341f88c6daf8a08ecb9d4808f269161b7127404d689684f5165db
7
+ data.tar.gz: b3693cc1e1277a2f7848fb886ed1665cfc885438da1d2e3fd7d94355a6081265eaa62fd4db3fafea38ade41e708d0515c51a0c0001ad453ccf711009adc8b6d4
@@ -1,6 +1,6 @@
1
1
  spec = Gem::Specification.new do |s|
2
2
  s.name = 'cddl'
3
- s.version = '0.6.4'
3
+ s.version = '0.6.5'
4
4
  s.summary = "CDDL generator and validator."
5
5
  s.description = %{A parser, generator, and validator for CDDL}
6
6
  s.add_dependency('cbor-diag')
@@ -70,21 +70,86 @@ module CDDL
70
70
  }]
71
71
  end
72
72
 
73
+ def walk(rule, anno = nil, &block)
74
+ r = []
75
+ case rule
76
+ when Array
77
+ r.concat(Array(yield rule, anno))
78
+ case rule[0]
79
+ when :type1, :array, :map
80
+ a = (rule.cbor_annotations rescue nil) if rule.size == 2
81
+ a = a.first if a
82
+ r.concat(rule[1..-1].map{|x| walk(x, a, &block)})
83
+ when :grpchoice
84
+ r.concat(rule[1..-1].map{|x| x.flat_map {|y| walk(y, &block)}})
85
+ when :member
86
+ r << walk(rule[3], &block)
87
+ r << walk(rule[4], &block)
88
+ when :anno
89
+ r << walk(rule[2], &block)
90
+ r << walk(rule[3], &block)
91
+ else
92
+ # p ["LEAF", rule[0]]
93
+ end
94
+ end
95
+ r.compact
96
+ end
97
+
98
+ def cname(s)
99
+ s.to_s.gsub(/-/, "_")
100
+ end
101
+
73
102
  # Generate some simple #define lines from the value-only rules
74
103
  def defines(prefix)
75
- s = ''
104
+ s = {} # keys form crude set of defines
105
+ r = rules
76
106
  prefix ||= "CDDL"
77
107
  ast.each :rule do |rule|
78
108
  if rulename = rule.typename
79
109
  t = rule.type.children(:type1)
80
110
  if t.size == 1
81
111
  if (t2 = t.first.children(:type2)) && t2.size == 1 && (v = t2.first.value)
82
- s += "#define #{prefix}_#{rulename.to_s.gsub(/-/, "_")} " << v.to_s << "\n"
112
+ s["#define #{prefix}_#{cname(rulename)} " << v.to_s << "\n"] = true
113
+ end
114
+ end
115
+ end
116
+ end
117
+ # CBOR::PP.pp r
118
+ walk(r) do |subtree, anno|
119
+ if subtree[0] == :type1 && subtree[1..-1].all? {|x| x[0] == :int}
120
+ if enumname = subtree.cbor_annotations rescue nil
121
+ enumname = cname(enumname.first)
122
+ subtree[1..-1].each do |x|
123
+ if memname = x.cbor_annotations
124
+ memname = "#{enumname}_#{cname(memname.first)}"
125
+ s["#define #{prefix}_#{memname} " << x[1].to_s << "\n"] = true
126
+ end
127
+ end
128
+ end
129
+ end
130
+ if subtree[0] == :array
131
+ if (arrayname = subtree.cbor_annotations rescue nil) || anno
132
+ arrayname = cname(arrayname ? arrayname.first : anno)
133
+ subtree[1..-1].each_with_index do |x, i|
134
+ if x[0] == :member
135
+ if Array === x[3] && x[3][0] == :string
136
+ memname = x[3][1] # preferably use key string
137
+ elsif memname = x[4].cbor_annotations
138
+ memname = memname.first # use value annotation otherwise
139
+ end
140
+ if memname
141
+ memname = "#{arrayname}_#{cname(memname)}"
142
+ s["#define #{prefix}_#{memname}_index " << i.to_s << "\n"] = true
143
+ end
144
+ end
145
+ if x[0] == :member && (x[1] != 1 || x[2] != 1)
146
+ break # can't give numbers if we have optionals etc.
147
+ end
83
148
  end
84
149
  end
85
150
  end
86
151
  end
87
- s
152
+ s.keys.join
88
153
  end
89
154
 
90
155
  def rules
@@ -0,0 +1,14 @@
1
+ message = [mtype,
2
+ beer: enum1, wine: bytes, ? optional: text, cantindex: int]
3
+
4
+ M_ONE = 1
5
+ M_TWO = 2
6
+ M_THREE = 3
7
+
8
+ mtype = M_ONE / M_TWO / M_THREE
9
+
10
+ enum1 = &(
11
+ one: 1
12
+ two: 2
13
+ three: 3
14
+ )
@@ -0,0 +1,90 @@
1
+ grasp-message = message
2
+
3
+ session-id = 0..16777215
4
+ ; that is up to 24 bits
5
+
6
+ message /= discovery-message
7
+ discovery-message = [M_DISCOVERY, session-id, objective]
8
+
9
+ message /= response-message
10
+ response-message = [M_RESPONSE, session-id,
11
+ (+locator-option // divert-option // objective)]
12
+
13
+ message /= request-message
14
+ request-message = [M_REQUEST, session-id, objective]
15
+
16
+ message /= negotiation-message
17
+ negotiation-message = [M_NEGOTIATE, sid: session-id, objective]
18
+
19
+ message /= end-message
20
+ end-message = [M_END, session-id, (accept-option / decline-option)]
21
+
22
+ message /= wait-message
23
+ wait-message = [M_WAIT, session-id, waiting-time-option]
24
+
25
+ divert-option = [O_DIVERT, +locator-option]
26
+
27
+ accept-option = [O_ACCEPT]
28
+
29
+ decline-option = [O_DECLINE]
30
+
31
+ waiting-time-option = [O_WAITING, option-waiting-time]
32
+ option-waiting-time = 0..4294967295 ; in milliseconds
33
+
34
+ option-device-id = [O_DEVICE_ID, bytes]
35
+
36
+ locator-option /= ipv4-locator-option
37
+ ipv4-locator-option = bytes .size 4
38
+ ; this is simpler than [O_IPv4_LOCATOR, bytes .size 4]
39
+
40
+ locator-option /= ipv6-locator-option
41
+ ipv6-locator-option = bytes .size 16
42
+
43
+ locator-option /= fqdn-locator-option
44
+ fqdn-locator-option = [O_FQDN_LOCATOR, text]
45
+
46
+ locator-option /= url-locator-option
47
+ url-locator-option = [O_URL_LOCATOR, text]
48
+
49
+ objective-flags = uint .bits objective-flag
50
+
51
+ objective-flag = &(
52
+ D: 0
53
+ N: 1
54
+ S: 2
55
+ )
56
+
57
+ ; D means valid for discovery only
58
+ ; N means valid for discovery and negotiation
59
+ ; S means valid for discovery and synchronization
60
+
61
+ objective /= generic-obj
62
+ generic-obj = [objective-name, objective-flags, loop-count, ?any]
63
+
64
+ objective /= vendor-obj
65
+ vendor-obj = [{"PEN":pen}, objective-name, objective-flags,
66
+ loop-count, ?any]
67
+
68
+ ; A PEN is used to distinguish vendor-specific options.
69
+
70
+ pen = 0..4294967295
71
+ objective-name = tstr
72
+ loop-count = 0..255
73
+
74
+ ; Constants
75
+
76
+ M_DISCOVERY = 1
77
+
78
+ M_RESPONSE = 2
79
+ M_REQUEST = 3
80
+ M_NEGOTIATE = 4
81
+ M_END = 5
82
+ M_WAIT = 6
83
+
84
+ O_DIVERT = 100
85
+ O_ACCEPT = 101
86
+ O_DECLINE = 102
87
+ O_WAITING = 103
88
+ O_DEVICE_ID = 104
89
+ O_FQDN_LOCATOR = 105
90
+ O_URL_LOCATOR = 106
@@ -1417,4 +1417,84 @@ HERE
1417
1417
  assert_equal g, {"a" => nil}
1418
1418
  end
1419
1419
 
1420
+ def test_extractor_ignores_map
1421
+ parser = CDDL::Parser.new <<HERE
1422
+ a = {a: nil,
1423
+ beer: mtype, wine: bytes, ? optional: text, cantindex: int}
1424
+ mtype = 1/2/3 ; can't generate anything meaningful here.
1425
+ HERE
1426
+ d = parser.defines("FOO")
1427
+ assert_equal "", d
1428
+ end
1429
+
1430
+ def test_extractor_type1
1431
+ parser = CDDL::Parser.new <<HERE
1432
+ BAR = a/b/c
1433
+ a = 1
1434
+ b = 2
1435
+ c = 3
1436
+ d = 4
1437
+ HERE
1438
+ expected = <<HERE
1439
+ #define FOO_a 1
1440
+ #define FOO_b 2
1441
+ #define FOO_c 3
1442
+ #define FOO_d 4
1443
+ #define FOO_BAR_a 1
1444
+ #define FOO_BAR_b 2
1445
+ #define FOO_BAR_c 3
1446
+ HERE
1447
+ d = parser.defines("FOO")
1448
+ assert_equal expected, d
1449
+ end
1450
+
1451
+ def test_extractor_enum
1452
+ parser = CDDL::Parser.new <<HERE
1453
+
1454
+ foo = enum1; why exactly do we need this one?
1455
+
1456
+ enum1 = &(
1457
+ one: 1
1458
+ two: 2
1459
+ three: 3
1460
+ enum2
1461
+ )
1462
+
1463
+ enum2 = (
1464
+ four: 4
1465
+ five: 5
1466
+ )
1467
+
1468
+ HERE
1469
+ expected = <<HERE
1470
+ #define FOO_enum1_one 1
1471
+ #define FOO_enum1_two 2
1472
+ #define FOO_enum1_three 3
1473
+ #define FOO_enum1_four 4
1474
+ #define FOO_enum1_five 5
1475
+ HERE
1476
+ d = parser.defines("FOO")
1477
+ assert_equal expected, d
1478
+ end
1479
+
1480
+ def test_extractor_array
1481
+ parser = CDDL::Parser.new <<HERE
1482
+ message = [mtype,
1483
+ beer: enum1, wine: bytes, ? optional: text, cantindex: int]
1484
+ enum1 = &( one: 1, two: 2)
1485
+ mtype = 1/2/3 ; can't generate anything meaningful here.
1486
+ HERE
1487
+ expected = <<HERE
1488
+ #define FOO_message_mtype_index 0
1489
+ #define FOO_message_beer_index 1
1490
+ #define FOO_message_wine_index 2
1491
+ #define FOO_message_optional_index 3
1492
+ #define FOO_enum1_one 1
1493
+ #define FOO_enum1_two 2
1494
+ HERE
1495
+ d = parser.defines("FOO")
1496
+ assert_equal expected, d
1497
+ end
1498
+
1499
+
1420
1500
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cddl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-18 00:00:00.000000000 Z
11
+ date: 2015-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cbor-diag
@@ -102,6 +102,8 @@ files:
102
102
  - test-data/cdni-ct.cddl
103
103
  - test-data/dcaf.cddl
104
104
  - test-data/dcaf1.cddl
105
+ - test-data/extractor-demo.cddl
106
+ - test-data/grasp-01-test.cddl
105
107
  - test-data/grasp-01.cddl
106
108
  - test-data/grasp-v1.cddl
107
109
  - test-data/grasp-v2X.cddl