Class: Yoda::Parsing::SourceCutter

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

Defined Under Namespace

Classes: BlockFixer, CannotRecoverError, FixingSource, LineFixer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, current_location) ⇒ SourceCutter

Returns a new instance of SourceCutter.



7
8
9
10
# File 'lib/yoda/parsing/source_cutter.rb', line 7

def initialize(source, current_location)
  @source = source
  @current_location = current_location
end

Instance Attribute Details

#current_locationObject (readonly)

Returns the value of attribute current_location.



6
7
8
# File 'lib/yoda/parsing/source_cutter.rb', line 6

def current_location
  @current_location
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/yoda/parsing/source_cutter.rb', line 6

def source
  @source
end

Instance Method Details

#current_location_token(Symbol, (String, ::Parser::Source::Range))

Returns:

  • ((Symbol, (String, ::Parser::Source::Range)))


32
33
34
# File 'lib/yoda/parsing/source_cutter.rb', line 32

def current_location_token
  tokens_of_source[current_location_token_index]
end

#current_location_token_indexInteger

Returns:

  • (Integer)


24
25
26
27
28
29
# 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::Parser::Source::Range

Returns:

  • (::Parser::Source::Range)


19
20
21
# 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_positionInteger

The last point of cut source.

Returns:

  • (Integer)


14
15
16
# File 'lib/yoda/parsing/source_cutter.rb', line 14

def cut_position
  @cut_position ||= current_location_token_range.end_pos - 1
end

#cut_sourceString

Returns:

  • (String)


45
46
47
# File 'lib/yoda/parsing/source_cutter.rb', line 45

def cut_source
  @cut_source ||= source.slice(0..cut_position)
end

#error_recovered_sourceString

Returns a source that is made parsable from cut_source.

Returns:

  • (String)


51
52
53
# File 'lib/yoda/parsing/source_cutter.rb', line 51

def error_recovered_source
  @error_recovered_source ||= recover_source
end

#tokens_of_sourceArray<(Symbol, (String, ::Parser::Source::Range))>

Returns:

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


37
38
39
40
41
42
# 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