2014-06-11 00:02:06 +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
|
|
|
|
*/
|
2014-06-12 01:51:12 +00:00
|
|
|
#include <utility>
|
2014-06-11 18:26:17 +00:00
|
|
|
#include <string>
|
2014-06-11 22:07:20 +00:00
|
|
|
#include <limits>
|
2014-06-18 16:14:50 +00:00
|
|
|
#include <vector>
|
2014-06-11 00:02:06 +00:00
|
|
|
#include "util/interrupt.h"
|
|
|
|
#include "util/script_exception.h"
|
2014-06-11 18:26:17 +00:00
|
|
|
#include "util/sstream.h"
|
2014-06-13 18:24:26 +00:00
|
|
|
#include "util/flet.h"
|
2014-06-16 23:37:46 +00:00
|
|
|
#include "util/lean_path.h"
|
2014-06-11 22:07:20 +00:00
|
|
|
#include "kernel/for_each_fn.h"
|
2014-06-14 01:00:52 +00:00
|
|
|
#include "kernel/replace_fn.h"
|
2014-06-12 01:51:12 +00:00
|
|
|
#include "kernel/abstract.h"
|
2014-06-12 03:56:10 +00:00
|
|
|
#include "kernel/instantiate.h"
|
2014-06-11 00:02:06 +00:00
|
|
|
#include "library/parser_nested_exception.h"
|
2014-06-11 22:07:20 +00:00
|
|
|
#include "library/aliases.h"
|
|
|
|
#include "library/private.h"
|
|
|
|
#include "library/choice.h"
|
2014-06-12 01:51:12 +00:00
|
|
|
#include "library/placeholder.h"
|
2014-06-11 22:07:20 +00:00
|
|
|
#include "library/deep_copy.h"
|
2014-06-16 23:37:46 +00:00
|
|
|
#include "library/module.h"
|
2014-06-18 20:55:48 +00:00
|
|
|
#include "library/scoped_ext.h"
|
2014-07-02 15:08:35 +00:00
|
|
|
#include "library/num.h"
|
2014-07-02 17:00:55 +00:00
|
|
|
#include "library/string.h"
|
2014-06-11 00:02:06 +00:00
|
|
|
#include "library/error_handling/error_handling.h"
|
2014-07-03 15:33:29 +00:00
|
|
|
#include "library/tactic/expr_to_tactic.h"
|
2014-06-11 00:02:06 +00:00
|
|
|
#include "frontends/lean/parser.h"
|
2014-07-08 02:15:46 +00:00
|
|
|
#include "frontends/lean/util.h"
|
2014-07-01 00:00:10 +00:00
|
|
|
#include "frontends/lean/parser_bindings.h"
|
2014-06-15 18:30:52 +00:00
|
|
|
#include "frontends/lean/notation_cmd.h"
|
2014-06-25 15:30:09 +00:00
|
|
|
#include "frontends/lean/elaborator.h"
|
2014-06-11 00:02:06 +00:00
|
|
|
|
|
|
|
#ifndef LEAN_DEFAULT_PARSER_SHOW_ERRORS
|
|
|
|
#define LEAN_DEFAULT_PARSER_SHOW_ERRORS true
|
|
|
|
#endif
|
|
|
|
|
2014-07-26 05:46:49 +00:00
|
|
|
#ifndef LEAN_DEFAULT_PARSER_PARALLEL_IMPORT
|
|
|
|
#define LEAN_DEFAULT_PARSER_PARALLEL_IMPORT false
|
|
|
|
#endif
|
|
|
|
|
2014-06-11 00:02:06 +00:00
|
|
|
namespace lean {
|
|
|
|
// ==========================================
|
|
|
|
// Parser configuration options
|
2014-07-26 05:46:49 +00:00
|
|
|
static name g_parser_show_errors {"parser", "show_errors"};
|
|
|
|
static name g_parser_parallel_import {"parser", "parallel_import"};
|
2014-06-11 00:02:06 +00:00
|
|
|
|
|
|
|
RegisterBoolOption(g_parser_show_errors, LEAN_DEFAULT_PARSER_SHOW_ERRORS, "(lean parser) display error messages in the regular output channel");
|
2014-07-26 05:46:49 +00:00
|
|
|
RegisterBoolOption(g_parser_parallel_import, LEAN_DEFAULT_PARSER_PARALLEL_IMPORT, "(lean parser) import modules in parallel");
|
2014-06-11 00:02:06 +00:00
|
|
|
|
|
|
|
bool get_parser_show_errors(options const & opts) { return opts.get_bool(g_parser_show_errors, LEAN_DEFAULT_PARSER_SHOW_ERRORS); }
|
2014-07-26 05:46:49 +00:00
|
|
|
bool get_parser_parallel_import(options const & opts) { return opts.get_bool(g_parser_parallel_import, LEAN_DEFAULT_PARSER_PARALLEL_IMPORT); }
|
2014-06-11 00:02:06 +00:00
|
|
|
// ==========================================
|
|
|
|
|
2014-06-16 16:24:41 +00:00
|
|
|
parser::local_scope::local_scope(parser & p):
|
|
|
|
m_p(p), m_env(p.env()) {
|
|
|
|
p.push_local_scope();
|
|
|
|
}
|
|
|
|
parser::local_scope::~local_scope() {
|
|
|
|
m_p.pop_local_scope();
|
|
|
|
m_p.m_env = m_env;
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:30:03 +00:00
|
|
|
parser::no_undef_id_error_scope::no_undef_id_error_scope(parser & p):m_p(p), m_old(m_p.m_no_undef_id_error) {
|
|
|
|
m_p.m_no_undef_id_error = true;
|
|
|
|
}
|
|
|
|
parser::no_undef_id_error_scope::~no_undef_id_error_scope() {
|
|
|
|
m_p.m_no_undef_id_error = m_old;
|
|
|
|
}
|
|
|
|
|
2014-06-21 00:17:39 +00:00
|
|
|
static name g_tmp_prefix = name::mk_internal_unique_name();
|
|
|
|
|
2014-06-11 00:02:06 +00:00
|
|
|
parser::parser(environment const & env, io_state const & ios,
|
|
|
|
std::istream & strm, char const * strm_name,
|
2014-06-18 04:38:10 +00:00
|
|
|
bool use_exceptions, unsigned num_threads,
|
2014-06-14 17:38:42 +00:00
|
|
|
local_level_decls const & lds, local_expr_decls const & eds,
|
|
|
|
unsigned line):
|
2014-06-21 00:17:39 +00:00
|
|
|
m_env(env), m_ios(ios), m_ngen(g_tmp_prefix),
|
2014-06-11 00:02:06 +00:00
|
|
|
m_verbose(true), m_use_exceptions(use_exceptions),
|
2014-06-14 16:56:05 +00:00
|
|
|
m_scanner(strm, strm_name), m_local_level_decls(lds), m_local_decls(eds),
|
2014-07-13 03:11:17 +00:00
|
|
|
m_pos_table(std::make_shared<pos_info_table>()),
|
2014-07-26 21:26:34 +00:00
|
|
|
m_theorem_queue(*this, num_threads > 1 ? num_threads - 1 : 0) {
|
2014-06-14 17:38:42 +00:00
|
|
|
m_scanner.set_line(line);
|
2014-06-16 23:37:46 +00:00
|
|
|
m_num_threads = num_threads;
|
2014-06-17 17:30:03 +00:00
|
|
|
m_no_undef_id_error = false;
|
2014-06-11 00:02:06 +00:00
|
|
|
m_found_errors = false;
|
|
|
|
updt_options();
|
|
|
|
m_next_tag_idx = 0;
|
|
|
|
m_curr = scanner::token_kind::Identifier;
|
|
|
|
protected_call([&]() { scan(); },
|
|
|
|
[&]() { sync_command(); });
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:50:57 +00:00
|
|
|
parser::~parser() {
|
|
|
|
try {
|
|
|
|
if (!m_theorem_queue.done()) {
|
|
|
|
m_theorem_queue.interrupt();
|
|
|
|
m_theorem_queue.join();
|
|
|
|
}
|
|
|
|
} catch (...) {}
|
|
|
|
}
|
|
|
|
|
2014-07-03 15:33:29 +00:00
|
|
|
bool parser::has_tactic_decls() {
|
|
|
|
if (!m_has_tactic_decls)
|
|
|
|
m_has_tactic_decls = ::lean::has_tactic_decls(m_env);
|
|
|
|
return *m_has_tactic_decls;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr parser::mk_by(expr const & t, pos_info const & pos) {
|
|
|
|
if (!has_tactic_decls())
|
|
|
|
throw parser_error("invalid 'by' expression, tactic module has not been imported", pos);
|
|
|
|
return save_pos(::lean::mk_by(t), pos);
|
|
|
|
}
|
|
|
|
|
2014-06-11 00:02:06 +00:00
|
|
|
void parser::updt_options() {
|
|
|
|
m_verbose = get_verbose(m_ios.get_options());
|
|
|
|
m_show_errors = get_parser_show_errors(m_ios.get_options());
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser::display_error_pos(unsigned line, unsigned pos) {
|
2014-06-12 03:56:10 +00:00
|
|
|
regular_stream() << get_stream_name() << ":" << line << ":";
|
2014-06-11 00:02:06 +00:00
|
|
|
if (pos != static_cast<unsigned>(-1))
|
2014-06-12 03:56:10 +00:00
|
|
|
regular_stream() << pos << ":";
|
|
|
|
regular_stream() << " error:";
|
2014-06-11 00:02:06 +00:00
|
|
|
}
|
|
|
|
void parser::display_error_pos(pos_info p) { display_error_pos(p.first, p.second); }
|
|
|
|
|
|
|
|
void parser::display_error(char const * msg, unsigned line, unsigned pos) {
|
|
|
|
display_error_pos(line, pos);
|
2014-06-12 03:56:10 +00:00
|
|
|
regular_stream() << " " << msg << endl;
|
2014-06-11 00:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void parser::display_error(char const * msg, pos_info p) {
|
|
|
|
display_error(msg, p.first, p.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser::display_error(exception const & ex) {
|
|
|
|
parser_pos_provider pos_provider(m_pos_table, get_stream_name(), m_last_cmd_pos);
|
2014-06-12 03:56:10 +00:00
|
|
|
::lean::display_error(regular_stream(), &pos_provider, ex);
|
2014-06-11 00:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void parser::throw_parser_exception(char const * msg, pos_info p) {
|
|
|
|
throw parser_exception(msg, get_stream_name().c_str(), p.first, p.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser::throw_nested_exception(exception & ex, pos_info p) {
|
|
|
|
throw parser_nested_exception(std::shared_ptr<exception>(ex.clone()),
|
|
|
|
std::make_shared<parser_pos_provider>(m_pos_table, get_stream_name(), p));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CATCH(ShowError, ThrowError) \
|
|
|
|
m_found_errors = true; \
|
|
|
|
if (!m_use_exceptions && m_show_errors) { ShowError ; } \
|
|
|
|
sync(); \
|
|
|
|
if (m_use_exceptions) { ThrowError ; }
|
|
|
|
|
|
|
|
void parser::protected_call(std::function<void()> && f, std::function<void()> && sync) {
|
|
|
|
try {
|
|
|
|
f();
|
|
|
|
// } catch (tactic_cmd_error & ex) {
|
|
|
|
// CATCH(display_error(ex),
|
|
|
|
// throw parser_exception(ex.what(), m_strm_name.c_str(), ex.m_pos.first, ex.m_pos.second));
|
|
|
|
} catch (parser_exception & ex) {
|
2014-06-12 03:56:10 +00:00
|
|
|
CATCH(regular_stream() << ex.what() << endl,
|
2014-06-11 00:02:06 +00:00
|
|
|
throw);
|
|
|
|
} catch (parser_error & ex) {
|
|
|
|
CATCH(display_error(ex.what(), ex.m_pos),
|
|
|
|
throw_parser_exception(ex.what(), ex.m_pos));
|
|
|
|
} catch (interrupted & ex) {
|
|
|
|
reset_interrupt();
|
|
|
|
if (m_verbose)
|
2014-06-12 03:56:10 +00:00
|
|
|
regular_stream() << "!!!Interrupted!!!" << endl;
|
2014-06-11 00:02:06 +00:00
|
|
|
sync();
|
|
|
|
if (m_use_exceptions)
|
|
|
|
throw;
|
|
|
|
} catch (script_exception & ex) {
|
2014-06-15 05:13:25 +00:00
|
|
|
reset_interrupt();
|
|
|
|
CATCH(display_error(ex), throw_nested_exception(ex, m_last_script_pos));
|
2014-06-11 00:02:06 +00:00
|
|
|
} catch (exception & ex) {
|
2014-06-15 05:13:25 +00:00
|
|
|
reset_interrupt();
|
|
|
|
CATCH(display_error(ex), throw_nested_exception(ex, m_last_cmd_pos));
|
2014-06-11 00:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser::sync_command() {
|
|
|
|
// Keep consuming tokens until we find a Command or End-of-file
|
|
|
|
while (curr() != scanner::token_kind::CommandKeyword && curr() != scanner::token_kind::Eof)
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
tag parser::get_tag(expr e) {
|
|
|
|
tag t = e.get_tag();
|
|
|
|
if (t == nulltag) {
|
|
|
|
t = m_next_tag_idx;
|
|
|
|
e.set_tag(t);
|
|
|
|
m_next_tag_idx++;
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2014-06-11 22:07:20 +00:00
|
|
|
expr parser::save_pos(expr e, pos_info p) {
|
2014-06-11 00:02:06 +00:00
|
|
|
m_pos_table->insert(mk_pair(get_tag(e), p));
|
2014-06-11 22:07:20 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr parser::rec_save_pos(expr const & e, pos_info p) {
|
|
|
|
unsigned m = std::numeric_limits<unsigned>::max();
|
|
|
|
pos_info dummy(m, 0);
|
|
|
|
for_each(e, [&](expr const & e, unsigned) {
|
|
|
|
if (pos_of(e, dummy).first == m) {
|
|
|
|
save_pos(e, p);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Create a copy of \c e, and the position of new expression with p */
|
|
|
|
expr parser::copy_with_new_pos(expr const & e, pos_info p) {
|
2014-06-15 17:40:18 +00:00
|
|
|
switch (e.kind()) {
|
|
|
|
case expr_kind::Sort: case expr_kind::Constant: case expr_kind::Meta:
|
|
|
|
case expr_kind::Local: case expr_kind::Var:
|
|
|
|
return save_pos(copy(e), p);
|
|
|
|
case expr_kind::App:
|
|
|
|
return save_pos(::lean::mk_app(copy_with_new_pos(app_fn(e), p),
|
|
|
|
copy_with_new_pos(app_arg(e), p)),
|
|
|
|
p);
|
|
|
|
case expr_kind::Lambda: case expr_kind::Pi:
|
|
|
|
return save_pos(update_binding(e,
|
|
|
|
copy_with_new_pos(binding_domain(e), p),
|
|
|
|
copy_with_new_pos(binding_body(e), p)),
|
|
|
|
p);
|
|
|
|
case expr_kind::Macro: {
|
|
|
|
buffer<expr> args;
|
|
|
|
for (unsigned i = 0; i < macro_num_args(e); i++)
|
|
|
|
args.push_back(copy_with_new_pos(macro_arg(e, i), p));
|
|
|
|
return save_pos(update_macro(e, args.size(), args.data()), p);
|
|
|
|
}}
|
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2014-06-11 22:07:20 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 01:00:52 +00:00
|
|
|
/** \brief Add \c ls to constants occurring in \c e. */
|
|
|
|
expr parser::propagate_levels(expr const & e, levels const & ls) {
|
|
|
|
if (is_nil(ls)) {
|
|
|
|
return e;
|
|
|
|
} else {
|
2014-07-19 11:53:01 +00:00
|
|
|
return replace(e, [&](expr const & e) {
|
2014-06-14 01:00:52 +00:00
|
|
|
if (is_constant(e)) {
|
|
|
|
auto it = m_env.find(const_name(e));
|
|
|
|
if (it) {
|
|
|
|
level_param_names const & univ_ps = it->get_univ_params();
|
|
|
|
levels const & old_ls = const_levels(e);
|
|
|
|
if (length(old_ls) + length(ls) <= length(univ_ps))
|
|
|
|
return some_expr(update_constant(e, append(old_ls, ls)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none_expr();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 22:07:20 +00:00
|
|
|
pos_info parser::pos_of(expr const & e, pos_info default_pos) {
|
|
|
|
auto it = m_pos_table->find(get_tag(e));
|
|
|
|
if (it == m_pos_table->end())
|
|
|
|
return default_pos;
|
|
|
|
else
|
|
|
|
return it->second;
|
2014-06-11 00:02:06 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 18:26:17 +00:00
|
|
|
bool parser::curr_is_token(name const & tk) const {
|
|
|
|
return
|
|
|
|
(curr() == scanner::token_kind::Keyword || curr() == scanner::token_kind::CommandKeyword) &&
|
|
|
|
get_token_info().value() == tk;
|
|
|
|
}
|
|
|
|
|
2014-06-12 16:08:38 +00:00
|
|
|
bool parser::curr_is_token_or_id(name const & tk) const {
|
|
|
|
if (curr() == scanner::token_kind::Keyword || curr() == scanner::token_kind::CommandKeyword)
|
|
|
|
return get_token_info().value() == tk;
|
|
|
|
else if (curr() == scanner::token_kind::Identifier)
|
|
|
|
return get_name_val() == tk;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-12 01:51:12 +00:00
|
|
|
void parser::check_token_next(name const & tk, char const * msg) {
|
|
|
|
if (!curr_is_token(tk))
|
|
|
|
throw parser_error(msg, pos());
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2014-06-13 22:13:32 +00:00
|
|
|
name parser::check_id_next(char const * msg) {
|
|
|
|
if (!curr_is_identifier())
|
|
|
|
throw parser_error(msg, pos());
|
|
|
|
name r = get_name_val();
|
|
|
|
next();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:56:51 +00:00
|
|
|
name parser::check_atomic_id_next(char const * msg) {
|
|
|
|
auto p = pos();
|
|
|
|
name id = check_id_next(msg);
|
|
|
|
if (!id.is_atomic())
|
|
|
|
throw parser_error(msg, p);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2014-06-11 22:07:20 +00:00
|
|
|
expr parser::mk_app(expr fn, expr arg, pos_info const & p) {
|
|
|
|
return save_pos(::lean::mk_app(fn, arg), p);
|
|
|
|
}
|
2014-06-11 18:26:17 +00:00
|
|
|
|
2014-06-18 00:15:38 +00:00
|
|
|
expr parser::mk_app(std::initializer_list<expr> const & args, pos_info const & p) {
|
2014-06-22 16:55:38 +00:00
|
|
|
lean_assert(args.size() >= 2);
|
2014-06-18 00:15:38 +00:00
|
|
|
auto it = args.begin();
|
|
|
|
expr r = *it;
|
|
|
|
it++;
|
|
|
|
for (; it != args.end(); it++)
|
|
|
|
r = mk_app(r, *it, p);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-06-13 18:24:26 +00:00
|
|
|
void parser::push_local_scope() {
|
2014-07-06 23:46:34 +00:00
|
|
|
m_local_level_decls.push();
|
2014-06-13 18:24:26 +00:00
|
|
|
m_local_decls.push();
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser::pop_local_scope() {
|
2014-07-06 23:46:34 +00:00
|
|
|
m_local_level_decls.pop();
|
2014-06-13 18:24:26 +00:00
|
|
|
m_local_decls.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser::add_local_level(name const & n, level const & l) {
|
|
|
|
if (m_env.is_universe(n))
|
|
|
|
throw exception(sstream() << "invalid universe declaration, '" << n << "' shadows a global universe");
|
|
|
|
if (m_local_level_decls.contains(n))
|
|
|
|
throw exception(sstream() << "invalid universe declaration, '" << n << "' shadows a local universe");
|
2014-06-14 16:56:05 +00:00
|
|
|
m_local_level_decls.insert(n, l);
|
2014-06-13 18:24:26 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 16:14:55 +00:00
|
|
|
void parser::add_local_expr(name const & n, expr const & p) {
|
2014-06-20 18:18:53 +00:00
|
|
|
m_local_decls.insert(n, p);
|
|
|
|
}
|
|
|
|
|
2014-06-14 16:56:05 +00:00
|
|
|
unsigned parser::get_local_level_index(name const & n) const {
|
|
|
|
return m_local_level_decls.find_idx(n);
|
2014-06-13 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 16:56:05 +00:00
|
|
|
unsigned parser::get_local_index(name const & n) const {
|
|
|
|
return m_local_decls.find_idx(n);
|
2014-06-13 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 06:00:41 +00:00
|
|
|
static name g_period("."), g_colon(":"), g_lparen("("), g_rparen(")"), g_llevel_curly(".{");
|
|
|
|
static name g_lcurly("{"), g_rcurly("}"), g_ldcurly("⦃"), g_rdcurly("⦄"), g_lbracket("["), g_rbracket("]");
|
|
|
|
static name g_bar("|"), g_comma(","), g_add("+"), g_max("max"), g_imax("imax"), g_cup("\u2294");
|
|
|
|
static name g_import("import"), g_show("show"), g_have("have"), g_assume("assume"), g_take("take"), g_fun("fun");
|
2014-06-12 16:08:38 +00:00
|
|
|
static unsigned g_level_add_prec = 10;
|
|
|
|
static unsigned g_level_cup_prec = 5;
|
|
|
|
|
2014-06-14 14:28:56 +00:00
|
|
|
unsigned parser::get_small_nat() {
|
2014-06-12 16:08:38 +00:00
|
|
|
mpz val = get_num_val().get_numerator();
|
|
|
|
lean_assert(val >= 0);
|
|
|
|
if (!val.is_unsigned_int())
|
2014-06-14 14:28:56 +00:00
|
|
|
throw parser_error("invalid level expression, value does not fit in a machine integer", pos());
|
2014-06-12 16:08:38 +00:00
|
|
|
return val.get_unsigned_int();
|
|
|
|
}
|
|
|
|
|
2014-06-14 14:28:56 +00:00
|
|
|
unsigned parser::parse_small_nat() {
|
2014-06-17 00:27:43 +00:00
|
|
|
if (!curr_is_numeral())
|
|
|
|
throw parser_error("(small) natural number expected", pos());
|
2014-06-14 14:28:56 +00:00
|
|
|
unsigned r = get_small_nat();
|
|
|
|
next();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-06-17 00:27:43 +00:00
|
|
|
double parser::parse_double() {
|
|
|
|
if (curr() != scanner::token_kind::Decimal)
|
|
|
|
throw parser_error("decimal value expected", pos());
|
|
|
|
return get_num_val().get_double();
|
|
|
|
}
|
|
|
|
|
2014-06-12 16:08:38 +00:00
|
|
|
static level lift(level l, unsigned k) {
|
|
|
|
while (k > 0) {
|
|
|
|
k--;
|
|
|
|
l = mk_succ(l);
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned parser::curr_level_lbp() const {
|
|
|
|
if (curr_is_token(g_cup))
|
|
|
|
return g_level_cup_prec;
|
|
|
|
else if (curr_is_token(g_add))
|
|
|
|
return g_level_add_prec;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
level parser::parse_max_imax(bool is_max) {
|
|
|
|
auto p = pos();
|
|
|
|
next();
|
|
|
|
buffer<level> lvls;
|
|
|
|
while (curr_is_identifier() || curr_is_numeral() || curr_is_token(g_lparen)) {
|
|
|
|
lvls.push_back(parse_level());
|
|
|
|
}
|
|
|
|
if (lvls.size() < 2)
|
|
|
|
throw parser_error("invalid level expression, max must have at least two arguments", p);
|
|
|
|
unsigned i = lvls.size() - 1;
|
|
|
|
level r = lvls[i];
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
if (is_max)
|
|
|
|
r = mk_max(lvls[i], r);
|
|
|
|
else
|
|
|
|
r = mk_imax(lvls[i], r);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
level parser::parse_level_id() {
|
|
|
|
auto p = pos();
|
|
|
|
name id = get_name_val();
|
|
|
|
next();
|
2014-06-14 16:56:05 +00:00
|
|
|
if (auto it = m_local_level_decls.find(id))
|
|
|
|
return *it;
|
2014-06-13 15:26:05 +00:00
|
|
|
if (m_env.is_universe(id))
|
2014-06-12 16:08:38 +00:00
|
|
|
return mk_global_univ(id);
|
2014-06-13 18:24:26 +00:00
|
|
|
if (auto it = get_alias_level(m_env, id))
|
|
|
|
return *it;
|
|
|
|
throw parser_error(sstream() << "unknown universe '" << id << "'", p);
|
2014-06-12 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
level parser::parse_level_nud() {
|
|
|
|
if (curr_is_token_or_id(g_max)) {
|
|
|
|
return parse_max_imax(true);
|
|
|
|
} else if (curr_is_token_or_id(g_imax)) {
|
|
|
|
return parse_max_imax(false);
|
|
|
|
} else if (curr_is_token(g_lparen)) {
|
|
|
|
next();
|
|
|
|
level l = parse_level();
|
|
|
|
check_token_next(g_rparen, "invalid level expression, ')' expected");
|
|
|
|
return l;
|
|
|
|
} else if (curr_is_numeral()) {
|
|
|
|
unsigned k = parse_small_nat();
|
|
|
|
return lift(level(), k);
|
|
|
|
} else if (curr_is_identifier()) {
|
|
|
|
return parse_level_id();
|
|
|
|
} else {
|
|
|
|
throw parser_error("invalid level expression", pos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
level parser::parse_level_led(level left) {
|
|
|
|
auto p = pos();
|
|
|
|
if (curr_is_token(g_cup)) {
|
|
|
|
next();
|
|
|
|
level right = parse_level(g_level_cup_prec);
|
|
|
|
return mk_max(left, right);
|
|
|
|
} else if (curr_is_token(g_add)) {
|
|
|
|
next();
|
|
|
|
if (curr_is_numeral()) {
|
|
|
|
unsigned k = parse_small_nat();
|
|
|
|
return lift(left, k);
|
|
|
|
} else {
|
|
|
|
throw parser_error("invalid level expression, right hand side of '+' (aka universe lift operator) must be a numeral", p);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw parser_error("invalid level expression", p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
level parser::parse_level(unsigned rbp) {
|
|
|
|
level left = parse_level_nud();
|
|
|
|
while (rbp < curr_level_lbp()) {
|
|
|
|
left = parse_level_led(left);
|
|
|
|
}
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2014-07-14 04:04:01 +00:00
|
|
|
std::tuple<expr, level_param_names> parser::elaborate_relaxed(expr const & e, list<expr> const & ctx) {
|
2014-07-08 21:28:33 +00:00
|
|
|
parser_pos_provider pp = get_pos_provider();
|
2014-07-14 04:04:01 +00:00
|
|
|
return ::lean::elaborate(m_env, m_local_level_decls, ctx, m_ios, e, &pp, false);
|
2014-06-13 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
2014-07-14 04:04:01 +00:00
|
|
|
std::tuple<expr, level_param_names> parser::elaborate_type(expr const & e, list<expr> const & ctx) {
|
2014-07-08 21:28:33 +00:00
|
|
|
parser_pos_provider pp = get_pos_provider();
|
2014-07-14 04:04:01 +00:00
|
|
|
return ::lean::elaborate(m_env, m_local_level_decls, ctx, m_ios, e, &pp, true, true);
|
2014-07-07 04:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<expr, level_param_names> parser::elaborate_at(environment const & env, expr const & e) {
|
2014-07-08 21:28:33 +00:00
|
|
|
parser_pos_provider pp = get_pos_provider();
|
2014-07-14 04:04:01 +00:00
|
|
|
return ::lean::elaborate(env, m_local_level_decls, list<expr>(), m_ios, e, &pp);
|
2014-06-28 22:33:56 +00:00
|
|
|
}
|
|
|
|
|
2014-07-07 04:54:16 +00:00
|
|
|
std::tuple<expr, expr, level_param_names> parser::elaborate_definition(name const & n, expr const & t, expr const & v) {
|
2014-07-08 21:28:33 +00:00
|
|
|
parser_pos_provider pp = get_pos_provider();
|
2014-07-06 23:46:34 +00:00
|
|
|
return ::lean::elaborate(m_env, m_local_level_decls, m_ios, n, t, v, &pp);
|
2014-06-14 00:30:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-21 16:32:13 +00:00
|
|
|
std::tuple<expr, expr, level_param_names> parser::elaborate_definition_at(environment const & env, local_level_decls const & lls,
|
|
|
|
name const & n, expr const & t, expr const & v) {
|
2014-07-12 21:50:57 +00:00
|
|
|
parser_pos_provider pp = get_pos_provider();
|
2014-07-21 16:32:13 +00:00
|
|
|
return ::lean::elaborate(env, lls, m_ios, n, t, v, &pp);
|
2014-07-12 21:50:57 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 23:52:20 +00:00
|
|
|
[[ noreturn ]] void throw_invalid_open_binder(pos_info const & pos) {
|
|
|
|
throw parser_error("invalid binder, '(', '{', '[', '{{', '⦃' or identifier expected", pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return an optional binder_info object based on the current token
|
|
|
|
- '(' : default
|
|
|
|
- '{' : implicit
|
|
|
|
- '{{' or '⦃' : strict implicit
|
|
|
|
- '[' : cast
|
|
|
|
*/
|
|
|
|
optional<binder_info> parser::parse_optional_binder_info() {
|
|
|
|
if (curr_is_token(g_lparen)) {
|
|
|
|
next();
|
|
|
|
return some(binder_info());
|
|
|
|
} else if (curr_is_token(g_lcurly)) {
|
|
|
|
next();
|
|
|
|
if (curr_is_token(g_lcurly)) {
|
|
|
|
next();
|
|
|
|
return some(mk_strict_implicit_binder_info());
|
|
|
|
} else {
|
|
|
|
return some(mk_implicit_binder_info());
|
|
|
|
}
|
|
|
|
} else if (curr_is_token(g_lbracket)) {
|
|
|
|
next();
|
|
|
|
return some(mk_cast_binder_info());
|
|
|
|
} else if (curr_is_token(g_ldcurly)) {
|
|
|
|
next();
|
|
|
|
return some(mk_strict_implicit_binder_info());
|
|
|
|
} else {
|
|
|
|
return optional<binder_info>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return binder_info object based on the current token, it fails if the current token
|
|
|
|
is not '(', '{', '{{', '⦃', or '['
|
|
|
|
|
|
|
|
\see parse_optional_binder_info
|
|
|
|
*/
|
|
|
|
binder_info parser::parse_binder_info() {
|
|
|
|
auto p = pos();
|
|
|
|
if (auto bi = parse_optional_binder_info()) {
|
|
|
|
return *bi;
|
|
|
|
} else {
|
|
|
|
throw_invalid_open_binder(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Consume the next token based on the value of \c bi
|
|
|
|
- none : do not consume anything
|
|
|
|
- default : consume ')'
|
|
|
|
- implicit : consume '}'
|
|
|
|
- strict implicit : consume '}}' or '⦄'
|
|
|
|
- cast : consume ']'
|
|
|
|
*/
|
|
|
|
void parser::parse_close_binder_info(optional<binder_info> const & bi) {
|
|
|
|
if (!bi) {
|
|
|
|
return;
|
|
|
|
} else if (bi->is_implicit()) {
|
|
|
|
check_token_next(g_rcurly, "invalid declaration, '}' expected");
|
|
|
|
} else if (bi->is_cast()) {
|
|
|
|
check_token_next(g_rbracket, "invalid declaration, ']' expected");
|
|
|
|
} else if (bi->is_strict_implicit()) {
|
|
|
|
if (curr_is_token(g_rcurly)) {
|
|
|
|
next();
|
|
|
|
check_token_next(g_rcurly, "invalid declaration, '}' expected");
|
|
|
|
} else {
|
|
|
|
check_token_next(g_rdcurly, "invalid declaration, '⦄' expected");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
check_token_next(g_rparen, "invalid declaration, ')' expected");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 01:51:12 +00:00
|
|
|
/** \brief Parse <tt>ID ':' expr</tt>, where the expression represents the type of the identifier. */
|
2014-06-30 16:14:55 +00:00
|
|
|
expr parser::parse_binder_core(binder_info const & bi) {
|
2014-06-12 01:51:12 +00:00
|
|
|
auto p = pos();
|
2014-07-08 01:56:51 +00:00
|
|
|
name id = check_atomic_id_next("invalid binder, atomic identifier expected");
|
2014-06-12 01:51:12 +00:00
|
|
|
expr type;
|
|
|
|
if (curr_is_token(g_colon)) {
|
|
|
|
next();
|
|
|
|
type = parse_expr();
|
|
|
|
} else {
|
|
|
|
type = save_pos(mk_expr_placeholder(), p);
|
|
|
|
}
|
2014-06-30 16:14:55 +00:00
|
|
|
return save_pos(mk_local(id, type, bi), p);
|
2014-06-12 01:51:12 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 16:14:55 +00:00
|
|
|
expr parser::parse_binder() {
|
2014-06-12 01:51:12 +00:00
|
|
|
if (curr_is_identifier()) {
|
|
|
|
return parse_binder_core(binder_info());
|
|
|
|
} else {
|
2014-06-30 23:52:20 +00:00
|
|
|
binder_info bi = parse_binder_info();
|
|
|
|
auto r = parse_binder_core(bi);
|
|
|
|
parse_close_binder_info(bi);
|
|
|
|
return r;
|
2014-06-12 01:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Parse <tt>ID ... ID ':' expr</tt>, where the expression
|
|
|
|
represents the type of the identifiers.
|
|
|
|
*/
|
2014-06-30 16:14:55 +00:00
|
|
|
void parser::parse_binder_block(buffer<expr> & r, binder_info const & bi) {
|
2014-06-12 01:51:12 +00:00
|
|
|
buffer<std::pair<pos_info, name>> names;
|
2014-07-08 01:56:51 +00:00
|
|
|
while (curr_is_identifier()) {
|
|
|
|
auto p = pos();
|
|
|
|
names.emplace_back(p, check_atomic_id_next("invalid binder, atomic identifier expected"));
|
|
|
|
}
|
2014-06-12 01:51:12 +00:00
|
|
|
if (names.empty())
|
|
|
|
throw parser_error("invalid binder, identifier expected", pos());
|
|
|
|
optional<expr> type;
|
|
|
|
if (curr_is_token(g_colon)) {
|
|
|
|
next();
|
|
|
|
type = parse_expr();
|
|
|
|
}
|
|
|
|
for (auto p : names) {
|
|
|
|
expr arg_type = type ? *type : save_pos(mk_expr_placeholder(), p.first);
|
2014-06-30 16:14:55 +00:00
|
|
|
expr local = save_pos(mk_local(p.second, arg_type, bi), p.first);
|
|
|
|
add_local(local);
|
|
|
|
r.push_back(local);
|
2014-06-12 01:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 16:14:55 +00:00
|
|
|
void parser::parse_binders_core(buffer<expr> & r) {
|
2014-06-30 23:52:20 +00:00
|
|
|
while (true) {
|
|
|
|
if (curr_is_identifier()) {
|
2014-06-15 18:30:52 +00:00
|
|
|
parse_binder_block(r, binder_info());
|
2014-06-26 00:47:38 +00:00
|
|
|
} else {
|
2014-06-30 23:52:20 +00:00
|
|
|
optional<binder_info> bi = parse_optional_binder_info();
|
|
|
|
if (bi) {
|
|
|
|
if (!parse_local_notation_decl())
|
|
|
|
parse_binder_block(r, *bi);
|
|
|
|
parse_close_binder_info(bi);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-26 00:47:38 +00:00
|
|
|
}
|
2014-06-12 01:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 16:14:55 +00:00
|
|
|
local_environment parser::parse_binders(buffer<expr> & r) {
|
2014-06-15 18:30:52 +00:00
|
|
|
flet<environment> save(m_env, m_env); // save environment
|
2014-06-14 16:56:05 +00:00
|
|
|
local_expr_decls::mk_scope scope(m_local_decls);
|
2014-06-12 01:51:12 +00:00
|
|
|
unsigned old_sz = r.size();
|
|
|
|
parse_binders_core(r);
|
|
|
|
if (old_sz == r.size())
|
2014-06-30 23:52:20 +00:00
|
|
|
throw_invalid_open_binder(pos());
|
2014-06-15 18:30:52 +00:00
|
|
|
return local_environment(m_env);
|
2014-06-12 01:51:12 +00:00
|
|
|
}
|
2014-06-11 22:07:20 +00:00
|
|
|
|
2014-06-16 16:24:41 +00:00
|
|
|
bool parser::parse_local_notation_decl() {
|
2014-06-17 17:12:04 +00:00
|
|
|
if (curr_is_notation_decl(*this)) {
|
|
|
|
buffer<token_entry> new_tokens;
|
|
|
|
auto ne = ::lean::parse_notation(*this, false, new_tokens);
|
|
|
|
for (auto const & te : new_tokens)
|
|
|
|
m_env = add_token(m_env, te);
|
|
|
|
m_env = add_notation(m_env, ne);
|
2014-06-16 16:24:41 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:56:10 +00:00
|
|
|
expr parser::parse_notation(parse_table t, expr * left) {
|
|
|
|
lean_assert(curr() == scanner::token_kind::Keyword);
|
|
|
|
auto p = pos();
|
2014-06-30 16:14:55 +00:00
|
|
|
buffer<expr> args;
|
|
|
|
buffer<expr> ps;
|
2014-06-15 18:30:52 +00:00
|
|
|
local_environment lenv(m_env);
|
2014-06-16 20:00:35 +00:00
|
|
|
pos_info binder_pos;
|
2014-06-12 03:56:10 +00:00
|
|
|
if (left)
|
|
|
|
args.push_back(*left);
|
|
|
|
while (true) {
|
|
|
|
if (curr() != scanner::token_kind::Keyword)
|
|
|
|
break;
|
2014-06-16 19:28:58 +00:00
|
|
|
auto r = t.find(get_token_info().value());
|
|
|
|
if (!r)
|
2014-06-12 03:56:10 +00:00
|
|
|
break;
|
|
|
|
next();
|
2014-06-16 19:28:58 +00:00
|
|
|
notation::action const & a = r->first;
|
2014-06-12 03:56:10 +00:00
|
|
|
switch (a.kind()) {
|
|
|
|
case notation::action_kind::Skip:
|
|
|
|
break;
|
|
|
|
case notation::action_kind::Expr:
|
|
|
|
args.push_back(parse_expr(a.rbp()));
|
|
|
|
break;
|
|
|
|
case notation::action_kind::Exprs: {
|
|
|
|
buffer<expr> r_args;
|
|
|
|
r_args.push_back(parse_expr(a.rbp()));
|
|
|
|
while (curr_is_token(a.get_sep())) {
|
2014-06-15 17:40:18 +00:00
|
|
|
next();
|
2014-06-12 03:56:10 +00:00
|
|
|
r_args.push_back(parse_expr(a.rbp()));
|
|
|
|
}
|
2014-07-13 00:24:55 +00:00
|
|
|
expr r = instantiate_rev(copy_with_new_pos(a.get_initial(), p), args.size(), args.data());
|
|
|
|
expr rec = copy_with_new_pos(a.get_rec(), p);
|
2014-06-12 03:56:10 +00:00
|
|
|
if (a.is_fold_right()) {
|
|
|
|
unsigned i = r_args.size();
|
2014-06-15 17:40:18 +00:00
|
|
|
while (i > 0) {
|
2014-06-12 03:56:10 +00:00
|
|
|
--i;
|
|
|
|
args.push_back(r_args[i]);
|
|
|
|
args.push_back(r);
|
2014-07-13 00:24:55 +00:00
|
|
|
r = instantiate_rev(rec, args.size(), args.data());
|
2014-06-12 03:56:10 +00:00
|
|
|
args.pop_back(); args.pop_back();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0; i < r_args.size(); i++) {
|
|
|
|
args.push_back(r_args[i]);
|
|
|
|
args.push_back(r);
|
2014-07-13 00:24:55 +00:00
|
|
|
r = instantiate_rev(rec, args.size(), args.data());
|
2014-06-12 03:56:10 +00:00
|
|
|
args.pop_back(); args.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args.push_back(r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case notation::action_kind::Binder:
|
2014-06-16 20:00:35 +00:00
|
|
|
binder_pos = pos();
|
2014-06-12 03:56:10 +00:00
|
|
|
ps.push_back(parse_binder());
|
|
|
|
break;
|
|
|
|
case notation::action_kind::Binders:
|
2014-06-16 20:00:35 +00:00
|
|
|
binder_pos = pos();
|
2014-06-15 18:30:52 +00:00
|
|
|
lenv = parse_binders(ps);
|
2014-06-12 03:56:10 +00:00
|
|
|
break;
|
|
|
|
case notation::action_kind::ScopedExpr: {
|
2014-07-13 00:24:55 +00:00
|
|
|
expr r = parse_scoped_expr(ps, lenv, a.rbp());
|
2014-06-12 03:56:10 +00:00
|
|
|
if (is_var(a.get_rec(), 0)) {
|
2014-07-14 04:41:44 +00:00
|
|
|
if (a.use_lambda_abstraction())
|
|
|
|
r = Fun(ps, r);
|
|
|
|
else
|
|
|
|
r = Pi(ps, r);
|
|
|
|
r = rec_save_pos(r, binder_pos);
|
2014-06-12 03:56:10 +00:00
|
|
|
} else {
|
2014-07-13 00:24:55 +00:00
|
|
|
expr rec = copy_with_new_pos(a.get_rec(), p);
|
2014-06-12 03:56:10 +00:00
|
|
|
unsigned i = ps.size();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
2014-06-30 16:14:55 +00:00
|
|
|
expr const & l = ps[i];
|
2014-06-12 03:56:10 +00:00
|
|
|
if (a.use_lambda_abstraction())
|
2014-06-30 16:14:55 +00:00
|
|
|
r = Fun(l, r);
|
2014-06-12 03:56:10 +00:00
|
|
|
else
|
2014-06-30 16:14:55 +00:00
|
|
|
r = Pi(l, r);
|
|
|
|
r = save_pos(r, binder_pos);
|
2014-06-12 03:56:10 +00:00
|
|
|
args.push_back(r);
|
2014-07-13 00:24:55 +00:00
|
|
|
r = instantiate_rev(rec, args.size(), args.data());
|
2014-06-12 03:56:10 +00:00
|
|
|
args.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args.push_back(r);
|
|
|
|
break;
|
|
|
|
}
|
2014-06-18 03:39:42 +00:00
|
|
|
case notation::action_kind::LuaExt:
|
2014-06-18 15:03:17 +00:00
|
|
|
m_last_script_pos = p;
|
2014-06-18 03:39:42 +00:00
|
|
|
using_script([&](lua_State * L) {
|
|
|
|
scoped_set_parser scope(L, *this);
|
|
|
|
lua_getglobal(L, a.get_lua_fn().c_str());
|
|
|
|
if (!lua_isfunction(L, -1))
|
|
|
|
throw parser_error(sstream() << "failed to use notation implemented in Lua, Lua state does not contain function '"
|
|
|
|
<< a.get_lua_fn() << "'", p);
|
|
|
|
lua_pushinteger(L, p.first);
|
|
|
|
lua_pushinteger(L, p.second);
|
|
|
|
for (unsigned i = 0; i < args.size(); i++)
|
|
|
|
push_expr(L, args[i]);
|
|
|
|
pcall(L, args.size() + 2, 1, 0);
|
|
|
|
if (!is_expr(L, -1))
|
|
|
|
throw parser_error(sstream() << "failed to use notation implemented in Lua, value returned by function '"
|
|
|
|
<< a.get_lua_fn() << "' is not an expression", p);
|
2014-06-18 15:03:17 +00:00
|
|
|
args.push_back(rec_save_pos(to_expr(L, -1), p));
|
2014-06-18 03:39:42 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
});
|
|
|
|
break;
|
2014-06-12 03:56:10 +00:00
|
|
|
case notation::action_kind::Ext:
|
2014-06-16 19:28:58 +00:00
|
|
|
args.push_back(a.get_parse_fn()(*this, args.size(), args.data(), p));
|
2014-06-12 03:56:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-06-18 03:39:42 +00:00
|
|
|
|
2014-06-16 19:28:58 +00:00
|
|
|
t = r->second;
|
2014-06-12 03:56:10 +00:00
|
|
|
}
|
|
|
|
list<expr> const & as = t.is_accepting();
|
|
|
|
if (is_nil(as)) {
|
|
|
|
if (p == pos())
|
|
|
|
throw parser_error(sstream() << "invalid expression", pos());
|
|
|
|
else
|
|
|
|
throw parser_error(sstream() << "invalid expression starting at " << p.first << ":" << p.second, pos());
|
|
|
|
}
|
|
|
|
buffer<expr> cs;
|
|
|
|
for (expr const & a : as) {
|
2014-06-15 17:40:18 +00:00
|
|
|
expr r = instantiate_rev(copy_with_new_pos(a, p), args.size(), args.data());
|
|
|
|
cs.push_back(r);
|
2014-06-12 03:56:10 +00:00
|
|
|
}
|
2014-06-26 15:50:44 +00:00
|
|
|
return save_pos(mk_choice(cs.size(), cs.data()), p);
|
2014-06-11 22:07:20 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 03:56:10 +00:00
|
|
|
expr parser::parse_nud_notation() {
|
2014-06-15 05:13:25 +00:00
|
|
|
return parse_notation(nud(), nullptr);
|
2014-06-12 03:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr parser::parse_led_notation(expr left) {
|
2014-06-15 05:13:25 +00:00
|
|
|
if (led().find(get_token_info().value()))
|
|
|
|
return parse_notation(led(), &left);
|
2014-06-12 03:56:10 +00:00
|
|
|
else
|
|
|
|
return mk_app(left, parse_nud_notation(), pos_of(left));
|
2014-06-11 22:07:20 +00:00
|
|
|
}
|
2014-06-12 03:56:10 +00:00
|
|
|
|
2014-06-21 00:17:39 +00:00
|
|
|
expr parser::id_to_expr(name const & id, pos_info const & p) {
|
2014-06-13 18:24:26 +00:00
|
|
|
buffer<level> lvl_buffer;
|
|
|
|
levels ls;
|
|
|
|
if (curr_is_token(g_llevel_curly)) {
|
|
|
|
next();
|
|
|
|
while (!curr_is_token(g_rcurly)) {
|
|
|
|
lvl_buffer.push_back(parse_level());
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
ls = to_list(lvl_buffer.begin(), lvl_buffer.end());
|
|
|
|
}
|
2014-07-08 01:56:51 +00:00
|
|
|
if (id.is_atomic()) {
|
|
|
|
// locals
|
|
|
|
if (auto it1 = m_local_decls.find(id))
|
|
|
|
return copy_with_new_pos(propagate_levels(*it1, ls), p);
|
|
|
|
optional<expr> r;
|
|
|
|
// globals
|
|
|
|
if (m_env.find(id))
|
|
|
|
r = save_pos(mk_constant(id, ls), p);
|
|
|
|
// aliases
|
|
|
|
auto as = get_alias_exprs(m_env, id);
|
|
|
|
if (!is_nil(as)) {
|
|
|
|
buffer<expr> new_as;
|
|
|
|
if (r)
|
|
|
|
new_as.push_back(*r);
|
|
|
|
for (auto const & e : as) {
|
|
|
|
new_as.push_back(copy_with_new_pos(propagate_levels(e, ls), p));
|
|
|
|
}
|
|
|
|
r = save_pos(mk_choice(new_as.size(), new_as.data()), p);
|
|
|
|
}
|
|
|
|
if (!r && m_no_undef_id_error)
|
|
|
|
r = save_pos(mk_constant(get_namespace(m_env) + id, ls), p);
|
|
|
|
if (!r)
|
|
|
|
throw parser_error(sstream() << "unknown identifier '" << id << "'", p);
|
|
|
|
return *r;
|
|
|
|
} else {
|
2014-07-08 02:15:46 +00:00
|
|
|
name new_id = remove_root_prefix(id);
|
|
|
|
if (m_env.find(new_id)) {
|
|
|
|
return save_pos(mk_constant(new_id, ls), p);
|
2014-07-08 01:56:51 +00:00
|
|
|
} else {
|
|
|
|
for (name const & ns : get_namespaces(m_env)) {
|
|
|
|
auto new_id = ns + id;
|
|
|
|
if (m_env.find(new_id))
|
|
|
|
return save_pos(mk_constant(new_id, ls), p);
|
|
|
|
}
|
2014-06-13 18:24:26 +00:00
|
|
|
}
|
|
|
|
throw parser_error(sstream() << "unknown identifier '" << id << "'", p);
|
2014-07-08 01:56:51 +00:00
|
|
|
}
|
2014-06-11 22:07:20 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 00:19:47 +00:00
|
|
|
name parser::check_constant_next(char const * msg) {
|
|
|
|
auto p = pos();
|
|
|
|
name id = check_id_next(msg);
|
|
|
|
expr e = id_to_expr(id, p);
|
|
|
|
if (!is_constant(e))
|
|
|
|
throw parser_error(msg, p);
|
|
|
|
return const_name(e);
|
|
|
|
}
|
|
|
|
|
2014-06-21 00:17:39 +00:00
|
|
|
expr parser::parse_id() {
|
|
|
|
auto p = pos();
|
|
|
|
name id = get_name_val();
|
|
|
|
next();
|
|
|
|
return id_to_expr(id, p);
|
|
|
|
}
|
|
|
|
|
2014-06-11 22:07:20 +00:00
|
|
|
expr parser::parse_numeral_expr() {
|
2014-07-02 15:08:35 +00:00
|
|
|
auto p = pos();
|
|
|
|
mpz n = get_num_val().get_numerator();
|
|
|
|
next();
|
|
|
|
if (!m_has_num)
|
|
|
|
m_has_num = has_num_decls(m_env);
|
|
|
|
if (!*m_has_num)
|
|
|
|
throw parser_error("numeral cannot be encoded as expression, environment does not contain the type 'num' "
|
|
|
|
"(solution: use 'import num')", p);
|
|
|
|
return from_num(n);
|
2014-06-11 22:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr parser::parse_decimal_expr() {
|
|
|
|
// TODO(Leo)
|
2014-07-01 23:46:58 +00:00
|
|
|
next(); // to avoid loop
|
2014-06-11 22:07:20 +00:00
|
|
|
return expr();
|
|
|
|
}
|
|
|
|
|
|
|
|
expr parser::parse_string_expr() {
|
2014-07-02 17:00:55 +00:00
|
|
|
auto p = pos();
|
|
|
|
std::string v = get_str_val();
|
|
|
|
next();
|
|
|
|
if (!m_has_string)
|
|
|
|
m_has_string = has_string_decls(m_env);
|
|
|
|
if (!*m_has_string)
|
|
|
|
throw parser_error("string cannot be encoded as expression, environment does not contain the type 'string' "
|
|
|
|
"(solution: use 'import string')", p);
|
|
|
|
return from_string(v);
|
2014-06-11 00:02:06 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 22:07:20 +00:00
|
|
|
expr parser::parse_nud() {
|
|
|
|
switch (curr()) {
|
2014-06-12 03:56:10 +00:00
|
|
|
case scanner::token_kind::Keyword: return parse_nud_notation();
|
2014-06-11 22:07:20 +00:00
|
|
|
case scanner::token_kind::Identifier: return parse_id();
|
|
|
|
case scanner::token_kind::Numeral: return parse_numeral_expr();
|
|
|
|
case scanner::token_kind::Decimal: return parse_decimal_expr();
|
|
|
|
case scanner::token_kind::String: return parse_string_expr();
|
|
|
|
default: throw parser_error("invalid expression, unexpected token", pos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expr parser::parse_led(expr left) {
|
|
|
|
switch (curr()) {
|
2014-06-12 03:56:10 +00:00
|
|
|
case scanner::token_kind::Keyword: return parse_led_notation(left);
|
2014-06-11 22:07:20 +00:00
|
|
|
case scanner::token_kind::Identifier: return mk_app(left, parse_id(), pos_of(left));
|
|
|
|
case scanner::token_kind::Numeral: return mk_app(left, parse_numeral_expr(), pos_of(left));
|
|
|
|
case scanner::token_kind::Decimal: return mk_app(left, parse_decimal_expr(), pos_of(left));
|
|
|
|
case scanner::token_kind::String: return mk_app(left, parse_string_expr(), pos_of(left));
|
|
|
|
default: return left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned parser::curr_lbp() const {
|
2014-06-12 03:56:10 +00:00
|
|
|
switch (curr()) {
|
|
|
|
case scanner::token_kind::Keyword:
|
2014-06-11 22:07:20 +00:00
|
|
|
return get_token_info().precedence();
|
2014-06-12 03:56:10 +00:00
|
|
|
case scanner::token_kind::CommandKeyword: case scanner::token_kind::Eof:
|
|
|
|
case scanner::token_kind::ScriptBlock:
|
2014-06-11 22:07:20 +00:00
|
|
|
return 0;
|
2014-06-12 03:56:10 +00:00
|
|
|
case scanner::token_kind::Identifier: case scanner::token_kind::Numeral:
|
|
|
|
case scanner::token_kind::Decimal: case scanner::token_kind::String:
|
2014-06-15 05:13:25 +00:00
|
|
|
case scanner::token_kind::QuotedSymbol:
|
2014-06-12 03:56:10 +00:00
|
|
|
return std::numeric_limits<unsigned>::max();
|
|
|
|
}
|
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2014-06-11 22:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr parser::parse_expr(unsigned rbp) {
|
|
|
|
expr left = parse_nud();
|
|
|
|
while (rbp < curr_lbp()) {
|
|
|
|
left = parse_led(left);
|
|
|
|
}
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2014-06-30 16:14:55 +00:00
|
|
|
expr parser::parse_scoped_expr(unsigned num_ps, expr const * ps, local_environment const & lenv, unsigned rbp) {
|
2014-06-23 23:10:36 +00:00
|
|
|
local_scope scope(*this);
|
|
|
|
m_env = lenv;
|
2014-06-30 16:14:55 +00:00
|
|
|
for (unsigned i = 0; i < num_ps; i++)
|
|
|
|
add_local(ps[i]);
|
2014-06-23 23:10:36 +00:00
|
|
|
return parse_expr(rbp);
|
2014-06-11 00:02:06 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 18:26:17 +00:00
|
|
|
void parser::parse_command() {
|
|
|
|
lean_assert(curr() == scanner::token_kind::CommandKeyword);
|
2014-06-13 18:24:26 +00:00
|
|
|
m_last_cmd_pos = pos();
|
2014-06-11 18:26:17 +00:00
|
|
|
name const & cmd_name = get_token_info().value();
|
|
|
|
if (auto it = cmds().find(cmd_name)) {
|
|
|
|
next();
|
|
|
|
m_env = it->get_fn()(*this);
|
|
|
|
} else {
|
|
|
|
auto p = pos();
|
|
|
|
next();
|
|
|
|
throw parser_error(sstream() << "unknown command '" << cmd_name << "'", p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser::parse_script(bool as_expr) {
|
|
|
|
m_last_script_pos = pos();
|
|
|
|
std::string script_code = m_scanner.get_str_val();
|
|
|
|
if (as_expr)
|
|
|
|
script_code = "return " + script_code;
|
|
|
|
next();
|
|
|
|
using_script([&](lua_State * L) {
|
|
|
|
dostring(L, script_code.c_str());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-16 23:37:46 +00:00
|
|
|
static optional<std::string> find_file(name const & f, char const * ext) {
|
|
|
|
try {
|
|
|
|
return optional<std::string>(find_file(f, {ext}));
|
|
|
|
} catch (...) {
|
|
|
|
return optional<std::string>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 06:23:51 +00:00
|
|
|
static std::string g_lua_module_key("lua_module");
|
|
|
|
static void lua_module_reader(deserializer & d, module_idx, shared_environment &,
|
|
|
|
std::function<void(asynch_update_fn const &)> &,
|
|
|
|
std::function<void(delayed_update_fn const &)> & add_delayed_update) {
|
2014-06-29 17:48:57 +00:00
|
|
|
name fname;
|
2014-06-28 06:23:51 +00:00
|
|
|
d >> fname;
|
|
|
|
add_delayed_update([=](environment const & env, io_state const &) -> environment {
|
2014-06-29 17:48:57 +00:00
|
|
|
std::string rname = find_file(fname, {".lua"});
|
|
|
|
system_import(rname.c_str());
|
2014-06-28 06:23:51 +00:00
|
|
|
return env;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
register_module_object_reader_fn g_lua_module_reader(g_lua_module_key, lua_module_reader);
|
|
|
|
|
2014-06-16 23:37:46 +00:00
|
|
|
void parser::parse_imports() {
|
2014-06-29 17:48:57 +00:00
|
|
|
buffer<name> olean_files;
|
|
|
|
buffer<name> lua_files;
|
2014-06-16 23:37:46 +00:00
|
|
|
while (curr_is_token(g_import)) {
|
|
|
|
m_last_cmd_pos = pos();
|
|
|
|
next();
|
|
|
|
while (curr_is_identifier()) {
|
|
|
|
name f = get_name_val();
|
|
|
|
if (auto it = find_file(f, ".lua")) {
|
2014-06-29 17:48:57 +00:00
|
|
|
lua_files.push_back(f);
|
2014-06-16 23:37:46 +00:00
|
|
|
} else if (auto it = find_file(f, ".olean")) {
|
2014-06-29 17:48:57 +00:00
|
|
|
olean_files.push_back(f);
|
2014-06-16 23:37:46 +00:00
|
|
|
} else {
|
|
|
|
throw parser_error(sstream() << "invalid import, unknow module '" << f << "'", pos());
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
2014-07-26 05:46:49 +00:00
|
|
|
unsigned num_threads = 0;
|
|
|
|
if (get_parser_parallel_import(m_ios.get_options()))
|
|
|
|
num_threads = m_num_threads;
|
|
|
|
m_env = import_modules(m_env, olean_files.size(), olean_files.data(), num_threads, true, m_ios);
|
2014-06-28 06:23:51 +00:00
|
|
|
for (auto const & f : lua_files) {
|
2014-06-29 17:48:57 +00:00
|
|
|
std::string rname = find_file(f, {".lua"});
|
|
|
|
system_import(rname.c_str());
|
2014-06-28 06:23:51 +00:00
|
|
|
m_env = module::add(m_env, g_lua_module_key, [=](serializer & s) {
|
|
|
|
s << f;
|
|
|
|
});
|
|
|
|
}
|
2014-06-16 23:37:46 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 18:26:17 +00:00
|
|
|
bool parser::parse_commands() {
|
2014-06-11 22:07:20 +00:00
|
|
|
// We disable hash-consing while parsing to make sure the pos-info are correct.
|
|
|
|
scoped_expr_caching disable(false);
|
2014-06-11 18:26:17 +00:00
|
|
|
try {
|
|
|
|
bool done = false;
|
2014-06-16 23:37:46 +00:00
|
|
|
parse_imports();
|
2014-06-11 18:26:17 +00:00
|
|
|
while (!done) {
|
|
|
|
protected_call([&]() {
|
|
|
|
check_interrupted();
|
|
|
|
switch (curr()) {
|
|
|
|
case scanner::token_kind::CommandKeyword:
|
|
|
|
parse_command();
|
|
|
|
break;
|
|
|
|
case scanner::token_kind::ScriptBlock:
|
|
|
|
parse_script();
|
|
|
|
break;
|
|
|
|
case scanner::token_kind::Eof:
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
case scanner::token_kind::Keyword:
|
|
|
|
if (curr_is_token(g_period)) {
|
|
|
|
next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw parser_error("command expected", pos());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[&]() { sync_command(); });
|
|
|
|
}
|
|
|
|
} catch (interrupt_parser) {}
|
2014-07-12 21:50:57 +00:00
|
|
|
for (certified_declaration const & thm : m_theorem_queue.join()) {
|
|
|
|
m_env.replace(thm);
|
|
|
|
}
|
2014-06-11 18:26:17 +00:00
|
|
|
return !m_found_errors;
|
|
|
|
}
|
|
|
|
|
2014-07-26 21:01:20 +00:00
|
|
|
void parser::add_delayed_theorem(environment const & env, name const & n, level_param_names const & ls, expr const & t, expr const & v) {
|
2014-07-26 21:26:34 +00:00
|
|
|
m_theorem_queue.add(env, n, ls, get_local_level_decls(), t, v);
|
2014-07-26 21:01:20 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 04:38:10 +00:00
|
|
|
bool parse_commands(environment & env, io_state & ios, std::istream & in, char const * strm_name, bool use_exceptions,
|
2014-06-16 23:37:46 +00:00
|
|
|
unsigned num_threads) {
|
2014-06-18 04:38:10 +00:00
|
|
|
parser p(env, ios, in, strm_name, use_exceptions, num_threads);
|
2014-06-11 18:26:17 +00:00
|
|
|
bool r = p();
|
|
|
|
ios = p.ios();
|
|
|
|
env = p.env();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-06-18 04:38:10 +00:00
|
|
|
bool parse_commands(environment & env, io_state & ios, char const * fname, bool use_exceptions, unsigned num_threads) {
|
2014-06-11 18:26:17 +00:00
|
|
|
std::ifstream in(fname);
|
|
|
|
if (in.bad() || in.fail())
|
|
|
|
throw exception(sstream() << "failed to open file '" << fname << "'");
|
2014-06-18 04:38:10 +00:00
|
|
|
return parse_commands(env, ios, in, fname, use_exceptions, num_threads);
|
2014-06-11 18:26:17 +00:00
|
|
|
}
|
2014-06-11 00:02:06 +00:00
|
|
|
}
|