Class: Yoda::Parsing::SourceCutter::LineFixer

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/parsing/source_cutter.rb

Instance Method Summary collapse

Instance Method Details

#fix_inline_block(remained_tokens, tokens_to_add) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# 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) ⇒ Symbol?

Returns:

  • (Symbol, nil)


136
137
138
139
140
141
142
143
144
145
146
147
148
# 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) ⇒ Object

Parameters:

  • remained_tokens (Array<(Symbol, (String, ::Parser::Source::Range))>)
  • tokens_to_add (Array<Symbol>)


126
127
128
129
130
131
132
133
# 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) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
# 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