2020-10-04 14:33:12 +00:00
|
|
|
#include "../../extern/doctest.hpp"
|
|
|
|
#include "../../src/Parser/Lexer/Lexer.hpp"
|
|
|
|
|
2020-10-05 15:45:00 +00:00
|
|
|
using namespace MalachScript::Parser;
|
2020-10-04 14:33:12 +00:00
|
|
|
|
|
|
|
#define INTEGER_TEST(script, expected) \
|
|
|
|
TEST_CASE("Lex " script) { \
|
2021-01-01 22:31:30 +00:00
|
|
|
MalachScript::Diagnostics::Logger diag; \
|
2020-10-08 16:15:48 +00:00
|
|
|
auto lexer = Lexer(u8##script, u8##script, &diag); \
|
2020-10-04 14:33:12 +00:00
|
|
|
const auto* token = lexer.Lex(); \
|
2020-10-04 17:38:13 +00:00
|
|
|
CHECK(diag.GetMessages().empty()); \
|
2020-10-04 15:15:28 +00:00
|
|
|
REQUIRE(token->GetKind() == LexTokenKind::IntegerLiteral); \
|
|
|
|
auto value = ((const IntegerLiteral*)token)->GetValue(); \
|
2020-10-04 14:33:12 +00:00
|
|
|
CHECK(value == (expected)); \
|
|
|
|
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile); \
|
|
|
|
}
|
|
|
|
|
2020-10-04 15:15:28 +00:00
|
|
|
#define FLOAT_TEST(script, expected) \
|
2020-10-04 14:33:12 +00:00
|
|
|
TEST_CASE("Lex " script) { \
|
2021-01-01 22:31:30 +00:00
|
|
|
MalachScript::Diagnostics::Logger diag; \
|
2020-10-08 16:15:48 +00:00
|
|
|
auto lexer = Lexer(u8##script, u8##script, &diag); \
|
2020-10-04 14:33:12 +00:00
|
|
|
const auto* token = lexer.Lex(); \
|
2020-10-04 17:38:13 +00:00
|
|
|
CHECK(diag.GetMessages().empty()); \
|
2020-10-04 15:15:28 +00:00
|
|
|
REQUIRE(token->GetKind() == LexTokenKind::FloatLiteral); \
|
|
|
|
auto value = ((const FloatLiteral*)token)->GetValue(); \
|
2020-10-04 14:33:12 +00:00
|
|
|
CHECK(value == (expected)); \
|
|
|
|
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile); \
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decimal lexing
|
|
|
|
INTEGER_TEST("123456", 123456);
|
|
|
|
INTEGER_TEST("0d123456", 123456);
|
2020-10-04 21:16:37 +00:00
|
|
|
INTEGER_TEST("0D123456", 123456);
|
2020-10-04 14:33:12 +00:00
|
|
|
INTEGER_TEST("50000000000", 50000000000);
|
|
|
|
|
|
|
|
// Decimal float lexing
|
2020-11-08 14:41:18 +00:00
|
|
|
FLOAT_TEST("123.456", 123.456L);
|
|
|
|
FLOAT_TEST("0.456", 0.456L);
|
|
|
|
FLOAT_TEST("0.456e12", 0.456e12L);
|
|
|
|
FLOAT_TEST("0.456E12", 0.456E12L);
|
2020-10-04 14:33:12 +00:00
|
|
|
|
|
|
|
// Hexadecimal lexing
|
|
|
|
INTEGER_TEST("0x0", 0);
|
|
|
|
INTEGER_TEST("0xF", 15);
|
|
|
|
INTEGER_TEST("0xf", 15);
|
|
|
|
INTEGER_TEST("0xFF", 255);
|
|
|
|
INTEGER_TEST("0xfF", 255);
|
|
|
|
INTEGER_TEST("0xFFF", 4095);
|
|
|
|
INTEGER_TEST("0xFFFF", 65535);
|
|
|
|
INTEGER_TEST("0xFFFFF", 1048575);
|
|
|
|
INTEGER_TEST("0xFFFFFF", 16777215);
|
2020-10-04 21:16:37 +00:00
|
|
|
INTEGER_TEST("0XFFFFFF", 16777215);
|
2020-10-04 14:33:12 +00:00
|
|
|
|
|
|
|
// Octal lexing
|
|
|
|
INTEGER_TEST("0o0", 0);
|
|
|
|
INTEGER_TEST("0o7", 7);
|
|
|
|
INTEGER_TEST("0o77", 63);
|
|
|
|
INTEGER_TEST("0o777", 511);
|
|
|
|
INTEGER_TEST("0o7777", 4095);
|
2020-10-04 21:16:37 +00:00
|
|
|
INTEGER_TEST("0O7777", 4095);
|
2020-10-04 14:33:12 +00:00
|
|
|
|
|
|
|
// Binary lexing
|
|
|
|
INTEGER_TEST("0b0", 0);
|
|
|
|
INTEGER_TEST("0b1", 1);
|
|
|
|
INTEGER_TEST("0b11", 3);
|
|
|
|
INTEGER_TEST("0b111", 7);
|
|
|
|
INTEGER_TEST("0b1111", 15);
|
2020-10-04 15:15:28 +00:00
|
|
|
INTEGER_TEST("0b110011", 51);
|
2020-10-04 21:16:37 +00:00
|
|
|
INTEGER_TEST("0B110011", 51);
|
2020-10-04 15:15:28 +00:00
|
|
|
|
|
|
|
#undef INTEGER_TEST
|
2020-10-04 17:38:13 +00:00
|
|
|
#undef FLOAT_TEST
|
|
|
|
|
|
|
|
TEST_CASE("Lex invalid numerical base") {
|
2021-01-01 22:31:30 +00:00
|
|
|
MalachScript::Diagnostics::Logger diag;
|
2020-10-08 16:15:48 +00:00
|
|
|
auto lexer = Lexer(u8"bad base", u8"0f553", &diag);
|
2020-10-04 20:21:20 +00:00
|
|
|
lexer.Lex();
|
2020-10-04 17:38:13 +00:00
|
|
|
const auto& messages = diag.GetMessages();
|
|
|
|
REQUIRE(messages.size() == 1);
|
2020-10-05 15:45:00 +00:00
|
|
|
CHECK(messages[0].GetType() == MalachScript::Diagnostics::DiagnosticType::InvalidNumericalBase);
|
|
|
|
CHECK(messages[0].GetLevel() == MalachScript::Diagnostics::DiagnosticLevel::Error);
|
|
|
|
CHECK(messages[0].GetSpan() == MalachScript::TextSpan(0, 2));
|
2020-10-05 10:18:05 +00:00
|
|
|
CHECK(messages[0].GetScriptName() == u8"bad base");
|
2020-10-04 17:38:13 +00:00
|
|
|
}
|