2020-11-17 02:31:51 +01:00
|
|
|
#include "lang/lexer.hpp"
|
2020-11-10 15:26:38 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace hex::lang {
|
|
|
|
|
|
|
|
Lexer::Lexer() { }
|
|
|
|
|
|
|
|
std::string matchTillInvalid(const char* characters, std::function<bool(char)> predicate) {
|
|
|
|
std::string ret;
|
|
|
|
|
|
|
|
while (*characters != 0x00) {
|
|
|
|
ret += *characters;
|
|
|
|
characters++;
|
|
|
|
|
|
|
|
if (!predicate(*characters))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<u64> parseInt(std::string_view string) {
|
|
|
|
u64 integer = 0;
|
2020-11-11 10:47:02 +01:00
|
|
|
u8 base;
|
2020-11-10 15:26:38 +01:00
|
|
|
|
|
|
|
std::string_view numberData;
|
|
|
|
|
|
|
|
if (string.starts_with("0x")) {
|
|
|
|
numberData = string.substr(2);
|
|
|
|
base = 16;
|
|
|
|
|
|
|
|
if (numberData.find_first_not_of("0123456789ABCDEFabcdef") != std::string_view::npos)
|
|
|
|
return { };
|
|
|
|
} else if (string.starts_with("0b")) {
|
|
|
|
numberData = string.substr(2);
|
|
|
|
base = 2;
|
|
|
|
|
|
|
|
if (numberData.find_first_not_of("01") != std::string_view::npos)
|
|
|
|
return { };
|
|
|
|
} else if (isdigit(string[0])) {
|
|
|
|
numberData = string;
|
|
|
|
base = 10;
|
|
|
|
|
|
|
|
if (numberData.find_first_not_of("0123456789") != std::string_view::npos)
|
|
|
|
return { };
|
|
|
|
} else return { };
|
|
|
|
|
|
|
|
if (numberData.length() == 0)
|
|
|
|
return { };
|
|
|
|
|
|
|
|
for (const char& c : numberData) {
|
|
|
|
integer *= base;
|
|
|
|
|
|
|
|
if (isdigit(c))
|
|
|
|
integer += (c - '0');
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
2020-11-10 16:24:48 +01:00
|
|
|
integer += 10 + (c - 'A');
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (c >= 'a' && c <= 'f')
|
2020-11-10 16:24:48 +01:00
|
|
|
integer += 10 + (c - 'a');
|
2020-11-10 15:26:38 +01:00
|
|
|
else return { };
|
|
|
|
}
|
|
|
|
|
|
|
|
return integer;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<Result, std::vector<Token>> Lexer::lex(const std::string& code) {
|
|
|
|
std::vector<Token> tokens;
|
|
|
|
u32 offset = 0;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
u32 lineNumber = 1;
|
|
|
|
|
2020-11-10 15:26:38 +01:00
|
|
|
while (offset < code.length()) {
|
2020-11-14 14:40:21 +01:00
|
|
|
|
2020-11-13 14:35:52 +01:00
|
|
|
// Handle comments
|
|
|
|
if (code[offset] == '/') {
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
if (offset < code.length() && code[offset] == '/') {
|
|
|
|
offset++;
|
|
|
|
while (offset < code.length()) {
|
|
|
|
if (code[offset] == '\n' || code[offset] == '\r')
|
|
|
|
break;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
} else if (offset < code.length() && code[offset] == '*') {
|
|
|
|
offset++;
|
|
|
|
while (offset < (code.length() - 1)) {
|
2020-11-27 21:20:23 +01:00
|
|
|
if (code[offset] == '\n') lineNumber++;
|
|
|
|
|
2020-11-13 14:35:52 +01:00
|
|
|
if (code[offset] == '*' && code[offset + 1] == '/')
|
|
|
|
break;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += 2;
|
|
|
|
} else offset--;
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:26:38 +01:00
|
|
|
const char& c = code[offset];
|
|
|
|
|
2020-11-16 22:54:54 +01:00
|
|
|
if (c == 0x00)
|
|
|
|
break;
|
|
|
|
|
2020-11-10 15:26:38 +01:00
|
|
|
if (std::isblank(c) || std::isspace(c)) {
|
2020-11-27 21:20:23 +01:00
|
|
|
if (code[offset] == '\n') lineNumber++;
|
2020-11-10 15:26:38 +01:00
|
|
|
offset += 1;
|
|
|
|
} else if (c == ';') {
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::EndOfExpression, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
offset += 1;
|
|
|
|
} else if (c == '{') {
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::ScopeOpen, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
offset += 1;
|
|
|
|
} else if (c == '}') {
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::ScopeClose, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
offset += 1;
|
2020-11-12 23:57:43 +01:00
|
|
|
} else if (c == '[') {
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::ArrayOpen, .lineNumber = lineNumber });
|
2020-11-12 23:57:43 +01:00
|
|
|
offset += 1;
|
|
|
|
} else if (c == ']') {
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({.type = Token::Type::ArrayClose, .lineNumber = lineNumber });
|
2020-11-12 23:57:43 +01:00
|
|
|
offset += 1;
|
2020-11-10 15:26:38 +01:00
|
|
|
} else if (c == ',') {
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Separator, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
offset += 1;
|
|
|
|
} else if (c == '@') {
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Operator, .operatorToken = { .op = Token::OperatorToken::Operator::AtDeclaration }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
offset += 1;
|
|
|
|
} else if (c == '=') {
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Operator, .operatorToken = { .op = Token::OperatorToken::Operator::Assignment }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
offset += 1;
|
2020-11-14 14:40:21 +01:00
|
|
|
} else if (c == ':') {
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Operator, .operatorToken = { .op = Token::OperatorToken::Operator::Inherit }, .lineNumber = lineNumber });
|
2020-11-14 14:40:21 +01:00
|
|
|
offset += 1;
|
2020-11-21 23:00:09 +01:00
|
|
|
} else if (c == '*') {
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Operator, .operatorToken = { .op = Token::OperatorToken::Operator::Star }, .lineNumber = lineNumber });
|
2020-11-21 23:00:09 +01:00
|
|
|
offset += 1;
|
2020-11-27 14:18:28 +01:00
|
|
|
} else if (c == '\'') {
|
|
|
|
offset += 1;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
if (offset >= code.length()) {
|
|
|
|
this->m_error = { lineNumber, "Invalid character literal" };
|
2020-11-27 14:18:28 +01:00
|
|
|
return { ResultLexicalError, { } };
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-27 14:18:28 +01:00
|
|
|
|
|
|
|
char character = code[offset];
|
|
|
|
|
|
|
|
if (character == '\\') {
|
|
|
|
offset += 1;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
if (offset >= code.length()) {
|
|
|
|
this->m_error = { lineNumber, "Invalid character literal" };
|
2020-11-27 14:18:28 +01:00
|
|
|
return { ResultLexicalError, { } };
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-27 14:18:28 +01:00
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
if (code[offset] != '\\' && code[offset] != '\'') {
|
|
|
|
this->m_error = { lineNumber, "Invalid escape sequence" };
|
2020-11-27 14:18:28 +01:00
|
|
|
return { ResultLexicalError, { } };
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-27 14:18:28 +01:00
|
|
|
|
|
|
|
character = code[offset];
|
|
|
|
} else {
|
2020-11-27 21:20:23 +01:00
|
|
|
if (code[offset] == '\\' || code[offset] == '\'' || character == '\n' || character == '\r') {
|
|
|
|
this->m_error = { lineNumber, "Invalid character literal" };
|
2020-11-27 14:18:28 +01:00
|
|
|
return { ResultLexicalError, { } };
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-27 14:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
offset += 1;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
if (offset >= code.length() || code[offset] != '\'') {
|
|
|
|
this->m_error = { lineNumber, "Missing terminating ' after character literal" };
|
2020-11-27 14:18:28 +01:00
|
|
|
return { ResultLexicalError, { } };
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-27 14:18:28 +01:00
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Integer, .integerToken = { .integer = character }, .lineNumber = lineNumber });
|
2020-11-27 14:18:28 +01:00
|
|
|
offset += 1;
|
|
|
|
|
2020-11-10 15:26:38 +01:00
|
|
|
} else if (std::isalpha(c)) {
|
|
|
|
std::string identifier = matchTillInvalid(&code[offset], [](char c) -> bool { return std::isalnum(c) || c == '_'; });
|
|
|
|
|
|
|
|
// Check for reserved keywords
|
|
|
|
|
|
|
|
if (identifier == "struct")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Keyword, .keywordToken = { .keyword = Token::KeywordToken::Keyword::Struct }, .lineNumber = lineNumber });
|
2020-11-20 21:29:28 +01:00
|
|
|
else if (identifier == "union")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Keyword, .keywordToken = { .keyword = Token::KeywordToken::Keyword::Union }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "using")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Keyword, .keywordToken = { .keyword = Token::KeywordToken::Keyword::Using }, .lineNumber = lineNumber });
|
2020-11-14 14:40:21 +01:00
|
|
|
else if (identifier == "enum")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Keyword, .keywordToken = { .keyword = Token::KeywordToken::Keyword::Enum }, .lineNumber = lineNumber });
|
2020-11-20 20:26:19 +01:00
|
|
|
else if (identifier == "bitfield")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Keyword, .keywordToken = { .keyword = Token::KeywordToken::Keyword::Bitfield }, .lineNumber = lineNumber });
|
2020-12-06 21:40:57 +01:00
|
|
|
else if (identifier == "be")
|
|
|
|
tokens.push_back({ .type = Token::Type::Keyword, .keywordToken = { .keyword = Token::KeywordToken::Keyword::BigEndian }, .lineNumber = lineNumber });
|
|
|
|
else if (identifier == "le")
|
|
|
|
tokens.push_back({ .type = Token::Type::Keyword, .keywordToken = { .keyword = Token::KeywordToken::Keyword::LittleEndian }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
// Check for built-in types
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "u8")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Unsigned8Bit }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "s8")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Signed8Bit }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "u16")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Unsigned16Bit }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "s16")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Signed16Bit }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "u32")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Unsigned32Bit }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "s32")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Signed32Bit }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "u64")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Unsigned64Bit }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "s64")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Signed64Bit }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "u128")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Unsigned128Bit }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "s128")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Signed128Bit }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "float")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Float }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (identifier == "double")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Double }, .lineNumber = lineNumber });
|
2020-11-20 21:59:27 +01:00
|
|
|
else if (identifier == "padding")
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Padding }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
|
|
|
|
// If it's not a keyword and a builtin type, it has to be an identifier
|
|
|
|
|
|
|
|
else
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({.type = Token::Type::Identifier, .identifierToken = { .identifier = identifier }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
|
|
|
|
offset += identifier.length();
|
|
|
|
} else if (std::isdigit(c)) {
|
|
|
|
char *end = nullptr;
|
|
|
|
std::strtoull(&code[offset], &end, 0);
|
|
|
|
|
2020-12-06 13:48:56 +01:00
|
|
|
auto integer = parseInt(std::string_view(&code[offset], end - &code[offset]));
|
2020-11-10 15:26:38 +01:00
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
if (!integer.has_value()) {
|
|
|
|
this->m_error = { lineNumber, "Invalid integer literal" };
|
2020-11-10 15:26:38 +01:00
|
|
|
return { ResultLexicalError, {}};
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-10 15:26:38 +01:00
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::Integer, .integerToken = { .integer = integer.value() }, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
offset += (end - &code[offset]);
|
2020-11-27 21:20:23 +01:00
|
|
|
} else {
|
|
|
|
this->m_error = { lineNumber, "Unknown token" };
|
|
|
|
return { ResultLexicalError, {} };
|
|
|
|
}
|
2020-11-10 15:26:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
tokens.push_back({ .type = Token::Type::EndOfProgram, .lineNumber = lineNumber });
|
2020-11-10 15:26:38 +01:00
|
|
|
|
|
|
|
return { ResultSuccess, tokens };
|
|
|
|
}
|
|
|
|
}
|