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 | 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 | : prefix(0), begin(0), end(0) { } |
81 | Token(const char* b, const char* e) |
82 | : prefix(b), begin(b), end(e) { } |
83 | Token(const char* str) |
84 | : prefix(str), begin(str), end(str + strlen(s: str)) { } |
85 | Token(const char* p, const char* b, const char* e) |
86 | : prefix(p), begin(b), end(e) { } |
87 | |
88 | size_t length() const { return end - begin; } |
89 | sass::string ws_before() const { return sass::string(prefix, begin); } |
90 | sass::string to_string() const { return sass::string(begin, end); } |
91 | sass::string time_wspace() const { |
92 | sass::string str(to_string()); |
93 | sass::string whitespaces(" \t\f\v\n\r" ); |
94 | return str.erase(pos: str.find_last_not_of(str: whitespaces)+1); |
95 | } |
96 | |
97 | operator bool() { return begin && end && begin >= end; } |
98 | operator sass::string() { return to_string(); } |
99 | |
100 | 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 | const char* getPath() const { |
114 | return source->getPath(); |
115 | } |
116 | |
117 | const char* getRawData() const { |
118 | return source->getRawData(); |
119 | } |
120 | |
121 | Offset getPosition() const { |
122 | return position; |
123 | } |
124 | |
125 | size_t getLine() const { |
126 | return position.line + 1; |
127 | } |
128 | |
129 | size_t getColumn() const { |
130 | return position.column + 1; |
131 | } |
132 | |
133 | size_t getSrcId() const { |
134 | return source == nullptr |
135 | ? std::string::npos |
136 | : source->getSrcId(); |
137 | } |
138 | |
139 | SourceDataObj source; |
140 | Offset position; |
141 | Offset offset; |
142 | |
143 | }; |
144 | |
145 | } |
146 | |
147 | #endif |
148 | |