cddlc 0.1.4 → 0.1.5

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: aa1feed69996a1e91c43877cfbe3e68c6b935039976ae95db312fef99a0e8bbd
4
+ data.tar.gz: 746c4b175cb78c502ab731e43672841663cfb4a4159bcf387f8f69c9f34e347a
5
5
  SHA512:
6
- metadata.gz: 2e501521ac303e9a8aba48198037a3f941f2ff3b6e4c875ea42bc64e8052c4a78516c0d7cb2c5a1a7efc1827295f5c446e349c51782ac6f52e9c009d341a8bd0
7
- data.tar.gz: f9ae3b76b1bbd807816b959e02d8ddaf7d79be417dfe3c3d56eb0fca3496eafd2d50f2b4496a8bd8abffb3fb539e3fb3702c140679cd25329d0a9cfe4c9af55d
6
+ metadata.gz: c9b58614695995f0336496734a3684992ddd6f7a1be0dfcbc0451f45afde7e78eb2a2f89ce60e9c5e0c09a1c0e7e8856cc5e5658fe3fe78bc22bc08c4897c4d4
7
+ data.tar.gz: d19ca545ca39eb87af227651438755c13475dd75bd29ef11a6a6327aac42e9c4a69fe0e5322a291e7819903d89c4de1296e1605934bce9b154dcb7ef6563c151
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.5"
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,146 @@ 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
+ puts "FROM #{from.inspect}" if $options.verbose
102
+ puts "PREFERRED_TAG #{preferred_tag.inspect}" if $options.verbose
103
+ puts "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
+ must_be_found = false
166
+ end
167
+ if preferred_tag
168
+ to_be_imported.each do |name|
169
+ warn "** Warning: undefined reference #{name} without namespace prefix is defined in namespaced imported module #{fn}" if name_rename[name]
170
+ ### instead just add rule...
118
171
  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
172
+ end
173
+ in "include"
174
+ if from
175
+ from = Set[*from]
176
+ end
177
+ warn "** INCLUDING #{from && from.join(", ") << " from "}#{fn}" if $options.verbose
178
+ included_cddl.rules.each do |k, v|
179
+ if from
180
+ next unless from.include?(k)
181
+ from.delete(k)
129
182
  end
183
+ if old = store_rule(k, v)
184
+ warn "*** included rule #{k} = #{v} would overwrite #{old}"
185
+ end
186
+ end
187
+ if from && !from.empty?
188
+ warn "*** could not find #{from.join(", ")} in #{fn}"
130
189
  end
131
190
  end
132
191
  end
133
- ret
192
+ end
193
+
194
+ def store_rule(k, v)
195
+ if old = rules[k]
196
+ if old != v
197
+ return old # error
198
+ end
199
+ else
200
+ rules[k] = v
201
+ end
202
+ nil # OK
203
+ end
204
+
205
+ def warn_error(s)
206
+ warn s
207
+ @error = true
134
208
  end
135
209
 
136
210
  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
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.5
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-05 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: