Class: TRuby::ParserCombinator::ParseError

Inherits:
Object
  • Object
show all
Defined in:
lib/t_ruby/parser_combinator.rb

Overview

Error Reporting

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message:, position:, input:) ⇒ ParseError

Returns a new instance of ParseError.



903
904
905
906
907
908
# File 'lib/t_ruby/parser_combinator.rb', line 903

def initialize(message:, position:, input:)
  @message = message
  @position = position
  @input = input
  @line, @column = calculate_line_column
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



901
902
903
# File 'lib/t_ruby/parser_combinator.rb', line 901

def column
  @column
end

#inputObject (readonly)

Returns the value of attribute input.



901
902
903
# File 'lib/t_ruby/parser_combinator.rb', line 901

def input
  @input
end

#lineObject (readonly)

Returns the value of attribute line.



901
902
903
# File 'lib/t_ruby/parser_combinator.rb', line 901

def line
  @line
end

#messageObject (readonly)

Returns the value of attribute message.



901
902
903
# File 'lib/t_ruby/parser_combinator.rb', line 901

def message
  @message
end

#positionObject (readonly)

Returns the value of attribute position.



901
902
903
# File 'lib/t_ruby/parser_combinator.rb', line 901

def position
  @position
end

Instance Method Details

#context(lines_before: 2, lines_after: 1) ⇒ Object



914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
# File 'lib/t_ruby/parser_combinator.rb', line 914

def context(lines_before: 2, lines_after: 1)
  input_lines = @input.split("\n")
  start_line = [@line - lines_before - 1, 0].max
  end_line = [@line + lines_after - 1, input_lines.length - 1].min

  result = []
  (start_line..end_line).each do |i|
    prefix = i == @line - 1 ? ">>> " : "    "
    result << "#{prefix}#{i + 1}: #{input_lines[i]}"

    if i == @line - 1
      result << "    " + " " * (@column + @line.to_s.length + 1) + "^"
    end
  end

  result.join("\n")
end

#to_sObject



910
911
912
# File 'lib/t_ruby/parser_combinator.rb', line 910

def to_s
  "Parse error at line #{@line}, column #{@column}: #{@message}"
end