edn-abnf 0.3.1 → 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: 0e8b236b54352f4702a4d5995eaaab5d7578addef93777e1a722c668ac1c6202
4
- data.tar.gz: 3168853cab882f8db393a9e5921a527d788f658f63fb3877c45315f9cc078ca7
3
+ metadata.gz: e17fb20ba9bb383a9172ef8bb3f04e2c473ec1d215972711ecac235dbf3da7d6
4
+ data.tar.gz: a9908d2846aef7899ec5ff96b4c0613a69019650239d89aa63c7cac1e8773ed7
5
5
  SHA512:
6
- metadata.gz: 5ae982135b96e6fa336b23a77d106a0d759cc112e2770cef7532dfd69ac0dee29342f5192c60ffa5aef927d0bad16960f9e9b174535200ebcf9ab568f1a030f2
7
- data.tar.gz: a16821958a1e34e3cdd6dd1040440f9e964ea39adbd2af0a0f965a6246d2a8964fbeae17dcd39ad59376a43f50e7542d3085648356f66ee010d44f3346b74245
6
+ metadata.gz: cd4641f2f4cdf9a48d79ab41f9891f29578d2c0c6d277c468e4ed92c6ed32756611375db720e30fb39e097ee3819d18902e71a330e666b97bd55d551219255b4
7
+ data.tar.gz: 62586e9cc378e802565f3c2e746bad989b7803c9a38a336b2c88db480dcf8df00125f254e30eafe67d3e80d1dbe920a4a9db24418e6aa0379fb9890214fc7fda
data/bin/edn-abnf CHANGED
@@ -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
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.3.1"
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"
@@ -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.3.1
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-26 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
@@ -104,6 +104,7 @@ extra_rdoc_files: []
104
104
  files:
105
105
  - bin/edn-abnf
106
106
  - edn-abnf.gemspec
107
+ - lib/cbor-diag-support.rb
107
108
  - lib/cbor-diagnostic-app/b64.rb
108
109
  - lib/cbor-diagnostic-app/b64grammar.rb
109
110
  - lib/cbor-diagnostic-app/dt.rb