Skip to content

Commit

Permalink
Add some helper function to source span
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Nov 15, 2019
1 parent a96e90e commit 49e1e70
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 60 deletions.
10 changes: 5 additions & 5 deletions src/backtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ namespace Sass {
const Backtrace& trace = traces[i];

// make path relative to the current directory
sass::string rel_path(File::abs2rel(trace.pstate.path, cwd, cwd));
sass::string rel_path(File::abs2rel(trace.pstate.getPath(), cwd, cwd));

// skip functions on error cases (unsure why ruby sass does this)
// if (trace.caller.substr(0, 6) == ", in f") continue;

if (first) {
ss << indent;
ss << "on line ";
ss << trace.pstate.line + 1;
ss << trace.pstate.getLine();
ss << ":";
ss << trace.pstate.column + 1;
ss << trace.pstate.getColumn();
ss << " of " << rel_path;
// ss << trace.caller;
first = false;
Expand All @@ -34,9 +34,9 @@ namespace Sass {
ss << std::endl;
ss << indent;
ss << "from line ";
ss << trace.pstate.line + 1;
ss << trace.pstate.getLine();
ss << ":";
ss << trace.pstate.column + 1;
ss << trace.pstate.getColumn();
ss << " of " << rel_path;
}

Expand Down
34 changes: 17 additions & 17 deletions src/error_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ namespace Sass {
void warning(sass::string msg, SourceSpan pstate)
{
sass::string cwd(Sass::File::get_cwd());
sass::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.path));
sass::string abs_path(Sass::File::rel2abs(pstate.getPath(), cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.getPath(), cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.getPath()));

std::cerr << "WARNING on line " << pstate.line+1 << ", column " << pstate.column+1 << " of " << output_path << ":" << std::endl;
std::cerr << "WARNING on line " << pstate.getLine() << ", column " << pstate.getColumn() << " of " << output_path << ":" << std::endl;
std::cerr << msg << std::endl << std::endl;
}

Expand All @@ -180,24 +180,24 @@ namespace Sass {
void deprecated_function(sass::string msg, SourceSpan pstate)
{
sass::string cwd(Sass::File::get_cwd());
sass::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.path));
sass::string abs_path(Sass::File::rel2abs(pstate.getPath(), cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.getPath(), cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.getPath()));

std::cerr << "DEPRECATION WARNING: " << msg << std::endl;
std::cerr << "will be an error in future versions of Sass." << std::endl;
std::cerr << " on line " << pstate.line+1 << " of " << output_path << std::endl;
std::cerr << " on line " << pstate.getLine() << " of " << output_path << std::endl;
}

void deprecated(sass::string msg, sass::string msg2, bool with_column, SourceSpan pstate)
{
sass::string cwd(Sass::File::get_cwd());
sass::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, pstate.path, pstate.path));
sass::string abs_path(Sass::File::rel2abs(pstate.getPath(), cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.getPath(), cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, pstate.getPath(), pstate.getPath()));

std::cerr << "DEPRECATION WARNING on line " << pstate.line + 1;
if (with_column) std::cerr << ", column " << pstate.column + pstate.offset.column + 1;
std::cerr << "DEPRECATION WARNING on line " << pstate.getLine();
// if (with_column) std::cerr << ", column " << pstate.column + pstate.offset.column + 1;
if (output_path.length()) std::cerr << " of " << output_path;
std::cerr << ":" << std::endl;
std::cerr << msg << std::endl;
Expand All @@ -208,12 +208,12 @@ namespace Sass {
void deprecated_bind(sass::string msg, SourceSpan pstate)
{
sass::string cwd(Sass::File::get_cwd());
sass::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.path));
sass::string abs_path(Sass::File::rel2abs(pstate.getPath(), cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.getPath(), cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.getPath()));

std::cerr << "WARNING: " << msg << std::endl;
std::cerr << " on line " << pstate.line+1 << " of " << output_path << std::endl;
std::cerr << " on line " << pstate.getLine() << " of " << output_path << std::endl;
std::cerr << "This will be an error in future versions of Sass." << std::endl;
}

Expand Down
40 changes: 20 additions & 20 deletions src/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ namespace Sass {
// add call stack entry
callee_stack().push_back({
"@warn",
w->pstate().path,
w->pstate().line + 1,
w->pstate().column + 1,
w->pstate().getPath(),
w->pstate().getLine(),
w->pstate().getColumn(),
SASS_CALLEE_FUNCTION,
{ env }
});
Expand Down Expand Up @@ -392,9 +392,9 @@ namespace Sass {
// add call stack entry
callee_stack().push_back({
"@error",
e->pstate().path,
e->pstate().line + 1,
e->pstate().column + 1,
e->pstate().getPath(),
e->pstate().getLine(),
e->pstate().getColumn(),
SASS_CALLEE_FUNCTION,
{ env }
});
Expand Down Expand Up @@ -436,9 +436,9 @@ namespace Sass {
// add call stack entry
callee_stack().push_back({
"@debug",
d->pstate().path,
d->pstate().line + 1,
d->pstate().column + 1,
d->pstate().getPath(),
d->pstate().getLine(),
d->pstate().getColumn(),
SASS_CALLEE_FUNCTION,
{ env }
});
Expand All @@ -462,12 +462,12 @@ namespace Sass {
}

sass::string result(unquote(message->to_sass()));
sass::string abs_path(Sass::File::rel2abs(d->pstate().path, cwd(), cwd()));
sass::string rel_path(Sass::File::abs2rel(d->pstate().path, cwd(), cwd()));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, d->pstate().path));
sass::string abs_path(Sass::File::rel2abs(d->pstate().getPath(), cwd(), cwd()));
sass::string rel_path(Sass::File::abs2rel(d->pstate().getPath(), cwd(), cwd()));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, d->pstate().getPath()));
options().output_style = outstyle;

std::cerr << output_path << ":" << d->pstate().line+1 << " DEBUG: " << result;
std::cerr << output_path << ":" << d->pstate().getLine() << " DEBUG: " << result;
std::cerr << std::endl;
return 0;
}
Expand Down Expand Up @@ -1043,9 +1043,9 @@ namespace Sass {
traces.push_back(Backtrace(c->pstate(), msg));
callee_stack().push_back({
c->name().c_str(),
c->pstate().path,
c->pstate().line + 1,
c->pstate().column + 1,
c->pstate().getPath(),
c->pstate().getLine(),
c->pstate().getColumn(),
SASS_CALLEE_FUNCTION,
{ env }
});
Expand Down Expand Up @@ -1083,9 +1083,9 @@ namespace Sass {
traces.push_back(Backtrace(c->pstate(), msg));
callee_stack().push_back({
c->name().c_str(),
c->pstate().path,
c->pstate().line + 1,
c->pstate().column + 1,
c->pstate().getPath(),
c->pstate().getLine(),
c->pstate().getColumn(),
SASS_CALLEE_C_FUNCTION,
{ env }
});
Expand Down Expand Up @@ -1122,7 +1122,7 @@ namespace Sass {

// link back to function definition
// only do this for custom functions
if (result->pstate().file == sass::string::npos)
if (result->pstate().getSrcId() == sass::string::npos)
result->pstate(c->pstate());

result = result->perform(this);
Expand Down
6 changes: 3 additions & 3 deletions src/expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,9 @@ namespace Sass {
traces.push_back(Backtrace(c->pstate(), msg));
ctx.callee_stack.push_back({
c->name().c_str(),
c->pstate().path,
c->pstate().line + 1,
c->pstate().column + 1,
c->pstate().getPath(),
c->pstate().getLine(),
c->pstate().getColumn(),
SASS_CALLEE_MIXIN,
{ env }
});
Expand Down
4 changes: 2 additions & 2 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ namespace Sass {
if (opt.source_comments) {
sass::ostream ss;
append_indentation();
sass::string path(File::abs2rel(r->pstate().path));
ss << "/* line " << r->pstate().line + 1 << ", " << path << " */";
sass::string path(File::abs2rel(r->pstate().getPath()));
ss << "/* line " << r->pstate().getLine() << ", " << path << " */";
append_string(ss.str());
append_optional_linefeed();
}
Expand Down
6 changes: 3 additions & 3 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace Sass {
// check seems a bit esoteric but works
if (ctx.resources.size() == 1) {
// apply headers only on very first include
ctx.apply_custom_headers(root, path, pstate);
ctx.apply_custom_headers(root, getPath(), pstate);
}

// parse children nodes
Expand Down Expand Up @@ -346,9 +346,9 @@ namespace Sass {
imp->urls().push_back(location.second);
}
// check if custom importers want to take over the handling
else if (!ctx.call_importers(unquote(location.first), path, pstate, imp)) {
else if (!ctx.call_importers(unquote(location.first), getPath(), pstate, imp)) {
// nobody wants it, so we do our import
ctx.import_url(imp, location.first, path);
ctx.import_url(imp, location.first, getPath());
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ namespace Sass {
Position pos() { return *this; }
SourceSpan pstate() { return *this; }

const char* getPath() const {
return path;
}
const char* getRawData() const {
return src;
}
size_t getLine() const {
return line + 1;
}
size_t getColumn() const {
return column + 1;
}
size_t getSrcId() const {
return file;
}

public:
const char* path;
const char* src;
Expand Down
20 changes: 10 additions & 10 deletions src/sass_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ namespace Sass {

if (e.traces.empty()) {
// we normally should have some traces, still here as a fallback
sass::string rel_path(Sass::File::abs2rel(e.pstate.path, cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(e.pstate.getPath(), cwd, cwd));
msg_stream << sass::string(msg_prefix.size() + 2, ' ');
msg_stream << " on line " << e.pstate.line + 1 << " of " << rel_path << "\n";
msg_stream << " on line " << e.pstate.getLine() << " of " << rel_path << "\n";
}
else {
sass::string rel_path(Sass::File::abs2rel(e.pstate.path, cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(e.pstate.getPath(), cwd, cwd));
msg_stream << traces_to_string(e.traces, " ");
}

Expand Down Expand Up @@ -109,20 +109,20 @@ namespace Sass {

JsonNode* json_err = json_mkobject();
json_append_member(json_err, "status", json_mknumber(1));
json_append_member(json_err, "file", json_mkstring(e.pstate.path));
json_append_member(json_err, "line", json_mknumber((double)(e.pstate.line + 1)));
json_append_member(json_err, "column", json_mknumber((double)(e.pstate.column + 1)));
json_append_member(json_err, "file", json_mkstring(e.pstate.getPath()));
json_append_member(json_err, "line", json_mknumber((double)(e.pstate.getLine())));
json_append_member(json_err, "column", json_mknumber((double)(e.pstate.getColumn())));
json_append_member(json_err, "message", json_mkstring(e.what()));
json_append_member(json_err, "formatted", json_mkstream(msg_stream));
try { c_ctx->error_json = json_stringify(json_err, " "); }
catch (...) {} // silently ignore this error?
c_ctx->error_message = sass_copy_string(msg_stream.str());
c_ctx->error_text = sass_copy_c_string(e.what());
c_ctx->error_status = 1;
c_ctx->error_file = sass_copy_c_string(e.pstate.path);
c_ctx->error_line = e.pstate.line + 1;
c_ctx->error_column = e.pstate.column + 1;
c_ctx->error_src = sass_copy_c_string(e.pstate.src);
c_ctx->error_file = sass_copy_c_string(e.pstate.getPath());
c_ctx->error_line = e.pstate.getLine();
c_ctx->error_column = e.pstate.getColumn();
c_ctx->error_src = sass_copy_c_string(e.pstate.getRawData());
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
json_delete(json_err);
Expand Down

0 comments on commit 49e1e70

Please sign in to comment.