cddlc 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/cddlc +14 -1
- data/cddlc.gemspec +1 -1
- data/lib/processor/cddl-flattening.rb +99 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d56a7033911531ad73966b0807a1c6b9d9c09b9b09638d43274337e067a02fa
|
4
|
+
data.tar.gz: 609ab555ddc49c64a1dde396b99e01b2c79fd9a4fb63b1efb19ccd0ba3a311c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c704b7fbc908dd1449a6c267936bea906f689852d54b54291fc8a2bc4ca62c39b2e947223da42069343e5d1b28e997907ffeb451d528c5436a7a96f82de17498
|
7
|
+
data.tar.gz: 8e9f965b91b31eae2986a5e4c621dc0048bb64308017ec917d1a7b90853b9c6322674eaf4ef034905c8f0c555791e5664112cb905b03255fe72b857f57830f67
|
data/bin/cddlc
CHANGED
@@ -55,6 +55,9 @@ begin
|
|
55
55
|
opts.on("-x", "--[no-]expand", "Expand generics") do |v|
|
56
56
|
$options.expand_generics = v
|
57
57
|
end
|
58
|
+
opts.on("-f", "--[no-]flattening", "Flattening") do |v|
|
59
|
+
$options.flattening = v
|
60
|
+
end
|
58
61
|
opts.on("-u", "--[no-]undefined", "Note undefined names") do |v|
|
59
62
|
$options.note_undefined = v
|
60
63
|
end
|
@@ -82,6 +85,11 @@ rescue Exception => e
|
|
82
85
|
exit 1
|
83
86
|
end
|
84
87
|
|
88
|
+
if ($options.expand_generics || $options.flattening) && !$options.rules
|
89
|
+
warn "** can't expand or flatten in tree; adding -r flag"
|
90
|
+
$options.rules = true
|
91
|
+
end
|
92
|
+
|
85
93
|
cddl_file = ""
|
86
94
|
if $options.start
|
87
95
|
cddl_file << "$.start.$ = #{$options.start}\n"
|
@@ -117,15 +125,20 @@ else
|
|
117
125
|
cddl_file << ARGF.read
|
118
126
|
end
|
119
127
|
|
128
|
+
|
120
129
|
cddl = CDDL.from_cddl(cddl_file)
|
121
130
|
result = if $options.rules
|
122
131
|
if $options.expand_generics
|
123
132
|
require_relative "../lib/processor/cddl-expander.rb"
|
124
133
|
cddl.expand_generics
|
125
134
|
end
|
135
|
+
if $options.flattening
|
136
|
+
require_relative "../lib/processor/cddl-flattening.rb"
|
137
|
+
cddl.flattening
|
138
|
+
warn "*** done flattening" if $options.verbose
|
139
|
+
end
|
126
140
|
cddl.rules
|
127
141
|
else
|
128
|
-
warn "** can't expand in tree; use -r flag as well" if $options.expand_generics
|
129
142
|
cddl.tree
|
130
143
|
end
|
131
144
|
|
data/cddlc.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "cddlc"
|
3
|
-
s.version = "0.1.
|
3
|
+
s.version = "0.1.7"
|
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"
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require_relative "../cddlc.rb"
|
2
|
+
require_relative "./cddl-visitor.rb"
|
3
|
+
|
4
|
+
class CDDL
|
5
|
+
ID_RE = /\A[A-Za-z@_$]([-.]*[A-Za-z@_$0-9])*\z/
|
6
|
+
MOGRIFIED_ID_RE = /\A\$\.[A-Za-z@_$]([-.]*[A-Za-z@_$0-9])*\z/
|
7
|
+
def flattening_key_name(key, value, env = nil)
|
8
|
+
case key
|
9
|
+
in ["enum", ["mem", ["text", ID_RE => text], _]]
|
10
|
+
[false, text]
|
11
|
+
in ["text", ID_RE => text]
|
12
|
+
[false, text]
|
13
|
+
in ["number", /\A0|[-]?[1-9][0-9]*\z/ => intval] if env
|
14
|
+
[true, "$.#{env}$#{intval}"]
|
15
|
+
else
|
16
|
+
[false]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
def flattening_occurrences
|
20
|
+
symtab = Hash.new { |h, k| h[k] = [] }
|
21
|
+
rules.each do |name, prod|
|
22
|
+
visit(prod) do |here|
|
23
|
+
case here
|
24
|
+
in ["mem", key, value]
|
25
|
+
_labeled, keyname = flattening_key_name(key, value, false)
|
26
|
+
if keyname
|
27
|
+
symtab[keyname] << [name, value]
|
28
|
+
false
|
29
|
+
end
|
30
|
+
else
|
31
|
+
false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
symtab_replacements = Hash[symtab.map do |k, v|
|
36
|
+
s = Set[*v.map{_2}]
|
37
|
+
# warn "** #{k} #{s.inspect}"
|
38
|
+
if s.size == 1
|
39
|
+
[k, [[v.map{|k, v| k}.join("|"), s.first]]]
|
40
|
+
end
|
41
|
+
end.compact]
|
42
|
+
# warn "** symtab_replacements: #{symtab_replacements.inspect}" if $options.verbose
|
43
|
+
symtab.merge(symtab_replacements)
|
44
|
+
end
|
45
|
+
def flattening_mogrify(name, prod, symtab, alias_rules)
|
46
|
+
step1 = visit(prod) do |here|
|
47
|
+
case here
|
48
|
+
in ["mem", key, value]
|
49
|
+
### mogrify
|
50
|
+
labeled, keyname = flattening_key_name(key, value, name)
|
51
|
+
if keyname
|
52
|
+
new_name =
|
53
|
+
unless labeled
|
54
|
+
syment = symtab[keyname]
|
55
|
+
fail keyname unless Array === syment
|
56
|
+
if syment.size == 1
|
57
|
+
"$.#{keyname}"
|
58
|
+
else
|
59
|
+
"$.#{name}$#{keyname}"
|
60
|
+
end
|
61
|
+
else
|
62
|
+
keyname
|
63
|
+
end
|
64
|
+
new_value2 = flattening_mogrify(new_name, value, symtab, alias_rules)
|
65
|
+
if ar = alias_rules[new_name]
|
66
|
+
fail [:ALIAS_RULES, ar, new_value].inspect if ar != new_value2
|
67
|
+
else
|
68
|
+
alias_rules[new_name] = new_value2
|
69
|
+
end
|
70
|
+
[true, ["mem", key, ["name", new_name]]]
|
71
|
+
end
|
72
|
+
else
|
73
|
+
false
|
74
|
+
end
|
75
|
+
end
|
76
|
+
step2 = visit(step1) do |here|
|
77
|
+
case here
|
78
|
+
in ["enum", ["mem", ["text", ID_RE], ["name", MOGRIFIED_ID_RE => new_name]]]
|
79
|
+
[true, ["name", new_name]]
|
80
|
+
else
|
81
|
+
false
|
82
|
+
end
|
83
|
+
end
|
84
|
+
step2
|
85
|
+
end
|
86
|
+
def flattening_replace(symtab)
|
87
|
+
alias_rules = {}
|
88
|
+
new_rules = Hash[rules.map do |name, prod|
|
89
|
+
[name,
|
90
|
+
flattening_mogrify(name, prod, symtab, alias_rules)]
|
91
|
+
end]
|
92
|
+
new_rules.merge(alias_rules)
|
93
|
+
end
|
94
|
+
def flattening
|
95
|
+
symtab = flattening_occurrences
|
96
|
+
PP.pp(["*** SYMTAB", symtab], STDERR) if $options.verbose
|
97
|
+
rules.replace(flattening_replace(symtab))
|
98
|
+
end
|
99
|
+
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.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carsten Bormann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-03-
|
11
|
+
date: 2023-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -101,6 +101,7 @@ files:
|
|
101
101
|
- lib/parser/cddl-util.rb
|
102
102
|
- lib/parser/cddlgrammar.rb
|
103
103
|
- lib/processor/cddl-expander.rb
|
104
|
+
- lib/processor/cddl-flattening.rb
|
104
105
|
- lib/processor/cddl-undefined.rb
|
105
106
|
- lib/processor/cddl-visitor.rb
|
106
107
|
- lib/writer/cddl-writer.rb
|