edn-abnf 0.3.1 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e8b236b54352f4702a4d5995eaaab5d7578addef93777e1a722c668ac1c6202
4
- data.tar.gz: 3168853cab882f8db393a9e5921a527d788f658f63fb3877c45315f9cc078ca7
3
+ metadata.gz: a7e57901f15e144386956e94dd3aaa6b808786e4d41b7ff5b5c1c5bc54c0df35
4
+ data.tar.gz: 35f7b7e937487e76f4f5f21ee265346099274eb62e87675912c28a49b57a68b1
5
5
  SHA512:
6
- metadata.gz: 5ae982135b96e6fa336b23a77d106a0d759cc112e2770cef7532dfd69ac0dee29342f5192c60ffa5aef927d0bad16960f9e9b174535200ebcf9ab568f1a030f2
7
- data.tar.gz: a16821958a1e34e3cdd6dd1040440f9e964ea39adbd2af0a0f965a6246d2a8964fbeae17dcd39ad59376a43f50e7542d3085648356f66ee010d44f3346b74245
6
+ metadata.gz: aeba40ba7ed1e0c9e19da2b10e236fce5d15b1cead0c128050c9b40b0500787a0377126d83f413582bdc04199534b58830dd105ed3bb4944c57d8ba90ce808e3
7
+ data.tar.gz: c5cca7b60d9059f98956d1aa18eae7a039d5de44e623e296d59019d27ded411063eb9dc584f6e82391d7b0d1b20206e789dd62694e95c4827b3179138742a99c
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.3"
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,141 @@
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
+ def cbor_diagnostic(opts = {})
25
+ ret = value.cbor_diagnostic(opts)
26
+ if ei = options[:ei]
27
+ ret << "_#{ei}"
28
+ end
29
+ ret
30
+ end
31
+ end
32
+
33
+ class Ibox < Box
34
+ INTEGER_EI = {
35
+ "i" => [23, 0],
36
+ "0" => [0xFF, 1],
37
+ "1" => [0xFFFF, 2],
38
+ "2" => [0xFFFFFFFF, 4],
39
+ "3" => [0xFFFFFFFFFFFFFFFF, 8]
40
+ }
41
+ def make_head(ib, plusbytes, d)
42
+ case plusbytes
43
+ when 0
44
+ [ib + d].pack("C")
45
+ when 1
46
+ [ib + 24, d].pack("CC")
47
+ when 2
48
+ [ib + 25, d].pack("Cn")
49
+ when 4
50
+ [ib + 26, d].pack("CN")
51
+ when 8
52
+ [ib + 27, d].pack("CQ>")
53
+ else
54
+ raise ArgumentError, "cbor-diagnostic: #{plusbytes} plusbytes when encoding head\n"
55
+ end
56
+ end
57
+
58
+ def to_cbor
59
+ if ei = options[:ei]
60
+ maxval, plusbytes = INTEGER_EI[ei]
61
+ raise ArgumentError, "cbor-diagnostic: unknown encoding indicator _#{ei} for Integer\n" unless maxval
62
+ d = value
63
+ ib = if d < 0
64
+ d = -1-d
65
+ 0x20
66
+ else
67
+ 0x00
68
+ end
69
+ raise ArgumentError, "cbor-diagnostic: #{value} doesn't fit into encoding indicator _#{ei}':\n" unless d <= maxval
70
+
71
+ make_head(ib, plusbytes, d)
72
+
73
+ # s = bignum_to_bytes(d)
74
+ # head(0xc0, TAG_BIGNUM_BASE + (ib >> 5))
75
+ # head(0x40, s.bytesize)
76
+ else
77
+ CBOR.encode(value)
78
+ end
79
+ end
80
+ end
81
+ class Fbox < Box
82
+ FLOAT_EI = {
83
+ "1" => 2,
84
+ "2" => 4,
85
+ "3" => 8
86
+ }
87
+ def make_float(plusbytes, fv)
88
+ ret =
89
+ if fv.nan?
90
+ # | Format | Sign bit | Exponent | Significand | Zero
91
+ # | binary16 | 1 | 5 | 10 | 42
92
+ # | binary32 | 1 | 8 | 23 | 29
93
+ # | binary64 | 1 | 11 | 52 | 0
94
+ ds = [fv].pack("G")
95
+ firstword = ds.unpack("n").first
96
+ raise "NaN exponent error #{firstword}" unless firstword & 0x7FF0 == 0x7FF0
97
+ iv = ds.unpack("Q>").first
98
+ ret = case plusbytes
99
+ when 2
100
+ if iv & 0x3ffffffffff == 0 # 42 zero, 10 bits fit in half
101
+ [0xf9, (firstword & 0xFC00) + ((iv >> 42) & 0x3ff)].pack("Cn")
102
+ end
103
+ when 4
104
+ if iv & 0x1fffffff == 0 # 29 zero, 23 bits fit in single
105
+ [0xfa, (ds.getbyte(0) << 24) + ((iv >> 29) & 0xffffff)].pack("CN")
106
+ end
107
+ when 8
108
+ "\xfb".b << ds
109
+ end
110
+ else
111
+ if plusbytes == 8
112
+ [0xfb, fv].pack("CG") # double-precision
113
+ else
114
+ ss = [fv].pack("g") # single-precision
115
+ if ss.unpack("g").first == fv
116
+ if plusbytes == 4
117
+ "\xfa".b << ss
118
+ else
119
+ if hs = Half.encode_from_single(fv, ss)
120
+ "\xf9".b << hs
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ raise ArgumentError, "cbor-diagnostic: make_float #{plusbytes.inspect} #{fv.inspect}" unless ret
127
+ ret
128
+ end
129
+
130
+ def to_cbor
131
+ if ei = options[:ei]
132
+ plusbytes = FLOAT_EI[ei]
133
+ raise ArgumentError, "cbor-diagnostic: unknown encoding indicator _#{ei} for Float':\n" unless plusbytes
134
+ make_float(plusbytes, value)
135
+ else
136
+ CBOR.encode(value)
137
+ end
138
+ end
139
+ end
140
+
141
+ 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.3
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-28 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