class Yoda::Parsing::SourceCutter::LineFixer

Public Instance Methods

fix_inline_block(remained_tokens, tokens_to_add) click to toggle source
# File lib/yoda/parsing/source_cutter.rb, line 150
def fix_inline_block(remained_tokens, tokens_to_add)
  stack = []

  remained_tokens.each_with_index.reverse_each do |(token, _), i|
    token_to_add =
      case token
      when :tSTRING_BEG
        reduce(stack, :tSTRING_END)
      when :tSTRING_END
        reduce(stack, :tSTRING_END)
      when :tLBRACE
        reduce(stack, :tRBRACE)
      when :tRBRACE
        next stack.push(:tRBRACE)
      when :tLPAREN, :tLPAREN2
        reduce(stack, :tRPAREN)
      when :tRPAREN, :tRPAREN2
        next stack.push(:tRPAREN)
      else
        nil
      end
    return [remained_tokens.slice(0...i), tokens_to_add.slice(0..-2) + [token_to_add, tokens_to_add.last]] if token_to_add
  end

  fail CannotRecoverError, "Cannot fix inline error"
end
fix_operator(remained_tokens) click to toggle source

@return [Symbol, nil]

# File lib/yoda/parsing/source_cutter.rb, line 136
def fix_operator(remained_tokens)
  case remained_tokens.last.first
  when :tEQL, :tAMPER2, :tPIPE, :tBANG, :tCARET, :tPLUS, :tMINUS, :tSTAR2, :tDIVIDE, :tPERCENT, :tTILDE, :tCOMMA, :tDOT2, :tDOT3, :tCOLON,
      :tANDOP, :tOROP, :tUMINUS, :tUPLUS, :tTILDE, :tPOW, :tMATCH, :tNMATCH, :tEQ, :tNEQ, :tGT, :tRSHFT, :tGEQ, :tLT, :tLSHFT, :tLEQ, :tASSOC, :tEQQ, :tCMP, :tBANG, :tANDDOT
    :kNIL
  when :tCOLON2, :tCOLON3
    :dummy_constant
  when :tDOT
    :dummy_method
  else
    nil
  end
end
process(remained_tokens, tokens_to_add) click to toggle source

@param remained_tokens [Array<(Symbol, (String, ::Parser::Source::Range))>] @param tokens_to_add [Array<Symbol>]

# File lib/yoda/parsing/source_cutter.rb, line 126
def process(remained_tokens, tokens_to_add)
  if tokens_to_add.first == :tSEMI
    token = fix_operator(remained_tokens)
    return [remained_tokens, [token] + tokens_to_add] if token
  end

  fix_inline_block(remained_tokens, tokens_to_add)
end
reduce(stack, expected) click to toggle source
# File lib/yoda/parsing/source_cutter.rb, line 177
def reduce(stack, expected)
  if stack.empty?
    expected
  else
    if stack.last == expected
      stack.pop
    else
      fail CannotRecoverError, "Block mismatch in existing source"
    end
    nil
  end
end