Skip to content

Commit fdee463

Browse files
committed
Minor re-factoring on ParserState and Token
Remove obsolete code, arguments and members
1 parent b44b06a commit fdee463

11 files changed

+81
-51
lines changed

error_handling.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ namespace Sass {
1313

1414
void error(string msg, ParserState pstate, Backtrace* bt)
1515
{
16-
if (!pstate.path.empty() && Prelexer::quoted_string(pstate.path.c_str()))
17-
pstate.path = pstate.path.substr(1, pstate.path.size() - 1);
1816

1917
Backtrace top(bt, pstate, "");
2018
msg += top.to_string();

error_handling.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace Sass {
1414
enum Type { read, write, syntax, evaluation };
1515

1616
Type type;
17-
string path;
1817
ParserState pstate;
1918
string message;
2019

eval.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ namespace Sass {
671671
case Textual::DIMENSION:
672672
result = new (ctx.mem) Number(t->pstate(),
673673
sass_atof(num.c_str()),
674-
Token(number(text.c_str()), t->pstate()),
674+
Token(number(text.c_str())),
675675
zero);
676676
break;
677677
case Textual::HEX: {

parser.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,10 @@ namespace Sass {
505505
}
506506
if (peek_newline()) ref_wrap->has_line_break(true);
507507
}
508-
while (peek< sequence< optional_css_whitespace, optional < block_comment >, exactly<','> > >())
508+
while (peek< sequence < optional_css_whitespace, optional < block_comment >, exactly<','> > >())
509509
{
510510
// consume everything up and including the comma speparator
511-
reloop = lex< sequence< optional_css_comments, exactly<','> > >() != 0;
511+
reloop = lex< sequence < optional_css_comments, exactly<','> > >() != 0;
512512
// remember line break (also between some commas)
513513
if (peek_newline()) comb->has_line_feed(true);
514514
if (comb->tail() && peek_newline()) comb->tail()->has_line_feed(true);
@@ -1114,12 +1114,13 @@ namespace Sass {
11141114
{
11151115
Expression* expr1 = parse_expression();
11161116
// if it's a singleton, return it directly; don't wrap it
1117-
if (!(peek< alternatives < eq_op,
1118-
neq_op,
1119-
gte_op,
1120-
gt_op,
1121-
lte_op,
1122-
lt_op
1117+
if (!(peek< alternatives <
1118+
eq_op,
1119+
neq_op,
1120+
gte_op,
1121+
gt_op,
1122+
lte_op,
1123+
lt_op
11231124
> >(position)))
11241125
{ return expr1; }
11251126

@@ -1363,7 +1364,7 @@ namespace Sass {
13631364
const char* j = skip_over_scopes< exactly<hash_lbrace>, exactly<rbrace> >(p + 2, chunk.end); // find the closing brace
13641365
if (j) { --j;
13651366
// parse the interpolant and accumulate it
1366-
Expression* interp_node = Parser::from_token(Token(p+2, j, before_token), ctx, pstate).parse_list();
1367+
Expression* interp_node = Parser::from_token(Token(p+2, j), ctx, pstate).parse_list();
13671368
interp_node->is_interpolant(true);
13681369
(*schema) << interp_node;
13691370
i = j;
@@ -1424,7 +1425,7 @@ namespace Sass {
14241425
const char* j = skip_over_scopes< exactly<hash_lbrace>, exactly<rbrace> >(p+2, str.end); // find the closing brace
14251426
if (j) {
14261427
// parse the interpolant and accumulate it
1427-
Expression* interp_node = Parser::from_token(Token(p+2, j, before_token), ctx, pstate).parse_list();
1428+
Expression* interp_node = Parser::from_token(Token(p+2, j), ctx, pstate).parse_list();
14281429
interp_node->is_interpolant(true);
14291430
(*schema) << interp_node;
14301431
i = j;
@@ -1467,7 +1468,7 @@ namespace Sass {
14671468
size_t num_items = 0;
14681469
while (position < stop) {
14691470
if (lex< interpolant >()) {
1470-
Token insides(Token(lexed.begin + 2, lexed.end - 1, before_token));
1471+
Token insides(Token(lexed.begin + 2, lexed.end - 1));
14711472
Expression* interp_node = Parser::from_token(insides, ctx, pstate).parse_list();
14721473
interp_node->is_interpolant(true);
14731474
(*schema) << interp_node;
@@ -1561,7 +1562,7 @@ namespace Sass {
15611562
const char* j = skip_over_scopes< exactly<hash_lbrace>, exactly<rbrace> >(p+2, id.end); // find the closing brace
15621563
if (j) {
15631564
// parse the interpolant and accumulate it
1564-
Expression* interp_node = Parser::from_token(Token(p+2, j, before_token), ctx, pstate).parse_list();
1565+
Expression* interp_node = Parser::from_token(Token(p+2, j), ctx, pstate).parse_list();
15651566
interp_node->is_interpolant(true);
15661567
(*schema) << interp_node;
15671568
schema->has_interpolants(true);
@@ -1592,7 +1593,7 @@ namespace Sass {
15921593
const char* arg_end = position;
15931594
lex< exactly<')'> >();
15941595

1595-
Argument* arg = new (ctx.mem) Argument(arg_pos, parse_interpolated_chunk(Token(arg_beg, arg_end, before_token)));
1596+
Argument* arg = new (ctx.mem) Argument(arg_pos, parse_interpolated_chunk(Token(arg_beg, arg_end)));
15961597
Arguments* args = new (ctx.mem) Arguments(arg_pos);
15971598
*args << arg;
15981599
return new (ctx.mem) Function_Call(call_pos, name, args);
@@ -1895,7 +1896,7 @@ namespace Sass {
18951896
if (!peek< alternatives< with_directive, without_directive > >()) {
18961897
const char* i = position;
18971898
const char* p = peek< until<')'> >(i);
1898-
Token* t = new Token(i, p, Position(0, 0));
1899+
Token* t = new Token(i, p);
18991900
error("Invalid CSS after \"(\": expected \"with\" or \"without\", was \""+t->to_string()+"\"", pstate);
19001901
}
19011902

parser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ namespace Sass {
197197
after_token = after_token + size;
198198

199199
// create parsed token string (public member)
200-
lexed = Token(wspace_start, it_before_token, it_after_token, optional_css_whitespace(it_after_token) ? optional_css_whitespace(it_after_token) : it_after_token, before_token);
200+
lexed = Token(wspace_start, it_before_token, it_after_token);
201201
Position pos(before_token.file, before_token.line, before_token.column);
202202
pstate = ParserState(path, lexed, pos, size);
203203

position.cpp

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,40 @@ namespace Sass {
1919
Offset::Offset(const size_t line, const size_t column)
2020
: line(line), column(column) { }
2121

22+
// init/create instance from const char substring
23+
Offset Offset::init(const char* beg, const char* end)
24+
{
25+
Offset offset(0, 0);
26+
if (end == 0) {
27+
end += strlen(beg);
28+
}
29+
offset.add(beg, end);
30+
return offset;
31+
}
32+
2233
// increase offset by given string (mostly called by lexer)
2334
// increase line counter and count columns on the last line
24-
Offset Offset::inc(const char* begin, const char* end) const
35+
// ToDo: make the col count utf8 aware
36+
void Offset::add(const char* begin, const char* end)
2537
{
26-
Offset offset(line, column);
2738
while (begin < end && *begin) {
2839
if (*begin == '\n') {
29-
++ offset.line;
30-
offset.column = 0;
40+
++ line;
41+
// start new line
42+
column = 0;
3143
} else {
32-
++ offset.column;
44+
++ column;
3345
}
3446
++begin;
3547
}
48+
}
49+
50+
// increase offset by given string (mostly called by lexer)
51+
// increase line counter and count columns on the last line
52+
Offset Offset::inc(const char* begin, const char* end) const
53+
{
54+
Offset offset(line, column);
55+
offset.add(begin, end);
3656
return offset;
3757
}
3858

@@ -48,12 +68,17 @@ namespace Sass {
4868

4969
void Offset::operator+= (const Offset &off)
5070
{
51-
*this = Offset(line + off.line, off.line > 0 ? off.column : off.column + column);
71+
*this = Offset(line + off.line, off.line > 0 ? off.column : column + off.column);
5272
}
5373

5474
Offset Offset::operator+ (const Offset &off) const
5575
{
56-
return Offset(line + off.line, off.line > 0 ? off.column : off.column + column);
76+
return Offset(line + off.line, off.line > 0 ? off.column : column + off.column);
77+
}
78+
79+
Offset Offset::operator- (const Offset &off) const
80+
{
81+
return Offset(line - off.line, off.line == line ? column - off.column : column);
5782
}
5883

5984
Position::Position(const size_t file)
@@ -69,9 +94,6 @@ namespace Sass {
6994
: Offset(line, column), file(file) { }
7095

7196

72-
ParserState::ParserState(string path)
73-
: Position(-1, 0, 0), path(path), offset(0, 0), token() { }
74-
7597
ParserState::ParserState(string path, const size_t file)
7698
: Position(file, 0, 0), path(path), offset(0, 0), token() { }
7799

@@ -81,6 +103,11 @@ namespace Sass {
81103
ParserState::ParserState(string path, Token token, Position position, Offset offset)
82104
: Position(position), path(path), offset(offset), token(token) { }
83105

106+
void Position::add(const char* begin, const char* end)
107+
{
108+
Offset::add(begin, end);
109+
}
110+
84111
Position Position::inc(const char* begin, const char* end) const
85112
{
86113
Offset offset(line, column);
@@ -100,12 +127,17 @@ namespace Sass {
100127

101128
void Position::operator+= (const Offset &off)
102129
{
103-
*this = Position(file, line + off.line, off.line > 0 ? off.column : off.column + column);
130+
*this = Position(file, line + off.line, off.line > 0 ? off.column : column + off.column);
104131
}
105132

106133
const Position Position::operator+ (const Offset &off) const
107134
{
108-
return Position(file, line + off.line, off.line > 0 ? off.column : off.column + column);
135+
return Position(file, line + off.line, off.line > 0 ? off.column : column + off.column);
136+
}
137+
138+
const Offset Position::operator- (const Offset &off) const
139+
{
140+
return Offset(line - off.line, off.line == line ? column - off.column : column);
109141
}
110142

111143
/* not used anymore - remove?

position.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ namespace Sass {
1919
Offset(const size_t line, const size_t column);
2020

2121
// return new position, incremented by the given string
22+
void add(const char* begin, const char* end);
2223
Offset inc(const char* begin, const char* end) const;
2324

25+
// init/create instance from const char substring
26+
static Offset init(const char* beg, const char* end);
27+
2428
public: // overload operators for position
2529
void operator+= (const Offset &pos);
2630
bool operator== (const Offset &pos) const;
2731
bool operator!= (const Offset &pos) const;
2832
Offset operator+ (const Offset &off) const;
33+
Offset operator- (const Offset &off) const;
2934

3035
public: // overload output stream operator
3136
// friend ostream& operator<<(ostream& strm, const Offset& off);
@@ -52,7 +57,9 @@ namespace Sass {
5257
bool operator== (const Position &pos) const;
5358
bool operator!= (const Position &pos) const;
5459
const Position operator+ (const Offset &off) const;
60+
const Offset operator- (const Offset &off) const;
5561
// return new position, incremented by the given string
62+
void add(const char* begin, const char* end);
5663
Position inc(const char* begin, const char* end) const;
5764

5865
public: // overload output stream operator
@@ -69,25 +76,19 @@ namespace Sass {
6976
const char* prefix;
7077
const char* begin;
7178
const char* end;
72-
const char* suffix;
73-
Position start;
74-
Position stop;
7579

7680
Token()
77-
: prefix(0), begin(0), end(0), suffix(0), start(0), stop(0) { }
78-
Token(const char* b, const char* e, const Position pos)
79-
: prefix(b), begin(b), end(e), suffix(e), start(pos), stop(pos.inc(b, e)) { }
80-
Token(const char* s, const Position pos)
81-
: prefix(s), begin(s), end(s + strlen(s)), suffix(end), start(pos), stop(pos.inc(s, s + strlen(s))) { }
82-
Token(const char* p, const char* b, const char* e, const char* s, const Position pos)
83-
: prefix(p), begin(b), end(e), suffix(s), start(pos), stop(pos.inc(b, e)) { }
81+
: prefix(0), begin(0), end(0) { }
82+
Token(const char* b, const char* e)
83+
: prefix(b), begin(b), end(e) { }
84+
Token(const char* str)
85+
: prefix(str), begin(str), end(str + strlen(str)) { }
86+
Token(const char* p, const char* b, const char* e)
87+
: prefix(p), begin(b), end(e) { }
8488

8589
size_t length() const { return end - begin; }
8690
string ws_before() const { return string(prefix, begin); }
8791
string to_string() const { return string(begin, end); }
88-
string ws_after() const { return string(end, suffix); }
89-
90-
// string unquote() const;
9192

9293
operator bool() { return begin && end && begin >= end; }
9394
operator string() { return to_string(); }
@@ -98,8 +99,7 @@ namespace Sass {
9899
class ParserState : public Position {
99100

100101
public: // c-tor
101-
ParserState(string path);
102-
ParserState(string path, const size_t file);
102+
ParserState(string path, const size_t file = string::npos);
103103
ParserState(string path, Position position, Offset offset = Offset(0, 0));
104104
ParserState(string path, Token token, Position position, Offset offset = Offset(0, 0));
105105

prelexer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ namespace Sass {
734734
const char* static_string(const char* src) {
735735
const char* pos = src;
736736
const char * s = quoted_string(pos);
737-
Token t(pos, s, Position(0, 0));
737+
Token t(pos, s);
738738
const unsigned int p = count_interval< interpolant >(t.begin, t.end);
739739
return (p == 0) ? t.end : 0;
740740
}

sass_context.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ extern "C" {
407407
c_ctx->error_status = 0;
408408
// reset error position
409409
c_ctx->error_file = 0;
410-
c_ctx->error_line = -1;
411-
c_ctx->error_column = -1;
410+
c_ctx->error_line = string::npos;
411+
c_ctx->error_column = string::npos;
412412

413413
// use to parse block
414414
return cpp_ctx;

sass_interface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ extern "C" {
236236
}
237237
catch (Sass_Error& e) {
238238
stringstream msg_stream;
239-
msg_stream << e.path << ":" << e.pstate.line << ": " << e.message << endl;
239+
msg_stream << e.pstate.path << ":" << e.pstate.line << ": " << e.message << endl;
240240
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
241241
c_ctx->error_status = 1;
242242
c_ctx->output_string = 0;

0 commit comments

Comments
 (0)