class Epuber::Compiler::Problem

Attributes

file_path[R]
level[R]
location[R]
message[R]
source[R]

Public Class Methods

caret_symbol(indent) click to toggle source

Formats caret symbol with space indent

@param [Fixnum] indent

@return [String]

# File lib/epuber/compiler/problem.rb, line 43
def self.caret_symbol(indent)
  ' ' * indent + '^'
end
caret_symbols(indent, length) click to toggle source

Formats caret symbols for indent and length

@param [Fixnum] length @param [Fixnum] indent

@return [String]

# File lib/epuber/compiler/problem.rb, line 54
def self.caret_symbols(indent, length)
  start_sign = caret_symbol(indent)
  end_sign = if length > 1
               caret_symbol(length-2)
             else
               ''
             end

  "#{start_sign}#{end_sign}"
end
formatted_match_line(text, location) click to toggle source
# File lib/epuber/compiler/problem.rb, line 86
def self.formatted_match_line(text, location)
  pre, matched, post = text_at(text, location)

  pre_line = pre.split("\n").last || ''
  post_line = post.split("\n").first || ''

  pre = match_pre_line = pre_line
  if remove_tabs(match_pre_line).length > 100
    pre = "#{match_pre_line.first(20)}...#{match_pre_line.last(30)}"
  end

  pre = remove_tabs(pre)

  post = if post_line.length > 50
           "#{post_line.first(50)}..."
         else
           post_line
         end

  [pre, matched, post]
end
new(level, message, source, location: nil, line: nil, column: nil, length: nil, file_path: nil) click to toggle source
# File lib/epuber/compiler/problem.rb, line 25
def initialize(level, message, source, location: nil, line: nil, column: nil, length: nil, file_path: nil)
  @level = level
  @message = message
  @source = source
  @location = location
  if @location.nil? && line && column
    @location = Location.new(line, column, length)
  end

  @file_path = file_path
end
remove_tabs(text) click to toggle source
# File lib/epuber/compiler/problem.rb, line 65
def self.remove_tabs(text)
  text.gsub("\t", ' ' * 4)
end
text_at(text, location) click to toggle source

@param [Location] location

# File lib/epuber/compiler/problem.rb, line 71
def self.text_at(text, location)
  line_index = location.line - 1
  column_index = location.column - 1

  lines = text.split("\n")

  line = lines[line_index] || ''
  matched_text = line[column_index ... column_index + location.length] || ''

  pre = (lines[0 ... line_index] + [line[0 ... column_index]]).join("\n")
  post = ([line[column_index + location.length .. line.length]] + (lines[location.line .. lines.count] || [])).join("\n")

  [pre, matched_text, post]
end

Public Instance Methods

to_s() click to toggle source
# File lib/epuber/compiler/problem.rb, line 108
def to_s
  pre, match_text, post = self.class.formatted_match_line(@source, @location)

  pointers = self.class.caret_symbols(pre.length, @location.length)
  colored_match_text = match_text.empty? ? match_text : match_text.ansi.red
  column = @location.column
  line = [@location.line, 1].max

  [
    "#{@file_path}:#{line} column: #{column} --- #{@message}",
    '  ' + pre + colored_match_text + post,
    '  ' + pointers,
  ].join("\n")
end