aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/tools/yql_highlight/generate_vim.cpp
blob: c93442cf5f4c3eb9eaebb34c91d327aaa73afcc4 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "generate_vim.h"

#include <yql/essentials/utils/yql_panic.h>

#include <contrib/libs/re2/re2/re2.h>

#include <util/string/builder.h>

#include <ranges>

namespace NSQLHighlight {

    namespace {

        TString ToVim(TString regex) {
            static RE2 LikelyUnquotedLParen(R"((^|[^\\])(\())");
            static RE2 LikelyNonGreedyMatch(R"re((^|[^\\])(\*\?))re");

            // We can leave some capturing groups in case `\\\\(`,
            // but it is okay as the goal is to meet the Vim limit.

            YQL_ENSURE(!regex.Contains(R"(\\*?)"), "" << regex);

            RE2::GlobalReplace(&regex, LikelyUnquotedLParen, R"(\1%()");
            RE2::GlobalReplace(&regex, LikelyNonGreedyMatch, R"re(\1{-})re");

            return regex;
        }

        TString ToVim(const TUnit& unit, const NSQLTranslationV1::TRegexPattern& pattern) {
            TStringBuilder vim;

            vim << R"(")";
            vim << R"(\v)";

            if (unit.IsPlain) {
                vim << R"(<)";
            }

            if (pattern.IsCaseInsensitive) {
                vim << R"(\c)";
            }

            vim << "(" << ToVim(pattern.Body) << ")";

            if (!pattern.After.empty()) {
                vim << "(" << ToVim(pattern.After) << ")@=";
            }

            if (unit.IsPlain) {
                vim << R"(>)";
            }

            vim << R"(")";

            return vim;
        }

        TString ToVimName(EUnitKind kind) {
            switch (kind) {
                case EUnitKind::Keyword:
                    return "yqlKeyword";
                case EUnitKind::Punctuation:
                    return "yqlPunctuation";
                case EUnitKind::QuotedIdentifier:
                    return "yqlQuotedIdentifier";
                case EUnitKind::BindParameterIdentifier:
                    return "yqlBindParameterIdentifier";
                case EUnitKind::TypeIdentifier:
                    return "yqlTypeIdentifier";
                case EUnitKind::FunctionIdentifier:
                    return "yqlFunctionIdentifier";
                case EUnitKind::Identifier:
                    return "yqlIdentifier";
                case EUnitKind::Literal:
                    return "yqlLiteral";
                case EUnitKind::StringLiteral:
                    return "yqlStringLiteral";
                case EUnitKind::Comment:
                    return "yqlComment";
                case EUnitKind::Whitespace:
                    return "yqlWhitespace";
                case EUnitKind::Error:
                    return "yqlError";
            }
        }

        void PrintRules(IOutputStream& out, const TUnit& unit) {
            TString name = ToVimName(unit.Kind);
            for (const auto& pattern : std::ranges::reverse_view(unit.Patterns)) {
                out << "syn match " << ToVimName(unit.Kind) << " "
                    << ToVim(unit, pattern) << '\n';
            }
            if (auto range = unit.RangePattern) {
                out << "syntax region " << name << "Multiline" << " "
                    << "start=\"" << range->Begin << "\" "
                    << "end=\"" << range->End << "\""
                    << '\n';
            }
        }

        TVector<TStringBuf> ToVimGroups(EUnitKind kind) {
            switch (kind) {
                case EUnitKind::Keyword:
                    return {"Keyword"};
                case EUnitKind::Punctuation:
                    return {"Operator"};
                case EUnitKind::QuotedIdentifier:
                    return {"Special", "Underlined"};
                case EUnitKind::BindParameterIdentifier:
                    return {"Identifier"};
                case EUnitKind::TypeIdentifier:
                    return {"Type"};
                case EUnitKind::FunctionIdentifier:
                    return {"Function"};
                case EUnitKind::Identifier:
                    return {"Identifier"};
                case EUnitKind::Literal:
                    return {"Number"};
                case EUnitKind::StringLiteral:
                    return {"String"};
                case EUnitKind::Comment:
                    return {"Comment"};
                case EUnitKind::Whitespace:
                    return {};
                case EUnitKind::Error:
                    return {};
            }
        }

    } // namespace

    void GenerateVim(IOutputStream& out, const THighlighting& highlighting) {
        out << "if exists(\"b:current_syntax\")" << '\n';
        out << "  finish" << '\n';
        out << "endif" << '\n';
        out << '\n';

        for (const TUnit& unit : std::ranges::reverse_view(highlighting.Units)) {
            if (unit.IsCodeGenExcluded) {
                continue;
            }

            PrintRules(out, unit);
        }

        out << '\n';

        for (const TUnit& unit : std::ranges::reverse_view(highlighting.Units)) {
            TString name = ToVimName(unit.Kind);
            for (TStringBuf group : ToVimGroups(unit.Kind)) {
                out << "highlight default link " << name << "Multiline" << " " << group << '\n';
                out << "highlight default link " << name << " " << group << '\n';
            }
        }

        out << '\n';

        out << "let b:current_syntax = \"yql\"" << '\n';
        out.Flush();
    }

} // namespace NSQLHighlight