cddlc 0.1.4 → 0.1.6

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: 000da759c9346bf077d53d94e0d303e6a60e2384ca81fe8a2e746a0f3a058ebc
4
- data.tar.gz: 93a2e95d3ab753726aa62f97e036e554bf2ca4c008eb7189dca1c87e63b53dd3
3
+ metadata.gz: 30602affec27761888bf5f3094ac596e21cbf80fa9726f2476210ed8b419b981
4
+ data.tar.gz: 4742739b320591da0f59be4732d4c0957acbfc80a1ca699caeda5943007b3d0a
5
5
  SHA512:
6
- metadata.gz: 2e501521ac303e9a8aba48198037a3f941f2ff3b6e4c875ea42bc64e8052c4a78516c0d7cb2c5a1a7efc1827295f5c446e349c51782ac6f52e9c009d341a8bd0
7
- data.tar.gz: f9ae3b76b1bbd807816b959e02d8ddaf7d79be417dfe3c3d56eb0fca3496eafd2d50f2b4496a8bd8abffb3fb539e3fb3702c140679cd25329d0a9cfe4c9af55d
6
+ metadata.gz: 9a726e557db0da91370c0c3f22ab31fc7f494d0c6902874b8129c3afb40bffd95855d7785be15f105a3cdc62831a79048daa919f714267e83bd372cf3c455bd1
7
+ data.tar.gz: a1b74234c1195480fa40eb2e298dcbb2450c07e12e282fbb0606733cf60c716f280a224f33ba5f9baa9e80d0cb1829dff526bbe78f492ae9e7f30a1bff8cc68b
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] [file.cddl... | -]"
48
48
 
49
49
  opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
50
50
  $options.verbose = v
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.4"
3
+ s.version = "0.1.6"
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"
@@ -15,6 +15,6 @@ Gem::Specification.new do |s|
15
15
 
16
16
  s.add_development_dependency 'bundler', '~>1'
17
17
  s.add_dependency 'treetop', '~>1'
18
- s.add_dependency 'json'
19
- s.add_dependency 'neatjson'
18
+ s.add_dependency 'json', '~>2'
19
+ s.add_dependency 'neatjson', '~>0.10'
20
20
  end
data/lib/cddlc.rb CHANGED
@@ -39,8 +39,22 @@ class CDDL
39
39
  end])
40
40
  end
41
41
 
42
+ def self.read_from_include_path(fn)
43
+ io = nil
44
+ CDDL::cddl_include_path.each do |path|
45
+ begin
46
+ io = (path + fn).open
47
+ break
48
+ rescue Errno::ENOENT
49
+ next
50
+ end
51
+ end
52
+ io
53
+ end
54
+
42
55
  SAFE_FN = /\A[-._a-zA-Z0-9]+\z/
43
56
  IMPINC = /\A(?:import|include)\z/
57
+ IDENTIFIER_RE = /\A[A-Za-z@_$]([-.]*[A-Za-z@_$0-9])*\z/
44
58
 
45
59
  def self.from_cddl(s)
46
60
  ast = @@parser.parse s
@@ -51,86 +65,147 @@ class CDDL
51
65
  directives = s.lines.grep(/^;# /).map(&:chomp).map{|l| l.sub(/^;#\s+/, '').split(/\s+/)}
52
66
  # puts directives.to_yaml
53
67
  end
68
+
54
69
  ret = CDDL.new(ast, directives)
55
70
 
56
71
  if $options.cddl2
57
- ret.directives.each do |di|
58
- preferred_tag = nil
59
- case di
60
- in [IMPINC => dir, SAFE_FN => docref]
61
- in [IMPINC => dir, SAFE_FN => docref, "as", SAFE_FN => preferred_tag]
62
- else
63
- warn "** Can't parse directive »#{di.join(" ")}«"
64
- next
65
- end
66
- puts "PREFERRED_TAG #{preferred_tag}" if $options.verbose
67
- puts "DOCREF #{docref}" if $options.verbose
68
- fn = docref.downcase << ".cddl"
69
-
70
- io = nil
71
- CDDL::cddl_include_path.each do |path|
72
- begin
73
- io = (path + fn).open
74
- break
75
- rescue Errno::ENOENT
76
- next
72
+ ret.perform_directives
73
+ end
74
+ ret
75
+ end
76
+
77
+ def perform_directives
78
+ directives.each do |di|
79
+ preferred_tag = nil
80
+ from = nil
81
+ case di
82
+ in [IMPINC => dir, SAFE_FN => docref, "as", SAFE_FN => preferred_tag]
83
+ in [IMPINC => dir, SAFE_FN => docref]
84
+ in [IMPINC => dir, *from, "from", SAFE_FN => docref, "as", SAFE_FN => preferred_tag]
85
+ in [IMPINC => dir, *from, "from", SAFE_FN => docref]
86
+ else
87
+ warn "** Can't parse directive »#{di.join(" ")}«"
88
+ next
89
+ end
90
+ if from
91
+ from = from.map do |name|
92
+ name.chomp!(",")
93
+ if IDENTIFIER_RE === name
94
+ name
95
+ else
96
+ warn "*** invalid identifier #{name.inspect} ignored in »#{di.join(' ')}«"
97
+ nil
77
98
  end
78
- end
79
- unless io
80
- warn "** include file #{fn} not found in #{CDDL::cddl_include_path.map(&:to_s)}"
81
- next
82
- end
99
+ end.compact
100
+ end
101
+ warn "FROM #{from.inspect}" if $options.verbose
102
+ warn "PREFERRED_TAG #{preferred_tag.inspect}" if $options.verbose
103
+ warn "DOCREF #{docref.inspect}" if $options.verbose
83
104
 
84
- include_file = io.read
85
- included_cddl = CDDL.from_cddl(include_file)
86
- if preferred_tag
87
- included_cddl = included_cddl.deep_clone # needed?
88
- renamed_names = included_cddl.rules.keys
89
- name_rename = Hash[
90
- renamed_names.map { |o|
91
- n = "#{preferred_tag}.#{o}"
92
- warn "** Warning: renamed name #{n} already in #{fn}" if included_cddl.rules[n]
93
- [o, n]}]
94
- included_cddl.rename(name_rename)
95
- end
105
+ fn = docref.downcase << ".cddl"
106
+ io = CDDL.read_from_include_path(fn)
107
+ unless io
108
+ warn "** #{dir} file #{fn} not found in #{CDDL::cddl_include_path.map(&:to_s)}"
109
+ next
110
+ end
96
111
 
97
- case dir
98
- in "import"
99
- warn "** IMPORTING #{fn}" if $options.verbose
100
- undef_rule = nil
101
- loop do
102
- undef_rule = ret.cddl_undefined # XXX square...
103
- # p undef_rule
104
- got_more = false
105
- undef_rule.each do |name|
106
- if rule = included_cddl.rules[name]
107
- ret.rules[name] = rule
108
- warn "IMPORTED #{name} from #{fn}" if $options.verbose
109
- got_more = true
110
- end
112
+ include_file = io.read
113
+ included_cddl = CDDL.from_cddl(include_file)
114
+ if preferred_tag
115
+ included_cddl = included_cddl.deep_clone # needed?
116
+ renamed_names = included_cddl.rules.keys
117
+ name_rename = Hash[
118
+ renamed_names.map { |o|
119
+ n = "#{preferred_tag}.#{o}"
120
+ warn "** Warning: renamed name #{n} already in #{fn}" if included_cddl.rules[n]
121
+ [o, n]}]
122
+ included_cddl.rename(name_rename)
123
+ end
124
+
125
+ case dir
126
+ in "import"
127
+ warn "** IMPORTING #{from && from.join(", ") << " from "}#{fn}" if $options.verbose
128
+
129
+ must_be_found = false
130
+ to_be_imported = if from
131
+ if preferred_tag
132
+ from.map{ |k|
133
+ if k =~ /\A#{preferred_tag}[.]/
134
+ k
135
+ elsif nn = name_rename[k]
136
+ if old = store_rule(k, v = ["name", nn])
137
+ warn "*** autogenerated rule #{k} = #{v} would overwrite #{old}"
138
+ end
139
+ nn
140
+ else
141
+ warn_error "*** imported rule #{k.inspect} not in #{fn}"
142
+ end
143
+ }.compact
144
+ else
145
+ must_be_found = true
146
+ from
147
+ end
148
+ else
149
+ cddl_undefined
150
+ end
151
+ loop do
152
+ # p to_be_imported
153
+ got_more = false
154
+ to_be_imported.each do |name|
155
+ if rule = included_cddl.rules[name]
156
+ rules[name] = rule
157
+ warn "IMPORTED #{name} from #{fn}" if $options.verbose
158
+ got_more = true
159
+ elsif must_be_found
160
+ warn_error "*** imported rule #{name.inspect} not in #{fn}"
111
161
  end
112
- break unless got_more
113
162
  end
114
- if preferred_tag
115
- undef_rule.each do |name|
116
- warn "** Warning: undefined reference #{name} without namespace prefix is defined in namespaced imported module #{fn}" if name_rename[name]
117
- end
163
+ break unless got_more
164
+ to_be_imported = cddl_undefined # XXX square...
165
+ warn "TO IMPORT #{to_be_imported.inspect}" if $options.verbose
166
+ must_be_found = false
167
+ end
168
+ if preferred_tag
169
+ to_be_imported.each do |name|
170
+ warn "** Warning: undefined reference #{name} without namespace prefix is defined in namespaced imported module #{fn}" if name_rename[name]
171
+ ### instead just add rule...
118
172
  end
119
- in "include"
120
- warn "** INCLUDING #{fn}" if $options.verbose
121
- included_cddl.rules.each do |k, v|
122
- if old = ret.rules[k]
123
- if old != v
124
- warn "** included rule #{k} = #{v} would overwrite #{old}"
125
- end
126
- else
127
- ret.rules[k] = v
128
- end
173
+ end
174
+ in "include"
175
+ if from
176
+ from = Set[*from]
177
+ end
178
+ warn "** INCLUDING #{from && from.join(", ") << " from "}#{fn}" if $options.verbose
179
+ included_cddl.rules.each do |k, v|
180
+ if from
181
+ next unless from.include?(k)
182
+ from.delete(k)
129
183
  end
184
+ if old = store_rule(k, v)
185
+ warn "*** included rule #{k} = #{v} would overwrite #{old}"
186
+ end
187
+ end
188
+ if from && !from.empty?
189
+ warn "*** could not find #{from.join(", ")} in #{fn}"
130
190
  end
131
191
  end
132
192
  end
133
- ret
193
+ end
194
+
195
+ def store_rule(k, v)
196
+ if old = rules[k]
197
+ if old != v
198
+ return old # error
199
+ end
200
+ else
201
+ rules[k] = v
202
+ end
203
+ nil # OK
204
+ end
205
+
206
+ def warn_error(s)
207
+ warn s
208
+ @error = true
134
209
  end
135
210
 
136
211
  attr_accessor :ast, :tree, :directives
@@ -69,12 +69,7 @@ module CDDLGRAMMAR
69
69
  break
70
70
  end
71
71
  end
72
- if s2.empty?
73
- @index = i2
74
- r2 = nil
75
- else
76
- r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
77
- end
72
+ r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
78
73
  s0 << r2
79
74
  end
80
75
  if s0.last
@@ -952,7 +947,7 @@ module CDDLGRAMMAR
952
947
  end
953
948
 
954
949
  module Type214
955
- def uint
950
+ def tag_number
956
951
  elements[1]
957
952
  end
958
953
  end
@@ -975,7 +970,7 @@ module CDDLGRAMMAR
975
970
  module Type216
976
971
  def ast
977
972
  el = elements[2].elements
978
- ["prim", 6, el && el[1].text_value.to_i, elements[5].ast]
973
+ ["prim", 6, el && el[1].ast, elements[5].ast]
979
974
  end
980
975
  end
981
976
 
@@ -1335,7 +1330,7 @@ module CDDLGRAMMAR
1335
1330
  end
1336
1331
  s48 << r49
1337
1332
  if r49
1338
- r50 = _nt_uint
1333
+ r50 = _nt_tag_number
1339
1334
  s48 << r50
1340
1335
  end
1341
1336
  if s48.last
@@ -1481,6 +1476,85 @@ module CDDLGRAMMAR
1481
1476
  r0
1482
1477
  end
1483
1478
 
1479
+ module TagNumber0
1480
+ def ast() text_value.to_i end
1481
+ end
1482
+
1483
+ module TagNumber1
1484
+ def type
1485
+ elements[1]
1486
+ end
1487
+
1488
+ end
1489
+
1490
+ module TagNumber2
1491
+ def ast() elements[1].ast end
1492
+ end
1493
+
1494
+ def _nt_tag_number
1495
+ start_index = index
1496
+ if node_cache[:tag_number].has_key?(index)
1497
+ cached = node_cache[:tag_number][index]
1498
+ if cached
1499
+ node_cache[:tag_number][index] = cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
1500
+ @index = cached.interval.end
1501
+ end
1502
+ return cached
1503
+ end
1504
+
1505
+ i0 = index
1506
+ r1 = _nt_uint
1507
+ r1.extend(TagNumber0)
1508
+ r1.extend(TagNumber0)
1509
+ if r1
1510
+ r1 = SyntaxNode.new(input, (index-1)...index) if r1 == true
1511
+ r0 = r1
1512
+ else
1513
+ i2, s2 = index, []
1514
+ if (match_len = has_terminal?("<", false, index))
1515
+ r3 = true
1516
+ @index += match_len
1517
+ else
1518
+ terminal_parse_failure('"<"')
1519
+ r3 = nil
1520
+ end
1521
+ s2 << r3
1522
+ if r3
1523
+ r4 = _nt_type
1524
+ s2 << r4
1525
+ if r4
1526
+ if (match_len = has_terminal?(">", false, index))
1527
+ r5 = true
1528
+ @index += match_len
1529
+ else
1530
+ terminal_parse_failure('">"')
1531
+ r5 = nil
1532
+ end
1533
+ s2 << r5
1534
+ end
1535
+ end
1536
+ if s2.last
1537
+ r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
1538
+ r2.extend(TagNumber1)
1539
+ r2.extend(TagNumber2)
1540
+ else
1541
+ @index = i2
1542
+ r2 = nil
1543
+ end
1544
+ if r2
1545
+ r2 = SyntaxNode.new(input, (index-1)...index) if r2 == true
1546
+ r0 = r2
1547
+ else
1548
+ @index = i0
1549
+ r0 = nil
1550
+ end
1551
+ end
1552
+
1553
+ node_cache[:tag_number][start_index] = r0
1554
+
1555
+ r0
1556
+ end
1557
+
1484
1558
  def _nt_rangeop
1485
1559
  start_index = index
1486
1560
  if node_cache[:rangeop].has_key?(index)
@@ -42,7 +42,7 @@ class CDDL
42
42
  else
43
43
  end
44
44
  end
45
- p @gen if $options.verbose
45
+ warn "@gen = #{@gen.inspect}" if $options.verbose
46
46
  @gen.each do |k, v|
47
47
  parmnames = v[0]
48
48
  fail unless rules[k] == ["parm", parmnames, v[1]]
@@ -53,7 +53,7 @@ class CDDL
53
53
  fail if Array === name
54
54
  @new_rules[name] = expand_prod(prod)
55
55
  end
56
- p @new_rules if $options.verbose
56
+ warn "@new_rules = #{@new_rules.inspect}" if $options.verbose
57
57
  @rules = @new_rules
58
58
  end
59
59
  end
@@ -31,7 +31,7 @@ class CDDL
31
31
  ["enum", visit(prod, &block)]
32
32
  in ["unwrap", prod]
33
33
  ["unwrap", visit(prod, &block)] # XXX, this may need to be bottled in a rule
34
- in ["prim", Array => prod, *prods]
34
+ in ["prim", prod, *prods]
35
35
  ["prim", visit(prod, &block), *visit_all(prods, &block)]
36
36
  in ["mem", *prods]
37
37
  ["mem", *visit_all(prods, &block)]
@@ -68,6 +68,8 @@ class CDDL
68
68
  [4, "##{maj}.#{min}"]
69
69
  in ["prim", 6, Integer => tag, type]
70
70
  [4, "#6.#{tag}(#{write_rhs(type, 0, indent, pn)})"]
71
+ in ["prim", 6, Array => tag, type]
72
+ [4, "#6.<#{write_rhs(tag, 0, indent, pn)}>(#{write_rhs(type, 0, indent, pn)})"]
71
73
  in ["prim", 6, nil, type]
72
74
  [4, "#6(#{write_rhs(type, 0, indent, pn)})"]
73
75
  # prim: extension for #6.<i>(t)
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
4
+ version: 0.1.6
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-02 00:00:00.000000000 Z
11
+ date: 2023-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -42,30 +42,30 @@ dependencies:
42
42
  name: json
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '2'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: neatjson
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '0.10'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '0.10'
69
69
  description: cddlc implements converters and miscellaneous tools for CDDL, RFC 8610
70
70
  email: cabo@tzi.org
71
71
  executables: