Module: Ibex::LALR::IELR::SplitStability

Defined in:
lib/ibex/lalr/ielr/split_stability.rb,
sig/ibex/lalr/ielr/split_stability.rbs

Overview

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Constant Summary collapse

MAX_ENUMERATED_POTENTIALS =

Returns:

  • (::Integer)
12

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.normalize(action) ⇒ Object

RBS:

  • (Object?) -> Object?



57
58
59
60
61
62
63
64
65
# File 'lib/ibex/lalr/ielr/split_stability.rb', line 57

def normalize(action)
  return nil unless action
  return [:shift] if action[:type] == :shift
  return [:reduce, action[:production]] if action[:type] == :reduce
  return [:accept] if action[:type] == :accept
  return [:error] if action[:type] == :error

  action
end

.resolve(resolver, token, contributions) ⇒ Object

RBS:

  • (ConflictResolver, Integer, Array[Object]) -> Object?



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ibex/lalr/ielr/split_stability.rb', line 42

def resolve(resolver, token, contributions)
  actions = contributions.map do |kind, production_id|
    case kind
    when :shift then { type: :shift, state: 0 }
    when :reduce then { type: :reduce, production: production_id }
    when :accept then { type: :accept }
    when :error then { type: :error }
    else raise Ibex::Error, "unknown IELR contribution #{kind.inspect}"
    end
  end
  action, = resolver.resolve(token, actions)
  normalize(action)
end

.simple?(annotation) ⇒ Boolean

RBS:

  • (Annotation) -> bool

Returns:

  • (Boolean)


18
19
20
# File 'lib/ibex/lalr/ielr/split_stability.rb', line 18

def simple?(annotation)
  annotation.matrix.all? { |row| row.nil? || row.zero? }
end

.split_stable?(annotation, resolver) ⇒ Boolean

RBS:

  • (Annotation, ConflictResolver) -> bool

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ibex/lalr/ielr/split_stability.rb', line 23

def split_stable?(annotation, resolver)
  return true if simple?(annotation)

  contributions = annotation.inadequacy.contributions
  kept = contributions.each_index.reject { |index| annotation.matrix[index]&.zero? }
  return true if kept.empty?

  potential = kept.reject { |index| annotation.matrix[index].nil? }
  return false if potential.length > MAX_ENUMERATED_POTENTIALS

  base = resolve(resolver, annotation.inadequacy.token, kept.map { |index| contributions[index] })
  subsets(potential).all? do |removed|
    remaining = kept.reject { |index| removed.include?(index) }
    remaining.empty? || resolve(resolver, annotation.inadequacy.token,
                                remaining.map { |index| contributions[index] }) == base
  end
end

.subsets(values) ⇒ Object

RBS:

  • (Array[Integer]) -> Array[Array[Integer]]



68
69
70
71
72
# File 'lib/ibex/lalr/ielr/split_stability.rb', line 68

def subsets(values)
  (0...(1 << values.length)).map do |bits|
    values.each_index.filter_map { |index| values.fetch(index) if bits.anybits?(1 << index) }
  end
end

Instance Method Details

#self?.normalizeObject?

RBS:

  • (Object?) -> Object?

Parameters:

  • (Object, nil)

Returns:

  • (Object, nil)


20
# File 'sig/ibex/lalr/ielr/split_stability.rbs', line 20

def self?.normalize: (Object?) -> Object?

#self?.resolveObject?

RBS:

  • (ConflictResolver, Integer, Array[Object]) -> Object?

Parameters:

Returns:

  • (Object, nil)


17
# File 'sig/ibex/lalr/ielr/split_stability.rbs', line 17

def self?.resolve: (ConflictResolver, Integer, Array[Object]) -> Object?

#self?.simple?Boolean

RBS:

  • (Annotation) -> bool

Parameters:

Returns:

  • (Boolean)


11
# File 'sig/ibex/lalr/ielr/split_stability.rbs', line 11

def self?.simple?: (Annotation) -> bool

#self?.split_stable?Boolean

RBS:

  • (Annotation, ConflictResolver) -> bool

Parameters:

Returns:

  • (Boolean)


14
# File 'sig/ibex/lalr/ielr/split_stability.rbs', line 14

def self?.split_stable?: (Annotation, ConflictResolver) -> bool

#self?.subsetsArray[Array[Integer]]

RBS:

  • (Array[Integer]) -> Array[Array[Integer]]

Parameters:

  • (Array[Integer])

Returns:

  • (Array[Array[Integer]])


23
# File 'sig/ibex/lalr/ielr/split_stability.rbs', line 23

def self?.subsets: (Array[Integer]) -> Array[Array[Integer]]