summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/ide/pure_ast/narrowing_visitor.cpp
blob: fade622b9862b1640a6a9f70927b6fe262322e6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "narrowing_visitor.h"

namespace NSQLPureAST {

TSQLv1NarrowingVisitor::TSQLv1NarrowingVisitor(antlr4::TokenStream* tokens, size_t cursorPosition)
    : Tokens_(tokens)
    , CursorPosition_(cursorPosition)
{
}

bool TSQLv1NarrowingVisitor::shouldVisitNextChild(antlr4::tree::ParseTree* node, const std::any& /*currentResult*/) {
    return TextInterval(node).a < static_cast<ssize_t>(CursorPosition_);
}

bool TSQLv1NarrowingVisitor::IsEnclosing(antlr4::tree::ParseTree* tree) const {
    return TextInterval(tree).properlyContains(CursorInterval());
}

ssize_t TSQLv1NarrowingVisitor::CursorPosition() const {
    return CursorPosition_;
}

antlr4::misc::Interval TSQLv1NarrowingVisitor::TextInterval(antlr4::tree::ParseTree* tree) const {
    auto tokens = tree->getSourceInterval();
    if (tokens.b == -1) {
        tokens.b = tokens.a;
    }
    return antlr4::misc::Interval(
        Tokens_->get(tokens.a)->getStartIndex(),
        Tokens_->get(tokens.b)->getStopIndex());
}

antlr4::misc::Interval TSQLv1NarrowingVisitor::CursorInterval() const {
    auto cursor = CursorPosition();
    return antlr4::misc::Interval(cursor, cursor);
}

} // namespace NSQLPureAST