static_literal_parser 1.1.1 → 1.3.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/static_literal_parser.rb +25 -2
  3. metadata +2 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b459bba28eff1d442f3b263827bd3fac5899c57e8f7eb6505b8f88f377ed0f4
4
- data.tar.gz: '0937a15bf39622e403c00a37f0b4457321b039ca5a5afd71ee1208708273b4a7'
3
+ metadata.gz: 12ae33173996d6d6a8ca83c11ad33e985bc323c193a4774e6985b39877007e35
4
+ data.tar.gz: c9c2201c0ca3c7527693108e05797013e6742cea5a2ac22ca5b9126c082b90dd
5
5
  SHA512:
6
- metadata.gz: 2eb6b4fbbf20b88b961caa97e6a8f197531a78b563a88124f18149d02ead1e21bd8817df05a99ae2cc906a0470b09199693859468806c3dbccca0491cac1ade2
7
- data.tar.gz: 8bb488da5a444b33cbb4ef11bb9946375c3ced23f7feee56ace30a2faca1475dab605337027017718eb5e4fc349eac9c62977c5f213f824490102d9acaadcbba
6
+ metadata.gz: e53a3748ac894be86a680d1db09e65220cbfa5cc7577265929bc024487092f54849e7f60caf775a013cb5cb93650ab1bde876aa729971e1e289b6c0f0211225e
7
+ data.tar.gz: 04f9828734feb07f82f906ff8e31f2a2cbfb66c11878ac13a28eca886b891a91b3859a2480736d5bb1f3f3b8b90ba2fc6e332d297083839599a9811a6eedebf2
@@ -2,10 +2,18 @@
2
2
 
3
3
  module StaticLiteralParser
4
4
  def self.parse(str, constants)
5
- parse_(RubyVM::AbstractSyntaxTree.parse(str), constants)
5
+ case constants
6
+ when Array
7
+ parse(str, constant_strs_array_to_hash(constants))
8
+ when Hash
9
+ parse_(RubyVM::AbstractSyntaxTree.parse(str), constants)
10
+ else
11
+ raise ArgumentError, "Unsupported constants type: #{constants}"
12
+ end
6
13
  end
7
14
 
8
15
  private_class_method def self.parse_(node, constants)
16
+ return unless node
9
17
  case node.type
10
18
  when :SCOPE
11
19
  parse_(node.children[2], constants)
@@ -13,8 +21,12 @@ module StaticLiteralParser
13
21
  node.children[0..-2].map {|x| parse_(x, constants) }
14
22
  when :HASH
15
23
  Hash[*parse_(node.children[0], constants)]
16
- when :LIT, :STR, :NIL
24
+ when :INTEGER, :FLOAT, :SYM, :IMAGINARY, :RATIONAL, :STR, :REGX, :NIL
17
25
  node.children[0]
26
+ when :TRUE
27
+ true
28
+ when :FALSE
29
+ false
18
30
  when :CONST
19
31
  constants[node.children[0]] or raise "Missing const: #{node.children[0]}"
20
32
  when :COLON2
@@ -32,4 +44,15 @@ module StaticLiteralParser
32
44
  raise "Unexpected node #{node.type}"
33
45
  end
34
46
  end
47
+
48
+ private_class_method def self.constant_strs_array_to_hash(constant_strs_array)
49
+ constant_strs_array.flat_map {|constant|
50
+ constant.split('::').inject([]) {|memo, part|
51
+ [*memo, [memo.last, part].compact.join('::')]
52
+ }
53
+ }.to_h {|name|
54
+ c = Object.const_get(name)
55
+ [name.to_sym, c]
56
+ }
57
+ end
35
58
  end
metadata CHANGED
@@ -1,11 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: static_literal_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tatsuhiro Ujihisa
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
10
  date: 2020-11-01 00:00:00.000000000 Z
@@ -24,7 +23,6 @@ homepage: https://github.com/ujihisa/static_literal_parser
24
23
  licenses:
25
24
  - GPL-3.0-or-later
26
25
  metadata: {}
27
- post_install_message:
28
26
  rdoc_options: []
29
27
  require_paths:
30
28
  - lib
@@ -39,8 +37,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
37
  - !ruby/object:Gem::Version
40
38
  version: '0'
41
39
  requirements: []
42
- rubygems_version: 3.2.0.rc.2
43
- signing_key:
40
+ rubygems_version: 3.6.2
44
41
  specification_version: 4
45
42
  summary: StaticLiteralParser.parse(str, constants)
46
43
  test_files: []