More work on better parser logging.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-01-06 13:32:06 +01:00
parent 034dcb118b
commit da82819fff
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 217 additions and 180 deletions

18
extern/format.hpp vendored
View File

@ -41,9 +41,12 @@ namespace util {
char* endptr = nullptr; char* endptr = nullptr;
index = strtol(&item[0], &endptr, 10); index = strtol(&item[0], &endptr, 10);
if (index < 0 || index >= (int)args.size()) { if (index >= (int)args.size()) {
return; return;
} }
if (index < 0) {
index = args.size() + index;
}
if (*endptr == ',') { if (*endptr == ',') {
alignment = strtol(endptr + 1, &endptr, 10); alignment = strtol(endptr + 1, &endptr, 10);
@ -57,8 +60,21 @@ namespace util {
if (*endptr == ':') { if (*endptr == ':') {
fmt = endptr + 1; fmt = endptr + 1;
} }
if (*endptr == '.' && *(endptr + 1) == '.') {
auto endIndex = strtol(endptr + 2, &endptr, 10);
if (endIndex < 0) {
endIndex = args.size() + endIndex + 1;
}
for (; index < endIndex; index++) {
args[index]->Format(ss, fmt); args[index]->Format(ss, fmt);
if (index != endIndex - 1) {
ss << ", ";
}
}
} else {
args[index]->Format(ss, fmt);
}
return; return;
} }

View File

@ -14,7 +14,14 @@ namespace MalachScript::Diagnostics {
case DiagnosticType::ExpectedEndOfString: case DiagnosticType::ExpectedEndOfString:
return util::Format("Expected end of string, found {0}", diag->GetFormats()); return util::Format("Expected end of string, found {0}", diag->GetFormats());
case DiagnosticType::UnexpectedToken: case DiagnosticType::UnexpectedToken:
return util::Format("Unexpected Token. Expected any of: {0}, but found {1}", diag->GetFormats()); if (diag->GetFormats().size() == 1) {
return util::Format("Unexpected Token: '{0}'", diag->GetFormats());
} else if (diag->GetFormats().size() == 2) {
return util::Format("Unexpected Token. Expected: '{0}', but found '{-1}'", diag->GetFormats());
} else if (diag->GetFormats().size() > 2) {
return util::Format("Unexpected Token. Expected any of: '{0..-2}', but found '{-1}'",
diag->GetFormats());
}
case DiagnosticType::DoubleProperty: return "Property block found twice."; case DiagnosticType::DoubleProperty: return "Property block found twice.";
} }
return std::to_string((uint8_t)diag->GetType()); return std::to_string((uint8_t)diag->GetType());

View File

@ -144,127 +144,127 @@ namespace MalachScript::Parser {
case LexTokenKind::Unknown: return "Unknown"; case LexTokenKind::Unknown: return "Unknown";
case LexTokenKind::EndOfFile: return "EndOfFile"; case LexTokenKind::EndOfFile: return "EndOfFile";
case LexTokenKind::Whitespace: break; case LexTokenKind::Whitespace: return "Whitespace";
case LexTokenKind::StarSymbol: break; case LexTokenKind::StarSymbol: return "*";
case LexTokenKind::StarStarSymbol: break; case LexTokenKind::StarStarSymbol: return "**";
case LexTokenKind::SlashSymbol: break; case LexTokenKind::SlashSymbol: return "/";
case LexTokenKind::PercentSymbol: break; case LexTokenKind::PercentSymbol: return "%";
case LexTokenKind::PlusSymbol: break; case LexTokenKind::PlusSymbol: return "+";
case LexTokenKind::MinusSymbol: break; case LexTokenKind::MinusSymbol: return "-";
case LexTokenKind::LessThanEqualsSymbol: break; case LexTokenKind::LessThanEqualsSymbol: return "<=";
case LexTokenKind::LessThanSymbol: break; case LexTokenKind::LessThanSymbol: return "<";
case LexTokenKind::GreaterThanEqualsSymbol: break; case LexTokenKind::GreaterThanEqualsSymbol: return ">=";
case LexTokenKind::GreaterThanSymbol: break; case LexTokenKind::GreaterThanSymbol: return ">";
case LexTokenKind::OpenParenthesisSymbol: break; case LexTokenKind::OpenParenthesisSymbol: return "(";
case LexTokenKind::CloseParenthesisSymbol: break; case LexTokenKind::CloseParenthesisSymbol: return ")";
case LexTokenKind::EqualsEqualsSymbol: break; case LexTokenKind::EqualsEqualsSymbol: return "==";
case LexTokenKind::ExclamationMarkEqualsSymbol: break; case LexTokenKind::ExclamationMarkEqualsSymbol: return "!=";
case LexTokenKind::QuestionMarkSymbol: break; case LexTokenKind::QuestionMarkSymbol: return "?";
case LexTokenKind::ColonSymbol: break; case LexTokenKind::ColonSymbol: return ":";
case LexTokenKind::EqualsSymbol: break; case LexTokenKind::EqualsSymbol: return "=";
case LexTokenKind::PlusEqualsSymbol: break; case LexTokenKind::PlusEqualsSymbol: return "+=";
case LexTokenKind::MinusEqualsSymbol: break; case LexTokenKind::MinusEqualsSymbol: return "-=";
case LexTokenKind::StarEqualsSymbol: break; case LexTokenKind::StarEqualsSymbol: return "*=";
case LexTokenKind::SlashEqualsSymbol: break; case LexTokenKind::SlashEqualsSymbol: return "/=";
case LexTokenKind::PercentEqualsSymbol: break; case LexTokenKind::PercentEqualsSymbol: return "%=";
case LexTokenKind::StarStarEqualsSymbol: break; case LexTokenKind::StarStarEqualsSymbol: return "**=";
case LexTokenKind::PlusPlusSymbol: break; case LexTokenKind::PlusPlusSymbol: return "++";
case LexTokenKind::MinusMinusSymbol: break; case LexTokenKind::MinusMinusSymbol: return "--";
case LexTokenKind::AmpersandSymbol: break; case LexTokenKind::AmpersandSymbol: return "&";
case LexTokenKind::CommaSymbol: break; case LexTokenKind::CommaSymbol: return ",";
case LexTokenKind::OpenCurlyParenthesisSymbol: break; case LexTokenKind::OpenCurlyParenthesisSymbol: return "{";
case LexTokenKind::CloseCurlyParenthesisSymbol: break; case LexTokenKind::CloseCurlyParenthesisSymbol: return "}";
case LexTokenKind::SemicolonSymbol: break; case LexTokenKind::SemicolonSymbol: return ";";
case LexTokenKind::VerticalLineSymbol: break; case LexTokenKind::VerticalLineSymbol: return "|";
case LexTokenKind::CaretSymbol: break; case LexTokenKind::CaretSymbol: return "^";
case LexTokenKind::TildeSymbol: break; case LexTokenKind::TildeSymbol: return "~";
case LexTokenKind::LessThanLessThanSymbol: break; case LexTokenKind::LessThanLessThanSymbol: return "<<";
case LexTokenKind::GreaterThanGreaterThanSymbol: break; case LexTokenKind::GreaterThanGreaterThanSymbol: return ">>";
case LexTokenKind::GreaterThanGreaterThanGreaterThanSymbol: break; case LexTokenKind::GreaterThanGreaterThanGreaterThanSymbol: return ">>>";
case LexTokenKind::AmpersandEqualsSymbol: break; case LexTokenKind::AmpersandEqualsSymbol: return "&=";
case LexTokenKind::VerticalLineEqualsSymbol: break; case LexTokenKind::VerticalLineEqualsSymbol: return "|=";
case LexTokenKind::CaretEqualsSymbol: break; case LexTokenKind::CaretEqualsSymbol: return "^=";
case LexTokenKind::LessThanLessThanEqualsSymbol: break; case LexTokenKind::LessThanLessThanEqualsSymbol: return "<<=";
case LexTokenKind::GreaterThanGreaterThanEqualsSymbol: break; case LexTokenKind::GreaterThanGreaterThanEqualsSymbol: return ">>=";
case LexTokenKind::GreaterThanGreaterThanGreaterThanEqualsSymbol: break; case LexTokenKind::GreaterThanGreaterThanGreaterThanEqualsSymbol: return ">>>=";
case LexTokenKind::DotSymbol: break; case LexTokenKind::DotSymbol: return ".";
case LexTokenKind::AmpersandAmpersandSymbol: break; case LexTokenKind::AmpersandAmpersandSymbol: return "&";
case LexTokenKind::VerticalLineVerticalLineSymbol: break; case LexTokenKind::VerticalLineVerticalLineSymbol: return "||";
case LexTokenKind::ExclamationMarkSymbol: break; case LexTokenKind::ExclamationMarkSymbol: return "!";
case LexTokenKind::OpenBlockParenthesisSymbol: break; case LexTokenKind::OpenBlockParenthesisSymbol: return "[";
case LexTokenKind::CloseBlockParenthesisSymbol: break; case LexTokenKind::CloseBlockParenthesisSymbol: return "]";
case LexTokenKind::CaretCaretSymbol: break; case LexTokenKind::CaretCaretSymbol: return "^^";
case LexTokenKind::AtSymbol: break; case LexTokenKind::AtSymbol: return "@";
case LexTokenKind::ExclamationMarkIsSymbol: break; case LexTokenKind::ExclamationMarkIsSymbol: return "!=";
case LexTokenKind::ColonColonSymbol: break; case LexTokenKind::ColonColonSymbol: return "::";
case LexTokenKind::AndKeyword: break; case LexTokenKind::AndKeyword: return "and";
case LexTokenKind::AbstractKeyword: break; case LexTokenKind::AbstractKeyword: return "abstract";
case LexTokenKind::AutoKeyword: break; case LexTokenKind::AutoKeyword: return "auto";
case LexTokenKind::BoolKeyword: break; case LexTokenKind::BoolKeyword: return "bool";
case LexTokenKind::BreakKeyword: break; case LexTokenKind::BreakKeyword: return "break";
case LexTokenKind::CaseKeyword: break; case LexTokenKind::CaseKeyword: return "case";
case LexTokenKind::CastKeyword: break; case LexTokenKind::CastKeyword: return "cast";
case LexTokenKind::CatchKeyword: break; case LexTokenKind::CatchKeyword: return "catch";
case LexTokenKind::ClassKeyword: break; case LexTokenKind::ClassKeyword: return "class";
case LexTokenKind::ConstKeyword: break; case LexTokenKind::ConstKeyword: return "const";
case LexTokenKind::ContinueKeyword: break; case LexTokenKind::ContinueKeyword: return "continue";
case LexTokenKind::DefaultKeyword: break; case LexTokenKind::DefaultKeyword: return "default";
case LexTokenKind::DoKeyword: break; case LexTokenKind::DoKeyword: return "do";
case LexTokenKind::DoubleKeyword: break; case LexTokenKind::DoubleKeyword: return "double";
case LexTokenKind::ElseKeyword: break; case LexTokenKind::ElseKeyword: return "else";
case LexTokenKind::EnumKeyword: break; case LexTokenKind::EnumKeyword: return "enum";
case LexTokenKind::ExplicitKeyword: break; case LexTokenKind::ExplicitKeyword: return "explicit";
case LexTokenKind::ExternalKeyword: break; case LexTokenKind::ExternalKeyword: return "external";
case LexTokenKind::FalseKeyword: break; case LexTokenKind::FalseKeyword: return "false";
case LexTokenKind::FinalKeyword: break; case LexTokenKind::FinalKeyword: return "final";
case LexTokenKind::FloatKeyword: break; case LexTokenKind::FloatKeyword: return "float";
case LexTokenKind::ForKeyword: break; case LexTokenKind::ForKeyword: return "for";
case LexTokenKind::FromKeyword: break; case LexTokenKind::FromKeyword: return "from";
case LexTokenKind::FuncdefKeyword: break; case LexTokenKind::FuncdefKeyword: return "funcdef";
case LexTokenKind::FunctionKeyword: break; case LexTokenKind::FunctionKeyword: return "function";
case LexTokenKind::GetKeyword: break; case LexTokenKind::GetKeyword: return "get";
case LexTokenKind::IfKeyword: break; case LexTokenKind::IfKeyword: return "if";
case LexTokenKind::ImportKeyword: break; case LexTokenKind::ImportKeyword: return "import";
case LexTokenKind::InKeyword: break; case LexTokenKind::InKeyword: return "in";
case LexTokenKind::InoutKeyword: break; case LexTokenKind::InoutKeyword: return "inout";
case LexTokenKind::IntKeyword: break; case LexTokenKind::IntKeyword: return "int";
case LexTokenKind::InterfaceKeyword: break; case LexTokenKind::InterfaceKeyword: return "interface";
case LexTokenKind::Int8Keyword: break; case LexTokenKind::Int8Keyword: return "int8";
case LexTokenKind::Int16Keyword: break; case LexTokenKind::Int16Keyword: return "int16";
case LexTokenKind::Int32Keyword: break; case LexTokenKind::Int32Keyword: return "int32";
case LexTokenKind::Int64Keyword: break; case LexTokenKind::Int64Keyword: return "int64";
case LexTokenKind::IsKeyword: break; case LexTokenKind::IsKeyword: return "is";
case LexTokenKind::MixinKeyword: break; case LexTokenKind::MixinKeyword: return "mixin";
case LexTokenKind::NamespaceKeyword: break; case LexTokenKind::NamespaceKeyword: return "namespace";
case LexTokenKind::NotKeyword: break; case LexTokenKind::NotKeyword: return "not";
case LexTokenKind::NullKeyword: break; case LexTokenKind::NullKeyword: return "null";
case LexTokenKind::OrKeyword: break; case LexTokenKind::OrKeyword: return "or";
case LexTokenKind::OutKeyword: break; case LexTokenKind::OutKeyword: return "out";
case LexTokenKind::OverrideKeyword: break; case LexTokenKind::OverrideKeyword: return "override";
case LexTokenKind::PrivateKeyword: break; case LexTokenKind::PrivateKeyword: return "private";
case LexTokenKind::PropertyKeyword: break; case LexTokenKind::PropertyKeyword: return "property";
case LexTokenKind::ProtectedKeyword: break; case LexTokenKind::ProtectedKeyword: return "protected";
case LexTokenKind::ReturnKeyword: break; case LexTokenKind::ReturnKeyword: return "return";
case LexTokenKind::SetKeyword: break; case LexTokenKind::SetKeyword: return "set";
case LexTokenKind::SharedKeyword: break; case LexTokenKind::SharedKeyword: return "shared";
case LexTokenKind::SuperKeyword: break; case LexTokenKind::SuperKeyword: return "super";
case LexTokenKind::SwitchKeyword: break; case LexTokenKind::SwitchKeyword: return "switch";
case LexTokenKind::ThisKeyword: break; case LexTokenKind::ThisKeyword: return "this";
case LexTokenKind::TrueKeyword: break; case LexTokenKind::TrueKeyword: return "true";
case LexTokenKind::TryKeyword: break; case LexTokenKind::TryKeyword: return "try";
case LexTokenKind::TypedefKeyword: break; case LexTokenKind::TypedefKeyword: return "typedef";
case LexTokenKind::UintKeyword: break; case LexTokenKind::UintKeyword: return "uint";
case LexTokenKind::Uint8Keyword: break; case LexTokenKind::Uint8Keyword: return "uint8";
case LexTokenKind::Uint16Keyword: break; case LexTokenKind::Uint16Keyword: return "uint16";
case LexTokenKind::Uint32Keyword: break; case LexTokenKind::Uint32Keyword: return "uint32";
case LexTokenKind::Uint64Keyword: break; case LexTokenKind::Uint64Keyword: return "uint64";
case LexTokenKind::VoidKeyword: break; case LexTokenKind::VoidKeyword: return "void";
case LexTokenKind::WhileKeyword: break; case LexTokenKind::WhileKeyword: return "while";
case LexTokenKind::XorKeyword: break; case LexTokenKind::XorKeyword: return "xor";
case LexTokenKind::FloatLiteral: break; case LexTokenKind::FloatLiteral: return "float";
case LexTokenKind::IntegerLiteral: break; case LexTokenKind::IntegerLiteral: return "integer";
case LexTokenKind::StringLiteral: break; case LexTokenKind::StringLiteral: return "string";
case LexTokenKind::Identifier: break; case LexTokenKind::Identifier: return "identifier";
} }
return "FAIL"; return "FAIL";
} }

View File

@ -11,7 +11,7 @@
#define EXPECT_TOKEN(token, kind) \ #define EXPECT_TOKEN(token, kind) \
if ((token)->GetKind() != LexTokenKind::kind) { \ if ((token)->GetKind() != LexTokenKind::kind) { \
logError(DiagnosticType::UnexpectedToken, (token)->GetSpan(), {}); \ logUnexpectedToken(kind, token); \
if ((token)->GetNext() != nullptr && (token)->GetNext()->GetNext() != nullptr && \ if ((token)->GetNext() != nullptr && (token)->GetNext()->GetNext() != nullptr && \
(token)->GetNext()->GetKind() == LexTokenKind::kind) { \ (token)->GetNext()->GetKind() == LexTokenKind::kind) { \
(token) = (token)->GetNext()->GetNext().get(); \ (token) = (token)->GetNext()->GetNext().get(); \
@ -21,6 +21,19 @@
} }
#define logError(type, span, formats) log(DiagnosticLevel::Error, type, span, formats) #define logError(type, span, formats) log(DiagnosticLevel::Error, type, span, formats)
#define logUnexpectedToken(expected, found) \
log(DiagnosticLevel::Error, DiagnosticType::UnexpectedToken, (found)->GetSpan(), \
{LexTokenKindHelper::ToString(LexTokenKind::expected), (found)->ToString()});
#define logUnexpectedTokenWithAnyOf(found, ...) \
std::vector<LexTokenKind> vars = {__VA_ARGS__}; \
auto expectedTokens = std::vector<std::string>(vars.size() + 1); \
for (size_t i = 0; i < vars.size(); i++) { \
expectedTokens[i] = LexTokenKindHelper::ToString(vars[i]); \
} \
expectedTokens[expectedTokens.size() - 1] = (found)->ToString(); \
log(DiagnosticLevel::Error, DiagnosticType::UnexpectedToken, (found)->GetSpan(), expectedTokens);
#define logUnexpectedTokenWithoutExpected(found) \
log(DiagnosticLevel::Error, DiagnosticType::UnexpectedToken, (found)->GetSpan(), {(found)->ToString()});
namespace MalachScript::Parser { namespace MalachScript::Parser {
using namespace Diagnostics; using namespace Diagnostics;
@ -64,8 +77,7 @@ namespace MalachScript::Parser {
auto result = ParseTypeDef(statement, current, log) || ParseClass(statement, current, log) || auto result = ParseTypeDef(statement, current, log) || ParseClass(statement, current, log) ||
ParseFunc(statement, current, log) || ParseNamespace(statement, current, log); ParseFunc(statement, current, log) || ParseNamespace(statement, current, log);
if (!result) { if (!result) {
log(DiagnosticLevel::Error, DiagnosticType::UnexpectedToken, current->GetSpan(), logUnexpectedTokenWithoutExpected(current);
{"", current->ToString()});
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
continue; continue;
} }
@ -100,7 +112,7 @@ namespace MalachScript::Parser {
// After class keyword, an identifier should always follow, if it doesn't, log an error. // After class keyword, an identifier should always follow, if it doesn't, log an error.
Identifier identifier; Identifier identifier;
if (!ParseIdentifier(identifier, current)) { if (!ParseIdentifier(identifier, current)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedToken(Identifier, current);
return false; return false;
} }
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
@ -116,21 +128,21 @@ namespace MalachScript::Parser {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
Identifier id; Identifier id;
if (!ParseIdentifier(id, current)) { if (!ParseIdentifier(id, current)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedToken(Identifier, current);
return false; return false;
} }
inherits.push_back(id); inherits.push_back(id);
while (current->GetKind() == LexTokenKind::CommaSymbol) { while (current->GetKind() == LexTokenKind::CommaSymbol) {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
if (!ParseIdentifier(id, current)) { if (!ParseIdentifier(id, current)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedToken(Identifier, current);
return false; return false;
} }
inherits.push_back(id); inherits.push_back(id);
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
} }
if (current->GetKind() != LexTokenKind::OpenCurlyParenthesisSymbol) { if (current->GetKind() != LexTokenKind::OpenCurlyParenthesisSymbol) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedToken(OpenCurlyParenthesisSymbol, current);
return false; return false;
} }
[[fallthrough]]; [[fallthrough]];
@ -148,14 +160,16 @@ namespace MalachScript::Parser {
// TODO: Sort by complexity // TODO: Sort by complexity
if (!ParseVirtProp(statement, current, log) && !ParseFunc(statement, current, log) && if (!ParseVirtProp(statement, current, log) && !ParseFunc(statement, current, log) &&
!ParseVar(statement, current, log) && !ParseFuncDef(statement, current, log)) { !ParseVar(statement, current, log) && !ParseFuncDef(statement, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
break; break;
} }
body.push_back(statement.TakeOwnership()); body.push_back(statement.TakeOwnership());
} }
break; break;
} }
default: logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); return false; default:
logUnexpectedTokenWithAnyOf(current, LexTokenKind::SemicolonSymbol, LexTokenKind::ColonSymbol,
LexTokenKind::OpenCurlyParenthesisSymbol);
} }
out = new ParsedClassStatement(TextSpan(start, current->GetSpan().GetEnd()), classAttr, identifier, inherits, out = new ParsedClassStatement(TextSpan(start, current->GetSpan().GetEnd()), classAttr, identifier, inherits,
body); body);
@ -172,13 +186,13 @@ namespace MalachScript::Parser {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
Identifier defineFrom; Identifier defineFrom;
if (!ParsePrimType(defineFrom, current, log) && !ParseIdentifier(defineFrom, current)) { if (!ParsePrimType(defineFrom, current, log) && !ParseIdentifier(defineFrom, current)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
} }
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
Identifier defineTo; Identifier defineTo;
if (!ParseIdentifier(defineTo, current)) { if (!ParseIdentifier(defineTo, current)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedToken(Identifier, current);
} }
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
EXPECT_TOKEN(current, SemicolonSymbol); EXPECT_TOKEN(current, SemicolonSymbol);
@ -195,14 +209,14 @@ namespace MalachScript::Parser {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
Identifier identifier; Identifier identifier;
if (!ParseIdentifier(identifier, current)) { if (!ParseIdentifier(identifier, current)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedToken(Identifier, current);
} else { } else {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
} }
EXPECT_TOKEN(current, OpenCurlyParenthesisSymbol); EXPECT_TOKEN(current, OpenCurlyParenthesisSymbol);
ScopedPtr<const ParsedStatement> script = nullptr; ScopedPtr<const ParsedStatement> script = nullptr;
if (!ParseScript(script, current, log)) { if (!ParseScript(script, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
} }
auto end = current->GetSpan().GetEnd(); auto end = current->GetSpan().GetEnd();
EXPECT_TOKEN(current, CloseCurlyParenthesisSymbol); EXPECT_TOKEN(current, CloseCurlyParenthesisSymbol);
@ -268,7 +282,7 @@ namespace MalachScript::Parser {
ScopedPtr<const ParsedStatement> statblock = nullptr; ScopedPtr<const ParsedStatement> statblock = nullptr;
if (current->GetKind() != LexTokenKind::SemicolonSymbol) { if (current->GetKind() != LexTokenKind::SemicolonSymbol) {
if (!ParseStatBlock(statblock, current, log)) { if (!ParseStatBlock(statblock, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
} else { } else {
@ -282,39 +296,39 @@ namespace MalachScript::Parser {
} }
bool Parser::ParseType(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken, const log_func& log) { bool Parser::ParseType(ScopedPtr<const ParsedStatement>& out, const LexToken*& currentToken, const log_func& log) {
const auto* token = currentToken; const auto* current = currentToken;
auto start = token->GetSpan().GetStart(); auto start = current->GetSpan().GetStart();
bool isConst = false; bool isConst = false;
bool isArray = false; bool isArray = false;
bool isHandle = false; bool isHandle = false;
if (token->GetKind() == LexTokenKind::ConstKeyword) { if (current->GetKind() == LexTokenKind::ConstKeyword) {
isConst = true; isConst = true;
PROGRESS_TOKEN(token); PROGRESS_TOKEN(current);
} }
ScopedIdentifier scopedIdentifier; ScopedIdentifier scopedIdentifier;
ParseScope(scopedIdentifier.GetScope(), token, log); ParseScope(scopedIdentifier.GetScope(), current, log);
if (!ParseDataType(scopedIdentifier.GetIdentifier(), token, log)) { if (!ParseDataType(scopedIdentifier.GetIdentifier(), current, log)) {
return false; return false;
} }
// TODO: Generics. // TODO: Generics.
if (token->GetKind() == LexTokenKind::OpenBlockParenthesisSymbol) { if (current->GetKind() == LexTokenKind::OpenBlockParenthesisSymbol) {
PROGRESS_TOKEN(token); PROGRESS_TOKEN(current);
if (token->GetKind() != LexTokenKind::CloseBlockParenthesisSymbol) { if (current->GetKind() != LexTokenKind::CloseBlockParenthesisSymbol) {
logError(DiagnosticType::UnexpectedToken, token->GetSpan(), {}); logUnexpectedToken(CloseCurlyParenthesisSymbol, current);
} else { } else {
PROGRESS_TOKEN(token); PROGRESS_TOKEN(current);
isArray = true; isArray = true;
} }
} else if (token->GetKind() == LexTokenKind::AtSymbol) { } else if (current->GetKind() == LexTokenKind::AtSymbol) {
isHandle = true; isHandle = true;
PROGRESS_TOKEN(token); PROGRESS_TOKEN(current);
if (token->GetKind() == LexTokenKind::ConstKeyword) { if (current->GetKind() == LexTokenKind::ConstKeyword) {
isConst = true; isConst = true;
PROGRESS_TOKEN(token); PROGRESS_TOKEN(current);
} }
} }
auto end = token->GetSpan().GetEnd(); auto end = current->GetSpan().GetEnd();
currentToken = token; currentToken = current;
out = new ParsedTypeStatement(TextSpan(start, end), isConst, isArray, isHandle, scopedIdentifier); out = new ParsedTypeStatement(TextSpan(start, end), isConst, isArray, isHandle, scopedIdentifier);
return true; return true;
} }
@ -348,7 +362,7 @@ namespace MalachScript::Parser {
break; break;
} }
} else { } else {
logError(DiagnosticType::UnexpectedToken, currentToken->GetSpan(), {}); logUnexpectedToken(Identifier, current);
break; break;
} }
} }
@ -412,11 +426,11 @@ namespace MalachScript::Parser {
const ParsedStatement* defaultExpression = nullptr; const ParsedStatement* defaultExpression = nullptr;
if (!ParseType(typeStatement, currentToken, log)) { if (!ParseType(typeStatement, currentToken, log)) {
logError(DiagnosticType::UnexpectedToken, currentToken->GetSpan(), {}); logUnexpectedTokenWithoutExpected(currentToken);
} }
ParseTypeMod(typeMod, currentToken, log); ParseTypeMod(typeMod, currentToken, log);
if (!ParseIdentifier(identifier, currentToken)) { if (!ParseIdentifier(identifier, currentToken)) {
logError(DiagnosticType::UnexpectedToken, currentToken->GetSpan(), {}); logUnexpectedToken(Identifier, currentToken);
} else { } else {
PROGRESS_TOKEN(currentToken); PROGRESS_TOKEN(currentToken);
} }
@ -503,7 +517,7 @@ namespace MalachScript::Parser {
ParseFuncAttr(getAttr, current, log); ParseFuncAttr(getAttr, current, log);
if (current->GetKind() != LexTokenKind::SemicolonSymbol) { if (current->GetKind() != LexTokenKind::SemicolonSymbol) {
if (!ParseStatBlock(getStatement, current, log)) { if (!ParseStatBlock(getStatement, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
} }
} else { } else {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
@ -522,7 +536,7 @@ namespace MalachScript::Parser {
ParseFuncAttr(setAttr, current, log); ParseFuncAttr(setAttr, current, log);
if (current->GetKind() != LexTokenKind::SemicolonSymbol) { if (current->GetKind() != LexTokenKind::SemicolonSymbol) {
if (!ParseStatBlock(setStatement, current, log)) { if (!ParseStatBlock(setStatement, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
} }
} else { } else {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
@ -537,7 +551,7 @@ namespace MalachScript::Parser {
} }
} }
if (current->GetKind() != LexTokenKind::CloseCurlyParenthesisSymbol) { if (current->GetKind() != LexTokenKind::CloseCurlyParenthesisSymbol) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedToken(CloseCurlyParenthesisSymbol, current);
} else { } else {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
} }
@ -560,18 +574,18 @@ namespace MalachScript::Parser {
EXPECT_TOKEN(current, OpenParenthesisSymbol); EXPECT_TOKEN(current, OpenParenthesisSymbol);
ScopedPtr<const ParsedStatement> condition = nullptr; ScopedPtr<const ParsedStatement> condition = nullptr;
if (!ParseAssign(condition, current, log)) { if (!ParseAssign(condition, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
} }
EXPECT_TOKEN(current, CloseParenthesisSymbol); EXPECT_TOKEN(current, CloseParenthesisSymbol);
ScopedPtr<const ParsedStatement> body = nullptr; ScopedPtr<const ParsedStatement> body = nullptr;
if (!ParseStatement(body, current, log)) { if (!ParseStatement(body, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
} }
ScopedPtr<const ParsedStatement> elseStatement = nullptr; ScopedPtr<const ParsedStatement> elseStatement = nullptr;
if (current->GetKind() == LexTokenKind::ElseKeyword) { if (current->GetKind() == LexTokenKind::ElseKeyword) {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
if (!ParseStatement(elseStatement, current, log)) { if (!ParseStatement(elseStatement, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
} }
} }
out = new ParsedIfStatement(TextSpan(currentToken->GetSpan().GetStart(), current->GetSpan().GetEnd()), out = new ParsedIfStatement(TextSpan(currentToken->GetSpan().GetStart(), current->GetSpan().GetEnd()),
@ -649,7 +663,7 @@ namespace MalachScript::Parser {
if (current->GetKind() == LexTokenKind::CloseCurlyParenthesisSymbol) { if (current->GetKind() == LexTokenKind::CloseCurlyParenthesisSymbol) {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
} else { } else {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedToken(CloseCurlyParenthesisSymbol, current);
} }
out = new ParsedStatBlockStatement(TextSpan(currentToken->GetSpan().GetStart(), current->GetSpan().GetEnd()), out = new ParsedStatBlockStatement(TextSpan(currentToken->GetSpan().GetStart(), current->GetSpan().GetEnd()),
@ -719,7 +733,7 @@ namespace MalachScript::Parser {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
ScopedPtr<const ParsedStatement> rightHand = nullptr; ScopedPtr<const ParsedStatement> rightHand = nullptr;
if (!ParseAssign(rightHand, current, log)) { if (!ParseAssign(rightHand, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
out = leftHand.TakeOwnership(); out = leftHand.TakeOwnership();
currentToken = current; currentToken = current;
return true; return true;
@ -743,7 +757,7 @@ namespace MalachScript::Parser {
PROGRESS_TOKEN(current); \ PROGRESS_TOKEN(current); \
ScopedPtr<const ParsedStatement> rightHand = nullptr; \ ScopedPtr<const ParsedStatement> rightHand = nullptr; \
if (!ParseExprTerm(rightHand, current, log)) { \ if (!ParseExprTerm(rightHand, current, log)) { \
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); \ logUnexpectedTokenWithoutExpected(current); \
out = leftHand.TakeOwnership(); \ out = leftHand.TakeOwnership(); \
currentToken = current; \ currentToken = current; \
return true; \ return true; \
@ -828,7 +842,7 @@ namespace MalachScript::Parser {
if (current->GetKind() == LexTokenKind::OpenParenthesisSymbol) { if (current->GetKind() == LexTokenKind::OpenParenthesisSymbol) {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
if (!ParseAssign(out, current, log)) { if (!ParseAssign(out, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
} }
EXPECT_TOKEN(current, CloseParenthesisSymbol); EXPECT_TOKEN(current, CloseParenthesisSymbol);
currentToken = current; currentToken = current;
@ -942,12 +956,12 @@ namespace MalachScript::Parser {
EXPECT_TOKEN(current, OpenParenthesisSymbol); EXPECT_TOKEN(current, OpenParenthesisSymbol);
ScopedPtr<const ParsedStatement> init; ScopedPtr<const ParsedStatement> init;
if (!ParseVar(init, current, log) && !ParseExprStat(init, current, log)) { if (!ParseVar(init, current, log) && !ParseExprStat(init, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
ScopedPtr<const ParsedStatement> condition; ScopedPtr<const ParsedStatement> condition;
if (!ParseExprStat(condition, current, log)) { if (!ParseExprStat(condition, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
std::vector<const ParsedStatement*> increment; std::vector<const ParsedStatement*> increment;
@ -963,7 +977,7 @@ namespace MalachScript::Parser {
EXPECT_TOKEN(current, CloseParenthesisSymbol); EXPECT_TOKEN(current, CloseParenthesisSymbol);
ScopedPtr<const ParsedStatement> statementBlock; ScopedPtr<const ParsedStatement> statementBlock;
if (!ParseStatement(statementBlock, current, log)) { if (!ParseStatement(statementBlock, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
@ -984,13 +998,13 @@ namespace MalachScript::Parser {
EXPECT_TOKEN(current, OpenParenthesisSymbol); EXPECT_TOKEN(current, OpenParenthesisSymbol);
ScopedPtr<const ParsedStatement> condition = nullptr; ScopedPtr<const ParsedStatement> condition = nullptr;
if (!ParseAssign(condition, current, log)) { if (!ParseAssign(condition, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
EXPECT_TOKEN(current, CloseParenthesisSymbol); EXPECT_TOKEN(current, CloseParenthesisSymbol);
ScopedPtr<const ParsedStatement> body = nullptr; ScopedPtr<const ParsedStatement> body = nullptr;
if (!ParseStatement(body, current, log)) { if (!ParseStatement(body, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
out = new ParsedWhileStatement(TextSpan(currentToken->GetSpan().GetStart(), current->GetSpan().GetEnd()), out = new ParsedWhileStatement(TextSpan(currentToken->GetSpan().GetStart(), current->GetSpan().GetEnd()),
@ -1007,14 +1021,14 @@ namespace MalachScript::Parser {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
ScopedPtr<const ParsedStatement> body = nullptr; ScopedPtr<const ParsedStatement> body = nullptr;
if (!ParseStatement(body, current, log)) { if (!ParseStatement(body, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
EXPECT_TOKEN(current, WhileKeyword); EXPECT_TOKEN(current, WhileKeyword);
EXPECT_TOKEN(current, OpenParenthesisSymbol); EXPECT_TOKEN(current, OpenParenthesisSymbol);
ScopedPtr<const ParsedStatement> condition = nullptr; ScopedPtr<const ParsedStatement> condition = nullptr;
if (!ParseAssign(condition, current, log)) { if (!ParseAssign(condition, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
EXPECT_TOKEN(current, CloseParenthesisSymbol); EXPECT_TOKEN(current, CloseParenthesisSymbol);
@ -1034,13 +1048,13 @@ namespace MalachScript::Parser {
ScopedPtr<const ParsedStatement> tryStatement = nullptr; ScopedPtr<const ParsedStatement> tryStatement = nullptr;
if (!ParseStatBlock(tryStatement, current, log)) { if (!ParseStatBlock(tryStatement, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
EXPECT_TOKEN(current, CatchKeyword); EXPECT_TOKEN(current, CatchKeyword);
ScopedPtr<const ParsedStatement> catchStatement = nullptr; ScopedPtr<const ParsedStatement> catchStatement = nullptr;
if (!ParseStatBlock(catchStatement, current, log)) { if (!ParseStatBlock(catchStatement, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
@ -1059,7 +1073,7 @@ namespace MalachScript::Parser {
EXPECT_TOKEN(currentToken, OpenParenthesisSymbol); EXPECT_TOKEN(currentToken, OpenParenthesisSymbol);
ScopedPtr<const ParsedStatement> expression = nullptr; ScopedPtr<const ParsedStatement> expression = nullptr;
if (!ParseExpr(expression, current, log)) { if (!ParseExpr(expression, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
EXPECT_TOKEN(currentToken, CloseParenthesisSymbol); EXPECT_TOKEN(currentToken, CloseParenthesisSymbol);
@ -1080,7 +1094,7 @@ namespace MalachScript::Parser {
} }
caseStatements.push_back(stat.TakeOwnership()); caseStatements.push_back(stat.TakeOwnership());
} else { } else {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedToken(CaseKeyword, current);
return false; return false;
} }
} }
@ -1106,7 +1120,7 @@ namespace MalachScript::Parser {
ScopedPtr<const ParsedStatement> expression = nullptr; ScopedPtr<const ParsedStatement> expression = nullptr;
if (!isDefault) { if (!isDefault) {
if (!ParseExpr(expression, current, log)) { if (!ParseExpr(expression, current, log)) {
logError(DiagnosticType::UnexpectedToken, current->GetSpan(), {}); logUnexpectedTokenWithoutExpected(current);
return false; return false;
} }
} }