<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
# frozen_string_literal: true
# :markup: markdown

require "stringio"

module Prism
  class ParseResult < Result
    # An object to represent the set of errors on a parse result. This object
    # can be used to format the errors in a human-readable way.
    class Errors
      # The parse result that contains the errors.
      attr_reader :parse_result

      # Initialize a new set of errors from the given parse result.
      def initialize(parse_result)
        @parse_result = parse_result
      end

      # Formats the errors in a human-readable way and return them as a string.
      def format
        error_lines = {} #: Hash[Integer, Array[ParseError]]
        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
