|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe Lrama::Warnings::MixedReferences do |
| 4 | + describe "#warn" do |
| 5 | + let(:mixed_references_y) do |
| 6 | + <<~STR |
| 7 | + %{ |
| 8 | + // Prologue |
| 9 | + %} |
| 10 | + %union { |
| 11 | + int i; |
| 12 | + } |
| 13 | + %token <i> NUM |
| 14 | + %type <i> expr |
| 15 | + %% |
| 16 | + program: expr |
| 17 | + ; |
| 18 | + expr[result]: NUM[left] NUM[right] |
| 19 | + { |
| 20 | + $result = $1 + $right; |
| 21 | + } |
| 22 | + ; |
| 23 | + STR |
| 24 | + end |
| 25 | + |
| 26 | + let(:named_references_only_y) do |
| 27 | + <<~STR |
| 28 | + %{ |
| 29 | + // Prologue |
| 30 | + %} |
| 31 | + %union { |
| 32 | + int i; |
| 33 | + } |
| 34 | + %token <i> NUM |
| 35 | + %type <i> expr |
| 36 | + %% |
| 37 | + program: expr |
| 38 | + ; |
| 39 | + expr[result]: NUM[left] NUM[right] |
| 40 | + { |
| 41 | + $result = $left + $right; |
| 42 | + } |
| 43 | + ; |
| 44 | + STR |
| 45 | + end |
| 46 | + |
| 47 | + let(:mixed_across_actions_y) do |
| 48 | + <<~STR |
| 49 | + %{ |
| 50 | + // Prologue |
| 51 | + %} |
| 52 | + %union { |
| 53 | + int i; |
| 54 | + } |
| 55 | + %token <i> NUM |
| 56 | + %type <i> expr |
| 57 | + %% |
| 58 | + program: expr |
| 59 | + ; |
| 60 | + expr[result]: NUM |
| 61 | + { |
| 62 | + $1; |
| 63 | + } |
| 64 | + NUM[right] |
| 65 | + { |
| 66 | + $result = $right; |
| 67 | + } |
| 68 | + ; |
| 69 | + STR |
| 70 | + end |
| 71 | + |
| 72 | + context "when warnings true" do |
| 73 | + it "warns about mixed positional and named references in a rule" do |
| 74 | + grammar = Lrama::Parser.new(mixed_references_y, "warnings/mixed_references.y").parse |
| 75 | + grammar.prepare |
| 76 | + grammar.validate! |
| 77 | + states = Lrama::States.new(grammar, Lrama::Tracer.new(Lrama::Logger.new)) |
| 78 | + states.compute |
| 79 | + logger = Lrama::Logger.new |
| 80 | + allow(logger).to receive(:warn) |
| 81 | + |
| 82 | + Lrama::Warnings.new(logger, true).warn(grammar, states) |
| 83 | + |
| 84 | + expect(logger).to have_received(:warn).with("rule `expr: NUM NUM` mixes positional and named references; use named references consistently") |
| 85 | + end |
| 86 | + |
| 87 | + it "does not warn when named references are used consistently" do |
| 88 | + grammar = Lrama::Parser.new(named_references_only_y, "warnings/named_references_only.y").parse |
| 89 | + grammar.prepare |
| 90 | + grammar.validate! |
| 91 | + states = Lrama::States.new(grammar, Lrama::Tracer.new(Lrama::Logger.new)) |
| 92 | + states.compute |
| 93 | + logger = Lrama::Logger.new |
| 94 | + allow(logger).to receive(:warn) |
| 95 | + |
| 96 | + Lrama::Warnings.new(logger, true).warn(grammar, states) |
| 97 | + |
| 98 | + expect(logger).not_to have_received(:warn).with(/mixes positional and named references/) |
| 99 | + end |
| 100 | + |
| 101 | + it "warns once when references are mixed across actions in the same rule" do |
| 102 | + grammar = Lrama::Parser.new(mixed_across_actions_y, "warnings/mixed_across_actions.y").parse |
| 103 | + grammar.prepare |
| 104 | + grammar.validate! |
| 105 | + states = Lrama::States.new(grammar, Lrama::Tracer.new(Lrama::Logger.new)) |
| 106 | + states.compute |
| 107 | + logger = Lrama::Logger.new |
| 108 | + allow(logger).to receive(:warn) |
| 109 | + |
| 110 | + Lrama::Warnings.new(logger, true).warn(grammar, states) |
| 111 | + |
| 112 | + expect(logger).to have_received(:warn).with(/rule `expr: .*` mixes positional and named references; use named references consistently/).once |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + context "when warnings false" do |
| 117 | + it "does not warn even if references are mixed" do |
| 118 | + grammar = Lrama::Parser.new(mixed_references_y, "warnings/mixed_references.y").parse |
| 119 | + grammar.prepare |
| 120 | + grammar.validate! |
| 121 | + states = Lrama::States.new(grammar, Lrama::Tracer.new(Lrama::Logger.new)) |
| 122 | + states.compute |
| 123 | + logger = Lrama::Logger.new |
| 124 | + allow(logger).to receive(:warn) |
| 125 | + |
| 126 | + Lrama::Warnings.new(logger, false).warn(grammar, states) |
| 127 | + |
| 128 | + expect(logger).not_to have_received(:warn) |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | +end |
0 commit comments