(Lexer)+to_string functions for Token, Token::Type

This commit is contained in:
2024-06-01 13:51:10 +01:00
parent a4689f9dd0
commit 4625b3b7a5
2 changed files with 24 additions and 17 deletions

View File

@@ -16,6 +16,7 @@ extern "C"
} }
#include <algorithm> #include <algorithm>
#include <sstream>
#include "./lexer.hpp" #include "./lexer.hpp"
@@ -417,12 +418,6 @@ namespace Lexer
return Err{}; return Err{};
} }
std::ostream &operator<<(std::ostream &os, Token &t)
{
return os << token_type_as_cstr(t.type) << "(`" << t.content << "`)@"
<< t.line << ", " << t.column;
}
Token::Token() Token::Token()
{} {}
@@ -430,7 +425,11 @@ namespace Lexer
: type{type}, column{col}, line{line}, content{content} : type{type}, column{col}, line{line}, content{content}
{} {}
const char *token_type_as_cstr(Token::Type type) Err::Err(Err::Type type, size_t col, size_t line)
: col{col}, line{line}, type{type}
{}
std::string to_string(const Token::Type &type)
{ {
switch (type) switch (type)
{ {
@@ -526,7 +525,20 @@ namespace Lexer
return ""; return "";
} }
std::ostream &operator<<(std::ostream &os, Err &lerr) std::string to_string(const Token &t)
{
std::stringstream stream;
stream << to_string(t.type) << "(`" << t.content << "`)@" << t.line << ", "
<< t.column;
return stream.str();
}
std::ostream &operator<<(std::ostream &os, const Token &t)
{
return os << to_string(t);
}
std::ostream &operator<<(std::ostream &os, const Err &lerr)
{ {
os << lerr.line << ":" << lerr.col << ": "; os << lerr.line << ":" << lerr.col << ": ";
switch (lerr.type) switch (lerr.type)
@@ -557,8 +569,4 @@ namespace Lexer
} }
return os; return os;
} }
Err::Err(Err::Type type, size_t col, size_t line)
: col{col}, line{line}, type{type}
{}
} // namespace Lexer } // namespace Lexer

View File

@@ -15,7 +15,6 @@
#include <ostream> #include <ostream>
#include <string> #include <string>
#include <tuple>
#include <vector> #include <vector>
namespace Lexer namespace Lexer
@@ -76,9 +75,9 @@ namespace Lexer
Token(Token::Type, std::string, size_t col = 0, size_t line = 0); Token(Token::Type, std::string, size_t col = 0, size_t line = 0);
}; };
const char *token_type_as_cstr(Token::Type type); std::ostream &operator<<(std::ostream &, const Token &);
std::string to_string(const Token::Type &);
std::ostream &operator<<(std::ostream &, Token &); std::string to_string(const Token &);
struct Err struct Err
{ {
@@ -97,7 +96,7 @@ namespace Lexer
Err(Type type = Type::OK, size_t col = 0, size_t line = 0); Err(Type type = Type::OK, size_t col = 0, size_t line = 0);
}; };
std::ostream &operator<<(std::ostream &, Err &); std::ostream &operator<<(std::ostream &, const Err &);
Err tokenise_buffer(std::string_view, std::vector<Token *> &); Err tokenise_buffer(std::string_view, std::vector<Token *> &);
} // namespace Lexer } // namespace Lexer