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-06-11 00:02:06 +00:00
|
|
|
#include "library/error_handling/error_handling.h"
|
2014-06-30 01:26:07 +00:00
|
|
|
#include "library/tactic/apply_tactic.h"
|
2014-06-11 00:02:06 +00:00
|
|
|
#include "frontends/lean/parser.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
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
// ==========================================
|
|
|
|
// Parser configuration options
|
|
|
|
static name g_parser_show_errors {"lean", "parser", "show_errors"};
|
|
|
|
|
|
|
|
RegisterBoolOption(g_parser_show_errors, LEAN_DEFAULT_PARSER_SHOW_ERRORS, "(lean parser) display error messages in the regular output channel");
|
|
|
|
|
|
|
|
bool get_parser_show_errors(options const & opts) { return opts.get_bool(g_parser_show_errors, LEAN_DEFAULT_PARSER_SHOW_ERRORS); }
|
|
|
|
// ==========================================
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
parser::param_universe_scope::param_universe_scope(parser & p):m_p(p), m_old(m_p.m_type_use_placeholder) {
|
|
|
|
m_p.m_type_use_placeholder = false;
|
|
|
|
}
|
|
|
|
parser::param_universe_scope::~param_universe_scope() {
|
|
|
|
m_p.m_type_use_placeholder = m_old;
|
|
|
|
}
|
|
|
|
|
2014-06-18 20:55:48 +00:00
|
|
|
parser::placeholder_universe_scope::placeholder_universe_scope(parser & p):m_p(p), m_old(m_p.m_type_use_placeholder) {
|
|
|
|
m_p.m_type_use_placeholder = true;
|
|
|
|
}
|
|
|
|
parser::placeholder_universe_scope::~placeholder_universe_scope() {
|
|
|
|
m_p.m_type_use_placeholder = m_old;
|
|
|
|
}
|
|
|
|
|
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-18 03:39:42 +00:00
|
|
|
static char g_parser_key;
|
|
|
|
void set_global_parser(lua_State * L, parser * p) {
|
|
|
|
lua_pushlightuserdata(L, static_cast<void *>(&g_parser_key));
|
|
|
|
lua_pushlightuserdata(L, static_cast<void *>(p));
|
|
|
|
lua_settable(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
|
|
|
|
|
|
|
parser * get_global_parser_ptr(lua_State * L) {
|
|
|
|
lua_pushlightuserdata(L, static_cast<void *>(&g_parser_key));
|
|
|
|
lua_gettable(L, LUA_REGISTRYINDEX);
|
|
|
|
if (!lua_islightuserdata(L, -1))
|
|
|
|
return nullptr;
|
|
|
|
parser * p = static_cast<parser*>(const_cast<void*>(lua_topointer(L, -1)));
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
parser & get_global_parser(lua_State * L) {
|
|
|
|
parser * p = get_global_parser_ptr(L);
|
|
|
|
if (p == nullptr)
|
|
|
|
throw exception("there is no Lean parser on the Lua stack");
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct scoped_set_parser {
|
|
|
|
lua_State * m_state;
|
|
|
|
parser * m_old;
|
|
|
|
scoped_set_parser(lua_State * L, parser & p):m_state(L) {
|
|
|
|
m_old = get_global_parser_ptr(L);
|
|
|
|
set_global_parser(L, &p);
|
|
|
|
}
|
|
|
|
~scoped_set_parser() {
|
|
|
|
set_global_parser(m_state, 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),
|
|
|
|
m_pos_table(std::make_shared<pos_info_table>()) {
|
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-13 18:24:26 +00:00
|
|
|
m_type_use_placeholder = true;
|
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(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return replace(e, [&](expr const & e, unsigned) {
|
|
|
|
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-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-06-16 16:24:41 +00:00
|
|
|
if (m_type_use_placeholder)
|
|
|
|
m_local_level_decls.push();
|
2014-06-13 18:24:26 +00:00
|
|
|
m_local_decls.push();
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser::pop_local_scope() {
|
2014-06-16 16:24:41 +00:00
|
|
|
if (m_type_use_placeholder)
|
|
|
|
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-12 01:51:12 +00:00
|
|
|
/** \brief Parse a sequence of identifiers <tt>ID*</tt>. Store the result in \c result. */
|
|
|
|
void parser::parse_names(buffer<std::pair<pos_info, name>> & result) {
|
|
|
|
while (curr_is_identifier()) {
|
|
|
|
result.emplace_back(pos(), get_name_val());
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-13 18:24:26 +00:00
|
|
|
expr parser::mk_Type() {
|
|
|
|
if (m_type_use_placeholder) {
|
|
|
|
return mk_sort(mk_level_placeholder());
|
|
|
|
} else {
|
|
|
|
unsigned i = 1;
|
|
|
|
name l("l");
|
|
|
|
name r = l.append_after(i);
|
|
|
|
while (m_local_level_decls.contains(r) || m_env.is_universe(r)) {
|
|
|
|
i++;
|
|
|
|
r = l.append_after(i);
|
|
|
|
}
|
|
|
|
level lvl = mk_param_univ(r);
|
|
|
|
add_local_level(r, lvl);
|
|
|
|
return mk_sort(lvl);
|
|
|
|
}
|
2014-06-12 16:08:38 +00:00
|
|
|
}
|
2014-06-12 01:51:12 +00:00
|
|
|
|
2014-06-25 15:30:09 +00:00
|
|
|
expr parser::elaborate(expr const & e) {
|
2014-06-29 16:47:25 +00:00
|
|
|
parser_pos_provider pp(m_pos_table, get_stream_name(), m_last_cmd_pos);
|
|
|
|
return ::lean::elaborate(m_env, m_ios, e, m_hints, &pp);
|
2014-06-13 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 16:14:55 +00:00
|
|
|
expr parser::elaborate(expr const & e, name_generator const & ngen, list<expr> const & ctx) {
|
2014-06-30 01:26:07 +00:00
|
|
|
parser_pos_provider pp(m_pos_table, get_stream_name(), m_last_cmd_pos);
|
|
|
|
return ::lean::elaborate(m_env, m_ios, e, ngen, m_hints, substitution(), ctx, &pp);
|
|
|
|
}
|
|
|
|
|
2014-06-28 22:33:56 +00:00
|
|
|
expr parser::elaborate(environment const & env, expr const & e) {
|
2014-06-29 16:47:25 +00:00
|
|
|
parser_pos_provider pp(m_pos_table, get_stream_name(), m_last_cmd_pos);
|
|
|
|
return ::lean::elaborate(env, m_ios, e, m_hints, &pp);
|
2014-06-28 22:33:56 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 15:30:09 +00:00
|
|
|
std::pair<expr, expr> parser::elaborate(name const & n, expr const & t, expr const & v) {
|
2014-06-29 16:47:25 +00:00
|
|
|
parser_pos_provider pp(m_pos_table, get_stream_name(), m_last_cmd_pos);
|
|
|
|
return ::lean::elaborate(m_env, m_ios, n, t, v, m_hints, &pp);
|
2014-06-14 00:30:35 +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();
|
|
|
|
if (!curr_is_identifier())
|
|
|
|
throw parser_error("invalid binder, identifier expected", p);
|
|
|
|
name id = get_name_val();
|
|
|
|
next();
|
|
|
|
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;
|
|
|
|
parse_names(names);
|
|
|
|
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-06-15 17:40:18 +00:00
|
|
|
expr r = instantiate_rev(a.get_initial(), args.size(), args.data());
|
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-06-15 17:40:18 +00:00
|
|
|
r = instantiate_rev(a.get_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-06-15 17:40:18 +00:00
|
|
|
r = instantiate_rev(a.get_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-06-15 18:30:52 +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-06-16 20:00:35 +00:00
|
|
|
r = abstract(ps, r, a.use_lambda_abstraction(), binder_pos);
|
2014-06-12 03:56:10 +00:00
|
|
|
} else {
|
|
|
|
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-06-15 17:40:18 +00:00
|
|
|
r = instantiate_rev(a.get_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-06-14 01:00:52 +00:00
|
|
|
// locals
|
2014-06-14 16:56:05 +00:00
|
|
|
if (auto it1 = m_local_decls.find(id))
|
2014-06-30 16:14:55 +00:00
|
|
|
return copy_with_new_pos(propagate_levels(*it1, ls), p);
|
2014-06-13 18:24:26 +00:00
|
|
|
optional<expr> r;
|
2014-06-11 22:07:20 +00:00
|
|
|
// globals
|
|
|
|
if (m_env.find(id))
|
2014-06-13 18:24:26 +00:00
|
|
|
r = save_pos(mk_constant(id, ls), p);
|
2014-06-11 22:07:20 +00:00
|
|
|
// aliases
|
2014-06-12 19:29:04 +00:00
|
|
|
auto as = get_alias_exprs(m_env, id);
|
2014-06-11 22:07:20 +00:00
|
|
|
if (!is_nil(as)) {
|
|
|
|
buffer<expr> new_as;
|
2014-06-13 18:24:26 +00:00
|
|
|
if (r)
|
|
|
|
new_as.push_back(*r);
|
|
|
|
for (auto const & e : as) {
|
2014-06-14 01:00:52 +00:00
|
|
|
new_as.push_back(copy_with_new_pos(propagate_levels(e, ls), p));
|
2014-06-13 18:24:26 +00:00
|
|
|
}
|
2014-06-25 17:32:43 +00:00
|
|
|
r = save_pos(mk_choice(new_as.size(), new_as.data()), p);
|
2014-06-11 22:07:20 +00:00
|
|
|
}
|
2014-06-17 17:30:03 +00:00
|
|
|
if (m_no_undef_id_error)
|
2014-06-25 17:32:43 +00:00
|
|
|
r = save_pos(mk_constant(get_namespace(m_env) + id, ls), p);
|
2014-06-13 18:24:26 +00:00
|
|
|
if (!r)
|
|
|
|
throw parser_error(sstream() << "unknown identifier '" << id << "'", p);
|
|
|
|
return *r;
|
2014-06-11 22:07:20 +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() {
|
|
|
|
// TODO(Leo)
|
|
|
|
return expr();
|
|
|
|
}
|
|
|
|
|
|
|
|
expr parser::parse_decimal_expr() {
|
|
|
|
// TODO(Leo)
|
|
|
|
return expr();
|
|
|
|
}
|
|
|
|
|
|
|
|
expr parser::parse_string_expr() {
|
|
|
|
// TODO(Leo)
|
2014-06-11 00:02:06 +00:00
|
|
|
return expr();
|
|
|
|
}
|
|
|
|
|
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-30 16:14:55 +00:00
|
|
|
expr parser::abstract(unsigned num_ps, expr const * ps, expr const & e, bool lambda, pos_info const & p) {
|
|
|
|
expr r = lambda ? Fun(num_ps, ps, e) : Pi(num_ps, ps, e);
|
|
|
|
return rec_save_pos(r, p);
|
2014-06-12 01:51:12 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 07:51:11 +00:00
|
|
|
/** \brief Throw an exception if \c e contains a local constant that is not marked as contextual in \c local_decls */
|
|
|
|
static void check_only_contextual_locals(expr const & e, local_expr_decls const & local_decls, pos_info const & pos) {
|
2014-06-30 01:26:07 +00:00
|
|
|
for_each(e, [&](expr const & e, unsigned) {
|
|
|
|
if (is_local(e)) {
|
2014-06-30 07:51:11 +00:00
|
|
|
auto it = local_decls.find(mlocal_name(e));
|
2014-06-30 01:26:07 +00:00
|
|
|
lean_assert(it);
|
2014-06-30 16:14:55 +00:00
|
|
|
if (!local_info(*it).is_contextual())
|
2014-06-30 07:51:11 +00:00
|
|
|
throw parser_error(sstream() << "invalid 'apply' tactic, it references non-contextual local '"
|
|
|
|
<< local_pp_name(e) << "'", pos);
|
2014-06-30 01:26:07 +00:00
|
|
|
}
|
|
|
|
return has_local(e);
|
|
|
|
});
|
2014-06-30 07:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** \brief Return a list of all contextual parameters in local_decls */
|
2014-06-30 16:14:55 +00:00
|
|
|
static list<expr> collect_contextual_parameters(local_expr_decls const & local_decls) {
|
|
|
|
buffer<expr> tmp;
|
|
|
|
for (expr const & p : local_decls.get_values()) {
|
|
|
|
if (is_local(p) && local_info(p).is_contextual())
|
2014-06-30 01:26:07 +00:00
|
|
|
tmp.push_back(p);
|
|
|
|
}
|
2014-06-30 07:51:11 +00:00
|
|
|
return to_list(tmp.begin(), tmp.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
tactic parser::parse_exact_apply() {
|
|
|
|
auto p = pos();
|
|
|
|
expr e = parse_expr();
|
|
|
|
check_only_contextual_locals(e, m_local_decls, p);
|
2014-06-30 16:14:55 +00:00
|
|
|
list<expr> ctx = collect_contextual_parameters(m_local_decls);
|
2014-06-30 07:51:11 +00:00
|
|
|
return tactic01([=](environment const & env, io_state const & ios, proof_state const & s) {
|
|
|
|
if (empty(s.get_goals()))
|
|
|
|
return none_proof_state();
|
|
|
|
name gname = head(s.get_goals()).first;
|
|
|
|
goal g = head(s.get_goals()).second;
|
|
|
|
goals new_gs = tail(s.get_goals());
|
|
|
|
auto const & s_locals = s.get_init_locals();
|
|
|
|
// Remark: the proof state initial hypotheses (s_locals) are essentially ctx "after elaboration".
|
|
|
|
// So, to process \c e, we must first replace its local constants with (s_locals).
|
|
|
|
name_generator ngen = s.ngen();
|
|
|
|
lean_assert(length(s_locals) == length(ctx));
|
|
|
|
buffer<expr> locals;
|
|
|
|
auto it1 = ctx;
|
|
|
|
auto it2 = s_locals;
|
|
|
|
while (!is_nil(it1) && !is_nil(it2)) {
|
|
|
|
auto const & p = head(it1);
|
2014-06-30 16:14:55 +00:00
|
|
|
locals.push_back(p);
|
2014-06-30 07:51:11 +00:00
|
|
|
it1 = tail(it1);
|
|
|
|
it2 = tail(it2);
|
|
|
|
}
|
|
|
|
std::reverse(locals.begin(), locals.end());
|
|
|
|
expr new_e = abstract_locals(e, locals.size(), locals.data());
|
|
|
|
for (auto const & l : s_locals)
|
|
|
|
new_e = instantiate(new_e, l);
|
|
|
|
parser_pos_provider pp(m_pos_table, get_stream_name(), m_last_cmd_pos);
|
|
|
|
new_e = ::lean::elaborate(env, ios, new_e, g.get_conclusion(), ngen.mk_child(),
|
2014-06-30 16:14:55 +00:00
|
|
|
m_hints, s_locals, &pp);
|
2014-06-30 07:51:11 +00:00
|
|
|
proof_builder new_pb = add_proof(s.get_pb(), gname, new_e);
|
|
|
|
return some_proof_state(proof_state(s, new_gs, new_pb, ngen));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tactic parser::parse_apply() {
|
|
|
|
auto p = pos();
|
|
|
|
expr e = parse_expr();
|
|
|
|
check_only_contextual_locals(e, m_local_decls, p);
|
2014-06-30 16:14:55 +00:00
|
|
|
list<expr> ctx = collect_contextual_parameters(m_local_decls);
|
2014-06-30 01:26:07 +00:00
|
|
|
return tactic([=](environment const & env, io_state const & ios, proof_state const & s) {
|
|
|
|
name_generator ngen = s.ngen();
|
|
|
|
auto const & s_locals = s.get_init_locals();
|
|
|
|
expr new_e = elaborate(e, ngen.mk_child(), ctx);
|
|
|
|
proof_state new_s(s, ngen);
|
|
|
|
buffer<expr> tmp;
|
|
|
|
for (auto const & p : ctx)
|
2014-06-30 16:14:55 +00:00
|
|
|
tmp.push_back(p);
|
2014-06-30 01:26:07 +00:00
|
|
|
std::reverse(tmp.begin(), tmp.end());
|
|
|
|
new_e = abstract_locals(new_e, tmp.size(), tmp.data());
|
|
|
|
for (auto const & l : s_locals)
|
|
|
|
new_e = instantiate(new_e, l);
|
|
|
|
return apply_tactic(new_e)(env, ios, new_s);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-11 00:02:06 +00:00
|
|
|
tactic parser::parse_tactic(unsigned /* rbp */) {
|
2014-06-29 19:24:13 +00:00
|
|
|
if (curr_is_token(g_lparen) || curr_is_token(g_lbracket)) {
|
|
|
|
bool paren = curr_is_token(g_lparen);
|
2014-06-16 19:28:58 +00:00
|
|
|
next();
|
2014-06-29 19:24:13 +00:00
|
|
|
buffer<tactic> choices;
|
|
|
|
tactic r = parse_tactic();
|
|
|
|
while (true) {
|
|
|
|
if (curr_is_token(g_comma)) {
|
|
|
|
next();
|
|
|
|
r = then(r, parse_tactic());
|
|
|
|
} else if (curr_is_token(g_bar)) {
|
|
|
|
next();
|
|
|
|
choices.push_back(r);
|
|
|
|
r = parse_tactic();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (paren)
|
|
|
|
check_token_next(g_rparen, "invalid tactic sequence, ')' expected");
|
|
|
|
else
|
|
|
|
check_token_next(g_rbracket, "invalid tactic sequence, ']' expected");
|
|
|
|
if (choices.empty()) {
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
choices.push_back(r);
|
|
|
|
r = choices[0];
|
|
|
|
for (unsigned i = 1; i < choices.size(); i++) {
|
|
|
|
r = orelse(r, choices[i]);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2014-06-30 06:00:41 +00:00
|
|
|
} else if (curr_is_token(g_have) || curr_is_token(g_show) || curr_is_token(g_assume) ||
|
|
|
|
curr_is_token(g_take) || curr_is_token(g_fun)) {
|
2014-06-30 07:51:11 +00:00
|
|
|
return parse_exact_apply();
|
2014-06-29 18:24:56 +00:00
|
|
|
} else {
|
|
|
|
name id;
|
|
|
|
auto p = pos();
|
|
|
|
if (curr_is_identifier()) {
|
|
|
|
id = get_name_val();
|
|
|
|
next();
|
2014-06-30 01:26:07 +00:00
|
|
|
} else if (curr_is_keyword() || curr_is_command()) {
|
2014-06-29 18:24:56 +00:00
|
|
|
id = get_token_info().value();
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
throw parser_error("invalid tactic, '(' or tactic command expected", p);
|
|
|
|
}
|
2014-06-16 19:28:58 +00:00
|
|
|
if (auto it = tactic_cmds().find(id)) {
|
2014-06-30 01:26:07 +00:00
|
|
|
return it->get_fn()(*this, p);
|
2014-06-16 19:28:58 +00:00
|
|
|
} else {
|
2014-06-29 18:24:56 +00:00
|
|
|
throw parser_error(sstream() << "unknown tactic command '" << id << "'", p);
|
2014-06-16 19:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parser::save_hint(expr const & e, tactic const & t) {
|
2014-06-29 14:03:25 +00:00
|
|
|
m_hints.insert(get_tag(e), t);
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_env = import_modules(m_env, olean_files.size(), olean_files.data(), m_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) {}
|
|
|
|
return !m_found_errors;
|
|
|
|
}
|
|
|
|
|
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-18 03:39:42 +00:00
|
|
|
|
2014-06-18 15:03:17 +00:00
|
|
|
static unsigned to_rbp(lua_State * L, int idx) {
|
2014-06-18 03:39:42 +00:00
|
|
|
int nargs = lua_gettop(L);
|
2014-06-18 15:03:17 +00:00
|
|
|
return idx < nargs ? 0 : lua_tointeger(L, idx);
|
|
|
|
}
|
|
|
|
|
2014-06-30 16:14:55 +00:00
|
|
|
typedef std::pair<local_environment, std::vector<expr>> local_scope_cell;
|
2014-06-18 15:38:23 +00:00
|
|
|
typedef std::shared_ptr<local_scope_cell> local_scope;
|
|
|
|
DECL_UDATA(local_scope);
|
|
|
|
static const struct luaL_Reg local_scope_m[] = {
|
|
|
|
{"__gc", local_scope_gc},
|
|
|
|
{0, 0}
|
|
|
|
};
|
2014-06-30 16:14:55 +00:00
|
|
|
int push_local_scope_ext(lua_State * L, local_environment const & lenv, buffer<expr> const & ps) {
|
2014-06-18 15:38:23 +00:00
|
|
|
local_scope r = std::make_shared<local_scope_cell>();
|
|
|
|
r->first = lenv;
|
|
|
|
for (auto const & p : ps)
|
|
|
|
r->second.push_back(p);
|
|
|
|
return push_local_scope(L, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void open_local_scope(lua_State * L) {
|
|
|
|
luaL_newmetatable(L, local_scope_mt);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
setfuncs(L, local_scope_m, 0);
|
|
|
|
|
|
|
|
SET_GLOBAL_FUN(local_scope_pred, "is_local_scope");
|
|
|
|
}
|
|
|
|
|
2014-06-18 15:03:17 +00:00
|
|
|
#define gparser get_global_parser(L)
|
|
|
|
|
|
|
|
static int parse_level(lua_State * L) { return push_level(L, gparser.parse_level(to_rbp(L, 1))); }
|
|
|
|
static int parse_expr(lua_State * L) { return push_expr(L, gparser.parse_expr(to_rbp(L, 1))); }
|
|
|
|
static int parse_led(lua_State * L) { return push_expr(L, gparser.parse_led(to_expr(L, 1))); }
|
2014-06-18 15:38:23 +00:00
|
|
|
static int parse_binders(lua_State * L) {
|
2014-06-30 16:14:55 +00:00
|
|
|
buffer<expr> ps;
|
2014-06-18 15:38:23 +00:00
|
|
|
auto lenv = gparser.parse_binders(ps);
|
|
|
|
return push_local_scope_ext(L, lenv, ps);
|
|
|
|
}
|
|
|
|
static int parse_binder(lua_State * L) {
|
2014-06-30 16:14:55 +00:00
|
|
|
buffer<expr> ps;
|
2014-06-18 15:38:23 +00:00
|
|
|
ps.push_back(gparser.parse_binder());
|
|
|
|
return push_local_scope_ext(L, gparser.env(), ps);
|
|
|
|
}
|
|
|
|
static int parse_scoped_expr(lua_State * L) {
|
|
|
|
local_scope const & s = to_local_scope(L, 1);
|
|
|
|
unsigned rbp = to_rbp(L, 2);
|
|
|
|
return push_expr(L, gparser.parse_scoped_expr(s->second.size(), s->second.data(), s->first, rbp));
|
|
|
|
}
|
|
|
|
static int lambda_abstract(lua_State * L) {
|
|
|
|
int nargs = lua_gettop(L);
|
|
|
|
local_scope const & s = to_local_scope(L, 1);
|
|
|
|
expr const & e = to_expr(L, 2);
|
|
|
|
expr r;
|
|
|
|
if (nargs == 2)
|
|
|
|
r = gparser.abstract(s->second.size(), s->second.data(), e, true, gparser.pos_of(e));
|
|
|
|
else
|
|
|
|
r = gparser.abstract(s->second.size(), s->second.data(), e, true, pos_info(lua_tointeger(L, 3), lua_tointeger(L, 4)));
|
|
|
|
return push_expr(L, r);
|
|
|
|
}
|
2014-06-18 15:03:17 +00:00
|
|
|
static int next(lua_State * L) { gparser.next(); return 0; }
|
|
|
|
static int curr(lua_State * L) { return push_integer(L, static_cast<unsigned>(gparser.curr())); }
|
|
|
|
static int curr_is_token(lua_State * L) { return push_boolean(L, gparser.curr_is_token(to_name_ext(L, 1))); }
|
|
|
|
static int curr_is_token_or_id(lua_State * L) { return push_boolean(L, gparser.curr_is_token_or_id(to_name_ext(L, 1))); }
|
|
|
|
static int curr_is_identifier(lua_State * L) { return push_boolean(L, gparser.curr_is_identifier()); }
|
|
|
|
static int curr_is_numeral(lua_State * L) { return push_boolean(L, gparser.curr_is_numeral()); }
|
|
|
|
static int curr_is_string(lua_State * L) { return push_boolean(L, gparser.curr_is_string()); }
|
|
|
|
static int curr_is_keyword(lua_State * L) { return push_boolean(L, gparser.curr_is_keyword()); }
|
|
|
|
static int curr_is_command(lua_State * L) { return push_boolean(L, gparser.curr_is_command()); }
|
|
|
|
static int curr_is_quoted_symbol(lua_State * L) { return push_boolean(L, gparser.curr_is_quoted_symbol()); }
|
|
|
|
static int check_token_next(lua_State * L) { gparser.check_token_next(to_name_ext(L, 1), lua_tostring(L, 2)); return 0; }
|
|
|
|
static int check_id_next(lua_State * L) { return push_name(L, gparser.check_id_next(lua_tostring(L, 1))); }
|
|
|
|
static int pos(lua_State * L) {
|
|
|
|
auto pos = gparser.pos();
|
|
|
|
push_integer(L, pos.first);
|
|
|
|
push_integer(L, pos.second);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
static int save_pos(lua_State * L) {
|
|
|
|
return push_expr(L, gparser.save_pos(to_expr(L, 1), pos_info(lua_tointeger(L, 2), lua_tointeger(L, 3))));
|
|
|
|
}
|
|
|
|
static int pos_of(lua_State * L) {
|
|
|
|
int nargs = lua_gettop(L);
|
|
|
|
pos_info pos;
|
|
|
|
if (nargs == 1)
|
|
|
|
pos = gparser.pos_of(to_expr(L, 1));
|
|
|
|
else
|
|
|
|
pos = gparser.pos_of(to_expr(L, 1), pos_info(lua_tointeger(L, 2), lua_tointeger(L, 3)));
|
|
|
|
push_integer(L, pos.first);
|
|
|
|
push_integer(L, pos.second);
|
|
|
|
return 2;
|
2014-06-18 03:39:42 +00:00
|
|
|
}
|
2014-06-18 15:03:17 +00:00
|
|
|
static int env(lua_State * L) { return push_environment(L, gparser.env()); }
|
|
|
|
static int ios(lua_State * L) { return push_io_state(L, gparser.ios()); }
|
2014-06-18 03:39:42 +00:00
|
|
|
|
|
|
|
void open_parser(lua_State * L) {
|
2014-06-18 15:38:23 +00:00
|
|
|
open_local_scope(L);
|
|
|
|
|
2014-06-18 15:03:17 +00:00
|
|
|
lua_newtable(L);
|
|
|
|
SET_FUN(parse_expr, "parse_expr");
|
|
|
|
SET_FUN(parse_level, "parse_level");
|
|
|
|
SET_FUN(parse_led, "parse_led");
|
2014-06-18 15:38:23 +00:00
|
|
|
SET_FUN(parse_binders, "parse_binders");
|
|
|
|
SET_FUN(parse_binder, "parse_binder");
|
|
|
|
SET_FUN(parse_scoped_expr, "parse_scoped_expr");
|
|
|
|
SET_FUN(lambda_abstract, "lambda_abstract");
|
|
|
|
SET_FUN(lambda_abstract, "abstract");
|
2014-06-18 15:03:17 +00:00
|
|
|
SET_FUN(next, "next");
|
|
|
|
SET_FUN(curr, "curr");
|
|
|
|
SET_FUN(curr_is_token, "curr_is_token");
|
|
|
|
SET_FUN(curr_is_token_or_id, "curr_is_token_or_id");
|
|
|
|
SET_FUN(curr_is_identifier, "curr_is_identifier");
|
|
|
|
SET_FUN(curr_is_numeral, "curr_is_numeral");
|
|
|
|
SET_FUN(curr_is_string, "curr_is_string");
|
|
|
|
SET_FUN(curr_is_keyword, "curr_is_keyword");
|
|
|
|
SET_FUN(curr_is_command, "curr_is_command");
|
|
|
|
SET_FUN(curr_is_quoted_symbol, "curr_is_quoted_symbol");
|
|
|
|
SET_FUN(check_token_next, "check_token_next");
|
|
|
|
SET_FUN(check_id_next, "check_id_next");
|
|
|
|
SET_FUN(pos, "pos");
|
|
|
|
SET_FUN(save_pos, "save_pos");
|
|
|
|
SET_FUN(pos_of, "pos_of");
|
|
|
|
SET_FUN(env, "env");
|
|
|
|
SET_FUN(ios, "ios");
|
|
|
|
lua_setglobal(L, "parser");
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
SET_ENUM("Keyword", scanner::token_kind::Keyword);
|
|
|
|
SET_ENUM("CommandKeyword", scanner::token_kind::CommandKeyword);
|
|
|
|
SET_ENUM("ScriptBlock", scanner::token_kind::ScriptBlock);
|
|
|
|
SET_ENUM("Identifier", scanner::token_kind::Identifier);
|
|
|
|
SET_ENUM("Numeral", scanner::token_kind::Numeral);
|
|
|
|
SET_ENUM("Decimal", scanner::token_kind::Decimal);
|
|
|
|
SET_ENUM("String", scanner::token_kind::String);
|
|
|
|
SET_ENUM("QuotedSymbol", scanner::token_kind::QuotedSymbol);
|
|
|
|
lua_setglobal(L, "token_kind");
|
2014-06-18 03:39:42 +00:00
|
|
|
}
|
2014-06-11 00:02:06 +00:00
|
|
|
}
|