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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e4a9fe5e7434a8b5f5bea9b9194c988206f74e95aef1e4d2e46067968367727
4
- data.tar.gz: de11c07b72815e2e6580ed35b36d4d0b56f39e282b3072f84d00e836b6bcd2aa
3
+ metadata.gz: b1b9a4aa1c2c8b9ba3310cdca0201947f6bd08c7d229443d462ac2881e3d0768
4
+ data.tar.gz: 7873a339b9f98917635e0d9e5bd6a1a62ce096b2ba6fb39d1daced9eb328209b
5
5
  SHA512:
6
- metadata.gz: 62ca200613f056851f397a41a57e64fd49fa0ff2196d0433d706760c05f11468ae3a6ed7652fc4ee5f4387476744e36f2f06f07f67efc14b68774460b55023cd
7
- data.tar.gz: b1b7560dbfe6f712f31b415138f37bde551a095fcb7a42a7afc82268db416de5eca5f1dff0d3d9f8cf02b82b39b9880d3fb19e7dc405186ffba61acd6b2d029a
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/parser/cddl-util.rb'
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("-tFMT", "--to=FMT", [:neat, :json, :yaml], "Target format") do |v|
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
- parser = CDDLParser.new
38
- ast = parser.parse cddl_file
39
- if ast
40
- # puts ast.to_yaml
41
- result = ast.ast
42
- case $options.target
43
- when :json, nil
44
- pp result
45
- when :neat
46
- require 'neatjson'
47
- puts JSON.neat_generate(result, after_comma: 1, after_colon: 1)
48
- when :yaml
49
- puts result.to_yaml
50
- else
51
- warn ["Unknown target format: ", $options.target].inspect
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 parser.failure_reason
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.1"
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
@@ -1,5 +1,5 @@
1
1
 
2
- require_relative './cddl'
2
+ require_relative './cddlgrammar'
3
3
 
4
4
  class Treetop::Runtime::SyntaxNode
5
5
  def ast
@@ -2,14 +2,14 @@
2
2
  # Autogenerated from a Treetop grammar. Edits may be lost.
3
3
 
4
4
 
5
- module CDDL
5
+ module CDDLGRAMMAR
6
6
  include Treetop::Runtime
7
7
 
8
8
  def root
9
- @root ||= :cddl
9
+ @root ||= :cddlgrammar
10
10
  end
11
11
 
12
- module Cddl0
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 Cddl1
22
+ module Cddlgrammar1
23
23
  def S
24
24
  elements[0]
25
25
  end
26
26
 
27
27
  end
28
28
 
29
- module Cddl2
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 _nt_cddl
35
+ def _nt_cddlgrammar
36
36
  start_index = index
37
- if node_cache[:cddl].has_key?(index)
38
- cached = node_cache[:cddl][index]
37
+ if node_cache[:cddlgrammar].has_key?(index)
38
+ cached = node_cache[:cddlgrammar][index]
39
39
  if cached
40
- node_cache[:cddl][index] = cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
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(Cddl0)
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(Cddl1)
83
- r0.extend(Cddl2)
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[:cddl][start_index] = r0
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 CDDLParser < Treetop::Runtime::CompiledParser
4014
- include CDDL
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.1
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: 2019-07-20 00:00:00.000000000 Z
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/cddl.rb
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.0.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: []