Extend the tester to compile sources
This commit is contained in:
@ -9,8 +9,8 @@ namespace elna
|
||||
|
||||
void editor_history::push(const std::string& entry)
|
||||
{
|
||||
commands.push_front(entry);
|
||||
current_pointer = commands.cbegin();
|
||||
commands.push_back(entry);
|
||||
current_pointer = commands.cend();
|
||||
}
|
||||
|
||||
void editor_history::clear()
|
||||
@ -46,4 +46,9 @@ namespace elna
|
||||
{
|
||||
return this->commands.cend();
|
||||
}
|
||||
|
||||
editor_history::const_iterator editor_history::current() const noexcept
|
||||
{
|
||||
return this->current_pointer;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
#include "elna/interactive.hpp"
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/process.hpp>
|
||||
#include <termios.h>
|
||||
#include <filesystem>
|
||||
|
||||
#include "elna/interactive.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
static termios original_termios;
|
||||
@ -39,9 +40,8 @@ namespace elna
|
||||
break;
|
||||
}
|
||||
history.push(line.value().command_line());
|
||||
auto tokens = lex(line.value().command_line());
|
||||
|
||||
if (!execute(tokens.assume_value()))
|
||||
if (!execute(line.value().command_line()))
|
||||
{
|
||||
break;
|
||||
}
|
||||
@ -53,6 +53,7 @@ namespace elna
|
||||
{
|
||||
editor_state state;
|
||||
std::string buffer;
|
||||
std::string saved;
|
||||
|
||||
state.draw(std::back_inserter(buffer));
|
||||
write(STDOUT_FILENO, buffer.data(), buffer.size());
|
||||
@ -81,7 +82,7 @@ namespace elna
|
||||
break;
|
||||
case action::complete:
|
||||
write(STDOUT_FILENO, "\x1b[1E\x1b[2K", 8);
|
||||
state.insert(complete());
|
||||
state.insert(complete(state.command_line(), state.relative_position()));
|
||||
write(STDOUT_FILENO, "\x1b[1A", 4);
|
||||
state.draw(std::back_inserter(buffer));
|
||||
write(STDOUT_FILENO, buffer.data(), buffer.size());
|
||||
@ -104,18 +105,26 @@ namespace elna
|
||||
|
||||
if (pressed_key == key(special_key::arrow_up, modifier::ctrl))
|
||||
{
|
||||
history_iterator = history.next();
|
||||
if (history.current() == history.cend())
|
||||
{
|
||||
saved = state.command_line();
|
||||
}
|
||||
history_iterator = history.prev();
|
||||
}
|
||||
else if (pressed_key == key(special_key::arrow_down, modifier::ctrl))
|
||||
{
|
||||
history_iterator = history.prev();
|
||||
history_iterator = history.next();
|
||||
if (history_iterator == history.cend())
|
||||
{
|
||||
state.load(saved);
|
||||
}
|
||||
}
|
||||
if (history_iterator != history.cend())
|
||||
{
|
||||
state.load(*history_iterator);
|
||||
state.draw(std::back_inserter(buffer));
|
||||
write(STDOUT_FILENO, buffer.data(), buffer.size());
|
||||
}
|
||||
state.draw(std::back_inserter(buffer));
|
||||
write(STDOUT_FILENO, buffer.data(), buffer.size());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -137,7 +146,7 @@ namespace elna
|
||||
{
|
||||
return key();
|
||||
}
|
||||
auto [number, last_char] = read_number<unsigned char>();
|
||||
auto [number, last_char] = read_number<key::char_t>();
|
||||
std::uint8_t modifiers{ 0 };
|
||||
|
||||
if (last_char == ';')
|
||||
@ -185,35 +194,51 @@ namespace elna
|
||||
return key();
|
||||
}
|
||||
|
||||
bool execute(const std::vector<token>& tokens)
|
||||
void print_exception(const std::exception& exception)
|
||||
{
|
||||
if (tokens.empty())
|
||||
std::string message{ exception.what() };
|
||||
message += "\r\n";
|
||||
write(STDERR_FILENO, message.data(), message.size());
|
||||
}
|
||||
|
||||
bool execute(const std::string& line)
|
||||
{
|
||||
if (line.empty())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
std::string program = tokens.front().identifier();
|
||||
if (program == "exit")
|
||||
if (line == "exit")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (program == "cd")
|
||||
else if (boost::starts_with(line, "cd "))
|
||||
{
|
||||
std::filesystem::current_path(tokens[1].identifier());
|
||||
try
|
||||
{
|
||||
std::filesystem::current_path(line.substr(strlen("cd ")));
|
||||
}
|
||||
catch (const std::filesystem::filesystem_error& exception)
|
||||
{
|
||||
print_exception(exception);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
std::vector<std::string> arguments;
|
||||
for (auto argument = tokens.cbegin() + 1; argument != std::cend(tokens); ++argument)
|
||||
{
|
||||
arguments.push_back(argument->identifier());
|
||||
}
|
||||
launch(program, arguments);
|
||||
launch(line);
|
||||
return true;
|
||||
}
|
||||
|
||||
void launch(const std::string& program, const std::vector<std::string>& arguments)
|
||||
void launch(const std::string& program)
|
||||
{
|
||||
boost::process::system(boost::process::search_path(program), arguments);
|
||||
try
|
||||
{
|
||||
boost::process::system(program);
|
||||
}
|
||||
catch (const boost::process::process_error& exception)
|
||||
{
|
||||
print_exception(exception);
|
||||
}
|
||||
enable_raw_mode();
|
||||
}
|
||||
|
||||
bool enable_raw_mode()
|
||||
@ -240,10 +265,17 @@ namespace elna
|
||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios);
|
||||
}
|
||||
|
||||
std::string complete()
|
||||
std::string complete(const std::string& input, const std::size_t position)
|
||||
{
|
||||
std::filesystem::path program = boost::process::search_path("fzf");
|
||||
|
||||
if (program.empty())
|
||||
{
|
||||
return "";
|
||||
}
|
||||
boost::process::ipstream output;
|
||||
boost::process::system("/usr/bin/fzf", "--height=10", "--layout=reverse",
|
||||
boost::process::system(program,
|
||||
"--height=10", "--layout=reverse", "-1", "--no-multi",
|
||||
boost::process::std_out > output);
|
||||
|
||||
std::string selections;
|
||||
|
124
shell/lexer.cpp
124
shell/lexer.cpp
@ -1,124 +0,0 @@
|
||||
#include "elna/lexer.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
source::source(const std::string& buffer)
|
||||
: m_buffer(buffer)
|
||||
{
|
||||
}
|
||||
|
||||
source::const_iterator source::begin() const
|
||||
{
|
||||
return source::const_iterator(std::cbegin(m_buffer), Position());
|
||||
}
|
||||
|
||||
source::const_iterator source::end() const
|
||||
{
|
||||
Position end_position{ 0, 0 };
|
||||
|
||||
return source::const_iterator(std::cend(m_buffer), end_position);
|
||||
}
|
||||
|
||||
source::const_iterator::const_iterator(std::string::const_iterator buffer, const Position& start_position)
|
||||
: m_buffer(buffer), m_position(start_position)
|
||||
{
|
||||
}
|
||||
|
||||
const Position& source::const_iterator::position() const noexcept
|
||||
{
|
||||
return this->m_position;
|
||||
}
|
||||
|
||||
source::const_iterator::reference source::const_iterator::operator*() const noexcept
|
||||
{
|
||||
return *m_buffer;
|
||||
}
|
||||
|
||||
source::const_iterator::pointer source::const_iterator::operator->() const noexcept
|
||||
{
|
||||
return m_buffer.base();
|
||||
}
|
||||
|
||||
source::const_iterator& source::const_iterator::operator++()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
source::const_iterator& source::const_iterator::operator++(int)
|
||||
{
|
||||
auto tmp = *this;
|
||||
++(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool source::const_iterator::operator==(const source::const_iterator& that) const noexcept
|
||||
{
|
||||
return this->m_buffer == that.m_buffer;
|
||||
}
|
||||
|
||||
bool source::const_iterator::operator!=(const source::const_iterator& that) const noexcept
|
||||
{
|
||||
return !(*this == that);
|
||||
}
|
||||
|
||||
token::token(const type of, source::const_iterator begin, source::const_iterator end)
|
||||
: m_type(of), m_value(begin, end)
|
||||
{
|
||||
}
|
||||
|
||||
token::type token::of() const noexcept
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
const token::value& token::identifier() const noexcept
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
const Position& token::position() const noexcept
|
||||
{
|
||||
return m_position;
|
||||
}
|
||||
|
||||
result<std::vector<token>> lex(const std::string& buffer)
|
||||
{
|
||||
std::vector<token> tokens;
|
||||
source input{ buffer };
|
||||
|
||||
for (auto iterator = input.begin(); iterator != input.end();)
|
||||
{
|
||||
if (*iterator == ' ' || *iterator == '\n')
|
||||
{
|
||||
}
|
||||
else if (std::isgraph(*iterator))
|
||||
{
|
||||
auto current_position = iterator;
|
||||
do
|
||||
{
|
||||
++current_position;
|
||||
}
|
||||
while (current_position != input.end() && std::isgraph(*current_position));
|
||||
token new_token{ token::type::word, iterator, current_position };
|
||||
|
||||
tokens.push_back(new_token);
|
||||
iterator = current_position;
|
||||
continue;
|
||||
}
|
||||
++iterator;
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
}
|
@ -2,24 +2,24 @@
|
||||
|
||||
namespace elna
|
||||
{
|
||||
CompileError::CompileError(char const *message, Position position) noexcept
|
||||
compile_error::compile_error(const char *message, const source_position position) noexcept
|
||||
{
|
||||
this->message_ = message;
|
||||
this->position_ = position;
|
||||
this->message = message;
|
||||
this->position = position;
|
||||
}
|
||||
|
||||
char const *CompileError::message() const noexcept
|
||||
char const *compile_error::what() const noexcept
|
||||
{
|
||||
return this->message_;
|
||||
return this->message;
|
||||
}
|
||||
|
||||
std::size_t CompileError::line() const noexcept
|
||||
std::size_t compile_error::line() const noexcept
|
||||
{
|
||||
return this->position_.line;
|
||||
return this->position.line;
|
||||
}
|
||||
|
||||
std::size_t CompileError::column() const noexcept
|
||||
std::size_t compile_error::column() const noexcept
|
||||
{
|
||||
return this->position_.column;
|
||||
return this->position.column;
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,6 @@
|
||||
#include <algorithm>
|
||||
#include <boost/endian.hpp>
|
||||
|
||||
#include "elna/state.hpp"
|
||||
|
||||
namespace elna
|
||||
@ -12,16 +15,16 @@ namespace elna
|
||||
}
|
||||
|
||||
key::key()
|
||||
: store(static_cast<unsigned char>(0x1b))
|
||||
: store(0x1bu)
|
||||
{
|
||||
}
|
||||
|
||||
key::key(const unsigned char c, const std::uint8_t modifiers)
|
||||
key::key(const char_t c, const std::uint8_t modifiers)
|
||||
: store(c), modifiers(modifiers == 0 ? 0 : modifiers - 1)
|
||||
{
|
||||
}
|
||||
|
||||
key::key(const unsigned char c, const modifier modifiers)
|
||||
key::key(const char_t c, const modifier modifiers)
|
||||
: store(c), modifiers(static_cast<uint8_t>(modifiers))
|
||||
{
|
||||
}
|
||||
@ -36,9 +39,9 @@ namespace elna
|
||||
{
|
||||
}
|
||||
|
||||
unsigned char key::character() const
|
||||
key::char_t key::character() const
|
||||
{
|
||||
return std::get<unsigned char>(this->store);
|
||||
return std::get<char_t>(this->store);
|
||||
}
|
||||
|
||||
special_key key::control() const
|
||||
@ -56,10 +59,10 @@ namespace elna
|
||||
return this->modifiers == static_cast<std::uint8_t>(modifiers);
|
||||
}
|
||||
|
||||
bool key::operator==(const unsigned char c) const
|
||||
bool key::operator==(const char_t c) const
|
||||
{
|
||||
return std::holds_alternative<unsigned char>(this->store)
|
||||
&& std::get<unsigned char>(this->store) == c;
|
||||
return std::holds_alternative<char_t>(this->store)
|
||||
&& std::get<char_t>(this->store) == c;
|
||||
}
|
||||
|
||||
bool key::operator==(const special_key control) const
|
||||
@ -73,7 +76,7 @@ namespace elna
|
||||
return this->store == that.store;
|
||||
}
|
||||
|
||||
bool key::operator!=(const unsigned char c) const
|
||||
bool key::operator!=(const char_t c) const
|
||||
{
|
||||
return !(*this == c);
|
||||
}
|
||||
@ -90,7 +93,7 @@ namespace elna
|
||||
|
||||
bool key::is_character() const noexcept
|
||||
{
|
||||
return std::holds_alternative<unsigned char>(this->store);
|
||||
return std::holds_alternative<char_t>(this->store);
|
||||
}
|
||||
|
||||
bool key::is_control() const noexcept
|
||||
@ -131,6 +134,7 @@ namespace elna
|
||||
void editor_state::load(const std::string& text)
|
||||
{
|
||||
this->input = text;
|
||||
this->position = this->input.size() + 1;
|
||||
}
|
||||
|
||||
const std::string& editor_state::command_line() const noexcept
|
||||
@ -143,9 +147,36 @@ namespace elna
|
||||
return this->prompt.size() + this->position;
|
||||
}
|
||||
|
||||
std::size_t editor_state::relative_position() const noexcept
|
||||
{
|
||||
return this->position;
|
||||
}
|
||||
|
||||
void editor_state::insert(const std::string& text)
|
||||
{
|
||||
this->input.insert(std::end(this->input), std::cbegin(text), std::cend(text));
|
||||
this->input.insert(this->position - 1, text);
|
||||
this->position += text.size();
|
||||
}
|
||||
|
||||
void editor_state::insert(const key::char_t text)
|
||||
{
|
||||
if (text == 0u)
|
||||
{
|
||||
this->input.insert(this->position - 1, '\0', 1);
|
||||
++this->position;
|
||||
return;
|
||||
}
|
||||
key::char_t buffer = boost::endian::native_to_big<key::char_t>(text);
|
||||
|
||||
const char *begin = reinterpret_cast<char *>(&buffer);
|
||||
const char *end = begin + sizeof(key::char_t);
|
||||
const char *significant = std::find_if(begin, end,
|
||||
[](const char byte) {
|
||||
return byte != 0;
|
||||
});
|
||||
|
||||
this->input.insert(this->position - 1, significant, end - significant);
|
||||
this->position += end - significant;
|
||||
}
|
||||
|
||||
action editor_state::process_keypress(const key& press)
|
||||
@ -202,12 +233,21 @@ namespace elna
|
||||
this->input.erase(this->position - 1, 1);
|
||||
}
|
||||
return action::redraw;
|
||||
case 9: // Tab.
|
||||
return action::complete;
|
||||
case '\r':
|
||||
return action::finalize;
|
||||
default:
|
||||
++this->position;
|
||||
this->input.push_back(press.character());
|
||||
return action::write;
|
||||
if (this->position - 1 < input.size())
|
||||
{
|
||||
insert(press.character());
|
||||
return action::redraw;
|
||||
}
|
||||
else
|
||||
{
|
||||
insert(press.character());
|
||||
return action::write;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user