Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose dirname from SourceCodePos #397

Merged
merged 3 commits into from
Sep 20, 2022
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
7 changes: 7 additions & 0 deletions core/SourceCodePos.savi
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@
// TODO: include more properties from the original Savi::Source::Pos?
:var string String: ""
:var filename String: ""
:: The filepath relative to the package
:var filepath_pkgrel String: ""
:: The filepath relative to the root package
:var filepath_rootpkgrel String: ""
:var dirname String: ""
:var pkgname String: ""
:var pkgpath String: ""
:var row USize: 0
:var col USize: 0
14 changes: 10 additions & 4 deletions src/savi/compiler/code_gen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3262,10 +3262,16 @@ class Savi::Compiler::CodeGen
def gen_source_code_pos(pos : Source::Pos)
@source_code_pos_globals.fetch pos do
global = gen_global_const(@gtypes["SourceCodePosition"], {
"string" => gen_string(pos.content),
"filename" => gen_string(pos.source.filename),
"row" => @isize.const_int(pos.row),
"col" => @isize.const_int(pos.col),
"string" => gen_string(pos.content),
"filename"=> gen_string(pos.source.filename),
"dirname" => gen_string(pos.source.dirname),
"pkgname" => gen_string(pos.source.package.name),
"pkgpath" => gen_string(pos.source.package.path),
"row" => @isize.const_int(pos.row),
"col" => @isize.const_int(pos.col),
"filepath_pkgrel" => gen_string(pos.source.filepath_relative_to_package),
"filepath_rootpkgrel" => gen_string((Path.new(pos.source.dirname).relative_to(
Path.new(@ctx.not_nil!.root_package_link.path)) / pos.source.filename).to_s)
})

@source_code_pos_globals[pos] = global
Expand Down
8 changes: 8 additions & 0 deletions src/savi/source.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ struct Savi::Source
File.join(@dirname, @filename)
end

def filepath_relative_to_package
if reldir = Path.new(@dirname).relative_to?(@package.path)
(reldir / @filename).to_s
else
raise "File #{path} not relative to package #{@package.path}"
end
end

def entire_pos
Source::Pos.index_range(self, 0, content.bytesize)
end
Expand Down