Exception: Liquor::Diagnostic

Inherits:
StandardError
  • Object
show all
Defined in:
lib/liquor/diagnostics.rb

Direct Known Subclasses

Deprecation, Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, location = nil) ⇒ Diagnostic


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/liquor/diagnostics.rb', line 6

def initialize(message, location=nil)
  location_info = ""
  if location
    if location.include? :file
      location_info << "`#{location[:file]}'"
    end

    if location.include? :line
      location_info << ": line #{location[:line] + 1}"
      if location.include? :start
        location_info << ", column #{location[:start] + 1}"
      end
    end
  end

  @location    = location
  @raw_message = message

  if location_info.empty?
    super(message)
  else
    super("#{message} at #{location_info}")
  end
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location


3
4
5
# File 'lib/liquor/diagnostics.rb', line 3

def location
  @location
end

#raw_messageObject (readonly)

Returns the value of attribute raw_message


4
5
6
# File 'lib/liquor/diagnostics.rb', line 4

def raw_message
  @raw_message
end

Instance Method Details

#as_json(options = nil) ⇒ Object


74
75
76
77
78
79
80
# File 'lib/liquor/diagnostics.rb', line 74

def as_json(options = nil)
  {
    message:  @raw_message,
    is_error: error?,
    location: @location,
  }
end

#decorate(source) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/liquor/diagnostics.rb', line 35

def decorate(source)
  if @location && @location.has_key?(:line)
    line   = source.lines.drop(@location[:line]).first
    line ||= "(source spans only #{source.lines.count} lines)"
    line   = line.rstrip

    if @location.has_key? :start
      start_col = tabify_column line, @location[:start]
      pointer =  " " * start_col

      if location.has_key? :end
        end_col = tabify_column line, @location[:end]

        pointer += "^" * (end_col - start_col + 1)
      else
        pointer += "^"
      end
    end
  end

  [ line, pointer ].compact
end

#error?Boolean


31
32
33
# File 'lib/liquor/diagnostics.rb', line 31

def error?
  false
end

#tabify_column(line, column) ⇒ Object

Dammit, why should I replicate parts of VT-52 all over again?! Curse backwards compatibility and C.


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/liquor/diagnostics.rb', line 60

def tabify_column(line, column)
  display_column = 0

  line[0...column].each_char do |char|
    if char == "\t"
      display_column += 8 - (display_column % 8)
    else
      display_column += 1
    end
  end

  display_column
end

#to_json(options = nil) ⇒ Object


82
83
84
# File 'lib/liquor/diagnostics.rb', line 82

def to_json(options = nil)
  as_json.to_json(options)
end