Skip to content

Instantly share code, notes, and snippets.

@schneems
Created January 15, 2022 15:14
Show Gist options
  • Save schneems/e62216b610a36a81a98d9d8146b0611a to your computer and use it in GitHub Desktop.
Save schneems/e62216b610a36a81a98d9d8146b0611a to your computer and use it in GitHub Desktop.
===== START ======
              start_line: keyword.location.start_line,
              start_char: keyword.location.end_char + 1,
              end_line: last_node.location.end_line,
              end_char: last_node.location.end_char
== one
    statements.bind(
      find_next_statement_start(last_node.location.end_char),
      char_pos
    )
    rescue_ex =
      if exceptions || variable
        RescueEx.new(
          exceptions: exceptions,
          variable: variable,
          location:
            Location.new(
              start_line: keyword.location.start_line,
              start_char: keyword.location.end_char + 1,
              end_line: last_node.location.end_line,
              end_char: last_node.location.end_char
            )
        )
      end
== two
  def on_rescue(exceptions, variable, statements, consequent)
    keyword = find_token(Kw, 'rescue')
    exceptions = exceptions[0] if exceptions.is_a?(Array)
    last_node = variable || exceptions || keyword
    statements.bind(
      find_next_statement_start(last_node.location.end_char),
      char_pos
    )
    rescue_ex =
      if exceptions || variable
        RescueEx.new(
          exceptions: exceptions,
          variable: variable,
          location:
            Location.new(
              start_line: keyword.location.start_line,
              start_char: keyword.location.end_char + 1,
              end_line: last_node.location.end_line,
              end_char: last_node.location.end_char
            )
        )
      end
    Rescue.new(
      exception: rescue_ex,
      statements: statements,
      consequent: consequent,
      location:
        Location.new(
          start_line: keyword.location.start_line,
          start_char: keyword.location.start_char,
          end_line: lineno,
          end_char: char_pos
        )
    )
  end
== three
  class Rescue
    attr_reader :exception
    attr_reader :statements
    attr_reader :consequent
    attr_reader :location
    def initialize(exception:, statements:, consequent:, location:)
      @exception = exception
      @statements = statements
      @consequent = consequent
      @location = location
    end
    def bind_end(end_char)
      @location =
        Location.new(
          start_line: location.start_line,
          start_char: location.start_char,
          end_line: location.end_line,
          end_char: end_char
        )
      if consequent
        consequent.bind_end(end_char)
        statements.bind_end(consequent.location.start_char)
      else
        statements.bind_end(end_char)
      end
    end
    def pretty_print(q)
      q.group(2, '(', ')') do
        q.text('rescue')
        if exception
          q.breakable
          q.pp(exception)
        end
        q.breakable
        q.pp(statements)
        if consequent
          q.breakable
          q.pp(consequent)
        end
      end
    end
    def to_json(*opts)
      {
        type: :rescue,
        extn: exception,
        stmts: statements,
        cons: consequent,
        loc: location
      }.to_json(*opts)
    end
  end
  def on_rescue(exceptions, variable, statements, consequent)
    keyword = find_token(Kw, 'rescue')
    exceptions = exceptions[0] if exceptions.is_a?(Array)
    last_node = variable || exceptions || keyword
    statements.bind(
      find_next_statement_start(last_node.location.end_char),
      char_pos
    )
    rescue_ex =
      if exceptions || variable
        RescueEx.new(
          exceptions: exceptions,
          variable: variable,
          location:
            Location.new(
              start_line: keyword.location.start_line,
              start_char: keyword.location.end_char + 1,
              end_line: last_node.location.end_line,
              end_char: last_node.location.end_char
            )
        )
      end
    Rescue.new(
      exception: rescue_ex,
      statements: statements,
      consequent: consequent,
      location:
        Location.new(
          start_line: keyword.location.start_line,
          start_char: keyword.location.start_char,
          end_line: lineno,
          end_char: char_pos
        )
    )
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment