Class: ErrorToCommunicate::Heuristic::SyntaxError

Inherits:
ErrorToCommunicate::Heuristic show all
Defined in:
lib/error_to_communicate/heuristic/syntax_error.rb

Instance Attribute Summary collapse

Attributes inherited from ErrorToCommunicate::Heuristic

#einfo, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ErrorToCommunicate::Heuristic

#backtrace, #classname, #explanation, #message, #semantic_backtrace, #semantic_summary

Constructor Details

#initializeSyntaxError

Returns a new instance of SyntaxError.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/error_to_communicate/heuristic/syntax_error.rb', line 13

def initialize(*)
  super
  self.reported_file ,
  self.reported_line ,
  self.unexpected    ,
  self.expected      = self.class.parse_message(message)
  self.invalid_loc   = ExceptionInfo::Location.new \
                         path:    reported_file,
                         linenum: reported_line,
                         label:   "unexpected #{unexpected}, expected: #{expected}",
                         pred:    backtrace[0],
                         binding: nil
end

Instance Attribute Details

#expectedObject

Returns the value of attribute expected.



11
12
13
# File 'lib/error_to_communicate/heuristic/syntax_error.rb', line 11

def expected
  @expected
end

#invalid_locObject

Returns the value of attribute invalid_loc.



11
12
13
# File 'lib/error_to_communicate/heuristic/syntax_error.rb', line 11

def invalid_loc
  @invalid_loc
end

#reported_fileObject

Returns the value of attribute reported_file.



11
12
13
# File 'lib/error_to_communicate/heuristic/syntax_error.rb', line 11

def reported_file
  @reported_file
end

#reported_lineObject

Returns the value of attribute reported_line.



11
12
13
# File 'lib/error_to_communicate/heuristic/syntax_error.rb', line 11

def reported_line
  @reported_line
end

#unexpectedObject

Returns the value of attribute unexpected.



11
12
13
# File 'lib/error_to_communicate/heuristic/syntax_error.rb', line 11

def unexpected
  @unexpected
end

Class Method Details

.for?(einfo) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/error_to_communicate/heuristic/syntax_error.rb', line 6

def self.for?(einfo)
  einfo.classname == 'SyntaxError' && parse_message(einfo.message)
end

.parse_message(message) ⇒ Object

“/Users/josh/code/what-we-ve-got-here-is-an-error-to-communicate/proving_grounds/simple_syntax_error.rb:2: syntax error, unexpected end-of-input, expecting keyword_end”



49
50
51
52
53
# File 'lib/error_to_communicate/heuristic/syntax_error.rb', line 49

def self.parse_message(message)
  return if message !~ /^(.*?):(\d+):.*?unexpected (.*?), expecting (.*)$/
  file, line, unexpected, expected = $1, $2.to_i, $3, $4
  [file, line, unexpected, expected]
end

Instance Method Details

#semantic_explanationObject



38
39
40
41
42
43
44
45
46
# File 'lib/error_to_communicate/heuristic/syntax_error.rb', line 38

def semantic_explanation
  [ :message,
    [ [:context, 'Unexpected '],
      [:details, unexpected],
      [:context, ', expected'],
      [:details, expected],
    ]
  ]
end

#semantic_infoObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/error_to_communicate/heuristic/syntax_error.rb', line 27

def semantic_info
  [:heuristic,
    [:code, {
      location:  invalid_loc,
      context:   -5..5,
      message:   "unexpected #{unexpected}, expected #{expected}",
      emphasis:  :code,
    }]
  ]
end