Many fixes for namespace parsing.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-01-05 19:21:06 +01:00
parent bfe27ec20f
commit b4c18e0f09
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 39 additions and 20 deletions

View File

@ -31,41 +31,53 @@ namespace MalachScript::Parser {
diagnostics->Log(level, type, scriptName, span);
};
return ParseScript(firstToken, log);
ScopedPtr<const ParsedStatement> s = nullptr;
ParseScript(s, firstToken, log);
return dynamic_cast<const ParsedScriptStatement*>(s.TakeOwnership());
}
const ParsedScriptStatement* Parser::ParseScript(const LexToken* firstToken, const log_func& log) {
bool Parser::ParseScript(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken,
const log_func& log) {
std::vector<const ParsedStatement*> statements;
const size_t reservedScriptSize = 32;
statements.reserve(reservedScriptSize);
size_t current = 0;
const auto* currentToken = firstToken;
size_t currentAmount = 0;
const auto* current = currentToken;
while (true) {
while (currentToken->GetKind() == LexTokenKind::Whitespace) {
currentToken = currentToken->GetNext().get();
while (current->GetKind() == LexTokenKind::Whitespace) {
current = current->GetNext().get();
}
if (currentToken->GetKind() == LexTokenKind::EndOfFile) {
if (current->GetKind() == LexTokenKind::EndOfFile) {
break;
}
if (current->GetKind() == LexTokenKind::SemicolonSymbol) {
PROGRESS_TOKEN(current);
continue;
}
// Deal with namespace
if (current->GetKind() == LexTokenKind::CloseCurlyParenthesisSymbol) {
break;
}
ScopedPtr<const ParsedStatement> statement;
// TODO: Sort by performance
auto result = ParseTypeDef(statement, currentToken, log) || ParseClass(statement, currentToken, log) ||
ParseFunc(statement, currentToken, log) || ParseNamespace(statement, currentToken, log);
auto result = ParseTypeDef(statement, current, log) || ParseClass(statement, current, log) ||
ParseFunc(statement, current, log) || ParseNamespace(statement, current, log);
if (!result) {
// TODO: Log error
logError(Diagnostics::DiagnosticType::UnexpectedToken, currentToken->GetSpan());
PROGRESS_TOKEN(currentToken);
logError(Diagnostics::DiagnosticType::UnexpectedToken, current->GetSpan());
PROGRESS_TOKEN(current);
continue;
}
statements.push_back(statement.TakeOwnership());
current++;
currentAmount++;
}
statements.resize(current);
statements.resize(currentAmount);
auto end = 0;
if (current > 0) {
if (currentAmount > 0) {
end = statements.back()->GetSpan().GetEnd();
}
return new ParsedScriptStatement(TextSpan(0, end), statements);
out = new ParsedScriptStatement(TextSpan(0, end), statements);
currentToken = current;
return true;
}
bool Parser::ParseClass(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken, const log_func& log) {
const auto* current = currentToken;
@ -182,11 +194,18 @@ namespace MalachScript::Parser {
Identifier identifier;
if (!ParseIdentifier(identifier, current)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan());
} else {
PROGRESS_TOKEN(current);
}
EXPECT_TOKEN(current, OpenCurlyParenthesisSymbol);
ScopedPtr<const ParsedStatement> script = nullptr;
if (!ParseScript(script, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan());
}
const auto* script = ParseScript(current, log);
auto end = current->GetSpan().GetEnd();
PROGRESS_TOKEN(current);
out = new ParsedNamespaceStatement(TextSpan(start, end), identifier, script);
EXPECT_TOKEN(current, CloseCurlyParenthesisSymbol);
out = new ParsedNamespaceStatement(TextSpan(start, end), identifier,
(const ParsedScriptStatement*)script.TakeOwnership());
currentToken = current;
return true;
}

View File

@ -190,7 +190,7 @@ namespace MalachScript::Parser {
// Interface
static bool ParseNamespace(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken,
const log_func&);
static const ParsedScriptStatement* ParseScript(const LexToken* firstToken, const log_func&);
static bool ParseScript(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken, const log_func&);
};
}