elna/source/lexer.cpp

303 lines
7.9 KiB
C++
Raw Normal View History

#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;
using source_result = elna::source::result<std::vector<token>>;
2024-02-22 21:29:25 +01:00
std::pair<text_iterator, text_iterator> text_iterators(const std::string &buffer)
2024-02-22 21:29:25 +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
}
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)
{
}
const source_position& text_iterator::position() const noexcept
2024-02-22 21:29:25 +01:00
{
return this->m_position;
}
text_iterator::reference text_iterator::operator*() const noexcept
2024-02-22 21:29:25 +01:00
{
return *m_buffer;
}
text_iterator::pointer text_iterator::operator->() const noexcept
2024-02-22 21:29:25 +01:00
{
return m_buffer.base();
}
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;
}
text_iterator& text_iterator::operator++(int)
2024-02-22 21:29:25 +01:00
{
auto tmp = *this;
++(*this);
return *this;
}
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;
}
bool text_iterator::operator!=(const text_iterator& that) const noexcept
2024-02-22 21:29:25 +01:00
{
return !(*this == that);
}
token::value::value()
: nil(nullptr)
2024-02-22 21:29:25 +01:00
{
}
2024-02-28 16:18:39 +01:00
token::value::value(std::int32_t value)
: number(value)
{
}
2024-02-28 16:18:39 +01:00
token::value::value(const std::string& value)
: identifier(value)
{
2024-02-28 16:18:39 +01:00
}
token::value::~value()
2024-02-28 16:18:39 +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)
{
}
token::token(const token& that)
2024-02-28 16:18:39 +01:00
: m_type(that.of()), m_position(that.position())
{
*this = that;
}
token::token(token&& that)
2024-02-28 16:18:39 +01:00
: m_type(that.of()), m_position(that.position())
{
*this = std::move(that);
}
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
{
m_value.identifier.~basic_string();
2024-02-28 16:18:39 +01:00
}
}
token& token::operator=(const token& that)
2024-02-28 16:18:39 +01:00
{
m_type = that.of();
m_position = that.position();
if (that.has_identifier())
2024-02-28 16:18:39 +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();
}
else
{
m_value.nil = nullptr;
}
2024-02-28 16:18:39 +01:00
return *this;
}
token& token::operator=(token&& that)
2024-02-28 16:18:39 +01:00
{
m_type = that.of();
m_position = that.position();
if (that.has_identifier())
2024-02-28 16:18:39 +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();
}
else
{
m_value.nil = nullptr;
}
2024-02-28 16:18:39 +01:00
return *this;
2024-02-22 21:29:25 +01:00
}
token::type token::of() const noexcept
2024-02-22 21:29:25 +01:00
{
return m_type;
}
const std::string& token::identifier() const
2024-02-28 16:18:39 +01:00
{
if (!has_identifier())
{
throw std::bad_variant_access();
}
2024-02-28 16:18:39 +01:00
return m_value.identifier;
}
std::int32_t token::number() const
2024-02-22 21:29:25 +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
}
const source_position& token::position() const noexcept
2024-02-22 21:29:25 +01:00
{
return m_position;
}
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();
}
source_result lex(const std::string& buffer)
2024-02-22 21:29:25 +01:00
{
std::vector<token> tokens;
auto [iterator, text_end] = text_iterators(buffer);
2024-02-22 21:29:25 +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(
token::type::number,
2024-02-28 16:18:39 +01:00
static_cast<std::int32_t>(*iterator - '0'),
iterator.position()
);
}
else if (*iterator == '=')
{
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
{
tokens.emplace_back(token::type::left_paren, iterator.position());
2024-02-28 16:18:39 +01:00
}
else if (*iterator == ')')
{
tokens.emplace_back(token::type::right_paren, iterator.position());
2024-02-28 16:18:39 +01:00
}
else if (*iterator == ';')
{
tokens.emplace_back(token::type::semicolon, iterator.position());
2024-02-28 16:18:39 +01:00
}
else if (*iterator == ',')
{
tokens.emplace_back(token::type::comma, iterator.position());
2024-02-28 16:18:39 +01:00
}
else if (*iterator == '!')
{
tokens.emplace_back(token::type::bang, iterator.position());
2024-02-28 16:18:39 +01:00
}
else if (*iterator == '.')
{
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;
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")
{
tokens.emplace_back(token::type::let, iterator.position());
2024-02-28 16:18:39 +01:00
}
else if (word == "var")
{
tokens.emplace_back(token::type::var, iterator.position());
2024-02-28 16:18:39 +01:00
}
else
{
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 };
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 };
tokens.emplace_back(token::type::factor_operator, _operator.c_str(), iterator.position());
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-09 08:36:07 +01:00
return source_result(std::move(tokens));
2024-02-22 21:29:25 +01:00
}
}