2014-08-07 01:07:04 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include <string>
|
2014-08-07 01:10:33 +00:00
|
|
|
#include <functional>
|
2014-08-11 02:57:24 +00:00
|
|
|
#include "util/sstream.h"
|
2014-08-07 01:07:04 +00:00
|
|
|
#include "frontends/lean/server.h"
|
|
|
|
#include "frontends/lean/parser.h"
|
|
|
|
|
|
|
|
namespace lean {
|
2014-08-16 00:24:37 +00:00
|
|
|
server::file::file(std::string const & fname):m_fname(fname) {}
|
2014-08-07 01:07:04 +00:00
|
|
|
|
2014-08-11 17:40:18 +00:00
|
|
|
void server::file::replace_line(unsigned linenum, std::string const & new_line) {
|
|
|
|
while (linenum >= m_lines.size())
|
|
|
|
m_lines.push_back("");
|
|
|
|
m_lines[linenum] = new_line;
|
2014-08-07 01:07:04 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 17:40:18 +00:00
|
|
|
void server::file::insert_line(unsigned linenum, std::string const & new_line) {
|
|
|
|
while (linenum >= m_lines.size())
|
|
|
|
m_lines.push_back("");
|
|
|
|
m_lines.push_back("");
|
|
|
|
lean_assert(m_lines.size() >= linenum+1);
|
|
|
|
unsigned i = m_lines.size();
|
|
|
|
while (i > linenum) {
|
|
|
|
--i;
|
|
|
|
m_lines[i] = m_lines[i-1];
|
|
|
|
}
|
|
|
|
m_lines[linenum] = new_line;
|
2014-08-07 01:07:04 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 17:40:18 +00:00
|
|
|
void server::file::remove_line(unsigned linenum) {
|
|
|
|
if (linenum >= m_lines.size())
|
|
|
|
return;
|
|
|
|
lean_assert(!m_lines.empty());
|
|
|
|
for (unsigned i = linenum; i < m_lines.size()-1; i++)
|
|
|
|
m_lines[i] = m_lines[i+1];
|
|
|
|
m_lines.pop_back();
|
2014-08-07 01:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return index i <= m_snapshots.size() s.t.
|
|
|
|
* forall j < i, m_snapshots[j].m_line < line
|
|
|
|
* forall i <= j < m_snapshots.size(), m_snapshots[j].m_line >= line
|
|
|
|
*/
|
2014-08-11 17:40:18 +00:00
|
|
|
unsigned server::file::find(unsigned linenum) {
|
2014-08-07 01:07:04 +00:00
|
|
|
unsigned low = 0;
|
|
|
|
unsigned high = m_snapshots.size();
|
|
|
|
while (true) {
|
|
|
|
lean_assert(low <= high);
|
|
|
|
if (low == high)
|
|
|
|
return low;
|
|
|
|
unsigned mid = low + ((high - low)/2);
|
|
|
|
lean_assert(low <= mid && mid < high);
|
|
|
|
lean_assert(mid < m_snapshots.size());
|
|
|
|
snapshot const & s = m_snapshots[mid];
|
|
|
|
if (s.m_line < linenum) {
|
|
|
|
low = mid+1;
|
|
|
|
} else {
|
|
|
|
high = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-11 17:40:18 +00:00
|
|
|
server::server(environment const & env, io_state const & ios, unsigned num_threads):
|
2014-08-14 21:40:46 +00:00
|
|
|
m_env(env), m_ios(ios), m_out(ios.get_regular_channel().get_stream()),
|
|
|
|
m_num_threads(num_threads), m_empty_snapshot(m_env, m_ios.get_options()) {
|
2014-08-16 00:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
server::~server() {
|
|
|
|
reset_thread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void server::interrupt_thread() {
|
|
|
|
if (m_thread_ptr)
|
|
|
|
m_thread_ptr->request_interrupt();
|
|
|
|
}
|
|
|
|
|
|
|
|
void server::reset_thread() {
|
|
|
|
if (m_thread_ptr) {
|
|
|
|
m_thread_ptr->request_interrupt();
|
|
|
|
m_thread_ptr->join();
|
|
|
|
m_thread_ptr.reset(nullptr);
|
|
|
|
}
|
2014-08-11 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string g_load("LOAD");
|
|
|
|
static std::string g_visit("VISIT");
|
|
|
|
static std::string g_replace("REPLACE");
|
|
|
|
static std::string g_insert("INSERT");
|
|
|
|
static std::string g_remove("REMOVE");
|
|
|
|
static std::string g_check("CHECK");
|
|
|
|
static std::string g_info("INFO");
|
2014-08-14 21:40:46 +00:00
|
|
|
static std::string g_set("SET");
|
2014-08-14 23:08:43 +00:00
|
|
|
static std::string g_eval("EVAL");
|
2014-08-17 19:14:42 +00:00
|
|
|
static std::string g_wait("WAIT");
|
2014-08-27 17:31:01 +00:00
|
|
|
static std::string g_clear_cache("CLEAR_CACHE");
|
2014-08-11 17:40:18 +00:00
|
|
|
|
|
|
|
static bool is_command(std::string const & cmd, std::string const & line) {
|
|
|
|
return line.compare(0, cmd.size(), cmd) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string & ltrim(std::string & s) {
|
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string & rtrim(std::string & s) {
|
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string & trim(std::string & s) {
|
|
|
|
return ltrim(rtrim(s));
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:07:04 +00:00
|
|
|
void server::process_from(unsigned linenum) {
|
2014-08-16 00:24:37 +00:00
|
|
|
reset_thread();
|
2014-08-11 17:40:18 +00:00
|
|
|
unsigned i = m_file->find(linenum);
|
|
|
|
m_file->m_snapshots.resize(i);
|
|
|
|
snapshot & s = i == 0 ? m_empty_snapshot : m_file->m_snapshots[i-1];
|
2014-08-07 01:07:04 +00:00
|
|
|
std::string block;
|
|
|
|
lean_assert(s.m_line > 0);
|
2014-08-11 17:40:18 +00:00
|
|
|
for (unsigned j = s.m_line-1; j < m_file->m_lines.size(); j++) {
|
|
|
|
block += m_file->m_lines[j];
|
2014-08-07 01:07:04 +00:00
|
|
|
block += '\n';
|
|
|
|
}
|
2014-08-16 00:24:37 +00:00
|
|
|
m_thread_ptr.reset(new interruptible_thread([=]() {
|
|
|
|
try {
|
|
|
|
snapshot & s = i == 0 ? m_empty_snapshot : m_file->m_snapshots[i-1];
|
|
|
|
std::istringstream strm(block);
|
2014-08-27 17:46:49 +00:00
|
|
|
std::shared_ptr<output_channel> out1(new string_output_channel());
|
|
|
|
std::shared_ptr<output_channel> out2(new string_output_channel());
|
|
|
|
io_state ios(m_ios, out1, out2);
|
|
|
|
ios.set_options(s.m_options);
|
|
|
|
parser p(s.m_env, ios, strm, m_file->m_fname.c_str(), false, 1, s.m_lds, s.m_eds, s.m_line,
|
2014-08-16 00:24:37 +00:00
|
|
|
&m_file->m_snapshots, &m_file->m_info);
|
2014-08-17 19:14:42 +00:00
|
|
|
p.set_cache(&m_cache);
|
2014-08-16 00:24:37 +00:00
|
|
|
p();
|
|
|
|
} catch (exception& ex) {}
|
|
|
|
}));
|
2014-08-11 02:57:24 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 17:40:18 +00:00
|
|
|
void server::load_file(std::string const & fname) {
|
2014-08-16 00:24:37 +00:00
|
|
|
interrupt_thread();
|
2014-08-07 01:07:04 +00:00
|
|
|
std::ifstream in(fname);
|
|
|
|
if (in.bad() || in.fail()) {
|
2014-08-11 17:40:18 +00:00
|
|
|
m_out << "-- ERROR failed to open file '" << fname << "'" << std::endl;
|
2014-08-07 01:07:04 +00:00
|
|
|
} else {
|
2014-08-16 00:24:37 +00:00
|
|
|
reset_thread();
|
2014-08-11 17:40:18 +00:00
|
|
|
m_file.reset(new file(fname));
|
|
|
|
m_file_map.insert(mk_pair(fname, m_file));
|
2014-08-07 01:07:04 +00:00
|
|
|
for (std::string line; std::getline(in, line);) {
|
2014-08-11 17:40:18 +00:00
|
|
|
m_file->m_lines.push_back(line);
|
2014-08-07 01:07:04 +00:00
|
|
|
}
|
2014-08-16 00:24:37 +00:00
|
|
|
process_from(0);
|
2014-08-11 02:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-11 17:40:18 +00:00
|
|
|
void server::visit_file(std::string const & fname) {
|
2014-08-16 00:24:37 +00:00
|
|
|
interrupt_thread();
|
2014-08-11 17:40:18 +00:00
|
|
|
auto it = m_file_map.find(fname);
|
2014-08-16 00:24:37 +00:00
|
|
|
if (it == m_file_map.end()) {
|
2014-08-11 17:40:18 +00:00
|
|
|
load_file(fname);
|
2014-08-16 00:24:37 +00:00
|
|
|
} else {
|
|
|
|
reset_thread();
|
2014-08-11 17:40:18 +00:00
|
|
|
m_file = it->second;
|
2014-08-16 00:24:37 +00:00
|
|
|
process_from(0);
|
|
|
|
}
|
2014-08-07 01:07:04 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 02:57:24 +00:00
|
|
|
void server::read_line(std::istream & in, std::string & line) {
|
|
|
|
if (!std::getline(in, line))
|
|
|
|
throw exception("unexpected end of input");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a line of the form "cmd linenum", return the linenum
|
|
|
|
unsigned server::get_linenum(std::string const & line, std::string const & cmd) {
|
|
|
|
std::string data = line.substr(cmd.size());
|
|
|
|
trim(data);
|
|
|
|
unsigned r = atoi(data.c_str());
|
|
|
|
if (r == 0)
|
|
|
|
throw exception("line numbers are indexed from 1");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-08-11 17:40:18 +00:00
|
|
|
void server::check_file() {
|
|
|
|
if (!m_file)
|
|
|
|
throw exception("no file has been loaded/visited");
|
|
|
|
}
|
|
|
|
|
|
|
|
void server::replace_line(unsigned linenum, std::string const & new_line) {
|
2014-08-16 00:24:37 +00:00
|
|
|
interrupt_thread();
|
2014-08-11 17:40:18 +00:00
|
|
|
check_file();
|
|
|
|
m_file->replace_line(linenum, new_line);
|
2014-08-17 19:14:42 +00:00
|
|
|
reset_thread();
|
|
|
|
m_file->m_info.invalidate_line(linenum+1);
|
2014-08-16 00:24:37 +00:00
|
|
|
process_from(linenum);
|
2014-08-11 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void server::insert_line(unsigned linenum, std::string const & new_line) {
|
2014-08-16 00:24:37 +00:00
|
|
|
interrupt_thread();
|
2014-08-11 17:40:18 +00:00
|
|
|
check_file();
|
|
|
|
m_file->insert_line(linenum, new_line);
|
2014-08-17 19:14:42 +00:00
|
|
|
reset_thread();
|
|
|
|
m_file->m_info.insert_line(linenum+1);
|
2014-08-16 00:24:37 +00:00
|
|
|
process_from(linenum);
|
2014-08-11 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void server::remove_line(unsigned linenum) {
|
2014-08-16 00:24:37 +00:00
|
|
|
interrupt_thread();
|
2014-08-11 17:40:18 +00:00
|
|
|
check_file();
|
|
|
|
m_file->remove_line(linenum);
|
2014-08-17 19:14:42 +00:00
|
|
|
reset_thread();
|
|
|
|
m_file->m_info.remove_line(linenum+1);
|
2014-08-16 00:24:37 +00:00
|
|
|
process_from(linenum);
|
2014-08-11 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void server::check_line(unsigned linenum, std::string const & line) {
|
|
|
|
check_file();
|
|
|
|
if (linenum >= m_file->m_lines.size()) {
|
|
|
|
m_out << "-- MISMATCH line out of range" << std::endl;
|
|
|
|
} else if (m_file->m_lines[linenum] != line) {
|
|
|
|
m_out << "-- MISMATCH expected " << m_file->m_lines[linenum] << std::endl;
|
|
|
|
} else {
|
|
|
|
m_out << "-- OK" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 21:40:46 +00:00
|
|
|
void server::set_option(std::string const & line) {
|
|
|
|
std::string cmd = "set_option ";
|
|
|
|
cmd += line;
|
|
|
|
std::istringstream strm(cmd);
|
|
|
|
m_out << "-- BEGINSET" << std::endl;
|
|
|
|
try {
|
|
|
|
parser p(m_env, m_ios, strm, "SET_command", true);
|
|
|
|
p();
|
|
|
|
m_ios.set_options(p.ios().get_options());
|
|
|
|
} catch (exception & ex) {
|
|
|
|
m_out << ex.what() << std::endl;
|
|
|
|
}
|
|
|
|
m_out << "-- ENDSET" << std::endl;
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:24:37 +00:00
|
|
|
void server::show_info(unsigned linenum) {
|
2014-08-21 23:49:21 +00:00
|
|
|
check_file();
|
2014-08-27 01:06:37 +00:00
|
|
|
m_out << "-- BEGININFO" << std::endl;
|
|
|
|
m_file->m_info.display(m_env, m_ios, linenum);
|
|
|
|
m_out << "-- ENDINFO" << std::endl;
|
2014-08-16 00:24:37 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 01:06:37 +00:00
|
|
|
void server::eval_core(environment const & env, options const & o, std::string const & line) {
|
2014-08-14 23:08:43 +00:00
|
|
|
std::istringstream strm(line);
|
2014-08-27 17:46:49 +00:00
|
|
|
io_state ios(m_ios, o);
|
2014-08-14 23:08:43 +00:00
|
|
|
m_out << "-- BEGINEVAL" << std::endl;
|
|
|
|
try {
|
2014-08-27 17:46:49 +00:00
|
|
|
parser p(env, ios, strm, "EVAL_command", true);
|
2014-08-14 23:08:43 +00:00
|
|
|
p();
|
|
|
|
} catch (exception & ex) {
|
|
|
|
m_out << ex.what() << std::endl;
|
|
|
|
}
|
|
|
|
m_out << "-- ENDEVAL" << std::endl;
|
|
|
|
}
|
|
|
|
|
2014-08-27 01:06:37 +00:00
|
|
|
void server::eval(std::string const & line) {
|
|
|
|
if (!m_file) {
|
|
|
|
eval_core(m_env, m_ios.get_options(), line);
|
|
|
|
} else if (auto p = m_file->m_info.get_final_env_opts()) {
|
2014-08-27 17:46:49 +00:00
|
|
|
eval_core(p->first, join(p->second, m_ios.get_options()), line);
|
2014-08-27 01:06:37 +00:00
|
|
|
} else {
|
|
|
|
eval_core(m_env, m_ios.get_options(), line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:07:04 +00:00
|
|
|
bool server::operator()(std::istream & in) {
|
|
|
|
for (std::string line; std::getline(in, line);) {
|
2014-08-11 02:57:24 +00:00
|
|
|
try {
|
2014-08-11 17:40:18 +00:00
|
|
|
if (is_command(g_load, line)) {
|
|
|
|
std::string fname = line.substr(g_load.size());
|
|
|
|
trim(fname);
|
|
|
|
load_file(fname);
|
|
|
|
} else if (is_command(g_visit, line)) {
|
|
|
|
std::string fname = line.substr(g_visit.size());
|
2014-08-11 02:57:24 +00:00
|
|
|
trim(fname);
|
2014-08-11 17:40:18 +00:00
|
|
|
visit_file(fname);
|
|
|
|
} else if (is_command(g_check, line)) {
|
|
|
|
unsigned linenum = get_linenum(line, g_check);
|
|
|
|
read_line(in, line);
|
|
|
|
check_line(linenum-1, line);
|
2014-08-11 02:57:24 +00:00
|
|
|
} else if (is_command(g_replace, line)) {
|
|
|
|
unsigned linenum = get_linenum(line, g_replace);
|
|
|
|
read_line(in, line);
|
|
|
|
replace_line(linenum-1, line);
|
|
|
|
} else if (is_command(g_insert, line)) {
|
2014-08-11 17:40:18 +00:00
|
|
|
unsigned linenum = get_linenum(line, g_insert);
|
2014-08-11 02:57:24 +00:00
|
|
|
read_line(in, line);
|
|
|
|
insert_line(linenum-1, line);
|
|
|
|
} else if (is_command(g_remove, line)) {
|
2014-08-11 17:40:18 +00:00
|
|
|
unsigned linenum = get_linenum(line, g_remove);
|
2014-08-11 02:57:24 +00:00
|
|
|
remove_line(linenum-1);
|
|
|
|
} else if (is_command(g_info, line)) {
|
|
|
|
unsigned linenum = get_linenum(line, g_info);
|
|
|
|
show_info(linenum);
|
2014-08-14 21:40:46 +00:00
|
|
|
} else if (is_command(g_set, line)) {
|
|
|
|
read_line(in, line);
|
|
|
|
set_option(line);
|
2014-08-14 23:08:43 +00:00
|
|
|
} else if (is_command(g_eval, line)) {
|
|
|
|
read_line(in, line);
|
|
|
|
eval(line);
|
2014-08-27 17:31:01 +00:00
|
|
|
} else if (is_command(g_clear_cache, line)) {
|
|
|
|
m_cache.clear();
|
2014-08-17 19:14:42 +00:00
|
|
|
} else if (is_command(g_wait, line)) {
|
|
|
|
if (m_thread_ptr) {
|
|
|
|
m_thread_ptr->join();
|
|
|
|
m_thread_ptr.reset(nullptr);
|
|
|
|
}
|
2014-08-07 01:07:04 +00:00
|
|
|
} else {
|
2014-08-11 02:57:24 +00:00
|
|
|
throw exception(sstream() << "unexpected command line: " << line);
|
2014-08-07 01:07:04 +00:00
|
|
|
}
|
2014-08-11 02:57:24 +00:00
|
|
|
} catch (exception & ex) {
|
2014-08-11 17:40:18 +00:00
|
|
|
m_out << "-- ERROR " << ex.what() << std::endl;
|
2014-08-07 01:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|