Support for diagnostics system.

This commit is contained in:
2020-10-04 19:38:13 +02:00
parent 20976010d6
commit b6a5e047c2
13 changed files with 347 additions and 217 deletions

View File

@@ -5,8 +5,10 @@ using namespace ElohimScript::Parser;
#define KEYWORD_TEST(script, symbol) \
TEST_CASE("Lex " script) { \
auto lexer = Lexer(script); \
ElohimScript::Diagnostics::Diagnostics diag; \
auto lexer = Lexer(script, &diag); \
const auto* token = lexer.Lex(); \
CHECK(diag.GetMessages().empty()); \
CHECK(token->GetKind() == LexTokenKind::symbol); \
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile); \
delete token; \
@@ -91,8 +93,10 @@ namespace doctest {
#define IDENTIFIER_TEST(identifier) \
TEST_CASE("Lex identifier " identifier) { \
auto lexer = Lexer(identifier); \
ElohimScript::Diagnostics::Diagnostics diag; \
auto lexer = Lexer(identifier, &diag); \
const auto* token = lexer.Lex(); \
CHECK(diag.GetMessages().empty()); \
REQUIRE(token->GetKind() == LexTokenKind::Identifier); \
auto value = ((IdentifierToken*)token)->GetValue(); \
CHECK(value == std::u8string(reinterpret_cast<const char8_t*>(identifier))); \

View File

@@ -5,8 +5,10 @@ using namespace ElohimScript::Parser;
#define INTEGER_TEST(script, expected) \
TEST_CASE("Lex " script) { \
auto lexer = Lexer(script); \
ElohimScript::Diagnostics::Diagnostics diag; \
auto lexer = Lexer(script, &diag); \
const auto* token = lexer.Lex(); \
CHECK(diag.GetMessages().empty()); \
REQUIRE(token->GetKind() == LexTokenKind::IntegerLiteral); \
auto value = ((const IntegerLiteral*)token)->GetValue(); \
CHECK(value == (expected)); \
@@ -16,8 +18,10 @@ using namespace ElohimScript::Parser;
#define FLOAT_TEST(script, expected) \
TEST_CASE("Lex " script) { \
auto lexer = Lexer(script); \
ElohimScript::Diagnostics::Diagnostics diag; \
auto lexer = Lexer(script, &diag); \
const auto* token = lexer.Lex(); \
CHECK(diag.GetMessages().empty()); \
REQUIRE(token->GetKind() == LexTokenKind::FloatLiteral); \
auto value = ((const FloatLiteral*)token)->GetValue(); \
CHECK(value == (expected)); \
@@ -63,4 +67,16 @@ INTEGER_TEST("0b1111", 15);
INTEGER_TEST("0b110011", 51);
#undef INTEGER_TEST
#undef FLOAT_TEST
#undef FLOAT_TEST
TEST_CASE("Lex invalid numerical base") {
ElohimScript::Diagnostics::Diagnostics diag;
auto lexer = Lexer("0f553", &diag);
const auto* token = lexer.Lex();
const auto& messages = diag.GetMessages();
REQUIRE(messages.size() == 1);
CHECK(messages[0].GetType() == ElohimScript::Diagnostics::DiagnosticType::InvalidNumericalBase);
CHECK(messages[0].GetLevel() == ElohimScript::Diagnostics::DiagnosticLevel::Error);
CHECK(messages[0].GetSpan() == ElohimScript::TextSpan(0, 2));
delete token;
}

View File

@@ -5,8 +5,10 @@ using namespace ElohimScript::Parser;
#define STRING_TEST(str, constraint) \
TEST_CASE("Lex string " constraint str constraint) { \
auto lexer = Lexer(constraint str constraint); \
ElohimScript::Diagnostics::Diagnostics diag; \
auto lexer = Lexer(constraint str constraint, &diag); \
const auto* token = lexer.Lex(); \
CHECK(diag.GetMessages().empty()); \
REQUIRE(token->GetKind() == LexTokenKind::StringLiteral); \
auto value = ((const StringLiteral*)token)->GetValue(); \
CHECK(value == std::u8string(reinterpret_cast<const char8_t*>(str))); \
@@ -21,9 +23,12 @@ STRING_TEST("\"foo bar\"", "\"\"\"");
STRING_TEST("\"\"foo bar\"\"", "\"\"\"");
TEST_CASE("Lex multiline string") {
ElohimScript::Diagnostics::Diagnostics diag;
auto lexer = Lexer(R"("""foo
bar""")");
bar""")",
&diag);
const auto* token = lexer.Lex();
CHECK(diag.GetMessages().empty());
REQUIRE(token->GetKind() == LexTokenKind::StringLiteral);
auto value = (dynamic_cast<const StringLiteral*>(token))->GetValue();
CHECK(value == std::u8string(reinterpret_cast<const char8_t*>(R"(foo

View File

@@ -6,8 +6,10 @@ using namespace ElohimScript::Parser;
#define SYMBOL_TEST(script, symbol) \
TEST_CASE("Lex " script) { \
auto lexer = Lexer(script); \
ElohimScript::Diagnostics::Diagnostics diag; \
auto lexer = Lexer(script, &diag); \
const auto* token = lexer.Lex(); \
CHECK(diag.GetMessages().empty()); \
CHECK(token->GetKind() == LexTokenKind::symbol); \
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile); \
delete token; \
@@ -71,9 +73,11 @@ SYMBOL_TEST(" ", Whitespace)
TEST_CASE("Lex whitespace") {
auto whitespace = {" ", "\t", "\n", "\r", "\xef\xbb\xbf"};
for (auto v : whitespace) {
auto lexer = Lexer(v);
for (const auto *v : whitespace) {
ElohimScript::Diagnostics::Diagnostics diag;
auto lexer = Lexer(v, &diag);
const auto* token = lexer.Lex();
CHECK(diag.GetMessages().empty());
CHECK(token->GetKind() == LexTokenKind::Whitespace);
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile);
delete token;