Debian Codegraph @

inst/include/sourcetools/cursor/TokenCursor.h

raw
1 #ifndef SOURCETOOLS_CURSOR_TOKEN_CURSOR_H
2 #define SOURCETOOLS_CURSOR_TOKEN_CURSOR_H
3
4 #include <cstring>
5 #include <algorithm>
6
7 #include <sourcetools/collection/Position.h>
8 #include <sourcetools/tokenization/Token.h>
9
10 namespace sourcetools {
11 namespace cursors {
12
13 class TokenCursor {
14
15 private:
16 typedef collections::Position Position;
17 typedef tokens::Token Token;
18
19 public:
20
21 TokenCursor(const std::vector<Token>& tokens)
22 : tokens_(tokens),
23 offset_(0),
24 n_(tokens.size()),
25 noSuchToken_(tokens::END)
26 {}
27
28 bool moveToNextToken()
29 {
30 if (UNLIKELY(offset_ >= n_ - 1))
31 return false;
32
33 ++offset_;
34 return true;
35 }
36
37 bool moveToNextSignificantToken()
38 {
39 if (!moveToNextToken())
40 return false;
41
42 if (!fwdOverWhitespaceAndComments())
43 return false;
44
45 return true;
46 }
47
48 bool moveToPreviousToken()
49 {
50 if (UNLIKELY(offset_ == 0))
51 return false;
52
53 --offset_;
54 return true;
55 }
56
57 bool moveToPreviousSignificantToken()
58 {
59 if (!moveToPreviousToken())
60 return false;
61
62 if (!bwdOverWhitespaceAndComments())
63 return false;
64
65 return true;
66 }
67
68 const Token& peekFwd(std::size_t offset = 1) const
69 {
70 std::size_t index = offset_ + offset;
71 if (UNLIKELY(index >= n_))
72 return noSuchToken_;
73
74 return tokens_[index];
75 }
76
77 const Token& peekBwd(std::size_t offset = 1) const
78 {
79 if (UNLIKELY(offset > offset_))
80 return noSuchToken_;
81
82 std::size_t index = offset_ - offset;
83 return tokens_[index];
84 }
85
86 const Token& currentToken() const
87 {
88 if (UNLIKELY(offset_ >= n_))
89 return noSuchToken_;
90 return tokens_[offset_];
91 }
92
93 operator const Token&() const { return currentToken(); }
94
95 bool fwdOverWhitespace()
96 {
97 while (isType(tokens::WHITESPACE))
98 if (!moveToNextToken())
99 return false;
100 return true;
101 }
102
103 bool bwdOverWhitespace()
104 {
105 while (isType(tokens::WHITESPACE))
106 if (!moveToPreviousToken())
107 return false;
108 return true;
109 }
110
111 bool fwdOverComments()
112 {
113 while (isType(tokens::COMMENT))
114 if (!moveToNextToken())
115 return false;
116 return true;
117 }
118
119 bool bwdOverComments()
120 {
121 while (isType(tokens::COMMENT))
122 if (!moveToPreviousToken())
123 return false;
124 return true;
125 }
126
127 bool fwdOverWhitespaceAndComments()
128 {
129 while (isType(tokens::COMMENT) || isType(tokens::WHITESPACE))
130 if (!moveToNextToken())
131 return false;
132 return true;
133 }
134
135 bool bwdOverWhitespaceAndComments()
136 {
137 while (isType(tokens::COMMENT) || isType(tokens::WHITESPACE))
138 if (!moveToPreviousToken())
139 return false;
140 return true;
141 }
142
143 const Token& nextSignificantToken(std::size_t times = 1) const
144 {
145 TokenCursor clone(*this);
146 for (std::size_t i = 0; i < times; ++i)
147 clone.moveToNextSignificantToken();
148 return clone;
149 }
150
151 const Token& previousSignificantToken(std::size_t times = 1) const
152 {
153 TokenCursor clone(*this);
154 for (std::size_t i = 0; i < times; ++i)
155 clone.moveToPreviousSignificantToken();
156 return clone;
157 }
158
159 bool moveToPosition(std::size_t row, std::size_t column)
160 {
161 return moveToPosition(Position(row, column));
162 }
163
164 bool moveToPosition(const Position& target)
165 {
166 if (UNLIKELY(n_ == 0))
167 return false;
168
169 if (UNLIKELY(tokens_[n_ - 1].position() <= target))
170 {
171 offset_ = n_ - 1;
172 return true;
173 }
174
175 std::size_t start = 0;
176 std::size_t end = n_;
177
178 std::size_t offset = 0;
179 while (true)
180 {
181 offset = (start + end) / 2;
182 const Position& current = tokens_[offset].position();
183
184 if (current == target || start == end)
185 break;
186 else if (current < target)
187 start = offset + 1;
188 else
189 end = offset - 1;
190 }
191
192 offset_ = offset;
193 return true;
194 }
195
196 template <typename F>
197 bool findFwd(F f)
198 {
199 do {
200 if (f(this))
201 return true;
202 } while (moveToNextToken());
203
204 return false;
205 }
206
207 template <typename F>
208 bool findBwd(F f)
209 {
210 do {
211 if (f(this))
212 return true;
213 } while (moveToPreviousToken());
214
215 return false;
216 }
217
218 bool findFwd(const char* contents)
219 {
220 return findFwd(std::string(contents, std::strlen(contents)));
221 }
222
223 bool findFwd(const std::string& contents)
224 {
225 do {
226 if (currentToken().contentsEqual(contents))
227 return true;
228 } while (moveToNextToken());
229
230 return false;
231 }
232
233 bool findBwd(const char* contents)
234 {
235 return findBwd(std::string(contents, std::strlen(contents)));
236 }
237
238 bool findBwd(const std::string& contents)
239 {
240 do {
241 if (currentToken().contentsEqual(contents))
242 return true;
243 } while (moveToPreviousToken());
244
245 return false;
246 }
247
248 bool fwdToMatchingBracket()
249 {
250 using namespace tokens;
251 if (!isLeftBracket(currentToken()))
252 return false;
253
254 TokenType lhs = currentToken().type();
255 TokenType rhs = complement(lhs);
256 std::size_t balance = 1;
257
258 while (moveToNextSignificantToken())
259 {
260 TokenType type = currentToken().type();
261 balance += type == lhs;
262 balance -= type == rhs;
263 if (balance == 0) return true;
264 }
265
266 return false;
267 }
268
269 bool bwdToMatchingBracket()
270 {
271 using namespace tokens;
272 if (!isRightBracket(currentToken()))
273 return false;
274
275 TokenType lhs = currentToken().type();
276 TokenType rhs = complement(lhs);
277 std::size_t balance = 1;
278
279 while (moveToPreviousSignificantToken())
280 {
281 TokenType type = currentToken().type();
282 balance += type == lhs;
283 balance -= type == rhs;
284 if (balance == 0) return true;
285 }
286
287 return false;
288 }
289
290 friend std::ostream& operator<<(std::ostream& os, const TokenCursor& cursor)
291 {
292 return os << toString(cursor.currentToken());
293 }
294
295 tokens::TokenType type() const { return currentToken().type(); }
296 bool isType(tokens::TokenType type) const { return currentToken().isType(type); }
297 collections::Position position() const { return currentToken().position(); }
298 std::size_t offset() const { return offset_; }
299 std::size_t row() const { return currentToken().row(); }
300 std::size_t column() const { return currentToken().column(); }
301
302
303 private:
304
305 const std::vector<Token>& tokens_;
306 std::size_t offset_;
307 std::size_t n_;
308 Token noSuchToken_;
309
310 };
311
312 } // namespace cursors
313
314 inline std::string toString(const cursors::TokenCursor& cursor)
315 {
316 return toString(cursor.currentToken());
317 }
318
319 } // namespace sourcetools
320
321 #endif /* SOURCETOOLS_CURSOR_TOKEN_CURSOR_H */