Created
October 8, 2020 19:32
-
-
Save ujihisa/3a963d0ed57fecd42dbd20d4afa46b38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse(str, constants) | |
parse_(RubyVM::AbstractSyntaxTree.parse(str), constants) | |
end | |
def parse_(node, constants) | |
case node.type | |
when :SCOPE | |
parse_(node.children[2], constants) | |
when :LIST | |
node.children[..-2].map { parse_(_1, constants) } | |
when :HASH | |
Hash[*parse_(node.children[0], constants)] | |
when :LIT, :STR | |
node.children[0] | |
when :CONST | |
constants[node.children[0]] or raise "Missing const: #{node.children[0]}" | |
when :COLON2 | |
(parent, child) = node.children | |
case parent.type | |
when :CONST, :COLON2 | |
parent = parse_(parent, constants) | |
constants[:"#{parent}::#{child}"] or raise "Missing nested const: #{[parent, child]}" | |
else | |
raise "Unexpected COLON2 parent #{parent.type}" | |
end | |
else | |
raise "Unexpected node.type #{node.type}" | |
end | |
end | |
module A | |
module B | |
module C | |
end | |
end | |
end | |
pp parse('A::B::C', {A: A, 'A::B': A::B, 'A::B::C': A::B::C}) | |
# def parse(str, constants) | |
parse_(RubyVM::AbstractSyntaxTree.parse(str), constants) | |
end | |
def parse_(node, constants) | |
case node.type | |
when :SCOPE | |
parse_(node.children[2], constants) | |
when :LIST | |
node.children[..-2].map { parse_(_1, constants) } | |
when :HASH | |
Hash[*parse_(node.children[0], constants)] | |
when :LIT, :STR | |
node.children[0] | |
when :CONST | |
constants[node.children[0]] or raise "Missing const: #{node.children[0]}" | |
when :COLON2 | |
(parent, child) = node.children | |
case parent.type | |
when :CONST, :COLON2 | |
parent = parse_(parent, constants) | |
constants[:"#{parent}::#{child}"] or raise "Missing nested const: #{[parent, child]}" | |
else | |
raise "Unexpected COLON2 parent #{parent.type}" | |
end | |
else | |
raise "Unexpected node.type #{node.type}" | |
end | |
end | |
module A | |
module B | |
module C | |
end | |
end | |
end | |
pp parse('A::B::C', {A: A, 'A::B': A::B, 'A::B::C': A::B::C}) | |
# A::B::C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment