|
| 1 | +# rbs_inline: enabled |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +module Lrama |
| 5 | + class Warnings |
| 6 | + # Warning rationale: Mixing positional and named references in one rule |
| 7 | + # - It reduces readability and makes semantic actions harder to maintain |
| 8 | + # - Named references are generally more robust when RHS changes |
| 9 | + # Scope: |
| 10 | + # - This warning only targets semantic value references (`$...`) |
| 11 | + # - It intentionally ignores location references (`@...`), |
| 12 | + # index references (`$:...`), and special LHS references (`$$`) |
| 13 | + class MixedReferences |
| 14 | + # @rbs (Lrama::Logger logger, bool warnings) -> void |
| 15 | + def initialize(logger, warnings) |
| 16 | + @logger = logger |
| 17 | + @warnings = warnings |
| 18 | + end |
| 19 | + |
| 20 | + # @rbs (Lrama::Grammar grammar) -> void |
| 21 | + def warn(grammar) |
| 22 | + return unless @warnings |
| 23 | + |
| 24 | + build_grouped_usage(grammar).each do |rule, usage| |
| 25 | + next unless usage[:positional] && usage[:named] |
| 26 | + |
| 27 | + @logger.warn("warning: rule `#{rule.as_comment}` mixes positional and named references; use named references consistently") |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + private |
| 32 | + |
| 33 | + # @rbs (Lrama::Grammar grammar) -> Hash[Lrama::Grammar::Rule, { positional: bool, named: bool }] |
| 34 | + def build_grouped_usage(grammar) |
| 35 | + grouped_usage = {} #: Hash[Lrama::Grammar::Rule, { positional: bool, named: bool }] |
| 36 | + |
| 37 | + grammar.rules.each do |rule| |
| 38 | + next unless (token_code = rule.token_code) |
| 39 | + |
| 40 | + original_rule = rule.original_rule || rule |
| 41 | + usage = (grouped_usage[original_rule] ||= { positional: false, named: false }) |
| 42 | + classify_references(token_code.references, token_code.s_value, usage) |
| 43 | + end |
| 44 | + |
| 45 | + grouped_usage |
| 46 | + end |
| 47 | + |
| 48 | + # @rbs (Array[Lrama::Grammar::Reference] references, String source, { positional: bool, named: bool } usage) -> void |
| 49 | + def classify_references(references, source, usage) |
| 50 | + references.each do |ref| |
| 51 | + if positional_reference?(ref) |
| 52 | + usage[:positional] = true |
| 53 | + elsif named_reference?(ref, source) |
| 54 | + usage[:named] = true |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + # @rbs (Lrama::Grammar::Reference ref) -> bool |
| 60 | + def positional_reference?(ref) |
| 61 | + return false unless ref.type == :dollar |
| 62 | + return false if ref.index.nil? |
| 63 | + |
| 64 | + ref.name.nil? |
| 65 | + end |
| 66 | + |
| 67 | + # @rbs (Lrama::Grammar::Reference ref, String source) -> bool |
| 68 | + def named_reference?(ref, source) |
| 69 | + return false unless ref.type == :dollar |
| 70 | + |
| 71 | + return true if !ref.name.nil? && ref.name != "$" |
| 72 | + |
| 73 | + lhs_alias_reference?(ref, source) |
| 74 | + end |
| 75 | + |
| 76 | + # @rbs (Lrama::Grammar::Reference ref, String source) -> bool |
| 77 | + def lhs_alias_reference?(ref, source) |
| 78 | + return false unless ref.name == "$" |
| 79 | + |
| 80 | + lexeme = source[ref.first_column...ref.last_column] |
| 81 | + return false if lexeme.nil? |
| 82 | + |
| 83 | + # Keep ignoring special LHS references ($$, $<tag>$), same as reference_to_c. |
| 84 | + return false if special_lhs_reference_lexeme?(lexeme) |
| 85 | + |
| 86 | + # Treat remaining `$...` forms as named LHS aliases. |
| 87 | + true |
| 88 | + end |
| 89 | + |
| 90 | + # @rbs (String lexeme) -> bool |
| 91 | + def special_lhs_reference_lexeme?(lexeme) |
| 92 | + lexeme == "$$" || (lexeme.start_with?("$<") && lexeme.end_with?("$")) |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | +end |
0 commit comments