class Yoda::Parsing::SourceCutter::FixingSource

Attributes

source[R]
tokens_to_append[R]

Public Class Methods

new(source, tokens_to_append) click to toggle source

@param source [String] @param tokens_to_append [Array<Symbol>]

# File lib/yoda/parsing/source_cutter.rb, line 80
def initialize(source, tokens_to_append)
  @source = source
  @tokens_to_append = tokens_to_append
end

Public Instance Methods

diagnostic() click to toggle source

@return [Symbol, nil]

# File lib/yoda/parsing/source_cutter.rb, line 104
def diagnostic
  begin
    ::Parser::CurrentRuby.parse(to_s)
    nil
  rescue ::Parser::SyntaxError => ex
    fail CannotRecoverError, "Cannot recover: #{ex.diagnostic.render}" unless ex.diagnostic.reason == :unexpected_token
    fail CannotRecoverError, "Cannot recover: #{ex.diagnostic.render}" unless ex.diagnostic.location.end_pos == to_s.length
    case ex.diagnostic.arguments[:token]
    when 'tSEMI'
      :fix_line
    when '$end'
      :fix_block
    else
      fail CannotRecoverError, "Cannot recover: #{ex.diagnostic.render}"
    end
  end
end
to_s() click to toggle source
# File lib/yoda/parsing/source_cutter.rb, line 85
def to_s
  @to_s ||= source + "\n" + tokens_to_append.map(&token_mapper).join("\n")
end
token_mapper() click to toggle source
# File lib/yoda/parsing/source_cutter.rb, line 89
def token_mapper
  {
    tSEMI: ';',
    tLBRACE: '{',
    tRBRACE: '}',
    tLPAREN: '(',
    tRPAREN: ')',
    kEND: 'end',
    kNIL: 'nil',
    dummy_constant: 'DUMMY_CONSTANT',
    dummy_method: 'dummy_method',
  }
end