sapphire/src/Token.cpp

191 lines
4.4 KiB
C++
Raw Normal View History

#include "Token.h"
#include "StringConversion.h"
#include "FormatString/FormatString.hpp"
const std::string token_strings[] = {
"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_NULL",
"TT_EQUALS",
"TT_GTE",
"TT_LTE",
};
Token::Token(const TokenType& type)
: tk_type(type), loc(0,0,"")
{
}
Token::Token(const TokenType& type, const Location& location)
: tk_type(type), loc(location)
{
}
Token::Token(const TokenType& type, const std::string& val)
: tk_type(type), loc(0,0,""), string_value(val)
{
}
Token::Token(const TokenType& type, const int& val, const Location& location)
: tk_type(type), int_value(val), loc(location)
{
}
Token::Token(const TokenType& type, const std::string& val, const Location& location)
: tk_type(type), string_value(val), loc(location)
{
}
Token::Token(const TokenType& type, const float& val, const Location& location)
: tk_type(type), float_value(val), loc(location)
{
}
Token::~Token()
{
}
Token Token::copy_with_new_type(const TokenType& type)
{
Token result(type,loc);
result.int_value = int_value;
result.float_value = float_value;
result.string_value = string_value;
result.line_text = line_text;
return result;
}
std::string Token::to_string() const
{
std::string details = loc.to_parenthesized_string();
if(tk_type == TT_Number)
{
return format_string("INT:%d %s",int_value,details);
}
else if (tk_type == TT_Float)
{
return format_string("FLOAT:%f %s",float_value,details);
}
else if (tk_type == TT_Identifier)
{
return format_string("ID:%s %s",string_value,details);
}
else if (tk_type == TT_Keyword)
{
return format_string("KEYWORD:%s %s",string_value,details);
}
else if (tk_type == TT_String)
{
return format_string("STRING:'%s' %s",string_value,details);
}
switch(tk_type)
{
case TT_EOF:
return "EOF " + details;
case TT_Plus:
return "PLUS " + details;
case TT_Minus:
return "MINUS " + details;
case TT_Mul:
return "MUL " + details;
case TT_Div:
return "DIV " + details;
case TT_At:
return "AT " + details;
case TT_Equal:
return "EQUAL " + details;
case TT_LessThan:
return "LESSTHAN " + details;
case TT_GreaterThan:
return "GREATERTHAN " + details;
case TT_LParen:
return "LPAREN " + details;
case TT_RParen:
return "RPAREN " + details;
case TT_LBracket:
return "LBRACKET " + details;
case TT_RBracket:
return "RBRACKET " + details;
case TT_Semicolon:
return "SEMICOLON " + details;
case TT_LoadedString:
return "LDSTRING " + details;
case TT_Equals:
return "EQUALS " + details;
case TT_GTE:
return "GTE " + details;
case TT_LTE:
return "LTE " + details;
}
return "";
}
std::string Token::line() const
{
return this->line_text;
}
// Return a copy of the original token, but adding the contents of the line where
// the token was located.
Token Token::make_with_line(const Token& origin, const std::string& line_text)
{
Token result(origin.tk_type,origin.loc);
result.int_value = origin.int_value;
result.float_value = origin.float_value;
result.string_value = origin.string_value;
result.line_text = line_text;
return result;
}
void Token::operator=(const Token& other)
{
tk_type = other.tk_type;
int_value = other.int_value;
string_value = other.string_value;
float_value = other.float_value;
line_text = other.line_text;
}
void Token::erase(Token& tk)
{
tk.tk_type = TT_Null;
}
bool Token::match_token_types(const TokenStream& a, const TokenStream& b, int count)
{
int size = [](int a, int b){ return a > b ? b : a; }(a.size() - count,b.size());
for(int i = 0; i < size; ++i)
{
if(a[i+count].tk_type != b[i].tk_type)
{
return false;
}
}
return true;
}