2024-03-06 07:51:56 +01:00
|
|
|
#include "elna/source/lexer.hpp"
|
|
|
|
#include <variant>
|
2024-03-09 08:36:07 +01:00
|
|
|
#include <sstream>
|
2024-03-03 13:11:39 +01:00
|
|
|
|
2024-03-07 09:15:11 +01:00
|
|
|
namespace elna::source
|
2024-03-03 13:11:39 +01:00
|
|
|
{
|
|
|
|
using source_position = elna::source::position;
|
|
|
|
using source_error = elna::source::error;
|
2024-03-06 07:51:56 +01:00
|
|
|
using source_result = elna::source::result<std::vector<token>>;
|
2024-02-22 21:29:25 +01:00
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
std::pair<text_iterator, text_iterator> text_iterators(const std::string &buffer)
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
return std::make_pair<>(text_iterator(std::cbegin(buffer)),
|
|
|
|
text_iterator(std::cend(buffer), position{0, 0}));
|
2024-02-22 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
text_iterator::text_iterator(std::string::const_iterator buffer,
|
2024-03-03 13:11:39 +01:00
|
|
|
const source_position start_position)
|
2024-02-22 21:29:25 +01:00
|
|
|
: m_buffer(buffer), m_position(start_position)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
const source_position& text_iterator::position() const noexcept
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
return this->m_position;
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
text_iterator::reference text_iterator::operator*() const noexcept
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
return *m_buffer;
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
text_iterator::pointer text_iterator::operator->() const noexcept
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
return m_buffer.base();
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
text_iterator& text_iterator::operator++()
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
if (*this->m_buffer == '\n')
|
|
|
|
{
|
|
|
|
this->m_position.column = 1;
|
|
|
|
++this->m_position.line;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++this->m_position.column;
|
|
|
|
}
|
|
|
|
std::advance(this->m_buffer, 1);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
text_iterator& text_iterator::operator++(int)
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
auto tmp = *this;
|
|
|
|
++(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-03-11 10:43:26 +01:00
|
|
|
text_iterator text_iterator::operator+(std::size_t step)
|
|
|
|
{
|
|
|
|
auto result = *this;
|
|
|
|
return ++result;
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
bool text_iterator::operator==(const text_iterator& that) const noexcept
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
return this->m_buffer == that.m_buffer;
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
bool text_iterator::operator!=(const text_iterator& that) const noexcept
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
return !(*this == that);
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
token::value::value()
|
|
|
|
: nil(nullptr)
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
}
|
2024-02-28 16:18:39 +01:00
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
token::value::value(std::int32_t value)
|
|
|
|
: number(value)
|
|
|
|
{
|
|
|
|
}
|
2024-02-28 16:18:39 +01:00
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
token::value::value(const std::string& value)
|
|
|
|
: identifier(value)
|
|
|
|
{
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
token::value::~value()
|
2024-02-28 16:18:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
token::token(const type of, const std::string& value, const source_position position)
|
|
|
|
: m_type(of), m_value(value), m_position(position)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
token::token(const type of, std::int32_t number, const source_position position)
|
|
|
|
: m_type(of), m_value(number), m_position(position)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
token::token(const type of, source_position position)
|
2024-02-28 16:18:39 +01:00
|
|
|
: m_type(of), m_position(position)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
token::token(const token& that)
|
2024-02-28 16:18:39 +01:00
|
|
|
: m_type(that.of()), m_position(that.position())
|
|
|
|
{
|
|
|
|
*this = that;
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
token::token(token&& that)
|
2024-02-28 16:18:39 +01:00
|
|
|
: m_type(that.of()), m_position(that.position())
|
|
|
|
{
|
|
|
|
*this = std::move(that);
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
token::~token()
|
2024-02-28 16:18:39 +01:00
|
|
|
{
|
2024-03-03 13:11:39 +01:00
|
|
|
if (m_type == type::identifier || m_type == type::term_operator || m_type == type::factor_operator)
|
2024-02-28 16:18:39 +01:00
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
m_value.identifier.~basic_string();
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
token& token::operator=(const token& that)
|
2024-02-28 16:18:39 +01:00
|
|
|
{
|
|
|
|
m_type = that.of();
|
|
|
|
m_position = that.position();
|
2024-03-06 07:51:56 +01:00
|
|
|
if (that.has_identifier())
|
2024-02-28 16:18:39 +01:00
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
m_value.identifier = that.identifier();
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
2024-03-03 13:11:39 +01:00
|
|
|
else if (that.of() == type::number)
|
2024-02-28 16:18:39 +01:00
|
|
|
{
|
|
|
|
m_value.number = that.number();
|
|
|
|
}
|
2024-03-06 07:51:56 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_value.nil = nullptr;
|
|
|
|
}
|
2024-02-28 16:18:39 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
token& token::operator=(token&& that)
|
2024-02-28 16:18:39 +01:00
|
|
|
{
|
|
|
|
m_type = that.of();
|
|
|
|
m_position = that.position();
|
2024-03-06 07:51:56 +01:00
|
|
|
if (that.has_identifier())
|
2024-02-28 16:18:39 +01:00
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
m_value.identifier = std::move(that.identifier());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
2024-03-03 13:11:39 +01:00
|
|
|
else if (that.of() == type::number)
|
2024-02-28 16:18:39 +01:00
|
|
|
{
|
|
|
|
m_value.number = that.number();
|
|
|
|
}
|
2024-03-06 07:51:56 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_value.nil = nullptr;
|
|
|
|
}
|
2024-02-28 16:18:39 +01:00
|
|
|
return *this;
|
2024-02-22 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
token::type token::of() const noexcept
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
const std::string& token::identifier() const
|
2024-02-28 16:18:39 +01:00
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
if (!has_identifier())
|
|
|
|
{
|
|
|
|
throw std::bad_variant_access();
|
|
|
|
}
|
2024-02-28 16:18:39 +01:00
|
|
|
return m_value.identifier;
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
std::int32_t token::number() const
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
if (of() != type::number)
|
|
|
|
{
|
|
|
|
throw std::bad_variant_access();
|
|
|
|
}
|
2024-02-28 16:18:39 +01:00
|
|
|
return m_value.number;
|
2024-02-22 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
const source_position& token::position() const noexcept
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
return m_position;
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
bool token::has_identifier() const noexcept
|
|
|
|
{
|
|
|
|
return of() == type::identifier
|
|
|
|
|| of() == type::term_operator
|
|
|
|
|| of() == type::factor_operator;
|
|
|
|
}
|
|
|
|
|
2024-03-09 08:36:07 +01:00
|
|
|
unexpected_character::unexpected_character(const std::string& character, const source::position position)
|
|
|
|
: error(position), character(character)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string unexpected_character::what() const
|
|
|
|
{
|
|
|
|
std::stringstream ss{ "Unexpected character '" };
|
|
|
|
|
|
|
|
ss << character << "'";
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2024-03-10 08:50:55 +01:00
|
|
|
unexpected_token::unexpected_token(const token& token)
|
|
|
|
: error(token.position()), m_token(token)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string unexpected_token::what() const
|
|
|
|
{
|
|
|
|
return "Unexpected token";
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
source_result lex(const std::string& buffer)
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
std::vector<token> tokens;
|
|
|
|
auto [iterator, text_end] = text_iterators(buffer);
|
2024-02-22 21:29:25 +01:00
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
while (iterator != text_end)
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
if (*iterator == ' ' || *iterator == '\n')
|
|
|
|
{
|
|
|
|
}
|
2024-02-28 16:18:39 +01:00
|
|
|
else if (std::isdigit(*iterator))
|
|
|
|
{
|
|
|
|
tokens.emplace_back(
|
2024-03-06 07:51:56 +01:00
|
|
|
token::type::number,
|
2024-02-28 16:18:39 +01:00
|
|
|
static_cast<std::int32_t>(*iterator - '0'),
|
|
|
|
iterator.position()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (*iterator == '=')
|
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::equals, iterator.position());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
|
|
|
else if (*iterator == '(')
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::left_paren, iterator.position());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
|
|
|
else if (*iterator == ')')
|
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::right_paren, iterator.position());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
|
|
|
else if (*iterator == ';')
|
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::semicolon, iterator.position());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
|
|
|
else if (*iterator == ',')
|
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::comma, iterator.position());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
|
|
|
else if (*iterator == '!')
|
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::bang, iterator.position());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
|
|
|
else if (*iterator == '.')
|
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::dot, iterator.position());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
|
|
|
else if (std::isalpha(*iterator))
|
|
|
|
{
|
|
|
|
std::string word;
|
|
|
|
auto i = iterator;
|
2024-03-06 07:51:56 +01:00
|
|
|
while (i != text_end && std::isalpha(*i))
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-02-28 16:18:39 +01:00
|
|
|
word.push_back(*i);
|
|
|
|
++i;
|
2024-02-22 21:29:25 +01:00
|
|
|
}
|
2024-02-28 16:18:39 +01:00
|
|
|
if (word == "const")
|
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::let, iterator.position());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
|
|
|
else if (word == "var")
|
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::var, iterator.position());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
2024-03-10 08:50:55 +01:00
|
|
|
else if (word == "begin")
|
|
|
|
{
|
|
|
|
tokens.emplace_back(token::type::begin, iterator.position());
|
|
|
|
}
|
|
|
|
else if (word == "end")
|
|
|
|
{
|
|
|
|
tokens.emplace_back(token::type::end, iterator.position());
|
|
|
|
}
|
2024-02-28 16:18:39 +01:00
|
|
|
else
|
|
|
|
{
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::identifier, word.c_str(), iterator.position());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
|
|
|
iterator = i;
|
2024-02-22 21:29:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
2024-02-28 16:18:39 +01:00
|
|
|
else if (*iterator == '+' || *iterator == '-')
|
|
|
|
{
|
|
|
|
std::string _operator{ *iterator };
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::term_operator, _operator.c_str(), iterator.position());
|
2024-03-03 13:11:39 +01:00
|
|
|
}
|
|
|
|
else if (*iterator == '*' || *iterator == '/')
|
|
|
|
{
|
|
|
|
std::string _operator{ *iterator };
|
|
|
|
|
2024-03-06 07:51:56 +01:00
|
|
|
tokens.emplace_back(token::type::factor_operator, _operator.c_str(), iterator.position());
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
2024-03-11 10:43:26 +01:00
|
|
|
else if (*iterator == ':' && iterator + 1 != text_end && *(iterator + 1) == '=')
|
|
|
|
{
|
|
|
|
tokens.emplace_back(token::type::assignment, iterator.position());
|
|
|
|
++iterator;
|
|
|
|
}
|
2024-02-28 16:18:39 +01:00
|
|
|
else
|
|
|
|
{
|
2024-03-09 08:36:07 +01:00
|
|
|
return source_result(unexpected_character{ std::string{ *iterator }, iterator.position() });
|
2024-02-28 16:18:39 +01:00
|
|
|
}
|
2024-02-22 21:29:25 +01:00
|
|
|
++iterator;
|
|
|
|
}
|
2024-03-10 08:50:55 +01:00
|
|
|
tokens.push_back(token(token::type::eof, iterator.position()));
|
|
|
|
|
2024-03-09 08:36:07 +01:00
|
|
|
return source_result(std::move(tokens));
|
2024-02-22 21:29:25 +01:00
|
|
|
}
|
|
|
|
}
|