#ifndef MALACHSCRIPT_LEXTOKEN_HPP #define MALACHSCRIPT_LEXTOKEN_HPP #include #include #include "../../CoreData/Identifier.hpp" #include "../../TextSpan.hpp" #include "../ParserDefines.hpp" #include "LexTokenKind.hpp" namespace MalachScript::Parser { class LexToken { std::unique_ptr _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& 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(token); } }; template 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 { ParseInt _value; public: IntegerLiteral(const ScriptTextSpan& span, ParseInt value) : LexTokenImpl(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 { ParseFloat _value; public: FloatLiteral(const ScriptTextSpan& span, ParseFloat value) : LexTokenImpl(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 { ParseString _value; public: StringLiteral(const ScriptTextSpan& span, ParseString value) : LexTokenImpl(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 { Identifier _value; public: IdentifierToken(const ScriptTextSpan& span, Identifier value) : LexTokenImpl(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