Class: Luoma::StaticAnalysis::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/luoma/static_analysis.rb,
sig/luoma/static_analysis.rbs

Overview

The location of a variable, tag or filter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, token) ⇒ Location

(Template, t_token) -> void

Parameters:



11
12
13
14
# File 'lib/luoma/static_analysis.rb', line 11

def initialize(template, token)
  @template = template
  @token = token
end

Instance Attribute Details

#templateLuoma::Template (readonly)

Returns the value of attribute template.

Returns:



8
9
10
# File 'lib/luoma/static_analysis.rb', line 8

def template
  @template
end

#tokenLuoma::t_token (readonly)

Returns the value of attribute token.

Returns:

  • (Luoma::t_token)


8
9
10
# File 'lib/luoma/static_analysis.rb', line 8

def token
  @token
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/luoma/static_analysis.rb', line 41

def ==(other)
  self.class == other.class &&
    @template.name == other.template.name &&
    @token == other.token
end

#hashObject

Returns:

  • (Object)


49
50
51
# File 'lib/luoma/static_analysis.rb', line 49

def hash
  [@template.name, @token].hash
end

#line_col(index) ⇒ [Numeric, Numeric]

Return the line and column number of the given index.

(Integer) -> [Integer, Integer]

Parameters:

  • index (Numeric)

Returns:

  • ([Numeric, Numeric])


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/luoma/static_analysis.rb', line 19

def line_col(index)
  lines = @template.lines
  cumulative_length = 0
  target_line_index = -1

  lines.each_with_index do |line, i|
    cumulative_length += line.length

    if index < cumulative_length
      target_line_index = i
      break
    end
  end

  raise LuomaError.new("index out of bounds") if target_line_index == -1

  line_number = target_line_index + 1
  line = lines[target_line_index]
  column_number = index - (cumulative_length - line.length)
  [line_number, column_number]
end

#span[[Numeric, Numeric],[Numeric, Numeric]]

Return the line and column number for the start and end index spanning this location.

() -> [[Integer, Integer],[Integer, Integer]]

Returns:

  • ([[Numeric, Numeric],[Numeric, Numeric]])


57
58
59
# File 'lib/luoma/static_analysis.rb', line 57

def span
  [line_col(@token[1]), line_col(@token[2])]
end

#valueString

Return the substring spanning this location.

() -> String

Returns:

  • (String)


64
65
66
# File 'lib/luoma/static_analysis.rb', line 64

def value
  Luoma.get_token_value(@token, @template.source)
end