ClickHouse/src/Parsers/TokenIterator.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
2.1 KiB
C++
Raw Normal View History

#pragma once
2021-11-29 09:47:21 +00:00
#include <Core/Defines.h>
#include <Parsers/Lexer.h>
2022-10-17 16:39:42 +00:00
#include <cassert>
2021-11-29 09:47:21 +00:00
#include <vector>
namespace DB
{
/** Parser operates on lazy stream of tokens.
* It could do lookaheads of any depth.
*/
/** Used as an input for parsers.
* All whitespace and comment tokens are transparently skipped.
*/
class Tokens
{
private:
std::vector<Token> data;
2022-10-17 10:06:28 +00:00
std::size_t last_accessed_index = 0;
public:
2023-05-25 14:05:44 +00:00
Tokens(const char * begin, const char * end, size_t max_query_size = 0, bool skip_insignificant = true);
2022-10-17 10:06:28 +00:00
ALWAYS_INLINE inline const Token & operator[](size_t index)
{
2022-10-17 16:39:42 +00:00
assert(index < data.size());
2022-10-17 10:06:28 +00:00
last_accessed_index = std::max(last_accessed_index, index);
return data[index];
}
2022-10-17 10:06:28 +00:00
ALWAYS_INLINE inline const Token & max() { return data[last_accessed_index]; }
};
/// To represent position in a token stream.
class TokenIterator
{
private:
Tokens * tokens;
size_t index = 0;
public:
2019-08-03 11:02:40 +00:00
explicit TokenIterator(Tokens & tokens_) : tokens(&tokens_) {}
2021-11-29 09:47:21 +00:00
ALWAYS_INLINE const Token & get() { return (*tokens)[index]; }
ALWAYS_INLINE const Token & operator*() { return get(); }
ALWAYS_INLINE const Token * operator->() { return &get(); }
2021-11-29 09:47:21 +00:00
ALWAYS_INLINE TokenIterator & operator++()
{
++index;
return *this;
}
ALWAYS_INLINE TokenIterator & operator--()
{
--index;
return *this;
}
2021-11-29 09:47:21 +00:00
ALWAYS_INLINE bool operator<(const TokenIterator & rhs) const { return index < rhs.index; }
ALWAYS_INLINE bool operator<=(const TokenIterator & rhs) const { return index <= rhs.index; }
ALWAYS_INLINE bool operator==(const TokenIterator & rhs) const { return index == rhs.index; }
ALWAYS_INLINE bool operator!=(const TokenIterator & rhs) const { return index != rhs.index; }
2021-11-29 09:47:21 +00:00
ALWAYS_INLINE bool isValid() { return get().type < TokenType::EndOfStream; }
/// Rightmost token we had looked.
2021-11-29 09:47:21 +00:00
ALWAYS_INLINE const Token & max() { return tokens->max(); }
};
/// Returns positions of unmatched parentheses.
using UnmatchedParentheses = std::vector<Token>;
2021-01-31 23:10:41 +00:00
UnmatchedParentheses checkUnmatchedParentheses(TokenIterator begin);
}