DecodeSourceDecodeLanguageReference

class Reference

An reference which can be resolved to zero or more definitions.

Definitions

def initialize(identifier, language, lexical_path = nil)

Initialize the reference.

Signature

parameter identifier String

The identifier part of the reference.

Implementation

def initialize(identifier, language, lexical_path = nil)
	@identifier = identifier
	@language = language
	
	@lexical_path = lexical_path
	@path = nil
end

def to_s

Generate a string representation of the reference.

Implementation

def to_s
	"{#{self.language} #{self.identifier}}"
end

def inspect

Generate a debug representation of the reference.

Implementation

def inspect
	"\#<#{self.class} {#{self.identifier}}>"
end

attr :identifier

Signature

attribute String

The identifier part of the reference.

attr :language

Signature

attribute Language::Generic

The language associated with this reference.

def absolute?

Whether the reference starts at the base of the lexical tree.

Implementation

def absolute?
	!self.relative?
end

def relative?

Check if this is a relative reference.

Implementation

def relative?
	prefix, name = self.lexical_path.first
	
	return prefix.nil?
end

def split(identifier)

Split an identifier into prefix and name components.

Signature

parameter identifier String

The identifier to split.

Implementation

def split(identifier)
	identifier.scan(/(\W+)?(\w+)/)
end

def lexical_path

Get the lexical path of this reference.

Implementation

def lexical_path
	@lexical_path ||= self.split(@identifier)
end

def priority(definition, prefix)

Calculate the priority of a definition for matching.

Signature

parameter definition String

The definition to check.

parameter prefix String

The prefix to match against.

Implementation

def priority(definition, prefix)
	if prefix.nil?
		return 1
	elsif definition.start_with?(prefix)
		return 0
	else
		return 2
	end
end

def best(definitions)

Find the best matching definition from a list.

Signature

parameter definitions Array(String)

The definitions to choose from.

Implementation

def best(definitions)
	prefix, name = lexical_path.last
	
	first = nil
	without_prefix = nil
	
	definitions.each do |definition|
		first ||= definition
		
		next unless definition.language == @language
		
		if prefix.nil?
			without_prefix ||= definition
		elsif definition.start_with?(prefix)
			return definition
		end
	end
	
	return without_prefix || first
end

def path

The lexical path of the reference.

Signature

returns Array(String)

Implementation

def path
	@path ||= self.lexical_path.map{|_, name| name.to_sym}
end