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.
- checksums.yaml +4 -4
- data/lib/static_literal_parser.rb +25 -2
- metadata +2 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12ae33173996d6d6a8ca83c11ad33e985bc323c193a4774e6985b39877007e35
|
4
|
+
data.tar.gz: c9c2201c0ca3c7527693108e05797013e6742cea5a2ac22ca5b9126c082b90dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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 :
|
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.
|
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.
|
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: []
|