2022-03-11 16:00:09 +00:00
|
|
|
#include "Token.h"
|
|
|
|
#include "StringConversion.h"
|
|
|
|
#include "FormatString/FormatString.hpp"
|
2022-05-28 18:44:26 +00:00
|
|
|
#include "replace.h"
|
2022-03-11 16:00:09 +00:00
|
|
|
|
|
|
|
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",
|
2022-05-28 18:44:26 +00:00
|
|
|
"TT_PERIOD",
|
|
|
|
"TT_COMMA",
|
|
|
|
"TT_PATH",
|
|
|
|
"TT_EXCLAMATION",
|
|
|
|
"TT_NEQUAL"
|
2022-03-11 16:00:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-05-28 18:44:26 +00:00
|
|
|
replace(const_cast<std::string&>(string_value),"\n","\\n");
|
2022-03-11 16:00:09 +00:00
|
|
|
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;
|
2022-05-28 18:44:26 +00:00
|
|
|
case TT_Period:
|
|
|
|
return "PERIOD " + details;
|
|
|
|
case TT_Comma:
|
|
|
|
return "COMMA " + details;
|
|
|
|
case TT_Path:
|
|
|
|
return "PATH " + details;
|
|
|
|
case TT_Exclamation:
|
|
|
|
return "EXCLAMATION " + details;
|
|
|
|
case TT_NEqual:
|
|
|
|
return "NEQUAL " + details;
|
2022-03-11 16:00:09 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-28 18:44:26 +00:00
|
|
|
bool Token::match_token_types(const std::vector<Token>& a, const std::vector<Token>& b, int count)
|
2022-03-11 16:00:09 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|