Class: Decode::Language::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/language/reference.rb

Overview

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

Direct Known Subclasses

Decode::Language::Ruby::Reference

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, language, lexical_path = nil) ⇒ Reference

Initialize the reference.



27
28
29
30
31
32
33
# File 'lib/decode/language/reference.rb', line 27

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

Instance Attribute Details

#identifierObject (readonly)

The identifier part of the reference.



45
46
47
# File 'lib/decode/language/reference.rb', line 45

def identifier
  @identifier
end

#languageObject (readonly)

The language associated with this reference.



49
50
51
# File 'lib/decode/language/reference.rb', line 49

def language
  @language
end

Instance Method Details

#absolute?Boolean

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

Returns:

  • (Boolean)


52
53
54
# File 'lib/decode/language/reference.rb', line 52

def absolute?
	!self.relative?
end

#best(definitions) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/decode/language/reference.rb', line 80

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

#inspectObject



39
40
41
# File 'lib/decode/language/reference.rb', line 39

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

#lexical_pathObject



66
67
68
# File 'lib/decode/language/reference.rb', line 66

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

#pathObject

The lexical path of the reference.



103
104
105
# File 'lib/decode/language/reference.rb', line 103

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

#priority(definition, prefix) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/decode/language/reference.rb', line 70

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

#relative?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/decode/language/reference.rb', line 56

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

#split(identifier) ⇒ Object



62
63
64
# File 'lib/decode/language/reference.rb', line 62

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

#to_sObject



35
36
37
# File 'lib/decode/language/reference.rb', line 35

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