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: untyped, ?expected_tokens: Array[String], ?location: untyped, ?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: (Object) (defaults to: nil)
  • expected_tokens: (Array[String]) (defaults to: [])
  • location: (Object) (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)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ibex/runtime/parser.rb', line 28

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)


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

def error_id
  @error_id
end

#expected_tokensArray[String] (readonly)

Signature:

  • Array[String]

Returns:

  • (Array[String])


19
20
21
# File 'lib/ibex/runtime/parser.rb', line 19

def expected_tokens
  @expected_tokens
end

#locationObject (readonly)

Signature:

  • untyped

Returns:

  • (Object)


20
21
22
# File 'lib/ibex/runtime/parser.rb', line 20

def location
  @location
end

#stateInteger? (readonly)

Signature:

  • Integer?

Returns:

  • (Integer, nil)


21
22
23
# File 'lib/ibex/runtime/parser.rb', line 21

def state
  @state
end

#suggestionsArray[String] (readonly)

Signature:

  • Array[String]

Returns:

  • (Array[String])


22
23
24
# File 'lib/ibex/runtime/parser.rb', line 22

def suggestions
  @suggestions
end

#token_idInteger? (readonly)

Signature:

  • Integer?

Returns:

  • (Integer, nil)


16
17
18
# File 'lib/ibex/runtime/parser.rb', line 16

def token_id
  @token_id
end

#token_nameString? (readonly)

Signature:

  • String?

Returns:

  • (String, nil)


17
18
19
# File 'lib/ibex/runtime/parser.rb', line 17

def token_name
  @token_name
end

#token_valueObject (readonly)

Signature:

  • untyped

Returns:

  • (Object)


18
19
20
# File 'lib/ibex/runtime/parser.rb', line 18

def token_value
  @token_value
end

Instance Method Details

#diagnostic_messageString

RBS:

  • () -> String

Returns:

  • (String)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ibex/runtime/parser.rb', line 63

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)


53
54
55
56
57
58
# File 'lib/ibex/runtime/parser.rb', line 53

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

RBS:

  • (Symbol key) -> untyped

Parameters:

  • key (Symbol)

Returns:

  • (Object)


76
77
78
79
80
81
82
# File 'lib/ibex/runtime/parser.rb', line 76

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