Skip to content

Instantly share code, notes, and snippets.

@ujihisa
Created October 8, 2020 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ujihisa/3a963d0ed57fecd42dbd20d4afa46b38 to your computer and use it in GitHub Desktop.
Save ujihisa/3a963d0ed57fecd42dbd20d4afa46b38 to your computer and use it in GitHub Desktop.
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