rbjq 1.0.0

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.
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rbjq
4
+ # A compiled jq program.
5
+ class Program
6
+ class << self
7
+ def prelude
8
+ @prelude ||= begin
9
+ defs = Parser.new(Lexer.new(PRELUDE).tokenize).parse_defs
10
+ h = {}
11
+ defs.each do |(_, name, params, body)|
12
+ h["#{name}/#{params.size}"] = FuncDef.new(name, params, body, nil)
13
+ end
14
+ Builtins.const_set(:PRELUDE_KEYS, h.keys.freeze) unless Builtins.const_defined?(:PRELUDE_KEYS)
15
+ h.freeze
16
+ end
17
+ end
18
+
19
+ def known_functions
20
+ @known_functions ||= Set.new(prelude.keys + Builtins::NATIVES.keys).freeze
21
+ end
22
+
23
+ # Convert arbitrary Ruby data into jq values (symbol keys -> strings, etc.)
24
+ def normalize_value(v)
25
+ case v
26
+ when nil, true, false, String then v
27
+ when Integer, Float then Value.normalize_number(v)
28
+ when Symbol then v.to_s
29
+ when Array then v.map { |e| normalize_value(e) }
30
+ when Hash then v.each_with_object({}) { |(k, x), h| h[k.to_s] = normalize_value(x) }
31
+ when Struct then normalize_value(v.to_h)
32
+ else
33
+ if v.respond_to?(:to_hash) then normalize_value(v.to_hash)
34
+ elsif v.is_a?(Enumerable) || v.respond_to?(:to_ary) then normalize_value(v.to_a)
35
+ else v.to_s
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ attr_reader :source, :ast
42
+
43
+ # vars: Hash of variable name (without '$') => value
44
+ # functions: Hash of name => callable, jq functions implemented in Ruby
45
+ # (see Rbjq::Functions); they take precedence over functions defined
46
+ # with Rbjq.define_function, which are captured at this point
47
+ # options: :stderr (IO for debug/stderr), :filename_proc
48
+ def initialize(source, vars: {}, functions: {}, **options)
49
+ @source = source
50
+ @ast = Parser.parse(source)
51
+ @vars = vars.each_with_object({}) { |(k, v), h| h[k.to_s.delete_prefix('$')] = self.class.normalize_value(v) }
52
+ @functions = Functions.global.merge(Functions.build_all(functions))
53
+ Checker.new(self.class.known_functions | @functions.keys).check(@ast, @vars.keys)
54
+ @options = options
55
+ # Holding back an operand's first output (see Interpreter#each_operand)
56
+ # would reorder side effects, so keep strict streaming for such programs.
57
+ @side_effects = calls_any?(@ast, SIDE_EFFECT_CALLS)
58
+ end
59
+
60
+ SIDE_EFFECT_CALLS = %w[input inputs debug stderr input_line_number].freeze
61
+
62
+ # Yields each output for the given input.
63
+ # inputs: an Enumerator/Proc used by `input`/`inputs`.
64
+ def each(input, inputs: nil, &blk)
65
+ return enum_for(:each, input, inputs: inputs) unless blk
66
+
67
+ interp = Interpreter.new(prelude: self.class.prelude, natives: Builtins::NATIVES,
68
+ options: @options.merge(defer_operands: !@side_effects),
69
+ functions: @functions)
70
+ interp.input_provider = make_provider(inputs) if inputs
71
+ env = nil
72
+ @vars.each { |k, v| env = Env.bind_var(env, k, v) }
73
+ interp.run(@ast, self.class.normalize_value(input), env, &blk)
74
+ end
75
+
76
+ def run(input, inputs: nil)
77
+ out = []
78
+ each(input, inputs: inputs) { |v| out << v }
79
+ out
80
+ end
81
+
82
+ alias call run
83
+
84
+ private
85
+
86
+ def calls_any?(node, names)
87
+ return false unless node.is_a?(Array)
88
+ return true if node[0] == :call && names.include?(node[1])
89
+
90
+ node.any? { |c| c.is_a?(Array) && calls_any?(c, names) }
91
+ end
92
+
93
+ def make_provider(inputs)
94
+ case inputs
95
+ when Proc then inputs
96
+ else
97
+ e = inputs.each
98
+ lambda do
99
+ self.class.normalize_value(e.next)
100
+ rescue StopIteration
101
+ JSONParser::EOF
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
data/lib/rbjq/value.rb ADDED
@@ -0,0 +1,188 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rbjq
4
+ # Helpers for jq value semantics on plain Ruby objects:
5
+ # nil, true, false, Integer, Float, String, Array, Hash(String => value)
6
+ module Value
7
+ MAX_SAFE = 2**53
8
+ DBL_MAX = Float::MAX
9
+
10
+ module_function
11
+
12
+ def normalize_number(n)
13
+ case n
14
+ when Integer
15
+ n.abs > MAX_SAFE ? n.to_f : n
16
+ when Float
17
+ if n.finite? && n == n.floor && n.abs <= MAX_SAFE && !(n.zero? && (1.0 / n).negative?)
18
+ n.to_i
19
+ else
20
+ n
21
+ end
22
+ else
23
+ n
24
+ end
25
+ end
26
+
27
+ def type_name(v)
28
+ case v
29
+ when nil then 'null'
30
+ when true, false then 'boolean'
31
+ when Integer, Float then 'number'
32
+ when String then 'string'
33
+ when Array then 'array'
34
+ when Hash then 'object'
35
+ else raise TypeError, "not a jq value: #{v.inspect}"
36
+ end
37
+ end
38
+
39
+ def truthy?(v)
40
+ !(v.nil? || v == false)
41
+ end
42
+
43
+ KIND_ORDER = { NilClass => 0, FalseClass => 1, TrueClass => 2, Integer => 3, Float => 3,
44
+ String => 4, Array => 5, Hash => 6 }.freeze
45
+
46
+ def kind_order(v)
47
+ KIND_ORDER[v.class] || raise(TypeError, "not a jq value: #{v.inspect}")
48
+ end
49
+
50
+ MAX_DEPTH = 10_000
51
+
52
+ def compare(a, b, depth = 0, msg = 'Comparison too deep')
53
+ ka = kind_order(a)
54
+ kb = kind_order(b)
55
+ return ka <=> kb if ka != kb
56
+
57
+ case a
58
+ when Integer, Float
59
+ a_nan = a.is_a?(Float) && a.nan?
60
+ b_nan = b.is_a?(Float) && b.nan?
61
+ return -1 if a_nan
62
+ return 1 if b_nan
63
+
64
+ a <=> b
65
+ when String
66
+ a.b <=> b.b
67
+ when Array
68
+ raise JQError, msg if depth > MAX_DEPTH
69
+
70
+ i = 0
71
+ while i < a.length && i < b.length
72
+ c = compare(a[i], b[i], depth + 1, msg)
73
+ return c unless c.zero?
74
+
75
+ i += 1
76
+ end
77
+ a.length <=> b.length
78
+ when Hash
79
+ raise JQError, msg if depth > MAX_DEPTH
80
+
81
+ ka = a.keys.sort_by(&:b)
82
+ kb = b.keys.sort_by(&:b)
83
+ i = 0
84
+ while i < ka.length && i < kb.length
85
+ c = ka[i].b <=> kb[i].b
86
+ return c unless c.zero?
87
+
88
+ i += 1
89
+ end
90
+ c = ka.length <=> kb.length
91
+ return c unless c.zero?
92
+
93
+ ka.each do |k|
94
+ c = compare(a[k], b[k], depth + 1, msg)
95
+ return c unless c.zero?
96
+ end
97
+ 0
98
+ else
99
+ 0
100
+ end
101
+ end
102
+
103
+ def equal?(a, b)
104
+ compare(a, b, 0, 'Equality check too deep').zero?
105
+ end
106
+
107
+ def sort(arr)
108
+ arr.each_with_index.sort { |(x, i), (y, j)| (c = compare(x, y)).zero? ? i <=> j : c }.map(&:first)
109
+ end
110
+
111
+ # Value dump shortened like jq 1.8 error messages: at most 29 bytes; a
112
+ # longer dump keeps its first bytes (not splitting a character), then
113
+ # "..." and the closing quote or bracket.
114
+ def dump_trunc(v)
115
+ s = Encoder.dump(v)
116
+ return s if s.bytesize <= 29
117
+
118
+ closing = ['"', '[', '{'].include?(s[0]) ? s[-1] : ''
119
+ prefix = s.byteslice(0, 26 - closing.bytesize)
120
+ prefix = prefix.byteslice(0, prefix.bytesize - 1) until prefix.valid_encoding?
121
+ "#{prefix}...#{closing}"
122
+ end
123
+
124
+ REPLACEMENT = "\uFFFD"
125
+
126
+ # Replace invalid UTF-8 the way jq does (jvp_utf8_next): a lead byte
127
+ # followed by too few continuation bytes, or cut off by the end of the
128
+ # string, becomes a single U+FFFD together with the bytes it consumed.
129
+ def scrub_utf8(str)
130
+ str = str.dup.force_encoding(Encoding::UTF_8)
131
+ return str if str.valid_encoding?
132
+
133
+ bytes = str.b
134
+ out = +''
135
+ i = 0
136
+ n = bytes.bytesize
137
+ while i < n
138
+ c = bytes.getbyte(i)
139
+ len = if c < 0x80 then 1
140
+ elsif c.between?(0xC2, 0xDF) then 2
141
+ elsif c.between?(0xE0, 0xEF) then 3
142
+ elsif c.between?(0xF0, 0xF4) then 4
143
+ else 0
144
+ end
145
+ if len.zero?
146
+ out << REPLACEMENT
147
+ i += 1
148
+ next
149
+ end
150
+ if i + len > n
151
+ out << REPLACEMENT
152
+ break
153
+ end
154
+
155
+ j = 1
156
+ j += 1 while j < len && (bytes.getbyte(i + j) & 0xC0) == 0x80
157
+ seq = bytes.byteslice(i, j).force_encoding(Encoding::UTF_8)
158
+ out << (j == len && seq.valid_encoding? ? seq : REPLACEMENT)
159
+ i += j
160
+ end
161
+ out
162
+ end
163
+
164
+ # Float() for a decimal literal, without Ruby's "out of range" warning:
165
+ # huge exponents become infinity and tiny ones zero, as strtod does.
166
+ def parse_float(str)
167
+ if (m = str.match(/\A([+-]?)(\d*)\.?(\d*)[eE]([+-]?\d+)\z/))
168
+ sign, int, frac, exp = m.captures
169
+ digits = (int + frac).sub(/\A0+/, '')
170
+ unless digits.empty?
171
+ magnitude = exp.to_i + int.sub(/\A0+/, '').length
172
+ magnitude = exp.to_i - frac[/\A0*/].length if int.sub(/\A0+/, '').empty?
173
+ return (sign == '-' ? -Float::INFINITY : Float::INFINITY) if magnitude > 400
174
+ return (sign == '-' ? -1 : 1) * 0.0 if magnitude < -400 # keeps the sign of zero
175
+ end
176
+ end
177
+ Float(str)
178
+ end
179
+
180
+ def desc(v)
181
+ "#{type_name(v)} (#{dump_trunc(v)})"
182
+ end
183
+
184
+ def number?(v)
185
+ v.is_a?(Integer) || v.is_a?(Float)
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rbjq
4
+ VERSION = '1.0.0'
5
+ end
data/lib/rbjq.rb ADDED
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rbjq/version'
4
+
5
+ module Rbjq
6
+ class Error < StandardError; end
7
+
8
+ # Syntax or compile-time (undefined function/variable) errors.
9
+ class CompileError < Error; end
10
+
11
+ # Runtime error raised by a jq program. `value` is the error value
12
+ # (usually a String message, but `error({...})` can raise any value).
13
+ class JQError < Error
14
+ attr_reader :value
15
+
16
+ def initialize(value = nil)
17
+ @value = value
18
+ super(value.is_a?(String) ? value : "#{Encoder.dump(value)} (not a string)")
19
+ end
20
+ end
21
+
22
+ # Invalid JSON text.
23
+ class ParseError < JQError; end
24
+
25
+ # Raised by halt / halt_error.
26
+ class HaltError < Error
27
+ attr_reader :value, :code
28
+
29
+ def initialize(value, code, has_value)
30
+ @value = value
31
+ @code = code
32
+ @has_value = has_value
33
+ super("halt (#{code})")
34
+ end
35
+
36
+ def value?
37
+ @has_value
38
+ end
39
+ end
40
+ end
41
+
42
+ require_relative 'rbjq/value'
43
+ require_relative 'rbjq/json'
44
+ require_relative 'rbjq/lexer'
45
+ require_relative 'rbjq/parser'
46
+ require_relative 'rbjq/env'
47
+ require_relative 'rbjq/interpreter'
48
+ require_relative 'rbjq/ops'
49
+ require_relative 'rbjq/formats'
50
+ require_relative 'rbjq/builtins'
51
+ require_relative 'rbjq/prelude'
52
+ require_relative 'rbjq/checker'
53
+ require_relative 'rbjq/program'
54
+ require_relative 'rbjq/functions'
55
+
56
+ module Rbjq
57
+ class << self
58
+ # Compile a jq filter.
59
+ #
60
+ # prog = Rbjq.compile('.[] | select(.age > $min)', vars: { 'min' => 20 })
61
+ # prog.run(data) #=> [...]
62
+ def compile(filter, vars: {}, **)
63
+ Program.new(filter, vars: vars, **)
64
+ end
65
+
66
+ # Run a jq filter against a Ruby value and return all outputs as an Array.
67
+ #
68
+ # Rbjq.query({ 'a' => [1, 2] }, '.a[]') #=> [1, 2]
69
+ def query(input, filter, vars: {}, **)
70
+ compile(filter, vars: vars, **).run(input)
71
+ end
72
+
73
+ # Run a jq filter against JSON text (which may contain several values)
74
+ # and return the outputs as an Array of Ruby values.
75
+ def query_json(json, filter, vars: {}, **)
76
+ prog = compile(filter, vars: vars, **)
77
+ JSONParser.parse_all(json).flat_map { |v| prog.run(v) }
78
+ end
79
+
80
+ # Parse JSON text into Ruby values (jq-compatible numbers).
81
+ def parse_json(json)
82
+ JSONParser.parse(json)
83
+ end
84
+
85
+ # Encode a Ruby value as JSON text the way jq prints it.
86
+ def dump_json(value, indent: 0, sort_keys: false)
87
+ Encoder.new(indent: indent, sort_keys: sort_keys).encode(Program.normalize_value(value))
88
+ end
89
+ end
90
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbjq
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - winebarrel
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: rbjq is a jq-compatible JSON processor written in pure Ruby, usable both
13
+ as a library and as a command line tool.
14
+ email:
15
+ - sugawara@winebarrel.jp
16
+ executables:
17
+ - rbjq
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - LICENSE-jq
23
+ - README.md
24
+ - exe/rbjq
25
+ - lib/rbjq.rb
26
+ - lib/rbjq/bessel.rb
27
+ - lib/rbjq/builtins.rb
28
+ - lib/rbjq/checker.rb
29
+ - lib/rbjq/cli.rb
30
+ - lib/rbjq/env.rb
31
+ - lib/rbjq/formats.rb
32
+ - lib/rbjq/functions.rb
33
+ - lib/rbjq/interpreter.rb
34
+ - lib/rbjq/json.rb
35
+ - lib/rbjq/lexer.rb
36
+ - lib/rbjq/ops.rb
37
+ - lib/rbjq/parser.rb
38
+ - lib/rbjq/prelude.rb
39
+ - lib/rbjq/program.rb
40
+ - lib/rbjq/value.rb
41
+ - lib/rbjq/version.rb
42
+ homepage: https://github.com/winebarrel/rbjq
43
+ licenses:
44
+ - CC0-1.0
45
+ metadata:
46
+ homepage_uri: https://github.com/winebarrel/rbjq
47
+ source_code_uri: https://github.com/winebarrel/rbjq
48
+ rubygems_mfa_required: 'true'
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '3.2'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 4.0.3
64
+ specification_version: 4
65
+ summary: A pure Ruby implementation of jq
66
+ test_files: []