Module: Ibex::CLIImpact

Includes:
CLIAnalysis
Defined in:
lib/ibex/cli/impact.rb,
sig/ibex/cli/impact.rbs

Overview

CLI adapter for potential and confirmed grammar impact reports. rubocop:disable Metrics/ModuleLength, Metrics/MethodLength, Metrics/AbcSize

Instance Method Summary collapse

Methods included from CLIAnalysis

#algo_set?, #analysis_options, #build_analysis_automaton, #load_analysis_automaton, #run_diff_command, #run_metrics_command, #write_analysis_report

Instance Method Details

#apply_baseline(report, identities, settings) ⇒ void

This method returns an undefined value.

RBS:

  • (Hash[Symbol, Object?], Array[String], Hash[Symbol, untyped]) -> void

Parameters:

  • (Hash[Symbol, Object?])
  • (Array[String])
  • (Hash[Symbol, untyped])


365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/ibex/cli/impact.rb', line 365

def apply_baseline(report, identities, settings)
  path = settings[:baseline]
  baseline = Impact::Baseline.new(path) if path
  baseline.write(identities) if baseline && settings[:update_baseline]
  return unless baseline

  known = baseline.conflicts
  conflicts = report.dig(:automaton, :conflicts, :added)
  return unless conflicts

  original_count = conflicts.length
  conflicts.reject! { |entry| known.include?(entry.fetch(:id)) }
  totals = report.fetch(:totals) #: Hash[Symbol, Integer]
  totals[:critical] -= original_count - conflicts.length
end

#compare_sets(before, after) ⇒ [ Hash[String, Hash[Symbol, Object?]], Hash[String, Array[String]] ]

RBS:

  • (IR::Automaton, IR::Automaton) -> [Hash[String, Hash[Symbol, Object?]], Hash[String, Array[String]]]

Parameters:

Returns:

  • ([ Hash[String, Hash[Symbol, Object?]], Hash[String, Array[String]] ])


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/ibex/cli/impact.rb', line 259

def compare_sets(before, after)
  before_sets = Analysis::Sets.new(before.grammar)
  after_sets = Analysis::Sets.new(after.grammar)
  names = (before.grammar.nonterminals.map(&:name) + after.grammar.nonterminals.map(&:name)).uniq.sort
  changes = {} #: Hash[String, Hash[Symbol, Object?]]
  kinds = {} #: Hash[String, Array[String]]
  names.each do |name|
    before_symbol = before.grammar.symbol(name)
    after_symbol = after.grammar.symbol(name)
    first = set_change(before_sets, after_sets, before_symbol, after_symbol, :first)
    follow = set_change(before_sets, after_sets, before_symbol, after_symbol, :follow)
    nullable = nullable_change(before_sets, after_sets, before_symbol, after_symbol)
    next if set_change_empty?(first, follow, nullable)

    changes[name] = { first: first, follow: follow, nullable: nullable }
    kinds[name] = []
    kinds[name] << "first" unless first[:added].empty? && first[:removed].empty?
    kinds[name] << "follow" unless follow[:added].empty? && follow[:removed].empty?
    kinds[name] << "nullable" if nullable
  end
  [changes, kinds]
end

#confirmed_automaton_document(before, after, diff, impact) ⇒ Hash[Symbol, Object?]

RBS:

  • (IR::Automaton, IR::Automaton, Hash[Symbol, Object?], Impact::AutomatonImpact) -> Hash[Symbol, Object?]

Parameters:

Returns:

  • (Hash[Symbol, Object?])


199
200
201
202
203
204
205
206
207
208
# File 'lib/ibex/cli/impact.rb', line 199

def confirmed_automaton_document(before, after, diff, impact)
  {
    states: {
      before: before.states.length, after: after.states.length, delta: after.states.length - before.states.length
    },
    affected_states: impact.affected_states, conflicts: diff.fetch(:conflicts),
    unreachable: newly_unreachable_state_ids(before, after),
    unreachable_nonterminals: newly_unreachable_nonterminal_names(before.grammar, after.grammar)
  }
end

#confirmed_impact(paths, settings) ⇒ [ Hash[Symbol, Object?], Array[String], Hash[Symbol, Object?] ]

RBS:

  • (Array[String], Hash[Symbol, untyped]) -> [Hash[Symbol, Object?], Array[String], Hash[Symbol, Object?]]

Parameters:

  • (Array[String])
  • (Hash[Symbol, untyped])

Returns:

  • ([ Hash[Symbol, Object?], Array[String], Hash[Symbol, Object?] ])


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ibex/cli/impact.rb', line 117

def confirmed_impact(paths, settings)
  before = load_analysis_automaton(paths.fetch(0), settings.fetch(:algorithm), explicit: algo_set?(settings))
  after = load_analysis_automaton(paths.fetch(1), settings.fetch(:algorithm), explicit: algo_set?(settings))
  diff = Diff.new(before, after).to_h
  rules = diff.fetch(:rules) #: Hash[Symbol, Array[Hash[Symbol, Object?]]]
  names = rules.values_at(:added, :removed, :changed).flatten.map do |record|
    record.fetch(:id).to_s
  end.uniq.sort
  seed_names = names.select { |name| after.grammar.symbol(name) }
  seeds = Impact::Seeds.new(after.grammar, seed_names, origin: "diff")
  removed_seed_names = names.reject { |name| after.grammar.symbol(name) }
  removed_seeds = Impact::Seeds.new(before.grammar, removed_seed_names, origin: "diff")
  seed_records = (seeds.records + removed_seeds.records).sort_by { |record| record.fetch(:symbol).to_s }
  symbol_changes = diff.fetch(:symbols) #: Hash[Symbol, Array[Hash[Symbol, Object?]]]
   = symbol_changes.values.flatten.map { |record| record.fetch(:id).to_s }.uniq.sort
  graph = Impact::Graph.new(after.grammar)
  nodes, symbol_kinds = propagate(graph, seeds.ids, settings)
  set_changes, changed_kinds = compare_sets(before, after)
  symbol_kinds = merge_symbol_kinds(symbol_kinds, changed_kinds, after.grammar)
  precedence_kinds = precedence_symbol_kinds(symbol_changes, after.grammar)
  symbol_kinds = merge_symbol_kinds(symbol_kinds, precedence_kinds, after.grammar)
  automaton_impact = Impact::AutomatonImpact.new(after, nodes.keys)
  actions = Impact::ActionImpact.new(before.grammar, after.grammar, affected_names: names).to_a
  coverage = load_coverage(after, automaton_impact.production_ids, settings)
  reporter = Impact::Report.new(
    mode: "diff", algorithm: after.algorithm, grammar: after.grammar, before: before, after: after,
    seeds: seed_records, nodes: nodes, symbol_kinds: symbol_kinds, set_changes: set_changes,
    metadata_names: ,
    automaton: confirmed_automaton_document(before, after, diff, automaton_impact), actions: actions,
    coverage: coverage.to_h, minimum: settings.fetch(:severity), warnings: coverage.warnings
  )
  [reporter.to_h, conflict_identities(after), reporter.to_h(minimum: "info")]
end

#conflict_identities(automaton) ⇒ Array[String]

RBS:

  • (IR::Automaton) -> Array[String]

Parameters:

Returns:

  • (Array[String])


336
337
338
339
340
# File 'lib/ibex/cli/impact.rb', line 336

def conflict_identities(automaton)
  automaton.states.flat_map do |state|
    state.conflicts.map { |conflict| conflict_identity(automaton.grammar, conflict) }
  end.uniq.sort
end

#conflict_identity(grammar, conflict) ⇒ String

RBS:

  • (IR::Grammar, Hash[Symbol, Object?]) -> String

Parameters:

Returns:

  • (String)


343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/ibex/cli/impact.rb', line 343

def conflict_identity(grammar, conflict)
  type = conflict.fetch(:type).to_s.to_sym
  symbol = conflict.fetch(:symbol).to_s
  if type == :shift_reduce
    reduce = conflict.fetch(:reduce) #: Integer
    "shift_reduce:#{symbol}:#{production_shape(grammar, reduce)}"
  else
    reduction_ids = conflict.fetch(:reductions) #: Array[Integer]
    reductions = reduction_ids.map { |id| production_shape(grammar, id) }.sort
    "reduce_reduce:#{symbol}:#{reductions.join('|')}"
  end
end

#impact_option_parser(arguments) ⇒ Hash[Symbol, untyped]

RBS:

  • (Array[String]) -> Hash[Symbol, untyped]

Parameters:

  • (Array[String])

Returns:

  • (Hash[Symbol, untyped])


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ibex/cli/impact.rb', line 44

def impact_option_parser(arguments)
  settings = {
    paths: [], symbols: [], depth: nil, kinds: [:all], severity: "medium", coverage: [],
    fail_on: [], format: "json", update_baseline: false, configuration_explicit: [],
    algorithm: Configuration::Registry.fetch("parser.algorithm").default,
    mode: Configuration::Registry.fetch("grammar.mode").default
  } #: Hash[Symbol, untyped]
  # rubocop:disable Metrics/BlockLength
  parser = OptionParser.new do |options|
    options.banner = "Usage: ibex impact [options] GRAMMAR or OLD NEW"
    options.on("--symbol=NAME[,NAME]", "seed nonterminals for a one-version analysis") do |value|
      settings[:symbols].concat(value.split(",").map(&:strip))
    end
    options.on("--depth=N", Integer, "maximum propagation depth") do |value|
      raise OptionParser::InvalidArgument, "--depth must be non-negative" if value.negative?

      settings[:depth] = value
    end
    options.on("--kind=LIST", "reference, first, or follow") { |value| settings[:kinds] = parse_kinds(value) }
    options.on("--severity=LEVEL", Impact::Severity::LEVELS, "minimum severity") do |value|
      settings[:severity] = value
    end
    options.on("--coverage=PATH", "runtime coverage report (repeatable)") { |value| settings[:coverage] << value }
    options.on("--baseline=PATH", "known conflict baseline") { |value| settings[:baseline] = value }
    options.on("--update-baseline", "write the current conflict baseline") { settings[:update_baseline] = true }
    options.on("--fail-on=LIST", "CI gate conditions") do |value|
      settings[:fail_on] = Impact::Severity.validate_gates(value.split(",").map(&:strip))
    end
    options.on("--algorithm=NAME", %w[slr lalr ielr lr1], "algorithm for grammar inputs") do |value|
      set_local_configuration_option(settings, :algorithm, value.to_sym)
    end
    options.on("--mode=MODE", %w[default extended], "grammar mode") do |value|
      set_local_configuration_option(settings, :mode, value.to_sym)
      set_configuration_option(:mode, value.to_sym)
    end
    options.on("--format=FORMAT", %w[json text], "json or text") { |value| settings[:format] = value }
    options.on("--help", "show help") { settings[:help] = options.to_s }
  end
  # rubocop:enable Metrics/BlockLength
  settings[:paths] = parser.parse(arguments)
  settings
end

#load_coverage(automaton, production_ids, settings) ⇒ Impact::CoverageImpact

RBS:

  • (IR::Automaton, Array[Integer], Hash[Symbol, untyped]) -> Impact::CoverageImpact

Parameters:

Returns:



169
170
171
172
# File 'lib/ibex/cli/impact.rb', line 169

def load_coverage(automaton, production_ids, settings)
  reports = settings.fetch(:coverage).map { |path| [path, Coverage::Report.load_file(path)] }
  Impact::CoverageImpact.new(automaton, production_ids, reports)
end

#merge_symbol_kinds(symbol_kinds, changed_kinds, grammar) ⇒ Hash[Integer, Array[String]]

RBS:

  • (Hash[Integer, Array[String]], Hash[String, Array[String]], IR::Grammar) -> Hash[Integer, Array[String]]

Parameters:

  • (Hash[Integer, Array[String]])
  • (Hash[String, Array[String]])
  • (IR::Grammar)

Returns:

  • (Hash[Integer, Array[String]])


309
310
311
312
313
314
315
316
317
318
# File 'lib/ibex/cli/impact.rb', line 309

def merge_symbol_kinds(symbol_kinds, changed_kinds, grammar)
  result = symbol_kinds.transform_values(&:dup)
  changed_kinds.each do |name, kinds|
    id = grammar.symbol(name)&.id
    next unless id

    result[id] = (result.fetch(id, []) + kinds).uniq.sort
  end
  result
end

#newly_unreachable_nonterminal_names(before, after) ⇒ Array[String]

RBS:

  • (IR::Grammar, IR::Grammar) -> Array[String]

Parameters:

Returns:

  • (Array[String])


220
221
222
# File 'lib/ibex/cli/impact.rb', line 220

def newly_unreachable_nonterminal_names(before, after)
  unreachable_nonterminal_names(after) - unreachable_nonterminal_names(before)
end

#newly_unreachable_state_ids(before, after) ⇒ Array[Integer]

RBS:

  • (IR::Automaton, IR::Automaton) -> Array[Integer]

Parameters:

Returns:

  • (Array[Integer])


231
232
233
234
# File 'lib/ibex/cli/impact.rb', line 231

def newly_unreachable_state_ids(before, after)
  before_keys = unreachable_state_keys(before)
  unreachable_state_ids(after).reject { |id| before_keys.include?(unreachable_state_key(after, id)) }
end

#nullable_change(before, after, before_symbol, after_symbol) ⇒ Hash[Symbol, Object?]?

RBS:

  • (Analysis::Sets, Analysis::Sets, IR::GrammarSymbol?, IR::GrammarSymbol?) -> Hash[Symbol, Object?]?

Parameters:

Returns:

  • (Hash[Symbol, Object?], nil)


298
299
300
301
302
303
304
305
306
# File 'lib/ibex/cli/impact.rb', line 298

def nullable_change(before, after, before_symbol, after_symbol)
  return nil unless before_symbol || after_symbol

  old = before_symbol ? before.nullable?(before_symbol) : false
  current = after_symbol ? after.nullable?(after_symbol) : false
  return nil if old == current

  { before: old, after: current }
end

#nullable_warnings(seeds, coverage) ⇒ Array[String]

RBS:

  • (Impact::Seeds, Impact::CoverageImpact) -> Array[String]

Parameters:

Returns:

  • (Array[String])


175
176
177
178
179
180
181
182
183
184
# File 'lib/ibex/cli/impact.rb', line 175

def nullable_warnings(seeds, coverage)
  warnings = coverage.warnings.dup
  seeds.records.each do |seed|
    next unless seed.fetch(:nullable_boundary)

    warnings << "#{seed.fetch(:symbol)}: nullable boundary may make potential propagation " \
                "incomplete; compare two versions"
  end
  warnings
end

#parse_kinds(value) ⇒ Array[Symbol]

RBS:

  • (String value) -> Array[Symbol]

Parameters:

  • value (String)

Returns:

  • (Array[Symbol])


88
89
90
91
92
93
94
# File 'lib/ibex/cli/impact.rb', line 88

def parse_kinds(value)
  kinds = value.split(",").map(&:strip).map(&:to_sym)
  unknown = kinds - %i[reference first follow]
  raise OptionParser::InvalidArgument, "unknown impact kind #{unknown.first.inspect}" unless unknown.empty?

  kinds.uniq
end

#potential_automaton_document(automaton, impact) ⇒ Hash[Symbol, Object?]

RBS:

  • (IR::Automaton, Impact::AutomatonImpact) -> Hash[Symbol, Object?]

Parameters:

Returns:

  • (Hash[Symbol, Object?])


187
188
189
190
191
192
193
194
195
196
# File 'lib/ibex/cli/impact.rb', line 187

def potential_automaton_document(automaton, impact)
  added = conflict_identities(automaton).map { |id| { id: id, value: nil } } #: Array[Hash[Symbol, Object?]]
  removed = [] #: Array[Hash[Symbol, Object?]]
  changed = [] #: Array[Hash[Symbol, Object?]]
  { states: { before: nil, after: automaton.states.length, delta: nil },
    affected_states: impact.affected_states,
    conflicts: { added: added, removed: removed, changed: changed },
    unreachable: unreachable_state_ids(automaton),
    unreachable_nonterminals: unreachable_nonterminal_names(automaton.grammar) }
end

#potential_impact(paths, settings) ⇒ [ Hash[Symbol, Object?], Array[String], Hash[Symbol, Object?] ]

RBS:

  • (Array[String], Hash[Symbol, untyped]) -> [Hash[Symbol, Object?], Array[String], Hash[Symbol, Object?]]

Parameters:

  • (Array[String])
  • (Hash[Symbol, untyped])

Returns:

  • ([ Hash[Symbol, Object?], Array[String], Hash[Symbol, Object?] ])


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ibex/cli/impact.rb', line 97

def potential_impact(paths, settings)
  automaton = load_analysis_automaton(paths.fetch(0), settings.fetch(:algorithm), explicit: algo_set?(settings))
  names = settings.fetch(:symbols)
  raise Ibex::Error, "(impact):1:1: one-version impact requires --symbol" if names.empty?

  seeds = Impact::Seeds.new(automaton.grammar, names)
  graph = Impact::Graph.new(automaton.grammar)
  nodes, symbol_kinds = propagate(graph, seeds.ids, settings)
  automaton_impact = Impact::AutomatonImpact.new(automaton, nodes.keys)
  coverage = load_coverage(automaton, automaton_impact.production_ids, settings)
  reporter = Impact::Report.new(
    mode: "potential", algorithm: automaton.algorithm, grammar: automaton.grammar, before: nil, after: automaton,
    seeds: seeds.records, nodes: nodes, symbol_kinds: symbol_kinds, set_changes: {},
    automaton: potential_automaton_document(automaton, automaton_impact), actions: [], coverage: coverage.to_h,
    minimum: settings.fetch(:severity), warnings: nullable_warnings(seeds, coverage)
  )
  [reporter.to_h, conflict_identities(automaton), reporter.to_h(minimum: "info")]
end

#precedence_symbol_kinds(symbol_changes, grammar) ⇒ Hash[String, Array[String]]

RBS:

  • (Hash[Symbol, Array[Hash[Symbol, Object?]]], IR::Grammar) -> Hash[String, Array[String]]

Parameters:

  • (Hash[Symbol, Array[Hash[Symbol, Object?]]])
  • (IR::Grammar)

Returns:

  • (Hash[String, Array[String]])


321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/ibex/cli/impact.rb', line 321

def precedence_symbol_kinds(symbol_changes, grammar)
  result = {} #: Hash[String, Array[String]]
  symbol_changes.fetch(:changed).each do |record|
    before = record.fetch(:before)
    after = record.fetch(:after)
    next unless before.is_a?(Hash) && after.is_a?(Hash)
    next if before.fetch(:precedence) == after.fetch(:precedence)

    name = record.fetch(:id).to_s
    result[name] = ["precedence"] if grammar.symbol(name)
  end
  result
end

#production_shape(grammar, id) ⇒ String

RBS:

  • (IR::Grammar, Integer) -> String

Parameters:

Returns:

  • (String)


357
358
359
360
361
362
# File 'lib/ibex/cli/impact.rb', line 357

def production_shape(grammar, id)
  production = grammar.productions.fetch(id)
  lhs = grammar.symbol_by_id(production.lhs)&.name || production.lhs.to_s
  rhs = production.rhs.map { |symbol_id| grammar.symbol_by_id(symbol_id)&.name || symbol_id.to_s }
  "#{lhs}->#{rhs.join(' ')}"
end

#propagate(graph, seed_ids, settings) ⇒ [ Hash[Integer, Impact::Node], Hash[Integer, Array[String]] ]

RBS:

  • (Impact::Graph, Array[Integer], Hash[Symbol, untyped]) -> [Hash[Integer, Impact::Node], Hash[Integer, Array[String]]]

Parameters:

Returns:

  • ([ Hash[Integer, Impact::Node], Hash[Integer, Array[String]] ])


153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ibex/cli/impact.rb', line 153

def propagate(graph, seed_ids, settings)
  kinds = settings.fetch(:kinds) == [:all] ? %i[reference first follow] : settings.fetch(:kinds)
  nodes = {} #: Hash[Integer, Impact::Node]
  symbol_kinds = Hash.new { |hash, key| hash[key] = [] } #: Hash[Integer, Array[String]]
  kinds.each do |kind|
    current = Impact::Propagation.new(graph).propagate(seed_ids, kind, max_depth: settings[:depth])
    current.each do |id, node|
      best = nodes[id]
      nodes[id] = node if best.nil? || node.distance < best.distance
      symbol_kinds[id] << kind.to_s unless node.distance.zero? && node.witness.empty?
    end
  end
  [nodes.sort.to_h, symbol_kinds.transform_values { |value| value.uniq.sort }]
end

#run_impact_command(arguments) ⇒ Integer

RBS:

  • (Array[String] arguments) -> Integer

Parameters:

  • arguments (Array[String])

Returns:

  • (Integer)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ibex/cli/impact.rb', line 18

def run_impact_command(arguments)
  settings = impact_option_parser(arguments)
  if settings[:help]
    @stdout.puts(settings.fetch(:help))
    return 0
  end
  paths = settings.fetch(:paths)
  unless [1, 2].include?(paths.length)
    raise Ibex::Error, "(impact):1:1: impact requires one grammar or two grammar files"
  end

  extend CLIAnalysis unless singleton_class.ancestors.include?(CLIAnalysis)
  settings[:algorithm] = local_configuration_value(settings, "parser.algorithm")
  analysis = if paths.length == 1
               potential_impact(paths, settings)
             else
               confirmed_impact(paths, settings)
             end
  report, identities, gate_report = analysis
  apply_baseline(report, identities, settings)
  apply_baseline(gate_report, identities, settings)
  write_impact_report(report, settings.fetch(:format))
  Impact::Severity.fails?(gate_report, settings.fetch(:fail_on)) ? 1 : 0
end

#set_change(before, after, before_symbol, after_symbol, kind) ⇒ Hash[Symbol, Array[String]]

RBS:

  • (Analysis::Sets, Analysis::Sets, IR::GrammarSymbol?, IR::GrammarSymbol?, Symbol) -> Hash[Symbol, Array[String]]

Parameters:

Returns:

  • (Hash[Symbol, Array[String]])


284
285
286
287
288
# File 'lib/ibex/cli/impact.rb', line 284

def set_change(before, after, before_symbol, after_symbol, kind)
  before_values = before_symbol&.nonterminal? ? before.public_send(kind, before_symbol) : [] #: Array[String]
  after_values = after_symbol&.nonterminal? ? after.public_send(kind, after_symbol) : [] #: Array[String]
  { added: after_values - before_values, removed: before_values - after_values }
end

#set_change_empty?(first, follow, nullable) ⇒ Boolean

RBS:

  • (Hash[Symbol, Array[String]], Hash[Symbol, Array[String]], Hash[Symbol, Object?]?) -> bool

Parameters:

  • (Hash[Symbol, Array[String]])
  • (Hash[Symbol, Array[String]])
  • (Hash[Symbol, Object?], nil)

Returns:

  • (Boolean)


291
292
293
294
295
# File 'lib/ibex/cli/impact.rb', line 291

def set_change_empty?(first, follow, nullable)
  first_empty = first[:added].empty? && first[:removed].empty?
  follow_empty = follow[:added].empty? && follow[:removed].empty?
  first_empty && follow_empty && nullable.nil?
end

#unreachable_nonterminal_names(grammar) ⇒ Array[String]

RBS:

  • (IR::Grammar) -> Array[String]

Parameters:

Returns:

  • (Array[String])


211
212
213
214
215
216
217
# File 'lib/ibex/cli/impact.rb', line 211

def unreachable_nonterminal_names(grammar)
  grammar.warnings.filter_map do |warning|
    next unless warning.fetch(:type).to_sym == :unreachable_nonterminal

    warning.fetch(:symbol).to_s
  end.uniq.sort
end

#unreachable_state_ids(automaton) ⇒ Array[Integer]

RBS:

  • (IR::Automaton) -> Array[Integer]

Parameters:

Returns:

  • (Array[Integer])


225
226
227
228
# File 'lib/ibex/cli/impact.rb', line 225

def unreachable_state_ids(automaton)
  reachable = LALR::UnreachableStates.reachable_states(automaton.states, automaton.entry_states.values)
  (0...automaton.states.length).to_a - reachable
end

#unreachable_state_key(automaton, id) ⇒ String

RBS:

  • (IR::Automaton, Integer) -> String

Parameters:

Returns:

  • (String)


242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/ibex/cli/impact.rb', line 242

def unreachable_state_key(automaton, id)
  state = automaton.states.fetch(id)
  items = state.items.map do |item|
    production_name = if item.production == LALR::Builder::AUGMENTED_PRODUCTION
                        "$accept"
                      else
                        production = automaton.grammar.productions.fetch(item.production)
                        production_shape(automaton.grammar, production.id)
                      end
    [production_name, item.dot,
     item.lookaheads.map { |lookahead| automaton.grammar.symbol_by_id(lookahead)&.name || lookahead.to_s }]
  end.sort_by(&:inspect)
  JSON.generate(items)
end

#unreachable_state_keys(automaton) ⇒ Array[String]

RBS:

  • (IR::Automaton) -> Array[String]

Parameters:

Returns:

  • (Array[String])


237
238
239
# File 'lib/ibex/cli/impact.rb', line 237

def unreachable_state_keys(automaton)
  unreachable_state_ids(automaton).map { |id| unreachable_state_key(automaton, id) }
end

#write_impact_report(report, format) ⇒ void

This method returns an undefined value.

RBS:

  • (Hash[Symbol, Object?], String) -> void

Parameters:

  • (Hash[Symbol, Object?])
  • (String)


382
383
384
385
386
387
388
389
390
# File 'lib/ibex/cli/impact.rb', line 382

def write_impact_report(report, format)
  return @stdout.puts(JSON.pretty_generate(report)) if format == "json"

  symbols = report.fetch(:symbols) #: Array[untyped]
  warnings = report.fetch(:warnings) #: Array[String]
  @stdout.puts("impact mode=#{report.fetch(:mode)} symbols=#{symbols.length} " \
               "affected_states=#{report.dig(:automaton, :affected_states).length}")
  warnings.each { |warning| @stdout.puts("warning: #{warning}") }
end