cddlc 0.0.1 → 0.0.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 +68 -22
- data/cddlc.gemspec +1 -1
- data/lib/cddlc.rb +69 -0
- data/lib/parser/cddl-util.rb +1 -1
- data/lib/parser/{cddl.rb → cddlgrammar.rb} +15 -15
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1b9a4aa1c2c8b9ba3310cdca0201947f6bd08c7d229443d462ac2881e3d0768
|
4
|
+
data.tar.gz: 7873a339b9f98917635e0d9e5bd6a1a62ce096b2ba6fb39d1daced9eb328209b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac5068f18dc4881e9b52edeb0e262d806f788d72835ca3bfd020911f1414bc60a8a35fe18b7ede71d56bee3c25e263afa16e81f7d8fabee349fe9c0e9c57218a
|
7
|
+
data.tar.gz: 3676274ba515d3ec01677a2e0db3e988659e692c9457e01566122dc672f00ec0d1490d602139d13e962ebd31544a6d90ae88842f466edbc65d3c2373d2b9a948
|
data/bin/cddlc
CHANGED
@@ -4,8 +4,39 @@ require 'yaml'
|
|
4
4
|
require 'treetop'
|
5
5
|
require 'json'
|
6
6
|
|
7
|
-
require_relative '../lib/
|
7
|
+
require_relative '../lib/cddlc.rb'
|
8
8
|
|
9
|
+
def named_keys(tree)
|
10
|
+
# warn [:T, tree].inspect
|
11
|
+
fail unless Array === tree
|
12
|
+
case tree[0]
|
13
|
+
when "seq", "gcho", "tcho"
|
14
|
+
tree[1..-1].flat_map {|x| named_keys(x)}
|
15
|
+
when "rep"
|
16
|
+
named_keys(tree[3])
|
17
|
+
when "mem"
|
18
|
+
key = tree[1]
|
19
|
+
if Array === key && key[0] == "name"
|
20
|
+
[key[1]]
|
21
|
+
end
|
22
|
+
end || []
|
23
|
+
end
|
24
|
+
|
25
|
+
def integer_value(tree)
|
26
|
+
tree and (
|
27
|
+
fail tree.inspect unless Array === tree
|
28
|
+
case tree[0]
|
29
|
+
when "number"
|
30
|
+
tree[1]
|
31
|
+
# XXX: could add other cases
|
32
|
+
end
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def snaky(name)
|
37
|
+
name.gsub(/-/, "_")
|
38
|
+
end
|
39
|
+
|
9
40
|
Encoding.default_external = Encoding::UTF_8
|
10
41
|
require 'optparse'
|
11
42
|
require 'ostruct'
|
@@ -18,7 +49,10 @@ begin
|
|
18
49
|
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
|
19
50
|
$options.verbose = v
|
20
51
|
end
|
21
|
-
opts.on("-
|
52
|
+
opts.on("-r", "--[no-]rules", "Process rules") do |v|
|
53
|
+
$options.rules = v
|
54
|
+
end
|
55
|
+
opts.on("-tFMT", "--to=FMT", [:basic, :neat, :json, :yaml, :enum], "Target format") do |v|
|
22
56
|
$options.target = v
|
23
57
|
end
|
24
58
|
end
|
@@ -34,26 +68,38 @@ if ARGV == []
|
|
34
68
|
end
|
35
69
|
cddl_file = ARGF.read
|
36
70
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
71
|
+
cddl = CDDL.from_cddl(cddl_file)
|
72
|
+
result = if $options.rules
|
73
|
+
cddl.rules
|
74
|
+
else
|
75
|
+
cddl.tree
|
76
|
+
end
|
77
|
+
|
78
|
+
case $options.target
|
79
|
+
when :basic, nil
|
80
|
+
pp result
|
81
|
+
when :neat, :json
|
82
|
+
require 'neatjson'
|
83
|
+
puts JSON.neat_generate(result, after_comma: 1, after_colon: 1)
|
84
|
+
when :yaml
|
85
|
+
puts result.to_yaml
|
86
|
+
when :enum
|
87
|
+
rules = cddl.rules
|
88
|
+
rules.each do |k, v|
|
89
|
+
if v[0] == "map"
|
90
|
+
nk = v[1..-1].flat_map {|e| named_keys(e)}
|
91
|
+
# warn [:NK, k, nk].inspect
|
92
|
+
nk = nk.map {|name| i = integer_value(rules[name]); [name, i] if i}.compact
|
93
|
+
# warn [:NK, k, nk].inspect
|
94
|
+
if nk != []
|
95
|
+
puts "enum #{snaky(k).downcase}_keys {"
|
96
|
+
nk.each do |n, v|
|
97
|
+
puts " #{snaky(n).upcase} = #{v},"
|
98
|
+
end
|
99
|
+
puts "};"
|
100
|
+
end
|
101
|
+
end
|
52
102
|
end
|
53
103
|
else
|
54
|
-
warn
|
55
|
-
parser.failure_reason =~ /^(Expected .+) after/m
|
56
|
-
warn "#{$1.gsub("\n", '<<<NEWLINE>>>')}:"
|
57
|
-
warn cddl_file.lines.to_a[parser.failure_line - 1]
|
58
|
-
warn "#{'~' * (parser.failure_column - 1)}^"
|
104
|
+
warn ["Unknown target format: ", $options.target].inspect
|
59
105
|
end
|
data/cddlc.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "cddlc"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.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
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require_relative "parser/cddl-util.rb"
|
2
|
+
|
3
|
+
class CDDL
|
4
|
+
@@parser = CDDLGRAMMARParser.new
|
5
|
+
|
6
|
+
def self.reason(parser, s)
|
7
|
+
reason = [parser.failure_reason]
|
8
|
+
parser.failure_reason =~ /^(Expected .+) after/m
|
9
|
+
reason << "#{$1.gsub("\n", '<<<NEWLINE>>>')}:" if $1
|
10
|
+
if line = s.lines.to_a[parser.failure_line - 1]
|
11
|
+
reason << line
|
12
|
+
reason << "#{'~' * (parser.failure_column - 1)}^"
|
13
|
+
end
|
14
|
+
reason.join("\n")
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.from_cddl(s)
|
18
|
+
ast = @@parser.parse s
|
19
|
+
if !ast
|
20
|
+
fail self.reason(@@parser, s)
|
21
|
+
end
|
22
|
+
CDDL.new(ast)
|
23
|
+
end
|
24
|
+
|
25
|
+
attr_accessor :ast, :tree
|
26
|
+
def initialize(ast_)
|
27
|
+
@ast = ast_
|
28
|
+
@tree = ast.ast
|
29
|
+
@rules = nil # only fill in if needed
|
30
|
+
end
|
31
|
+
|
32
|
+
RULE_OP_TO_CHOICE = {"/=" => "tcho", "//=" => "gcho"}
|
33
|
+
|
34
|
+
def rules
|
35
|
+
if @rules.nil? # memoize
|
36
|
+
@rules = {}
|
37
|
+
fail unless @tree.first == "cddl"
|
38
|
+
@tree[1..-1].each do |x|
|
39
|
+
op, name, val, rest = x
|
40
|
+
cho = RULE_OP_TO_CHOICE[op]
|
41
|
+
fail rest if rest
|
42
|
+
fail name unless Array === name
|
43
|
+
case name[0]
|
44
|
+
when "name"
|
45
|
+
fail unless name.size == 2
|
46
|
+
name = name[1]
|
47
|
+
when "gen"
|
48
|
+
name = name[1..-1]
|
49
|
+
else
|
50
|
+
fail name
|
51
|
+
end
|
52
|
+
@rules[name] =
|
53
|
+
if old = @rules[name]
|
54
|
+
fail "duplicate rule for name #{name}" unless cho
|
55
|
+
if Array === old && old[0] == cho
|
56
|
+
old.dup << val
|
57
|
+
else
|
58
|
+
[cho, old, val]
|
59
|
+
end
|
60
|
+
else
|
61
|
+
val
|
62
|
+
end
|
63
|
+
end
|
64
|
+
# warn "** rules #{rules.inspect}"
|
65
|
+
end
|
66
|
+
@rules
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
data/lib/parser/cddl-util.rb
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
# Autogenerated from a Treetop grammar. Edits may be lost.
|
3
3
|
|
4
4
|
|
5
|
-
module
|
5
|
+
module CDDLGRAMMAR
|
6
6
|
include Treetop::Runtime
|
7
7
|
|
8
8
|
def root
|
9
|
-
@root ||= :
|
9
|
+
@root ||= :cddlgrammar
|
10
10
|
end
|
11
11
|
|
12
|
-
module
|
12
|
+
module Cddlgrammar0
|
13
13
|
def r__rule
|
14
14
|
elements[0]
|
15
15
|
end
|
@@ -19,25 +19,25 @@ module CDDL
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
module
|
22
|
+
module Cddlgrammar1
|
23
23
|
def S
|
24
24
|
elements[0]
|
25
25
|
end
|
26
26
|
|
27
27
|
end
|
28
28
|
|
29
|
-
module
|
29
|
+
module Cddlgrammar2
|
30
30
|
def ast
|
31
31
|
["cddl", *elements[1].elements.map(&:r__rule).map(&:ast)]
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
def
|
35
|
+
def _nt_cddlgrammar
|
36
36
|
start_index = index
|
37
|
-
if node_cache[:
|
38
|
-
cached = node_cache[:
|
37
|
+
if node_cache[:cddlgrammar].has_key?(index)
|
38
|
+
cached = node_cache[:cddlgrammar][index]
|
39
39
|
if cached
|
40
|
-
node_cache[:
|
40
|
+
node_cache[:cddlgrammar][index] = cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
|
41
41
|
@index = cached.interval.end
|
42
42
|
end
|
43
43
|
return cached
|
@@ -58,7 +58,7 @@ module CDDL
|
|
58
58
|
end
|
59
59
|
if s3.last
|
60
60
|
r3 = instantiate_node(SyntaxNode,input, i3...index, s3)
|
61
|
-
r3.extend(
|
61
|
+
r3.extend(Cddlgrammar0)
|
62
62
|
else
|
63
63
|
@index = i3
|
64
64
|
r3 = nil
|
@@ -79,14 +79,14 @@ module CDDL
|
|
79
79
|
end
|
80
80
|
if s0.last
|
81
81
|
r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
|
82
|
-
r0.extend(
|
83
|
-
r0.extend(
|
82
|
+
r0.extend(Cddlgrammar1)
|
83
|
+
r0.extend(Cddlgrammar2)
|
84
84
|
else
|
85
85
|
@index = i0
|
86
86
|
r0 = nil
|
87
87
|
end
|
88
88
|
|
89
|
-
node_cache[:
|
89
|
+
node_cache[:cddlgrammar][start_index] = r0
|
90
90
|
|
91
91
|
r0
|
92
92
|
end
|
@@ -4010,7 +4010,7 @@ module CDDL
|
|
4010
4010
|
|
4011
4011
|
end
|
4012
4012
|
|
4013
|
-
class
|
4014
|
-
include
|
4013
|
+
class CDDLGRAMMARParser < Treetop::Runtime::CompiledParser
|
4014
|
+
include CDDLGRAMMAR
|
4015
4015
|
end
|
4016
4016
|
|
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.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carsten Bormann
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -75,13 +75,14 @@ extra_rdoc_files: []
|
|
75
75
|
files:
|
76
76
|
- bin/cddlc
|
77
77
|
- cddlc.gemspec
|
78
|
+
- lib/cddlc.rb
|
78
79
|
- lib/parser/cddl-util.rb
|
79
|
-
- lib/parser/
|
80
|
+
- lib/parser/cddlgrammar.rb
|
80
81
|
homepage: http://github.com/cabo/cddlc
|
81
82
|
licenses:
|
82
83
|
- MIT
|
83
84
|
metadata: {}
|
84
|
-
post_install_message:
|
85
|
+
post_install_message:
|
85
86
|
rdoc_options: []
|
86
87
|
require_paths:
|
87
88
|
- lib
|
@@ -96,8 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
97
|
- !ruby/object:Gem::Version
|
97
98
|
version: '0'
|
98
99
|
requirements: []
|
99
|
-
rubygems_version: 3.
|
100
|
-
signing_key:
|
100
|
+
rubygems_version: 3.2.3
|
101
|
+
signing_key:
|
101
102
|
specification_version: 4
|
102
103
|
summary: CDDL (Concise Data Definition Language) converters and miscellaneous tools
|
103
104
|
test_files: []
|