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


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

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.



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

def source
  @source
end

#tokens_to_appendObject (readonly)

Returns the value of attribute tokens_to_append.



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

def tokens_to_append
  @tokens_to_append
end

Instance Method Details

#diagnosticSymbol?

Returns:

  • (Symbol, nil)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/yoda/parsing/source_cutter.rb', line 108

def diagnostic
  begin
    Parsing::Parser.new.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



89
90
91
# File 'lib/yoda/parsing/source_cutter.rb', line 89

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

#token_mapperObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/yoda/parsing/source_cutter.rb', line 93

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