edn-abnf 0.2.5 → 0.3.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: c3897c19560ddc1c89c6a54f9ae9e71d88e1f1450589cd703261db76bfcca52c
4
- data.tar.gz: 561925dbb535a93b97bce28fd6c36882a8ee73dd12e71bce99381f9f0d94dcfd
3
+ metadata.gz: e17fb20ba9bb383a9172ef8bb3f04e2c473ec1d215972711ecac235dbf3da7d6
4
+ data.tar.gz: a9908d2846aef7899ec5ff96b4c0613a69019650239d89aa63c7cac1e8773ed7
5
5
  SHA512:
6
- metadata.gz: a77b88f8fa087d4a5f1284cfb9735183af07a20090788e9d9b3b3eb0396005d35396cca28228842a0d7d5a9ecbbfa3debb3f6f7a86d83564df1ee2474aed52ff
7
- data.tar.gz: a6c564e1577ea8dc88859465112a57a5e5d62473fa36e0f73cf3c30eeb46de0ad46f67197a826f178987b8b7dd856697e3e7c117f127909798e88715bfeda64c
6
+ metadata.gz: cd4641f2f4cdf9a48d79ab41f9891f29578d2c0c6d277c468e4ed92c6ed32756611375db720e30fb39e097ee3819d18902e71a330e666b97bd55d551219255b4
7
+ data.tar.gz: 62586e9cc378e802565f3c2e746bad989b7803c9a38a336b2c88db480dcf8df00125f254e30eafe67d3e80d1dbe920a4a9db24418e6aa0379fb9890214fc7fda
data/bin/edn-abnf CHANGED
@@ -46,7 +46,7 @@ begin
46
46
  opts.on("-eEDN", "--e=EDN", "supply EDN on command line") do |v|
47
47
  $options.edn = v
48
48
  end
49
- opts.on("-tFMT", "--to=FMT", [:basic, :neat, :json, :yaml, :edn, :diag], "Target format") do |v|
49
+ opts.on("-tFMT", "--to=FMT", [:basic, :neat, :json, :yaml, :edn, :diag], "Target format (default: diag)") do |v|
50
50
  $options.target = v
51
51
  end
52
52
  end
@@ -83,7 +83,11 @@ if $options.lines
83
83
  result_diag = result.cbor_diagnostic
84
84
  puts "x #{ok}, #{ednin.inspect} / #{out} ➔ #{result_diag.inspect} / #{result_hex}" if $options.verbose
85
85
  if result_hex != out
86
+ if CBOR.decode(out.xeh).to_cbor == result.to_cbor
87
+ puts "** x variant / #{ednin.inspect} / #{out} ≠ #{result_diag.inspect} / #{result_hex}"
88
+ else
86
89
  puts "** x / #{ednin.inspect} / #{out} ≠ #{result_diag.inspect} / #{result_hex}"
90
+ end
87
91
  end
88
92
  next
89
93
  end
@@ -96,7 +100,7 @@ if $options.lines
96
100
  puts "** ≠ / #{diag.inspect} / #{out.inspect}"
97
101
  end
98
102
  if out != diag && ok
99
- outdiag = tree_from_edn_possibly_hex(out).cbor_diagnostic
103
+ outdiag = tree_from_edn_possibly_hex(out || "").cbor_diagnostic
100
104
  if outdiag != diag
101
105
  puts "** ≡ / #{ednin.inspect} #{diag.inspect} / #{out.inspect} #{outdiag.inspect}"
102
106
  end
@@ -124,14 +128,14 @@ rescue ArgumentError => e
124
128
  end
125
129
 
126
130
  case $options.target
127
- when :basic, nil
131
+ when :basic
128
132
  pp result
129
133
  when :neat, :json
130
134
  require 'neatjson'
131
135
  puts JSON.neat_generate(result, after_comma: 1, after_colon: 1)
132
136
  when :yaml
133
137
  puts result.to_yaml
134
- when :edn, :diag
138
+ when :edn, :diag, nil
135
139
  puts result.cbor_diagnostic
136
140
  else
137
141
  warn ["Unknown target format: ", $options.target].inspect
data/edn-abnf.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "edn-abnf"
3
- s.version = "0.2.5"
3
+ s.version = "0.3.2"
4
4
  s.summary = "CBOR Extended Diagnostic Notation (EDN) implemented in ABNF"
5
5
  s.description = %q{edn-abnf implements converters and miscellaneous tools for CBOR EDN's ABNF}
6
6
  s.author = "Carsten Bormann"
@@ -18,4 +18,5 @@ Gem::Specification.new do |s|
18
18
  s.add_dependency 'treetop', '~>1'
19
19
  s.add_dependency 'json', '~>2'
20
20
  s.add_dependency 'neatjson', '~>0.10'
21
+ s.add_dependency 'cbor-diag'
21
22
  end
@@ -0,0 +1,134 @@
1
+ module CBOR
2
+ Box = Struct.new(:value, :options) do
3
+ def to_s
4
+ value.to_s
5
+ end
6
+ def inspect
7
+ "#<CBOR::Box #{self.class} value=#{value.inspect}, options=#{options.inspect}>"
8
+ end
9
+ def self.from_number(n, options={})
10
+ case n
11
+ when Box
12
+ n.class.new(n.value, n.options.merge(options))
13
+ when ::Integer
14
+ Ibox.new(n, options.dup)
15
+ when ::Float
16
+ Fbox.new(n, options.dup)
17
+ else
18
+ raise ArgumentError, "cbor-diagnostic: can't box number from #{n.inspect}':\n"
19
+ end
20
+ end
21
+ def to_cbor
22
+ CBOR.encode(value)
23
+ end
24
+ end
25
+
26
+ class Ibox < Box
27
+ INTEGER_EI = {
28
+ "i" => [23, 0],
29
+ "0" => [0xFF, 1],
30
+ "1" => [0xFFFF, 2],
31
+ "2" => [0xFFFFFFFF, 4],
32
+ "3" => [0xFFFFFFFFFFFFFFFF, 8]
33
+ }
34
+ def make_head(ib, plusbytes, d)
35
+ case plusbytes
36
+ when 0
37
+ [ib + d].pack("C")
38
+ when 1
39
+ [ib + 24, d].pack("CC")
40
+ when 2
41
+ [ib + 25, d].pack("Cn")
42
+ when 4
43
+ [ib + 26, d].pack("CN")
44
+ when 8
45
+ [ib + 27, d].pack("CQ>")
46
+ else
47
+ raise ArgumentError, "cbor-diagnostic: #{plusbytes} plusbytes when encoding head\n"
48
+ end
49
+ end
50
+
51
+ def to_cbor
52
+ if ei = options[:ei]
53
+ maxval, plusbytes = INTEGER_EI[ei]
54
+ raise ArgumentError, "cbor-diagnostic: unknown encoding indicator _#{ei} for Integer\n" unless maxval
55
+ d = value
56
+ ib = if d < 0
57
+ d = -1-d
58
+ 0x20
59
+ else
60
+ 0x00
61
+ end
62
+ raise ArgumentError, "cbor-diagnostic: #{value} doesn't fit into encoding indicator _#{ei}':\n" unless d <= maxval
63
+
64
+ make_head(ib, plusbytes, d)
65
+
66
+ # s = bignum_to_bytes(d)
67
+ # head(0xc0, TAG_BIGNUM_BASE + (ib >> 5))
68
+ # head(0x40, s.bytesize)
69
+ else
70
+ CBOR.encode(value)
71
+ end
72
+ end
73
+ end
74
+ class Fbox < Box
75
+ FLOAT_EI = {
76
+ "1" => 2,
77
+ "2" => 4,
78
+ "3" => 8
79
+ }
80
+ def make_float(plusbytes, fv)
81
+ ret =
82
+ if fv.nan?
83
+ # | Format | Sign bit | Exponent | Significand | Zero
84
+ # | binary16 | 1 | 5 | 10 | 42
85
+ # | binary32 | 1 | 8 | 23 | 29
86
+ # | binary64 | 1 | 11 | 52 | 0
87
+ ds = [fv].pack("G")
88
+ firstword = ds.unpack("n").first
89
+ raise "NaN exponent error #{firstword}" unless firstword & 0x7FF0 == 0x7FF0
90
+ iv = ds.unpack("Q>").first
91
+ ret = case plusbytes
92
+ when 2
93
+ if iv & 0x3ffffffffff == 0 # 42 zero, 10 bits fit in half
94
+ [0xf9, (firstword & 0xFC00) + ((iv >> 42) & 0x3ff)].pack("Cn")
95
+ end
96
+ when 4
97
+ if iv & 0x1fffffff == 0 # 29 zero, 23 bits fit in single
98
+ [0xfa, (ds.getbyte(0) << 24) + ((iv >> 29) & 0xffffff)].pack("CN")
99
+ end
100
+ when 8
101
+ "\xfb".b << ds
102
+ end
103
+ else
104
+ if plusbytes == 8
105
+ [0xfb, fv].pack("CG") # double-precision
106
+ else
107
+ ss = [fv].pack("g") # single-precision
108
+ if ss.unpack("g").first == fv
109
+ if plusbytes == 4
110
+ "\xfa".b << ss
111
+ else
112
+ if hs = Half.encode_from_single(fv, ss)
113
+ "\xf9".b << hs
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ raise ArgumentError, "cbor-diagnostic: make_float #{plusbytes.inspect} #{fv.inspect}" unless ret
120
+ ret
121
+ end
122
+
123
+ def to_cbor
124
+ if ei = options[:ei]
125
+ plusbytes = FLOAT_EI[ei]
126
+ raise ArgumentError, "cbor-diagnostic: unknown encoding indicator _#{ei} for Float':\n" unless plusbytes
127
+ make_float(plusbytes, value)
128
+ else
129
+ CBOR.encode(value)
130
+ end
131
+ end
132
+ end
133
+
134
+ end
data/lib/edn-abnf.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative "parser/edn-util.rb"
2
+ require_relative "cbor-diag-support.rb"
2
3
 
3
4
  class EDN
4
5
  @@parser = EDNGRAMMARParser.new
@@ -464,10 +464,13 @@ module EDNGRAMMAR
464
464
 
465
465
  module Number1
466
466
  def ast
467
+ val = elements[0].ast
468
+
467
469
  if ei = spec.text_value[1..-1]
468
- warn "*** ignoring unimplemented encoding indicator #{ei.inspect} for #{elements[0].text_value}"
470
+ # warn "*** implementing encoding indicator #{ei.inspect} for #{elements[0].text_value}"
471
+ val = CBOR::Box.from_number(val, {ei: ei})
469
472
  end
470
- elements[0].ast
473
+ val
471
474
  end
472
475
  end
473
476
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: edn-abnf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-22 00:00:00.000000000 Z
11
+ date: 2024-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: cbor-diag
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: edn-abnf implements converters and miscellaneous tools for CBOR EDN's
84
98
  ABNF
85
99
  email: cabo@tzi.org
@@ -90,6 +104,7 @@ extra_rdoc_files: []
90
104
  files:
91
105
  - bin/edn-abnf
92
106
  - edn-abnf.gemspec
107
+ - lib/cbor-diag-support.rb
93
108
  - lib/cbor-diagnostic-app/b64.rb
94
109
  - lib/cbor-diagnostic-app/b64grammar.rb
95
110
  - lib/cbor-diagnostic-app/dt.rb