summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/ide/completion/syntax/format.cpp
blob: cdaa66efff3877b8fcc8a8f7186760541c7df06c (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "format.h"

#include "grammar.h"

#include <yql/essentials/sql/v1/ide/completion/antlr4/vocabulary.h>

#include <util/generic/hash_set.h>
#include <util/charset/utf8.h>

namespace NSQLComplete {

namespace {

const THashSet<std::string> Keywords = [] {
    const auto& grammar = GetSqlGrammar();
    const auto& vocabulary = grammar.GetVocabulary();

    THashSet<std::string> keywords;
    for (auto& token : grammar.GetKeywordTokens()) {
        keywords.emplace(Display(vocabulary, token));
    }
    return keywords;
}();

} // namespace

TString FormatKeywords(const TVector<TString>& seq) {
    if (seq.empty()) {
        return "";
    }

    TString text = seq[0];
    for (size_t i = 1; i < seq.size(); ++i) {
        const auto& token = seq[i];
        if (Keywords.contains(token)) {
            text += " ";
        }
        text += token;
    }
    return text;
}

bool IsPlain(TStringBuf content) {
    return GetSqlGrammar().IsPlainIdentifier(content);
}

bool IsQuoted(TStringBuf content) {
    return 2 <= content.size() && content.front() == '`' && content.back() == '`';
}

TString Quoted(TString content) {
    content.prepend('`');
    content.append('`');
    return content;
}

TStringBuf Unquoted(TStringBuf content) {
    Y_ENSURE(IsQuoted(content));
    return content.SubStr(1, content.size() - 2);
}

bool IsBinding(TStringBuf content) {
    return !content.empty() && content.front() == '$';
}

TStringBuf Unbinded(TStringBuf content) {
    Y_ENSURE(IsBinding(content));
    return content.SubStr(1);
}

} // namespace NSQLComplete