Fixes for tests, support for comments.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-01-09 13:43:29 +01:00
parent 59af34fac9
commit 0fbca3f01e
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
7 changed files with 153 additions and 133 deletions

View File

@ -9,6 +9,7 @@ namespace MalachScript::Parser {
Unknown,
EndOfFile,
Whitespace,
Comment,
// Symbols
StarSymbol,
@ -265,6 +266,7 @@ namespace MalachScript::Parser {
case LexTokenKind::IntegerLiteral: return "integer";
case LexTokenKind::StringLiteral: return "string";
case LexTokenKind::Identifier: return "identifier";
case LexTokenKind::Comment: return "Comment";
}
return "FAIL";
}

View File

@ -57,6 +57,25 @@ namespace MalachScript::Parser {
// /=
return Create<LexTokenImpl<LexTokenKind::SlashEqualsSymbol>>(
ScriptTextSpan(start, start + 2, _scriptName));
} else if (Peek() == '/') {
Progress();
while (true) {
auto next = Consume();
if (next == '\n' || next == '\0') {
return Create<LexTokenImpl<LexTokenKind::Comment>>(
ScriptTextSpan(start, _position, _scriptName));
}
}
} else if (Peek() == '*') {
Progress();
while (true) {
auto next = Consume();
if ((next == '*' && Peek() == '/') || next == '\0') {
Progress();
return Create<LexTokenImpl<LexTokenKind::Comment>>(
ScriptTextSpan(start, _position, _scriptName));
}
}
}
// /
return Create<LexTokenImpl<LexTokenKind::SlashSymbol>>(ScriptTextSpan(start, start + 1, _scriptName));

View File

@ -5,7 +5,7 @@
#define PROGRESS_TOKEN(token) \
token = (token)->GetNext().get(); \
while ((token)->GetKind() == LexTokenKind::Whitespace) { \
while ((token)->GetKind() == LexTokenKind::Whitespace || (token)->GetKind() == LexTokenKind::Comment) { \
(token) = (token)->GetNext().get(); \
}
@ -57,7 +57,7 @@ namespace MalachScript::Parser {
size_t currentAmount = 0;
const auto* current = currentToken;
while (true) {
while (current->GetKind() == LexTokenKind::Whitespace) {
while (current->GetKind() == LexTokenKind::Whitespace || current->GetKind() == LexTokenKind::Comment) {
current = current->GetNext().get();
}
if (current->GetKind() == LexTokenKind::EndOfFile) {

View File

@ -13,7 +13,7 @@ using namespace MalachScript;
vec[i]->SetNext(vec[i + 1]); \
} \
Diagnostics::Logger diags; \
auto* script = Parser::Parser::Parse(vec.front(), "scriptname", &diags); \
auto* script = Parser::Parser::Parse(vec.front(), &diags); \
REQUIRE(diags.GetMessages().empty()); \
asserts; \
delete vec[0]; \
@ -21,6 +21,7 @@ using namespace MalachScript;
}
#define PARSER_TEST_TOKENS(...) __VA_ARGS__
#define TextSpan(a, b) ScriptTextSpan(a, b, "")
PARSER_TEST("Parse basic class without body",
PARSER_TEST_TOKENS(new Parser::LexTokenImpl<Parser::LexTokenKind::ClassKeyword>(TextSpan(0, 0)),

View File

@ -13,7 +13,7 @@ using namespace MalachScript;
vec[i]->SetNext(vec[i + 1]); \
} \
Diagnostics::Logger diags; \
auto* script = Parser::Parser::Parse(vec.front(), "scriptname", &diags); \
auto* script = Parser::Parser::Parse(vec.front(), &diags); \
REQUIRE(diags.GetMessages().empty()); \
asserts; \
delete vec[0]; \
@ -21,6 +21,7 @@ using namespace MalachScript;
}
#define PARSER_TEST_TOKENS(...) __VA_ARGS__
#define TextSpan(a, b) ScriptTextSpan(a, b, "")
PARSER_TEST("Parse ``void foobar();``",
PARSER_TEST_TOKENS(new Parser::LexTokenImpl<Parser::LexTokenKind::VoidKeyword>(TextSpan(0, 0)),

View File

@ -9,7 +9,7 @@ using namespace MalachScript;
Diagnostics::Logger diags; \
auto lexer = Parser::Lexer(name, scriptText, &diags); \
auto token = lexer.Lex(); \
auto script = Parser::Parser::Parse(token, name, &diags); \
auto script = Parser::Parser::Parse(token, &diags); \
asserts; \
delete script; \
}

View File

@ -13,7 +13,7 @@ using namespace MalachScript;
vec[i]->SetNext(vec[i + 1]); \
} \
Diagnostics::Logger diags; \
auto* script = Parser::Parser::Parse(vec.front(), "scriptname", &diags); \
auto* script = Parser::Parser::Parse(vec.front(), &diags); \
REQUIRE(diags.GetMessages().empty()); \
asserts; \
delete vec[0]; \
@ -21,14 +21,14 @@ using namespace MalachScript;
}
#define PARSER_TEST_TOKENS(...) __VA_ARGS__
#define TextSpan(a, b) ScriptTextSpan(a, b, "")
PARSER_TEST(
"Parse class foobar { bool foo { get; set; } }",
PARSER_TEST_TOKENS(new Parser::LexTokenImpl<Parser::LexTokenKind::ClassKeyword>(TextSpan(0, 0)),
PARSER_TEST("Parse class foobar { bool foo { get; set; } }",
PARSER_TEST_TOKENS(
new Parser::LexTokenImpl<Parser::LexTokenKind::ClassKeyword>(TextSpan(0, 0)),
new Parser::IdentifierToken(TextSpan(0, 0), "foobar"),
new Parser::LexTokenImpl<Parser::LexTokenKind::OpenCurlyParenthesisSymbol>(TextSpan(0, 0)),
new Parser::IdentifierToken(TextSpan(0, 0), "bool"),
new Parser::IdentifierToken(TextSpan(0, 0), "foo"),
new Parser::IdentifierToken(TextSpan(0, 0), "bool"), new Parser::IdentifierToken(TextSpan(0, 0), "foo"),
new Parser::LexTokenImpl<Parser::LexTokenKind::OpenCurlyParenthesisSymbol>(TextSpan(0, 0)),
new Parser::LexTokenImpl<Parser::LexTokenKind::GetKeyword>(TextSpan(0, 0)),
new Parser::LexTokenImpl<Parser::LexTokenKind::SemicolonSymbol>(TextSpan(0, 0)),
@ -55,13 +55,12 @@ PARSER_TEST(
REQUIRE(virtPropStatement->GetSetStatement() == nullptr);
})
PARSER_TEST(
"Parse class foobar { bool foo { get const; set const; } }",
PARSER_TEST_TOKENS(new Parser::LexTokenImpl<Parser::LexTokenKind::ClassKeyword>(TextSpan(0, 0)),
PARSER_TEST("Parse class foobar { bool foo { get const; set const; } }",
PARSER_TEST_TOKENS(
new Parser::LexTokenImpl<Parser::LexTokenKind::ClassKeyword>(TextSpan(0, 0)),
new Parser::IdentifierToken(TextSpan(0, 0), "foobar"),
new Parser::LexTokenImpl<Parser::LexTokenKind::OpenCurlyParenthesisSymbol>(TextSpan(0, 0)),
new Parser::IdentifierToken(TextSpan(0, 0), "bool"),
new Parser::IdentifierToken(TextSpan(0, 0), "foo"),
new Parser::IdentifierToken(TextSpan(0, 0), "bool"), new Parser::IdentifierToken(TextSpan(0, 0), "foo"),
new Parser::LexTokenImpl<Parser::LexTokenKind::OpenCurlyParenthesisSymbol>(TextSpan(0, 0)),
new Parser::LexTokenImpl<Parser::LexTokenKind::GetKeyword>(TextSpan(0, 0)),
new Parser::LexTokenImpl<Parser::LexTokenKind::ConstKeyword>(TextSpan(0, 0)),
@ -90,13 +89,12 @@ PARSER_TEST(
REQUIRE(virtPropStatement->GetSetStatement() == nullptr);
})
PARSER_TEST(
"Parse class foobar { bool foo { get const override; set const override; } }",
PARSER_TEST_TOKENS(new Parser::LexTokenImpl<Parser::LexTokenKind::ClassKeyword>(TextSpan(0, 0)),
PARSER_TEST("Parse class foobar { bool foo { get const override; set const override; } }",
PARSER_TEST_TOKENS(
new Parser::LexTokenImpl<Parser::LexTokenKind::ClassKeyword>(TextSpan(0, 0)),
new Parser::IdentifierToken(TextSpan(0, 0), "foobar"),
new Parser::LexTokenImpl<Parser::LexTokenKind::OpenCurlyParenthesisSymbol>(TextSpan(0, 0)),
new Parser::IdentifierToken(TextSpan(0, 0), "bool"),
new Parser::IdentifierToken(TextSpan(0, 0), "foo"),
new Parser::IdentifierToken(TextSpan(0, 0), "bool"), new Parser::IdentifierToken(TextSpan(0, 0), "foo"),
new Parser::LexTokenImpl<Parser::LexTokenKind::OpenCurlyParenthesisSymbol>(TextSpan(0, 0)),
new Parser::LexTokenImpl<Parser::LexTokenKind::GetKeyword>(TextSpan(0, 0)),
new Parser::LexTokenImpl<Parser::LexTokenKind::ConstKeyword>(TextSpan(0, 0)),
@ -143,8 +141,7 @@ PARSER_TEST(
// }
//}
PARSER_TEST(
"Virtprops with bodies",
PARSER_TEST("Virtprops with bodies",
PARSER_TEST_TOKENS(
new Parser::LexTokenImpl<Parser::LexTokenKind::ClassKeyword>(TextSpan(0, 0)),
new Parser::IdentifierToken(TextSpan(0, 0), "foobar"),