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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
#include "lexer.h"
#include "generic.h"
#include "regex.h"
#include <contrib/libs/re2/re2/re2.h>
#include <yql/essentials/core/issue/yql_issue.h>
#include <yql/essentials/sql/v1/reflect/sql_reflect.h>
#include <util/generic/algorithm.h>
#include <util/generic/string.h>
#include <util/generic/maybe.h>
#include <util/string/subst.h>
#include <util/string/ascii.h>
namespace NSQLTranslationV1 {
using NSQLReflect::TLexerGrammar;
using NSQLTranslation::TParsedToken;
using NSQLTranslation::TParsedTokenList;
size_t MatchANSIMultilineComment(TStringBuf remaining);
TTokenMatcher ANSICommentMatcher(TTokenMatcher defaultComment) {
return [defaultComment](TStringBuf prefix) -> TMaybe<TStringBuf> {
const auto basic = defaultComment(prefix);
if (basic.Empty()) {
return Nothing();
}
if (!prefix.StartsWith("/*")) {
return basic;
}
size_t ll1Length = MatchANSIMultilineComment(prefix);
TStringBuf ll1Content = prefix.SubString(0, ll1Length);
Y_ENSURE(ll1Content == 0 || basic <= ll1Content);
if (ll1Content == 0) {
return basic;
}
return ll1Content;
};
}
size_t MatchANSIMultilineComment(TStringBuf prefix) {
if (!prefix.StartsWith("/*")) {
return 0;
}
size_t skipped = 0;
prefix.Skip(2);
skipped += 2;
for (;;) {
if (prefix.StartsWith("*/")) {
prefix.Skip(2);
skipped += 2;
return skipped;
}
bool isSkipped = false;
if (prefix.StartsWith("/*")) {
size_t limit = prefix.rfind("*/");
if (limit == std::string::npos) {
return 0;
}
size_t len = MatchANSIMultilineComment(prefix.Head(limit));
prefix.Skip(len);
skipped += len;
isSkipped = len != 0;
}
if (isSkipped) {
continue;
}
if (prefix.size() == 0) {
return 0;
}
prefix.Skip(1);
skipped += 1;
}
}
TGenericLexerGrammar MakeGenericLexerGrammar(
bool ansi,
const TLexerGrammar& grammar,
const TVector<std::tuple<TString, TString>>& regexByOtherName) {
TGenericLexerGrammar generic;
for (const auto& name : grammar.KeywordNames) {
auto matcher = Compile({
.Body = TString(TLexerGrammar::KeywordBlock(name)),
.IsCaseInsensitive = true,
});
generic.emplace_back(name, std::move(matcher));
}
for (const auto& name : grammar.PunctuationNames) {
generic.emplace_back(
name, Compile({RE2::QuoteMeta(grammar.BlockByName.at(name))}));
}
for (const auto& [name, regex] : regexByOtherName) {
auto matcher = Compile({
.Body = regex,
});
generic.emplace_back(name, std::move(matcher));
}
if (ansi) {
auto it = FindIf(generic, [](const auto& m) {
return m.TokenName == "COMMENT";
});
Y_ENSURE(it != std::end(generic));
it->Match = ANSICommentMatcher(it->Match);
}
return generic;
}
class TRegexLexer: public NSQLTranslation::ILexer {
public:
TRegexLexer(IGenericLexer::TPtr lexer)
: Lexer_(std::move(lexer))
{
}
bool Tokenize(
const TString& query,
const TString& queryName,
const TTokenCallback& onNextToken,
NYql::TIssues& issues,
size_t maxErrors) override {
bool isFailed = false;
const auto onNext = [&](TGenericToken&& token) {
if (token.Name == TGenericToken::Error) {
NYql::TPosition pos(token.Begin, 0, queryName);
TString message = TString("no candidates, skipping ") + token.Content;
issues.AddIssue(std::move(pos), std::move(message));
isFailed = true;
return;
}
onNextToken({
.Name = TString(token.Name),
.Content = TString(token.Content),
});
};
Lexer_->Tokenize(query, onNext, maxErrors);
return !isFailed;
}
private:
IGenericLexer::TPtr Lexer_;
};
namespace {
class TFactory final: public NSQLTranslation::ILexerFactory {
public:
explicit TFactory(bool ansi) {
auto grammar = NSQLReflect::LoadLexerGrammar();
auto regexes = MakeRegexByOtherName(grammar, ansi);
Lexer_ = MakeGenericLexer(MakeGenericLexerGrammar(ansi, grammar, regexes));
}
NSQLTranslation::ILexer::TPtr MakeLexer() const override {
return NSQLTranslation::ILexer::TPtr(
new TRegexLexer(Lexer_));
}
private:
IGenericLexer::TPtr Lexer_;
};
} // namespace
NSQLTranslation::TLexerFactoryPtr MakeRegexLexerFactory(bool ansi) {
return NSQLTranslation::TLexerFactoryPtr(new TFactory(ansi));
}
} // namespace NSQLTranslationV1
|