Class: Yoda::Parsing::SourceCutter
- Inherits:
-
Object
- Object
- Yoda::Parsing::SourceCutter
- Defined in:
- lib/yoda/parsing/source_cutter.rb
Defined Under Namespace
Classes: BlockFixer, CannotRecoverError, FixingSource, LineFixer
Instance Attribute Summary collapse
-
#current_location ⇒ Object
readonly
Returns the value of attribute current_location.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #current_location_token ⇒ (Symbol, (String, ::Parser::Source::Range))
- #current_location_token_index ⇒ Integer
- #current_location_token_range ⇒ ::Parser::Source::Range
-
#cut_position ⇒ Integer
The last point of cut source.
- #cut_source ⇒ String
-
#error_recovered_source ⇒ String
Returns a source that is made parsable from cut_source.
-
#initialize(source, current_location) ⇒ SourceCutter
constructor
A new instance of SourceCutter.
- #tokens_of_source ⇒ Array<(Symbol, (String, ::Parser::Source::Range))>
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_location ⇒ Object (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 |
#source ⇒ Object (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))
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_index ⇒ 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
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_position ⇒ Integer
The last point of cut source.
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_source ⇒ 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_source ⇒ String
Returns a source that is made parsable from cut_source.
51 52 53 |
# File 'lib/yoda/parsing/source_cutter.rb', line 51 def error_recovered_source @error_recovered_source ||= recover_source end |
#tokens_of_source ⇒ 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 |