static_literal_parser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/static_literal_parser.rb +33 -0
  3. metadata +43 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bef75aedec92b2a8537588268fa5f323fa0ef20327fad37b8600870d6be334c9
4
+ data.tar.gz: a93d7994e01601f3ef0d6b59b6c423334c2d90fec7f6496d571e4a9b8d2aef23
5
+ SHA512:
6
+ metadata.gz: 2062b397ac78c50252f2c7c85d669e7341962a306aacf9cb0f377337d9c60cb2f0356e86adaaf0d45938f71211c4948a8c76f4cbff1a1cb0561758ffc23c55eb
7
+ data.tar.gz: a3381a952bcfa265b62df1c855b5014f73d146d03204d0dece25758207b0d03e4974a7959b44083e85c79f63843826147f2e42a0b63b157eb8bf1ab28ff95ad6
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StaticLiteralParser
4
+ def self.parse(str, constants)
5
+ parse_(RubyVM::AbstractSyntaxTree.parse(str), constants)
6
+ end
7
+
8
+ private_class_method def self.parse_(node, constants)
9
+ case node.type
10
+ when :SCOPE
11
+ parse_(node.children[2], constants)
12
+ when :LIST, :ZLIST
13
+ node.children[..-2].map { parse_(_1, constants) }
14
+ when :HASH
15
+ Hash[*parse_(node.children[0], constants)]
16
+ when :LIT, :STR
17
+ node.children[0]
18
+ when :CONST
19
+ constants[node.children[0]] or raise "Missing const: #{node.children[0]}"
20
+ when :COLON2
21
+ (parent, child) = node.children
22
+ case parent.type
23
+ when :CONST, :COLON2
24
+ parent = parse_(parent, constants)
25
+ constants[:"#{parent}::#{child}"] or raise "Missing nested const: #{[parent, child]}"
26
+ else
27
+ raise "Unexpected COLON2 parent #{parent.type}"
28
+ end
29
+ else
30
+ raise "Unexpected node #{node.type}"
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: static_literal_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tatsuhiro Ujihisa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-10-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: description
14
+ email: ujihisa at gmail com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/static_literal_parser.rb
20
+ homepage: https://github.com/ujihisa/static_literal_parser
21
+ licenses:
22
+ - GPL-3.0-or-later
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.2.0.rc.1
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: summary
43
+ test_files: []