diff --git a/src/syntaxer.cpp b/src/syntaxer.cpp index 2cfd9ae..53e6a6d 100644 --- a/src/syntaxer.cpp +++ b/src/syntaxer.cpp @@ -29,6 +29,70 @@ static int rank_op(const Expression::Token::Operator o) } } +static int same_lvl_opa_location(const std::vector& 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& 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& 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& 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 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& 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& tv)