Class: Ibex::IR::ParserContract::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/ir/parser_contract.rb,
sig/ibex/ir/parser_contract.rbs

Overview

One specified or explicitly unspecified contract field.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value: nil, location: nil, explicit: false) ⇒ Entry

Returns a new instance of Entry.

RBS:

  • (Symbol key, ?value: Symbol?, ?location: Location?, ?explicit: bool) -> void

Parameters:

  • key (Symbol)
  • value: (Symbol, nil) (defaults to: nil)
  • location: (Location, nil) (defaults to: nil)
  • explicit: (Boolean) (defaults to: false)


35
36
37
38
39
40
41
42
43
44
# File 'lib/ibex/ir/parser_contract.rb', line 35

def initialize(key, value: nil, location: nil, explicit: false)
  definition = DEFINITIONS.fetch(key) { raise ArgumentError, "unknown parser contract key #{key.inspect}" }
  validate_state!(key, definition.fetch(:values), value, location, explicit)

  @key = key
  @value = value
  @location = location
  @explicit = explicit
  freeze
end

Instance Attribute Details

#explicitBoolean (readonly)

Signature:

  • bool

Returns:

  • (Boolean)


32
33
34
# File 'lib/ibex/ir/parser_contract.rb', line 32

def explicit
  @explicit
end

#keySymbol (readonly)

Signature:

  • Symbol

Returns:

  • (Symbol)


29
30
31
# File 'lib/ibex/ir/parser_contract.rb', line 29

def key
  @key
end

#locationLocation? (readonly)

Signature:

  • Location?

Returns:



31
32
33
# File 'lib/ibex/ir/parser_contract.rb', line 31

def location
  @location
end

#valueSymbol? (readonly)

Signature:

  • Symbol?

Returns:

  • (Symbol, nil)


30
31
32
# File 'lib/ibex/ir/parser_contract.rb', line 30

def value
  @value
end

Instance Method Details

#serialized_locationentry_location?

RBS:

  • () -> entry_location?

Returns:

  • (entry_location, nil)


71
72
73
74
75
76
# File 'lib/ibex/ir/parser_contract.rb', line 71

def serialized_location
  location = @location
  return unless location

  { file: location.file, line: location.line, column: location.column }
end

#to_hHash[Symbol, entry_value]

RBS:

  • () -> Hash[Symbol, entry_value]

Returns:

  • (Hash[Symbol, entry_value])


47
48
49
# File 'lib/ibex/ir/parser_contract.rb', line 47

def to_h
  { value: @value&.to_s, explicit: @explicit, loc: serialized_location }
end

#validate_state!(key, allowed, value, location, explicit) ⇒ void

This method returns an undefined value.

RBS:

  • (Symbol key, Array[Symbol] allowed, Symbol? value, Location? location, bool explicit) -> void

Parameters:

  • key (Symbol)
  • allowed (Array[Symbol])
  • value (Symbol, nil)
  • location (Location, nil)
  • explicit (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ibex/ir/parser_contract.rb', line 54

def validate_state!(key, allowed, value, location, explicit)
  raise ArgumentError, "explicit must be boolean" unless [true, false].include?(explicit)

  if explicit
    raise ArgumentError, "#{key} must be one of #{allowed.join(', ')}" unless allowed.include?(value)
    unless location.is_a?(Location) && location.file && !location.file.empty?
      raise ArgumentError, "explicit #{key} requires a source location with a file"
    end

    return
  end

  raise ArgumentError, "unspecified #{key} cannot carry a value" unless value.nil?
  raise ArgumentError, "unspecified #{key} cannot carry a source location" unless location.nil?
end