Exception: Ibex::Runtime::ParseError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/ibex/runtime/parser.rb,
sig/ibex/runtime/parser.rbs

Overview

Raised by the default parser error handler.

Direct Known Subclasses

ResourceLimitError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, token_id: nil, token_name: nil, token_value: nil, expected_tokens: [], location: nil, state: nil, suggestions: [], error_id: nil, detail: nil) ⇒ ParseError

rubocop:disable Layout/LineLength rubocop:enable Layout/LineLength

RBS:

  • (?String? message, ?token_id: Integer?, ?token_name: String?, ?token_value: runtime_value, ?expected_tokens: Array[String], ?location: runtime_value, ?state: Integer?, ?suggestions: Array[String], ?error_id: String?, ?detail: String?) -> void

Parameters:

  • message (String, nil) (defaults to: nil)
  • token_id: (Integer, nil) (defaults to: nil)
  • token_name: (String, nil) (defaults to: nil)
  • token_value: (runtime_value) (defaults to: nil)
  • expected_tokens: (Array[String]) (defaults to: [])
  • location: (runtime_value) (defaults to: nil)
  • state: (Integer, nil) (defaults to: nil)
  • suggestions: (Array[String]) (defaults to: [])
  • error_id: (String, nil) (defaults to: nil)
  • detail: (String, nil) (defaults to: nil)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ibex/runtime/parser.rb', line 36

def initialize(
  message = nil,
  token_id: nil,
  token_name: nil,
  token_value: nil,
  expected_tokens: [],
  location: nil,
  state: nil,
  suggestions: [],
  error_id: nil,
  detail: nil
)
  @token_id = token_id
  @token_name = token_name
  @token_value = token_value
  @expected_tokens = expected_tokens.dup.freeze
  @location = location
  @state = state
  @suggestions = suggestions.dup.freeze
  @error_id = error_id&.dup&.freeze
  @detail = detail
  super(message || diagnostic_message)
end

Instance Attribute Details

#error_idString? (readonly)

Signature:

  • String?

Returns:

  • (String, nil)


31
32
33
# File 'lib/ibex/runtime/parser.rb', line 31

def error_id
  @error_id
end

#expected_tokensArray[String] (readonly)

Signature:

  • Array[String]

Returns:

  • (Array[String])


27
28
29
# File 'lib/ibex/runtime/parser.rb', line 27

def expected_tokens
  @expected_tokens
end

#locationruntime_value (readonly)

Signature:

  • runtime_value

Returns:

  • (runtime_value)


28
29
30
# File 'lib/ibex/runtime/parser.rb', line 28

def location
  @location
end

#stateInteger? (readonly)

Signature:

  • Integer?

Returns:

  • (Integer, nil)


29
30
31
# File 'lib/ibex/runtime/parser.rb', line 29

def state
  @state
end

#suggestionsArray[String] (readonly)

Signature:

  • Array[String]

Returns:

  • (Array[String])


30
31
32
# File 'lib/ibex/runtime/parser.rb', line 30

def suggestions
  @suggestions
end

#token_idInteger? (readonly)

Signature:

  • Integer?

Returns:

  • (Integer, nil)


24
25
26
# File 'lib/ibex/runtime/parser.rb', line 24

def token_id
  @token_id
end

#token_nameString? (readonly)

Signature:

  • String?

Returns:

  • (String, nil)


25
26
27
# File 'lib/ibex/runtime/parser.rb', line 25

def token_name
  @token_name
end

#token_valueruntime_value (readonly)

Signature:

  • runtime_value

Returns:

  • (runtime_value)


26
27
28
# File 'lib/ibex/runtime/parser.rb', line 26

def token_value
  @token_value
end

Instance Method Details

#diagnostic_messageString

RBS:

  • () -> String

Returns:

  • (String)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ibex/runtime/parser.rb', line 71

def diagnostic_message
  expected = @expected_tokens.empty? ? "" : "; expected #{@expected_tokens.join(', ')}"
  default = "unexpected #{@token_name || @token_id}#{expected} (#{@token_value.inspect})"
  identifier = @error_id ? "[#{@error_id}] " : ""
  message = "#{location_label}: #{identifier}#{@detail || default}"
  source_line = location_value(:source_line)
  column = location_value(:column)
  message += "\n#{source_line}\n#{' ' * [column.to_i - 1, 0].max}^" if source_line
  message += "\ndid you mean #{@suggestions.join(' or ')}?" unless @suggestions.empty?
  message
end

#location_labelString

RBS:

  • () -> String

Returns:

  • (String)


61
62
63
64
65
66
# File 'lib/ibex/runtime/parser.rb', line 61

def location_label
  file = location_value(:file) || "(input)"
  line = location_value(:line) || 1
  column = location_value(:column) || 1
  "#{file}:#{line}:#{column}"
end

#location_value(key) ⇒ runtime_value

RBS:

  • (Symbol key) -> runtime_value

Parameters:

  • key (Symbol)

Returns:

  • (runtime_value)


84
85
86
87
88
89
90
# File 'lib/ibex/runtime/parser.rb', line 84

def location_value(key)
  return nil unless @location
  return @location.public_send(key) if @location.respond_to?(key)
  return @location[key] || @location[key.to_s] if @location.is_a?(Hash)

  nil
end