diff --git a/lib/lrama/states_reporter.rb b/lib/lrama/states_reporter.rb index ec5364c0..3d47e3c6 100644 --- a/lib/lrama/states_reporter.rb +++ b/lib/lrama/states_reporter.rb @@ -26,37 +26,25 @@ def _report(io, grammar: false, terms: false, states: false, itemsets: false, lo end def report_unused_terms(io) - results = [] - terms = [] - used_symbols = [] - - terms = @states.symbols.select(&:term?) - - @states.states.each do |state| - state.reduces.select do |reduce| - used_symbols << reduce.look_ahead.flatten if !reduce.look_ahead.nil? + look_aheads = @states.states.each do |state| + state.reduces.flat_map do |reduce| + reduce.look_ahead unless reduce.look_ahead.nil? end end - @states.states.each do |state| - used_symbols << state.shifts.map(&:next_sym).select(&:term?).flatten - end - - used_symbols = used_symbols.flatten - - results = terms.select do |term| - !used_symbols.include?(term) + next_terms = @states.states.flat_map do |state| + state.shifts.map(&:next_sym).select(&:term?) end - if !results.empty? - io << "#{results.count} Unused Terms\n\n" + unused_symbols = @states.terms.select do |term| + !(look_aheads + next_terms).include?(term) end - results.each.with_index(1) do |term, index| - io << sprintf("%5d %s\n", index, term.id.s_value) - end - - if !results.empty? + unless unused_symbols.empty? + io << "#{unused_symbols.count} Unused Terms\n\n" + unused_symbols.each.with_index(1) do |term, index| + io << sprintf("%5d %s\n", index, term.id.s_value) + end io << "\n\n" end end