diff options
| author | vitya-smirnov <[email protected]> | 2025-10-07 09:34:39 +0300 |
|---|---|---|
| committer | vitya-smirnov <[email protected]> | 2025-10-07 09:52:14 +0300 |
| commit | babe7533f18c11be1f8a195ed2324d2d9a89436a (patch) | |
| tree | 45b7627141bf5a52b45a3d61fd1fbdd564bb8dd9 /yql/essentials/sql/v1/reflect/sql_reflect.cpp | |
| parent | 8fe7cfe254fde2772477a8933a163b5f303716b4 (diff) | |
YQL-20086 sql/v1
commit_hash:55bc611cdaa0d8a0fc3c4c7708ed9f17cc4976cf
Diffstat (limited to 'yql/essentials/sql/v1/reflect/sql_reflect.cpp')
| -rw-r--r-- | yql/essentials/sql/v1/reflect/sql_reflect.cpp | 286 |
1 files changed, 143 insertions, 143 deletions
diff --git a/yql/essentials/sql/v1/reflect/sql_reflect.cpp b/yql/essentials/sql/v1/reflect/sql_reflect.cpp index 97988d7bb6a..40a8eb70bc6 100644 --- a/yql/essentials/sql/v1/reflect/sql_reflect.cpp +++ b/yql/essentials/sql/v1/reflect/sql_reflect.cpp @@ -9,186 +9,186 @@ namespace NSQLReflect { - const TStringBuf ReflectPrefix = "//!"; - const TStringBuf SectionPrefix = "//! section:"; - const TStringBuf SectionPunctuation = "//! section:punctuation"; - const TStringBuf SectionLetter = "//! section:letter"; - const TStringBuf SectionKeyword = "//! section:keyword"; - const TStringBuf SectionOther = "//! section:other"; - const TStringBuf FragmentPrefix = "fragment "; - - const TStringBuf TLexerGrammar::KeywordBlockByName(const TStringBuf name Y_LIFETIME_BOUND) { - if (name == "TSKIP") { - return "SKIP"; - } - return name; +const TStringBuf ReflectPrefix = "//!"; +const TStringBuf SectionPrefix = "//! section:"; +const TStringBuf SectionPunctuation = "//! section:punctuation"; +const TStringBuf SectionLetter = "//! section:letter"; +const TStringBuf SectionKeyword = "//! section:keyword"; +const TStringBuf SectionOther = "//! section:other"; +const TStringBuf FragmentPrefix = "fragment "; + +const TStringBuf TLexerGrammar::KeywordBlockByName(const TStringBuf name Y_LIFETIME_BOUND) { + if (name == "TSKIP") { + return "SKIP"; } + return name; +} - const TString TLexerGrammar::KeywordNameByBlock(const TStringBuf block) { - if (TCaseInsensitiveStringBuf(block) == "SKIP") { - return "TSKIP"; - } - return ToUpperUTF8(block); +const TString TLexerGrammar::KeywordNameByBlock(const TStringBuf block) { + if (TCaseInsensitiveStringBuf(block) == "SKIP") { + return "TSKIP"; } - - TVector<TString> GetResourceLines(const TStringBuf key) { - TString text; - Y_ENSURE(NResource::FindExact(key, &text)); - - TVector<TString> lines; - Split(text, "\n", lines); - for (auto& line : lines) { - if (!line.empty() && line.back() == '\r') { - line.pop_back(); - } + return ToUpperUTF8(block); +} + +TVector<TString> GetResourceLines(const TStringBuf key) { + TString text; + Y_ENSURE(NResource::FindExact(key, &text)); + + TVector<TString> lines; + Split(text, "\n", lines); + for (auto& line : lines) { + if (!line.empty() && line.back() == '\r') { + line.pop_back(); } - return lines; } + return lines; +} - void Format(TVector<TString>& lines) { - for (size_t i = 0; i < lines.size(); ++i) { - auto& line = lines[i]; +void Format(TVector<TString>& lines) { + for (size_t i = 0; i < lines.size(); ++i) { + auto& line = lines[i]; - StripInPlace(line); - - if (line.StartsWith("//") || (line.Contains(':') && line.Contains(';'))) { - continue; - } + StripInPlace(line); - size_t j = i + 1; - do { - line += lines.at(j); - } while (!lines.at(j++).Contains(';')); - - auto first = std::next(std::begin(lines), i + 1); - auto last = std::next(std::begin(lines), j); - lines.erase(first, last); + if (line.StartsWith("//") || (line.Contains(':') && line.Contains(';'))) { + continue; } - for (auto& line : lines) { - CollapseInPlace(line); - SubstGlobal(line, " ;", ";"); - SubstGlobal(line, " :", ":"); - SubstGlobal(line, " )", ")"); - SubstGlobal(line, "( ", "("); - } - } + size_t j = i + 1; + do { + line += lines.at(j); + } while (!lines.at(j++).Contains(';')); - void Purify(TVector<TString>& lines) { - const auto [first, last] = std::ranges::remove_if(lines, [](const TString& line) { - return (line.StartsWith("//") && !line.StartsWith(ReflectPrefix)) || line.empty(); - }); + auto first = std::next(std::begin(lines), i + 1); + auto last = std::next(std::begin(lines), j); lines.erase(first, last); } - THashMap<TStringBuf, TVector<TString>> GroupBySection(TVector<TString>&& lines) { - TVector<TStringBuf> sections = { - "", - SectionPunctuation, - SectionLetter, - SectionKeyword, - SectionOther, - }; + for (auto& line : lines) { + CollapseInPlace(line); + SubstGlobal(line, " ;", ";"); + SubstGlobal(line, " :", ":"); + SubstGlobal(line, " )", ")"); + SubstGlobal(line, "( ", "("); + } +} + +void Purify(TVector<TString>& lines) { + const auto [first, last] = std::ranges::remove_if(lines, [](const TString& line) { + return (line.StartsWith("//") && !line.StartsWith(ReflectPrefix)) || line.empty(); + }); + lines.erase(first, last); +} + +THashMap<TStringBuf, TVector<TString>> GroupBySection(TVector<TString>&& lines) { + TVector<TStringBuf> sections = { + "", + SectionPunctuation, + SectionLetter, + SectionKeyword, + SectionOther, + }; + + size_t section = 0; + + THashMap<TStringBuf, TVector<TString>> groups; + for (auto& line : lines) { + if (line.StartsWith(SectionPrefix)) { + Y_ENSURE(sections.at(section + 1) == line); + section += 1; + continue; + } - size_t section = 0; + groups[sections.at(section)].emplace_back(std::move(line)); + } - THashMap<TStringBuf, TVector<TString>> groups; - for (auto& line : lines) { - if (line.StartsWith(SectionPrefix)) { - Y_ENSURE(sections.at(section + 1) == line); - section += 1; - continue; - } + groups.erase(""); + groups.erase(SectionLetter); - groups[sections.at(section)].emplace_back(std::move(line)); - } + return groups; +} - groups.erase(""); - groups.erase(SectionLetter); +std::tuple<TString, TString> ParseLexerRule(TString&& line) { + size_t colonPos = line.find(':'); + size_t semiPos = line.rfind(';'); - return groups; - } + Y_ENSURE( + colonPos != TString::npos && + semiPos != TString::npos && + colonPos < semiPos); - std::tuple<TString, TString> ParseLexerRule(TString&& line) { - size_t colonPos = line.find(':'); - size_t semiPos = line.rfind(';'); + TString block = line.substr(colonPos + 2, semiPos - colonPos - 2); + SubstGlobal(block, "\\\\", "\\"); - Y_ENSURE( - colonPos != TString::npos && - semiPos != TString::npos && - colonPos < semiPos); + TString name = std::move(line); + name.resize(colonPos); - TString block = line.substr(colonPos + 2, semiPos - colonPos - 2); - SubstGlobal(block, "\\\\", "\\"); + return std::make_tuple(std::move(name), std::move(block)); +} - TString name = std::move(line); - name.resize(colonPos); +void ParsePunctuationLine(TString&& line, TLexerGrammar& grammar) { + auto [name, block] = ParseLexerRule(std::move(line)); + block = block.erase(std::begin(block)); + block.pop_back(); - return std::make_tuple(std::move(name), std::move(block)); - } + SubstGlobal(block, "\\\'", "\'"); - void ParsePunctuationLine(TString&& line, TLexerGrammar& grammar) { - auto [name, block] = ParseLexerRule(std::move(line)); - block = block.erase(std::begin(block)); - block.pop_back(); + if (!name.StartsWith(FragmentPrefix)) { + grammar.PunctuationNames.emplace(name); + } - SubstGlobal(block, "\\\'", "\'"); + SubstGlobal(name, FragmentPrefix, ""); + grammar.BlockByName.emplace(std::move(name), std::move(block)); +} - if (!name.StartsWith(FragmentPrefix)) { - grammar.PunctuationNames.emplace(name); - } +void ParseKeywordLine(TString&& line, TLexerGrammar& grammar) { + auto [name, block] = ParseLexerRule(std::move(line)); + SubstGlobal(block, "'", ""); + SubstGlobal(block, " ", ""); - SubstGlobal(name, FragmentPrefix, ""); - grammar.BlockByName.emplace(std::move(name), std::move(block)); - } + Y_ENSURE(name == block || (name == "TSKIP" && block == TLexerGrammar::KeywordBlockByName("TSKIP"))); + grammar.KeywordNames.emplace(std::move(name)); +} - void ParseKeywordLine(TString&& line, TLexerGrammar& grammar) { - auto [name, block] = ParseLexerRule(std::move(line)); - SubstGlobal(block, "'", ""); - SubstGlobal(block, " ", ""); +void ParseOtherLine(TString&& line, TLexerGrammar& grammar) { + auto [name, block] = ParseLexerRule(std::move(line)); - Y_ENSURE(name == block || (name == "TSKIP" && block == TLexerGrammar::KeywordBlockByName("TSKIP"))); - grammar.KeywordNames.emplace(std::move(name)); + if (!name.StartsWith(FragmentPrefix)) { + grammar.OtherNames.emplace_back(name); } - void ParseOtherLine(TString&& line, TLexerGrammar& grammar) { - auto [name, block] = ParseLexerRule(std::move(line)); + SubstGlobal(name, FragmentPrefix, ""); + SubstGlobal(block, " -> channel(HIDDEN)", ""); + grammar.BlockByName.emplace(std::move(name), std::move(block)); +} - if (!name.StartsWith(FragmentPrefix)) { - grammar.OtherNames.emplace_back(name); - } +TLexerGrammar LoadLexerGrammar() { + TVector<TString> lines = GetResourceLines("SQLv1Antlr4.g.in"); + Purify(lines); + Format(lines); + Purify(lines); - SubstGlobal(name, FragmentPrefix, ""); - SubstGlobal(block, " -> channel(HIDDEN)", ""); - grammar.BlockByName.emplace(std::move(name), std::move(block)); - } + THashMap<TStringBuf, TVector<TString>> sections; + sections = GroupBySection(std::move(lines)); + + TLexerGrammar grammar; - TLexerGrammar LoadLexerGrammar() { - TVector<TString> lines = GetResourceLines("SQLv1Antlr4.g.in"); - Purify(lines); - Format(lines); - Purify(lines); - - THashMap<TStringBuf, TVector<TString>> sections; - sections = GroupBySection(std::move(lines)); - - TLexerGrammar grammar; - - for (auto& [section, lines] : sections) { - for (auto& line : lines) { - if (section == SectionPunctuation) { - ParsePunctuationLine(std::move(line), grammar); - } else if (section == SectionKeyword) { - ParseKeywordLine(std::move(line), grammar); - } else if (section == SectionOther) { - ParseOtherLine(std::move(line), grammar); - } else { - Y_ABORT("Unexpected section %s", section); - } + for (auto& [section, lines] : sections) { + for (auto& line : lines) { + if (section == SectionPunctuation) { + ParsePunctuationLine(std::move(line), grammar); + } else if (section == SectionKeyword) { + ParseKeywordLine(std::move(line), grammar); + } else if (section == SectionOther) { + ParseOtherLine(std::move(line), grammar); + } else { + Y_ABORT("Unexpected section %s", section); } } - - return grammar; } + return grammar; +} + } // namespace NSQLReflect |
