aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/complete/syntax/format.cpp
blob: 19c7c9d214843e64e1d9d0bbd97ef03cd9da8489 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "format.h"

#include "grammar.h"

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

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

namespace NSQLComplete {

    namespace {

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

        static const THashSet<TString> TypeConstructors = {
            "DECIMAL",
            "OPTIONAL",
            "TUPLE",
            "STRUCT",
            "VARIANT",
            "LIST",
            "STREAM",
            "FLOW",
            "DICT",
            "SET",
            "ENUM",
            "RESOURCE",
            "TAGGED",
            "CALLABLE",
        };

    } // namespace

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

        size_t i = 0;
        TString text = seq[i++];

        if (2 <= seq.size() &&
            TypeConstructors.contains(text) &&
            (seq[i] == "<" || seq[i] == "(")) {
            text.to_title();
            text.append(seq[i]);
            i += 1;
        }

        for (; 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