Initial work on parsing.

This commit is contained in:
2020-10-07 22:11:18 +02:00
parent f299d5183f
commit 2036f1ce43
10 changed files with 276 additions and 55 deletions

View File

@@ -1,32 +1,43 @@
#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(const LexToken* firstToken) : _currentToken(firstToken) {}
Parser(const char* scriptName, const LexToken* firstToken, Diagnostics::Diagnostics* diagnostics)
: _scriptName(reinterpret_cast<const char8_t*>(scriptName)), _diagnostics(diagnostics),
_currentToken(firstToken) {}
Parser(std::u8string_view scriptName, const LexToken* firstToken, Diagnostics::Diagnostics* diagnostics)
: _scriptName(scriptName), _diagnostics(diagnostics), _currentToken(firstToken) {}
ParsedScriptStatement* Parse();
private:
std::u8string_view _scriptName;
Diagnostics::Diagnostics* _diagnostics;
const LexToken* _currentToken;
inline const LexToken* Peek() {
if (_currentToken->GetKind() == LexTokenKind::EndOfFile) {
return _currentToken;
}
return _currentToken->GetNext().get();
inline void LogError(Diagnostics::DiagnosticType type, const TextSpan& span) {
_diagnostics->LogError(type, _scriptName, span);
}
inline const LexToken* Consume() {
if (_currentToken->GetKind() == LexTokenKind::EndOfFile) {
return _currentToken;
}
_currentToken = _currentToken->GetNext().get();
return _currentToken;
}
bool ParseClass(const ParsedStatement*& out);
bool ParseVirtProp(const ParsedStatement*& out);
bool ParseFunc(const ParsedStatement*& out);
bool ParseVar(const ParsedStatement*& out);
bool ParseFuncDef(const ParsedStatement*& out);
const ParsedStatement* ParseStatement(const LexToken* token);
std::u8string_view ParseIdentifier(const LexToken* token, bool& logError) {
if (logError && token->GetKind() != LexTokenKind::Identifier) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, token->GetSpan());
logError = false;
return std::u8string_view();
}
return reinterpret_cast<const IdentifierToken*>(token)->GetValue();
}
};
}