Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/lrama/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
97 changes: 96 additions & 1 deletion spec/lrama/output_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down