2015-02-02 23:59:57 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include "library/tactic/location.h"
|
|
|
|
#include "frontends/lean/parser.h"
|
|
|
|
#include "frontends/lean/tokens.h"
|
|
|
|
#include "frontends/lean/parse_tactic_location.h"
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
static occurrence parse_occurrence(parser & p) {
|
2015-02-03 01:02:14 +00:00
|
|
|
if (p.curr_is_token(get_lcurly_tk())) {
|
2015-02-02 23:59:57 +00:00
|
|
|
p.next();
|
|
|
|
bool has_pos = false;
|
|
|
|
bool has_neg = false;
|
|
|
|
buffer<unsigned> occs;
|
|
|
|
while (true) {
|
|
|
|
if (p.curr_is_token(get_sub_tk())) {
|
|
|
|
if (has_pos)
|
|
|
|
throw parser_error("invalid tactic location, cannot mix positive and negative occurrences", p.pos());
|
|
|
|
has_neg = true;
|
|
|
|
p.next();
|
2015-02-04 22:04:56 +00:00
|
|
|
auto pos = p.pos();
|
|
|
|
unsigned i = p.parse_small_nat();
|
|
|
|
if (i == 0)
|
|
|
|
throw parser_error("invalid tactic location, first occurrence is 1", pos);
|
|
|
|
occs.push_back(i);
|
2015-02-02 23:59:57 +00:00
|
|
|
} else {
|
2015-02-04 22:04:56 +00:00
|
|
|
auto pos = p.pos();
|
|
|
|
unsigned i = p.parse_small_nat();
|
|
|
|
if (i == 0)
|
|
|
|
throw parser_error("invalid tactic location, first occurrence is 1", pos);
|
|
|
|
occs.push_back(i);
|
2015-02-02 23:59:57 +00:00
|
|
|
if (has_neg)
|
|
|
|
throw parser_error("invalid tactic location, cannot mix positive and negative occurrences", pos);
|
|
|
|
has_pos = true;
|
|
|
|
}
|
2015-02-03 01:02:14 +00:00
|
|
|
if (p.curr_is_token(get_rcurly_tk()))
|
2015-02-02 23:59:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-02-03 01:02:14 +00:00
|
|
|
p.next();
|
2015-02-02 23:59:57 +00:00
|
|
|
if (has_pos)
|
|
|
|
return occurrence::mk_occurrences(occs);
|
|
|
|
else
|
|
|
|
return occurrence::mk_except_occurrences(occs);
|
|
|
|
} else {
|
|
|
|
return occurrence();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
location parse_tactic_location(parser & p) {
|
2015-02-03 01:02:14 +00:00
|
|
|
if (p.curr_is_token(get_at_tk())) {
|
2015-02-02 23:59:57 +00:00
|
|
|
p.next();
|
|
|
|
if (p.curr_is_token(get_star_tk())) {
|
|
|
|
p.next();
|
|
|
|
if (p.curr_is_token(get_turnstile_tk())) {
|
|
|
|
p.next();
|
|
|
|
if (p.curr_is_token(get_star_tk())) {
|
2015-02-03 01:02:14 +00:00
|
|
|
// at * |- *
|
2015-02-02 23:59:57 +00:00
|
|
|
return location::mk_everywhere();
|
|
|
|
} else {
|
2015-02-03 01:02:14 +00:00
|
|
|
// at * |-
|
2015-02-02 23:59:57 +00:00
|
|
|
return location::mk_all_hypotheses();
|
|
|
|
}
|
|
|
|
} else {
|
2015-02-03 01:02:14 +00:00
|
|
|
// at *
|
2015-02-02 23:59:57 +00:00
|
|
|
return location::mk_everywhere();
|
|
|
|
}
|
2015-02-03 01:37:11 +00:00
|
|
|
} else if (p.curr_is_token(get_lparen_tk())) {
|
|
|
|
p.next();
|
2015-02-02 23:59:57 +00:00
|
|
|
buffer<name> hyps;
|
|
|
|
buffer<occurrence> hyp_occs;
|
2015-02-03 01:37:11 +00:00
|
|
|
while (true) {
|
2015-02-02 23:59:57 +00:00
|
|
|
hyps.push_back(p.get_name_val());
|
|
|
|
p.next();
|
|
|
|
hyp_occs.push_back(parse_occurrence(p));
|
2015-02-03 01:37:11 +00:00
|
|
|
if (!p.curr_is_token(get_comma_tk()))
|
|
|
|
break;
|
|
|
|
p.next();
|
2015-02-02 23:59:57 +00:00
|
|
|
}
|
2015-02-03 01:37:11 +00:00
|
|
|
p.check_token_next(get_rparen_tk(), "invalid tactic location, ')' expected");
|
|
|
|
if (p.curr_is_token(get_turnstile_tk())) {
|
2015-02-03 01:02:14 +00:00
|
|
|
p.next();
|
2015-02-02 23:59:57 +00:00
|
|
|
occurrence goal_occ = parse_occurrence(p);
|
|
|
|
return location::mk_at(goal_occ, hyps, hyp_occs);
|
|
|
|
} else {
|
|
|
|
return location::mk_hypotheses_at(hyps, hyp_occs);
|
|
|
|
}
|
2015-02-03 01:37:11 +00:00
|
|
|
} else if (p.curr_is_token(get_lcurly_tk())) {
|
|
|
|
occurrence o = parse_occurrence(p);
|
|
|
|
return location::mk_goal_at(o);
|
|
|
|
} else {
|
|
|
|
buffer<name> hyps;
|
|
|
|
buffer<occurrence> hyp_occs;
|
|
|
|
hyps.push_back(p.check_id_next("invalid tactic location, identifier expected"));
|
|
|
|
hyp_occs.push_back(parse_occurrence(p));
|
|
|
|
return location::mk_hypotheses_at(hyps, hyp_occs);
|
2015-02-02 23:59:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return location::mk_goal_only();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|