115 return !(*
this == s);
147 static const u32 END_OF_STREAM = 0xFFFFFFFF;
157 TokenStream(
const std::vector<T>& increase_level_tokens = {
"("},
const std::vector<T>& decrease_level_tokens = {
")"})
160 m_increase_level_tokens = increase_level_tokens;
161 m_decrease_level_tokens = decrease_level_tokens;
173 TokenStream(
const std::vector<
Token<T>>& init,
const std::vector<T>& increase_level_tokens = {
"("},
const std::vector<T>& decrease_level_tokens = {
")"})
174 : TokenStream(increase_level_tokens, decrease_level_tokens)
187 m_data = other.m_data;
188 m_increase_level_tokens = other.m_increase_level_tokens;
189 m_decrease_level_tokens = other.m_decrease_level_tokens;
201 m_data = other.m_data;
202 m_increase_level_tokens = other.m_increase_level_tokens;
203 m_decrease_level_tokens = other.m_decrease_level_tokens;
219 return at(m_pos - 1);
230 bool consume(
const T& expected,
bool throw_on_error =
false)
236 throw TokenStreamException({
"expected Token '" + expected +
"' but reached the end of the stream", get_current_line_number()});
241 if (at(m_pos) != expected)
245 throw TokenStreamException({
"expected Token '" + expected +
"' but got '" + at(m_pos).string +
"'", get_current_line_number()});
268 auto found = find_next(expected, end, level_aware);
269 if (found > size() && throw_on_error)
271 throw TokenStreamException({
"expected Token '" + expected +
"' not found", get_current_line_number()});
273 m_pos = std::min(size(), found);
274 return at(m_pos - 1);
284 u32 line = at(m_pos).number;
287 while (++i < size() && at(i).number == line)
292 return at(m_pos - 1);
310 auto found = find_next(expected, end, level_aware);
311 if (found > size() && throw_on_error)
313 throw TokenStreamException({
"expected Token '" + expected +
"' not found", get_current_line_number()});
315 auto end_pos = std::min(size(), found);
316 TokenStream res(m_increase_level_tokens, m_decrease_level_tokens);
317 res.m_data.reserve(res.m_data.size() + (end_pos - m_pos));
318 res.m_data.insert(res.m_data.begin(), m_data.begin() + m_pos, m_data.begin() + end_pos);
338 Token<T> join_until(
const T& match,
const T& joiner,
u32 end = END_OF_STREAM,
bool level_aware =
true,
bool throw_on_error =
false)
340 u32 start_line = get_current_line_number();
341 auto found = find_next(match, end, level_aware);
342 if (found > size() && throw_on_error)
346 auto end_pos = std::min(size(), found);
348 while (m_pos < end_pos && remaining() > 0)
356 return {start_line, result};
369 u32 start_line = get_current_line_number();
371 while (remaining() > 0)
379 return {start_line, result};
394 return at(m_pos + offset);
402 return at(m_pos + offset);
417 if (position >= m_data.size())
421 return m_data[position];
429 if (position >= m_data.size())
433 return m_data[position];
449 for (
u32 i = m_pos; i < size() && i < end; ++i)
451 const auto&
Token = at(i);
452 if ((!level_aware || level == 0) &&
Token == match)
456 else if (level_aware && std::find_if(m_increase_level_tokens.begin(), m_increase_level_tokens.end(), [&
Token](
const auto& x) { return Token == x; }) != m_increase_level_tokens.end())
460 else if (level_aware && level > 0
461 && std::find_if(m_decrease_level_tokens.begin(), m_decrease_level_tokens.end(), [&
Token](
const auto& x) { return Token == x; }) != m_decrease_level_tokens.end())
476 return m_data.size();
496 return size() - consumed();
520 std::vector<T> m_increase_level_tokens;
521 std::vector<T> m_decrease_level_tokens;
522 std::vector<Token<T>> m_data;
525 u32 get_current_line_number()
const
527 if (m_pos < m_data.size())
529 return m_data[m_pos].number;
531 else if (!m_data.empty())
533 return m_data.back().number;
535 return END_OF_STREAM;
Token< T > consume_until(const T &expected, u32 end=END_OF_STREAM, bool level_aware=true, bool throw_on_error=false)
TokenStream(const std::vector< T > &increase_level_tokens={"("}, const std::vector< T > &decrease_level_tokens={")"})
Token< T > consume_current_line()
Token< T > & at(u32 position)
Token< T > & peek(i32 offset=0)
TokenStream(const TokenStream< T > &other)
u32 find_next(const T &match, u32 end=END_OF_STREAM, bool level_aware=true) const
const Token< T > & peek(i32 offset=0) const
Token< T > join_until(const T &match, const T &joiner, u32 end=END_OF_STREAM, bool level_aware=true, bool throw_on_error=false)
bool consume(const T &expected, bool throw_on_error=false)
TokenStream(const std::vector< Token< T >> &init, const std::vector< T > &increase_level_tokens={"("}, const std::vector< T > &decrease_level_tokens={")"})
TokenStream< T > & operator=(const TokenStream< T > &other)
TokenStream< T > extract_until(const T &expected, u32 end=END_OF_STREAM, bool level_aware=true, bool throw_on_error=false)
Token< T > consume(u32 num=1)
const Token< T > & at(u32 position) const
Token< T > join(const T &joiner)
bool operator!=(const T &s) const
Token< T > & operator=(const T &s)
bool operator==(const T &s) const
Token< T > & operator+=(const T &s)