aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/complete/syntax/format.cpp
blob: 43c36aea9dd5e060a39b1517219506d671819b0a (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
#include "format.h"

#include "grammar.h"

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

#include <util/generic/hash_set.h>

namespace NSQLComplete {

    TString FormatKeywords(const TVector<TString>& seq) {
        static 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;
        }();

        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;
    }

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

    TString Unquoted(TString content) {
        Y_ENSURE(2 <= content.size() && content.front() == '`' && content.back() == '`');
        content.erase(0, 1);
        content.pop_back();
        return content;
    }

} // namespace NSQLComplete