Class: LL::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/ll/message.rb

Overview

A warning/error generated during the compilation of a grammar.

Constant Summary collapse

COLORS =

The colours to use for the various message types.

{
  :error   => :red,
  :warning => :yellow
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, message, source_line) ⇒ Message



23
24
25
26
27
# File 'lib/ll/message.rb', line 23

def initialize(type, message, source_line)
  @type        = type
  @message     = message
  @source_line = source_line
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



6
7
8
# File 'lib/ll/message.rb', line 6

def message
  @message
end

#source_lineObject (readonly)

Returns the value of attribute source_line.



6
7
8
# File 'lib/ll/message.rb', line 6

def source_line
  @source_line
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/ll/message.rb', line 6

def type
  @type
end

Instance Method Details

#columnFixnum



87
88
89
# File 'lib/ll/message.rb', line 87

def column
  return source_line.column
end

#determine_pathString

Returns the path to the source of the message. If the path resides in the current working directory (or a child directory) the path is relative, otherwise it's absolute.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ll/message.rb', line 59

def determine_path
  if source_line.file == SourceLine::DEFAULT_FILE
    return source_line.file
  end

  full_path = File.expand_path(source_line.file)
  pwd       = Dir.pwd

  if full_path.start_with?(pwd)
    from = Pathname.new(full_path)
    to   = Pathname.new(pwd)

    return from.relative_path_from(to).to_s
  else
    return full_path
  end
end

#inspectString



47
48
49
50
# File 'lib/ll/message.rb', line 47

def inspect
  return "Message(type: #{type.inspect}, message: #{message.inspect}, " \
    "file: #{determine_path.inspect}, line: #{line}, column: #{column})"
end

#lineFixnum



80
81
82
# File 'lib/ll/message.rb', line 80

def line
  return source_line.line
end

#markerString (private)



96
97
98
99
100
# File 'lib/ll/message.rb', line 96

def marker
  padding = ' ' * (column - 1)

  return padding + ANSI.ansi('^', :magenta, :bold)
end

#to_sString

Returns a String containing details of the message, complete with ANSI colour sequences.



35
36
37
38
39
40
41
42
# File 'lib/ll/message.rb', line 35

def to_s
  location = ANSI.ansi("#{determine_path}:#{line}:#{column}", :white, :bold)

  type_label = ANSI.ansi(type.to_s, COLORS[type], :bold)
  msg_line   = "#{location}:#{type_label}: #{message}"

  return "#{msg_line}\n#{source_line.source}\n#{marker}"
end