Support basic parsing function statements.

This commit is contained in:
2020-10-10 14:29:37 +02:00
parent 911be3f2ed
commit dcf143b1b2
12 changed files with 505 additions and 46 deletions

View File

@@ -1,5 +1,6 @@
#include "Parser.hpp"
#include <iostream>
#include "../CoreData/FuncAttr.hpp"
#include "../CoreData/PrimitiveTypes.hpp"
#define PROGRESS_TOKEN(token) \
@@ -28,9 +29,10 @@ namespace MalachScript::Parser {
break;
}
const ParsedStatement* statement;
auto result = ParseClass(statement) || ParseNamespace(statement);
auto result = ParseClass(statement) || ParseFunc(statement) || ParseNamespace(statement);
if (!result) {
// TODO: Log error
PROGRESS_TOKEN(_currentToken);
continue;
}
statements.push_back(statement);
@@ -61,7 +63,9 @@ namespace MalachScript::Parser {
}
// After class keyword, an identifier should always follow, if it doesn't, log an error.
Identifier identifier;
ParseIdentifier(identifier, current, encounteredError);
if (!ParseIdentifier(identifier, current)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, _currentToken->GetSpan());
}
PROGRESS_TOKEN(current);
std::vector<Identifier> inherits;
std::vector<const ParsedStatement*> body;
@@ -75,11 +79,15 @@ namespace MalachScript::Parser {
case LexTokenKind::ColonSymbol: {
PROGRESS_TOKEN(current);
Identifier id;
ParseIdentifier(id, current, encounteredError);
if (!ParseIdentifier(id, _currentToken)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, _currentToken->GetSpan());
}
inherits.push_back(id);
while (current->GetKind() == LexTokenKind::CommaSymbol) {
PROGRESS_TOKEN(current);
ParseIdentifier(id, current, encounteredError);
if (!ParseIdentifier(id, _currentToken)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, _currentToken->GetSpan());
}
inherits.push_back(id);
PROGRESS_TOKEN(current);
}
@@ -121,14 +129,15 @@ namespace MalachScript::Parser {
auto start = _currentToken->GetSpan().GetStart();
PROGRESS_TOKEN(_currentToken);
Identifier defineFrom;
auto encounteredErrors = false;
if (!ParsePrimType(defineFrom) && !ParseIdentifier(defineFrom, _currentToken, encounteredErrors)) {
if (!ParsePrimType(defineFrom, _currentToken) && !ParseIdentifier(defineFrom, _currentToken)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, _currentToken->GetSpan());
}
PROGRESS_TOKEN(_currentToken);
Identifier defineTo;
ParseIdentifier(defineTo, _currentToken, encounteredErrors);
if (!ParseIdentifier(defineTo, _currentToken)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, _currentToken->GetSpan());
}
PROGRESS_TOKEN(_currentToken);
EXPECT_TOKEN(_currentToken, SemicolonSymbol);
PROGRESS_TOKEN(_currentToken);
@@ -142,21 +151,239 @@ namespace MalachScript::Parser {
auto start = _currentToken->GetSpan().GetStart();
PROGRESS_TOKEN(_currentToken);
Identifier identifier;
bool encounteredErrors = false;
ParseIdentifier(identifier, _currentToken, encounteredErrors);
if (!ParseIdentifier(identifier, _currentToken)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, _currentToken->GetSpan());
}
auto script = ParseScript();
auto end = _currentToken->GetSpan().GetEnd();
PROGRESS_TOKEN(_currentToken);
out = new ParsedNamespaceStatement(TextSpan(start, end), identifier, script);
return true;
}
bool Parser::ParseFunc(const ParsedStatement*& out) {
auto start = _currentToken->GetSpan().GetStart();
const auto* token = _currentToken;
bool isShared = false;
bool isExternal = false;
bool modifiers = true;
while (modifiers) {
switch (token->GetKind()) {
case LexTokenKind::SharedKeyword:
isShared = true;
PROGRESS_TOKEN(token);
continue;
case LexTokenKind::ExternalKeyword:
isExternal = true;
PROGRESS_TOKEN(token);
continue;
default: modifiers = false; break;
}
}
AccessModifier accessModifier = AccessModifier::Public;
if (token->GetKind() == LexTokenKind::PrivateKeyword) {
accessModifier = AccessModifier::Private;
PROGRESS_TOKEN(token);
} else if (token->GetKind() == LexTokenKind::ProtectedKeyword) {
accessModifier = AccessModifier::Protected;
PROGRESS_TOKEN(token);
}
const ParsedStatement* typeStatement = nullptr;
bool returnsReference = false;
if (token->GetKind() == LexTokenKind::TildeSymbol) {
// TODO: Handle destructor
throw std::logic_error("not implemented");
} else if (ParseType(typeStatement, token)) {
if (token->GetKind() == LexTokenKind::AmpersandSymbol) {
returnsReference = true;
PROGRESS_TOKEN(token);
}
}
Identifier identifier;
if (!ParseIdentifier(identifier, token)) {
return false;
}
PROGRESS_TOKEN(token);
const ParsedStatement* paramList = nullptr;
if (!ParseParamList(paramList, token)) {
return false;
}
_currentToken = token;
bool isConst = false;
if (_currentToken->GetKind() == LexTokenKind::ConstKeyword) {
isConst = true;
PROGRESS_TOKEN(_currentToken);
}
bool lookingForFuncAttr = true;
FuncAttr funcAttr;
while (lookingForFuncAttr) {
switch (_currentToken->GetKind()) {
case LexTokenKind::OverrideKeyword:
PROGRESS_TOKEN(_currentToken);
funcAttr = FuncAttrHelpers::Set(funcAttr, FuncAttr::Override);
continue;
case LexTokenKind::FinalKeyword:
PROGRESS_TOKEN(_currentToken);
funcAttr = FuncAttrHelpers::Set(funcAttr, FuncAttr::Final);
continue;
case LexTokenKind::ExplicitKeyword:
PROGRESS_TOKEN(_currentToken);
funcAttr = FuncAttrHelpers::Set(funcAttr, FuncAttr::Explicit);
continue;
case LexTokenKind::PropertyKeyword:
PROGRESS_TOKEN(_currentToken);
funcAttr = FuncAttrHelpers::Set(funcAttr, FuncAttr::Property);
continue;
default: lookingForFuncAttr = false; break;
}
}
const ParsedStatement* statblock = nullptr;
if (_currentToken->GetKind() != LexTokenKind::SemicolonSymbol) {
// TODO: Parse stat block.
throw std::logic_error("not implemented");
}
out = new ParsedFuncStatement(TextSpan(start, _currentToken->GetSpan().GetEnd()), isShared, isExternal,
accessModifier, typeStatement, returnsReference, identifier, paramList, isConst,
funcAttr, statblock);
return true;
}
bool Parser::ParseType(const ParsedStatement*& out, const LexToken*& currentToken) {
const auto* token = currentToken;
auto start = token->GetSpan().GetStart();
bool isConst = false;
bool isArray = false;
bool isHandle = false;
if (token->GetKind() == LexTokenKind::ConstKeyword) {
isConst = true;
PROGRESS_TOKEN(token);
}
ScopedIdentifier scopedIdentifier;
ParseScope(scopedIdentifier.GetScope(), token);
if (!ParseDataType(scopedIdentifier.GetIdentifier(), token)) {
return false;
}
// TODO: Generics.
if (token->GetKind() == LexTokenKind::OpenBlockParenthesisSymbol) {
PROGRESS_TOKEN(token);
if (token->GetKind() != LexTokenKind::CloseBlockParenthesisSymbol) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, token->GetSpan());
} else {
PROGRESS_TOKEN(token);
isArray = true;
}
} else if (token->GetKind() == LexTokenKind::AtSymbol) {
isHandle = true;
PROGRESS_TOKEN(token);
if (token->GetKind() == LexTokenKind::ConstKeyword) {
isConst = true;
PROGRESS_TOKEN(token);
}
}
auto end = token->GetSpan().GetEnd();
currentToken = token;
out = new ParsedTypeStatement(TextSpan(start, end), isConst, isArray, isHandle, scopedIdentifier);
return true;
}
bool Parser::ParseScope(std::vector<Identifier>& scope, const LexToken*& currentToken) {
if (currentToken->GetKind() == LexTokenKind::ColonColonSymbol) {
scope.emplace_back();
PROGRESS_TOKEN(currentToken);
}
Identifier identifier;
if (ParseIdentifier(identifier, currentToken)) {
const auto* n = currentToken->GetNext().get();
currentToken = n;
scope.push_back(identifier);
} else {
return false;
}
while (currentToken != nullptr && currentToken->GetKind() == LexTokenKind::ColonColonSymbol) {
const auto* n = currentToken;
PROGRESS_TOKEN(n);
if (ParseIdentifier(identifier, n)) {
PROGRESS_TOKEN(n);
if (n->GetKind() == LexTokenKind::ColonColonSymbol) {
currentToken = n;
scope.push_back(identifier);
} else {
break;
}
} else {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, currentToken->GetSpan());
break;
}
}
// TODO: Handle generics in script class name.
return true;
}
bool Parser::ParseParamList(const ParsedStatement*& out, const LexToken*& currentToken) {
if (currentToken->GetKind() != LexTokenKind::OpenParenthesisSymbol) {
return false;
}
auto start = currentToken->GetSpan().GetStart();
PROGRESS_TOKEN(currentToken);
std::vector<ParsedParamListStatement::ParsedParameter> parameters;
if (currentToken->GetKind() == LexTokenKind::VoidKeyword) {
PROGRESS_TOKEN(currentToken);
if (currentToken->GetKind() != LexTokenKind::CloseParenthesisSymbol) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, currentToken->GetSpan());
}
PROGRESS_TOKEN(currentToken);
out = new ParsedParamListStatement(TextSpan(start, currentToken->GetSpan().GetEnd()), parameters);
return true;
} else if (currentToken->GetKind() == LexTokenKind::CloseParenthesisSymbol) {
out = new ParsedParamListStatement(TextSpan(start, currentToken->GetSpan().GetEnd()), parameters);
PROGRESS_TOKEN(currentToken);
return true;
}
while (true) {
parameters.emplace_back();
auto parameter = parameters.at(parameters.size() - 1);
if (!ParseType((const ParsedStatement*&)parameter.GetTypeStatement(), currentToken)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, currentToken->GetSpan());
}
ParseTypeMod(parameter.GetTypeMod());
ParseIdentifier(parameter.GetIdentifier(), currentToken);
if (currentToken->GetKind() != LexTokenKind::CommaSymbol) {
break;
}
PROGRESS_TOKEN(currentToken);
}
if (currentToken->GetKind() != LexTokenKind::CloseParenthesisSymbol) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, currentToken->GetSpan());
}
out = new ParsedParamListStatement(TextSpan(start, currentToken->GetSpan().GetEnd()), parameters);
PROGRESS_TOKEN(currentToken);
return true;
}
bool Parser::ParseDataType(Identifier& out, const LexToken*& currentToken) {
switch (currentToken->GetKind()) {
case LexTokenKind::Identifier:
out = static_cast<const IdentifierToken*>(currentToken)->GetValue();
PROGRESS_TOKEN(currentToken);
return true;
case LexTokenKind::AutoKeyword:
out = PrimitiveTypes::AutoName();
PROGRESS_TOKEN(currentToken);
return true;
default:
if (ParsePrimType(out, currentToken)) {
PROGRESS_TOKEN(currentToken);
return true;
}
return false;
}
}
bool Parser::ParseVirtProp([[maybe_unused]] const ParsedStatement*& out) { return false; }
bool Parser::ParseFunc([[maybe_unused]] const ParsedStatement*& out) { return false; }
bool Parser::ParseVar([[maybe_unused]] const ParsedStatement*& out) { return false; }
bool Parser::ParseFuncDef([[maybe_unused]] const ParsedStatement*& out) { return false; }
bool Parser::ParsePrimType(Identifier& out) {
// TODO: out needs to return a value.
switch (_currentToken->GetKind()) {
bool Parser::ParsePrimType(Identifier& out, const LexToken*& token) {
switch (token->GetKind()) {
case LexTokenKind::VoidKeyword: out = PrimitiveTypes::VoidName(); return true;
case LexTokenKind::IntKeyword: out = PrimitiveTypes::IntName(); return true;
case LexTokenKind::Int8Keyword: out = PrimitiveTypes::Int8Name(); return true;
@@ -174,4 +401,25 @@ namespace MalachScript::Parser {
default: return false;
}
}
bool Parser::ParseTypeMod(TypeMod& typeMod) {
if (_currentToken->GetKind() != LexTokenKind::AmpersandSymbol) {
return false;
}
PROGRESS_TOKEN(_currentToken);
switch (_currentToken->GetKind()) {
case LexTokenKind::InKeyword:
typeMod = TypeMod::RefIn;
PROGRESS_TOKEN(_currentToken);
return true;
case LexTokenKind::OutKeyword:
typeMod = TypeMod::RefOut;
PROGRESS_TOKEN(_currentToken);
return true;
case LexTokenKind::InoutKeyword:
typeMod = TypeMod::RefInOut;
PROGRESS_TOKEN(_currentToken);
return true;
default: typeMod = TypeMod::RefInOut; return true;
}
}
}