jpt 0.0.1 → 0.0.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: af31eebfdf3961e232c44fce1d5d43ee0c483ce3cf8406fb654a12a874286cba
4
- data.tar.gz: 2bd781845060b59cd924edf1f84408f8c47e1c9d8373ae3a21296fce7ad2ee8b
3
+ metadata.gz: cbc6d1c075c3cc8c7cb51d81dc564daab584b89d802d11b169e2657efc1d4f2a
4
+ data.tar.gz: 59bc9814077744bbaee805fd5a9e2991c548708504a4fcb0571644b25bc97c05
5
5
  SHA512:
6
- metadata.gz: 6f4d08616d657cb0f42e9bf90e251ec81792cc4568e3817cf58acb880f23d8558a355e2564383fb0e8d1f91b7d2cefdf5cfb63f3bf28662bc4d6def3e9a9d5ff
7
- data.tar.gz: 6e994cde0c251ef44b70959d160ae4c541afc9b2f7ee12d520c0343bc9f95b0ca9058fd6249e9a0b2a9bc5b2b4ccf45cfbe27968ecd4921edc463d1eb061e0b2
6
+ metadata.gz: 1b99d81d4482db5157946c9c487a2c2194a905d03d5cf61658ba49e2c071af3fcde3185807e1cda9873175f5df2d7ca28dd04685fdddf178ec09acc9f1bb0856
7
+ data.tar.gz: 85623a3e0467b4f80844dc18d4da942ffe3c03f3ff180bfbad800f0457799b7e515770f301097dfb6f41fb2d5e5e9f8bbc48c020f9a45fabc0f6371ce2570e2a
data/bin/jpt CHANGED
@@ -4,7 +4,7 @@ require 'yaml'
4
4
  require 'treetop'
5
5
  require 'json'
6
6
 
7
- require_relative '../lib/jpt.rb'
7
+ require_relative '../lib/jpt'
8
8
 
9
9
  Encoding.default_external = Encoding::UTF_8
10
10
  require 'optparse'
data/jpt.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "jpt"
3
- s.version = "0.0.1"
3
+ s.version = "0.0.2"
4
4
  s.summary = "JSONPath tools"
5
5
  s.description = %q{jpt implements converters and miscellaneous tools for JSONPath}
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'
19
+ s.add_dependency 'neatjson', '~>0.10'
20
20
  end
data/lib/jpt.rb CHANGED
@@ -1,7 +1,51 @@
1
+ module JPTType
2
+ # :value, :nodes, :logical
3
+
4
+ FUNCTABLE = {
5
+ "length" => [:value, :value],
6
+ "count" => [:value, :nodes],
7
+ "match" => [:logical, :value, :value],
8
+ "search" => [:logical, :value, :value],
9
+ }
10
+
11
+ def declared_type(ast)
12
+ case ast
13
+ in Numeric | String | false | true | nil
14
+ :value
15
+ in ["@", *] | ["$", *]
16
+ :nodes
17
+ in ["func", funcname, *funcargs]
18
+ ret, *parms = FUNCTABLE[funcname]
19
+ if parms.length != funcargs.length
20
+ warn "*** Incorrect number of arguments #{ast} #{parms.inspect} #{funcargs.inspect}"
21
+ else
22
+ parms.zip(funcargs).each do |pm, ar|
23
+ declared_as(ar, pm, " in #{ast}") # XXX overhead
24
+ end
25
+ end
26
+ ret
27
+ end
28
+ end
29
+
30
+ def declared_as(ast, rt, s = "")
31
+ dt = declared_type(ast)
32
+ case [dt, rt]
33
+ in a, b if a == b
34
+ true
35
+ in [:nodes, :value] | [:nodes, :logical]
36
+ true
37
+ else
38
+ warn "*** Cannot use #{ast} with declared_type #{dt||:undefined} for required type #{rt}#{s}"
39
+ false
40
+ end
41
+ end
42
+ end
43
+
1
44
  require_relative "parser/jpt-util.rb"
2
45
 
3
46
  class JPT
4
47
  @@parser = JPTGRAMMARParser.new
48
+ include ::JPTType
5
49
 
6
50
  DATA_DIR = Pathname.new(__FILE__) + "../../data/"
7
51
 
@@ -1,18 +1,13 @@
1
1
  require 'treetop'
2
2
  require_relative './jptgrammar'
3
+ require_relative '../jpt'
3
4
 
4
5
  class Treetop::Runtime::SyntaxNode
6
+ include ::JPTType
5
7
  def ast
6
8
  fail "undefined_ast #{inspect}"
7
9
  end
8
10
  def ast1 # devhack
9
11
  "#{inspect[10..20]}--#{text_value[0..15]}"
10
12
  end
11
- def repwrap(el, val)
12
- if el.text_value == ''
13
- val
14
- else
15
- ["rep", *el.ast, val]
16
- end
17
- end
18
13
  end
@@ -2309,10 +2309,14 @@ module JPTGRAMMAR
2309
2309
 
2310
2310
  module TestExpr2
2311
2311
  def ast
2312
+ nest_ast = elements[1].ast
2313
+ if nest_ast[0] == "func"
2314
+ declared_as(nest_ast, :logical, " in test expression")
2315
+ end
2312
2316
  if elements[0].text_value == ''
2313
- elements[1].ast
2317
+ nest_ast
2314
2318
  else
2315
- ["not", elements[1].ast]
2319
+ ["not", nest_ast]
2316
2320
  end
2317
2321
  end
2318
2322
  end
@@ -2646,7 +2650,11 @@ module JPTGRAMMAR
2646
2650
 
2647
2651
  module ComparisonExpr1
2648
2652
  def ast
2649
- [comparison_op.text_value, elements[0].ast, elements[4].ast]
2653
+ sides = [elements[0].ast, elements[4].ast]
2654
+ sides.each do |s|
2655
+ declared_as(s, :value, " in comparable")
2656
+ end
2657
+ [comparison_op.text_value, *sides]
2650
2658
  end
2651
2659
  end
2652
2660
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jpt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.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: 2023-02-19 00:00:00.000000000 Z
11
+ date: 2023-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,48 +38,28 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1'
41
- - !ruby/object:Gem::Dependency
42
- name: json
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: neatjson
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
- - - ">="
45
+ - - "~>"
60
46
  - !ruby/object:Gem::Version
61
- version: '0'
47
+ version: '0.10'
62
48
  type: :runtime
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
- - - ">="
52
+ - - "~>"
67
53
  - !ruby/object:Gem::Version
68
- version: '0'
54
+ version: '0.10'
69
55
  description: jpt implements converters and miscellaneous tools for JSONPath
70
56
  email: cabo@tzi.org
71
57
  executables:
72
- - cddlc~
73
- - jpc~
74
58
  - jpt
75
- - jpt~
76
59
  extensions: []
77
60
  extra_rdoc_files: []
78
61
  files:
79
- - bin/cddlc~
80
- - bin/jpc~
81
62
  - bin/jpt
82
- - bin/jpt~
83
63
  - jpt.gemspec
84
64
  - lib/jpt.rb
85
65
  - lib/parser/jpt-util.rb
data/bin/cddlc~ DELETED
@@ -1,131 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'pp'
3
- require 'yaml'
4
- require 'treetop'
5
- require 'json'
6
-
7
- require_relative '../lib/cddlc.rb'
8
-
9
- def named_keys(tree)
10
- # warn [:T, tree].inspect
11
- fail unless Array === tree
12
- case tree[0]
13
- when "seq", "gcho", "tcho"
14
- tree[1..-1].flat_map {|x| named_keys(x)}
15
- when "rep"
16
- named_keys(tree[3])
17
- when "mem"
18
- key = tree[1]
19
- if Array === key && key[0] == "name"
20
- [key[1]]
21
- end
22
- end || []
23
- end
24
-
25
- def integer_value(tree)
26
- tree and (
27
- fail tree.inspect unless Array === tree
28
- case tree[0]
29
- when "number"
30
- tree[1]
31
- # XXX: could add other cases
32
- end
33
- )
34
- end
35
-
36
- def snaky(name)
37
- name.gsub(/-/, "_")
38
- end
39
-
40
- Encoding.default_external = Encoding::UTF_8
41
- require 'optparse'
42
- require 'ostruct'
43
-
44
- $options = OpenStruct.new
45
- begin
46
- op = OptionParser.new do |opts|
47
- opts.banner = "Usage: cddlc.rb [options] file.cddl"
48
-
49
- opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
50
- $options.verbose = v
51
- end
52
- opts.on("-r", "--[no-]rules", "Process rules") do |v|
53
- $options.rules = v
54
- end
55
- opts.on("-x", "--[no-]expand", "Expand generics") do |v|
56
- $options.expand_generics = v
57
- end
58
- opts.on("-u", "--[no-]undefined", "Note undefined names") do |v|
59
- $options.note_undefined = v
60
- end
61
- opts.on("-2", "--[no-]cddl2", "Perform some CDDL 2.0 processing") do |v|
62
- $options.cddl2 = v
63
- end
64
- opts.on("-tFMT", "--to=FMT", [:basic, :neat, :json, :yaml, :enum, :cddl], "Target format") do |v|
65
- $options.target = v
66
- end
67
- end
68
- op.parse!
69
- rescue Exception => e
70
- warn e
71
- exit 1
72
- end
73
-
74
- if ARGV == []
75
- puts op
76
- exit 1
77
- end
78
- cddl_file = ARGF.read
79
-
80
- cddl = CDDL.from_cddl(cddl_file)
81
- result = if $options.rules
82
- if $options.expand_generics
83
- require_relative "../lib/processor/cddl-expander.rb"
84
- cddl.expand_generics
85
- end
86
- cddl.rules
87
- else
88
- warn "** can't expand in tree; use -r flag as well" if $options.expand_generics
89
- cddl.tree
90
- end
91
-
92
- warn "** can't note undefined for target != cddl" if $options.note_undefined && $options.target != :cddl
93
-
94
- case $options.target
95
- when :basic, nil
96
- pp result
97
- when :neat, :json
98
- require 'neatjson'
99
- puts JSON.neat_generate(result, after_comma: 1, after_colon: 1)
100
- when :yaml
101
- puts result.to_yaml
102
- when :enum
103
- rules = cddl.rules
104
- rules.each do |k, v|
105
- if v[0] == "map"
106
- nk = v[1..-1].flat_map {|e| named_keys(e)}
107
- # warn [:NK, k, nk].inspect
108
- nk = nk.map {|name| i = integer_value(rules[name]); [name, i] if i}.compact
109
- # warn [:NK, k, nk].inspect
110
- if nk != []
111
- puts "enum #{snaky(k).downcase}_keys {"
112
- nk.each do |n, v|
113
- puts " #{snaky(n).upcase} = #{v},"
114
- end
115
- puts "};"
116
- end
117
- end
118
- end
119
- when :cddl
120
- require_relative '../lib/writer/cddl-writer.rb'
121
- if $options.note_undefined
122
- require_relative '../lib/processor/cddl-undefined.rb'
123
- undef_rule = cddl.cddl_undefined
124
- undef_rule.each do |k|
125
- puts ";;; *** undefined: #{k}"
126
- end
127
- end
128
- puts cddl.to_s
129
- else
130
- warn ["Unknown target format: ", $options.target].inspect
131
- end
data/bin/jpc~ DELETED
@@ -1,53 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'pp'
3
- require 'yaml'
4
- require 'treetop'
5
- require 'json'
6
-
7
- require_relative '../lib/jpc.rb'
8
-
9
- Encoding.default_external = Encoding::UTF_8
10
- require 'optparse'
11
- require 'ostruct'
12
-
13
- $options = OpenStruct.new
14
- begin
15
- op = OptionParser.new do |opts|
16
- opts.banner = "Usage: jpc.rb [options] file.jp"
17
-
18
- opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
19
- $options.verbose = v
20
- end
21
- opts.on("-tFMT", "--to=FMT", [:basic, :neat, :json, :yaml, :enum, :jp], "Target format") do |v|
22
- $options.target = v
23
- end
24
- end
25
- op.parse!
26
- rescue Exception => e
27
- warn e
28
- exit 1
29
- end
30
-
31
- if ARGV == []
32
- puts op
33
- exit 1
34
- end
35
- jpc_file = ARGF.read
36
-
37
- jpc = JPC.from_jpc(jpc_file)
38
- result = jp.tree
39
-
40
- case $options.target
41
- when :basic, nil
42
- pp result
43
- when :neat, :json
44
- require 'neatjson'
45
- puts JSON.neat_generate(result, after_comma: 1, after_colon: 1)
46
- when :yaml
47
- puts result.to_yaml
48
- when :jp
49
- require_relative '../lib/writer/jp-writer.rb'
50
- puts jpc.to_s
51
- else
52
- warn ["Unknown target format: ", $options.target].inspect
53
- end
data/bin/jpt~ DELETED
@@ -1,53 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'pp'
3
- require 'yaml'
4
- require 'treetop'
5
- require 'json'
6
-
7
- require_relative '../lib/jpt.rb'
8
-
9
- Encoding.default_external = Encoding::UTF_8
10
- require 'optparse'
11
- require 'ostruct'
12
-
13
- $options = OpenStruct.new
14
- begin
15
- op = OptionParser.new do |opts|
16
- opts.banner = "Usage: jpt.rb [options] file.jp"
17
-
18
- opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
19
- $options.verbose = v
20
- end
21
- opts.on("-tFMT", "--to=FMT", [:basic, :neat, :json, :yaml, :enum, :jp], "Target format") do |v|
22
- $options.target = v
23
- end
24
- end
25
- op.parse!
26
- rescue Exception => e
27
- warn e
28
- exit 1
29
- end
30
-
31
- if ARGV == []
32
- puts op
33
- exit 1
34
- end
35
- jp_file = ARGF.read
36
-
37
- jpt = JPT.from_jp(jp_file)
38
- result = jpt.tree
39
-
40
- case $options.target
41
- when :basic, nil
42
- pp result
43
- when :neat, :json
44
- require 'neatjson'
45
- puts JSON.neat_generate(result, after_comma: 1, after_colon: 1)
46
- when :yaml
47
- puts result.to_yaml
48
- when :jp
49
- require_relative '../lib/writer/jp-writer.rb'
50
- puts jpt.to_s
51
- else
52
- warn ["Unknown target format: ", $options.target].inspect
53
- end