Class: Kumi::Parser::ErrorExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/parser/error_extractor.rb

Overview

Extracts errors from parslet parse failures

Class Method Summary collapse

Class Method Details

.extract(error) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kumi/parser/error_extractor.rb', line 7

def self.extract(error)
  # Basic error extraction from parslet parse failures
  # This would typically parse the parslet error message
  # and extract location information

  return {} unless error.respond_to?(:message)

  message = error.message

  # Determine error type based on class
  error_type = case error.class.name
               when /Syntax/ then :syntax
               else :runtime
               end

  # Simple regex to extract line/column info
  if match = message.match(/at line (\d+) char (\d+)/)
    line = match[1].to_i
    column = match[2].to_i
  else
    line = 1
    column = 1
  end

  # Format message based on error type
  formatted_message = if error_type == :syntax
                        extract_user_friendly_message(message)
                      else
                        "#{error.class.name}: #{message}"
                      end

  {
    message: formatted_message,
    line: line,
    column: column,
    severity: :error,
    type: error_type
  }
end

.extract_user_friendly_message(raw_message) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/kumi/parser/error_extractor.rb', line 51

def self.extract_user_friendly_message(raw_message)
  # Clean up the message first - remove markers, location info, and extra whitespace
  cleaned_message = raw_message.gsub(/^\s*`-\s*/, '').gsub(/ at line \d+ char \d+\.?/, '').strip

  # Convert parslet's technical error messages to user-friendly ones
  case cleaned_message
  when /Expected ":", but got "(\w+)"/
    "Missing ':' before symbol, but got \"#{::Regexp.last_match(1)}\""
  when /Expected ":"/
    "Missing ':' before symbol"
  when /Expected "do", but got "(\w+)"/
    "Missing 'do' keyword, but got \"#{::Regexp.last_match(1)}\""
  when /Expected "do"/
    "Missing 'do' keyword"
  when /Expected "end", but got (.+)/
    "Missing 'end' keyword, but got #{::Regexp.last_match(1)}"
  when /Expected "end"/
    "Missing 'end' keyword"
  when /Expected "(\w+)", but got "(\w+)"/
    "Missing '#{::Regexp.last_match(1)}' keyword, but got \"#{::Regexp.last_match(2)}\""
  when /Expected '(\w+)'/
    "Expected '#{::Regexp.last_match(1)}'"
  when /Expected "([^"]+)", but got "([^"]+)"/
    "Expected '#{::Regexp.last_match(1)}', but got \"#{::Regexp.last_match(2)}\""
  when /Expected "(\w+)"/
    "Missing '#{::Regexp.last_match(1)}' keyword"
  when /Failed to match.*Premature end of input/m
    'Failed to match - premature end of input'
  when /Premature end of input/
    "Unexpected end of file - missing 'end'?"
  when /Failed to match/
    'Failed to match sequence'
  else
    'Parse error'
  end
end

.humanize_error_message(raw_message) ⇒ Object



47
48
49
# File 'lib/kumi/parser/error_extractor.rb', line 47

def self.humanize_error_message(raw_message)
  extract_user_friendly_message(raw_message)
end