Skip to content

Commit b7ced5d

Browse files
committed
Add source pointer to ParserState
Maybe we want to access the original source! Should come in handy to generate better errors!
1 parent af6acfd commit b7ced5d

File tree

8 files changed

+22
-16
lines changed

8 files changed

+22
-16
lines changed

context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ namespace Sass {
297297
0, 0
298298
);
299299
import_stack.push_back(import);
300-
Parser p(Parser::from_c_str(queue[i].source, *this, ParserState(queue[i].abs_path, i)));
300+
Parser p(Parser::from_c_str(queue[i].source, *this, ParserState(queue[i].abs_path, queue[i].source, i)));
301301
Block* ast = p.parse();
302302
sass_delete_import(import_stack.back());
303303
import_stack.pop_back();

parser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ namespace Sass {
213213
// char *srcmap = sass_import_take_srcmap(include);
214214
if (message) {
215215
if (line == string::npos && column == string::npos) error(message, pstate);
216-
else error(message, ParserState(message, Position(line, column)));
216+
else error(message, ParserState(message, source, Position(line, column)));
217217
} else if (source) {
218218
if (file) {
219219
ctx.add_source(file, inc_path, source);
@@ -564,7 +564,7 @@ namespace Sass {
564564
sel_source_position = before_token;
565565
}
566566
if (!sel_source_position.line) sel_source_position = before_token;
567-
Complex_Selector* cpx = new (ctx.mem) Complex_Selector(ParserState(path, sel_source_position), cmb, lhs, rhs);
567+
Complex_Selector* cpx = new (ctx.mem) Complex_Selector(ParserState(path, source, sel_source_position), cmb, lhs, rhs);
568568
cpx->media_block(last_media_block);
569569
cpx->last_block(block_stack.back());
570570
if (cpx_lf) cpx->has_line_break(cpx_lf);
@@ -2184,7 +2184,7 @@ namespace Sass {
21842184

21852185
void Parser::error(string msg, Position pos)
21862186
{
2187-
throw Sass_Error(Sass_Error::syntax, ParserState(path, pos.line ? pos : before_token, Offset(0, 0)), msg);
2187+
throw Sass_Error(Sass_Error::syntax, ParserState(path, source, pos.line ? pos : before_token, Offset(0, 0)), msg);
21882188
}
21892189

21902190
}

parser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ namespace Sass {
139139
after_token = before_token.inc(it_before_token, it_after_token);
140140

141141
// ToDo: could probably do this incremetal on original object
142-
pstate = ParserState(path, lexed, before_token, after_token - before_token);
142+
pstate = ParserState(path, source, lexed, before_token, after_token - before_token);
143143

144144
// advance internal char iterator
145145
return position = it_after_token;

position.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ namespace Sass {
9494
: Offset(line, column), file(file) { }
9595

9696

97-
ParserState::ParserState(string path, const size_t file)
98-
: Position(file, 0, 0), path(path), offset(0, 0), token() { }
97+
ParserState::ParserState(string path, const char* src, const size_t file)
98+
: Position(file, 0, 0), path(path), src(src), offset(0, 0), token() { }
9999

100-
ParserState::ParserState(string path, Position position, Offset offset)
101-
: Position(position), path(path), offset(offset), token() { }
100+
ParserState::ParserState(string path, const char* src, Position position, Offset offset)
101+
: Position(position), path(path), src(src), offset(offset), token() { }
102102

103-
ParserState::ParserState(string path, Token token, Position position, Offset offset)
104-
: Position(position), path(path), offset(offset), token(token) { }
103+
ParserState::ParserState(string path, const char* src, Token token, Position position, Offset offset)
104+
: Position(position), path(path), src(src), offset(offset), token(token) { }
105105

106106
void Position::add(const char* begin, const char* end)
107107
{

position.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,17 @@ namespace Sass {
9999
class ParserState : public Position {
100100

101101
public: // c-tor
102-
ParserState(string path, const size_t file = string::npos);
103-
ParserState(string path, Position position, Offset offset = Offset(0, 0));
104-
ParserState(string path, Token token, Position position, Offset offset = Offset(0, 0));
102+
ParserState(string path, const char* src = 0, const size_t file = string::npos);
103+
ParserState(string path, const char* src, Position position, Offset offset = Offset(0, 0));
104+
ParserState(string path, const char* src, Token token, Position position, Offset offset = Offset(0, 0));
105105

106106
public: // down casts
107107
Offset off() { return *this; };
108108
Position pos() { return *this; };
109109

110110
public:
111111
string path;
112+
const char* src;
112113
Offset offset;
113114
Token token;
114115

sass_context.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ extern "C" {
128128
char* error_file;
129129
size_t error_line;
130130
size_t error_column;
131+
const char* error_src;
131132

132133
// report imported files
133134
char** included_files;
@@ -256,6 +257,7 @@ extern "C" {
256257
c_ctx->error_file = sass_strdup(e.pstate.path.c_str());
257258
c_ctx->error_line = e.pstate.line+1;
258259
c_ctx->error_column = e.pstate.column+1;
260+
c_ctx->error_src = e.pstate.src;
259261
c_ctx->output_string = 0;
260262
c_ctx->source_map_string = 0;
261263
json_delete(json_err);
@@ -406,6 +408,7 @@ extern "C" {
406408
c_ctx->error_message = 0;
407409
c_ctx->error_status = 0;
408410
// reset error position
411+
c_ctx->error_src = 0;
409412
c_ctx->error_file = 0;
410413
c_ctx->error_line = string::npos;
411414
c_ctx->error_column = string::npos;
@@ -757,6 +760,7 @@ extern "C" {
757760
IMPLEMENT_SASS_CONTEXT_GETTER(const char*, error_file);
758761
IMPLEMENT_SASS_CONTEXT_GETTER(size_t, error_line);
759762
IMPLEMENT_SASS_CONTEXT_GETTER(size_t, error_column);
763+
IMPLEMENT_SASS_CONTEXT_GETTER(const char*, error_src);
760764
IMPLEMENT_SASS_CONTEXT_GETTER(const char*, output_string);
761765
IMPLEMENT_SASS_CONTEXT_GETTER(const char*, source_map_string);
762766
IMPLEMENT_SASS_CONTEXT_GETTER(char**, included_files);

sass_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ ADDAPI const char* ADDCALL sass_context_get_error_json (struct Sass_Context* ctx
104104
ADDAPI const char* ADDCALL sass_context_get_error_text (struct Sass_Context* ctx);
105105
ADDAPI const char* ADDCALL sass_context_get_error_message (struct Sass_Context* ctx);
106106
ADDAPI const char* ADDCALL sass_context_get_error_file (struct Sass_Context* ctx);
107+
ADDAPI const char* ADDCALL sass_context_get_error_src (struct Sass_Context* ctx);
107108
ADDAPI size_t ADDCALL sass_context_get_error_line (struct Sass_Context* ctx);
108109
ADDAPI size_t ADDCALL sass_context_get_error_column (struct Sass_Context* ctx);
109110
ADDAPI const char* ADDCALL sass_context_get_source_map_string (struct Sass_Context* ctx);

source_map.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ namespace Sass {
175175
mappings[i].generated_position.file == pstate.file &&
176176
mappings[i].generated_position.line == pstate.line &&
177177
mappings[i].generated_position.column == pstate.column
178-
) return ParserState(pstate.path, mappings[i].original_position, pstate.offset);
178+
) return ParserState(pstate.path, pstate.src, mappings[i].original_position, pstate.offset);
179179
}
180-
return ParserState(pstate.path, Position(-1, -1, -1), Offset(0, 0));
180+
return ParserState(pstate.path, pstate.src, Position(-1, -1, -1), Offset(0, 0));
181181

182182
}
183183

0 commit comments

Comments
 (0)