Class: Decode::Language::Reference
- Inherits:
-
Object
- Object
- Decode::Language::Reference
- Defined in:
- lib/decode/language/reference.rb
Overview
An reference which can be resolved to zero or more definitions.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#language ⇒ Object
readonly
Returns the value of attribute language.
- #The identifier part of the reference.(identifierpartofthereference.) ⇒ Object readonly
- #The language associated with this reference.(languageassociatedwiththisreference.) ⇒ Object readonly
Instance Method Summary collapse
-
#absolute? ⇒ Boolean
Whether the reference starts at the base of the lexical tree.
-
#best(definitions) ⇒ Object
Find the best matching definition from a list.
-
#initialize(identifier, language, lexical_path = nil) ⇒ Reference
constructor
Initialize the reference.
-
#inspect ⇒ Object
Generate a debug representation of the reference.
-
#lexical_path ⇒ Object
Get the lexical path of this reference.
-
#path ⇒ Object
The lexical path of the reference.
-
#priority(definition, prefix) ⇒ Object
Calculate the priority of a definition for matching.
-
#relative? ⇒ Boolean
Check if this is a relative reference.
-
#split(identifier) ⇒ Object
Split an identifier into prefix and name components.
-
#to_s ⇒ Object
Generate a string representation of the reference.
Constructor Details
#initialize(identifier, language, lexical_path = nil) ⇒ Reference
Initialize the reference.
14 15 16 17 18 19 20 |
# File 'lib/decode/language/reference.rb', line 14 def initialize(identifier, language, lexical_path = nil) @identifier = identifier @language = language @lexical_path = lexical_path @path = nil end |
Instance Attribute Details
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
33 34 35 |
# File 'lib/decode/language/reference.rb', line 33 def identifier @identifier end |
#language ⇒ Object (readonly)
Returns the value of attribute language.
36 37 38 |
# File 'lib/decode/language/reference.rb', line 36 def language @language end |
#The identifier part of the reference.(identifierpartofthereference.) ⇒ Object (readonly)
33 |
# File 'lib/decode/language/reference.rb', line 33 attr :identifier |
#The language associated with this reference.(languageassociatedwiththisreference.) ⇒ Object (readonly)
36 |
# File 'lib/decode/language/reference.rb', line 36 attr :language |
Instance Method Details
#absolute? ⇒ Boolean
Whether the reference starts at the base of the lexical tree.
39 40 41 |
# File 'lib/decode/language/reference.rb', line 39 def absolute? !self.relative? end |
#best(definitions) ⇒ Object
Find the best matching definition from a list.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/decode/language/reference.rb', line 77 def best(definitions) prefix, name = lexical_path.last first = nil #: Definition? without_prefix = nil #: Definition? 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 |
#inspect ⇒ Object
Generate a debug representation of the reference.
28 29 30 |
# File 'lib/decode/language/reference.rb', line 28 def inspect "\#<#{self.class} {#{self.identifier}}>" end |
#lexical_path ⇒ Object
Get the lexical path of this reference.
57 58 59 |
# File 'lib/decode/language/reference.rb', line 57 def lexical_path @lexical_path ||= self.split(@identifier) end |
#path ⇒ Object
The lexical path of the reference.
100 101 102 |
# File 'lib/decode/language/reference.rb', line 100 def path @path ||= self.lexical_path.map{|_, name| name.to_sym} end |
#priority(definition, prefix) ⇒ Object
Calculate the priority of a definition for matching.
64 65 66 67 68 69 70 71 72 |
# File 'lib/decode/language/reference.rb', line 64 def priority(definition, prefix) if prefix.nil? return 1 elsif definition.start_with?(prefix) return 0 else return 2 end end |
#relative? ⇒ Boolean
Check if this is a relative reference.
44 45 46 47 48 |
# File 'lib/decode/language/reference.rb', line 44 def relative? prefix, name = self.lexical_path.first return prefix.nil? end |
#split(identifier) ⇒ Object
Split an identifier into prefix and name components.
52 53 54 |
# File 'lib/decode/language/reference.rb', line 52 def split(identifier) identifier.scan(/(\W+)?(\w+)/) end |
#to_s ⇒ Object
Generate a string representation of the reference.
23 24 25 |
# File 'lib/decode/language/reference.rb', line 23 def to_s "{#{self.language} #{self.identifier}}" end |