diff options
Diffstat (limited to 'lib/prism')
-rw-r--r-- | lib/prism/parse_result.rb | 7 | ||||
-rw-r--r-- | lib/prism/parse_result/errors.rb | 60 |
2 files changed, 67 insertions, 0 deletions
diff --git a/lib/prism/parse_result.rb b/lib/prism/parse_result.rb index 798fde09e5..cc4f942283 100644 --- a/lib/prism/parse_result.rb +++ b/lib/prism/parse_result.rb @@ -575,6 +575,7 @@ module Prism # This is a result specific to the `parse` and `parse_file` methods. class ParseResult < Result autoload :Comments, "prism/parse_result/comments" + autoload :Errors, "prism/parse_result/errors" autoload :Newlines, "prism/parse_result/newlines" private_constant :Comments @@ -604,6 +605,12 @@ module Prism def mark_newlines! value.accept(Newlines.new(source.offsets.size)) # steep:ignore end + + # Returns a string representation of the syntax tree with the errors + # displayed inline. + def errors_format + Errors.new(self).format + end end # This is a result specific to the `lex` and `lex_file` methods. diff --git a/lib/prism/parse_result/errors.rb b/lib/prism/parse_result/errors.rb new file mode 100644 index 0000000000..40dda3c264 --- /dev/null +++ b/lib/prism/parse_result/errors.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require "stringio" + +module Prism + class ParseResult < Result + class Errors + attr_reader :parse_result + + def initialize(parse_result) + @parse_result = parse_result + end + + def format + error_lines = {} + parse_result.errors.each do |error| + location = error.location + (location.start_line..location.end_line).each do |line| + error_lines[line] ||= [] + error_lines[line] << error + end + end + + source_lines = parse_result.source.source.lines + source_lines << "" if error_lines.key?(source_lines.size + 1) + + io = StringIO.new + source_lines.each.with_index(1) do |line, line_number| + io.puts(line) + + (error_lines.delete(line_number) || []).each do |error| + location = error.location + + case line_number + when location.start_line + io.print(" " * location.start_column + "^") + + if location.start_line == location.end_line + if location.start_column != location.end_column + io.print("~" * (location.end_column - location.start_column - 1)) + end + + io.puts(" " + error.message) + else + io.puts("~" * (line.bytesize - location.start_column)) + end + when location.end_line + io.puts("~" * location.end_column + " " + error.message) + else + io.puts("~" * line.bytesize) + end + end + end + + io.puts + io.string + end + end + end +end |