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

COLORS =

The colours to use for the various message types.

Returns:

  • (Hash)
{
  :error   => :red,
  :warning => :yellow
}

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Message) initialize(type, message, source_line)

Returns a new instance of Message

Parameters:



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

- (Object) message (readonly)

Returns the value of attribute message



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

def message
  @message
end

- (Object) source_line (readonly)

Returns the value of attribute source_line



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

def source_line
  @source_line
end

- (Object) type (readonly)

Returns the value of attribute type



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

def type
  @type
end

Instance Method Details

- (Fixnum) column

Returns:

  • (Fixnum)


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

def column
  return source_line.column
end

- (String) determine_path

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.

Returns:

  • (String)


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

- (String) inspect

Returns:

  • (String)


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

- (Fixnum) line

Returns:

  • (Fixnum)


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

def line
  return source_line.line
end

- (String) marker (private)

Returns:

  • (String)


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

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

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

- (String) to_s

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

Returns:

  • (String)


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