MalachScript/src/Parser/Lexer/Lexer.hpp

68 lines
2.1 KiB
C++
Raw Normal View History

2020-10-05 15:45:00 +00:00
#ifndef MALACHSCRIPT_LEXER_HPP
#define MALACHSCRIPT_LEXER_HPP
#include <string_view>
2021-01-01 22:31:30 +00:00
#include "../../Diagnostics/Logger.hpp"
2020-10-09 09:54:43 +00:00
#include "../../Utils/MemoryPool.hpp"
#include "LexToken.hpp"
2020-10-05 15:45:00 +00:00
namespace MalachScript::Parser {
class Lexer {
public:
Lexer(const char* scriptName, const char* script, Diagnostics::Logger* diag)
: Lexer(std::string_view(scriptName), std::string_view(script), diag) {}
Lexer(std::string_view scriptName, std::string_view script, Diagnostics::Logger* diag)
2020-10-05 10:18:05 +00:00
: _scriptName(scriptName), _script(script), _scriptLength(script.size()), _diagnostics(diag) {}
const LexToken* Lex();
private:
std::string_view _scriptName;
std::string_view _script;
size_t _position = -1;
2020-10-04 20:21:20 +00:00
size_t _scriptLength;
2021-01-01 22:31:30 +00:00
Diagnostics::Logger* _diagnostics;
2021-01-04 12:33:29 +00:00
Utils::MemoryPool<4096> _allocator;
2020-10-04 20:21:20 +00:00
inline char Consume() {
2020-10-04 20:21:20 +00:00
if (++_position >= _scriptLength) {
return '\0';
}
return _script[_position];
}
2020-10-04 17:38:13 +00:00
inline void Progress(size_t steps = 1) { _position += steps; }
inline char Peek(size_t offset = 1) {
auto pos = _position + offset;
2020-10-04 20:21:20 +00:00
if (pos >= _scriptLength) {
return '\0';
}
return _script[pos];
}
LexToken* LexNext();
LexToken* LexNumerical(char);
LexToken* LexDecimal(uint64_t initial);
2020-10-04 15:15:28 +00:00
IntegerLiteral* LexHexadecimal();
IntegerLiteral* LexOctal();
IntegerLiteral* LexBinary();
StringLiteral* LexString(char opening, bool heredoc);
2020-10-04 16:30:53 +00:00
LexToken* LexKeywordOrIdentifier();
static bool IsAlphaNumericalOrUnderscore(char c);
2020-10-04 20:21:20 +00:00
template <class T, class... parameters> inline T* Create(parameters... args) {
return _allocator.Create<T>(args...);
}
2020-10-05 10:18:05 +00:00
inline void LogError(Diagnostics::DiagnosticType type, const ScriptTextSpan& span,
const std::vector<std::string>& formats) {
_diagnostics->LogError(type, span, formats);
2020-10-05 10:18:05 +00:00
}
};
}
2020-10-05 15:45:00 +00:00
#endif // MALACHSCRIPT_LEXER_HPP