Add TokenStream

This commit is contained in:
Sofia 2026-04-01 22:42:27 +03:00
parent ad04da8218
commit e5f7430586
3 changed files with 58 additions and 1 deletions

View File

@ -50,6 +50,9 @@ int main() {
std::cout << token << std::endl; std::cout << token << std::endl;
} }
auto stream = token::TokenStream{ tokens };
stream.expect(token::Type::Eof);
// LLVM Hello World // LLVM Hello World
// llvm_hello_world(); // llvm_hello_world();

View File

@ -14,7 +14,8 @@ static bool iswhitespace(char& character) {
} }
namespace token { namespace token {
std::string Token::name() {
std::string type_name(Type& type) {
switch (type) { switch (type) {
case token::Type::Ident: case token::Type::Ident:
return "Ident"; return "Ident";
@ -29,16 +30,51 @@ namespace token {
case token::Type::Whitespace: case token::Type::Whitespace:
return "Whitespace"; return "Whitespace";
case token::Type::Eof:
return "EOF";
default: default:
return "Unknown"; return "Unknown";
} }
} }
std::string Token::name() {
return type_name(this->type);
}
std::ostream& operator<<(std::ostream& stream, Token& token) { std::ostream& operator<<(std::ostream& stream, Token& token) {
stream << token.name() << "(" << token.content << ")"; stream << token.name() << "(" << token.content << ")";
return stream; return stream;
} }
TokenStream::TokenStream(std::vector<Token>& tokens)
: m_tokens{ tokens }, m_position{ 0 } {
};
Token TokenStream::peek(int length) {
int new_pos = m_position + length;
if (new_pos < 0 || new_pos > m_tokens.size()) {
return Token{ Type::Eof, {} };
}
return m_tokens[new_pos];
}
Token TokenStream::peek() {
return this->peek(0);
}
Token TokenStream::next() {
return this->peek(++m_position);
}
Token TokenStream::expect(Type type) {
auto next = this->next();
if (next.type == type) {
return next;
}
throw std::runtime_error("Expected " + type_name(type) + ", got " + type_name(next.type));
}
std::vector<token::Token> tokenize(std::string_view text) { std::vector<token::Token> tokenize(std::string_view text) {
std::vector<token::Token> tokens{}; std::vector<token::Token> tokens{};
@ -80,6 +116,8 @@ namespace token {
} }
} }
tokens.push_back(token::Token{ token::Type::Eof, {} });
return tokens; return tokens;
} }
} }

View File

@ -14,8 +14,12 @@ namespace token {
ReturnKeyword, ReturnKeyword,
Whitespace, Whitespace,
Eof,
}; };
std::string type_name(Type& type);
struct Token { struct Token {
Type type; Type type;
std::string content; std::string content;
@ -23,6 +27,18 @@ namespace token {
std::string name(); std::string name();
}; };
class TokenStream {
private:
std::vector<Token>& m_tokens;
int m_position;
public:
TokenStream(std::vector<Token>& tokens);
Token peek(int length);
Token peek();
Token next();
Token expect(Type type);
};
std::ostream& operator<<(std::ostream& stream, Token& token); std::ostream& operator<<(std::ostream& stream, Token& token);
std::vector<Token> tokenize(std::string_view text); std::vector<Token> tokenize(std::string_view text);