From ece36063989bb772ff78958feeb5c271a7dff45b Mon Sep 17 00:00:00 2001 From: "William T. Nelson" <35801+wtn@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:18:13 -0600 Subject: [PATCH] Fix header include path for output outside the current directory Co-authored-by: Claude --- lib/lrama/output.rb | 10 +++- spec/lrama/output_spec.rb | 97 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 2 deletions(-) diff --git a/lib/lrama/output.rb b/lib/lrama/output.rb index 9bc5bca0..b5a8abc6 100644 --- a/lib/lrama/output.rb +++ b/lib/lrama/output.rb @@ -28,7 +28,7 @@ def initialize( @context = context @grammar = grammar @error_recovery = error_recovery - @include_header = header_file_path ? header_file_path.sub("./", "") : nil + @include_header = header_file_path ? include_header_path(output_file_path, header_file_path) : nil end if ERB.instance_method(:initialize).parameters.last.first == :key @@ -386,6 +386,14 @@ def percent_code(name) private + def include_header_path(output_file_path, header_file_path) + if File.identical?(File.dirname(output_file_path), File.dirname(header_file_path)) + File.basename(header_file_path) + else + header_file_path.sub(/\A\.\//, "") + end + end + def eval_template(file, path) tmp = ERB.render(file, context: @context, output: self) replace_special_variables(tmp, path) diff --git a/spec/lrama/output_spec.rb b/spec/lrama/output_spec.rb index 45e819cf..760fcbb1 100644 --- a/spec/lrama/output_spec.rb +++ b/spec/lrama/output_spec.rb @@ -6,7 +6,7 @@ let(:output) { Lrama::Output.new( out: out, - output_file_path: "y.tab.c", + output_file_path: output_file_path, template_name: "bison/yacc.c", grammar_file_path: grammar_file_path, header_out: header_out, @@ -27,6 +27,7 @@ let(:states) { s = Lrama::States.new(grammar, Lrama::Tracer.new(Lrama::Logger.new)); s.compute; s } let(:context) { Lrama::Context.new(states) } let(:grammar_file_path) { fixture_path("common/basic.y") } + let(:output_file_path) { "y.tab.c" } let(:header_file_path) { "y.tab.h" } describe "#token_enums" do @@ -216,6 +217,100 @@ end end + describe "#include_header" do + around do |example| + Dir.mktmpdir do |dir| + Dir.chdir(dir) do + FileUtils.mkdir_p(["ext", "~build"]) + example.run + end + end + end + + context "header_file_path is not specified" do + let(:header_file_path) { nil } + + it "returns nil" do + expect(output.include_header).to be_nil + end + end + + context "header file is in the current directory" do + let(:header_file_path) { "./y.tab.h" } + + it "returns the file name" do + expect(output.include_header).to eq("y.tab.h") + end + end + + context "header file and output file are in the same parent directory" do + let(:output_file_path) { "../y.tab.c" } + let(:header_file_path) { "../y.tab.h" } + + it "returns the file name" do + expect(output.include_header).to eq("y.tab.h") + end + end + + context "header file and output file are in the same subdirectory" do + let(:output_file_path) { "ext/y.tab.c" } + let(:header_file_path) { "ext/y.tab.h" } + + it "returns the file name" do + expect(output.include_header).to eq("y.tab.h") + end + end + + context "the same directory is spelled differently" do + let(:output_file_path) { "./ext/y.tab.c" } + let(:header_file_path) { "ext/y.tab.h" } + + it "returns the file name" do + expect(output.include_header).to eq("y.tab.h") + end + end + + context "directory name starts with ~" do + let(:output_file_path) { "~build/y.tab.c" } + let(:header_file_path) { "~build/y.tab.h" } + + it "returns the file name" do + expect(output.include_header).to eq("y.tab.h") + end + end + + context "header file is in a different directory from output file" do + let(:header_file_path) { "../include/y.tab.h" } + + it "returns the path as specified" do + expect(output.include_header).to eq("../include/y.tab.h") + end + end + + context "header file path in a different directory starts with ./" do + let(:header_file_path) { "./include/y.tab.h" } + + it "removes only the leading ./" do + expect(output.include_header).to eq("include/y.tab.h") + end + end + + context "output directory path goes through a symlink and .." do + let(:output_file_path) { "build/link/../parser.c" } + let(:header_file_path) { File.join(Dir.pwd, "build/parser.h") } + + before do + skip "Windows resolves .. before symlinks" if windows? + FileUtils.mkdir_p(["build", "actual/sub"]) + File.symlink("../actual/sub", "build/link") + end + + it "returns the path as specified" do + expect(output.include_header).to eq(header_file_path) + end + end + end + describe "#render" do context "header_file_path is specified" do before do