Skip to content

Commit

Permalink
Make source span position an explicit object member
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Nov 14, 2019
1 parent 98dbe38 commit bfafab4
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace Sass {

void AST_Node::update_pstate(const SourceSpan& pstate)
{
pstate_.offset += pstate - pstate_ + pstate.offset;
pstate_.offset += pstate.position - pstate_.position + pstate.offset;
}

sass::string AST_Node::to_string(Sass_Inspect_Options opt) const
Expand Down
4 changes: 2 additions & 2 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ namespace Sass {
// ToDo: add specific implementions to all children
virtual bool find ( bool (*f)(AST_Node_Obj) ) { return f(this); };
void update_pstate(const SourceSpan& pstate);
Offset off() { return pstate(); }
Position pos() { return pstate(); }
Offset off() { return pstate().off(); }
Position pos() { return pstate().pos(); }

// Some obects are not meant to be compared
// ToDo: maybe fallback to pointer comparison?
Expand Down
26 changes: 13 additions & 13 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Sass {
void Parser::advanceToNextToken() {
lex < css_comments >(false);
// advance to position
pstate += pstate.offset;
pstate.position += pstate.offset;
pstate.offset.column = 0;
pstate.offset.line = 0;
}
Expand Down Expand Up @@ -70,7 +70,7 @@ namespace Sass {

// report invalid utf8
if (it != end) {
pstate += Offset::init(position, it);
pstate.position += Offset::init(position, it);
traces.push_back(Backtrace(pstate));
throw Exception::InvalidSass(pstate, traces, "Invalid UTF-8 sequence");
}
Expand Down Expand Up @@ -545,7 +545,7 @@ namespace Sass {
if (i < p) {
sass::string parsed(i, p);
String_Constant_Obj str = SASS_MEMORY_NEW(String_Constant, pstate, parsed);
pstate += Offset(parsed);
pstate.position += Offset(parsed);
str->update_pstate(pstate);
schema->append(str);
}
Expand All @@ -567,7 +567,7 @@ namespace Sass {
// add to the string schema
schema->append(interpolant);
// advance parser state
pstate.add(p+2, j);
pstate.position.add(p+2, j);
// advance position
i = j;
}
Expand All @@ -578,7 +578,7 @@ namespace Sass {
if (i < end_of_selector) {
sass::string parsed(i, end_of_selector);
String_Constant_Obj str = SASS_MEMORY_NEW(String_Constant, pstate, parsed);
pstate += Offset(parsed);
pstate.position += Offset(parsed);
str->update_pstate(pstate);
i = end_of_selector;
schema->append(str);
Expand All @@ -595,7 +595,7 @@ namespace Sass {
selector_schema->update_pstate(pstate);
schema->update_pstate(pstate);

after_token = before_token = pstate;
after_token = before_token = pstate.position;

// return parsed result
return selector_schema.detach();
Expand Down Expand Up @@ -941,7 +941,7 @@ namespace Sass {
}

SourceSpan ps = map->pstate();
ps.offset = pstate - ps + pstate.offset;
ps.offset = pstate.position - ps.position + pstate.offset;
map->pstate(ps);

return map;
Expand Down Expand Up @@ -1081,7 +1081,7 @@ namespace Sass {
if (operands.size() == 0) return conj;
// fold all operands into one binary expression
ExpressionObj ex = fold_operands(conj, operands, { Sass_OP::OR });
state.offset = pstate - state + pstate.offset;
state.offset = pstate.position - state.position + pstate.offset;
ex->pstate(state);
return ex;
}
Expand All @@ -1104,7 +1104,7 @@ namespace Sass {
if (operands.size() == 0) return rel;
// fold all operands into one binary expression
ExpressionObj ex = fold_operands(rel, operands, { Sass_OP::AND });
state.offset = pstate - state + pstate.offset;
state.offset = pstate.position - state.position + pstate.offset;
ex->pstate(state);
return ex;
}
Expand Down Expand Up @@ -1153,7 +1153,7 @@ namespace Sass {
// single nested items. So we cannot set delay on the
// returned result here, as we have lost nestings ...
ExpressionObj ex = fold_operands(lhs, operands, operators);
state.offset = pstate - state + pstate.offset;
state.offset = pstate.position - state.position + pstate.offset;
ex->pstate(state);
return ex;
}
Expand Down Expand Up @@ -1202,7 +1202,7 @@ namespace Sass {

if (operands.size() == 0) return lhs;
ExpressionObj ex = fold_operands(lhs, operands, operators);
state.offset = pstate - state + pstate.offset;
state.offset = pstate.position - state.position + pstate.offset;
ex->pstate(state);
return ex;
}
Expand Down Expand Up @@ -1232,7 +1232,7 @@ namespace Sass {
}
// operands and operators to binary expression
ExpressionObj ex = fold_operands(factor, operands, operators);
state.offset = pstate - state + pstate.offset;
state.offset = pstate.position - state.position + pstate.offset;
ex->pstate(state);
return ex;
}
Expand Down Expand Up @@ -2889,7 +2889,7 @@ namespace Sass {

void Parser::error(sass::string msg)
{
error(msg, pstate);
error(msg, pstate.position);
}

// print a css parsing error with actual context information from parsed source
Expand Down
2 changes: 1 addition & 1 deletion src/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Sass {

Parser(Context& ctx, const SourceSpan& pstate, Backtraces traces, bool allow_parent = true)
: SourceSpan(pstate), ctx(ctx), block_stack(), stack(0),
source(0), position(0), end(0), before_token(pstate), after_token(pstate),
source(0), position(0), end(0), before_token(pstate.position), after_token(pstate.position),
pstate(pstate), traces(traces), indentation(0), nestings(0), allow_parent(allow_parent)
{
stack.push_back(Scope::Root);
Expand Down
2 changes: 1 addition & 1 deletion src/parser_selectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace Sass {
if (!seq->empty()) { sel = seq->last()->to_string({ NESTED, 5 }); }
// ToDo: parser should throw parser exceptions
error("Invalid CSS after \"" + sel + "\": expected \"{\", was \"" + found + "\"\n\n"
"\"" + found + "\" may only be used at the beginning of a compound selector.", state);
"\"" + found + "\" may only be used at the beginning of a compound selector.", state.position);
}
// parse functional
else if (match < re_functional >())
Expand Down
4 changes: 2 additions & 2 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ namespace Sass {


SourceSpan::SourceSpan(const char* path, const char* src, const size_t file)
: Position(file, 0, 0), path(path), src(src), offset(0, 0) { }
: position(file, 0, 0), offset(0, 0), path(path), src(src) { }

SourceSpan::SourceSpan(const char* path, const char* src, const Position& position, Offset offset)
: Position(position), path(path), src(src), offset(offset) { }
: position(position), offset(offset), path(path), src(src) { }

Position Position::add(const char* begin, const char* end)
{
Expand Down
15 changes: 8 additions & 7 deletions src/position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ namespace Sass {
bool operator==(Token t) { return to_string() == t.to_string(); }
};

class SourceSpan : public Position {
class SourceSpan {

public: // c-tor
SourceSpan(const char* path, const char* src = 0, const size_t file = sass::string::npos);
SourceSpan(const char* path, const char* src, const Position& position, Offset offset = Offset(0, 0));

public: // down casts
Offset off() { return *this; }
Position pos() { return *this; }
Offset off() { return position; }
Position pos() { return position; }
SourceSpan pstate() { return *this; }

const char* getPath() const {
Expand All @@ -117,19 +117,20 @@ namespace Sass {
return src;
}
size_t getLine() const {
return line + 1;
return position.line + 1;
}
size_t getColumn() const {
return column + 1;
return position.column + 1;
}
size_t getSrcId() const {
return file;
return position.file;
}

public:
Position position;
Offset offset;
const char* path;
const char* src;
Offset offset;

};

Expand Down
12 changes: 6 additions & 6 deletions src/sass_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ namespace Sass {
}

// now create the code trace (ToDo: maybe have util functions?)
if (e.pstate.line != sass::string::npos &&
e.pstate.column != sass::string::npos &&
if (e.pstate.position.line != sass::string::npos &&
e.pstate.position.column != sass::string::npos &&
e.pstate.src != nullptr) {
size_t lines = e.pstate.line;
size_t lines = e.pstate.position.line;
// scan through src until target line
// move line_beg pointer to line start
const char* line_beg;
Expand All @@ -96,12 +96,12 @@ namespace Sass {
size_t move_in = 0; size_t shorten = 0;
size_t left_chars = 42; size_t max_chars = 76;
// reported excerpt should not exceed `max_chars` chars
if (e.pstate.column > line_len) left_chars = e.pstate.column;
if (e.pstate.column > left_chars) move_in = e.pstate.column - left_chars;
if (e.pstate.position.column > line_len) left_chars = e.pstate.position.column;
if (e.pstate.position.column > left_chars) move_in = e.pstate.position.column - left_chars;
if (line_len > max_chars + move_in) shorten = line_len - move_in - max_chars;
utf8::advance(line_beg, move_in, line_end);
utf8::retreat(line_end, shorten, line_beg);
sass::string sanitized; sass::string marker(e.pstate.column - move_in, '-');
sass::string sanitized; sass::string marker(e.pstate.position.column - move_in, '-');
utf8::replace_invalid(line_beg, line_end, std::back_inserter(sanitized));
msg_stream << ">> " << sanitized << "\n";
msg_stream << " " << marker << "^\n";
Expand Down
10 changes: 5 additions & 5 deletions src/source_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,20 @@ namespace Sass {

void SourceMap::add_open_mapping(const AST_Node* node)
{
mappings.push_back(Mapping(node->pstate(), current_position));
mappings.push_back(Mapping(node->pstate().position, current_position));
}

void SourceMap::add_close_mapping(const AST_Node* node)
{
mappings.push_back(Mapping(node->pstate() + node->pstate().offset, current_position));
mappings.push_back(Mapping(node->pstate().position + node->pstate().offset, current_position));
}

SourceSpan SourceMap::remap(const SourceSpan& pstate) {
for (size_t i = 0; i < mappings.size(); ++i) {
if (
mappings[i].generated_position.file == pstate.file &&
mappings[i].generated_position.line == pstate.line &&
mappings[i].generated_position.column == pstate.column
mappings[i].generated_position.file == pstate.position.file &&
mappings[i].generated_position.line == pstate.position.line &&
mappings[i].generated_position.column == pstate.position.column
) return SourceSpan(pstate.path, pstate.src, mappings[i].original_position, pstate.offset);
}
return SourceSpan(pstate.path, pstate.src, Position(-1, -1, -1), Offset(0, 0));
Expand Down

0 comments on commit bfafab4

Please sign in to comment.