2020-11-17 02:31:51 +01:00
|
|
|
#include "lang/parser.hpp"
|
2020-11-10 15:26:38 +01:00
|
|
|
|
2020-11-28 21:55:52 +01:00
|
|
|
#include "helpers/utils.hpp"
|
2020-12-27 15:54:12 +01:00
|
|
|
#include "lang/token.hpp"
|
2020-11-21 23:00:09 +01:00
|
|
|
|
2020-11-10 15:26:38 +01:00
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace hex::lang {
|
|
|
|
|
|
|
|
Parser::Parser() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
bool Parser::tryConsume(TokenIter &curr, std::initializer_list<Token::Type> tokenTypes) {
|
2020-11-10 15:26:38 +01:00
|
|
|
std::vector<Token>::const_iterator originalPosition = curr;
|
|
|
|
|
|
|
|
for (const auto& type : tokenTypes) {
|
|
|
|
if (curr->type != type) {
|
|
|
|
curr = originalPosition;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
curr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
ASTNode* Parser::parseBuiltinVariableDecl(TokenIter &curr, bool hasEndianDef) {
|
|
|
|
if (hasEndianDef) {
|
|
|
|
std::endian endianess;
|
|
|
|
|
|
|
|
if (curr[-4].keywordToken.keyword == Token::KeywordToken::Keyword::LittleEndian)
|
|
|
|
endianess = std::endian::little;
|
|
|
|
else if (curr[-4].keywordToken.keyword == Token::KeywordToken::Keyword::BigEndian)
|
|
|
|
endianess = std::endian::big;
|
|
|
|
else {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected be or le identifier" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ASTNodeVariableDecl(curr[-4].lineNumber, curr[-3].typeToken.type, curr[-2].identifierToken.identifier, "", {}, 1, {}, {}, endianess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return new ASTNodeVariableDecl(curr[-3].lineNumber, curr[-3].typeToken.type, curr[-2].identifierToken.identifier);
|
2020-11-10 15:26:38 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
ASTNode* Parser::parseCustomTypeVariableDecl(TokenIter &curr, bool hasEndianDef) {
|
|
|
|
if (hasEndianDef) {
|
|
|
|
std::endian endianess;
|
|
|
|
|
|
|
|
if (curr[-4].keywordToken.keyword == Token::KeywordToken::Keyword::LittleEndian)
|
|
|
|
endianess = std::endian::little;
|
|
|
|
else if (curr[-4].keywordToken.keyword == Token::KeywordToken::Keyword::BigEndian)
|
|
|
|
endianess = std::endian::big;
|
|
|
|
else return nullptr;
|
|
|
|
|
|
|
|
return new ASTNodeVariableDecl(curr[-4].lineNumber, Token::TypeToken::Type::CustomType, curr[-2].identifierToken.identifier, curr[-3].identifierToken.identifier, {}, 1, {}, {}, endianess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return new ASTNodeVariableDecl(curr[-3].lineNumber, Token::TypeToken::Type::CustomType, curr[-2].identifierToken.identifier, curr[-3].identifierToken.identifier);
|
2020-11-10 15:26:38 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
ASTNode* Parser::parseBuiltinPointerVariableDecl(TokenIter &curr, bool hasEndianDef) {
|
2020-11-21 23:00:09 +01:00
|
|
|
auto pointerType = curr[-2].typeToken.type;
|
|
|
|
|
2020-12-27 15:54:12 +01:00
|
|
|
if (!Token::isUnsigned(pointerType)) {
|
2020-11-27 21:20:23 +01:00
|
|
|
this->m_error = { curr->lineNumber, "Pointer size needs to be a unsigned type" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curr[-5].operatorToken.op != Token::OperatorToken::Operator::Star) {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected '*' for pointer definition" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curr[-3].operatorToken.op != Token::OperatorToken::Operator::Inherit) {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected ':' after member name" };
|
2020-11-21 23:00:09 +01:00
|
|
|
return nullptr;
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-21 23:00:09 +01:00
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
|
|
|
|
if (hasEndianDef) {
|
|
|
|
std::endian endianess;
|
|
|
|
|
|
|
|
if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::LittleEndian)
|
|
|
|
endianess = std::endian::little;
|
|
|
|
else if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::BigEndian)
|
|
|
|
endianess = std::endian::big;
|
|
|
|
else return nullptr;
|
|
|
|
|
2020-12-27 15:54:12 +01:00
|
|
|
return new ASTNodeVariableDecl(curr[-7].lineNumber, curr[-6].typeToken.type, curr[-4].identifierToken.identifier, "", { }, 1, { }, Token::getTypeSize(pointerType), endianess);
|
2020-12-06 21:40:57 +01:00
|
|
|
}
|
|
|
|
else
|
2020-12-27 15:54:12 +01:00
|
|
|
return new ASTNodeVariableDecl(curr[-6].lineNumber, curr[-6].typeToken.type, curr[-4].identifierToken.identifier, "", { }, 1, { }, Token::getTypeSize(pointerType));
|
2020-11-21 23:00:09 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
ASTNode* Parser::parseCustomTypePointerVariableDecl(TokenIter &curr, bool hasEndianDef) {
|
2020-11-21 23:00:09 +01:00
|
|
|
auto pointerType = curr[-2].typeToken.type;
|
|
|
|
|
2020-12-27 15:54:12 +01:00
|
|
|
if (!Token::isUnsigned(pointerType)) {
|
2020-11-27 21:20:23 +01:00
|
|
|
this->m_error = { curr->lineNumber, "Pointer size needs to be a unsigned type" };
|
2020-11-21 23:00:09 +01:00
|
|
|
return nullptr;
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-21 23:00:09 +01:00
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
if (curr[-5].operatorToken.op != Token::OperatorToken::Operator::Star) {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected '*' for pointer definition" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curr[-3].operatorToken.op != Token::OperatorToken::Operator::Inherit) {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected ':' after member name" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
if (hasEndianDef) {
|
|
|
|
std::endian endianess;
|
|
|
|
|
|
|
|
if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::LittleEndian)
|
|
|
|
endianess = std::endian::little;
|
|
|
|
else if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::BigEndian)
|
|
|
|
endianess = std::endian::big;
|
|
|
|
else {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected be or le identifier" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:54:12 +01:00
|
|
|
return new ASTNodeVariableDecl(curr[-7].lineNumber,Token::TypeToken::Type::CustomType, curr[-4].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, 1, { }, Token::getTypeSize(pointerType), endianess);
|
2020-12-06 21:40:57 +01:00
|
|
|
}
|
|
|
|
else
|
2020-12-27 15:54:12 +01:00
|
|
|
return new ASTNodeVariableDecl(curr[-6].lineNumber, Token::TypeToken::Type::CustomType, curr[-4].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, 1, { }, Token::getTypeSize(pointerType));
|
2020-11-21 23:00:09 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
ASTNode* Parser::parseBuiltinArrayDecl(TokenIter &curr, bool hasEndianDef) {
|
|
|
|
if (hasEndianDef) {
|
|
|
|
std::endian endianess;
|
|
|
|
|
|
|
|
if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::LittleEndian)
|
|
|
|
endianess = std::endian::little;
|
|
|
|
else if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::BigEndian)
|
|
|
|
endianess = std::endian::big;
|
|
|
|
else {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected be or le identifier" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ASTNodeVariableDecl(curr[-7].lineNumber, curr[-6].typeToken.type, curr[-5].identifierToken.identifier, "", { }, curr[-3].integerToken.integer, { }, { }, endianess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return new ASTNodeVariableDecl(curr[-6].lineNumber, curr[-6].typeToken.type, curr[-5].identifierToken.identifier, "", { }, curr[-3].integerToken.integer);
|
2020-11-12 23:57:43 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
ASTNode* Parser::parseCustomTypeArrayDecl(TokenIter &curr, bool hasEndianDef) {
|
|
|
|
if (hasEndianDef) {
|
|
|
|
std::endian endianess;
|
|
|
|
|
|
|
|
if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::LittleEndian)
|
|
|
|
endianess = std::endian::little;
|
|
|
|
else if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::BigEndian)
|
|
|
|
endianess = std::endian::big;
|
|
|
|
else {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected be or le identifier" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ASTNodeVariableDecl(curr[-7].lineNumber, Token::TypeToken::Type::CustomType, curr[-5].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, curr[-3].integerToken.integer, { }, { }, endianess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return new ASTNodeVariableDecl(curr[-6].lineNumber, Token::TypeToken::Type::CustomType, curr[-5].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, curr[-3].integerToken.integer);
|
2020-11-12 23:57:43 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
ASTNode* Parser::parseBuiltinVariableArrayDecl(TokenIter &curr, bool hasEndianDef) {
|
|
|
|
if (hasEndianDef) {
|
|
|
|
std::endian endianess;
|
|
|
|
|
|
|
|
if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::LittleEndian)
|
|
|
|
endianess = std::endian::little;
|
|
|
|
else if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::BigEndian)
|
|
|
|
endianess = std::endian::big;
|
|
|
|
else {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected be or le identifier" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ASTNodeVariableDecl(curr[-7].lineNumber, curr[-6].typeToken.type, curr[-5].identifierToken.identifier, "", { }, 0, curr[-3].identifierToken.identifier, { }, endianess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return new ASTNodeVariableDecl(curr[-6].lineNumber, curr[-6].typeToken.type, curr[-5].identifierToken.identifier, "", { }, 0, curr[-3].identifierToken.identifier);
|
2020-11-21 20:19:33 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
ASTNode* Parser::parseCustomTypeVariableArrayDecl(TokenIter &curr, bool hasEndianDef) {
|
|
|
|
if (hasEndianDef) {
|
|
|
|
std::endian endianess;
|
|
|
|
|
|
|
|
if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::LittleEndian)
|
|
|
|
endianess = std::endian::little;
|
|
|
|
else if (curr[-7].keywordToken.keyword == Token::KeywordToken::Keyword::BigEndian)
|
|
|
|
endianess = std::endian::big;
|
|
|
|
else {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected be or le identifier" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ASTNodeVariableDecl(curr[-7].lineNumber, Token::TypeToken::Type::CustomType, curr[-5].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, 0, curr[-3].identifierToken.identifier, { }, endianess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return new ASTNodeVariableDecl(curr[-6].lineNumber, Token::TypeToken::Type::CustomType, curr[-5].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, 0, curr[-3].identifierToken.identifier);
|
2020-11-21 20:19:33 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
ASTNode* Parser::parsePaddingDecl(TokenIter &curr) {
|
2020-12-06 21:40:57 +01:00
|
|
|
return new ASTNodeVariableDecl(curr[-5].lineNumber, curr[-5].typeToken.type, "", "", { }, curr[-3].integerToken.integer);
|
2020-11-21 20:19:33 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
ASTNode* Parser::parseFreeBuiltinVariableDecl(TokenIter &curr, bool hasEndianDef) {
|
|
|
|
if (hasEndianDef) {
|
|
|
|
std::endian endianess;
|
|
|
|
|
|
|
|
if (curr[-6].keywordToken.keyword == Token::KeywordToken::Keyword::LittleEndian)
|
|
|
|
endianess = std::endian::little;
|
|
|
|
else if (curr[-6].keywordToken.keyword == Token::KeywordToken::Keyword::BigEndian)
|
|
|
|
endianess = std::endian::big;
|
|
|
|
else {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected be or le identifier" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ASTNodeVariableDecl(curr[-6].lineNumber, curr[-5].typeToken.type, curr[-4].identifierToken.identifier, "", curr[-2].integerToken.integer, 1, { }, { }, endianess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return new ASTNodeVariableDecl(curr[-5].lineNumber, curr[-5].typeToken.type, curr[-4].identifierToken.identifier, "", curr[-2].integerToken.integer);
|
2020-11-10 16:24:48 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
ASTNode* Parser::parseFreeCustomTypeVariableDecl(TokenIter &curr, bool hasEndianDef) {
|
|
|
|
if (hasEndianDef) {
|
|
|
|
std::endian endianess;
|
|
|
|
|
|
|
|
if (curr[-6].keywordToken.keyword == Token::KeywordToken::Keyword::LittleEndian)
|
|
|
|
endianess = std::endian::little;
|
|
|
|
else if (curr[-6].keywordToken.keyword == Token::KeywordToken::Keyword::BigEndian)
|
|
|
|
endianess = std::endian::big;
|
|
|
|
else {
|
|
|
|
this->m_error = { curr->lineNumber, "Expected be or le identifier" };
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ASTNodeVariableDecl(curr[-6].lineNumber, Token::TypeToken::Type::CustomType, curr[-4].identifierToken.identifier, curr[-5].identifierToken.identifier, curr[-2].integerToken.integer, 1, { }, { }, endianess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return new ASTNodeVariableDecl(curr[-5].lineNumber, Token::TypeToken::Type::CustomType, curr[-4].identifierToken.identifier, curr[-5].identifierToken.identifier, curr[-2].integerToken.integer);
|
2020-11-10 16:24:48 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
ASTNode* Parser::parseStruct(TokenIter &curr) {
|
2020-11-10 15:26:38 +01:00
|
|
|
const std::string &structName = curr[-2].identifierToken.identifier;
|
|
|
|
std::vector<ASTNode*> nodes;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
u32 startLineNumber = curr[-3].lineNumber;
|
|
|
|
|
2020-11-10 15:26:38 +01:00
|
|
|
while (!tryConsume(curr, {Token::Type::ScopeClose})) {
|
|
|
|
if (tryConsume(curr, {Token::Type::Type, Token::Type::Identifier, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseBuiltinVariableDecl(curr, false));
|
2020-11-10 15:26:38 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Identifier, Token::Type::Identifier, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseCustomTypeVariableDecl(curr, false));
|
2020-11-12 23:57:43 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Type, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseBuiltinArrayDecl(curr, false));
|
2020-11-12 23:57:43 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Identifier, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseCustomTypeArrayDecl(curr, false));
|
2020-11-21 20:19:33 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Type, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Identifier, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseBuiltinVariableArrayDecl(curr, false));
|
2020-11-21 20:19:33 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Identifier, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Identifier, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseCustomTypeVariableArrayDecl(curr, false));
|
2020-11-20 21:59:27 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Type, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression})) {
|
|
|
|
if (curr[-5].typeToken.type != Token::TypeToken::Type::Padding) {
|
|
|
|
for(auto &node : nodes) delete node;
|
2020-11-27 21:20:23 +01:00
|
|
|
|
|
|
|
this->m_error = { curr[-5].lineNumber, "No member name provided" };
|
2020-11-20 21:59:27 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nodes.push_back(parsePaddingDecl(curr));
|
2020-11-21 23:00:09 +01:00
|
|
|
} else if (tryConsume(curr, {Token::Type::Type, Token::Type::Operator, Token::Type::Identifier, Token::Type::Operator, Token::Type::Type, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseBuiltinPointerVariableDecl(curr, false));
|
2020-11-21 23:00:09 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Identifier, Token::Type::Operator, Token::Type::Identifier, Token::Type::Operator, Token::Type::Type, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseCustomTypePointerVariableDecl(curr, false));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Type, Token::Type::Identifier, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseBuiltinVariableDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Identifier, Token::Type::Identifier, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseCustomTypeVariableDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Type, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseBuiltinArrayDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Identifier, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseCustomTypeArrayDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Type, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Identifier, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseBuiltinVariableArrayDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Identifier, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Identifier, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseCustomTypeVariableArrayDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Type, Token::Type::Operator, Token::Type::Identifier, Token::Type::Operator, Token::Type::Type, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseBuiltinPointerVariableDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Identifier, Token::Type::Operator, Token::Type::Identifier, Token::Type::Operator, Token::Type::Type, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseCustomTypePointerVariableDecl(curr, true));
|
2020-11-27 21:20:23 +01:00
|
|
|
else {
|
|
|
|
for(auto &node : nodes) delete node;
|
2020-12-07 23:49:19 +01:00
|
|
|
this->m_error = { curr[-1].lineNumber, "Invalid sequence, expected member declaration" };
|
2020-11-27 21:20:23 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-11-10 15:26:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tryConsume(curr, {Token::Type::EndOfExpression})) {
|
2020-12-07 23:49:19 +01:00
|
|
|
this->m_error = { curr[-1].lineNumber, "Expected ';' after struct definition" };
|
2020-11-10 15:26:38 +01:00
|
|
|
for(auto &node : nodes) delete node;
|
2020-11-14 14:40:21 +01:00
|
|
|
return nullptr;
|
2020-11-10 15:26:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
return new ASTNodeStruct(startLineNumber, structName, nodes);
|
2020-11-10 15:26:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
ASTNode* Parser::parseUnion(TokenIter &curr) {
|
2020-11-20 21:29:28 +01:00
|
|
|
const std::string &unionName = curr[-2].identifierToken.identifier;
|
|
|
|
std::vector<ASTNode*> nodes;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
u32 startLineNumber = curr[-3].lineNumber;
|
|
|
|
|
2020-11-20 21:29:28 +01:00
|
|
|
while (!tryConsume(curr, {Token::Type::ScopeClose})) {
|
|
|
|
if (tryConsume(curr, {Token::Type::Type, Token::Type::Identifier, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseBuiltinVariableDecl(curr, false));
|
2020-11-20 21:29:28 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Identifier, Token::Type::Identifier, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseCustomTypeVariableDecl(curr, false));
|
2020-11-20 21:29:28 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Type, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseBuiltinArrayDecl(curr, false));
|
2020-11-20 21:29:28 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Identifier, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseCustomTypeArrayDecl(curr, false));
|
2020-11-21 23:00:09 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Type, Token::Type::Operator, Token::Type::Identifier, Token::Type::Operator, Token::Type::Type, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseBuiltinPointerVariableDecl(curr, false));
|
2020-11-21 23:00:09 +01:00
|
|
|
else if (tryConsume(curr, {Token::Type::Identifier, Token::Type::Operator, Token::Type::Identifier, Token::Type::Operator, Token::Type::Type, Token::Type::EndOfExpression}))
|
2020-12-06 21:40:57 +01:00
|
|
|
nodes.push_back(parseCustomTypePointerVariableDecl(curr, false));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Type, Token::Type::Identifier, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseBuiltinVariableDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Identifier, Token::Type::Identifier, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseCustomTypeVariableDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Type, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseBuiltinArrayDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Identifier, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseCustomTypeArrayDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Type, Token::Type::Operator, Token::Type::Identifier, Token::Type::Operator, Token::Type::Type, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseBuiltinPointerVariableDecl(curr, true));
|
|
|
|
else if (tryConsume(curr, {Token::Type::Keyword, Token::Type::Identifier, Token::Type::Operator, Token::Type::Identifier, Token::Type::Operator, Token::Type::Type, Token::Type::EndOfExpression}))
|
|
|
|
nodes.push_back(parseCustomTypePointerVariableDecl(curr, true));
|
2020-11-27 21:20:23 +01:00
|
|
|
else {
|
|
|
|
for(auto &node : nodes) delete node;
|
2020-12-07 23:49:19 +01:00
|
|
|
this->m_error = { curr[-1].lineNumber, "Invalid sequence, expected member declaration" };
|
2020-11-27 21:20:23 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-11-20 21:29:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tryConsume(curr, {Token::Type::EndOfExpression})) {
|
|
|
|
for(auto &node : nodes) delete node;
|
2020-11-27 21:20:23 +01:00
|
|
|
this->m_error = { curr[-1].lineNumber, "Expected ';' after union definition" };
|
2020-11-20 21:29:28 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
return new ASTNodeUnion(startLineNumber, unionName, nodes);
|
2020-11-20 21:29:28 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
ASTNode* Parser::parseEnum(TokenIter &curr) {
|
2020-11-14 14:40:21 +01:00
|
|
|
const std::string &enumName = curr[-4].identifierToken.identifier;
|
|
|
|
const Token::TypeToken::Type underlyingType = curr[-2].typeToken.type;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
u32 startLineNumber = curr[-5].lineNumber;
|
|
|
|
|
|
|
|
if (curr[-3].operatorToken.op != Token::OperatorToken::Operator::Inherit) {
|
|
|
|
this->m_error = { curr[-3].lineNumber, "Expected ':' after enum name" };
|
2020-11-14 14:40:21 +01:00
|
|
|
return nullptr;
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-14 14:40:21 +01:00
|
|
|
|
2020-12-27 15:54:12 +01:00
|
|
|
if (!Token::isUnsigned(underlyingType)) {
|
2020-11-27 21:20:23 +01:00
|
|
|
this->m_error = { curr[-3].lineNumber, "Underlying type needs to be an unsigned type" };
|
2020-11-14 14:40:21 +01:00
|
|
|
return nullptr;
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-14 14:40:21 +01:00
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
auto enumNode = new ASTNodeEnum(startLineNumber, underlyingType, enumName);
|
2020-11-14 14:40:21 +01:00
|
|
|
|
|
|
|
while (!tryConsume(curr, {Token::Type::ScopeClose})) {
|
2020-11-14 15:06:27 +01:00
|
|
|
if (tryConsume(curr, { Token::Type::Identifier, Token::Type::Separator }) || tryConsume(curr, { Token::Type::Identifier, Token::Type::ScopeClose })) {
|
2020-11-14 14:40:21 +01:00
|
|
|
u64 value;
|
|
|
|
if (enumNode->getValues().empty())
|
|
|
|
value = 0;
|
|
|
|
else
|
|
|
|
value = enumNode->getValues().back().first + 1;
|
|
|
|
|
2020-11-14 15:06:27 +01:00
|
|
|
enumNode->getValues().emplace_back(value, curr[-2].identifierToken.identifier);
|
|
|
|
|
|
|
|
if (curr[-1].type == Token::Type::ScopeClose)
|
|
|
|
break;
|
2020-11-14 14:40:21 +01:00
|
|
|
}
|
|
|
|
else if (tryConsume(curr, { Token::Type::Identifier, Token::Type::Operator, Token::Type::Integer, Token::Type::Separator})) {
|
2020-11-14 15:06:27 +01:00
|
|
|
enumNode->getValues().emplace_back(curr[-2].integerToken.integer, curr[-4].identifierToken.identifier);
|
|
|
|
}
|
|
|
|
else if (tryConsume(curr, { Token::Type::Identifier, Token::Type::Operator, Token::Type::Integer, Token::Type::ScopeClose})) {
|
|
|
|
enumNode->getValues().emplace_back(curr[-2].integerToken.integer, curr[-4].identifierToken.identifier);
|
|
|
|
break;
|
2020-11-14 14:40:21 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete enumNode;
|
2020-11-27 21:20:23 +01:00
|
|
|
this->m_error = { curr->lineNumber, "Expected constant identifier" };
|
2020-11-14 14:40:21 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tryConsume(curr, {Token::Type::EndOfExpression})) {
|
|
|
|
delete enumNode;
|
2020-11-27 21:20:23 +01:00
|
|
|
this->m_error = { curr[-1].lineNumber, "Expected ';' after enum definition" };
|
2020-11-14 14:40:21 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return enumNode;
|
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
ASTNode* Parser::parseBitField(TokenIter &curr) {
|
2020-11-20 20:26:19 +01:00
|
|
|
const std::string &bitfieldName = curr[-2].identifierToken.identifier;
|
|
|
|
std::vector<std::pair<std::string, size_t>> fields;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
u32 startLineNumber = curr[-3].lineNumber;
|
|
|
|
|
2020-11-20 20:26:19 +01:00
|
|
|
while (!tryConsume(curr, {Token::Type::ScopeClose})) {
|
|
|
|
if (tryConsume(curr, {Token::Type::Identifier, Token::Type::Operator, Token::Type::Integer, Token::Type::EndOfExpression})) {
|
2020-11-27 21:20:23 +01:00
|
|
|
if (curr[-3].operatorToken.op != Token::OperatorToken::Operator::Inherit) {
|
|
|
|
this->m_error = { curr[-3].lineNumber, "Expected ':' after member name" };
|
2020-11-20 20:26:19 +01:00
|
|
|
return nullptr;
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-20 20:26:19 +01:00
|
|
|
|
|
|
|
fields.emplace_back(curr[-4].identifierToken.identifier, curr[-2].integerToken.integer);
|
|
|
|
}
|
2020-11-27 21:20:23 +01:00
|
|
|
else {
|
2020-12-07 23:49:19 +01:00
|
|
|
this->m_error = { curr[-1].lineNumber, "Invalid sequence, expected member declaration" };
|
2020-11-27 21:20:23 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-11-20 20:26:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
if (!tryConsume(curr, {Token::Type::EndOfExpression})) {
|
|
|
|
this->m_error = { curr[-1].lineNumber, "Expected ';' after bitfield definition" };
|
2020-11-20 20:26:19 +01:00
|
|
|
return nullptr;
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-20 20:26:19 +01:00
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
return new ASTNodeBitField(startLineNumber, bitfieldName, fields);
|
2020-11-20 20:26:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
ASTNode* Parser::parseScope(TokenIter &curr) {
|
|
|
|
return new ASTNodeScope(curr[-1].lineNumber, parseTillToken(curr, Token::Type::ScopeClose));
|
2020-11-10 15:26:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
std::optional<ASTNode*> Parser::parseUsingDeclaration(TokenIter &curr) {
|
2020-11-10 15:26:38 +01:00
|
|
|
auto keyword = curr[-5].keywordToken;
|
|
|
|
auto name = curr[-4].identifierToken;
|
|
|
|
auto op = curr[-3].operatorToken;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
if (keyword.keyword != Token::KeywordToken::Keyword::Using) {
|
|
|
|
this->m_error = { curr[-5].lineNumber, "Invalid keyword. Expected 'using'" };
|
2020-11-10 15:26:38 +01:00
|
|
|
return { };
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-10 15:26:38 +01:00
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
if (op.op != Token::OperatorToken::Operator::Assignment) {
|
|
|
|
this->m_error = { curr[-3].lineNumber, "Invalid operator. Expected '='" };
|
2020-11-10 15:26:38 +01:00
|
|
|
return { };
|
2020-11-27 21:20:23 +01:00
|
|
|
}
|
2020-11-10 15:26:38 +01:00
|
|
|
|
|
|
|
if (curr[-2].type == Token::Type::Type) {
|
|
|
|
auto type = curr[-2].typeToken;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
return new ASTNodeTypeDecl(curr[-2].lineNumber, type.type, name.identifier);
|
2020-11-10 15:26:38 +01:00
|
|
|
} else if (curr[-2].type == Token::Type::Identifier) {
|
|
|
|
auto customType = curr[-2].identifierToken;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
return new ASTNodeTypeDecl(curr[-2].lineNumber, Token::TypeToken::Type::CustomType, name.identifier, customType.identifier);
|
2020-11-10 15:26:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
this->m_error = { curr[-2].lineNumber, hex::format("'%s' does not name a type") };
|
2020-11-10 15:26:38 +01:00
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
std::optional<std::vector<ASTNode*>> Parser::parseStatement(TokenIter &curr) {
|
2020-11-10 15:26:38 +01:00
|
|
|
std::vector<ASTNode*> program;
|
|
|
|
|
2020-11-10 16:24:48 +01:00
|
|
|
// Struct
|
2020-11-10 15:26:38 +01:00
|
|
|
if (tryConsume(curr, { Token::Type::Keyword, Token::Type::Identifier, Token::Type::ScopeOpen })) {
|
|
|
|
if (curr[-3].keywordToken.keyword == Token::KeywordToken::Keyword::Struct) {
|
|
|
|
auto structAst = parseStruct(curr);
|
|
|
|
|
2020-11-14 14:40:21 +01:00
|
|
|
if (structAst == nullptr) {
|
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
|
|
|
program.push_back(structAst);
|
2020-11-20 21:29:28 +01:00
|
|
|
} else if (curr[-3].keywordToken.keyword == Token::KeywordToken::Keyword::Union) {
|
|
|
|
auto unionAst = parseUnion(curr);
|
|
|
|
|
|
|
|
if (unionAst == nullptr) {
|
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
|
|
|
program.push_back(unionAst);
|
2020-11-20 20:26:19 +01:00
|
|
|
} else if (curr[-3].keywordToken.keyword == Token::KeywordToken::Keyword::Bitfield) {
|
|
|
|
auto bitfieldAst = parseBitField(curr);
|
|
|
|
|
|
|
|
if (bitfieldAst == nullptr) {
|
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
|
|
|
program.push_back(bitfieldAst);
|
2020-11-14 14:40:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return program;
|
|
|
|
|
|
|
|
} // Enum
|
2020-11-20 20:26:19 +01:00
|
|
|
else if (tryConsume(curr, { Token::Type::Keyword, Token::Type::Identifier, Token::Type::Operator, Token::Type::Type, Token::Type::ScopeOpen })) {
|
2020-11-14 14:40:21 +01:00
|
|
|
if (curr[-5].keywordToken.keyword == Token::KeywordToken::Keyword::Enum) {
|
|
|
|
auto enumAst = parseEnum(curr);
|
|
|
|
|
|
|
|
if (enumAst == nullptr) {
|
2020-11-10 15:26:38 +01:00
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2020-11-14 14:40:21 +01:00
|
|
|
program.push_back(enumAst);
|
2020-11-10 15:26:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return program;
|
2020-11-10 16:24:48 +01:00
|
|
|
// Scope
|
2020-11-10 15:26:38 +01:00
|
|
|
} else if (tryConsume(curr, { Token::Type::ScopeOpen })) {
|
|
|
|
program.push_back(parseScope(curr));
|
|
|
|
|
|
|
|
return program;
|
2020-11-10 16:24:48 +01:00
|
|
|
|
|
|
|
// Using declaration with built-in type
|
2020-11-10 15:26:38 +01:00
|
|
|
} else if (tryConsume(curr, { Token::Type::Keyword, Token::Type::Identifier, Token::Type::Operator, Token::Type::Type, Token::Type::EndOfExpression})) {
|
|
|
|
auto usingDecl = parseUsingDeclaration(curr);
|
|
|
|
|
|
|
|
if (!usingDecl.has_value()) {
|
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
|
|
|
program.push_back(usingDecl.value());
|
|
|
|
|
|
|
|
return program;
|
2020-11-10 16:24:48 +01:00
|
|
|
|
|
|
|
// Using declaration with custom type
|
2020-11-10 15:26:38 +01:00
|
|
|
} else if (tryConsume(curr, { Token::Type::Keyword, Token::Type::Identifier, Token::Type::Operator, Token::Type::Identifier, Token::Type::EndOfExpression})) {
|
|
|
|
auto usingDecl = parseUsingDeclaration(curr);
|
|
|
|
|
|
|
|
if (!usingDecl.has_value()) {
|
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
|
|
|
program.push_back(usingDecl.value());
|
|
|
|
|
2020-11-10 16:24:48 +01:00
|
|
|
return program;
|
2020-11-27 21:20:23 +01:00
|
|
|
// Variable placement declaration with built-in type
|
2020-11-10 16:24:48 +01:00
|
|
|
} else if (tryConsume(curr, { Token::Type::Type, Token::Type::Identifier, Token::Type::Operator, Token::Type::Integer, Token::Type::EndOfExpression})) {
|
2020-11-27 21:20:23 +01:00
|
|
|
if (curr[-3].operatorToken.op != Token::OperatorToken::Operator::AtDeclaration) {
|
|
|
|
this->m_error = { curr[-3].lineNumber, "Expected '@' after variable placement declaration" };
|
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
auto variableDecl = parseFreeBuiltinVariableDecl(curr, false);
|
2020-11-10 16:24:48 +01:00
|
|
|
|
|
|
|
program.push_back(variableDecl);
|
|
|
|
|
|
|
|
return program;
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
// Variable placement declaration with custom type
|
2020-11-10 16:24:48 +01:00
|
|
|
} else if (tryConsume(curr, { Token::Type::Identifier, Token::Type::Identifier, Token::Type::Operator, Token::Type::Integer, Token::Type::EndOfExpression})) {
|
2020-11-27 21:20:23 +01:00
|
|
|
if (curr[-3].operatorToken.op != Token::OperatorToken::Operator::AtDeclaration) {
|
|
|
|
this->m_error = { curr[-3].lineNumber, "Expected '@' after variable placement declaration" };
|
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
auto variableDecl = parseFreeCustomTypeVariableDecl(curr, false);
|
|
|
|
|
|
|
|
program.push_back(variableDecl);
|
|
|
|
|
|
|
|
return program;
|
|
|
|
|
|
|
|
// Variable placement declaration with built-in type and big/little endian setting
|
|
|
|
} else if (tryConsume(curr, { Token::Type::Keyword, Token::Type::Type, Token::Type::Identifier, Token::Type::Operator, Token::Type::Integer, Token::Type::EndOfExpression})) {
|
|
|
|
if (curr[-3].operatorToken.op != Token::OperatorToken::Operator::AtDeclaration) {
|
|
|
|
this->m_error = { curr[-3].lineNumber, "Expected '@' after variable placement declaration" };
|
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2020-12-07 23:49:19 +01:00
|
|
|
if (curr[-6].keywordToken.keyword != Token::KeywordToken::Keyword::LittleEndian && curr[-6].keywordToken.keyword != Token::KeywordToken::Keyword::BigEndian) {
|
|
|
|
this->m_error = { curr[-3].lineNumber, "Expected endianess identifier" };
|
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
auto variableDecl = parseFreeBuiltinVariableDecl(curr, true);
|
|
|
|
|
|
|
|
program.push_back(variableDecl);
|
|
|
|
|
|
|
|
return program;
|
|
|
|
|
|
|
|
// Variable placement declaration with custom type and big/little endian setting
|
|
|
|
} else if (tryConsume(curr, { Token::Type::Keyword, Token::Type::Identifier, Token::Type::Identifier, Token::Type::Operator, Token::Type::Integer, Token::Type::EndOfExpression})) {
|
|
|
|
if (curr[-3].operatorToken.op != Token::OperatorToken::Operator::AtDeclaration) {
|
|
|
|
this->m_error = { curr[-3].lineNumber, "Expected '@' after variable placement declaration" };
|
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2020-12-07 23:49:19 +01:00
|
|
|
if (curr[-6].keywordToken.keyword != Token::KeywordToken::Keyword::LittleEndian && curr[-6].keywordToken.keyword != Token::KeywordToken::Keyword::BigEndian) {
|
|
|
|
this->m_error = { curr[-6].lineNumber, "Expected endianess identifier" };
|
|
|
|
for(auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2020-12-06 21:40:57 +01:00
|
|
|
auto variableDecl = parseFreeCustomTypeVariableDecl(curr, true);
|
2020-11-10 16:24:48 +01:00
|
|
|
|
|
|
|
program.push_back(variableDecl);
|
|
|
|
|
2020-11-10 15:26:38 +01:00
|
|
|
return program;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(auto &node : program) delete node;
|
2020-11-27 21:20:23 +01:00
|
|
|
this->m_error = { curr->lineNumber, "Invalid sequence" };
|
2020-11-10 15:26:38 +01:00
|
|
|
return { };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
std::vector<ASTNode*> Parser::parseTillToken(TokenIter &curr, Token::Type endTokenType) {
|
2020-11-10 15:26:38 +01:00
|
|
|
std::vector<ASTNode*> program;
|
|
|
|
|
|
|
|
while (curr->type != endTokenType) {
|
|
|
|
auto newTokens = parseStatement(curr);
|
|
|
|
|
2020-12-07 23:49:19 +01:00
|
|
|
if (!newTokens.has_value()) {
|
|
|
|
for (auto &node : program) delete node;
|
|
|
|
return { };
|
|
|
|
}
|
2020-11-10 15:26:38 +01:00
|
|
|
|
|
|
|
program.insert(program.end(), newTokens->begin(), newTokens->end());
|
|
|
|
}
|
|
|
|
|
|
|
|
curr++;
|
|
|
|
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
std::optional<std::vector<ASTNode*>> Parser::parse(const std::vector<Token> &tokens) {
|
2020-11-10 15:26:38 +01:00
|
|
|
auto currentToken = tokens.begin();
|
|
|
|
|
|
|
|
auto program = parseTillToken(currentToken, Token::Type::EndOfProgram);
|
|
|
|
|
2020-11-19 22:06:38 +01:00
|
|
|
if (program.empty() || currentToken != tokens.end())
|
2020-12-22 18:10:01 +01:00
|
|
|
return { };
|
2020-11-10 15:26:38 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
return program;
|
2020-11-10 15:26:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|