fix unchecked selection of brackets that caught and removed unrelated tokens

This commit is contained in:
2026-10-14 21:17:32 +03:00
parent ddcf261af7
commit f430b3616a
+73 -4
View File
@@ -29,6 +29,70 @@ static int rank_op(const Expression::Token::Operator o)
}
}
static int same_lvl_opa_location(const std::vector<Expression::Token>& tv, const int location)
{
for (int i = location - 1, lv = 0; i >= 0; i--)
{
const Expression::Token &t = tv[i];
if (t.t != Expression::Token::OPERATOR)
{
continue;
}
if (t.v.o == Expression::Token::CPA)
{
lv++;
}
if (t.v.o == Expression::Token::OPA)
{
if (!lv)
{
return i;
}
else
{
lv--;
}
}
}
return -1;
}
static int same_lvl_cpa_location(const std::vector<Expression::Token>& tv, const int location)
{
for (int i = location + 1, lv = 0, l = tv.size(); i < l; i++)
{
const Expression::Token &t = tv[i];
if (t.t != Expression::Token::OPERATOR)
{
continue;
}
if (t.v.o == Expression::Token::OPA)
{
lv++;
}
if (t.v.o == Expression::Token::CPA)
{
if (!lv)
{
return i;
}
else
{
lv--;
}
}
}
return tv.size() - 1;
}
static void highlight_issue(const std::vector<Expression::Token>& tv, const int location)
{
int start = location - 7 < 0 ? 0 : location - 7;
@@ -634,9 +698,14 @@ static Expression::Token::Operator get_prior_mop(const std::vector<Expression::T
static void open_brackets(std::vector<Expression::Token>& tv, const Range &r)
{
Range brackets(
same_lvl_opa_location(tv, get_mop_location(tv, r.start)),
same_lvl_cpa_location(tv, get_mop_location(tv, r.finish))
);
std::vector<int> hl;
hl.emplace_back(get_mop_location(tv, r.start) - 2);
hl.emplace_back(get_mop_location(tv, r.finish) + 2);
hl.emplace_back(brackets.start);
hl.emplace_back(brackets.finish);
Parser::print_math_vector(tv, hl);
if (Expression::Token::Operator po = get_prior_mop(tv, get_mop_location(tv, r.start)); po == Expression::Token::SUB || po == Expression::Token::DIV)
@@ -665,8 +734,8 @@ static void open_brackets(std::vector<Expression::Token>& tv, const Range &r)
}
}
tv.erase(tv.begin() + get_mop_location(tv, r.start) - 2);
tv.erase(tv.begin() + get_mop_location(tv, r.finish) + 2);
tv.erase(tv.begin() + brackets.finish);
tv.erase(tv.begin() + brackets.start);
}
static void remove_extra_brackets(std::vector<Expression::Token>& tv)