Initial commit
This commit is contained in:
66
Parser/Lexer.h
Normal file
66
Parser/Lexer.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#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);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user