forked from sass/libsass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktrace.hpp
77 lines (60 loc) · 1.5 KB
/
backtrace.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef SASS_BACKTRACE_H
#define SASS_BACKTRACE_H
#include <sstream>
#include "file.hpp"
#include "position.hpp"
namespace Sass {
using namespace std;
struct Backtrace {
Backtrace* parent;
ParserState pstate;
string caller;
Backtrace(Backtrace* prn, ParserState pstate, string c)
: parent(prn),
pstate(pstate),
caller(c)
{ }
string to_string(bool warning = false)
{
size_t i = -1;
stringstream ss;
string cwd(Sass::File::get_cwd());
Backtrace* this_point = this;
if (!warning) ss << endl << "Backtrace:";
// the first tracepoint (which is parent-less) is an empty placeholder
while (this_point->parent) {
// make path relative to the current directory
string rel_path(Sass::File::resolve_relative_path(this_point->pstate.path, cwd, cwd));
if (warning) {
ss << endl
<< "\t"
<< (++i == 0 ? "on" : "from")
<< " line "
<< this_point->pstate.line
<< " of "
<< rel_path;
} else {
ss << endl
<< "\t"
<< rel_path
<< ":"
<< this_point->pstate.line
<< this_point->parent->caller;
}
this_point = this_point->parent;
}
return ss.str();
}
size_t depth()
{
size_t d = 0;
Backtrace* p = parent;
while (p) {
++d;
p = p->parent;
}
return d-1;
}
};
}
#endif