class Yoda::Parsing::SourceCutter

Attributes

current_location[R]
source[R]

Public Class Methods

new(source, current_location) click to toggle source
# File lib/yoda/parsing/source_cutter.rb, line 7
def initialize(source, current_location)
  @source = source
  @current_location = current_location
end

Public Instance Methods

current_location_token() click to toggle source

@return [(Symbol, (String, ::Parser::Source::Range))]

# File lib/yoda/parsing/source_cutter.rb, line 32
def current_location_token
  tokens_of_source[current_location_token_index]
end
current_location_token_index() click to toggle source

@return [Integer]

# File lib/yoda/parsing/source_cutter.rb, line 24
def current_location_token_index
  @current_location_token_index ||= begin
    reverse_index = tokens_of_source.reverse_each.find_index { |type, (name, range)| current_location.later_than?(range) }
    tokens_of_source.length - 1 - reverse_index
  end
end
current_location_token_range() click to toggle source

@return [::Parser::Source::Range]

# File lib/yoda/parsing/source_cutter.rb, line 19
def current_location_token_range
  @current_location_token_range ||= current_location_token.last.last
end
cut_position() click to toggle source

The last point of cut source. @return [Integer]

# File lib/yoda/parsing/source_cutter.rb, line 14
def cut_position
  @cut_position ||= current_location_token_range.end_pos - 1
end
cut_source() click to toggle source

@return [String]

# File lib/yoda/parsing/source_cutter.rb, line 45
def cut_source
  @cut_source ||= source.slice(0..cut_position)
end
error_recovered_source() click to toggle source

Returns a source that is made parsable from cut_source. @return [String]

# File lib/yoda/parsing/source_cutter.rb, line 51
def error_recovered_source
  @error_recovered_source ||= recover_source
end
tokens_of_source() click to toggle source

@return [Array<(Symbol, (String, ::Parser::Source::Range))>]

# File lib/yoda/parsing/source_cutter.rb, line 37
def tokens_of_source
  @tokens_of_source ||= begin
    _, _, tokens = ::Parser::CurrentRuby.new.tokenize(::Parser::Source::Buffer.new("(string)").tap { |b| b.source = source }, true)
    tokens
  end
end

Private Instance Methods

recover_source() click to toggle source

@return [String]

# File lib/yoda/parsing/source_cutter.rb, line 58
def recover_source
  remained_tokens = tokens_of_source.slice(0..current_location_token_index)
  tokens_to_append = [:tSEMI]

  while
    fixing_source = FixingSource.new(cut_source, tokens_to_append)
    case fixing_source.diagnostic
    when :fix_line
      remained_tokens, tokens_to_append = LineFixer.new.process(remained_tokens, tokens_to_append)
    when :fix_block
      tokens_to_append = BlockFixer.new.process(remained_tokens, tokens_to_append)
      remained_tokens = []
    else
      return fixing_source.to_s
    end
  end
end