Class: Types::Parser

Inherits:
BasicObject
Defined in:
lib/types/parser.rb

Overview

Parses type signatures, must be defined at the top level for the purpose of name resolution.

Defined Under Namespace

Modules: TOP

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.const_missing(name) ⇒ Object

Handles missing constants by creating Named types for unknown types. This allows parsing of type signatures with unknown types.



15
16
17
# File 'lib/types/parser.rb', line 15

def self.const_missing(name)
	::Types::Named.new(name.to_s)
end

Instance Method Details

#parse(signature) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/types/parser.rb', line 19

def parse(signature)
	# Validate the signature format:
	unless signature.match?(/\A[a-zA-Z0-9\(\):,_|\s]+\z/)
		::Kernel.raise ::ArgumentError, "Invalid type signature: #{signature.inspect}!"
	end
	
	# Replace leading :: with TOP:: to handle absolute type paths
	normalized_signature = signature.gsub(/(?<=\A|\W)::/, "TOP::")
	
	binding = ::Kernel.binding
	
	return ::Kernel.eval(normalized_signature, binding)
end