163 Token(TokenType type, std::string lexeme, Location location)
164 : tok_type(type), lexeme(std::move(lexeme)), location(location) {}
165 bool is_comparison() {
166 return tok_type == TokLESS || tok_type == TokGreaterEqual ||
167 tok_type == TokLessEqual || tok_type == TokGREATER ||
168 tok_type == TokEQUAL;
170 bool is_logical() {
return tok_type == TokOR || tok_type == TokAND; }
171 Location get_location()
const {
return this->location; }
179 std::vector<std::shared_ptr<Token>> TokStream;
180 size_t current_index;
184 std::vector<std::shared_ptr<Token>> ErrStream;
186 TokenStream() : TokStream(), current_index(0), error(
false) {}
188 void push_back(
const std::shared_ptr<Token> &token) {
189 if (token->tok_type == TokINVALID) {
191 ErrStream.push_back(token);
193 TokStream.push_back(token);
197 bool hasErrors() {
return error; }
199 void push_back(
const Token &token) {
200 this->push_back(std::make_shared<Token>(token));
203 std::shared_ptr<Token> &exhaust_until(TokenType tokType) {
204 if (tokType == TokenType::TokEOF) {
205 current_index = TokStream.size() - 1;
206 return TokStream.back();
209 if (TokStream[current_index]->tok_type == tokType)
210 return TokStream[current_index++];
215 return TokStream.back();
218 bool isEnd() {
return current_index >= (TokStream.size() - 1); }
219 std::shared_ptr<Token> peek() {
return TokStream[current_index]; };
220 std::shared_ptr<Token> consume() {
222 current_index = std::min(TokStream.size() - 1, current_index + 1);
227 if (!TokStream.empty()) {
228 return TokStream[current_index]->get_location();