Class: Ibex::Runtime::RepairSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/generated_parser.rb

Overview

Bounded Dijkstra search over LR state stacks. Semantic actions never run. rubocop:disable Metrics/ClassLength -- queue policy and LR transitions form one bounded search invariant.

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

NEED_INPUT =

RBS:

  • type configuration_key = [Array[Integer], Integer, Integer, bool]
    type priority = Array[untyped]
    type lookup_table = Tables::action_table | Tables::goto_table
    type lookup_value = IR::runtime_action | Integer?
Object.new.freeze
LIMIT =

Signature:

  • Object

Object.new.freeze

Instance Method Summary collapse

Constructor Details

#initialize(tables, policy, tokens, complete:) ⇒ RepairSearch

Returns a new instance of RepairSearch.

RBS:

  • (Hash[Symbol, Object?] tables, RepairPolicy policy, Array[RepairInput] tokens, complete: bool) -> void



4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
# File 'lib/json5/generated_parser.rb', line 4726

def initialize(tables, policy, tokens, complete:)
  @tables = tables
  @policy = policy
  @tokens = tokens
  @complete = complete
  @configurations = 0
  @heap = RepairPriorityQueue.new
  @best = {}
  reserved = [Parser::EOF_TOKEN, Parser::ERROR_TOKEN]
  token_names = tables.fetch(:token_names) #: Hash[Integer, String]
  @candidate_ids = token_names.keys.reject { |id| reserved.include?(id) }.sort.freeze
end

Instance Method Details

#search(state_stack) ⇒ Object

Compatibility projection used by the semantic runtime path.

RBS:

  • (Array[Integer] state_stack) -> (RepairPlan | Object | nil)



4741
4742
4743
4744
4745
4746
4747
# File 'lib/json5/generated_parser.rb', line 4741

def search(state_stack)
  result = search_result(state_stack)
  return result.plan if result.selected?
  return NEED_INPUT if result.status == :need_input

  nil
end

#search_result(state_stack) ⇒ Object

Preserve the bounded outcome for syntax tooling and diagnostics.

RBS:

  • (Array[Integer] state_stack) -> RepairSearchResult



4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
# File 'lib/json5/generated_parser.rb', line 4751

def search_result(state_stack)
  empty_edits = [] #: Array[RepairEdit]
  push(
    Configuration.new(
      stack: state_stack.dup.freeze,
      input_index: 0,
      shifts: 0,
      cost: 0,
      edits: empty_edits.freeze,
      goal: false
    )
  )
  needs_input = false
  until @heap.empty?
    configuration = pop
    return outcome(:exhausted) if configuration.equal?(LIMIT)
    next unless configuration.is_a?(Configuration)

    result = visit(configuration)
    return outcome(:exhausted) if result.equal?(LIMIT)
    return outcome(:selected, plan: result) if result.is_a?(RepairPlan)

    needs_input = true if result.equal?(NEED_INPUT)
  end
  outcome(needs_input ? :need_input : :not_found)
end