Files
Training-Coding-Interview/Parser/Lexer.h
2025-10-15 17:20:56 +02:00

66 lines
1.4 KiB
C++

#pragma once
#include <list>
#include <string_view>
#include <ranges>
#include <sys/types.h>
#include <string>
namespace Parser
{
class Lexer
{
public:
struct Token
{
enum TokenType
{
INT,
PLUS,
MINUS,
MULTIPLY,
DIVIDE,
ASSIGNMENT,
BRACKET_LEFT,
BRACKET_RIGHT,
IDENTIFIER
};
TokenType type;
uint line;
std::string_view occurence;
Token(TokenType type, uint line, std::string_view&& str)
{
this->type = type;
this->line = line;
occurence = str;
}
std::string toString(){
return std::to_string(type) + " " + std::string(occurence);
}
};
Lexer() = default;
std::list<Lexer::Token> tokenize(const std::string_view &text);
private:
uint current{};
uint start{};
uint line{};
std::string_view text;
std::list<Token> tokens;
private:
void identifier();
bool isAlpha(const char& c);
bool isDigit(const char& c);
void number();
void advance();
char peek();
void token(const Token::TokenType type);
};
}