MalachScript/src/Parser/Parser.hpp

46 lines
1.7 KiB
C++

#ifndef MALACHSCRIPT_PARSER_HPP
#define MALACHSCRIPT_PARSER_HPP
#include "../Diagnostics/Diagnostics.hpp"
#include "Lexer/LexToken.hpp"
#include "Statements/ParsedStatement.hpp"
namespace MalachScript::Parser {
class Parser {
public:
Parser(std::u8string_view scriptName, const LexToken* firstToken, Diagnostics::Diagnostics* diagnostics)
: _scriptName(scriptName), _diagnostics(diagnostics), _currentToken(firstToken) {}
const ParsedScriptStatement* Parse();
private:
std::u8string_view _scriptName;
Diagnostics::Diagnostics* _diagnostics;
const LexToken* _currentToken;
inline void LogError(Diagnostics::DiagnosticType type, const TextSpan& span) {
_diagnostics->LogError(type, _scriptName, span);
}
const ParsedScriptStatement* ParseScript();
bool ParseClass(const ParsedStatement*& out);
bool ParseTypeDef(const ParsedStatement*& out);
bool ParseNamespace(const ParsedStatement*& out);
bool ParseVirtProp(const ParsedStatement*& out);
bool ParseFunc(const ParsedStatement*& out);
bool ParseVar(const ParsedStatement*& out);
bool ParseFuncDef(const ParsedStatement*& out);
bool ParsePrimType(Identifier& out);
bool ParseIdentifier(Identifier& out, const LexToken* token, bool& logError) {
if (logError && token->GetKind() != LexTokenKind::Identifier) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, token->GetSpan());
logError = false;
return false;
}
out = reinterpret_cast<const IdentifierToken*>(token)->GetValue();
return true;
}
};
}
#endif // MALACHSCRIPT_PARSER_HPP