Class: Ibex::IR::ParserContract

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

Overview

Root-owned parser configuration persisted by the current Grammar IR.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

DEFINITIONS =

RBS:

  • type definition = { configuration: String, values: Array[Symbol] }
    type entry_location = { file: String?, line: Integer, column: Integer }
    type entry_value = String? | bool | entry_location?

Returns:

  • (Hash[Symbol, definition])
{
  algorithm: {
    configuration: "parser.algorithm", values: %i[slr lalr ielr lr1].freeze
  }.freeze,
  entries: {
    configuration: "parser.entries", values: %i[shared isolated].freeze
  }.freeze,
  cst_trivia: {
    configuration: "cst.trivia", values: %i[leading balanced drop].freeze
  }.freeze
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm: nil, entries: nil, cst_trivia: nil) ⇒ ParserContract

Returns a new instance of ParserContract.

RBS:

  • (?algorithm: Entry?, ?entries: Entry?, ?cst_trivia: Entry?) -> void

Parameters:

  • algorithm: (Entry, nil) (defaults to: nil)
  • entries: (Entry, nil) (defaults to: nil)
  • cst_trivia: (Entry, nil) (defaults to: nil)


84
85
86
87
88
89
# File 'lib/ibex/ir/parser_contract.rb', line 84

def initialize(algorithm: nil, entries: nil, cst_trivia: nil)
  @algorithm = normalize_entry(:algorithm, algorithm)
  @entries = normalize_entry(:entries, entries)
  @cst_trivia = normalize_entry(:cst_trivia, cst_trivia)
  freeze
end

Instance Attribute Details

#algorithmEntry (readonly)

Signature:

  • Entry

Returns:



79
80
81
# File 'lib/ibex/ir/parser_contract.rb', line 79

def algorithm
  @algorithm
end

#cst_triviaEntry (readonly)

Signature:

  • Entry

Returns:



81
82
83
# File 'lib/ibex/ir/parser_contract.rb', line 81

def cst_trivia
  @cst_trivia
end

#entriesEntry (readonly)

Signature:

  • Entry

Returns:



80
81
82
# File 'lib/ibex/ir/parser_contract.rb', line 80

def entries
  @entries
end

Instance Method Details

#configuration_locationsHash[String, Location]

Source locations supplied to the typed resolver.

RBS:

  • () -> Hash[String, Location]

Returns:



111
112
113
114
115
116
# File 'lib/ibex/ir/parser_contract.rb', line 111

def configuration_locations
  specified_entries.to_h do |entry|
    location = entry.location || raise("explicit parser contract entry is missing its location")
    [DEFINITIONS.fetch(entry.key).fetch(:configuration), location]
  end
end

#configuration_valuesHash[String, Symbol]

Values supplied to the typed resolver; unspecified fields are absent.

RBS:

  • () -> Hash[String, Symbol]

Returns:

  • (Hash[String, Symbol])


102
103
104
105
106
107
# File 'lib/ibex/ir/parser_contract.rb', line 102

def configuration_values
  specified_entries.to_h do |entry|
    value = entry.value || raise("explicit parser contract entry is missing its value")
    [DEFINITIONS.fetch(entry.key).fetch(:configuration), value]
  end
end

#normalize_entry(key, entry) ⇒ Entry

RBS:

  • (Symbol key, Entry? entry) -> Entry

Parameters:

  • key (Symbol)
  • entry (Entry, nil)

Returns:



121
122
123
124
125
126
127
# File 'lib/ibex/ir/parser_contract.rb', line 121

def normalize_entry(key, entry)
  entry ||= Entry.new(key)
  raise ArgumentError, "#{key} must be a ParserContract::Entry" unless entry.is_a?(Entry)
  raise ArgumentError, "#{key} entry has key #{entry.key.inspect}" unless entry.key == key

  entry
end

#specified_entriesArray[Entry]

RBS:

  • () -> Array[Entry]

Returns:



130
131
132
# File 'lib/ibex/ir/parser_contract.rb', line 130

def specified_entries
  [@algorithm, @entries, @cst_trivia].select(&:explicit)
end

#to_hHash[Symbol, Hash[Symbol, entry_value]]

RBS:

  • () -> Hash[Symbol, Hash[Symbol, entry_value]]

Returns:

  • (Hash[Symbol, Hash[Symbol, entry_value]])


92
93
94
95
96
97
98
# File 'lib/ibex/ir/parser_contract.rb', line 92

def to_h
  {
    algorithm: @algorithm.to_h,
    entries: @entries.to_h,
    cst_trivia: @cst_trivia.to_h
  }
end