sapphire/src/Token.h

98 lines
2.1 KiB
C++

#pragma once
#include "Location.h"
#include "sapphirepch.h"
/* All current token types. Will change in the future. */
enum TokenType
{
TT_Identifier,
TT_Number,
TT_Float,
TT_Keyword,
TT_String,
TT_Plus,
TT_Minus,
TT_Mul,
TT_Div,
TT_At,
TT_Equal,
TT_LessThan,
TT_GreaterThan,
TT_LParen,
TT_RParen,
TT_LBracket,
TT_RBracket,
TT_Semicolon,
TT_LoadedString,
TT_EOF,
TT_Equals,
TT_GTE,
TT_LTE,
TT_Period,
TT_Comma,
TT_Exclamation,
TT_NEqual,
TT_LSQB,
TT_RSQB,
TT_Syscall0,
TT_Syscall1,
TT_Syscall2,
TT_Syscall3,
TT_Syscall4,
TT_Syscall5,
TT_Let,
TT_In,
TT_Colon,
};
/* Struct to represent tokens generated by the Lexer. */
struct Token
{
TokenType type;
std::optional<int> int_value;
std::optional<std::string> string_value;
std::optional<float> float_value;
Location location;
Token(const Token& other);
Token(TokenType type);
Token(TokenType type, const Location& location);
Token(TokenType type, int value, const Location& location);
Token(TokenType type, std::string value, const Location& location);
Token(TokenType type, std::string value);
Token(TokenType type, float val, const Location& location);
~Token() = default;
/* Return the contents of the line where the Token was located. */
std::string line() const
{
return m_line_text;
}
/* Return a copy of the original token, but adding the contents of the line where
the token was located. */
static Token make_with_line(const Token& origin, const std::string& line_text);
void operator=(const Token& other);
/* Return a copy of this Token, but with its TokenType changed. */
Token copy_with_new_type(const TokenType& type) const;
private:
// FIXME: this should be moved to Location, to remove all Token* that are only used to throw errors at a certain
// location.
std::string m_line_text;
};
/* typedef to make it easier to see a what a std::vector of tokens is being used for. */
typedef std::vector<Token> TokenStream;