2014-06-15 05:13:25 +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-17 17:12:04 +00:00
|
|
|
#include <utility>
|
2014-06-15 05:13:25 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
2014-09-06 18:05:07 +00:00
|
|
|
#include "util/sstream.h"
|
2014-10-19 20:39:06 +00:00
|
|
|
#include "util/utf8.h"
|
2014-06-15 17:40:18 +00:00
|
|
|
#include "kernel/abstract.h"
|
2014-09-06 18:05:07 +00:00
|
|
|
#include "kernel/replace_fn.h"
|
|
|
|
#include "library/scoped_ext.h"
|
|
|
|
#include "library/explicit.h"
|
2014-06-15 05:13:25 +00:00
|
|
|
#include "frontends/lean/parser.h"
|
2014-09-23 17:00:36 +00:00
|
|
|
#include "frontends/lean/tokens.h"
|
2014-06-15 05:13:25 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
static std::string parse_symbol(parser & p, char const * msg) {
|
|
|
|
name n;
|
|
|
|
if (p.curr_is_identifier() || p.curr_is_quoted_symbol()) {
|
|
|
|
n = p.get_name_val();
|
|
|
|
} else if (p.curr_is_keyword()) {
|
|
|
|
n = p.get_token_info().value();
|
|
|
|
} else {
|
|
|
|
throw parser_error(msg, p.pos());
|
|
|
|
}
|
|
|
|
p.next();
|
|
|
|
return n.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
static optional<unsigned> parse_optional_precedence(parser & p) {
|
|
|
|
if (p.curr_is_numeral()) {
|
|
|
|
return optional<unsigned>(p.parse_small_nat());
|
2014-09-23 17:00:36 +00:00
|
|
|
} else if (p.curr_is_token_or_id(get_max_tk())) {
|
2014-06-15 05:13:25 +00:00
|
|
|
p.next();
|
2014-11-07 15:51:40 +00:00
|
|
|
return optional<unsigned>(get_max_prec());
|
2014-06-15 05:13:25 +00:00
|
|
|
} else {
|
|
|
|
return optional<unsigned>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned parse_precedence(parser & p, char const * msg) {
|
|
|
|
auto r = parse_optional_precedence(p);
|
|
|
|
if (!r)
|
|
|
|
throw parser_error(msg, p.pos());
|
|
|
|
return *r;
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:38:55 +00:00
|
|
|
LEAN_THREAD_VALUE(bool, g_allow_local, false);
|
2014-09-06 18:05:07 +00:00
|
|
|
|
2014-10-12 00:13:33 +00:00
|
|
|
static void check_notation_expr(expr const & e, pos_info const & pos) {
|
2014-10-11 23:38:55 +00:00
|
|
|
if (!g_allow_local && (has_local(e) || has_param_univ(e)))
|
|
|
|
throw parser_error("invalid notation declaration, contains reference to local variables", pos);
|
2014-09-06 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 04:37:56 +00:00
|
|
|
enum class mixfix_kind { infixl, infixr, postfix, prefix };
|
2014-06-15 05:13:25 +00:00
|
|
|
|
|
|
|
using notation::mk_expr_action;
|
2014-06-15 17:40:18 +00:00
|
|
|
using notation::mk_binder_action;
|
|
|
|
using notation::mk_binders_action;
|
|
|
|
using notation::mk_exprs_action;
|
|
|
|
using notation::mk_scoped_expr_action;
|
2014-06-15 05:13:25 +00:00
|
|
|
using notation::mk_skip_action;
|
2014-06-18 03:39:42 +00:00
|
|
|
using notation::mk_ext_lua_action;
|
2014-06-15 05:13:25 +00:00
|
|
|
using notation::transition;
|
2014-06-15 17:40:18 +00:00
|
|
|
using notation::action;
|
2014-06-15 05:13:25 +00:00
|
|
|
|
2014-10-21 23:28:36 +00:00
|
|
|
static char const * g_forbidden_tokens[] = {"!", "@", nullptr};
|
|
|
|
|
|
|
|
void check_not_forbidden(char const * tk) {
|
|
|
|
auto it = g_forbidden_tokens;
|
|
|
|
while (*it) {
|
|
|
|
if (strcmp(*it, tk) == 0)
|
|
|
|
throw exception(sstream() << "invalid token `" << tk << "`, it is reserved");
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 04:51:42 +00:00
|
|
|
static auto parse_mixfix_notation(parser & p, mixfix_kind k, bool overload, bool reserve, bool parse_only)
|
|
|
|
-> pair<notation_entry, optional<token_entry>> {
|
2014-06-15 05:13:25 +00:00
|
|
|
std::string tk = parse_symbol(p, "invalid notation declaration, quoted symbol or identifier expected");
|
2014-10-21 22:27:45 +00:00
|
|
|
char const * tks = tk.c_str();
|
2014-10-21 23:28:36 +00:00
|
|
|
check_not_forbidden(tks);
|
2014-10-21 22:27:45 +00:00
|
|
|
environment const & env = p.env();
|
|
|
|
optional<token_entry> new_token;
|
2014-07-01 23:55:41 +00:00
|
|
|
optional<unsigned> prec;
|
2014-10-21 22:27:45 +00:00
|
|
|
|
|
|
|
optional<parse_table> reserved_pt;
|
|
|
|
optional<action> reserved_action;
|
|
|
|
if (!reserve) {
|
|
|
|
if (k == mixfix_kind::prefix) {
|
|
|
|
if (auto at = get_reserved_nud_table(p.env()).find(tks)) {
|
|
|
|
reserved_pt = at->second;
|
|
|
|
reserved_action = at->first;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (auto at = get_reserved_led_table(p.env()).find(tks)) {
|
|
|
|
reserved_pt = at->second;
|
|
|
|
reserved_action = at->first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 17:00:36 +00:00
|
|
|
if (p.curr_is_token(get_colon_tk())) {
|
2014-10-21 22:27:45 +00:00
|
|
|
if (reserved_pt)
|
|
|
|
throw parser_error("invalid notation declaration, invalid ':' occurrence "
|
|
|
|
"(declaration matches reserved notation)", p.pos());
|
2014-07-01 23:55:41 +00:00
|
|
|
p.next();
|
|
|
|
prec = parse_precedence(p, "invalid notation declaration, numeral or 'max' expected");
|
|
|
|
}
|
2014-10-21 22:27:45 +00:00
|
|
|
|
2014-11-14 06:05:37 +00:00
|
|
|
if (prec && k == mixfix_kind::infixr && *prec == 0)
|
|
|
|
throw parser_error("invalid infixr declaration, precedence must be greater than zero", p.pos());
|
|
|
|
|
2014-06-15 05:13:25 +00:00
|
|
|
if (!prec) {
|
2014-11-14 06:05:37 +00:00
|
|
|
if (reserved_action && k == mixfix_kind::prefix && reserved_action->kind() == notation::action_kind::Expr) {
|
|
|
|
prec = reserved_action->rbp();
|
|
|
|
} else if (reserved_action && k == mixfix_kind::infixr && reserved_action->kind() == notation::action_kind::Expr) {
|
|
|
|
prec = reserved_action->rbp();
|
|
|
|
} else {
|
|
|
|
prec = get_precedence(get_token_table(env), tk.c_str());
|
|
|
|
if (prec && k == mixfix_kind::infixr)
|
|
|
|
prec = *prec - 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto old_prec = get_precedence(get_token_table(env), tk.c_str());
|
|
|
|
if (!old_prec || (k != mixfix_kind::prefix && old_prec != prec))
|
|
|
|
new_token = token_entry(tk.c_str(), *prec);
|
|
|
|
if (k == mixfix_kind::infixr)
|
|
|
|
prec = *prec - 1;
|
2014-06-15 05:13:25 +00:00
|
|
|
}
|
|
|
|
|
2014-10-21 22:27:45 +00:00
|
|
|
if (!prec) {
|
|
|
|
lean_assert(!reserved_pt);
|
2014-10-21 20:30:40 +00:00
|
|
|
throw parser_error("invalid notation declaration, precedence was not provided, "
|
|
|
|
"and it is not set for the given symbol, "
|
2014-06-15 05:13:25 +00:00
|
|
|
"solution: use the 'precedence' command", p.pos());
|
2014-10-21 22:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (reserved_action) {
|
|
|
|
switch (k) {
|
|
|
|
case mixfix_kind::infixl:
|
|
|
|
if (reserved_action->kind() != notation::action_kind::Expr || reserved_action->rbp() != *prec)
|
|
|
|
throw parser_error("invalid infixl declaration, declaration conflicts with reserved notation", p.pos());
|
|
|
|
break;
|
|
|
|
case mixfix_kind::infixr:
|
2014-11-14 06:05:37 +00:00
|
|
|
if (reserved_action->kind() != notation::action_kind::Expr || reserved_action->rbp() != *prec)
|
2014-10-21 22:27:45 +00:00
|
|
|
throw parser_error("invalid infixr declaration, declaration conflicts with reserved notation", p.pos());
|
|
|
|
break;
|
|
|
|
case mixfix_kind::postfix:
|
|
|
|
if (reserved_action->kind() != notation::action_kind::Skip)
|
|
|
|
throw parser_error("invalid postfix declaration, declaration conflicts with reserved notation", p.pos());
|
|
|
|
break;
|
|
|
|
case mixfix_kind::prefix:
|
|
|
|
if (reserved_action->kind() != notation::action_kind::Expr || reserved_action->rbp() != *prec)
|
|
|
|
throw parser_error("invalid prefix declaration, declaration conflicts with reserved notation", p.pos());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 20:30:40 +00:00
|
|
|
if (reserve) {
|
|
|
|
// reserve notation commands do not have a denotation
|
2014-10-21 22:27:45 +00:00
|
|
|
expr dummy = mk_Prop();
|
2014-10-21 20:30:40 +00:00
|
|
|
if (p.curr_is_token(get_assign_tk()))
|
|
|
|
throw parser_error("invalid reserve notation, found `:=`", p.pos());
|
2014-10-21 22:27:45 +00:00
|
|
|
switch (k) {
|
|
|
|
case mixfix_kind::infixl:
|
|
|
|
return mk_pair(notation_entry(false, to_list(transition(tks, mk_expr_action(*prec))),
|
2014-11-17 04:51:42 +00:00
|
|
|
dummy, overload, reserve, parse_only), new_token);
|
2014-10-21 22:27:45 +00:00
|
|
|
case mixfix_kind::infixr:
|
2014-11-14 06:05:37 +00:00
|
|
|
return mk_pair(notation_entry(false, to_list(transition(tks, mk_expr_action(*prec))),
|
2014-11-17 04:51:42 +00:00
|
|
|
dummy, overload, reserve, parse_only), new_token);
|
2014-10-21 22:27:45 +00:00
|
|
|
case mixfix_kind::postfix:
|
|
|
|
return mk_pair(notation_entry(false, to_list(transition(tks, mk_skip_action())),
|
2014-11-17 04:51:42 +00:00
|
|
|
dummy, overload, reserve, parse_only), new_token);
|
2014-10-21 22:27:45 +00:00
|
|
|
case mixfix_kind::prefix:
|
|
|
|
return mk_pair(notation_entry(true, to_list(transition(tks, mk_expr_action(*prec))),
|
2014-11-17 04:51:42 +00:00
|
|
|
dummy, overload, reserve, parse_only), new_token);
|
2014-10-21 22:27:45 +00:00
|
|
|
}
|
2014-10-21 20:30:40 +00:00
|
|
|
} else {
|
|
|
|
p.check_token_next(get_assign_tk(), "invalid notation declaration, ':=' expected");
|
|
|
|
auto f_pos = p.pos();
|
2014-10-21 22:27:45 +00:00
|
|
|
expr f = p.parse_expr();
|
2014-10-21 20:30:40 +00:00
|
|
|
check_notation_expr(f, f_pos);
|
2014-10-21 22:27:45 +00:00
|
|
|
switch (k) {
|
|
|
|
case mixfix_kind::infixl:
|
2014-11-15 00:25:13 +00:00
|
|
|
#if defined(__GNUC__) && !defined(__CLANG__)
|
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
2014-10-21 22:27:45 +00:00
|
|
|
return mk_pair(notation_entry(false, to_list(transition(tks, mk_expr_action(*prec))),
|
2014-11-17 04:51:42 +00:00
|
|
|
mk_app(f, Var(1), Var(0)), overload, reserve, parse_only), new_token);
|
2014-11-15 00:25:13 +00:00
|
|
|
#endif
|
2014-10-21 22:27:45 +00:00
|
|
|
case mixfix_kind::infixr:
|
2014-11-14 06:05:37 +00:00
|
|
|
return mk_pair(notation_entry(false, to_list(transition(tks, mk_expr_action(*prec))),
|
2014-11-17 04:51:42 +00:00
|
|
|
mk_app(f, Var(1), Var(0)), overload, reserve, parse_only), new_token);
|
2014-10-21 22:27:45 +00:00
|
|
|
case mixfix_kind::postfix:
|
|
|
|
return mk_pair(notation_entry(false, to_list(transition(tks, mk_skip_action())),
|
2014-11-17 04:51:42 +00:00
|
|
|
mk_app(f, Var(0)), overload, reserve, parse_only), new_token);
|
2014-10-21 22:27:45 +00:00
|
|
|
case mixfix_kind::prefix:
|
|
|
|
return mk_pair(notation_entry(true, to_list(transition(tks, mk_expr_action(*prec))),
|
2014-11-17 04:51:42 +00:00
|
|
|
mk_app(f, Var(0)), overload, reserve, parse_only), new_token);
|
2014-10-21 22:27:45 +00:00
|
|
|
}
|
2014-06-15 05:13:25 +00:00
|
|
|
}
|
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
|
|
|
}
|
|
|
|
|
2014-10-21 20:30:40 +00:00
|
|
|
static notation_entry parse_mixfix_notation(parser & p, mixfix_kind k, bool overload, bool reserve,
|
2014-11-17 04:51:42 +00:00
|
|
|
buffer<token_entry> & new_tokens, bool parse_only) {
|
|
|
|
auto nt = parse_mixfix_notation(p, k, overload, reserve, parse_only);
|
2014-06-17 17:12:04 +00:00
|
|
|
if (nt.second)
|
|
|
|
new_tokens.push_back(*nt.second);
|
|
|
|
return nt.first;
|
|
|
|
}
|
|
|
|
|
2014-11-15 00:25:13 +00:00
|
|
|
static name parse_quoted_symbol_or_token(parser & p, buffer<token_entry> & new_tokens, bool & used_default) {
|
|
|
|
used_default = false;
|
2014-06-15 17:40:18 +00:00
|
|
|
if (p.curr_is_quoted_symbol()) {
|
2014-06-17 17:12:04 +00:00
|
|
|
environment const & env = p.env();
|
2014-06-15 17:40:18 +00:00
|
|
|
auto tk = p.get_name_val();
|
|
|
|
auto tks = tk.to_string();
|
|
|
|
auto tkcs = tks.c_str();
|
2014-10-21 23:28:36 +00:00
|
|
|
check_not_forbidden(tkcs);
|
2014-06-15 17:40:18 +00:00
|
|
|
p.next();
|
2014-09-23 17:00:36 +00:00
|
|
|
if (p.curr_is_token(get_colon_tk())) {
|
2014-06-15 17:40:18 +00:00
|
|
|
p.next();
|
|
|
|
unsigned prec = parse_precedence(p, "invalid notation declaration, precedence (small numeral) expected");
|
|
|
|
auto old_prec = get_precedence(get_token_table(env), tkcs);
|
|
|
|
if (!old_prec || prec != *old_prec)
|
2014-06-17 17:12:04 +00:00
|
|
|
new_tokens.push_back(token_entry(tkcs, prec));
|
2014-06-15 17:40:18 +00:00
|
|
|
} else if (!get_precedence(get_token_table(env), tkcs)) {
|
2014-10-21 01:40:55 +00:00
|
|
|
new_tokens.push_back(token_entry(tkcs, LEAN_DEFAULT_PRECEDENCE));
|
2014-11-15 00:25:13 +00:00
|
|
|
used_default = true;
|
2014-06-15 17:40:18 +00:00
|
|
|
}
|
|
|
|
return tk;
|
2014-06-18 16:14:50 +00:00
|
|
|
} else if (p.curr_is_keyword()) {
|
|
|
|
auto tk = p.get_token_info().token();
|
2014-10-21 23:28:36 +00:00
|
|
|
check_not_forbidden(tk.to_string().c_str());
|
2014-06-18 16:14:50 +00:00
|
|
|
p.next();
|
|
|
|
return tk;
|
2014-06-15 17:40:18 +00:00
|
|
|
} else {
|
2014-06-18 16:14:50 +00:00
|
|
|
throw parser_error("invalid notation declaration, symbol expected", p.pos());
|
2014-06-15 17:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 00:25:13 +00:00
|
|
|
static name parse_quoted_symbol_or_token(parser & p, buffer<token_entry> & new_tokens) {
|
|
|
|
bool dummy;
|
|
|
|
return parse_quoted_symbol_or_token(p, new_tokens, dummy);
|
|
|
|
}
|
|
|
|
|
2014-06-15 17:40:18 +00:00
|
|
|
static expr parse_notation_expr(parser & p, buffer<expr> const & locals) {
|
2014-10-11 23:38:55 +00:00
|
|
|
auto pos = p.pos();
|
2014-06-15 17:40:18 +00:00
|
|
|
expr r = p.parse_expr();
|
2014-10-11 23:38:55 +00:00
|
|
|
r = abstract(r, locals.size(), locals.data());
|
2014-10-12 00:13:33 +00:00
|
|
|
check_notation_expr(r, pos);
|
2014-10-11 23:38:55 +00:00
|
|
|
return r;
|
2014-06-15 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_notation_local(parser & p, buffer<expr> & locals) {
|
|
|
|
if (p.curr_is_identifier()) {
|
|
|
|
name n = p.get_name_val();
|
|
|
|
p.next();
|
2014-09-23 17:00:36 +00:00
|
|
|
expr local_type = mk_Prop(); // type used in notation local declarations, it is irrelevant
|
|
|
|
expr l = mk_local(n, local_type); // remark: the type doesn't matter
|
2014-10-09 00:40:04 +00:00
|
|
|
p.add_local(l);
|
2014-06-15 17:40:18 +00:00
|
|
|
locals.push_back(l);
|
|
|
|
} else {
|
|
|
|
throw parser_error("invalid notation declaration, identifier expected", p.pos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:38:55 +00:00
|
|
|
static unsigned get_precedence(environment const & env, buffer<token_entry> const & new_tokens, name const & token) {
|
2014-07-03 23:38:49 +00:00
|
|
|
std::string token_str = token.to_string();
|
|
|
|
for (auto const & e : new_tokens) {
|
|
|
|
if (e.m_token == token_str)
|
|
|
|
return e.m_prec;
|
|
|
|
}
|
|
|
|
auto prec = get_precedence(get_token_table(env), token_str.c_str());
|
|
|
|
if (prec)
|
|
|
|
return *prec;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:38:55 +00:00
|
|
|
static action parse_action(parser & p, name const & prev_token, unsigned default_prec,
|
|
|
|
buffer<expr> & locals, buffer<token_entry> & new_tokens) {
|
2014-09-23 17:00:36 +00:00
|
|
|
if (p.curr_is_token(get_colon_tk())) {
|
2014-06-15 17:40:18 +00:00
|
|
|
p.next();
|
2014-09-23 17:00:36 +00:00
|
|
|
if (p.curr_is_numeral() || p.curr_is_token_or_id(get_max_tk())) {
|
2014-06-15 17:40:18 +00:00
|
|
|
unsigned prec = parse_precedence(p, "invalid notation declaration, small numeral expected");
|
|
|
|
return mk_expr_action(prec);
|
2014-09-23 17:00:36 +00:00
|
|
|
} else if (p.curr_is_token_or_id(get_prev_tk())) {
|
2014-07-03 23:38:49 +00:00
|
|
|
p.next();
|
|
|
|
return mk_expr_action(get_precedence(p.env(), new_tokens, prev_token));
|
2014-06-18 03:39:42 +00:00
|
|
|
} else if (p.curr_is_string()) {
|
|
|
|
std::string fn = p.get_str_val();
|
|
|
|
p.next();
|
|
|
|
return mk_ext_lua_action(fn.c_str());
|
2014-09-23 17:00:36 +00:00
|
|
|
} else if (p.curr_is_token_or_id(get_scoped_tk())) {
|
2014-06-15 17:40:18 +00:00
|
|
|
p.next();
|
|
|
|
return mk_scoped_expr_action(mk_var(0));
|
|
|
|
} else {
|
2014-09-23 17:00:36 +00:00
|
|
|
p.check_token_next(get_lparen_tk(), "invalid notation declaration, '(', numeral or 'scoped' expected");
|
|
|
|
if (p.curr_is_token_or_id(get_foldl_tk()) || p.curr_is_token_or_id(get_foldr_tk())) {
|
|
|
|
bool is_fold_right = p.curr_is_token_or_id(get_foldr_tk());
|
2014-06-15 17:40:18 +00:00
|
|
|
p.next();
|
|
|
|
auto prec = parse_optional_precedence(p);
|
2014-06-18 16:14:50 +00:00
|
|
|
name sep = parse_quoted_symbol_or_token(p, new_tokens);
|
2014-06-15 17:40:18 +00:00
|
|
|
expr rec;
|
|
|
|
{
|
|
|
|
parser::local_scope scope(p);
|
2014-09-23 17:00:36 +00:00
|
|
|
p.check_token_next(get_lparen_tk(), "invalid fold notation argument, '(' expected");
|
2014-06-15 17:40:18 +00:00
|
|
|
parse_notation_local(p, locals);
|
|
|
|
parse_notation_local(p, locals);
|
2014-09-23 17:00:36 +00:00
|
|
|
p.check_token_next(get_comma_tk(), "invalid fold notation argument, ',' expected");
|
2014-06-15 17:40:18 +00:00
|
|
|
rec = parse_notation_expr(p, locals);
|
2014-09-23 17:00:36 +00:00
|
|
|
p.check_token_next(get_rparen_tk(), "invalid fold notation argument, ')' expected");
|
2014-06-15 17:40:18 +00:00
|
|
|
locals.pop_back();
|
|
|
|
locals.pop_back();
|
|
|
|
}
|
2014-11-09 22:08:33 +00:00
|
|
|
optional<expr> ini;
|
|
|
|
if (!p.curr_is_token(get_rparen_tk()) && !p.curr_is_quoted_symbol())
|
|
|
|
ini = parse_notation_expr(p, locals);
|
2014-07-30 22:04:44 +00:00
|
|
|
optional<name> terminator;
|
2014-09-23 17:00:36 +00:00
|
|
|
if (!p.curr_is_token(get_rparen_tk()))
|
2014-07-30 22:04:44 +00:00
|
|
|
terminator = parse_quoted_symbol_or_token(p, new_tokens);
|
2014-09-23 17:00:36 +00:00
|
|
|
p.check_token_next(get_rparen_tk(), "invalid fold notation argument, ')' expected");
|
2014-07-30 22:04:44 +00:00
|
|
|
return mk_exprs_action(sep, rec, ini, terminator, is_fold_right, prec ? *prec : 0);
|
2014-09-23 17:00:36 +00:00
|
|
|
} else if (p.curr_is_token_or_id(get_scoped_tk())) {
|
2014-06-15 17:40:18 +00:00
|
|
|
p.next();
|
|
|
|
auto prec = parse_optional_precedence(p);
|
|
|
|
expr rec;
|
|
|
|
{
|
|
|
|
parser::local_scope scope(p);
|
|
|
|
parse_notation_local(p, locals);
|
2014-09-23 17:00:36 +00:00
|
|
|
p.check_token_next(get_comma_tk(), "invalid scoped notation argument, ',' expected");
|
2014-06-15 17:40:18 +00:00
|
|
|
rec = parse_notation_expr(p, locals);
|
|
|
|
locals.pop_back();
|
|
|
|
}
|
2014-09-23 17:00:36 +00:00
|
|
|
p.check_token_next(get_rparen_tk(), "invalid scoped notation argument, ')' expected");
|
2014-06-15 17:40:18 +00:00
|
|
|
return mk_scoped_expr_action(rec, prec ? *prec : 0);
|
2014-09-23 17:00:36 +00:00
|
|
|
} else if (p.curr_is_token_or_id(get_call_tk())) {
|
2014-06-18 03:39:42 +00:00
|
|
|
p.next();
|
|
|
|
name fn = p.check_id_next("invalid call notation argument, identifier expected");
|
2014-09-23 17:00:36 +00:00
|
|
|
p.check_token_next(get_rparen_tk(), "invalid call notation argument, ')' expected");
|
2014-06-18 03:39:42 +00:00
|
|
|
return mk_ext_lua_action(fn.to_string().c_str());
|
2014-06-15 17:40:18 +00:00
|
|
|
} else {
|
|
|
|
throw parser_error("invalid notation declaration, 'foldl', 'foldr' or 'scoped' expected", p.pos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-07-04 00:23:29 +00:00
|
|
|
return mk_expr_action(default_prec);
|
2014-06-15 17:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 00:23:29 +00:00
|
|
|
/**
|
|
|
|
\brief If the action for token \c tk in parse table \c pt is an Expr action,
|
|
|
|
then return its precedence. We use this value as the default precedence
|
|
|
|
when the user does not provide it. The idea is to minimize conflict with existing
|
|
|
|
notation.
|
|
|
|
*/
|
2014-10-11 23:38:55 +00:00
|
|
|
static unsigned get_default_prec(optional<parse_table> const & pt, name const & tk) {
|
2014-07-04 00:23:29 +00:00
|
|
|
if (!pt)
|
2014-10-21 01:40:55 +00:00
|
|
|
return LEAN_DEFAULT_PRECEDENCE;
|
2014-07-04 00:23:29 +00:00
|
|
|
if (auto at = pt->find(tk)) {
|
|
|
|
if (at->first.kind() == notation::action_kind::Expr)
|
|
|
|
return at->first.rbp();
|
|
|
|
}
|
2014-10-21 01:40:55 +00:00
|
|
|
return LEAN_DEFAULT_PRECEDENCE;
|
2014-07-04 00:23:29 +00:00
|
|
|
}
|
|
|
|
|
2014-10-21 20:30:40 +00:00
|
|
|
/** \brief Given a parsing table a \c pt and transition \c new_trans, if \c new_trans is a
|
|
|
|
transition in \c pt, then return the successor table */
|
|
|
|
static optional<parse_table> find_match(optional<parse_table> const & pt, transition const & new_trans) {
|
|
|
|
if (pt) {
|
|
|
|
if (auto at = pt->find(new_trans.get_token())) {
|
|
|
|
if (new_trans.get_action().is_equal(at->first))
|
|
|
|
return optional<parse_table>(at->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return optional<parse_table>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Lift parse_table::find method to optional<parse_table> */
|
|
|
|
static optional<pair<action, parse_table>> find_next(optional<parse_table> const & pt, name const & tk) {
|
|
|
|
if (pt)
|
|
|
|
return pt->find(tk);
|
|
|
|
else
|
|
|
|
return optional<pair<action, parse_table>>();
|
|
|
|
}
|
|
|
|
|
2014-11-24 00:42:53 +00:00
|
|
|
static unsigned parse_binders_rbp(parser & p) {
|
|
|
|
if (p.curr_is_token(get_colon_tk())) {
|
|
|
|
p.next();
|
|
|
|
return parse_precedence(p, "invalid binder/binders, precedence expected");
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 04:51:42 +00:00
|
|
|
static notation_entry parse_notation_core(parser & p, bool overload, bool reserve, buffer<token_entry> & new_tokens, bool parse_only) {
|
2014-06-15 17:40:18 +00:00
|
|
|
buffer<expr> locals;
|
|
|
|
buffer<transition> ts;
|
|
|
|
parser::local_scope scope(p);
|
|
|
|
bool is_nud = true;
|
2014-07-04 00:23:29 +00:00
|
|
|
optional<parse_table> pt;
|
2014-10-21 20:30:40 +00:00
|
|
|
optional<parse_table> reserved_pt;
|
2014-10-03 20:54:11 +00:00
|
|
|
if (p.curr_is_numeral()) {
|
|
|
|
lean_assert(p.curr_is_numeral());
|
|
|
|
mpz num = p.get_num_val().get_numerator();
|
|
|
|
p.next();
|
|
|
|
p.check_token_next(get_assign_tk(), "invalid numeral notation, `:=` expected");
|
2014-10-11 23:38:55 +00:00
|
|
|
auto e_pos = p.pos();
|
|
|
|
expr e = p.parse_expr();
|
2014-10-12 00:13:33 +00:00
|
|
|
check_notation_expr(e, e_pos);
|
2014-11-17 04:51:42 +00:00
|
|
|
return notation_entry(num, e, overload, parse_only);
|
2014-10-03 20:54:11 +00:00
|
|
|
} else if (p.curr_is_identifier()) {
|
2014-06-15 17:40:18 +00:00
|
|
|
parse_notation_local(p, locals);
|
|
|
|
is_nud = false;
|
2014-07-04 00:23:29 +00:00
|
|
|
pt = get_led_table(p.env());
|
2014-10-21 20:30:40 +00:00
|
|
|
if (!reserve)
|
|
|
|
reserved_pt = get_reserved_led_table(p.env());
|
2014-07-04 00:23:29 +00:00
|
|
|
} else {
|
|
|
|
pt = get_nud_table(p.env());
|
2014-10-21 20:30:40 +00:00
|
|
|
if (!reserve)
|
|
|
|
reserved_pt = get_reserved_nud_table(p.env());
|
2014-06-15 17:40:18 +00:00
|
|
|
}
|
2014-11-15 00:25:13 +00:00
|
|
|
bool used_default = false;
|
2014-10-21 20:30:40 +00:00
|
|
|
while ((!reserve && !p.curr_is_token(get_assign_tk())) ||
|
|
|
|
(reserve && !p.curr_is_command() && !p.curr_is_eof())) {
|
2014-11-15 00:25:13 +00:00
|
|
|
name tk = parse_quoted_symbol_or_token(p, new_tokens, used_default);
|
2014-10-21 20:30:40 +00:00
|
|
|
if (auto at = find_next(reserved_pt, tk)) {
|
|
|
|
action const & a = at->first;
|
|
|
|
reserved_pt = at->second;
|
|
|
|
switch (a.kind()) {
|
|
|
|
case notation::action_kind::Skip:
|
|
|
|
if (!p.curr_is_quoted_symbol() && !p.curr_is_keyword() && !p.curr_is_token(get_assign_tk())) {
|
|
|
|
p.check_token_or_id_next(get_binders_tk(),
|
|
|
|
"invalid notation declaration, quoted-symbol, keyword or `:=` expected "
|
|
|
|
"(declaration prefix matches reserved notation)");
|
2014-07-04 00:23:29 +00:00
|
|
|
}
|
2014-10-21 20:30:40 +00:00
|
|
|
ts.push_back(transition(tk, a));
|
|
|
|
break;
|
|
|
|
case notation::action_kind::Binder:
|
|
|
|
p.check_token_or_id_next(get_binders_tk(),
|
|
|
|
"invalid notation declaration, 'binder' expected "
|
|
|
|
"(declaration prefix matches reserved notation)");
|
|
|
|
ts.push_back(transition(tk, a));
|
|
|
|
break;
|
|
|
|
case notation::action_kind::Binders:
|
|
|
|
p.check_token_or_id_next(get_binders_tk(),
|
|
|
|
"invalid notation declaration, 'binders' expected "
|
|
|
|
"(declaration prefix matches reserved notation)");
|
|
|
|
ts.push_back(transition(tk, a));
|
|
|
|
break;
|
|
|
|
case notation::action_kind::Expr: case notation::action_kind::Exprs: case notation::action_kind::ScopedExpr:
|
|
|
|
case notation::action_kind::Ext: case notation::action_kind::LuaExt: {
|
|
|
|
name n = p.check_id_next("invalid notation declaration, identifier expected "
|
|
|
|
"(declaration prefix matches reserved notation)");
|
|
|
|
expr local_type = mk_Prop(); // type used in notation local declarations, it is irrelevant
|
|
|
|
expr l = mk_local(n, local_type);
|
|
|
|
p.add_local(l);
|
|
|
|
locals.push_back(l);
|
|
|
|
ts.push_back(transition(tk, a));
|
|
|
|
break;
|
|
|
|
}}
|
|
|
|
if (p.curr_is_token(get_colon_tk()))
|
2014-10-21 22:27:45 +00:00
|
|
|
throw parser_error("invalid notation declaration, invalid ':' occurrence "
|
2014-10-21 20:30:40 +00:00
|
|
|
"(declaration prefix matches reserved notation)", p.pos());
|
|
|
|
} else {
|
|
|
|
reserved_pt = optional<parse_table>();
|
|
|
|
if (p.curr_is_token_or_id(get_binder_tk())) {
|
|
|
|
p.next();
|
2014-11-24 00:42:53 +00:00
|
|
|
unsigned rbp = parse_binders_rbp(p);
|
|
|
|
ts.push_back(transition(tk, mk_binder_action(rbp)));
|
2014-10-21 20:30:40 +00:00
|
|
|
} else if (p.curr_is_token_or_id(get_binders_tk())) {
|
|
|
|
p.next();
|
2014-11-24 00:42:53 +00:00
|
|
|
unsigned rbp = parse_binders_rbp(p);
|
|
|
|
ts.push_back(transition(tk, mk_binders_action(rbp)));
|
2014-10-21 20:30:40 +00:00
|
|
|
} else if (p.curr_is_identifier()) {
|
|
|
|
unsigned default_prec = get_default_prec(pt, tk);
|
|
|
|
name n = p.get_name_val();
|
|
|
|
p.next();
|
|
|
|
action a = parse_action(p, tk, default_prec, locals, new_tokens);
|
|
|
|
expr local_type = mk_Prop(); // type used in notation local declarations, it is irrelevant
|
|
|
|
expr l = mk_local(n, local_type);
|
|
|
|
p.add_local(l);
|
|
|
|
locals.push_back(l);
|
|
|
|
ts.push_back(transition(tk, a));
|
|
|
|
} else if (p.curr_is_quoted_symbol() || p.curr_is_keyword() ||
|
|
|
|
p.curr_is_token(get_assign_tk()) || p.curr_is_command() || p.curr_is_eof()) {
|
|
|
|
ts.push_back(transition(tk, mk_skip_action()));
|
2014-07-04 00:23:29 +00:00
|
|
|
} else {
|
2014-10-21 20:30:40 +00:00
|
|
|
throw parser_error("invalid notation declaration, quoted-symbol, identifier, "
|
|
|
|
"'binder', 'binders' expected", p.pos());
|
2014-07-04 00:23:29 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-21 20:30:40 +00:00
|
|
|
pt = find_match(pt, ts.back());
|
2014-06-15 17:40:18 +00:00
|
|
|
}
|
2014-11-15 00:25:13 +00:00
|
|
|
// for atomic notation where binding power was not set, we set it to max
|
|
|
|
if (used_default && ts.size() == 1 && ts.back().get_action().kind() == notation::action_kind::Skip) {
|
|
|
|
lean_assert(!new_tokens.empty());
|
|
|
|
new_tokens.back().m_prec = get_max_prec();
|
|
|
|
}
|
2014-10-21 20:30:40 +00:00
|
|
|
expr n;
|
|
|
|
if (reserve) {
|
|
|
|
// reserve notation commands do not have a denotation
|
|
|
|
lean_assert(p.curr_is_command() || p.curr_is_eof());
|
|
|
|
expr dummy = mk_Prop(); // any expression without free variables will do
|
|
|
|
n = dummy;
|
|
|
|
} else {
|
|
|
|
lean_assert(p.curr_is_token(get_assign_tk()));
|
|
|
|
p.next();
|
|
|
|
if (ts.empty())
|
|
|
|
throw parser_error("invalid notation declaration, empty notation is not allowed", p.pos());
|
|
|
|
n = parse_notation_expr(p, locals);
|
|
|
|
}
|
2014-11-17 04:51:42 +00:00
|
|
|
return notation_entry(is_nud, to_list(ts.begin(), ts.end()), n, overload, reserve, parse_only);
|
2014-06-17 17:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool curr_is_notation_decl(parser & p) {
|
2014-10-11 23:38:55 +00:00
|
|
|
return
|
|
|
|
p.curr_is_token(get_infix_tk()) || p.curr_is_token(get_infixl_tk()) || p.curr_is_token(get_infixr_tk()) ||
|
2014-09-23 17:00:36 +00:00
|
|
|
p.curr_is_token(get_postfix_tk()) || p.curr_is_token(get_prefix_tk()) || p.curr_is_token(get_notation_tk());
|
2014-06-17 17:12:04 +00:00
|
|
|
}
|
|
|
|
|
2014-10-21 20:30:40 +00:00
|
|
|
static notation_entry parse_notation(parser & p, bool overload, bool reserve, buffer<token_entry> & new_tokens,
|
|
|
|
bool allow_local) {
|
2014-11-17 04:51:42 +00:00
|
|
|
bool parse_only = false;
|
2014-10-11 23:38:55 +00:00
|
|
|
flet<bool> set_allow_local(g_allow_local, allow_local);
|
2014-09-23 17:00:36 +00:00
|
|
|
if (p.curr_is_token(get_infix_tk()) || p.curr_is_token(get_infixl_tk())) {
|
2014-06-17 17:12:04 +00:00
|
|
|
p.next();
|
2014-11-17 04:51:42 +00:00
|
|
|
return parse_mixfix_notation(p, mixfix_kind::infixl, overload, reserve, new_tokens, parse_only);
|
2014-09-23 17:00:36 +00:00
|
|
|
} else if (p.curr_is_token(get_infixr_tk())) {
|
2014-06-17 17:12:04 +00:00
|
|
|
p.next();
|
2014-11-17 04:51:42 +00:00
|
|
|
return parse_mixfix_notation(p, mixfix_kind::infixr, overload, reserve, new_tokens, parse_only);
|
2014-09-23 17:00:36 +00:00
|
|
|
} else if (p.curr_is_token(get_postfix_tk())) {
|
2014-06-17 17:12:04 +00:00
|
|
|
p.next();
|
2014-11-17 04:51:42 +00:00
|
|
|
return parse_mixfix_notation(p, mixfix_kind::postfix, overload, reserve, new_tokens, parse_only);
|
2014-09-23 17:00:36 +00:00
|
|
|
} else if (p.curr_is_token(get_prefix_tk())) {
|
2014-07-04 04:37:56 +00:00
|
|
|
p.next();
|
2014-11-17 04:51:42 +00:00
|
|
|
return parse_mixfix_notation(p, mixfix_kind::prefix, overload, reserve, new_tokens, parse_only);
|
2014-09-23 17:00:36 +00:00
|
|
|
} else if (p.curr_is_token(get_notation_tk())) {
|
2014-06-17 17:12:04 +00:00
|
|
|
p.next();
|
2014-11-17 04:51:42 +00:00
|
|
|
return parse_notation_core(p, overload, reserve, new_tokens, parse_only);
|
2014-06-17 17:12:04 +00:00
|
|
|
} else {
|
2014-10-21 20:30:40 +00:00
|
|
|
throw parser_error("invalid notation, 'infix', 'infixl', 'infixr', 'prefix', "
|
|
|
|
"'postfix' or 'notation' expected", p.pos());
|
2014-06-17 17:12:04 +00:00
|
|
|
}
|
2014-06-15 05:13:25 +00:00
|
|
|
}
|
2014-06-17 20:39:44 +00:00
|
|
|
|
2014-10-21 20:30:40 +00:00
|
|
|
notation_entry parse_notation(parser & p, bool overload, buffer<token_entry> & new_tokens, bool allow_local) {
|
|
|
|
bool reserve = false;
|
|
|
|
return parse_notation(p, overload, reserve, new_tokens, allow_local);
|
|
|
|
}
|
|
|
|
|
2014-10-19 20:39:06 +00:00
|
|
|
static char g_reserved_chars[] = {'(', ')', ',', 0};
|
|
|
|
|
|
|
|
static void check_token(char const * tk) {
|
|
|
|
while (tk && *tk) {
|
|
|
|
unsigned sz = get_utf8_size(*tk);
|
|
|
|
if (sz == 0) {
|
|
|
|
throw exception(sstream() << "invalid token `" << tk << "`, contains invalid utf-8 character");
|
|
|
|
} else if (sz > 1) {
|
|
|
|
// multi-size unicode character
|
|
|
|
for (unsigned i = 0; i < sz; i++) {
|
|
|
|
if (!*tk)
|
|
|
|
throw exception(sstream() << "invalid token `" << tk << "`, contains invalid utf-8 character");
|
|
|
|
++tk;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto it = g_reserved_chars;
|
|
|
|
while (*it) {
|
|
|
|
if (*tk == *it)
|
2014-10-21 20:30:40 +00:00
|
|
|
throw exception(sstream() << "invalid token `" << tk
|
|
|
|
<< "`, it contains reserved character `" << *it << "`");
|
2014-10-19 20:39:06 +00:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
++tk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 04:51:42 +00:00
|
|
|
static environment add_user_token(environment const & env, token_entry const & e, bool persistent) {
|
2014-10-19 20:39:06 +00:00
|
|
|
check_token(e.m_token.c_str());
|
2014-11-17 04:51:42 +00:00
|
|
|
return add_token(env, e, persistent);
|
2014-10-19 20:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static environment add_user_token(environment const & env, char const * val, unsigned prec) {
|
|
|
|
check_token(val);
|
|
|
|
return add_token(env, val, prec);
|
|
|
|
}
|
|
|
|
|
2014-11-17 04:51:42 +00:00
|
|
|
struct notation_modifiers {
|
|
|
|
bool m_persistent;
|
|
|
|
bool m_parse_only;
|
|
|
|
notation_modifiers():m_persistent(true), m_parse_only(false) {}
|
|
|
|
void parse(parser & p) {
|
|
|
|
while (true) {
|
|
|
|
if (p.curr_is_token(get_local_tk())) {
|
|
|
|
p.next();
|
|
|
|
m_persistent = false;
|
|
|
|
} else if (p.curr_is_token(get_parsing_only_tk())) {
|
|
|
|
p.next();
|
|
|
|
m_parse_only = true;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-21 20:30:40 +00:00
|
|
|
static environment notation_cmd_core(parser & p, bool overload, bool reserve) {
|
2014-11-17 04:51:42 +00:00
|
|
|
notation_modifiers mods;
|
|
|
|
mods.parse(p);
|
|
|
|
flet<bool> set_allow_local(g_allow_local, in_context(p.env()) || !mods.m_persistent);
|
2014-10-11 23:38:55 +00:00
|
|
|
environment env = p.env();
|
|
|
|
buffer<token_entry> new_tokens;
|
2014-11-17 04:51:42 +00:00
|
|
|
auto ne = parse_notation_core(p, overload, reserve, new_tokens, mods.m_parse_only);
|
2014-10-11 23:38:55 +00:00
|
|
|
for (auto const & te : new_tokens)
|
2014-11-17 04:51:42 +00:00
|
|
|
env = add_user_token(env, te, mods.m_persistent);
|
|
|
|
env = add_notation(env, ne, mods.m_persistent);
|
2014-10-11 23:38:55 +00:00
|
|
|
return env;
|
|
|
|
}
|
2014-11-17 04:51:42 +00:00
|
|
|
|
2014-10-21 20:30:40 +00:00
|
|
|
static environment mixfix_cmd(parser & p, mixfix_kind k, bool overload, bool reserve) {
|
2014-11-17 04:51:42 +00:00
|
|
|
notation_modifiers mods;
|
|
|
|
mods.parse(p);
|
|
|
|
flet<bool> set_allow_local(g_allow_local, in_context(p.env()) || !mods.m_persistent);
|
|
|
|
auto nt = parse_mixfix_notation(p, k, overload, reserve, mods.m_parse_only);
|
2014-10-11 23:38:55 +00:00
|
|
|
environment env = p.env();
|
|
|
|
if (nt.second)
|
2014-11-17 04:51:42 +00:00
|
|
|
env = add_user_token(env, *nt.second, mods.m_persistent);
|
|
|
|
env = add_notation(env, nt.first, mods.m_persistent);
|
2014-10-11 23:38:55 +00:00
|
|
|
return env;
|
|
|
|
}
|
2014-10-21 20:30:40 +00:00
|
|
|
|
|
|
|
static environment notation_cmd(parser & p) {
|
|
|
|
bool overload = !in_context(p.env());
|
|
|
|
bool reserve = false;
|
|
|
|
return notation_cmd_core(p, overload, reserve);
|
|
|
|
}
|
|
|
|
|
|
|
|
static environment infixl_cmd(parser & p) {
|
|
|
|
bool overload = !in_context(p.env());
|
|
|
|
bool reserve = false;
|
|
|
|
return mixfix_cmd(p, mixfix_kind::infixl, overload, reserve);
|
|
|
|
}
|
|
|
|
|
|
|
|
static environment infixr_cmd(parser & p) {
|
|
|
|
bool overload = !in_context(p.env());
|
|
|
|
bool reserve = false;
|
|
|
|
return mixfix_cmd(p, mixfix_kind::infixr, overload, reserve);
|
|
|
|
}
|
|
|
|
|
|
|
|
static environment postfix_cmd(parser & p) {
|
|
|
|
bool overload = !in_context(p.env());
|
|
|
|
bool reserve = false;
|
|
|
|
return mixfix_cmd(p, mixfix_kind::postfix, overload, reserve);
|
|
|
|
}
|
|
|
|
|
|
|
|
static environment prefix_cmd(parser & p) {
|
|
|
|
bool overload = !in_context(p.env());
|
|
|
|
bool reserve = false;
|
|
|
|
return mixfix_cmd(p, mixfix_kind::prefix, overload, reserve);
|
|
|
|
}
|
|
|
|
|
|
|
|
static environment reserve_cmd(parser & p) {
|
|
|
|
bool overload = false;
|
|
|
|
bool reserve = true;
|
2014-10-21 21:50:50 +00:00
|
|
|
if (p.curr_is_token(get_notation_tk())) {
|
2014-10-21 20:30:40 +00:00
|
|
|
p.next();
|
|
|
|
return notation_cmd_core(p, overload, reserve);
|
|
|
|
} else if (p.curr_is_token(get_infixl_tk())) {
|
|
|
|
p.next();
|
|
|
|
return mixfix_cmd(p, mixfix_kind::infixl, overload, reserve);
|
|
|
|
} else if (p.curr_is_token(get_infix_tk())) {
|
|
|
|
p.next();
|
|
|
|
return mixfix_cmd(p, mixfix_kind::infixl, overload, reserve);
|
|
|
|
} else if (p.curr_is_token(get_infixr_tk())) {
|
|
|
|
p.next();
|
|
|
|
return mixfix_cmd(p, mixfix_kind::infixr, overload, reserve);
|
|
|
|
} else if (p.curr_is_token(get_prefix_tk())) {
|
|
|
|
p.next();
|
|
|
|
return mixfix_cmd(p, mixfix_kind::prefix, overload, reserve);
|
|
|
|
} else if (p.curr_is_token(get_postfix_tk())) {
|
|
|
|
p.next();
|
|
|
|
return mixfix_cmd(p, mixfix_kind::postfix, overload, reserve);
|
|
|
|
} else {
|
|
|
|
throw parser_error("invalid reserve notation, 'infix', 'infixl', 'infixr', 'prefix', "
|
|
|
|
"'postfix' or 'notation' expected", p.pos());
|
|
|
|
}
|
|
|
|
}
|
2014-10-11 23:38:55 +00:00
|
|
|
|
|
|
|
static environment precedence_cmd(parser & p) {
|
|
|
|
std::string tk = parse_symbol(p, "invalid precedence declaration, quoted symbol or identifier expected");
|
|
|
|
p.check_token_next(get_colon_tk(), "invalid precedence declaration, ':' expected");
|
|
|
|
unsigned prec = parse_precedence(p, "invalid precedence declaration, numeral or 'max' expected");
|
2014-10-19 20:39:06 +00:00
|
|
|
return add_user_token(p.env(), tk.c_str(), prec);
|
2014-10-11 23:38:55 +00:00
|
|
|
}
|
2014-06-17 20:39:44 +00:00
|
|
|
|
|
|
|
void register_notation_cmds(cmd_table & r) {
|
|
|
|
add_cmd(r, cmd_info("precedence", "set token left binding power", precedence_cmd));
|
|
|
|
add_cmd(r, cmd_info("infixl", "declare a new infix (left) notation", infixl_cmd));
|
|
|
|
add_cmd(r, cmd_info("infix", "declare a new infix (left) notation", infixl_cmd));
|
|
|
|
add_cmd(r, cmd_info("infixr", "declare a new infix (right) notation", infixr_cmd));
|
|
|
|
add_cmd(r, cmd_info("postfix", "declare a new postfix notation", postfix_cmd));
|
2014-07-04 04:37:56 +00:00
|
|
|
add_cmd(r, cmd_info("prefix", "declare a new prefix notation", prefix_cmd));
|
2014-06-17 20:39:44 +00:00
|
|
|
add_cmd(r, cmd_info("notation", "declare a new notation", notation_cmd));
|
2014-10-21 20:30:40 +00:00
|
|
|
add_cmd(r, cmd_info("reserve", "reserve notation", reserve_cmd));
|
2014-06-17 20:39:44 +00:00
|
|
|
}
|
2014-06-15 05:13:25 +00:00
|
|
|
}
|