MalachScript/src/Parser/Lexer/LexToken.hpp

97 lines
3.5 KiB
C++

#ifndef MALACHSCRIPT_LEXTOKEN_HPP
#define MALACHSCRIPT_LEXTOKEN_HPP
#include <memory>
#include <sstream>
#include "../../CoreData/Identifier.hpp"
#include "../../TextSpan.hpp"
#include "../ParserDefines.hpp"
#include "LexTokenKind.hpp"
namespace MalachScript::Parser {
class LexToken {
std::unique_ptr<const LexToken> _next;
ScriptTextSpan _span;
public:
inline LexToken(const ScriptTextSpan& span) : _span(span) {}
virtual ~LexToken() = default;
[[nodiscard]] virtual LexTokenKind GetKind() const noexcept = 0;
[[nodiscard]] inline const std::unique_ptr<const LexToken>& GetNext() const noexcept { return _next; }
[[nodiscard]] inline const ScriptTextSpan& GetSpan() const noexcept { return _span; }
[[nodiscard]] virtual std::string ToString() const noexcept { return LexTokenKindHelper::ToString(GetKind()); }
inline void SetNext(LexToken* token) { _next = std::unique_ptr<const LexToken>(token); }
};
template <LexTokenKind kind> class LexTokenImpl : public LexToken {
public:
inline LexTokenImpl(const ScriptTextSpan& span) : LexToken(span){};
[[nodiscard]] inline LexTokenKind GetKind() const noexcept final { return kind; }
};
class IntegerLiteral : public LexTokenImpl<LexTokenKind::IntegerLiteral> {
ParseInt _value;
public:
IntegerLiteral(const ScriptTextSpan& span, ParseInt value)
: LexTokenImpl<LexTokenKind::IntegerLiteral>(span), _value(value) {}
[[nodiscard]] inline ParseInt GetValue() const noexcept { return _value; }
[[nodiscard]] std::string ToString() const noexcept override {
std::stringstream ss;
ss << "Integer(" << GetValue() << ")";
return ss.str();
}
};
class FloatLiteral : public LexTokenImpl<LexTokenKind::FloatLiteral> {
ParseFloat _value;
public:
FloatLiteral(const ScriptTextSpan& span, ParseFloat value)
: LexTokenImpl<LexTokenKind::FloatLiteral>(span), _value(value) {}
[[nodiscard]] inline long double GetValue() const noexcept { return _value; }
std::string ToString() const noexcept override {
std::stringstream ss;
ss << "Float(" << GetValue() << ")";
return ss.str();
}
};
class StringLiteral : public LexTokenImpl<LexTokenKind::StringLiteral> {
ParseString _value;
public:
StringLiteral(const ScriptTextSpan& span, ParseString value)
: LexTokenImpl<LexTokenKind::StringLiteral>(span), _value(std::move(value)) {}
[[nodiscard]] inline const ParseString& GetValue() const noexcept { return _value; }
std::string ToString() const noexcept override {
std::stringstream ss;
ss << "String("
<< "\"" << (char*)GetValue().c_str() << "\""
<< ")";
return ss.str();
}
};
class IdentifierToken : public LexTokenImpl<LexTokenKind::Identifier> {
Identifier _value;
public:
IdentifierToken(const ScriptTextSpan& span, Identifier value)
: LexTokenImpl<LexTokenKind::Identifier>(span), _value(value) {}
[[nodiscard]] inline const Identifier& GetValue() const noexcept { return _value; }
[[nodiscard]] std::string ToString() const noexcept override {
std::stringstream ss;
ss << "Identifier(" << GetValue() << ")";
return ss.str();
}
};
}
#endif // MALACHSCRIPT_LEXTOKEN_HPP