Class: Collie::Linter::Rules::UnusedNonterminal

Inherits:
Base
  • Object
show all
Defined in:
lib/collie/linter/rules/unused_nonterminal.rb

Overview

Detects nonterminals that are defined but never referenced

Constant Summary

Constants inherited from Base

Base::VALID_SEVERITIES

Instance Method Summary collapse

Methods inherited from Base

#autocorrectable?, #initialize

Constructor Details

This class inherits a constructor from Collie::Linter::Base

Instance Method Details

#check(ast, _context = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/collie/linter/rules/unused_nonterminal.rb', line 17

def check(ast, _context = {})
  symbol_table = Analyzer::SymbolTable.new

  # Register all nonterminals
  ast.rules.each do |rule|
    symbol_table.add_nonterminal(rule.name, location: rule.location)
  end

  ast.declarations.each do |decl|
    case decl
    when AST::TokenDeclaration
      decl.names.each do |name|
        symbol_table.add_token(name, type_tag: decl.type_tag, location: decl.location)
      rescue Error
        # Ignore duplicates while building the resolver table.
      end
    when AST::PrecedenceDeclaration
      decl.tokens.each do |name|
        symbol_table.add_token(name, location: decl.location)
      rescue Error
        # Ignore duplicates while building the resolver table.
      end
    when AST::ParameterizedRule
      symbol_table.add_nonterminal(decl.name, location: decl.location)
    when AST::InlineRule
      symbol_table.add_nonterminal(decl.rule, location: decl.location)
    end
  end

  Analyzer::SymbolResolver.resolve(ast, symbol_table)

  # Find start symbol
  start_symbol = find_start_symbol(ast)

  # Track nonterminal usage in normal rules
  ast.rules.each do |rule|
    rule.alternatives.each do |alt|
      alt.symbols.each do |symbol|
        if symbol.nonterminal?
          symbol_table.use_nonterminal(symbol.name)
          # Also consider parameterized rule call arguments: list(expr)
          if symbol.arguments
            symbol.arguments.each do |arg|
              symbol_table.use_nonterminal(arg.name) if arg.nonterminal?
            end
          end
        end
      end
    end
  end

  # Track nonterminal usage in parameterized rules (%rule)
  ast.declarations.each do |decl|
    next unless decl.is_a?(AST::ParameterizedRule) || decl.is_a?(AST::InlineRule)

    decl.alternatives.each do |alt|
      alt.symbols.each do |symbol|
        if symbol.nonterminal?
          symbol_table.use_nonterminal(symbol.name)
          if symbol.arguments
            symbol.arguments.each do |arg|
              symbol_table.use_nonterminal(arg.name) if arg.nonterminal?
            end
          end
        end
      end
    end
  end

  # Mark start symbol as used
  symbol_table.use_nonterminal(start_symbol) if start_symbol
  unreachable_rules = unreachable_rules(ast, start_symbol)

  # Find unused nonterminals
  symbol_table.unused_nonterminals.each do |nonterminal_name|
    # Skip start symbol
    next if nonterminal_name == start_symbol
    next if unreachable_rules.include?(nonterminal_name)

    rule = ast.rules.find { |r| r.name == nonterminal_name }
    next unless rule

    add_offense(rule,
                message: "Nonterminal '#{nonterminal_name}' is defined but never used")
  end

  @offenses
end