/src/libsass/src/position.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef SASS_POSITION_H |
2 | | #define SASS_POSITION_H |
3 | | |
4 | | #include <string> |
5 | | #include <cstring> |
6 | | #include "source_data.hpp" |
7 | | #include "ast_fwd_decl.hpp" |
8 | | |
9 | | namespace Sass { |
10 | | |
11 | | |
12 | | class Offset { |
13 | | |
14 | | public: // c-tor |
15 | | Offset(const char chr); |
16 | | Offset(const char* string); |
17 | | Offset(const sass::string& text); |
18 | | Offset(const size_t line, const size_t column); |
19 | | |
20 | | // return new position, incremented by the given string |
21 | | Offset add(const char* begin, const char* end); |
22 | | Offset inc(const char* begin, const char* end) const; |
23 | | |
24 | | // init/create instance from const char substring |
25 | | static Offset init(const char* beg, const char* end); |
26 | | |
27 | | public: // overload operators for position |
28 | | void operator+= (const Offset &pos); |
29 | | bool operator== (const Offset &pos) const; |
30 | | bool operator!= (const Offset &pos) const; |
31 | | Offset operator+ (const Offset &off) const; |
32 | | Offset operator- (const Offset &off) const; |
33 | | |
34 | | public: // overload output stream operator |
35 | | // friend std::ostream& operator<<(std::ostream& strm, const Offset& off); |
36 | | |
37 | | public: |
38 | 0 | Offset off() { return *this; } |
39 | | |
40 | | public: |
41 | | size_t line; |
42 | | size_t column; |
43 | | |
44 | | }; |
45 | | |
46 | | class Position : public Offset { |
47 | | |
48 | | public: // c-tor |
49 | | Position(const size_t file); // line(0), column(0) |
50 | | Position(const size_t file, const Offset& offset); |
51 | | Position(const size_t line, const size_t column); // file(-1) |
52 | | Position(const size_t file, const size_t line, const size_t column); |
53 | | |
54 | | public: // overload operators for position |
55 | | void operator+= (const Offset &off); |
56 | | bool operator== (const Position &pos) const; |
57 | | bool operator!= (const Position &pos) const; |
58 | | const Position operator+ (const Offset &off) const; |
59 | | const Offset operator- (const Offset &off) const; |
60 | | // return new position, incremented by the given string |
61 | | Position add(const char* begin, const char* end); |
62 | | Position inc(const char* begin, const char* end) const; |
63 | | |
64 | | public: // overload output stream operator |
65 | | // friend std::ostream& operator<<(std::ostream& strm, const Position& pos); |
66 | | |
67 | | public: |
68 | | size_t file; |
69 | | |
70 | | }; |
71 | | |
72 | | // Token type for representing lexed chunks of text |
73 | | class Token { |
74 | | public: |
75 | | const char* prefix; |
76 | | const char* begin; |
77 | | const char* end; |
78 | | |
79 | | Token() |
80 | 706 | : prefix(0), begin(0), end(0) { } |
81 | | Token(const char* b, const char* e) |
82 | 2.00k | : prefix(b), begin(b), end(e) { } |
83 | | Token(const char* str) |
84 | 3.45k | : prefix(str), begin(str), end(str + strlen(str)) { } |
85 | | Token(const char* p, const char* b, const char* e) |
86 | 467k | : prefix(p), begin(b), end(e) { } |
87 | | |
88 | 0 | size_t length() const { return end - begin; } |
89 | 0 | sass::string ws_before() const { return sass::string(prefix, begin); } |
90 | 362k | sass::string to_string() const { return sass::string(begin, end); } |
91 | 1 | sass::string time_wspace() const { |
92 | 1 | sass::string str(to_string()); |
93 | 1 | sass::string whitespaces(" \t\f\v\n\r"); |
94 | 1 | return str.erase(str.find_last_not_of(whitespaces)+1); |
95 | 1 | } |
96 | | |
97 | 0 | operator bool() { return begin && end && begin >= end; } |
98 | 360k | operator sass::string() { return to_string(); } |
99 | | |
100 | 0 | bool operator==(Token t) { return to_string() == t.to_string(); } |
101 | | }; |
102 | | |
103 | | class SourceSpan { |
104 | | |
105 | | public: |
106 | | |
107 | | SourceSpan(const char* path); |
108 | | |
109 | | SourceSpan(SourceDataObj source, |
110 | | const Offset& position = Offset(0, 0), |
111 | | const Offset& offset = Offset(0, 0)); |
112 | | |
113 | 18.7k | const char* getPath() const { |
114 | 18.7k | return source->getPath(); |
115 | 18.7k | } |
116 | | |
117 | 38 | const char* getRawData() const { |
118 | 38 | return source->getRawData(); |
119 | 38 | } |
120 | | |
121 | 0 | Offset getPosition() const { |
122 | 0 | return position; |
123 | 0 | } |
124 | | |
125 | 6.25k | size_t getLine() const { |
126 | 6.25k | return position.line + 1; |
127 | 6.25k | } |
128 | | |
129 | 6.25k | size_t getColumn() const { |
130 | 6.25k | return position.column + 1; |
131 | 6.25k | } |
132 | | |
133 | 2.57k | size_t getSrcId() const { |
134 | 2.57k | return source == nullptr |
135 | 2.57k | ? std::string::npos |
136 | 2.57k | : source->getSrcId(); |
137 | 2.57k | } |
138 | | |
139 | | SourceDataObj source; |
140 | | Offset position; |
141 | | Offset offset; |
142 | | |
143 | | }; |
144 | | |
145 | | } |
146 | | |
147 | | #endif |