Parse class attributes (shared, abstract, final, external), cleanup some parser code.
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
5d57838bec
commit
329848d9d5
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef MALACHSCRIPT_CLASSATTR_HPP
|
||||||
|
#define MALACHSCRIPT_CLASSATTR_HPP
|
||||||
|
|
||||||
|
namespace MalachScript {
|
||||||
|
enum class ClassAttr : uint8_t {
|
||||||
|
None = 0,
|
||||||
|
Shared = 1 << 1,
|
||||||
|
Abstract = 1 << 2,
|
||||||
|
Final = 1 << 3,
|
||||||
|
External = 1 << 4,
|
||||||
|
};
|
||||||
|
|
||||||
|
class ClassAttrHelpers {
|
||||||
|
public:
|
||||||
|
constexpr inline static bool Contains(ClassAttr set, ClassAttr flag) {
|
||||||
|
return (static_cast<uint8_t>(set) & static_cast<uint8_t>(flag)) != 0;
|
||||||
|
}
|
||||||
|
constexpr inline static ClassAttr Set(ClassAttr set, ClassAttr flag) {
|
||||||
|
return static_cast<ClassAttr>(static_cast<uint8_t>(set) | static_cast<uint8_t>(flag));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // MALACHSCRIPT_CLASSATTR_HPP
|
|
@ -9,8 +9,8 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
#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()); \
|
logError(DiagnosticType::UnexpectedToken, (token)->GetSpan()); \
|
||||||
} else { \
|
} else { \
|
||||||
PROGRESS_TOKEN(token); \
|
PROGRESS_TOKEN(token); \
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,8 @@ namespace MalachScript::Parser {
|
||||||
|
|
||||||
const ParsedScriptStatement* Parser::ParseScript(const LexToken* firstToken, const log_func& log) {
|
const ParsedScriptStatement* Parser::ParseScript(const LexToken* firstToken, const log_func& log) {
|
||||||
std::vector<const ParsedStatement*> statements;
|
std::vector<const ParsedStatement*> statements;
|
||||||
statements.reserve(32);
|
const size_t reservedScriptSize = 32;
|
||||||
|
statements.reserve(reservedScriptSize);
|
||||||
size_t current = 0;
|
size_t current = 0;
|
||||||
const auto* currentToken = firstToken;
|
const auto* currentToken = firstToken;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -64,12 +65,13 @@ namespace MalachScript::Parser {
|
||||||
const auto* current = currentToken;
|
const auto* current = currentToken;
|
||||||
auto start = current->GetSpan().GetStart();
|
auto start = current->GetSpan().GetStart();
|
||||||
bool lookingForClass = true;
|
bool lookingForClass = true;
|
||||||
|
ClassAttr classAttr = ClassAttr::None;
|
||||||
while (lookingForClass) {
|
while (lookingForClass) {
|
||||||
switch (current->GetKind()) {
|
switch (current->GetKind()) {
|
||||||
case LexTokenKind::SharedKeyword: break;
|
case LexTokenKind::SharedKeyword: ClassAttrHelpers::Set(classAttr, ClassAttr::Shared); break;
|
||||||
case LexTokenKind::AbstractKeyword: break;
|
case LexTokenKind::AbstractKeyword: ClassAttrHelpers::Set(classAttr, ClassAttr::Abstract); break;
|
||||||
case LexTokenKind::FinalKeyword: break;
|
case LexTokenKind::FinalKeyword: ClassAttrHelpers::Set(classAttr, ClassAttr::Final); break;
|
||||||
case LexTokenKind::ExternalKeyword: break;
|
case LexTokenKind::ExternalKeyword: ClassAttrHelpers::Set(classAttr, ClassAttr::External); break;
|
||||||
case LexTokenKind::ClassKeyword: lookingForClass = false; break;
|
case LexTokenKind::ClassKeyword: lookingForClass = false; break;
|
||||||
default: return false;
|
default: return false;
|
||||||
}
|
}
|
||||||
|
@ -83,7 +85,6 @@ namespace MalachScript::Parser {
|
||||||
PROGRESS_TOKEN(current);
|
PROGRESS_TOKEN(current);
|
||||||
std::vector<Identifier> inherits;
|
std::vector<Identifier> inherits;
|
||||||
std::vector<const ParsedStatement*> body;
|
std::vector<const ParsedStatement*> body;
|
||||||
body.reserve(16);
|
|
||||||
|
|
||||||
switch (current->GetKind()) {
|
switch (current->GetKind()) {
|
||||||
case LexTokenKind::SemicolonSymbol: {
|
case LexTokenKind::SemicolonSymbol: {
|
||||||
|
@ -132,7 +133,8 @@ namespace MalachScript::Parser {
|
||||||
}
|
}
|
||||||
default: throw;
|
default: throw;
|
||||||
}
|
}
|
||||||
out = new ParsedClassStatement(TextSpan(start, current->GetSpan().GetEnd()), identifier, inherits, body);
|
out = new ParsedClassStatement(TextSpan(start, current->GetSpan().GetEnd()), classAttr, identifier, inherits,
|
||||||
|
body);
|
||||||
currentToken = current;
|
currentToken = current;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -716,7 +718,7 @@ namespace MalachScript::Parser {
|
||||||
}
|
}
|
||||||
|
|
||||||
#define EXPR_PARSER(operator, name, func) \
|
#define EXPR_PARSER(operator, name, func) \
|
||||||
operator name; \
|
operator(name); \
|
||||||
if (func(name, current)) { \
|
if (func(name, current)) { \
|
||||||
PROGRESS_TOKEN(current); \
|
PROGRESS_TOKEN(current); \
|
||||||
ScopedPtr<const ParsedStatement> rightHand = nullptr; \
|
ScopedPtr<const ParsedStatement> rightHand = nullptr; \
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "../../CoreData/AccessModifier.hpp"
|
#include "../../CoreData/AccessModifier.hpp"
|
||||||
|
#include "../../CoreData/ClassAttr.hpp"
|
||||||
#include "../../CoreData/FuncAttr.hpp"
|
#include "../../CoreData/FuncAttr.hpp"
|
||||||
#include "../../CoreData/TypeMod.hpp"
|
#include "../../CoreData/TypeMod.hpp"
|
||||||
#include "../../TextSpan.hpp"
|
#include "../../TextSpan.hpp"
|
||||||
|
@ -41,24 +42,27 @@ namespace MalachScript::Parser {
|
||||||
};
|
};
|
||||||
|
|
||||||
class ParsedClassStatement : public ParsedStatementImpl<ParsedStatementKind::Class> {
|
class ParsedClassStatement : public ParsedStatementImpl<ParsedStatementKind::Class> {
|
||||||
Identifier _identifier;
|
|
||||||
std::vector<Identifier> _inherits;
|
|
||||||
std::vector<std::unique_ptr<const ParsedStatement>> _body;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ParsedClassStatement(TextSpan span, Identifier identifier, std::vector<Identifier> inherits,
|
ParsedClassStatement(TextSpan span, ClassAttr classAttr, Identifier identifier,
|
||||||
const std::vector<const ParsedStatement*>& body)
|
std::vector<Identifier> inherits, const std::vector<const ParsedStatement*>& body)
|
||||||
: ParsedStatementImpl<ParsedStatementKind::Class>(span), _identifier(identifier), _inherits(inherits),
|
: ParsedStatementImpl<ParsedStatementKind::Class>(span), _classAttr(classAttr), _identifier(identifier),
|
||||||
_body(body.size()) {
|
_inherits(inherits), _body(body.size()) {
|
||||||
for (size_t i = 0; i < body.size(); i++)
|
for (size_t i = 0; i < body.size(); i++)
|
||||||
_body[i] = std::unique_ptr<const ParsedStatement>(body[i]);
|
_body[i] = std::unique_ptr<const ParsedStatement>(body[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] ClassAttr GetClassAttr() const noexcept { return _classAttr; }
|
||||||
[[nodiscard]] const Identifier& GetIdentifier() const noexcept { return _identifier; }
|
[[nodiscard]] const Identifier& GetIdentifier() const noexcept { return _identifier; }
|
||||||
[[nodiscard]] const std::vector<Identifier>& GetInherits() const noexcept { return _inherits; }
|
[[nodiscard]] const std::vector<Identifier>& GetInherits() const noexcept { return _inherits; }
|
||||||
[[nodiscard]] const std::vector<std::unique_ptr<const ParsedStatement>>& GetBody() const noexcept {
|
[[nodiscard]] const std::vector<std::unique_ptr<const ParsedStatement>>& GetBody() const noexcept {
|
||||||
return _body;
|
return _body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
ClassAttr _classAttr;
|
||||||
|
Identifier _identifier;
|
||||||
|
std::vector<Identifier> _inherits;
|
||||||
|
std::vector<std::unique_ptr<const ParsedStatement>> _body;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ParsedTypeDefStatement : public ParsedStatementImpl<ParsedStatementKind::TypeDef> {
|
class ParsedTypeDefStatement : public ParsedStatementImpl<ParsedStatementKind::TypeDef> {
|
||||||
|
|
Loading…
Reference in New Issue