blob: 4274d4bfb44fad1001a79bf0b7f7711e6759ab9a (
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
|
#include "grammar.h"
#include <yql/essentials/sql/v1/format/sql_format.h>
namespace NSQLComplete {
class TSqlGrammar: public ISqlGrammar {
public:
TSqlGrammar()
: Vocabulary(GetVocabularyP())
, AllTokens(ComputeAllTokens())
, KeywordTokens(ComputeKeywordTokens())
{
}
const antlr4::dfa::Vocabulary& GetVocabulary() const override {
return *Vocabulary;
}
const std::unordered_set<TTokenId>& GetAllTokens() const override {
return AllTokens;
}
const std::unordered_set<TTokenId>& GetKeywordTokens() const override {
return KeywordTokens;
}
private:
static const antlr4::dfa::Vocabulary* GetVocabularyP() {
return &NALADefaultAntlr4::SQLv1Antlr4Parser(nullptr).getVocabulary();
}
std::unordered_set<TTokenId> ComputeAllTokens() {
const auto& vocabulary = GetVocabulary();
std::unordered_set<TTokenId> allTokens;
for (size_t type = 1; type <= vocabulary.getMaxTokenType(); ++type) {
allTokens.emplace(type);
}
return allTokens;
}
std::unordered_set<TTokenId> ComputeKeywordTokens() {
const auto& vocabulary = GetVocabulary();
const auto keywords = NSQLFormat::GetKeywords();
auto keywordTokens = GetAllTokens();
std::erase_if(keywordTokens, [&](TTokenId token) {
return !keywords.contains(vocabulary.getSymbolicName(token));
});
keywordTokens.erase(TOKEN_EOF);
return keywordTokens;
}
const antlr4::dfa::Vocabulary* Vocabulary;
const std::unordered_set<TTokenId> AllTokens;
const std::unordered_set<TTokenId> KeywordTokens;
};
const ISqlGrammar& GetSqlGrammar() {
const static TSqlGrammar DefaultSqlGrammar{};
return DefaultSqlGrammar;
}
} // namespace NSQLComplete
|