Skip to content

Commit 2af9e00

Browse files
committed
Make source span position an explicit object member
1 parent 49e1e70 commit 2af9e00

File tree

9 files changed

+39
-38
lines changed

9 files changed

+39
-38
lines changed

src/ast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace Sass {
5555

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

6161
sass::string AST_Node::to_string(Sass_Inspect_Options opt) const

src/ast.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ namespace Sass {
8484
// ToDo: add specific implementions to all children
8585
virtual bool find ( bool (*f)(AST_Node_Obj) ) { return f(this); };
8686
void update_pstate(const SourceSpan& pstate);
87-
Offset off() { return pstate(); }
88-
Position pos() { return pstate(); }
87+
Offset off() { return pstate().off(); }
88+
Position pos() { return pstate().pos(); }
8989

9090
// Some obects are not meant to be compared
9191
// ToDo: maybe fallback to pointer comparison?

src/parser.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace Sass {
4040
void Parser::advanceToNextToken() {
4141
lex < css_comments >(false);
4242
// advance to position
43-
pstate += pstate.offset;
43+
pstate.position += pstate.offset;
4444
pstate.offset.column = 0;
4545
pstate.offset.line = 0;
4646
}
@@ -70,7 +70,7 @@ namespace Sass {
7070

7171
// report invalid utf8
7272
if (it != end) {
73-
pstate += Offset::init(position, it);
73+
pstate.position += Offset::init(position, it);
7474
traces.push_back(Backtrace(pstate));
7575
throw Exception::InvalidSass(pstate, traces, "Invalid UTF-8 sequence");
7676
}
@@ -545,7 +545,7 @@ namespace Sass {
545545
if (i < p) {
546546
sass::string parsed(i, p);
547547
String_Constant_Obj str = SASS_MEMORY_NEW(String_Constant, pstate, parsed);
548-
pstate += Offset(parsed);
548+
pstate.position += Offset(parsed);
549549
str->update_pstate(pstate);
550550
schema->append(str);
551551
}
@@ -567,7 +567,7 @@ namespace Sass {
567567
// add to the string schema
568568
schema->append(interpolant);
569569
// advance parser state
570-
pstate.add(p+2, j);
570+
pstate.position.add(p+2, j);
571571
// advance position
572572
i = j;
573573
}
@@ -578,7 +578,7 @@ namespace Sass {
578578
if (i < end_of_selector) {
579579
sass::string parsed(i, end_of_selector);
580580
String_Constant_Obj str = SASS_MEMORY_NEW(String_Constant, pstate, parsed);
581-
pstate += Offset(parsed);
581+
pstate.position += Offset(parsed);
582582
str->update_pstate(pstate);
583583
i = end_of_selector;
584584
schema->append(str);
@@ -595,7 +595,7 @@ namespace Sass {
595595
selector_schema->update_pstate(pstate);
596596
schema->update_pstate(pstate);
597597

598-
after_token = before_token = pstate;
598+
after_token = before_token = pstate.position;
599599

600600
// return parsed result
601601
return selector_schema.detach();
@@ -941,7 +941,7 @@ namespace Sass {
941941
}
942942

943943
SourceSpan ps = map->pstate();
944-
ps.offset = pstate - ps + pstate.offset;
944+
ps.offset = pstate.position - ps.position + pstate.offset;
945945
map->pstate(ps);
946946

947947
return map;
@@ -1081,7 +1081,7 @@ namespace Sass {
10811081
if (operands.size() == 0) return conj;
10821082
// fold all operands into one binary expression
10831083
ExpressionObj ex = fold_operands(conj, operands, { Sass_OP::OR });
1084-
state.offset = pstate - state + pstate.offset;
1084+
state.offset = pstate.position - state.position + pstate.offset;
10851085
ex->pstate(state);
10861086
return ex;
10871087
}
@@ -1104,7 +1104,7 @@ namespace Sass {
11041104
if (operands.size() == 0) return rel;
11051105
// fold all operands into one binary expression
11061106
ExpressionObj ex = fold_operands(rel, operands, { Sass_OP::AND });
1107-
state.offset = pstate - state + pstate.offset;
1107+
state.offset = pstate.position - state.position + pstate.offset;
11081108
ex->pstate(state);
11091109
return ex;
11101110
}
@@ -1153,7 +1153,7 @@ namespace Sass {
11531153
// single nested items. So we cannot set delay on the
11541154
// returned result here, as we have lost nestings ...
11551155
ExpressionObj ex = fold_operands(lhs, operands, operators);
1156-
state.offset = pstate - state + pstate.offset;
1156+
state.offset = pstate.position - state.position + pstate.offset;
11571157
ex->pstate(state);
11581158
return ex;
11591159
}
@@ -1202,7 +1202,7 @@ namespace Sass {
12021202

12031203
if (operands.size() == 0) return lhs;
12041204
ExpressionObj ex = fold_operands(lhs, operands, operators);
1205-
state.offset = pstate - state + pstate.offset;
1205+
state.offset = pstate.position - state.position + pstate.offset;
12061206
ex->pstate(state);
12071207
return ex;
12081208
}
@@ -1232,7 +1232,7 @@ namespace Sass {
12321232
}
12331233
// operands and operators to binary expression
12341234
ExpressionObj ex = fold_operands(factor, operands, operators);
1235-
state.offset = pstate - state + pstate.offset;
1235+
state.offset = pstate.position - state.position + pstate.offset;
12361236
ex->pstate(state);
12371237
return ex;
12381238
}
@@ -2889,7 +2889,7 @@ namespace Sass {
28892889

28902890
void Parser::error(sass::string msg)
28912891
{
2892-
error(msg, pstate);
2892+
error(msg, pstate.position);
28932893
}
28942894

28952895
// print a css parsing error with actual context information from parsed source

src/parser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace Sass {
5757

5858
Parser(Context& ctx, const SourceSpan& pstate, Backtraces traces, bool allow_parent = true)
5959
: SourceSpan(pstate), ctx(ctx), block_stack(), stack(0),
60-
source(0), position(0), end(0), before_token(pstate), after_token(pstate),
60+
source(0), position(0), end(0), before_token(pstate.position), after_token(pstate.position),
6161
pstate(pstate), traces(traces), indentation(0), nestings(0), allow_parent(allow_parent)
6262
{
6363
stack.push_back(Scope::Root);

src/parser_selectors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ namespace Sass {
148148
if (!seq->empty()) { sel = seq->last()->to_string({ NESTED, 5 }); }
149149
// ToDo: parser should throw parser exceptions
150150
error("Invalid CSS after \"" + sel + "\": expected \"{\", was \"" + found + "\"\n\n"
151-
"\"" + found + "\" may only be used at the beginning of a compound selector.", state);
151+
"\"" + found + "\" may only be used at the beginning of a compound selector.", state.position);
152152
}
153153
// parse functional
154154
else if (match < re_functional >())

src/position.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ namespace Sass {
118118

119119

120120
SourceSpan::SourceSpan(const char* path, const char* src, const size_t file)
121-
: Position(file, 0, 0), path(path), src(src), offset(0, 0) { }
121+
: position(file, 0, 0), offset(0, 0), path(path), src(src) { }
122122

123123
SourceSpan::SourceSpan(const char* path, const char* src, const Position& position, Offset offset)
124-
: Position(position), path(path), src(src), offset(offset) { }
124+
: position(position), offset(offset), path(path), src(src) { }
125125

126126
Position Position::add(const char* begin, const char* end)
127127
{

src/position.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ namespace Sass {
9999
bool operator==(Token t) { return to_string() == t.to_string(); }
100100
};
101101

102-
class SourceSpan : public Position {
102+
class SourceSpan {
103103

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

108108
public: // down casts
109-
Offset off() { return *this; }
110-
Position pos() { return *this; }
109+
Offset off() { return position; }
110+
Position pos() { return position; }
111111
SourceSpan pstate() { return *this; }
112112

113113
const char* getPath() const {
@@ -117,19 +117,20 @@ namespace Sass {
117117
return src;
118118
}
119119
size_t getLine() const {
120-
return line + 1;
120+
return position.line + 1;
121121
}
122122
size_t getColumn() const {
123-
return column + 1;
123+
return position.column + 1;
124124
}
125125
size_t getSrcId() const {
126-
return file;
126+
return position.file;
127127
}
128128

129129
public:
130+
Position position;
131+
Offset offset;
130132
const char* path;
131133
const char* src;
132-
Offset offset;
133134

134135
};
135136

src/sass_context.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ namespace Sass {
7575
}
7676

7777
// now create the code trace (ToDo: maybe have util functions?)
78-
if (e.pstate.line != sass::string::npos &&
79-
e.pstate.column != sass::string::npos &&
78+
if (e.pstate.position.line != sass::string::npos &&
79+
e.pstate.position.column != sass::string::npos &&
8080
e.pstate.src != nullptr) {
81-
size_t lines = e.pstate.line;
81+
size_t lines = e.pstate.position.line;
8282
// scan through src until target line
8383
// move line_beg pointer to line start
8484
const char* line_beg;
@@ -96,12 +96,12 @@ namespace Sass {
9696
size_t move_in = 0; size_t shorten = 0;
9797
size_t left_chars = 42; size_t max_chars = 76;
9898
// reported excerpt should not exceed `max_chars` chars
99-
if (e.pstate.column > line_len) left_chars = e.pstate.column;
100-
if (e.pstate.column > left_chars) move_in = e.pstate.column - left_chars;
99+
if (e.pstate.position.column > line_len) left_chars = e.pstate.position.column;
100+
if (e.pstate.position.column > left_chars) move_in = e.pstate.position.column - left_chars;
101101
if (line_len > max_chars + move_in) shorten = line_len - move_in - max_chars;
102102
utf8::advance(line_beg, move_in, line_end);
103103
utf8::retreat(line_end, shorten, line_beg);
104-
sass::string sanitized; sass::string marker(e.pstate.column - move_in, '-');
104+
sass::string sanitized; sass::string marker(e.pstate.position.column - move_in, '-');
105105
utf8::replace_invalid(line_beg, line_end, std::back_inserter(sanitized));
106106
msg_stream << ">> " << sanitized << "\n";
107107
msg_stream << " " << marker << "^\n";

src/source_map.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,20 @@ namespace Sass {
175175

176176
void SourceMap::add_open_mapping(const AST_Node* node)
177177
{
178-
mappings.push_back(Mapping(node->pstate(), current_position));
178+
mappings.push_back(Mapping(node->pstate().position, current_position));
179179
}
180180

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

186186
SourceSpan SourceMap::remap(const SourceSpan& pstate) {
187187
for (size_t i = 0; i < mappings.size(); ++i) {
188188
if (
189-
mappings[i].generated_position.file == pstate.file &&
190-
mappings[i].generated_position.line == pstate.line &&
191-
mappings[i].generated_position.column == pstate.column
189+
mappings[i].generated_position.file == pstate.position.file &&
190+
mappings[i].generated_position.line == pstate.position.line &&
191+
mappings[i].generated_position.column == pstate.position.column
192192
) return SourceSpan(pstate.path, pstate.src, mappings[i].original_position, pstate.offset);
193193
}
194194
return SourceSpan(pstate.path, pstate.src, Position(-1, -1, -1), Offset(0, 0));

0 commit comments

Comments
 (0)