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.



12
13
14
15
16
17
18
# File 'lib/decode/language/reference.rb', line 12

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.



30
31
32
# File 'lib/decode/language/reference.rb', line 30

def identifier
  @identifier
end

#languageObject (readonly)

The language associated with this reference.



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

def language
  @language
end

Instance Method Details

#absolute?Boolean

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

Returns:

  • (Boolean)


37
38
39
# File 'lib/decode/language/reference.rb', line 37

def absolute?
	!self.relative?
end

#best(definitions) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/decode/language/reference.rb', line 65

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



24
25
26
# File 'lib/decode/language/reference.rb', line 24

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

#lexical_pathObject



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

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

#pathObject

The lexical path of the reference.



88
89
90
# File 'lib/decode/language/reference.rb', line 88

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

#priority(definition, prefix) ⇒ Object



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

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)


41
42
43
44
45
# File 'lib/decode/language/reference.rb', line 41

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

#split(identifier) ⇒ Object



47
48
49
# File 'lib/decode/language/reference.rb', line 47

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

#to_sObject



20
21
22
# File 'lib/decode/language/reference.rb', line 20

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