Assign variables

This commit is contained in:
2024-03-14 08:52:45 +01:00
parent 25657ac36d
commit 99a1ef5f96
18 changed files with 700 additions and 339 deletions

View File

@ -6,7 +6,6 @@ namespace elna::source
{
using source_position = elna::source::position;
using source_error = elna::source::error;
using source_result = elna::source::result<std::vector<token>>;
std::pair<text_iterator, text_iterator> text_iterators(const std::string &buffer)
{
@ -225,7 +224,83 @@ namespace elna::source
return "Unexpected token";
}
source_result lex(const std::string& buffer)
lexer::lexer(std::vector<token>&& tokens, const position last_position)
: tokens(std::move(tokens)), iterator(this->tokens.cbegin()), eof(token(token::type::eof, last_position))
{
}
lexer& lexer::operator++()
{
++iterator;
return *this;
}
const token& lexer::operator*() const
{
return *iterator;
}
const token *lexer::operator->() const
{
return iterator.base();
}
const token& lexer::current() const noexcept
{
if (iterator == tokens.cend())
{
return this->eof;
}
return *iterator;
}
bool lexer::current(const token::type token_type) const noexcept
{
return current().of() == token_type;
}
void lexer::add_error(const token& expected)
{
m_errors.push_back(std::make_unique<unexpected_token>(expected));
}
std::optional<std::reference_wrapper<const token>> lexer::advance(const token::type token_type)
{
if (iterator != tokens.cend() && iterator->of() == token_type)
{
return std::make_optional<>(std::cref(*iterator++));
}
add_error(current());
return std::optional<std::reference_wrapper<const token>>();
}
const token& lexer::look_ahead() const
{
auto tmp = iterator;
++tmp;
if (iterator == tokens.cend() || tmp == tokens.cend())
{
return eof;
}
return *tmp;
}
bool lexer::look_ahead(const token::type token_type) const
{
return look_ahead().of() == token_type;
}
bool lexer::skip(const token::type token_type)
{
return advance(token_type).has_value();
}
const std::list<std::unique_ptr<error>>& lexer::errors() const noexcept
{
return m_errors;
}
result<lexer> lex(const std::string& buffer)
{
std::vector<token> tokens;
auto [iterator, text_end] = text_iterators(buffer);
@ -322,12 +397,10 @@ namespace elna::source
}
else
{
return source_result(unexpected_character{ std::string{ *iterator }, iterator.position() });
return result<lexer>(unexpected_character{ std::string{ *iterator }, iterator.position() });
}
++iterator;
}
tokens.push_back(token(token::type::eof, iterator.position()));
return source_result(std::move(tokens));
return result<lexer>(std::in_place, std::move(tokens), iterator.position());
}
}