Class: Yoda::Parsing::SourceCutter::FixingSource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, tokens_to_append) ⇒ FixingSource

Returns a new instance of FixingSource.

Parameters:

  • source (String)
  • tokens_to_append (Array<Symbol>)


80
81
82
83
# File 'lib/yoda/parsing/source_cutter.rb', line 80

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

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



77
78
79
# File 'lib/yoda/parsing/source_cutter.rb', line 77

def source
  @source
end

#tokens_to_appendObject (readonly)

Returns the value of attribute tokens_to_append.



77
78
79
# File 'lib/yoda/parsing/source_cutter.rb', line 77

def tokens_to_append
  @tokens_to_append
end

Instance Method Details

#diagnosticSymbol?

Returns:

  • (Symbol, nil)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/yoda/parsing/source_cutter.rb', line 104

def diagnostic
  begin
    ::Parser::CurrentRuby.parse(to_s)
    nil
  rescue ::Parser::SyntaxError => ex
    fail CannotRecoverError, "Cannot recover: #{ex.diagnostic.render}" unless ex.diagnostic.reason == :unexpected_token
    fail CannotRecoverError, "Cannot recover: #{ex.diagnostic.render}" unless ex.diagnostic.location.end_pos == to_s.length
    case ex.diagnostic.arguments[:token]
    when 'tSEMI'
      :fix_line
    when '$end'
      :fix_block
    else
      fail CannotRecoverError, "Cannot recover: #{ex.diagnostic.render}"
    end
  end
end

#to_sObject



85
86
87
# File 'lib/yoda/parsing/source_cutter.rb', line 85

def to_s
  @to_s ||= source + "\n" + tokens_to_append.map(&token_mapper).join("\n")
end

#token_mapperObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/yoda/parsing/source_cutter.rb', line 89

def token_mapper
  {
    tSEMI: ';',
    tLBRACE: '{',
    tRBRACE: '}',
    tLPAREN: '(',
    tRPAREN: ')',
    kEND: 'end',
    kNIL: 'nil',
    dummy_constant: 'DUMMY_CONSTANT',
    dummy_method: 'dummy_method',
  }
end