Exception: Luoma::DetailedLuomaError

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, token, source, template_name) ⇒ DetailedLuomaError

Returns a new instance of DetailedLuomaError.

Parameters:

  • message (String)
  • token (t_token)
  • source (String)
  • template_name (String)


11
12
13
14
15
16
# File 'lib/luoma/errors.rb', line 11

def initialize(message, token, source, template_name)
  super(message)
  @token = token
  @source = source
  @template_name = template_name
end

Instance Attribute Details

#sourceString

Returns the value of attribute source.

Returns:

  • (String)


9
10
11
# File 'lib/luoma/errors.rb', line 9

def source
  @source
end

#template_nameString

Returns the value of attribute template_name.

Returns:

  • (String)


9
10
11
# File 'lib/luoma/errors.rb', line 9

def template_name
  @template_name
end

#tokent_token

Returns the value of attribute token.

Returns:

  • (t_token)


9
10
11
# File 'lib/luoma/errors.rb', line 9

def token
  @token
end

Instance Method Details

#detailed_message(highlight: true, **kwargs) ⇒ String

Parameters:

  • highlight: (Boolean) (defaults to: true)
  • kwargs (Object)

Returns:

  • (String)


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

def detailed_message(highlight: true, **kwargs)
  value = Luoma.get_token_value(@token, @source)
  line, col, current_line = error_context(@source, @token[1])

  name_and_position = if @template_name.empty?
                        "#{current_line.inspect}:#{line}:#{col}"
                      else
                        "#{@template_name}:#{line}:#{col}"
                      end

  pad = " " * line.to_s.length
  pointer_size = value.empty? ? 1 : value.length
  pointer = (" " * (col - 1)) + ("^" * pointer_size)

  <<~MESSAGE.strip
    #{self.class}: #{message}
    #{pad} -> #{name_and_position}
    #{pad} |
    #{line} | #{current_line}
    #{pad} | #{pointer} #{highlight ? "\e[1m#{message}\e[0m" : message}
  MESSAGE
end

#error_context(source, index) ⇒ [Integer, Integer, String]

Parameters:

  • source (String)
  • index (Integer)

Returns:

  • ([Integer, Integer, String])


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/luoma/errors.rb', line 43

def error_context(source, index)
  lines = source.lines
  cumulative_length = 0
  target_line_index = -1

  lines.each_with_index do |line, i|
    cumulative_length += line.length
    next unless index < cumulative_length

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

  # Point to end of file
  line_number = lines.length
  column_number = lines.last.length + 1
  [line_number, column_number, lines.last.rstrip]
end