Class: Yoda::Parsing::SourceCutter

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

Overview

SourceCutter modifies the given source to fix parse errors around the location.

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.

Parameters:

  • source (String)
  • current_location (Location)


11
12
13
14
# File 'lib/yoda/parsing/source_cutter.rb', line 11

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.



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

def current_location
  @current_location
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

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

Returns:

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


36
37
38
# File 'lib/yoda/parsing/source_cutter.rb', line 36

def current_location_token
  tokens_of_source[current_location_token_index]
end

#current_location_token_indexInteger

Returns:

  • (Integer)


28
29
30
31
32
33
# File 'lib/yoda/parsing/source_cutter.rb', line 28

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)


23
24
25
# File 'lib/yoda/parsing/source_cutter.rb', line 23

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)


18
19
20
# File 'lib/yoda/parsing/source_cutter.rb', line 18

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

#cut_sourceString

Returns:

  • (String)


49
50
51
# File 'lib/yoda/parsing/source_cutter.rb', line 49

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)


55
56
57
# File 'lib/yoda/parsing/source_cutter.rb', line 55

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))>)


41
42
43
44
45
46
# File 'lib/yoda/parsing/source_cutter.rb', line 41

def tokens_of_source
  @tokens_of_source ||= begin
    _, _, tokens = Parser.new.tokenize(source, recover: true)
    tokens
  end
end