Class: Ibex::IR::ParserContract::Entry
- Inherits:
-
Object
- Object
- Ibex::IR::ParserContract::Entry
- 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
- #explicit ⇒ Boolean readonly
- #key ⇒ Symbol readonly
- #location ⇒ Location? readonly
- #value ⇒ Symbol? readonly
Instance Method Summary collapse
-
#initialize(key, value: nil, location: nil, explicit: false) ⇒ Entry
constructor
A new instance of Entry.
- #serialized_location ⇒ entry_location?
- #to_h ⇒ Hash[Symbol, entry_value]
- #validate_state!(key, allowed, value, location, explicit) ⇒ void
Constructor Details
#initialize(key, value: nil, location: nil, explicit: false) ⇒ Entry
Returns a new instance of Entry.
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
#explicit ⇒ Boolean (readonly)
32 33 34 |
# File 'lib/ibex/ir/parser_contract.rb', line 32 def explicit @explicit end |
#key ⇒ Symbol (readonly)
29 30 31 |
# File 'lib/ibex/ir/parser_contract.rb', line 29 def key @key end |
#location ⇒ Location? (readonly)
31 32 33 |
# File 'lib/ibex/ir/parser_contract.rb', line 31 def location @location end |
#value ⇒ Symbol? (readonly)
30 31 32 |
# File 'lib/ibex/ir/parser_contract.rb', line 30 def value @value end |
Instance Method Details
#serialized_location ⇒ entry_location?
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_h ⇒ 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.
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 |