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-09-22 17:27:48 +00:00
|
|
|
#include "util/sexpr/option_declarations.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-07-27 16:41:25 +00:00
|
|
|
#include "kernel/error_msgs.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-29 20:04:58 +00:00
|
|
|
#include "library/explicit.h"
|
2014-08-29 16:58:11 +00:00
|
|
|
#include "library/let.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-08-17 22:55:58 +00:00
|
|
|
#include "library/sorry.h"
|
2014-10-15 01:26:21 +00:00
|
|
|
#include "library/flycheck.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-09-23 00:30:29 +00:00
|
|
|
#include "frontends/lean/tokens.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-07-27 16:41:25 +00:00
|
|
|
#include "frontends/lean/pp_options.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-09-22 17:27:48 +00:00
|
|
|
static name * g_parser_show_errors;
|
|
|
|
static name * g_parser_parallel_import;
|
2014-06-11 00:02:06 +00:00
|
|
|
|
2014-09-22 17:27:48 +00:00
|
|
|
bool get_parser_show_errors(options const & opts) {
|
|
|
|
return opts.get_bool(*g_parser_show_errors, LEAN_DEFAULT_PARSER_SHOW_ERRORS);
|
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
2014-10-26 22:47:29 +00:00
|
|
|
m_p.push_local_scope();
|
|
|
|
}
|
|
|
|
parser::local_scope::local_scope(parser & p, environment const & env):
|
|
|
|
m_p(p), m_env(p.env()) {
|
|
|
|
m_p.m_env = env;
|
|
|
|
m_p.push_local_scope();
|
2014-06-16 16:24:41 +00:00
|
|
|
}
|
|
|
|
parser::local_scope::~local_scope() {
|
|
|
|
m_p.pop_local_scope();
|
|
|
|
m_p.m_env = m_env;
|
|
|
|
}
|
|
|
|
|
2014-10-26 16:47:11 +00:00
|
|
|
parser::undef_id_to_const_scope::undef_id_to_const_scope(parser & p):
|
|
|
|
flet<undef_id_behavior>(p.m_undef_id_behavior, undef_id_behavior::AssumeConstant) {}
|
|
|
|
parser::undef_id_to_local_scope::undef_id_to_local_scope(parser & p):
|
|
|
|
flet<undef_id_behavior>(p.m_undef_id_behavior, undef_id_behavior::AssumeLocal) {}
|
2014-06-17 17:30:03 +00:00
|
|
|
|
2014-09-23 00:30:29 +00:00
|
|
|
static name * g_tmp_prefix = nullptr;
|
2014-06-21 00:17:39 +00:00
|
|
|
|
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-10-09 20:07:21 +00:00
|
|
|
snapshot const * s, snapshot_vector * sv, info_manager * im,
|
2014-10-13 17:05:38 +00:00
|
|
|
keep_theorem_mode tmode):
|
2014-09-23 00:30:29 +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-09-10 15:39:39 +00:00
|
|
|
m_scanner(strm, strm_name, s ? s->m_line : 1),
|
2014-08-07 01:07:04 +00:00
|
|
|
m_theorem_queue(*this, num_threads > 1 ? num_threads - 1 : 0),
|
2014-08-15 16:06:34 +00:00
|
|
|
m_snapshot_vector(sv), m_info_manager(im), m_cache(nullptr), m_index(nullptr) {
|
2014-10-13 17:05:38 +00:00
|
|
|
m_keep_theorem_mode = tmode;
|
2014-09-10 15:39:39 +00:00
|
|
|
if (s) {
|
2014-10-03 15:52:35 +00:00
|
|
|
m_local_level_decls = s->m_lds;
|
|
|
|
m_local_decls = s->m_eds;
|
2014-10-04 14:13:19 +00:00
|
|
|
m_level_variables = s->m_lvars;
|
2014-10-03 15:52:35 +00:00
|
|
|
m_variables = s->m_vars;
|
|
|
|
m_include_vars = s->m_include_vars;
|
|
|
|
m_parser_scope_stack = s->m_parser_scope_stack;
|
2014-09-10 15:39:39 +00:00
|
|
|
}
|
2014-06-16 23:37:46 +00:00
|
|
|
m_num_threads = num_threads;
|
2014-10-26 16:47:11 +00:00
|
|
|
m_undef_id_behavior = undef_id_behavior::Error;
|
2014-06-11 00:02:06 +00:00
|
|
|
m_found_errors = false;
|
2014-08-01 01:35:57 +00:00
|
|
|
m_used_sorry = false;
|
2014-06-11 00:02:06 +00:00
|
|
|
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-08-10 18:04:16 +00:00
|
|
|
void parser::cache_definition(name const & n, expr const & pre_type, expr const & pre_value,
|
|
|
|
level_param_names const & ls, expr const & type, expr const & value) {
|
|
|
|
if (m_cache)
|
2014-09-30 00:28:23 +00:00
|
|
|
m_cache->add(m_env, n, pre_type, pre_value, ls, type, value);
|
2014-08-10 18:04:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 15:26:39 +00:00
|
|
|
auto parser::find_cached_definition(name const & n, expr const & pre_type, expr const & pre_value)
|
|
|
|
-> optional<std::tuple<level_param_names, expr, expr>> {
|
2014-08-10 18:04:16 +00:00
|
|
|
if (m_cache)
|
2014-09-30 00:28:23 +00:00
|
|
|
return m_cache->find(m_env, n, pre_type, pre_value);
|
2014-08-10 18:04:16 +00:00
|
|
|
else
|
|
|
|
return optional<std::tuple<level_param_names, expr, expr>>();
|
|
|
|
}
|
|
|
|
|
2014-08-23 19:39:59 +00:00
|
|
|
void parser::add_decl_index(name const & n, pos_info const & pos, name const & k, expr const & t) {
|
2014-08-15 00:44:07 +00:00
|
|
|
if (m_index)
|
2014-08-23 19:39:59 +00:00
|
|
|
m_index->add_decl(get_stream_name(), pos, n, k, t);
|
2014-08-15 00:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void parser::add_ref_index(name const & n, pos_info const & pos) {
|
|
|
|
if (m_index)
|
|
|
|
m_index->add_ref(get_stream_name(), pos, n);
|
|
|
|
}
|
|
|
|
|
2014-09-04 16:30:25 +00:00
|
|
|
void parser::add_abbrev_index(name const & a, name const & d) {
|
|
|
|
if (m_index)
|
|
|
|
m_index->add_abbrev(a, d);
|
|
|
|
}
|
|
|
|
|
2014-08-17 19:14:42 +00:00
|
|
|
bool parser::are_info_lines_valid(unsigned start_line, unsigned end_line) const {
|
|
|
|
if (!m_info_manager)
|
|
|
|
return true; // we are not tracking info
|
|
|
|
for (unsigned i = start_line; i <= end_line; i++)
|
|
|
|
if (m_info_manager->is_invalidated(i))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-01 01:35:57 +00:00
|
|
|
expr parser::mk_sorry(pos_info const & p) {
|
|
|
|
m_used_sorry = true;
|
|
|
|
{
|
2014-09-26 15:55:54 +00:00
|
|
|
#ifndef LEAN_IGNORE_SORRY
|
|
|
|
// TODO(Leo): remove the #ifdef.
|
|
|
|
// The compilation option LEAN_IGNORE_SORRY is a temporary hack for the nightly builds
|
|
|
|
// We use it to avoid a buch of warnings on cdash.
|
2014-08-01 01:35:57 +00:00
|
|
|
flycheck_warning wrn(regular_stream());
|
2014-08-02 06:21:55 +00:00
|
|
|
display_warning_pos(p.first, p.second);
|
|
|
|
regular_stream() << " using 'sorry'" << endl;
|
2014-09-26 15:55:54 +00:00
|
|
|
#endif
|
2014-08-01 01:35:57 +00:00
|
|
|
}
|
2014-08-29 21:32:11 +00:00
|
|
|
return save_pos(::lean::mk_sorry(), p);
|
2014-08-01 01:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void parser::declare_sorry() {
|
|
|
|
m_used_sorry = true;
|
|
|
|
m_env = ::lean::declare_sorry(m_env);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2014-08-02 06:21:55 +00:00
|
|
|
void parser::display_warning_pos(unsigned line, unsigned pos) {
|
|
|
|
::lean::display_warning_pos(regular_stream(), get_stream_name().c_str(), line, pos);
|
|
|
|
}
|
|
|
|
void parser::display_warning_pos(pos_info p) { display_warning_pos(p.first, p.second); }
|
|
|
|
|
2014-06-11 00:02:06 +00:00
|
|
|
void parser::display_error_pos(unsigned line, unsigned pos) {
|
2014-08-02 06:21:55 +00:00
|
|
|
::lean::display_error_pos(regular_stream(), get_stream_name().c_str(), line, pos);
|
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) {
|
2014-08-01 00:41:39 +00:00
|
|
|
flycheck_error err(regular_stream());
|
2014-06-11 00:02:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-02 06:21:55 +00:00
|
|
|
void parser::display_error(char const * msg, pos_info p) { display_error(msg, p.first, p.second); }
|
2014-06-11 00:02:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-15 03:39:41 +00:00
|
|
|
void parser::display_error(script_exception const & ex) {
|
|
|
|
parser_pos_provider pos_provider(m_pos_table, get_stream_name(), m_last_script_pos);
|
|
|
|
::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) \
|
2014-08-07 06:15:05 +00:00
|
|
|
save_pre_info_data(); \
|
2014-06-11 00:02:06 +00:00
|
|
|
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-08-01 00:41:39 +00:00
|
|
|
CATCH(flycheck_error err(regular_stream()); 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) {
|
2014-08-07 06:15:05 +00:00
|
|
|
save_pre_info_data();
|
2014-06-11 00:02:06 +00:00
|
|
|
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();
|
2014-08-16 00:24:37 +00:00
|
|
|
if (m_use_exceptions || m_info_manager)
|
2014-06-11 00:02:06 +00:00
|
|
|
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();
|
2014-08-17 19:14:42 +00:00
|
|
|
if (m_info_manager)
|
|
|
|
m_info_manager->commit_upto(m_scanner.get_line()+1, false);
|
2014-06-11 00:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-13 01:43:56 +00:00
|
|
|
auto t = get_tag(e);
|
|
|
|
if (!m_pos_table.contains(t))
|
|
|
|
m_pos_table.insert(t, 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-08-29 16:58:11 +00:00
|
|
|
if (is_let_value(e))
|
2014-08-28 23:27:52 +00:00
|
|
|
return e;
|
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-10-01 17:23:05 +00:00
|
|
|
if (is_constant(e))
|
|
|
|
return some_expr(update_constant(e, ls));
|
2014-06-14 01:00:52 +00:00
|
|
|
return none_expr();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 22:07:20 +00:00
|
|
|
pos_info parser::pos_of(expr const & e, pos_info default_pos) {
|
2014-08-13 01:43:56 +00:00
|
|
|
if (auto it = m_pos_table.find(get_tag(e)))
|
|
|
|
return *it;
|
2014-06-11 22:07:20 +00:00
|
|
|
else
|
2014-08-13 01:43:56 +00:00
|
|
|
return default_pos;
|
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-10-21 20:30:40 +00:00
|
|
|
void parser::check_token_or_id_next(name const & tk, char const * msg) {
|
|
|
|
if (!curr_is_token_or_id(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-09-09 18:02:01 +00:00
|
|
|
void parser::push_local_scope(bool save_options) {
|
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();
|
2014-10-03 15:52:35 +00:00
|
|
|
optional<options> opts;
|
2014-09-09 18:02:01 +00:00
|
|
|
if (save_options)
|
2014-10-03 15:52:35 +00:00
|
|
|
opts = m_ios.get_options();
|
2014-10-04 14:13:19 +00:00
|
|
|
m_parser_scope_stack = cons(parser_scope_stack_elem(opts, m_level_variables, m_variables, m_include_vars),
|
2014-10-03 15:52:35 +00:00
|
|
|
m_parser_scope_stack);
|
2014-06-13 18:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void parser::pop_local_scope() {
|
2014-10-14 04:08:36 +00:00
|
|
|
if (!m_local_level_decls.has_scopes()) {
|
|
|
|
throw parser_error("invalid 'end', there is no open namespace/section/context", pos());
|
|
|
|
}
|
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();
|
2014-10-03 15:52:35 +00:00
|
|
|
lean_assert(!is_nil(m_parser_scope_stack));
|
|
|
|
auto s = head(m_parser_scope_stack);
|
|
|
|
if (s.m_options) {
|
|
|
|
m_ios.set_options(*s.m_options);
|
2014-09-09 18:02:01 +00:00
|
|
|
updt_options();
|
|
|
|
}
|
2014-10-04 14:13:19 +00:00
|
|
|
m_level_variables = s.m_level_variables;
|
2014-10-03 15:52:35 +00:00
|
|
|
m_variables = s.m_variables;
|
|
|
|
m_include_vars = s.m_include_vars;
|
|
|
|
m_parser_scope_stack = tail(m_parser_scope_stack);
|
2014-06-13 18:24:26 +00:00
|
|
|
}
|
|
|
|
|
2014-10-04 14:13:19 +00:00
|
|
|
void parser::add_local_level(name const & n, level const & l, bool is_variable) {
|
2014-06-13 18:24:26 +00:00
|
|
|
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-10-04 14:13:19 +00:00
|
|
|
if (is_variable) {
|
|
|
|
lean_assert(is_param(l));
|
|
|
|
m_level_variables.insert(n);
|
|
|
|
}
|
2014-06-13 18:24:26 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 00:29:22 +00:00
|
|
|
void parser::add_local_expr(name const & n, expr const & p, bool is_variable) {
|
2014-06-20 18:18:53 +00:00
|
|
|
m_local_decls.insert(n, p);
|
2014-10-03 00:29:22 +00:00
|
|
|
if (is_variable) {
|
|
|
|
lean_assert(is_local(p));
|
2014-10-03 00:42:33 +00:00
|
|
|
m_variables.insert(local_pp_name(p));
|
2014-10-03 00:29:22 +00:00
|
|
|
}
|
2014-06-20 18:18:53 +00:00
|
|
|
}
|
|
|
|
|
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-10-03 14:23:24 +00:00
|
|
|
void parser::get_include_variables(buffer<expr> & vars) const {
|
|
|
|
m_include_vars.for_each([&](name const & n) {
|
|
|
|
vars.push_back(*get_local(n));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-08 15:37:54 +00:00
|
|
|
list<expr> parser::locals_to_context() const {
|
2014-10-09 05:21:29 +00:00
|
|
|
return map_filter<expr>(m_local_decls.get_entries(),
|
|
|
|
[](pair<name, expr> const & p, expr & out) {
|
|
|
|
out = p.second;
|
|
|
|
return is_local(p.second);
|
|
|
|
});
|
2014-10-08 15:37:54 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2014-09-23 00:30:29 +00:00
|
|
|
if (curr_is_token(get_cup_tk()))
|
2014-06-12 16:08:38 +00:00
|
|
|
return g_level_cup_prec;
|
2014-09-23 00:30:29 +00:00
|
|
|
else if (curr_is_token(get_add_tk()))
|
2014-06-12 16:08:38 +00:00
|
|
|
return g_level_add_prec;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
level parser::parse_max_imax(bool is_max) {
|
|
|
|
auto p = pos();
|
|
|
|
next();
|
|
|
|
buffer<level> lvls;
|
2014-09-23 00:30:29 +00:00
|
|
|
while (curr_is_identifier() || curr_is_numeral() || curr_is_token(get_lparen_tk())) {
|
2014-06-12 16:08:38 +00:00
|
|
|
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-07-28 04:01:59 +00:00
|
|
|
if (auto it = get_level_alias(m_env, id))
|
|
|
|
return mk_global_univ(*it);
|
2014-06-13 18:24:26 +00:00
|
|
|
throw parser_error(sstream() << "unknown universe '" << id << "'", p);
|
2014-06-12 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
level parser::parse_level_nud() {
|
2014-09-23 00:30:29 +00:00
|
|
|
if (curr_is_token_or_id(get_max_tk())) {
|
2014-06-12 16:08:38 +00:00
|
|
|
return parse_max_imax(true);
|
2014-09-23 00:30:29 +00:00
|
|
|
} else if (curr_is_token_or_id(get_imax_tk())) {
|
2014-06-12 16:08:38 +00:00
|
|
|
return parse_max_imax(false);
|
2014-10-01 17:19:35 +00:00
|
|
|
} else if (curr_is_token_or_id(get_placeholder_tk())) {
|
|
|
|
next();
|
|
|
|
return mk_level_placeholder();
|
2014-09-23 00:30:29 +00:00
|
|
|
} else if (curr_is_token(get_lparen_tk())) {
|
2014-06-12 16:08:38 +00:00
|
|
|
next();
|
|
|
|
level l = parse_level();
|
2014-09-23 00:30:29 +00:00
|
|
|
check_token_next(get_rparen_tk(), "invalid level expression, ')' expected");
|
2014-06-12 16:08:38 +00:00
|
|
|
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();
|
2014-09-23 00:30:29 +00:00
|
|
|
if (curr_is_token(get_cup_tk())) {
|
2014-06-12 16:08:38 +00:00
|
|
|
next();
|
|
|
|
level right = parse_level(g_level_cup_prec);
|
|
|
|
return mk_max(left, right);
|
2014-09-23 00:30:29 +00:00
|
|
|
} else if (curr_is_token(get_add_tk())) {
|
2014-06-12 16:08:38 +00:00
|
|
|
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-08-13 19:45:57 +00:00
|
|
|
elaborator_context parser::mk_elaborator_context(pos_info_provider const & pp, bool check_unassigned) {
|
|
|
|
return elaborator_context(m_env, m_ios, m_local_level_decls, &pp, m_info_manager, check_unassigned);
|
2014-08-13 01:43:56 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 19:45:57 +00:00
|
|
|
elaborator_context parser::mk_elaborator_context(environment const & env, pos_info_provider const & pp) {
|
|
|
|
return elaborator_context(env, m_ios, m_local_level_decls, &pp, m_info_manager, true);
|
2014-08-13 01:43:56 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 15:26:39 +00:00
|
|
|
elaborator_context parser::mk_elaborator_context(environment const & env, local_level_decls const & lls,
|
|
|
|
pos_info_provider const & pp) {
|
2014-08-13 19:45:57 +00:00
|
|
|
return elaborator_context(env, m_ios, lls, &pp, m_info_manager, true);
|
2014-08-13 01:43:56 +00:00
|
|
|
}
|
|
|
|
|
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-27 19:01:06 +00:00
|
|
|
bool relax = true;
|
|
|
|
bool check_unassigned = false;
|
2014-08-07 02:35:26 +00:00
|
|
|
bool ensure_type = false;
|
2014-08-13 01:43:56 +00:00
|
|
|
parser_pos_provider pp = get_pos_provider();
|
2014-08-13 19:45:57 +00:00
|
|
|
elaborator_context env = mk_elaborator_context(pp, check_unassigned);
|
2014-08-13 01:43:56 +00:00
|
|
|
auto r = ::lean::elaborate(env, ctx, e, relax, ensure_type);
|
2014-08-17 19:14:42 +00:00
|
|
|
m_pre_info_manager.clear();
|
2014-08-07 04:56:57 +00:00
|
|
|
return r;
|
2014-06-13 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 08:12:08 +00:00
|
|
|
std::tuple<expr, level_param_names> parser::elaborate_type(expr const & e, list<expr> const & ctx, bool clear_pre_info) {
|
2014-07-27 19:01:06 +00:00
|
|
|
bool relax = false;
|
|
|
|
bool check_unassigned = true;
|
2014-08-07 02:35:26 +00:00
|
|
|
bool ensure_type = true;
|
2014-08-13 01:43:56 +00:00
|
|
|
parser_pos_provider pp = get_pos_provider();
|
2014-08-13 19:45:57 +00:00
|
|
|
elaborator_context env = mk_elaborator_context(pp, check_unassigned);
|
2014-08-13 01:43:56 +00:00
|
|
|
auto r = ::lean::elaborate(env, ctx, e, relax, ensure_type);
|
2014-09-05 08:12:08 +00:00
|
|
|
if (clear_pre_info)
|
|
|
|
m_pre_info_manager.clear();
|
2014-08-07 04:56:57 +00:00
|
|
|
return r;
|
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-27 19:01:06 +00:00
|
|
|
bool relax = false;
|
2014-08-13 01:43:56 +00:00
|
|
|
parser_pos_provider pp = get_pos_provider();
|
2014-08-13 19:45:57 +00:00
|
|
|
elaborator_context eenv = mk_elaborator_context(env, pp);
|
2014-08-13 01:43:56 +00:00
|
|
|
auto r = ::lean::elaborate(eenv, list<expr>(), e, relax);
|
2014-08-17 19:14:42 +00:00
|
|
|
m_pre_info_manager.clear();
|
2014-08-07 04:56:57 +00:00
|
|
|
return r;
|
2014-06-28 22:33:56 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 15:26:39 +00:00
|
|
|
auto parser::elaborate_definition(name const & n, expr const & t, expr const & v,
|
|
|
|
bool is_opaque)
|
|
|
|
-> std::tuple<expr, expr, level_param_names> {
|
2014-07-08 21:28:33 +00:00
|
|
|
parser_pos_provider pp = get_pos_provider();
|
2014-08-13 19:45:57 +00:00
|
|
|
elaborator_context eenv = mk_elaborator_context(pp);
|
2014-08-13 01:43:56 +00:00
|
|
|
auto r = ::lean::elaborate(eenv, n, t, v, is_opaque);
|
2014-08-17 19:14:42 +00:00
|
|
|
m_pre_info_manager.clear();
|
2014-08-07 04:56:57 +00:00
|
|
|
return r;
|
2014-06-14 00:30:35 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 15:26:39 +00:00
|
|
|
auto parser::elaborate_definition_at(environment const & env, local_level_decls const & lls,
|
|
|
|
name const & n, expr const & t, expr const & v, bool is_opaque)
|
|
|
|
-> std::tuple<expr, expr, level_param_names> {
|
2014-07-12 21:50:57 +00:00
|
|
|
parser_pos_provider pp = get_pos_provider();
|
2014-08-13 19:45:57 +00:00
|
|
|
elaborator_context eenv = mk_elaborator_context(env, lls, pp);
|
2014-08-13 01:43:56 +00:00
|
|
|
auto r = ::lean::elaborate(eenv, n, t, v, is_opaque);
|
2014-08-17 19:14:42 +00:00
|
|
|
m_pre_info_manager.clear();
|
2014-08-07 04:56:57 +00:00
|
|
|
return r;
|
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() {
|
2014-09-23 00:30:29 +00:00
|
|
|
if (curr_is_token(get_lparen_tk())) {
|
2014-06-30 23:52:20 +00:00
|
|
|
next();
|
|
|
|
return some(binder_info());
|
2014-09-23 00:30:29 +00:00
|
|
|
} else if (curr_is_token(get_lcurly_tk())) {
|
2014-06-30 23:52:20 +00:00
|
|
|
next();
|
2014-09-23 00:30:29 +00:00
|
|
|
if (curr_is_token(get_lcurly_tk())) {
|
2014-06-30 23:52:20 +00:00
|
|
|
next();
|
|
|
|
return some(mk_strict_implicit_binder_info());
|
|
|
|
} else {
|
|
|
|
return some(mk_implicit_binder_info());
|
|
|
|
}
|
2014-09-23 00:30:29 +00:00
|
|
|
} else if (curr_is_token(get_lbracket_tk())) {
|
2014-06-30 23:52:20 +00:00
|
|
|
next();
|
2014-10-12 20:06:00 +00:00
|
|
|
return some(mk_inst_implicit_binder_info());
|
2014-09-23 00:30:29 +00:00
|
|
|
} else if (curr_is_token(get_ldcurly_tk())) {
|
2014-06-30 23:52:20 +00:00
|
|
|
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 '⦄'
|
2014-10-12 20:06:00 +00:00
|
|
|
- inst implicit : consume ']'
|
2014-06-30 23:52:20 +00:00
|
|
|
*/
|
|
|
|
void parser::parse_close_binder_info(optional<binder_info> const & bi) {
|
|
|
|
if (!bi) {
|
|
|
|
return;
|
|
|
|
} else if (bi->is_implicit()) {
|
2014-09-23 00:30:29 +00:00
|
|
|
check_token_next(get_rcurly_tk(), "invalid declaration, '}' expected");
|
2014-10-12 20:06:00 +00:00
|
|
|
} else if (bi->is_inst_implicit()) {
|
2014-09-23 00:30:29 +00:00
|
|
|
check_token_next(get_rbracket_tk(), "invalid declaration, ']' expected");
|
2014-06-30 23:52:20 +00:00
|
|
|
} else if (bi->is_strict_implicit()) {
|
2014-09-23 00:30:29 +00:00
|
|
|
if (curr_is_token(get_rcurly_tk())) {
|
2014-06-30 23:52:20 +00:00
|
|
|
next();
|
2014-09-23 00:30:29 +00:00
|
|
|
check_token_next(get_rcurly_tk(), "invalid declaration, '}' expected");
|
2014-06-30 23:52:20 +00:00
|
|
|
} else {
|
2014-09-23 00:30:29 +00:00
|
|
|
check_token_next(get_rdcurly_tk(), "invalid declaration, '⦄' expected");
|
2014-06-30 23:52:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-09-23 00:30:29 +00:00
|
|
|
check_token_next(get_rparen_tk(), "invalid declaration, ')' expected");
|
2014-06-30 23:52:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2014-09-23 00:30:29 +00:00
|
|
|
if (curr_is_token(get_colon_tk())) {
|
2014-06-12 01:51:12 +00:00
|
|
|
next();
|
|
|
|
type = parse_expr();
|
|
|
|
} else {
|
|
|
|
type = save_pos(mk_expr_placeholder(), p);
|
|
|
|
}
|
2014-10-09 05:54:10 +00:00
|
|
|
save_identifier_info(p, id);
|
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-08-19 23:28:58 +00:00
|
|
|
buffer<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;
|
2014-09-23 00:30:29 +00:00
|
|
|
if (curr_is_token(get_colon_tk())) {
|
2014-06-12 01:51:12 +00:00
|
|
|
next();
|
|
|
|
type = parse_expr();
|
|
|
|
}
|
|
|
|
for (auto p : names) {
|
|
|
|
expr arg_type = type ? *type : save_pos(mk_expr_placeholder(), p.first);
|
2014-10-09 05:54:10 +00:00
|
|
|
save_identifier_info(p.first, p.second);
|
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-10-11 01:08:03 +00:00
|
|
|
void parser::parse_binders_core(buffer<expr> & r, buffer<notation_entry> * nentries,
|
|
|
|
bool & last_block_delimited) {
|
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-10-11 01:08:03 +00:00
|
|
|
last_block_delimited = false;
|
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) {
|
2014-10-11 01:08:03 +00:00
|
|
|
last_block_delimited = true;
|
2014-07-29 02:04:57 +00:00
|
|
|
if (!parse_local_notation_decl(nentries))
|
2014-06-30 23:52:20 +00:00
|
|
|
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-10-11 01:08:03 +00:00
|
|
|
local_environment parser::parse_binders(buffer<expr> & r, buffer<notation_entry> * nentries,
|
|
|
|
bool & last_block_delimited) {
|
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();
|
2014-10-11 01:08:03 +00:00
|
|
|
parse_binders_core(r, nentries, last_block_delimited);
|
2014-06-12 01:51:12 +00:00
|
|
|
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-07-29 02:04:57 +00:00
|
|
|
bool parser::parse_local_notation_decl(buffer<notation_entry> * nentries) {
|
2014-06-17 17:12:04 +00:00
|
|
|
if (curr_is_notation_decl(*this)) {
|
|
|
|
buffer<token_entry> new_tokens;
|
2014-10-11 23:38:55 +00:00
|
|
|
bool overload = false;
|
|
|
|
bool allow_local = true;
|
|
|
|
auto ne = ::lean::parse_notation(*this, overload, new_tokens, allow_local);
|
2014-06-17 17:12:04 +00:00
|
|
|
for (auto const & te : new_tokens)
|
|
|
|
m_env = add_token(m_env, te);
|
2014-07-29 02:04:57 +00:00
|
|
|
if (nentries) nentries->push_back(ne);
|
2014-06-17 17:12:04 +00:00
|
|
|
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-08-17 22:06:46 +00:00
|
|
|
if (m_info_manager)
|
|
|
|
m_info_manager->add_symbol_info(p.first, p.second, get_token_info().token());
|
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;
|
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:
|
2014-10-28 00:12:25 +00:00
|
|
|
next();
|
2014-06-12 03:56:10 +00:00
|
|
|
break;
|
|
|
|
case notation::action_kind::Expr:
|
2014-10-28 00:12:25 +00:00
|
|
|
next();
|
2014-06-12 03:56:10 +00:00
|
|
|
args.push_back(parse_expr(a.rbp()));
|
|
|
|
break;
|
|
|
|
case notation::action_kind::Exprs: {
|
2014-10-28 00:12:25 +00:00
|
|
|
next();
|
2014-06-12 03:56:10 +00:00
|
|
|
buffer<expr> r_args;
|
2014-07-30 22:04:44 +00:00
|
|
|
auto terminator = a.get_terminator();
|
|
|
|
if (!terminator || !curr_is_token(*terminator)) {
|
2014-06-12 03:56:10 +00:00
|
|
|
r_args.push_back(parse_expr(a.rbp()));
|
2014-07-30 22:04:44 +00:00
|
|
|
while (curr_is_token(a.get_sep())) {
|
|
|
|
next();
|
|
|
|
r_args.push_back(parse_expr(a.rbp()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (terminator) {
|
|
|
|
if (curr_is_token(*terminator)) {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
throw parser_error(sstream() << "invalid composite expression, '" << *terminator << "' expected", pos());
|
|
|
|
}
|
2014-06-12 03:56:10 +00:00
|
|
|
}
|
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-10-28 00:12:25 +00:00
|
|
|
next();
|
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-10-28 00:12:25 +00:00
|
|
|
next();
|
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-10-28 00:12:25 +00:00
|
|
|
next();
|
2014-07-13 00:24:55 +00:00
|
|
|
expr r = parse_scoped_expr(ps, lenv, a.rbp());
|
2014-10-01 21:02:49 +00:00
|
|
|
bool no_cache = false;
|
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())
|
2014-10-01 21:02:49 +00:00
|
|
|
r = Fun(ps, r, no_cache);
|
2014-07-14 04:41:44 +00:00
|
|
|
else
|
2014-10-01 21:02:49 +00:00
|
|
|
r = Pi(ps, r, no_cache);
|
2014-07-14 04:41:44 +00:00
|
|
|
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-10-01 21:02:49 +00:00
|
|
|
r = Fun(l, r, no_cache);
|
2014-06-12 03:56:10 +00:00
|
|
|
else
|
2014-10-01 21:02:49 +00:00
|
|
|
r = Pi(l, r, no_cache);
|
2014-06-30 16:14:55 +00:00
|
|
|
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-10-28 00:12:25 +00:00
|
|
|
next();
|
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))
|
2014-10-08 15:26:39 +00:00
|
|
|
throw parser_error(sstream() << "failed to use notation implemented in Lua, "
|
|
|
|
<< "Lua state does not contain function '"
|
2014-06-18 03:39:42 +00:00
|
|
|
<< 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();
|
2014-09-10 00:44:19 +00:00
|
|
|
save_overload_notation(as, p);
|
2014-06-12 03:56:10 +00:00
|
|
|
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-08-07 02:13:09 +00:00
|
|
|
expr r = save_pos(mk_choice(cs.size(), cs.data()), p);
|
2014-08-07 04:56:57 +00:00
|
|
|
save_type_info(r);
|
2014-08-07 02:13:09 +00:00
|
|
|
return r;
|
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;
|
2014-09-23 00:30:29 +00:00
|
|
|
if (curr_is_token(get_llevel_curly_tk())) {
|
2014-06-13 18:24:26 +00:00
|
|
|
next();
|
2014-09-23 00:30:29 +00:00
|
|
|
while (!curr_is_token(get_rcurly_tk())) {
|
2014-06-13 18:24:26 +00:00
|
|
|
lvl_buffer.push_back(parse_level());
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
ls = to_list(lvl_buffer.begin(), lvl_buffer.end());
|
|
|
|
}
|
2014-08-05 05:08:10 +00:00
|
|
|
|
2014-07-08 01:56:51 +00:00
|
|
|
if (id.is_atomic()) {
|
|
|
|
// locals
|
2014-08-07 04:56:57 +00:00
|
|
|
if (auto it1 = m_local_decls.find(id)) {
|
|
|
|
auto r = copy_with_new_pos(propagate_levels(*it1, ls), p);
|
|
|
|
save_type_info(r);
|
2014-08-17 22:06:46 +00:00
|
|
|
save_identifier_info(p, id);
|
2014-08-07 04:56:57 +00:00
|
|
|
return r;
|
|
|
|
}
|
2014-08-19 01:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (name const & ns : get_namespaces(m_env)) {
|
|
|
|
auto new_id = ns + id;
|
|
|
|
if (!ns.is_anonymous() && m_env.find(new_id)) {
|
|
|
|
auto r = save_pos(mk_constant(new_id, ls), p);
|
|
|
|
save_type_info(r);
|
|
|
|
add_ref_index(new_id, p);
|
|
|
|
save_identifier_info(p, new_id);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!id.is_atomic()) {
|
|
|
|
name new_id = id;
|
|
|
|
new_id = remove_root_prefix(new_id);
|
2014-07-08 02:15:46 +00:00
|
|
|
if (m_env.find(new_id)) {
|
2014-08-07 04:56:57 +00:00
|
|
|
auto r = save_pos(mk_constant(new_id, ls), p);
|
|
|
|
save_type_info(r);
|
2014-08-15 00:44:07 +00:00
|
|
|
add_ref_index(new_id, p);
|
2014-08-17 22:06:46 +00:00
|
|
|
save_identifier_info(p, new_id);
|
2014-08-07 04:56:57 +00:00
|
|
|
return r;
|
2014-06-13 18:24:26 +00:00
|
|
|
}
|
2014-07-08 01:56:51 +00:00
|
|
|
}
|
2014-08-05 05:08:10 +00:00
|
|
|
|
|
|
|
optional<expr> r;
|
|
|
|
// globals
|
|
|
|
if (m_env.find(id))
|
|
|
|
r = save_pos(mk_constant(id, ls), p);
|
|
|
|
// aliases
|
|
|
|
auto as = get_expr_aliases(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(mk_constant(e, ls), p));
|
|
|
|
}
|
|
|
|
r = save_pos(mk_choice(new_as.size(), new_as.data()), p);
|
2014-08-07 02:13:09 +00:00
|
|
|
save_overload(*r);
|
2014-08-05 05:08:10 +00:00
|
|
|
}
|
2014-10-26 16:47:11 +00:00
|
|
|
if (!r) {
|
|
|
|
if (m_undef_id_behavior == undef_id_behavior::AssumeConstant)
|
|
|
|
r = save_pos(mk_constant(get_namespace(m_env) + id, ls), p);
|
|
|
|
else if (m_undef_id_behavior == undef_id_behavior::AssumeLocal)
|
|
|
|
r = save_pos(mk_local(id, mk_expr_placeholder()), p);
|
|
|
|
}
|
2014-08-05 05:08:10 +00:00
|
|
|
if (!r)
|
|
|
|
throw parser_error(sstream() << "unknown identifier '" << id << "'", p);
|
2014-08-07 04:56:57 +00:00
|
|
|
save_type_info(*r);
|
2014-08-21 23:58:04 +00:00
|
|
|
if (is_constant(*r)) {
|
|
|
|
add_ref_index(const_name(*r), p);
|
|
|
|
save_identifier_info(p, const_name(*r));
|
2014-10-26 17:19:44 +00:00
|
|
|
} else if (is_local(*r)) {
|
|
|
|
save_identifier_info(p, local_pp_name(*r));
|
2014-08-21 23:58:04 +00:00
|
|
|
}
|
2014-08-05 05:08:10 +00:00
|
|
|
return *r;
|
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);
|
2014-07-29 20:04:58 +00:00
|
|
|
|
2014-10-12 00:13:33 +00:00
|
|
|
if (in_context(m_env) && is_as_atomic(e)) {
|
2014-10-10 22:41:55 +00:00
|
|
|
e = get_app_fn(get_as_atomic_arg(e));
|
2014-07-29 20:04:58 +00:00
|
|
|
if (is_explicit(e))
|
|
|
|
e = get_explicit_arg(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_constant(e)) {
|
|
|
|
return const_name(e);
|
|
|
|
} else {
|
2014-07-15 00:19:47 +00:00
|
|
|
throw parser_error(msg, p);
|
2014-07-29 20:04:58 +00:00
|
|
|
}
|
2014-07-15 00:19:47 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2014-09-26 21:54:39 +00:00
|
|
|
list<expr> vals = get_mpz_notation(m_env, n);
|
|
|
|
if (!*m_has_num && !vals) {
|
2014-07-02 15:08:35 +00:00
|
|
|
throw parser_error("numeral cannot be encoded as expression, environment does not contain the type 'num' "
|
2014-09-26 21:54:39 +00:00
|
|
|
"nor notation was defined for the given numeral "
|
|
|
|
"(solution: use 'import data.num', or define notation for the given numeral)", p);
|
|
|
|
}
|
|
|
|
buffer<expr> cs;
|
|
|
|
for (expr const & c : vals)
|
|
|
|
cs.push_back(copy_with_new_pos(c, p));
|
|
|
|
if (*m_has_num)
|
|
|
|
cs.push_back(save_pos(from_num(n), p));
|
|
|
|
lean_assert(!cs.empty());
|
|
|
|
if (cs.size() == 1)
|
|
|
|
return cs[0];
|
|
|
|
else
|
|
|
|
return save_pos(mk_choice(cs.size(), cs.data()), p);
|
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:
|
2014-07-30 22:04:44 +00:00
|
|
|
case scanner::token_kind::ScriptBlock: case scanner::token_kind::QuotedSymbol:
|
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:
|
|
|
|
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();
|
2014-08-23 19:39:59 +00:00
|
|
|
m_cmd_token = get_token_info().token();
|
2014-06-11 18:26:17 +00:00
|
|
|
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-08-03 02:47:55 +00:00
|
|
|
static optional<std::string> try_file(name const & f, char const * ext) {
|
2014-06-16 23:37:46 +00:00
|
|
|
try {
|
|
|
|
return optional<std::string>(find_file(f, {ext}));
|
|
|
|
} catch (...) {
|
|
|
|
return optional<std::string>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 02:47:55 +00:00
|
|
|
static optional<std::string> try_file(std::string const & base, optional<unsigned> const & k, name const & f, char const * ext) {
|
|
|
|
try {
|
|
|
|
return optional<std::string>(find_file(base, k, f, ext));
|
|
|
|
} catch (...) {
|
|
|
|
return optional<std::string>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 00:30:29 +00:00
|
|
|
static std::string * g_lua_module_key = nullptr;
|
2014-06-28 06:23:51 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-16 23:37:46 +00:00
|
|
|
void parser::parse_imports() {
|
2014-08-03 02:47:55 +00:00
|
|
|
buffer<module_name> olean_files;
|
|
|
|
buffer<name> lua_files;
|
|
|
|
std::string base = dirname(get_stream_name().c_str());
|
2014-09-15 17:09:42 +00:00
|
|
|
bool imported = false;
|
2014-09-23 00:30:29 +00:00
|
|
|
while (curr_is_token(get_import_tk())) {
|
2014-09-15 17:09:42 +00:00
|
|
|
imported = true;
|
2014-06-16 23:37:46 +00:00
|
|
|
m_last_cmd_pos = pos();
|
|
|
|
next();
|
2014-08-03 02:47:55 +00:00
|
|
|
while (true) {
|
|
|
|
optional<unsigned> k;
|
2014-09-23 00:30:29 +00:00
|
|
|
while (curr_is_token(get_period_tk())) {
|
2014-08-03 02:47:55 +00:00
|
|
|
next();
|
|
|
|
if (!k)
|
|
|
|
k = 0;
|
|
|
|
else
|
|
|
|
k = *k + 1;
|
|
|
|
}
|
|
|
|
if (!curr_is_identifier())
|
|
|
|
break;
|
2014-06-16 23:37:46 +00:00
|
|
|
name f = get_name_val();
|
2014-08-03 02:47:55 +00:00
|
|
|
if (auto it = try_file(f, ".lua")) {
|
|
|
|
if (k)
|
|
|
|
throw parser_error(sstream() << "invalid import, failed to import '" << f
|
|
|
|
<< "', relative paths are not supported for .lua files", pos());
|
2014-06-29 17:48:57 +00:00
|
|
|
lua_files.push_back(f);
|
2014-08-03 02:47:55 +00:00
|
|
|
} else if (auto it = try_file(base, k, f, ".olean")) {
|
|
|
|
olean_files.push_back(module_name(k, f));
|
2014-06-16 23:37:46 +00:00
|
|
|
} else {
|
2014-08-01 02:03:54 +00:00
|
|
|
m_found_errors = true;
|
|
|
|
if (!m_use_exceptions && m_show_errors) {
|
|
|
|
flycheck_error err(regular_stream());
|
|
|
|
display_error_pos(pos());
|
|
|
|
regular_stream() << " invalid import, unknown module '" << f << "'" << endl;
|
|
|
|
}
|
|
|
|
if (m_use_exceptions)
|
|
|
|
throw parser_error(sstream() << "invalid import, unknown module '" << f << "'", pos());
|
2014-06-16 23:37:46 +00:00
|
|
|
}
|
|
|
|
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;
|
2014-10-13 17:05:38 +00:00
|
|
|
bool keep_imported_thms = (m_keep_theorem_mode == keep_theorem_mode::All);
|
2014-10-09 20:07:21 +00:00
|
|
|
m_env = import_modules(m_env, base, olean_files.size(), olean_files.data(), num_threads,
|
2014-10-13 17:05:38 +00:00
|
|
|
keep_imported_thms, 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-09-23 00:30:29 +00:00
|
|
|
m_env = module::add(m_env, *g_lua_module_key, [=](serializer & s) {
|
2014-06-28 06:23:51 +00:00
|
|
|
s << f;
|
|
|
|
});
|
|
|
|
}
|
2014-09-15 17:09:42 +00:00
|
|
|
if (imported)
|
|
|
|
commit_info(1);
|
2014-06-16 23:37:46 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 01:06:37 +00:00
|
|
|
void parser::commit_info(unsigned line) {
|
|
|
|
save_snapshot();
|
|
|
|
if (m_info_manager) {
|
|
|
|
m_info_manager->save_environment_options(line, m_env, m_ios.get_options());
|
|
|
|
m_info_manager->commit_upto(line, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-27 16:41:25 +00:00
|
|
|
scoped_set_distinguishing_pp_options set(get_distinguishing_pp_options());
|
2014-06-11 18:26:17 +00:00
|
|
|
try {
|
|
|
|
bool done = false;
|
2014-08-14 18:02:07 +00:00
|
|
|
protected_call([&]() {
|
|
|
|
parse_imports();
|
|
|
|
},
|
|
|
|
[&]() { sync_command(); });
|
2014-08-02 06:21:55 +00:00
|
|
|
if (has_sorry(m_env)) {
|
2014-09-26 15:55:54 +00:00
|
|
|
#ifndef LEAN_IGNORE_SORRY
|
|
|
|
// TODO(Leo): remove the #ifdef.
|
|
|
|
// The compilation option LEAN_IGNORE_SORRY is a temporary hack for the nightly builds
|
|
|
|
// We use it to avoid a buch of warnings on cdash.
|
2014-08-02 06:21:55 +00:00
|
|
|
flycheck_warning wrn(regular_stream());
|
|
|
|
display_warning_pos(pos());
|
|
|
|
regular_stream() << " imported file uses 'sorry'" << endl;
|
2014-09-26 15:55:54 +00:00
|
|
|
#endif
|
2014-08-02 06:21:55 +00:00
|
|
|
}
|
2014-06-11 18:26:17 +00:00
|
|
|
while (!done) {
|
|
|
|
protected_call([&]() {
|
|
|
|
check_interrupted();
|
|
|
|
switch (curr()) {
|
|
|
|
case scanner::token_kind::CommandKeyword:
|
|
|
|
parse_command();
|
2014-08-27 01:06:37 +00:00
|
|
|
commit_info(m_scanner.get_line());
|
2014-06-11 18:26:17 +00:00
|
|
|
break;
|
|
|
|
case scanner::token_kind::ScriptBlock:
|
|
|
|
parse_script();
|
2014-08-07 06:15:05 +00:00
|
|
|
save_snapshot();
|
2014-06-11 18:26:17 +00:00
|
|
|
break;
|
|
|
|
case scanner::token_kind::Eof:
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
case scanner::token_kind::Keyword:
|
2014-09-23 00:30:29 +00:00
|
|
|
if (curr_is_token(get_period_tk())) {
|
2014-06-11 18:26:17 +00:00
|
|
|
next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw parser_error("command expected", pos());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[&]() { sync_command(); });
|
|
|
|
}
|
2014-08-08 00:08:59 +00:00
|
|
|
if (has_open_scopes(m_env)) {
|
|
|
|
m_found_errors = true;
|
|
|
|
if (!m_use_exceptions && m_show_errors)
|
|
|
|
display_error("invalid end of module, expecting 'end'", pos());
|
|
|
|
else if (m_use_exceptions)
|
|
|
|
throw_parser_exception("invalid end of module, expecting 'end'", pos());
|
|
|
|
}
|
2014-06-11 18:26:17 +00:00
|
|
|
} catch (interrupt_parser) {}
|
2014-08-27 01:06:37 +00:00
|
|
|
commit_info(m_scanner.get_line()+1);
|
2014-07-12 21:50:57 +00:00
|
|
|
for (certified_declaration const & thm : m_theorem_queue.join()) {
|
2014-10-13 17:05:38 +00:00
|
|
|
if (keep_new_thms())
|
|
|
|
m_env.replace(thm);
|
2014-07-12 21:50:57 +00:00
|
|
|
}
|
2014-06-11 18:26:17 +00:00
|
|
|
return !m_found_errors;
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:03:54 +00:00
|
|
|
bool parser::curr_is_command_like() const {
|
|
|
|
switch (curr()) {
|
|
|
|
case scanner::token_kind::CommandKeyword:
|
|
|
|
return true;
|
|
|
|
case scanner::token_kind::ScriptBlock:
|
|
|
|
return true;
|
|
|
|
case scanner::token_kind::Eof:
|
|
|
|
return true;
|
|
|
|
case scanner::token_kind::Keyword:
|
2014-09-23 00:30:29 +00:00
|
|
|
return curr_is_token(get_period_tk());
|
2014-08-18 00:03:54 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 15:26:39 +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-08-07 01:31:53 +00:00
|
|
|
void parser::save_snapshot() {
|
2014-08-17 19:14:42 +00:00
|
|
|
m_pre_info_manager.clear();
|
2014-08-07 01:31:53 +00:00
|
|
|
if (!m_snapshot_vector)
|
|
|
|
return;
|
|
|
|
if (m_snapshot_vector->empty() || static_cast<int>(m_snapshot_vector->back().m_line) != m_scanner.get_line())
|
2014-10-04 14:13:19 +00:00
|
|
|
m_snapshot_vector->push_back(snapshot(m_env, m_local_level_decls, m_local_decls,
|
|
|
|
m_level_variables, m_variables, m_include_vars,
|
2014-10-03 15:52:35 +00:00
|
|
|
m_ios.get_options(), m_parser_scope_stack, m_scanner.get_line()));
|
2014-08-07 06:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void parser::save_pre_info_data() {
|
2014-08-07 04:56:57 +00:00
|
|
|
// if elaborator failed, then m_pre_info_data contains type information before elaboration.
|
|
|
|
if (m_info_manager) {
|
2014-08-27 01:06:37 +00:00
|
|
|
bool overwrite = false;
|
|
|
|
m_info_manager->merge(m_pre_info_manager, overwrite);
|
2014-08-17 19:14:42 +00:00
|
|
|
m_pre_info_manager.clear();
|
2014-08-07 04:56:57 +00:00
|
|
|
}
|
2014-08-07 01:31:53 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 02:13:09 +00:00
|
|
|
void parser::save_overload(expr const & e) {
|
2014-08-07 04:23:37 +00:00
|
|
|
if (!m_info_manager || !is_choice(e))
|
2014-08-07 02:13:09 +00:00
|
|
|
return;
|
|
|
|
auto p = pos_of(e);
|
2014-08-17 19:14:42 +00:00
|
|
|
m_info_manager->add_overload_info(p.first, p.second, e);
|
2014-08-07 02:13:09 +00:00
|
|
|
}
|
|
|
|
|
2014-09-10 00:44:19 +00:00
|
|
|
void parser::save_overload_notation(list<expr> const & as, pos_info const & p) {
|
|
|
|
if (!m_info_manager || length(as) <= 1)
|
|
|
|
return;
|
|
|
|
m_info_manager->add_overload_notation_info(p.first, p.second, as);
|
|
|
|
}
|
|
|
|
|
2014-08-17 22:06:46 +00:00
|
|
|
void parser::save_identifier_info(pos_info const & p, name const & full_id) {
|
|
|
|
if (!m_info_manager)
|
|
|
|
return;
|
|
|
|
m_info_manager->add_identifier_info(p.first, p.second, full_id);
|
|
|
|
}
|
|
|
|
|
2014-08-07 04:56:57 +00:00
|
|
|
void parser::save_type_info(expr const & e) {
|
|
|
|
if (!m_info_manager)
|
|
|
|
return;
|
|
|
|
if (is_explicit(e)) {
|
|
|
|
save_type_info(get_explicit_arg(e));
|
2014-10-10 22:41:55 +00:00
|
|
|
} else if (is_as_atomic(e)) {
|
|
|
|
save_type_info(get_as_atomic_arg(e));
|
2014-08-07 04:56:57 +00:00
|
|
|
} else if (is_choice(e)) {
|
|
|
|
for (unsigned i = 0; i < get_num_choices(e); i++)
|
|
|
|
save_type_info(get_choice(e, i));
|
|
|
|
} else if (is_app(e)) {
|
|
|
|
save_type_info(get_app_fn(e));
|
|
|
|
} else if (is_constant(e)) {
|
|
|
|
auto d = m_env.find(const_name(e));
|
|
|
|
if (!d)
|
|
|
|
return;
|
|
|
|
auto p = pos_of(e);
|
2014-08-17 19:14:42 +00:00
|
|
|
m_pre_info_manager.add_type_info(p.first, p.second, d->get_type());
|
2014-08-07 04:56:57 +00:00
|
|
|
} else if (is_local(e)) {
|
|
|
|
auto p = pos_of(e);
|
|
|
|
expr t = mlocal_type(e);
|
|
|
|
if (is_meta(t))
|
|
|
|
return;
|
2014-08-17 19:14:42 +00:00
|
|
|
m_pre_info_manager.add_type_info(p.first, p.second, t);
|
2014-08-07 04:56:57 +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-10-13 17:05:38 +00:00
|
|
|
unsigned num_threads, definition_cache * cache, declaration_index * index,
|
|
|
|
keep_theorem_mode tmode) {
|
|
|
|
parser p(env, ios, in, strm_name, use_exceptions, num_threads, nullptr, nullptr, nullptr, tmode);
|
2014-08-10 18:04:16 +00:00
|
|
|
p.set_cache(cache);
|
2014-08-15 00:44:07 +00:00
|
|
|
p.set_index(index);
|
2014-06-11 18:26:17 +00:00
|
|
|
bool r = p();
|
|
|
|
ios = p.ios();
|
|
|
|
env = p.env();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-08-10 18:04:16 +00:00
|
|
|
bool parse_commands(environment & env, io_state & ios, char const * fname, bool use_exceptions, unsigned num_threads,
|
2014-10-13 17:05:38 +00:00
|
|
|
definition_cache * cache, declaration_index * index, keep_theorem_mode tmode) {
|
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-10-13 17:05:38 +00:00
|
|
|
return parse_commands(env, ios, in, fname, use_exceptions, num_threads, cache, index, tmode);
|
2014-06-11 18:26:17 +00:00
|
|
|
}
|
2014-09-23 00:30:29 +00:00
|
|
|
|
|
|
|
void initialize_parser() {
|
|
|
|
g_parser_show_errors = new name{"parser", "show_errors"};
|
|
|
|
g_parser_parallel_import = new name{"parser", "parallel_import"};
|
|
|
|
register_bool_option(*g_parser_show_errors, LEAN_DEFAULT_PARSER_SHOW_ERRORS,
|
|
|
|
"(lean parser) display error messages in the regular output channel");
|
|
|
|
register_bool_option(*g_parser_parallel_import, LEAN_DEFAULT_PARSER_PARALLEL_IMPORT,
|
|
|
|
"(lean parser) import modules in parallel");
|
|
|
|
g_tmp_prefix = new name(name::mk_internal_unique_name());
|
|
|
|
g_lua_module_key = new std::string("lua_module");
|
|
|
|
register_module_object_reader(*g_lua_module_key, lua_module_reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void finalize_parser() {
|
|
|
|
delete g_lua_module_key;
|
|
|
|
delete g_tmp_prefix;
|
|
|
|
delete g_parser_show_errors;
|
|
|
|
delete g_parser_parallel_import;
|
|
|
|
}
|
2014-06-11 00:02:06 +00:00
|
|
|
}
|