cddlc 0.2.7 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/cddlc +7 -1
- data/cddlc.gemspec +1 -1
- data/lib/cddlc.rb +39 -8
- data/lib/parser/cddlgrammar.rb +99 -23
- data/lib/processor/cddl-visitor.rb +4 -0
- data/lib/writer/cddl-writer.rb +16 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a38ea6117afabc57b950af7598c2d497bb00f587ec937cbe41b45b33f3775dfe
|
4
|
+
data.tar.gz: f726477776dee7586f5f85909473cfb22acc38630cf45995c732d2df7a842ebf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 332fcf064ebfc558cc82908832575a3fd4f77219f6f1ceb46ffab2270ee4f9273ecc7d83edcc5dc6dc574ef4ccefdbf9fd7c25d10a9f88466dc4e0ca7ae3dc32
|
7
|
+
data.tar.gz: 3bbb56ffb6853eaab2792e5518914128ecf734bff0db4651861227bd77eaf4241e29b11306e04937855e46d67c05f3dd55cdde91e86da5ef2d5cc9f7fd4edb96
|
data/bin/cddlc
CHANGED
@@ -44,7 +44,7 @@ require 'ostruct'
|
|
44
44
|
$options = OpenStruct.new
|
45
45
|
begin
|
46
46
|
op = OptionParser.new do |opts|
|
47
|
-
opts.banner = "Usage: cddlc.rb [options] [file.cddl... | -]"
|
47
|
+
opts.banner = "Usage: cddlc.rb [options] [-e cddl | file.cddl... | -]"
|
48
48
|
|
49
49
|
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
|
50
50
|
$options.verbose = v
|
@@ -70,6 +70,9 @@ begin
|
|
70
70
|
opts.on("-sRULE", "--start=RULE", String, "Start rule name") do |v|
|
71
71
|
$options.start = v
|
72
72
|
end
|
73
|
+
opts.on("-eCDDL", "CDDL model on command line") do |v|
|
74
|
+
$options.model = v
|
75
|
+
end
|
73
76
|
opts.on("-iIMPORT", "--import=IMPORT", String, "Import [namespace=]reference") do |v|
|
74
77
|
$options.import ||= []
|
75
78
|
$options.import << v
|
@@ -94,6 +97,9 @@ cddl_file = ""
|
|
94
97
|
if $options.start
|
95
98
|
cddl_file << "$.start.$ = #{$options.start}\n"
|
96
99
|
end
|
100
|
+
if m = $options.model
|
101
|
+
cddl_file << m << "\n"
|
102
|
+
end
|
97
103
|
|
98
104
|
[[$options.include, "include"], [$options.import, "import"]].each do |arr, directive|
|
99
105
|
if arr
|
data/cddlc.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "cddlc"
|
3
|
-
s.version = "0.2
|
3
|
+
s.version = "0.3.2"
|
4
4
|
s.summary = "CDDL (Concise Data Definition Language) converters and miscellaneous tools"
|
5
5
|
s.description = %q{cddlc implements converters and miscellaneous tools for CDDL, RFC 8610}
|
6
6
|
s.author = "Carsten Bormann"
|
data/lib/cddlc.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require_relative "parser/cddl-util.rb"
|
2
2
|
require_relative "processor/cddl-visitor.rb"
|
3
3
|
require_relative 'processor/cddl-undefined.rb'
|
4
|
+
require_relative 'writer/cddl-writer.rb'
|
4
5
|
|
5
6
|
class CDDL
|
6
7
|
@@parser = CDDLGRAMMARParser.new
|
@@ -226,7 +227,8 @@ class CDDL
|
|
226
227
|
end
|
227
228
|
|
228
229
|
|
229
|
-
RULE_OP_TO_CHOICE = {"/=" => "tcho", "//=" => "gcho"}
|
230
|
+
RULE_OP_TO_CHOICE = {"/=" => ["tcho", "tadd"], "//=" => ["gcho", "gadd"]}
|
231
|
+
CHOICERULES = RULE_OP_TO_CHOICE.values.flatten
|
230
232
|
|
231
233
|
def rules
|
232
234
|
if @rules.nil? # memoize
|
@@ -258,27 +260,56 @@ class CDDL
|
|
258
260
|
else
|
259
261
|
fail name
|
260
262
|
end
|
261
|
-
@rules[name] =
|
263
|
+
@rules[name] = nv =
|
262
264
|
if (old = @rules[name]) && old != val
|
263
|
-
fail "
|
264
|
-
if Array === old && old[0]
|
265
|
-
|
265
|
+
fail "overwriting #{write_rule(name, old)} ...with... #{write_rule(name, val)}" unless cho
|
266
|
+
if Array === old && cho.include?(old[0])
|
267
|
+
if cho.include?(val[0])
|
268
|
+
old.dup.append(*val[1..-1])
|
269
|
+
else
|
270
|
+
old.dup << val
|
271
|
+
end
|
266
272
|
else
|
267
|
-
|
273
|
+
# can't put an old "g/tadd" into a new "t/gcho"
|
274
|
+
fail "can't add #{write_rule(name, [cho[1], val])} ...to... #{write_rule(name, old)}" if CHOICERULES.include?(old[0])
|
275
|
+
[cho[0], old, val] # old might need to be packaged for gcho
|
268
276
|
end
|
269
277
|
else
|
270
|
-
|
278
|
+
if cho
|
279
|
+
if val[0] == cho[0]
|
280
|
+
[cho[1], *val[1..-1]]
|
281
|
+
else
|
282
|
+
[cho[1], val]
|
283
|
+
end
|
284
|
+
else
|
285
|
+
val
|
286
|
+
end
|
271
287
|
end
|
288
|
+
if name[0..1] == "$$"
|
289
|
+
check_socket(name, nv, "gadd", "tadd", "tcho", "type", "group")
|
290
|
+
elsif name[0] == "$"
|
291
|
+
check_socket(name, nv, "tadd", "gadd", "gcho", "group", "type")
|
292
|
+
end
|
272
293
|
end
|
273
294
|
# warn "** rules #{rules.inspect}"
|
274
295
|
end
|
275
296
|
@rules
|
276
297
|
end
|
277
298
|
|
299
|
+
def check_socket(name, nv, right, wrong, wrong_plain, wrong_kind, kind)
|
300
|
+
unless nv[0] == right
|
301
|
+
if nv[0] == wrong
|
302
|
+
flaw = "#{wrong_kind} choice #{write_rhs([wrong_plain, *nv[1..-1]], 2.1)} in"
|
303
|
+
else
|
304
|
+
flaw = "plain assignment of #{write_rhs(nv, 2.1)} to"
|
305
|
+
end
|
306
|
+
warn "** warning: #{flaw} #{kind} socket #{name}"
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
278
310
|
def prelude
|
279
311
|
if @prelude.nil?
|
280
312
|
@prelude = CDDL.from_cddl(File.read(DATA_DIR + "prelude.cddl"))
|
281
|
-
|
282
313
|
end
|
283
314
|
@prelude
|
284
315
|
end
|
data/lib/parser/cddlgrammar.rb
CHANGED
@@ -947,7 +947,7 @@ module CDDLGRAMMAR
|
|
947
947
|
end
|
948
948
|
|
949
949
|
module Type214
|
950
|
-
def
|
950
|
+
def head_number
|
951
951
|
elements[1]
|
952
952
|
end
|
953
953
|
end
|
@@ -975,26 +975,42 @@ module CDDLGRAMMAR
|
|
975
975
|
end
|
976
976
|
|
977
977
|
module Type217
|
978
|
-
def
|
978
|
+
def head_number
|
979
979
|
elements[1]
|
980
980
|
end
|
981
981
|
end
|
982
982
|
|
983
983
|
module Type218
|
984
|
+
end
|
985
|
+
|
986
|
+
module Type219
|
987
|
+
def ast
|
988
|
+
el = elements[2].elements
|
989
|
+
["prim", 7, *el && [el[1].ast]]
|
990
|
+
end
|
991
|
+
end
|
992
|
+
|
993
|
+
module Type220
|
994
|
+
def uint
|
995
|
+
elements[1]
|
996
|
+
end
|
997
|
+
end
|
998
|
+
|
999
|
+
module Type221
|
984
1000
|
def DIGIT
|
985
1001
|
elements[1]
|
986
1002
|
end
|
987
1003
|
|
988
1004
|
end
|
989
1005
|
|
990
|
-
module
|
1006
|
+
module Type222
|
991
1007
|
def ast
|
992
1008
|
["prim", elements[1].text_value.to_i,
|
993
1009
|
*(el = elements[2].elements) ? [el[1].text_value.to_i] : []]
|
994
1010
|
end
|
995
1011
|
end
|
996
1012
|
|
997
|
-
module
|
1013
|
+
module Type223
|
998
1014
|
def ast() ["prim"] end
|
999
1015
|
end
|
1000
1016
|
|
@@ -1330,7 +1346,7 @@ module CDDLGRAMMAR
|
|
1330
1346
|
end
|
1331
1347
|
s48 << r49
|
1332
1348
|
if r49
|
1333
|
-
r50 =
|
1349
|
+
r50 = _nt_head_number
|
1334
1350
|
s48 << r50
|
1335
1351
|
end
|
1336
1352
|
if s48.last
|
@@ -1402,7 +1418,13 @@ module CDDLGRAMMAR
|
|
1402
1418
|
end
|
1403
1419
|
s56 << r57
|
1404
1420
|
if r57
|
1405
|
-
|
1421
|
+
if (match_len = has_terminal?("7", false, index))
|
1422
|
+
r58 = true
|
1423
|
+
@index += match_len
|
1424
|
+
else
|
1425
|
+
terminal_parse_failure('"7"')
|
1426
|
+
r58 = nil
|
1427
|
+
end
|
1406
1428
|
s56 << r58
|
1407
1429
|
if r58
|
1408
1430
|
i60, s60 = index, []
|
@@ -1415,7 +1437,7 @@ module CDDLGRAMMAR
|
|
1415
1437
|
end
|
1416
1438
|
s60 << r61
|
1417
1439
|
if r61
|
1418
|
-
r62 =
|
1440
|
+
r62 = _nt_head_number
|
1419
1441
|
s60 << r62
|
1420
1442
|
end
|
1421
1443
|
if s60.last
|
@@ -1445,20 +1467,74 @@ module CDDLGRAMMAR
|
|
1445
1467
|
r56 = SyntaxNode.new(input, (index-1)...index) if r56 == true
|
1446
1468
|
r0 = r56
|
1447
1469
|
else
|
1470
|
+
i63, s63 = index, []
|
1448
1471
|
if (match_len = has_terminal?("#", false, index))
|
1449
|
-
|
1450
|
-
r63.extend(Type220)
|
1472
|
+
r64 = true
|
1451
1473
|
@index += match_len
|
1452
1474
|
else
|
1453
1475
|
terminal_parse_failure('"#"')
|
1476
|
+
r64 = nil
|
1477
|
+
end
|
1478
|
+
s63 << r64
|
1479
|
+
if r64
|
1480
|
+
r65 = _nt_DIGIT
|
1481
|
+
s63 << r65
|
1482
|
+
if r65
|
1483
|
+
i67, s67 = index, []
|
1484
|
+
if (match_len = has_terminal?(".", false, index))
|
1485
|
+
r68 = true
|
1486
|
+
@index += match_len
|
1487
|
+
else
|
1488
|
+
terminal_parse_failure('"."')
|
1489
|
+
r68 = nil
|
1490
|
+
end
|
1491
|
+
s67 << r68
|
1492
|
+
if r68
|
1493
|
+
r69 = _nt_uint
|
1494
|
+
s67 << r69
|
1495
|
+
end
|
1496
|
+
if s67.last
|
1497
|
+
r67 = instantiate_node(SyntaxNode,input, i67...index, s67)
|
1498
|
+
r67.extend(Type220)
|
1499
|
+
else
|
1500
|
+
@index = i67
|
1501
|
+
r67 = nil
|
1502
|
+
end
|
1503
|
+
if r67
|
1504
|
+
r66 = r67
|
1505
|
+
else
|
1506
|
+
r66 = instantiate_node(SyntaxNode,input, index...index)
|
1507
|
+
end
|
1508
|
+
s63 << r66
|
1509
|
+
end
|
1510
|
+
end
|
1511
|
+
if s63.last
|
1512
|
+
r63 = instantiate_node(SyntaxNode,input, i63...index, s63)
|
1513
|
+
r63.extend(Type221)
|
1514
|
+
r63.extend(Type222)
|
1515
|
+
else
|
1516
|
+
@index = i63
|
1454
1517
|
r63 = nil
|
1455
1518
|
end
|
1456
1519
|
if r63
|
1457
1520
|
r63 = SyntaxNode.new(input, (index-1)...index) if r63 == true
|
1458
1521
|
r0 = r63
|
1459
1522
|
else
|
1460
|
-
|
1461
|
-
|
1523
|
+
if (match_len = has_terminal?("#", false, index))
|
1524
|
+
r70 = instantiate_node(SyntaxNode,input, index...(index + match_len))
|
1525
|
+
r70.extend(Type223)
|
1526
|
+
@index += match_len
|
1527
|
+
else
|
1528
|
+
terminal_parse_failure('"#"')
|
1529
|
+
r70 = nil
|
1530
|
+
end
|
1531
|
+
if r70
|
1532
|
+
r70 = SyntaxNode.new(input, (index-1)...index) if r70 == true
|
1533
|
+
r0 = r70
|
1534
|
+
else
|
1535
|
+
@index = i0
|
1536
|
+
r0 = nil
|
1537
|
+
end
|
1462
1538
|
end
|
1463
1539
|
end
|
1464
1540
|
end
|
@@ -1476,27 +1552,27 @@ module CDDLGRAMMAR
|
|
1476
1552
|
r0
|
1477
1553
|
end
|
1478
1554
|
|
1479
|
-
module
|
1555
|
+
module HeadNumber0
|
1480
1556
|
def ast() text_value.to_i end
|
1481
1557
|
end
|
1482
1558
|
|
1483
|
-
module
|
1559
|
+
module HeadNumber1
|
1484
1560
|
def type
|
1485
1561
|
elements[1]
|
1486
1562
|
end
|
1487
1563
|
|
1488
1564
|
end
|
1489
1565
|
|
1490
|
-
module
|
1566
|
+
module HeadNumber2
|
1491
1567
|
def ast() elements[1].ast end
|
1492
1568
|
end
|
1493
1569
|
|
1494
|
-
def
|
1570
|
+
def _nt_head_number
|
1495
1571
|
start_index = index
|
1496
|
-
if node_cache[:
|
1497
|
-
cached = node_cache[:
|
1572
|
+
if node_cache[:head_number].has_key?(index)
|
1573
|
+
cached = node_cache[:head_number][index]
|
1498
1574
|
if cached
|
1499
|
-
node_cache[:
|
1575
|
+
node_cache[:head_number][index] = cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
|
1500
1576
|
@index = cached.interval.end
|
1501
1577
|
end
|
1502
1578
|
return cached
|
@@ -1504,8 +1580,8 @@ module CDDLGRAMMAR
|
|
1504
1580
|
|
1505
1581
|
i0 = index
|
1506
1582
|
r1 = _nt_uint
|
1507
|
-
r1.extend(
|
1508
|
-
r1.extend(
|
1583
|
+
r1.extend(HeadNumber0)
|
1584
|
+
r1.extend(HeadNumber0)
|
1509
1585
|
if r1
|
1510
1586
|
r1 = SyntaxNode.new(input, (index-1)...index) if r1 == true
|
1511
1587
|
r0 = r1
|
@@ -1535,8 +1611,8 @@ module CDDLGRAMMAR
|
|
1535
1611
|
end
|
1536
1612
|
if s2.last
|
1537
1613
|
r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
|
1538
|
-
r2.extend(
|
1539
|
-
r2.extend(
|
1614
|
+
r2.extend(HeadNumber1)
|
1615
|
+
r2.extend(HeadNumber2)
|
1540
1616
|
else
|
1541
1617
|
@index = i2
|
1542
1618
|
r2 = nil
|
@@ -1550,7 +1626,7 @@ module CDDLGRAMMAR
|
|
1550
1626
|
end
|
1551
1627
|
end
|
1552
1628
|
|
1553
|
-
node_cache[:
|
1629
|
+
node_cache[:head_number][start_index] = r0
|
1554
1630
|
|
1555
1631
|
r0
|
1556
1632
|
end
|
@@ -25,6 +25,10 @@ class CDDL
|
|
25
25
|
["gcho", *visit_all(prods, &block)]
|
26
26
|
in ["tcho", *prods]
|
27
27
|
["tcho", *visit_all(prods, &block)]
|
28
|
+
in ["gadd", *prods]
|
29
|
+
["gadd", *visit_all(prods, &block)]
|
30
|
+
in ["tcho", *prods]
|
31
|
+
["tcho", *visit_all(prods, &block)]
|
28
32
|
in ["seq", *prods]
|
29
33
|
["seq", *visit_all(prods, &block)]
|
30
34
|
in ["enum", prod]
|
data/lib/writer/cddl-writer.rb
CHANGED
@@ -64,8 +64,10 @@ class CDDL
|
|
64
64
|
[4, "#"]
|
65
65
|
in ["prim", maj]
|
66
66
|
[4, "##{maj}"]
|
67
|
-
in ["prim", maj, min]
|
67
|
+
in ["prim", maj, Integer => min]
|
68
68
|
[4, "##{maj}.#{min}"]
|
69
|
+
in ["prim", 7, Array => min]
|
70
|
+
[4, "##{maj}.<#{write_rhs(min, 0, indent, pn)}>"]
|
69
71
|
in ["prim", 6, Integer => tag, type]
|
70
72
|
[4, "#6.#{tag}(#{write_rhs(type, 0, indent, pn)})"]
|
71
73
|
in ["prim", 6, Array => tag, type]
|
@@ -109,15 +111,24 @@ class CDDL
|
|
109
111
|
prec_check(ret, targetprec, prec, indent_s)
|
110
112
|
end
|
111
113
|
|
112
|
-
def
|
113
|
-
rules.map {|k, v|
|
114
|
+
def write_rule(k, v)
|
114
115
|
parmnames = false
|
116
|
+
assign = "="
|
115
117
|
case v
|
118
|
+
in ["tadd", *rest]
|
119
|
+
assign = "/="
|
120
|
+
v = ["tcho", *rest]
|
121
|
+
in ["gadd", *rest]
|
122
|
+
assign = "//="
|
123
|
+
v = ["gcho", *rest]
|
116
124
|
in ["parm", parmnames, _type]
|
117
125
|
else
|
118
126
|
end
|
119
|
-
"#{write_lhs(k, parmnames)}
|
120
|
-
|
127
|
+
"#{write_lhs(k, parmnames)} #{assign} #{write_rhs(v, 2.1)}" # 2: parenthesize groups
|
128
|
+
end
|
129
|
+
|
130
|
+
def to_s
|
131
|
+
rules.map {|k, v| write_rule(k, v) }.join("\n")
|
121
132
|
end
|
122
133
|
|
123
134
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cddlc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carsten Bormann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|