sql_format.cpp 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247
  1. #include "sql_format.h"
  2. #include <yql/essentials/parser/lexer_common/lexer.h>
  3. #include <yql/essentials/core/sql_types/simple_types.h>
  4. #include <yql/essentials/sql/v1/lexer/lexer.h>
  5. #include <yql/essentials/sql/v1/proto_parser/proto_parser.h>
  6. #include <yql/essentials/parser/proto_ast/gen/v1_proto_split/SQLv1Parser.pb.main.h>
  7. #include <library/cpp/protobuf/util/simple_reflection.h>
  8. #include <library/cpp/resource/resource.h>
  9. #include <util/string/builder.h>
  10. #include <util/string/split.h>
  11. #include <util/string/strip.h>
  12. #include <util/string/subst.h>
  13. #include <util/generic/hash_set.h>
  14. namespace NSQLFormat {
  15. namespace {
  16. using namespace NSQLv1Generated;
  17. using NSQLTranslation::TParsedToken;
  18. using NSQLTranslation::TParsedTokenList;
  19. using NSQLTranslationV1::IsProbablyKeyword;
  20. using TTokenIterator = TParsedTokenList::const_iterator;
  21. TTokenIterator SkipWS(TTokenIterator curr, TTokenIterator end) {
  22. while (curr != end && curr->Name == "WS") {
  23. ++curr;
  24. }
  25. return curr;
  26. }
  27. TTokenIterator SkipWSOrComment(TTokenIterator curr, TTokenIterator end) {
  28. while (curr != end && (curr->Name == "WS" || curr->Name == "COMMENT")) {
  29. ++curr;
  30. }
  31. return curr;
  32. }
  33. TTokenIterator SkipWSOrCommentBackward(TTokenIterator curr, TTokenIterator begin) {
  34. while (curr != begin && (curr->Name == "WS" || curr->Name == "COMMENT")) {
  35. --curr;
  36. }
  37. return curr;
  38. }
  39. void SkipForValidate(
  40. TTokenIterator& in,
  41. TTokenIterator& out,
  42. const TParsedTokenList& query,
  43. const TParsedTokenList& formattedQuery,
  44. i32& parenthesesBalance
  45. ) {
  46. in = SkipWS(in, query.end());
  47. out = SkipWS(out, formattedQuery.end());
  48. auto inSkippedComments = SkipWSOrComment(in, query.end());
  49. auto skipAddedToken = [&](const TString& addedToken, const TVector<TString>& beforeTokens, const TVector<TString>& afterTokens) {
  50. if (
  51. out != formattedQuery.end() && out->Name == addedToken &&
  52. (in == query.end() || in->Name != addedToken) &&
  53. (beforeTokens.empty() || (inSkippedComments != query.end() && IsIn(beforeTokens, inSkippedComments->Name))) &&
  54. (afterTokens.empty() || (in != query.begin() && IsIn(afterTokens, SkipWSOrCommentBackward(in - 1, query.begin())->Name)))
  55. ) {
  56. out = SkipWS(++out, formattedQuery.end());
  57. if (addedToken == "LPAREN") {
  58. ++parenthesesBalance;
  59. } else if (addedToken == "RPAREN") {
  60. --parenthesesBalance;
  61. }
  62. }
  63. };
  64. skipAddedToken("LPAREN", {}, {"EQUALS"});
  65. skipAddedToken("RPAREN", {"END", "EOF", "SEMICOLON"}, {});
  66. skipAddedToken("SEMICOLON", {"END", "RBRACE_CURLY"}, {});
  67. auto skipDeletedToken = [&](const TString& deletedToken, const TVector<TString>& afterTokens) {
  68. if (
  69. in != query.end() && in->Name == deletedToken &&
  70. (out == formattedQuery.end() || out->Name != deletedToken) &&
  71. in != query.begin() && IsIn(afterTokens, SkipWSOrCommentBackward(in - 1, query.begin())->Name)
  72. ) {
  73. in = SkipWS(++in, query.end());
  74. return true;
  75. }
  76. return false;
  77. };
  78. while (skipDeletedToken("SEMICOLON", {"AS", "BEGIN", "LBRACE_CURLY", "SEMICOLON"})) {}
  79. }
  80. TParsedToken TransformTokenForValidate(TParsedToken token) {
  81. if (token.Name == "EQUALS2") {
  82. token.Name = "EQUALS";
  83. token.Content = "=";
  84. } else if (token.Name == "NOT_EQUALS2") {
  85. token.Name = "NOT_EQUALS";
  86. token.Content = "!=";
  87. }
  88. return token;
  89. }
  90. TStringBuf SkipQuotes(const TString& content) {
  91. TStringBuf str = content;
  92. str.SkipPrefix("\"");
  93. str.ChopSuffix("\"");
  94. str.SkipPrefix("'");
  95. str.ChopSuffix("'");
  96. return str;
  97. }
  98. TStringBuf SkipNewline(const TString& content) {
  99. TStringBuf str = content;
  100. str.ChopSuffix("\n");
  101. return str;
  102. }
  103. bool Validate(const TParsedTokenList& query, const TParsedTokenList& formattedQuery) {
  104. auto in = query.begin();
  105. auto out = formattedQuery.begin();
  106. auto inEnd = query.end();
  107. auto outEnd = formattedQuery.end();
  108. i32 parenthesesBalance = 0;
  109. while (in != inEnd && out != outEnd) {
  110. SkipForValidate(in, out, query, formattedQuery, parenthesesBalance);
  111. if (in != inEnd && out != outEnd) {
  112. auto inToken = TransformTokenForValidate(*in);
  113. auto outToken = TransformTokenForValidate(*out);
  114. if (inToken.Name != outToken.Name) {
  115. return false;
  116. }
  117. if (IsProbablyKeyword(inToken)) {
  118. if (!AsciiEqualsIgnoreCase(inToken.Content, outToken.Content)) {
  119. return false;
  120. }
  121. } else if (inToken.Name == "STRING_VALUE") {
  122. if (SkipQuotes(inToken.Content) != SkipQuotes(outToken.Content)) {
  123. return false;
  124. }
  125. } else if (inToken.Name == "COMMENT") {
  126. if (SkipNewline(inToken.Content) != SkipNewline(outToken.Content)) {
  127. return false;
  128. }
  129. } else {
  130. if (inToken.Content != outToken.Content) {
  131. return false;
  132. }
  133. }
  134. ++in;
  135. ++out;
  136. }
  137. }
  138. SkipForValidate(in, out, query, formattedQuery, parenthesesBalance);
  139. return in == inEnd && out == outEnd && parenthesesBalance == 0;
  140. }
  141. enum class EScope {
  142. Default,
  143. TypeName,
  144. Identifier,
  145. DoubleQuestion
  146. };
  147. class TPrettyVisitor;
  148. using TPrettyFunctor = std::function<void(TPrettyVisitor&, const NProtoBuf::Message& msg)>;
  149. class TObfuscatingVisitor;
  150. using TObfuscatingFunctor = std::function<void(TObfuscatingVisitor&, const NProtoBuf::Message& msg)>;
  151. struct TStaticData {
  152. TStaticData();
  153. static const TStaticData& GetInstance() {
  154. return *Singleton<TStaticData>();
  155. }
  156. THashSet<TString> Keywords;
  157. THashMap<const NProtoBuf::Descriptor*, EScope> ScopeDispatch;
  158. THashMap<const NProtoBuf::Descriptor*, TPrettyFunctor> PrettyVisitDispatch;
  159. THashMap<const NProtoBuf::Descriptor*, TObfuscatingFunctor> ObfuscatingVisitDispatch;
  160. };
  161. template <typename T, void (T::*Func)(const NProtoBuf::Message&)>
  162. void VisitAllFieldsImpl(T* obj, const NProtoBuf::Descriptor* descr, const NProtoBuf::Message& msg) {
  163. for (int i = 0; i < descr->field_count(); ++i) {
  164. const NProtoBuf::FieldDescriptor* fd = descr->field(i);
  165. NProtoBuf::TConstField field(msg, fd);
  166. if (field.IsMessage()) {
  167. for (size_t j = 0; j < field.Size(); ++j) {
  168. (obj->*Func)(*field.template Get<NProtoBuf::Message>(j));
  169. }
  170. }
  171. }
  172. }
  173. class TObfuscatingVisitor {
  174. friend struct TStaticData;
  175. public:
  176. TObfuscatingVisitor()
  177. : StaticData(TStaticData::GetInstance())
  178. {}
  179. TString Process(const NProtoBuf::Message& msg) {
  180. Scopes.push_back(EScope::Default);
  181. Visit(msg);
  182. return SB;
  183. }
  184. private:
  185. void VisitToken(const TToken& token) {
  186. auto str = token.GetValue();
  187. if (str == "<EOF>") {
  188. return;
  189. }
  190. if (!First) {
  191. SB << ' ';
  192. } else {
  193. First = false;
  194. }
  195. if (str == "$" && FuncCall) {
  196. FuncCall = false;
  197. }
  198. if (Scopes.back() == EScope::Identifier && !FuncCall) {
  199. if (str != "$" && !NYql::LookupSimpleTypeBySqlAlias(str, true)) {
  200. SB << "id";
  201. } else {
  202. SB << str;
  203. }
  204. } else if (NextToken) {
  205. SB << *NextToken;
  206. NextToken = Nothing();
  207. } else {
  208. SB << str;
  209. }
  210. }
  211. void VisitPragmaValue(const TRule_pragma_value& msg) {
  212. switch (msg.Alt_case()) {
  213. case TRule_pragma_value::kAltPragmaValue1: {
  214. NextToken = "0";
  215. break;
  216. }
  217. case TRule_pragma_value::kAltPragmaValue3: {
  218. NextToken = "'str'";
  219. break;
  220. }
  221. case TRule_pragma_value::kAltPragmaValue4: {
  222. NextToken = "false";
  223. break;
  224. }
  225. default:;
  226. }
  227. VisitAllFields(TRule_pragma_value::GetDescriptor(), msg);
  228. }
  229. void VisitLiteralValue(const TRule_literal_value& msg) {
  230. switch (msg.Alt_case()) {
  231. case TRule_literal_value::kAltLiteralValue1: {
  232. NextToken = "0";
  233. break;
  234. }
  235. case TRule_literal_value::kAltLiteralValue2: {
  236. NextToken = "0.0";
  237. break;
  238. }
  239. case TRule_literal_value::kAltLiteralValue3: {
  240. NextToken = "'str'";
  241. break;
  242. }
  243. case TRule_literal_value::kAltLiteralValue9: {
  244. NextToken = "false";
  245. break;
  246. }
  247. default:;
  248. }
  249. VisitAllFields(TRule_literal_value::GetDescriptor(), msg);
  250. }
  251. void VisitAtomExpr(const TRule_atom_expr& msg) {
  252. switch (msg.Alt_case()) {
  253. case TRule_atom_expr::kAltAtomExpr7: {
  254. FuncCall = true;
  255. break;
  256. }
  257. default:;
  258. }
  259. VisitAllFields(TRule_atom_expr::GetDescriptor(), msg);
  260. FuncCall = false;
  261. }
  262. void VisitInAtomExpr(const TRule_in_atom_expr& msg) {
  263. switch (msg.Alt_case()) {
  264. case TRule_in_atom_expr::kAltInAtomExpr6: {
  265. FuncCall = true;
  266. break;
  267. }
  268. default:;
  269. }
  270. VisitAllFields(TRule_in_atom_expr::GetDescriptor(), msg);
  271. FuncCall = false;
  272. }
  273. void VisitUnaryCasualSubexpr(const TRule_unary_casual_subexpr& msg) {
  274. bool invoke = false;
  275. for (auto& b : msg.GetRule_unary_subexpr_suffix2().GetBlock1()) {
  276. switch (b.GetBlock1().Alt_case()) {
  277. case TRule_unary_subexpr_suffix::TBlock1::TBlock1::kAlt2: {
  278. invoke = true;
  279. break;
  280. }
  281. default:;
  282. }
  283. break;
  284. }
  285. if (invoke) {
  286. FuncCall = true;
  287. }
  288. Visit(msg.GetBlock1());
  289. if (invoke) {
  290. FuncCall = false;
  291. }
  292. Visit(msg.GetRule_unary_subexpr_suffix2());
  293. }
  294. void VisitInUnaryCasualSubexpr(const TRule_in_unary_casual_subexpr& msg) {
  295. bool invoke = false;
  296. for (auto& b : msg.GetRule_unary_subexpr_suffix2().GetBlock1()) {
  297. switch (b.GetBlock1().Alt_case()) {
  298. case TRule_unary_subexpr_suffix::TBlock1::TBlock1::kAlt2: {
  299. invoke = true;
  300. break;
  301. }
  302. default:;
  303. }
  304. break;
  305. }
  306. if (invoke) {
  307. FuncCall = true;
  308. }
  309. Visit(msg.GetBlock1());
  310. if (invoke) {
  311. FuncCall = false;
  312. }
  313. Visit(msg.GetRule_unary_subexpr_suffix2());
  314. }
  315. void Visit(const NProtoBuf::Message& msg) {
  316. const NProtoBuf::Descriptor* descr = msg.GetDescriptor();
  317. auto scopePtr = StaticData.ScopeDispatch.FindPtr(descr);
  318. if (scopePtr) {
  319. Scopes.push_back(*scopePtr);
  320. }
  321. auto funcPtr = StaticData.ObfuscatingVisitDispatch.FindPtr(descr);
  322. if (funcPtr) {
  323. (*funcPtr)(*this, msg);
  324. } else {
  325. VisitAllFields(descr, msg);
  326. }
  327. if (scopePtr) {
  328. Scopes.pop_back();
  329. }
  330. }
  331. void VisitAllFields(const NProtoBuf::Descriptor* descr, const NProtoBuf::Message& msg) {
  332. VisitAllFieldsImpl<TObfuscatingVisitor, &TObfuscatingVisitor::Visit>(this, descr, msg);
  333. }
  334. const TStaticData& StaticData;
  335. TStringBuilder SB;
  336. bool First = true;
  337. TMaybe<TString> NextToken;
  338. TVector<EScope> Scopes;
  339. bool FuncCall = false;
  340. };
  341. class TPrettyVisitor {
  342. friend struct TStaticData;
  343. public:
  344. TPrettyVisitor(const TParsedTokenList& parsedTokens, const TParsedTokenList& comments, bool ansiLexer)
  345. : StaticData(TStaticData::GetInstance())
  346. , ParsedTokens(parsedTokens)
  347. , Comments(comments)
  348. , AnsiLexer(ansiLexer)
  349. {
  350. }
  351. TString Process(const NProtoBuf::Message& msg, bool& addLineBefore, bool& addLineAfter, TMaybe<ui32>& stmtCoreAltCase) {
  352. Scopes.push_back(EScope::Default);
  353. MarkedTokens.reserve(ParsedTokens.size());
  354. MarkTokens(msg);
  355. Y_ENSURE(MarkTokenStack.empty());
  356. Y_ENSURE(TokenIndex == ParsedTokens.size());
  357. TokenIndex = 0;
  358. Visit(msg);
  359. Y_ENSURE(TokenIndex == ParsedTokens.size());
  360. Y_ENSURE(MarkTokenStack.empty());
  361. for (; LastComment < Comments.size(); ++LastComment) {
  362. const auto text = Comments[LastComment].Content;
  363. AddComment(text);
  364. }
  365. ui32 lines = OutLine - (OutColumn == 0 ? 1 : 0);
  366. addLineBefore = AddLine.GetOrElse(true) || lines > 1;
  367. addLineAfter = AddLine.GetOrElse(true) || lines - CommentLines > 1;
  368. stmtCoreAltCase = StmtCoreAltCase;
  369. return SB;
  370. }
  371. private:
  372. struct TTokenInfo {
  373. bool OpeningBracket = false;
  374. bool ClosingBracket = false;
  375. bool BracketForcedExpansion = false;
  376. ui32 ClosingBracketIndex = 0;
  377. };
  378. using TMarkTokenStack = TVector<ui32>;
  379. void Out(TStringBuf s) {
  380. for (ui32 i = 0; i < s.size(); ++i) {
  381. Out(s[i], i == 0);
  382. }
  383. }
  384. void Out(char c, bool useIndent = true) {
  385. if (c == '\n' || c == '\r') {
  386. SB << c;
  387. if (!(c == '\n' && !SB.empty() && SB.back() == '\r')) {
  388. // do not increase OutLine if \n is preceded by \r
  389. // this way we handle \r, \n, or \r\n as single new line
  390. ++OutLine;
  391. }
  392. OutColumn = 0;
  393. } else {
  394. if (!OutColumn && useIndent) {
  395. ui32 indent = (CurrentIndent >= 0) ? CurrentIndent : 0;
  396. for (ui32 i = 0; i < indent; ++i) {
  397. SB << ' ';
  398. }
  399. }
  400. SB << c;
  401. ++OutColumn;
  402. }
  403. }
  404. void NewLine() {
  405. if (TokenIndex >= ParsedTokens.size() || ParsedTokens[TokenIndex].Line > LastLine) {
  406. WriteComments(true);
  407. }
  408. if (OutColumn) {
  409. Out('\n');
  410. }
  411. }
  412. void AddComment(TStringBuf text) {
  413. if (!AfterComment && OutLine > BlockFirstLine && OutColumn == 0) {
  414. Out('\n');
  415. }
  416. AfterComment = true;
  417. if (!SB.empty() && SB.back() != ' ' && SB.back() != '\n') {
  418. Out(' ');
  419. }
  420. if (OutColumn == 0) {
  421. ++CommentLines;
  422. }
  423. if (!text.StartsWith("--")) {
  424. CommentLines += CountIf(text, [](auto c) { return c == '\n'; });
  425. }
  426. Out(text);
  427. if (text.StartsWith("--") && !text.EndsWith("\n")) {
  428. Out('\n');
  429. }
  430. if (!text.StartsWith("--") &&
  431. TokenIndex < ParsedTokens.size() &&
  432. Comments[LastComment].Line < ParsedTokens[TokenIndex].Line &&
  433. (LastComment + 1 >= Comments.size() || Comments[LastComment].Line < Comments[LastComment + 1].Line)
  434. ) {
  435. Out('\n');
  436. }
  437. }
  438. void MarkTokens(const NProtoBuf::Message& msg) {
  439. const NProtoBuf::Descriptor* descr = msg.GetDescriptor();
  440. auto scopePtr = StaticData.ScopeDispatch.FindPtr(descr);
  441. if (scopePtr) {
  442. if (*scopePtr == EScope::TypeName) {
  443. ++InsideType;
  444. }
  445. Scopes.push_back(*scopePtr);
  446. }
  447. bool suppressExpr = false;
  448. if (descr == TToken::GetDescriptor()) {
  449. const auto& token = dynamic_cast<const TToken&>(msg);
  450. MarkToken(token);
  451. } else if (descr == TRule_sql_stmt_core::GetDescriptor()) {
  452. if (AddLine.Empty()) {
  453. const auto& rule = dynamic_cast<const TRule_sql_stmt_core&>(msg);
  454. AddLine = !IsSimpleStatement(rule).GetOrElse(false);
  455. StmtCoreAltCase = rule.Alt_case();
  456. }
  457. } else if (descr == TRule_lambda_body::GetDescriptor()) {
  458. Y_ENSURE(TokenIndex >= 1);
  459. auto prevIndex = TokenIndex - 1;
  460. Y_ENSURE(prevIndex < ParsedTokens.size());
  461. Y_ENSURE(ParsedTokens[prevIndex].Content == "{");
  462. MarkedTokens[prevIndex].OpeningBracket = false;
  463. ForceExpandedColumn = ParsedTokens[prevIndex].LinePos;
  464. ForceExpandedLine = ParsedTokens[prevIndex].Line;
  465. } else if (descr == TRule_in_atom_expr::GetDescriptor()) {
  466. const auto& value = dynamic_cast<const TRule_in_atom_expr&>(msg);
  467. if (value.Alt_case() == TRule_in_atom_expr::kAltInAtomExpr7) {
  468. suppressExpr = true;
  469. }
  470. } else if (descr == TRule_select_kind_parenthesis::GetDescriptor()) {
  471. const auto& value = dynamic_cast<const TRule_select_kind_parenthesis&>(msg);
  472. if (value.Alt_case() == TRule_select_kind_parenthesis::kAltSelectKindParenthesis2) {
  473. suppressExpr = true;
  474. }
  475. } else if (descr == TRule_window_specification::GetDescriptor()) {
  476. const auto& value = dynamic_cast<const TRule_window_specification&>(msg);
  477. const auto& details = value.GetRule_window_specification_details2();
  478. const bool needsNewline = details.HasBlock1() || details.HasBlock2() ||
  479. details.HasBlock3() || details.HasBlock4();
  480. if (needsNewline) {
  481. auto& paren = value.GetToken1();
  482. ForceExpandedColumn = paren.GetColumn();
  483. ForceExpandedLine = paren.GetLine();
  484. }
  485. suppressExpr = true;
  486. } else if (descr == TRule_exists_expr::GetDescriptor()) {
  487. const auto& value = dynamic_cast<const TRule_exists_expr&>(msg);
  488. auto& paren = value.GetToken2();
  489. ForceExpandedColumn = paren.GetColumn();
  490. ForceExpandedLine = paren.GetLine();
  491. suppressExpr = true;
  492. } else if (descr == TRule_case_expr::GetDescriptor()) {
  493. const auto& value = dynamic_cast<const TRule_case_expr&>(msg);
  494. auto& token = value.GetToken1();
  495. ForceExpandedColumn = token.GetColumn();
  496. ForceExpandedLine = token.GetLine();
  497. }
  498. const bool expr = (descr == TRule_expr::GetDescriptor() || descr == TRule_in_expr::GetDescriptor() || descr == TRule_type_name_composite::GetDescriptor());
  499. if (expr) {
  500. ++InsideExpr;
  501. }
  502. ui64 prevInsideExpr = InsideExpr;
  503. if (suppressExpr) {
  504. InsideExpr = 0;
  505. }
  506. VisitAllFieldsImpl<TPrettyVisitor, &TPrettyVisitor::MarkTokens>(this, descr, msg);
  507. if (suppressExpr) {
  508. InsideExpr = prevInsideExpr;
  509. }
  510. if (scopePtr) {
  511. if (*scopePtr == EScope::TypeName) {
  512. --InsideType;
  513. }
  514. Scopes.pop_back();
  515. }
  516. if (expr) {
  517. --InsideExpr;
  518. }
  519. }
  520. void MarkToken(const TToken& token) {
  521. auto str = token.GetValue();
  522. if (str == "<EOF>") {
  523. return;
  524. }
  525. MarkedTokens.emplace_back();
  526. if (str == "(" || str == "[" || str == "{" || str == "<|" || (InsideType && str == "<")) {
  527. MarkTokenStack.push_back(TokenIndex);
  528. auto& info = MarkedTokens[TokenIndex];
  529. info.OpeningBracket = (InsideExpr > 0);
  530. } else if (str == ")") {
  531. PopBracket("(");
  532. } else if (str == "]") {
  533. PopBracket("[");
  534. } else if (str == "}") {
  535. PopBracket("{");
  536. } else if (str == "|>") {
  537. PopBracket("<|");
  538. } else if (InsideType && str == ">") {
  539. PopBracket("<");
  540. }
  541. TokenIndex++;
  542. }
  543. void PopBracket(const TString& expected) {
  544. Y_ENSURE(!MarkTokenStack.empty());
  545. Y_ENSURE(MarkTokenStack.back() < ParsedTokens.size());
  546. auto& openToken = ParsedTokens[MarkTokenStack.back()];
  547. Y_ENSURE(openToken.Content == expected);
  548. auto& openInfo = MarkedTokens[MarkTokenStack.back()];
  549. auto& closeInfo = MarkedTokens[TokenIndex];
  550. const bool forcedExpansion = openToken.Line == ForceExpandedLine && openToken.LinePos <= ForceExpandedColumn;
  551. if (openInfo.OpeningBracket) {
  552. openInfo.ClosingBracketIndex = TokenIndex;
  553. openInfo.BracketForcedExpansion = forcedExpansion;
  554. closeInfo.BracketForcedExpansion = forcedExpansion;
  555. closeInfo.ClosingBracket = true;
  556. }
  557. MarkTokenStack.pop_back();
  558. }
  559. void Visit(const NProtoBuf::Message& msg) {
  560. const NProtoBuf::Descriptor* descr = msg.GetDescriptor();
  561. //Cerr << descr->name() << "\n";
  562. auto scopePtr = StaticData.ScopeDispatch.FindPtr(descr);
  563. if (descr == TRule_invoke_expr::GetDescriptor()) {
  564. AfterInvokeExpr = true;
  565. }
  566. if (descr == TRule_unary_op::GetDescriptor()) {
  567. AfterUnaryOp = true;
  568. }
  569. if (scopePtr) {
  570. if (*scopePtr == EScope::TypeName) {
  571. ++InsideType;
  572. }
  573. Scopes.push_back(*scopePtr);
  574. }
  575. auto funcPtr = StaticData.PrettyVisitDispatch.FindPtr(descr);
  576. if (funcPtr) {
  577. (*funcPtr)(*this, msg);
  578. } else {
  579. VisitAllFields(descr, msg);
  580. }
  581. if (scopePtr) {
  582. if (*scopePtr == EScope::TypeName) {
  583. --InsideType;
  584. }
  585. Scopes.pop_back();
  586. }
  587. }
  588. TMaybe<bool> IsSimpleStatement(const TRule_sql_stmt_core& msg) {
  589. switch (msg.Alt_case()) {
  590. case TRule_sql_stmt_core::kAltSqlStmtCore1: // pragma
  591. case TRule_sql_stmt_core::kAltSqlStmtCore5: // drop table
  592. case TRule_sql_stmt_core::kAltSqlStmtCore6: // use
  593. case TRule_sql_stmt_core::kAltSqlStmtCore8: // commit
  594. case TRule_sql_stmt_core::kAltSqlStmtCore11: // rollback
  595. case TRule_sql_stmt_core::kAltSqlStmtCore12: // declare
  596. case TRule_sql_stmt_core::kAltSqlStmtCore13: // import
  597. case TRule_sql_stmt_core::kAltSqlStmtCore14: // export
  598. case TRule_sql_stmt_core::kAltSqlStmtCore32: // drop external data source
  599. case TRule_sql_stmt_core::kAltSqlStmtCore34: // drop replication
  600. case TRule_sql_stmt_core::kAltSqlStmtCore60: // drop transfer
  601. return true;
  602. case TRule_sql_stmt_core::kAltSqlStmtCore3: { // named nodes
  603. const auto& stmt = msg.GetAlt_sql_stmt_core3().GetRule_named_nodes_stmt1();
  604. if (stmt.GetBlock3().HasAlt1()) {
  605. return true;
  606. }
  607. break;
  608. }
  609. case TRule_sql_stmt_core::kAltSqlStmtCore17: { // do
  610. const auto& stmt = msg.GetAlt_sql_stmt_core17().GetRule_do_stmt1();
  611. if (stmt.GetBlock2().HasAlt1()) {
  612. return true;
  613. }
  614. break;
  615. }
  616. case TRule_sql_stmt_core::kAltSqlStmtCore19: // if
  617. case TRule_sql_stmt_core::kAltSqlStmtCore20: // for
  618. return false;
  619. default:
  620. break;
  621. }
  622. return {};
  623. }
  624. template <typename T>
  625. void VisitRepeated(const ::google::protobuf::RepeatedPtrField<T>& field) {
  626. for (const auto& m : field) {
  627. Visit(m);
  628. }
  629. }
  630. template <typename T>
  631. void SkipSemicolons(const ::google::protobuf::RepeatedPtrField<T>& field, bool printOne = false) {
  632. for (const auto& m : field) {
  633. if (printOne) {
  634. Visit(m);
  635. printOne = false;
  636. } else {
  637. ++TokenIndex;
  638. }
  639. }
  640. if (printOne) {
  641. Out(';');
  642. }
  643. }
  644. void VisitDefineActionOrSubqueryBody(const TRule_define_action_or_subquery_body& msg) {
  645. SkipSemicolons(msg.GetBlock1());
  646. if (msg.HasBlock2()) {
  647. const auto& b = msg.GetBlock2();
  648. Visit(b.GetRule_sql_stmt_core1());
  649. for (auto block : b.GetBlock2()) {
  650. SkipSemicolons(block.GetBlock1(), /* printOne = */ true);
  651. if (!IsSimpleStatement(block.GetRule_sql_stmt_core2()).GetOrElse(false)) {
  652. Out('\n');
  653. }
  654. Visit(block.GetRule_sql_stmt_core2());
  655. }
  656. SkipSemicolons(b.GetBlock3(), /* printOne = */ true);
  657. }
  658. }
  659. void VisitPragma(const TRule_pragma_stmt& msg) {
  660. NewLine();
  661. VisitKeyword(msg.GetToken1());
  662. auto prefix = msg.GetRule_opt_id_prefix_or_type2();
  663. if (prefix.HasBlock1()) {
  664. Visit(prefix.GetBlock1().GetRule_an_id_or_type1());
  665. VisitKeyword(prefix.GetBlock1().GetToken2());
  666. AfterDot = true;
  667. }
  668. Visit(msg.GetRule_an_id3());
  669. if (msg.GetBlock4().HasAlt2()) {
  670. AfterInvokeExpr = true;
  671. const auto& alt2 = msg.GetBlock4().GetAlt2();
  672. VisitKeyword(alt2.GetToken1());
  673. Visit(alt2.GetRule_pragma_value2());
  674. VisitRepeated(alt2.GetBlock3());
  675. VisitKeyword(alt2.GetToken4());
  676. } else {
  677. Visit(msg.GetBlock4());
  678. }
  679. }
  680. void VisitSelect(const TRule_select_stmt& msg) {
  681. NewLine();
  682. Visit(msg.GetRule_select_kind_parenthesis1());
  683. for (const auto& block : msg.GetBlock2()) {
  684. NewLine();
  685. Visit(block.GetRule_select_op1());
  686. NewLine();
  687. Visit(block.GetRule_select_kind_parenthesis2());
  688. }
  689. }
  690. void VisitSelectUnparenthesized(const TRule_select_unparenthesized_stmt& msg) {
  691. NewLine();
  692. Visit(msg.GetRule_select_kind_partial1());
  693. for (const auto& block : msg.GetBlock2()) {
  694. NewLine();
  695. Visit(block.GetRule_select_op1());
  696. NewLine();
  697. Visit(block.GetRule_select_kind_parenthesis2());
  698. }
  699. }
  700. void VisitNamedNodes(const TRule_named_nodes_stmt& msg) {
  701. NewLine();
  702. Visit(msg.GetRule_bind_parameter_list1());
  703. Visit(msg.GetToken2());
  704. ExprLineIndent = CurrentIndent;
  705. switch (msg.GetBlock3().Alt_case()) {
  706. case TRule_named_nodes_stmt::TBlock3::kAlt1: {
  707. const auto& alt = msg.GetBlock3().GetAlt1();
  708. Visit(alt);
  709. break;
  710. }
  711. case TRule_named_nodes_stmt::TBlock3::kAlt2: {
  712. const auto& alt = msg.GetBlock3().GetAlt2();
  713. const auto& subselect = alt.GetRule_subselect_stmt1();
  714. switch (subselect.GetBlock1().Alt_case()) {
  715. case TRule_subselect_stmt::TBlock1::kAlt1: {
  716. const auto& alt = subselect.GetBlock1().GetAlt1();
  717. Visit(alt.GetToken1());
  718. NewLine();
  719. PushCurrentIndent();
  720. Visit(alt.GetRule_select_stmt2());
  721. PopCurrentIndent();
  722. NewLine();
  723. Visit(alt.GetToken3());
  724. break;
  725. }
  726. case TRule_subselect_stmt::TBlock1::kAlt2: {
  727. const auto& alt = subselect.GetBlock1().GetAlt2();
  728. Out(" (");
  729. NewLine();
  730. PushCurrentIndent();
  731. Visit(alt);
  732. PopCurrentIndent();
  733. NewLine();
  734. Out(')');
  735. break;
  736. }
  737. default:
  738. ythrow yexception() << "Alt is not supported";
  739. }
  740. break;
  741. }
  742. default:
  743. ythrow yexception() << "Alt is not supported";
  744. }
  745. ExprLineIndent = 0;
  746. }
  747. void VisitAlterDatabase(const TRule_alter_database_stmt& msg) {
  748. NewLine();
  749. VisitAllFields(TRule_alter_database_stmt::GetDescriptor(), msg);
  750. }
  751. void VisitCreateTable(const TRule_create_table_stmt& msg) {
  752. NewLine();
  753. Visit(msg.GetToken1());
  754. Visit(msg.GetBlock2());
  755. Visit(msg.GetBlock3());
  756. Visit(msg.GetBlock4());
  757. Visit(msg.GetRule_simple_table_ref5());
  758. Visit(msg.GetToken6());
  759. PushCurrentIndent();
  760. NewLine();
  761. Visit(msg.GetRule_create_table_entry7());
  762. for (const auto& b : msg.GetBlock8()) {
  763. Visit(b.GetToken1());
  764. NewLine();
  765. Visit(b.GetRule_create_table_entry2());
  766. }
  767. if (msg.HasBlock9()) {
  768. Visit(msg.GetBlock9());
  769. }
  770. PopCurrentIndent();
  771. NewLine();
  772. Visit(msg.GetToken10());
  773. if (msg.HasBlock11()) {
  774. NewLine();
  775. Visit(msg.GetBlock11());
  776. }
  777. if (msg.HasBlock12()) {
  778. NewLine();
  779. Visit(msg.GetBlock12());
  780. }
  781. if (msg.HasBlock13()) {
  782. NewLine();
  783. Visit(msg.GetBlock13());
  784. }
  785. if (msg.HasBlock14()) {
  786. NewLine();
  787. Visit(msg.GetBlock14());
  788. }
  789. if (msg.HasBlock15()) {
  790. NewLine();
  791. Visit(msg.GetBlock15());
  792. }
  793. }
  794. void VisitDropTable(const TRule_drop_table_stmt& msg) {
  795. NewLine();
  796. VisitAllFields(TRule_drop_table_stmt::GetDescriptor(), msg);
  797. }
  798. void VisitAnalyze(const TRule_analyze_stmt& msg) {
  799. NewLine();
  800. VisitAllFields(TRule_analyze_stmt::GetDescriptor(), msg);
  801. }
  802. void VisitBackup(const TRule_backup_stmt& msg) {
  803. NewLine();
  804. VisitAllFields(TRule_backup_stmt::GetDescriptor(), msg);
  805. }
  806. void VisitRestore(const TRule_restore_stmt& msg) {
  807. NewLine();
  808. VisitAllFields(TRule_restore_stmt::GetDescriptor(), msg);
  809. }
  810. void VisitUse(const TRule_use_stmt& msg) {
  811. NewLine();
  812. VisitAllFields(TRule_use_stmt::GetDescriptor(), msg);
  813. }
  814. void VisitAlterSequence(const TRule_alter_sequence_stmt& msg) {
  815. PosFromToken(msg.GetToken1());
  816. NewLine();
  817. VisitAllFields(TRule_alter_sequence_stmt::GetDescriptor(), msg);
  818. }
  819. void VisitIntoTable(const TRule_into_table_stmt& msg) {
  820. NewLine();
  821. VisitAllFields(TRule_into_table_stmt::GetDescriptor(), msg);
  822. }
  823. void VisitCommit(const TRule_commit_stmt& msg) {
  824. NewLine();
  825. VisitAllFields(TRule_commit_stmt::GetDescriptor(), msg);
  826. }
  827. void VisitUpdate(const TRule_update_stmt& msg) {
  828. NewLine();
  829. Visit(msg.GetToken2());
  830. Visit(msg.GetRule_simple_table_ref3());
  831. switch (msg.GetBlock4().Alt_case()) {
  832. case TRule_update_stmt_TBlock4::kAlt1: {
  833. const auto& alt = msg.GetBlock4().GetAlt1();
  834. NewLine();
  835. Visit(alt.GetToken1());
  836. const auto& choice = alt.GetRule_set_clause_choice2();
  837. NewLine();
  838. switch (choice.Alt_case()) {
  839. case TRule_set_clause_choice::kAltSetClauseChoice1: {
  840. const auto& clauses = choice.GetAlt_set_clause_choice1().GetRule_set_clause_list1();
  841. NewLine();
  842. PushCurrentIndent();
  843. Visit(clauses.GetRule_set_clause1());
  844. for (auto& block : clauses.GetBlock2()) {
  845. Visit(block.GetToken1());
  846. NewLine();
  847. Visit(block.GetRule_set_clause2());
  848. }
  849. PopCurrentIndent();
  850. NewLine();
  851. break;
  852. }
  853. case TRule_set_clause_choice::kAltSetClauseChoice2: {
  854. const auto& multiColumn = choice.GetAlt_set_clause_choice2().GetRule_multiple_column_assignment1();
  855. const auto& targets = multiColumn.GetRule_set_target_list1();
  856. Visit(targets.GetToken1());
  857. NewLine();
  858. PushCurrentIndent();
  859. Visit(targets.GetRule_set_target2());
  860. for (auto& block : targets.GetBlock3()) {
  861. Visit(block.GetToken1());
  862. NewLine();
  863. Visit(block.GetRule_set_target2());
  864. }
  865. NewLine();
  866. PopCurrentIndent();
  867. Visit(targets.GetToken4());
  868. Visit(multiColumn.GetToken2());
  869. Visit(multiColumn.GetToken3());
  870. NewLine();
  871. const auto& simpleValues = multiColumn.GetRule_simple_values_source4();
  872. switch (simpleValues.Alt_case()) {
  873. case TRule_simple_values_source::kAltSimpleValuesSource1: {
  874. const auto& exprs = simpleValues.GetAlt_simple_values_source1().GetRule_expr_list1();
  875. NewLine();
  876. PushCurrentIndent();
  877. Visit(exprs.GetRule_expr1());
  878. for (const auto& block : exprs.GetBlock2()) {
  879. Visit(block.GetToken1());
  880. NewLine();
  881. Visit(block.GetRule_expr2());
  882. }
  883. PopCurrentIndent();
  884. NewLine();
  885. break;
  886. }
  887. case TRule_simple_values_source::kAltSimpleValuesSource2: {
  888. NewLine();
  889. PushCurrentIndent();
  890. Visit(simpleValues.GetAlt_simple_values_source2());
  891. PopCurrentIndent();
  892. NewLine();
  893. break;
  894. }
  895. default:
  896. ythrow yexception() << "Alt is not supported";
  897. }
  898. NewLine();
  899. Visit(multiColumn.GetToken5());
  900. break;
  901. }
  902. default:
  903. ythrow yexception() << "Alt is not supported";
  904. }
  905. PopCurrentIndent();
  906. if (alt.HasBlock3()) {
  907. NewLine();
  908. Visit(alt.GetBlock3());
  909. }
  910. PopCurrentIndent();
  911. break;
  912. }
  913. case TRule_update_stmt_TBlock4::kAlt2: {
  914. const auto& alt = msg.GetBlock4().GetAlt2();
  915. NewLine();
  916. Visit(alt.GetToken1());
  917. Visit(alt.GetRule_into_values_source2());
  918. break;
  919. }
  920. default:
  921. ythrow yexception() << "Alt is not supported";
  922. }
  923. }
  924. void VisitDelete(const TRule_delete_stmt& msg) {
  925. NewLine();
  926. Visit(msg.GetToken2());
  927. Visit(msg.GetToken3());
  928. Visit(msg.GetRule_simple_table_ref4());
  929. if (msg.HasBlock5()) {
  930. switch (msg.GetBlock5().Alt_case()) {
  931. case TRule_delete_stmt_TBlock5::kAlt1: {
  932. const auto& alt = msg.GetBlock5().GetAlt1();
  933. NewLine();
  934. Visit(alt);
  935. break;
  936. }
  937. case TRule_delete_stmt_TBlock5::kAlt2: {
  938. const auto& alt = msg.GetBlock5().GetAlt2();
  939. NewLine();
  940. Visit(alt);
  941. break;
  942. }
  943. default:
  944. ythrow yexception() << "Alt is not supported";
  945. }
  946. }
  947. }
  948. void VisitRollback(const TRule_rollback_stmt& msg) {
  949. NewLine();
  950. VisitAllFields(TRule_rollback_stmt::GetDescriptor(), msg);
  951. }
  952. void VisitDeclare(const TRule_declare_stmt& msg) {
  953. NewLine();
  954. VisitAllFields(TRule_declare_stmt::GetDescriptor(), msg);
  955. }
  956. void VisitImport(const TRule_import_stmt& msg) {
  957. NewLine();
  958. VisitAllFields(TRule_import_stmt::GetDescriptor(), msg);
  959. }
  960. void VisitExport(const TRule_export_stmt& msg) {
  961. NewLine();
  962. VisitKeyword(msg.GetToken1());
  963. NewLine();
  964. PushCurrentIndent();
  965. const auto& list = msg.GetRule_bind_parameter_list2();
  966. Visit(list.GetRule_bind_parameter1());
  967. for (auto& b : list.GetBlock2()) {
  968. Visit(b.GetToken1());
  969. NewLine();
  970. Visit(b.GetRule_bind_parameter2());
  971. }
  972. PopCurrentIndent();
  973. NewLine();
  974. }
  975. void VisitAlterTable(const TRule_alter_table_stmt& msg) {
  976. NewLine();
  977. VisitKeyword(msg.GetToken1());
  978. VisitKeyword(msg.GetToken2());
  979. Visit(msg.GetRule_simple_table_ref3());
  980. NewLine();
  981. PushCurrentIndent();
  982. Visit(msg.GetRule_alter_table_action4());
  983. for (auto& b : msg.GetBlock5()) {
  984. Visit(b.GetToken1());
  985. NewLine();
  986. Visit(b.GetRule_alter_table_action2());
  987. }
  988. PopCurrentIndent();
  989. NewLine();
  990. }
  991. void VisitAlterTableStore(const TRule_alter_table_store_stmt& msg) {
  992. NewLine();
  993. VisitAllFields(TRule_alter_table_store_stmt::GetDescriptor(), msg);
  994. }
  995. void VisitAlterExternalTable(const TRule_alter_external_table_stmt& msg) {
  996. NewLine();
  997. VisitKeyword(msg.GetToken1());
  998. VisitKeyword(msg.GetToken2());
  999. VisitKeyword(msg.GetToken3());
  1000. Visit(msg.GetRule_simple_table_ref4());
  1001. NewLine();
  1002. PushCurrentIndent();
  1003. Visit(msg.GetRule_alter_external_table_action5());
  1004. for (auto& b : msg.GetBlock6()) {
  1005. Visit(b.GetToken1());
  1006. NewLine();
  1007. Visit(b.GetRule_alter_external_table_action2());
  1008. }
  1009. PopCurrentIndent();
  1010. NewLine();
  1011. }
  1012. void VisitDo(const TRule_do_stmt& msg) {
  1013. VisitKeyword(msg.GetToken1());
  1014. switch (msg.GetBlock2().Alt_case()) {
  1015. case TRule_do_stmt_TBlock2::kAlt1: { // CALL
  1016. PushCurrentIndent();
  1017. NewLine();
  1018. const auto& alt = msg.GetBlock2().GetAlt1().GetRule_call_action1();
  1019. Visit(alt.GetBlock1());
  1020. AfterInvokeExpr = true;
  1021. Visit(alt.GetToken2());
  1022. if (alt.HasBlock3()) {
  1023. Visit(alt.GetBlock3());
  1024. }
  1025. Visit(alt.GetToken4());
  1026. PopCurrentIndent();
  1027. NewLine();
  1028. break;
  1029. }
  1030. case TRule_do_stmt_TBlock2::kAlt2: { // INLINE
  1031. const auto& alt = msg.GetBlock2().GetAlt2().GetRule_inline_action1();
  1032. VisitKeyword(alt.GetToken1());
  1033. PushCurrentIndent();
  1034. NewLine();
  1035. Visit(alt.GetRule_define_action_or_subquery_body2());
  1036. PopCurrentIndent();
  1037. NewLine();
  1038. VisitKeyword(alt.GetToken3());
  1039. VisitKeyword(alt.GetToken4());
  1040. break;
  1041. }
  1042. default:
  1043. ythrow yexception() << "Alt is not supported";
  1044. }
  1045. }
  1046. void VisitAction(const TRule_define_action_or_subquery_stmt& msg) {
  1047. NewLine();
  1048. VisitKeyword(msg.GetToken1());
  1049. VisitKeyword(msg.GetToken2());
  1050. Visit(msg.GetRule_bind_parameter3());
  1051. AfterInvokeExpr = true;
  1052. Visit(msg.GetToken4());
  1053. if (msg.HasBlock5()) {
  1054. Visit(msg.GetBlock5());
  1055. }
  1056. Visit(msg.GetToken6());
  1057. VisitKeyword(msg.GetToken7()); // AS
  1058. NewLine();
  1059. PushCurrentIndent();
  1060. Visit(msg.GetRule_define_action_or_subquery_body8());
  1061. PopCurrentIndent();
  1062. NewLine();
  1063. VisitKeyword(msg.GetToken9());
  1064. VisitKeyword(msg.GetToken10());
  1065. }
  1066. void VisitIf(const TRule_if_stmt& msg) {
  1067. NewLine();
  1068. VisitAllFields(TRule_if_stmt::GetDescriptor(), msg);
  1069. }
  1070. void VisitFor(const TRule_for_stmt& msg) {
  1071. NewLine();
  1072. VisitAllFields(TRule_for_stmt::GetDescriptor(), msg);
  1073. }
  1074. void VisitValues(const TRule_values_stmt& msg) {
  1075. NewLine();
  1076. VisitKeyword(msg.GetToken1());
  1077. const auto& rowList = msg.GetRule_values_source_row_list2();
  1078. PushCurrentIndent();
  1079. NewLine();
  1080. Visit(rowList.GetRule_values_source_row1());
  1081. for (const auto& b : rowList.GetBlock2()) {
  1082. Visit(b.GetToken1());
  1083. NewLine();
  1084. Visit(b.GetRule_values_source_row2());
  1085. }
  1086. PopCurrentIndent();
  1087. NewLine();
  1088. }
  1089. void VisitGrantPermissions(const TRule_grant_permissions_stmt& msg) {
  1090. NewLine();
  1091. VisitAllFields(TRule_grant_permissions_stmt::GetDescriptor(), msg);
  1092. }
  1093. void VisitRevokePermissions(const TRule_revoke_permissions_stmt& msg) {
  1094. NewLine();
  1095. VisitAllFields(TRule_revoke_permissions_stmt::GetDescriptor(), msg);
  1096. }
  1097. void VisitCreateUser(const TRule_create_user_stmt& msg) {
  1098. NewLine();
  1099. VisitAllFields(TRule_create_user_stmt::GetDescriptor(), msg);
  1100. }
  1101. void VisitAlterUser(const TRule_alter_user_stmt& msg) {
  1102. NewLine();
  1103. VisitAllFields(TRule_alter_user_stmt::GetDescriptor(), msg);
  1104. }
  1105. void VisitCreateGroup(const TRule_create_group_stmt& msg) {
  1106. NewLine();
  1107. VisitAllFields(TRule_create_group_stmt::GetDescriptor(), msg);
  1108. }
  1109. void VisitAlterGroup(const TRule_alter_group_stmt& msg) {
  1110. NewLine();
  1111. VisitAllFields(TRule_alter_group_stmt::GetDescriptor(), msg);
  1112. }
  1113. void VisitDropRole(const TRule_drop_role_stmt& msg) {
  1114. NewLine();
  1115. VisitAllFields(TRule_drop_role_stmt::GetDescriptor(), msg);
  1116. }
  1117. void VisitUpsertObject(const TRule_upsert_object_stmt& msg) {
  1118. NewLine();
  1119. VisitAllFields(TRule_upsert_object_stmt::GetDescriptor(), msg);
  1120. }
  1121. void VisitCreateObject(const TRule_create_object_stmt& msg) {
  1122. NewLine();
  1123. VisitAllFields(TRule_create_object_stmt::GetDescriptor(), msg);
  1124. }
  1125. void VisitAlterObject(const TRule_alter_object_stmt& msg) {
  1126. NewLine();
  1127. VisitAllFields(TRule_alter_object_stmt::GetDescriptor(), msg);
  1128. }
  1129. void VisitDropObject(const TRule_drop_object_stmt& msg) {
  1130. NewLine();
  1131. VisitAllFields(TRule_drop_object_stmt::GetDescriptor(), msg);
  1132. }
  1133. void VisitCreateTopic(const TRule_create_topic_stmt& msg) {
  1134. NewLine();
  1135. VisitKeyword(msg.GetToken1());
  1136. VisitKeyword(msg.GetToken2());
  1137. Visit(msg.GetBlock3());
  1138. Visit(msg.GetRule_topic_ref4());
  1139. if (msg.HasBlock5()) {
  1140. auto& b = msg.GetBlock5().GetRule_create_topic_entries1();
  1141. Visit(b.GetToken1());
  1142. NewLine();
  1143. PushCurrentIndent();
  1144. Visit(b.GetRule_create_topic_entry2());
  1145. for (auto& subEntry : b.GetBlock3()) {
  1146. Visit(subEntry.GetToken1());
  1147. NewLine();
  1148. Visit(subEntry.GetRule_create_topic_entry2());
  1149. }
  1150. NewLine();
  1151. PopCurrentIndent();
  1152. Visit(b.GetToken4());
  1153. }
  1154. if (msg.HasBlock6()) {
  1155. auto& b = msg.GetBlock6().GetRule_with_topic_settings1();
  1156. VisitKeyword(b.GetToken1());
  1157. VisitKeyword(b.GetToken2());
  1158. PushCurrentIndent();
  1159. NewLine();
  1160. Visit(b.GetRule_topic_settings3());
  1161. PopCurrentIndent();
  1162. NewLine();
  1163. VisitKeyword(b.GetToken4());
  1164. }
  1165. }
  1166. void VisitAlterTopic(const TRule_alter_topic_stmt& msg) {
  1167. NewLine();
  1168. VisitKeyword(msg.GetToken1());
  1169. VisitKeyword(msg.GetToken2());
  1170. Visit(msg.GetBlock3());
  1171. Visit(msg.GetRule_topic_ref4());
  1172. NewLine();
  1173. PushCurrentIndent();
  1174. Visit(msg.GetRule_alter_topic_action5());
  1175. for (auto& b : msg.GetBlock6()) {
  1176. Visit(b.GetToken1());
  1177. NewLine();
  1178. Visit(b.GetRule_alter_topic_action2());
  1179. }
  1180. PopCurrentIndent();
  1181. NewLine();
  1182. }
  1183. void VisitDropTopic(const TRule_drop_topic_stmt& msg) {
  1184. NewLine();
  1185. VisitAllFields(TRule_drop_topic_stmt::GetDescriptor(), msg);
  1186. }
  1187. void VisitCreateExternalDataSource(const TRule_create_external_data_source_stmt& msg) {
  1188. NewLine();
  1189. VisitAllFields(TRule_create_external_data_source_stmt::GetDescriptor(), msg);
  1190. }
  1191. void VisitAlterExternalDataSource(const TRule_alter_external_data_source_stmt& msg) {
  1192. NewLine();
  1193. VisitToken(msg.GetToken1());
  1194. VisitToken(msg.GetToken2());
  1195. VisitToken(msg.GetToken3());
  1196. VisitToken(msg.GetToken4());
  1197. Visit(msg.GetRule_object_ref5());
  1198. NewLine();
  1199. PushCurrentIndent();
  1200. Visit(msg.GetRule_alter_external_data_source_action6());
  1201. for (const auto& action : msg.GetBlock7()) {
  1202. Visit(action.GetToken1()); // comma
  1203. NewLine();
  1204. Visit(action.GetRule_alter_external_data_source_action2());
  1205. }
  1206. PopCurrentIndent();
  1207. NewLine();
  1208. }
  1209. void VisitDropExternalDataSource(const TRule_drop_external_data_source_stmt& msg) {
  1210. NewLine();
  1211. VisitAllFields(TRule_drop_external_data_source_stmt::GetDescriptor(), msg);
  1212. }
  1213. void VisitCreateView(const TRule_create_view_stmt& msg) {
  1214. NewLine();
  1215. VisitAllFields(TRule_create_view_stmt::GetDescriptor(), msg);
  1216. }
  1217. void VisitDropView(const TRule_drop_view_stmt& msg) {
  1218. NewLine();
  1219. VisitAllFields(TRule_drop_view_stmt::GetDescriptor(), msg);
  1220. }
  1221. void VisitCreateAsyncReplication(const TRule_create_replication_stmt& msg) {
  1222. NewLine();
  1223. VisitAllFields(TRule_create_replication_stmt::GetDescriptor(), msg);
  1224. }
  1225. void VisitAlterAsyncReplication(const TRule_alter_replication_stmt& msg) {
  1226. NewLine();
  1227. VisitAllFields(TRule_alter_replication_stmt::GetDescriptor(), msg);
  1228. }
  1229. void VisitDropAsyncReplication(const TRule_drop_replication_stmt& msg) {
  1230. NewLine();
  1231. VisitAllFields(TRule_drop_replication_stmt::GetDescriptor(), msg);
  1232. }
  1233. void VisitCreateTransfer(const TRule_create_transfer_stmt& msg) {
  1234. NewLine();
  1235. VisitAllFields(TRule_create_transfer_stmt::GetDescriptor(), msg);
  1236. }
  1237. void VisitAlterTransfer(const TRule_alter_transfer_stmt& msg) {
  1238. NewLine();
  1239. VisitAllFields(TRule_alter_transfer_stmt::GetDescriptor(), msg);
  1240. }
  1241. void VisitDropTransfer(const TRule_drop_transfer_stmt& msg) {
  1242. NewLine();
  1243. VisitAllFields(TRule_drop_transfer_stmt::GetDescriptor(), msg);
  1244. }
  1245. void VisitCreateResourcePool(const TRule_create_resource_pool_stmt& msg) {
  1246. NewLine();
  1247. VisitAllFields(TRule_create_resource_pool_stmt::GetDescriptor(), msg);
  1248. }
  1249. void VisitAlterResourcePool(const TRule_alter_resource_pool_stmt& msg) {
  1250. NewLine();
  1251. VisitToken(msg.GetToken1());
  1252. VisitToken(msg.GetToken2());
  1253. VisitToken(msg.GetToken3());
  1254. Visit(msg.GetRule_object_ref4());
  1255. NewLine();
  1256. PushCurrentIndent();
  1257. Visit(msg.GetRule_alter_resource_pool_action5());
  1258. for (const auto& action : msg.GetBlock6()) {
  1259. Visit(action.GetToken1()); // comma
  1260. NewLine();
  1261. Visit(action.GetRule_alter_resource_pool_action2());
  1262. }
  1263. PopCurrentIndent();
  1264. NewLine();
  1265. }
  1266. void VisitDropResourcePool(const TRule_drop_resource_pool_stmt& msg) {
  1267. NewLine();
  1268. VisitAllFields(TRule_drop_resource_pool_stmt::GetDescriptor(), msg);
  1269. }
  1270. void VisitCreateBackupCollection(const TRule_create_backup_collection_stmt& msg) {
  1271. NewLine();
  1272. VisitAllFields(TRule_create_backup_collection_stmt::GetDescriptor(), msg);
  1273. }
  1274. void VisitAlterBackupCollection(const TRule_alter_backup_collection_stmt& msg) {
  1275. NewLine();
  1276. VisitToken(msg.GetToken1());
  1277. Visit(msg.GetRule_backup_collection2());
  1278. NewLine();
  1279. PushCurrentIndent();
  1280. switch (msg.GetBlock3().Alt_case()) {
  1281. case TRule_alter_backup_collection_stmt_TBlock3::kAlt1: {
  1282. Visit(msg.GetBlock3().GetAlt1().GetRule_alter_backup_collection_actions1().GetRule_alter_backup_collection_action1());
  1283. for (const auto& action : msg.GetBlock3().GetAlt1().GetRule_alter_backup_collection_actions1().GetBlock2()) {
  1284. Visit(action.GetToken1()); // comma
  1285. NewLine();
  1286. Visit(action.GetRule_alter_backup_collection_action2());
  1287. }
  1288. break;
  1289. }
  1290. case TRule_alter_backup_collection_stmt_TBlock3::kAlt2: {
  1291. Visit(msg.GetBlock3().GetAlt2().GetRule_alter_backup_collection_entries1().GetRule_alter_backup_collection_entry1());
  1292. for (const auto& entry : msg.GetBlock3().GetAlt2().GetRule_alter_backup_collection_entries1().GetBlock2()) {
  1293. Visit(entry.GetToken1()); // comma
  1294. NewLine();
  1295. Visit(entry.GetRule_alter_backup_collection_entry2());
  1296. }
  1297. break;
  1298. }
  1299. default:
  1300. ythrow yexception() << "Alt is not supported";
  1301. }
  1302. PopCurrentIndent();
  1303. NewLine();
  1304. }
  1305. void VisitDropBackupCollection(const TRule_drop_backup_collection_stmt& msg) {
  1306. NewLine();
  1307. VisitAllFields(TRule_drop_backup_collection_stmt::GetDescriptor(), msg);
  1308. }
  1309. void VisitCreateResourcePoolClassifier(const TRule_create_resource_pool_classifier_stmt& msg) {
  1310. NewLine();
  1311. VisitAllFields(TRule_create_resource_pool_classifier_stmt::GetDescriptor(), msg);
  1312. }
  1313. void VisitAlterResourcePoolClassifier(const TRule_alter_resource_pool_classifier_stmt& msg) {
  1314. NewLine();
  1315. VisitToken(msg.GetToken1());
  1316. VisitToken(msg.GetToken2());
  1317. VisitToken(msg.GetToken3());
  1318. VisitToken(msg.GetToken4());
  1319. Visit(msg.GetRule_object_ref5());
  1320. NewLine();
  1321. PushCurrentIndent();
  1322. Visit(msg.GetRule_alter_resource_pool_classifier_action6());
  1323. for (const auto& action : msg.GetBlock7()) {
  1324. Visit(action.GetToken1()); // comma
  1325. NewLine();
  1326. Visit(action.GetRule_alter_resource_pool_classifier_action2());
  1327. }
  1328. PopCurrentIndent();
  1329. NewLine();
  1330. }
  1331. void VisitDropResourcePoolClassifier(const TRule_drop_resource_pool_classifier_stmt& msg) {
  1332. NewLine();
  1333. VisitAllFields(TRule_drop_resource_pool_classifier_stmt::GetDescriptor(), msg);
  1334. }
  1335. void VisitAllFields(const NProtoBuf::Descriptor* descr, const NProtoBuf::Message& msg) {
  1336. VisitAllFieldsImpl<TPrettyVisitor, &TPrettyVisitor::Visit>(this, descr, msg);
  1337. }
  1338. void WriteComments(bool completeLine) {
  1339. while (LastComment < Comments.size()) {
  1340. const auto& c = Comments[LastComment];
  1341. if (c.Line > LastLine || !completeLine && c.Line == LastLine && c.LinePos > LastColumn) {
  1342. break;
  1343. }
  1344. AddComment(c.Content);
  1345. ++LastComment;
  1346. }
  1347. }
  1348. void PosFromToken(const TToken& token) {
  1349. LastLine = token.GetLine();
  1350. LastColumn = token.GetColumn();
  1351. WriteComments(false);
  1352. }
  1353. void VisitToken(const TToken& token) {
  1354. VisitTokenImpl(token, false);
  1355. }
  1356. void VisitKeyword(const TToken& token) {
  1357. VisitTokenImpl(token, true);
  1358. }
  1359. void VisitTokenImpl(const TToken& token, bool forceKeyword) {
  1360. PosFromToken(token);
  1361. auto str = token.GetValue();
  1362. if (str == "<EOF>") {
  1363. return;
  1364. }
  1365. //Cerr << str << "\n";
  1366. auto currentScope = Scopes.back();
  1367. if (!SkipSpaceAfterUnaryOp && !InMultiTokenOp) {
  1368. if (AfterLess && str == ">") {
  1369. Out(' ');
  1370. } else if (AfterDigits && str == ".") {
  1371. Out(' ');
  1372. } else if (OutColumn && (currentScope == EScope::DoubleQuestion || str != "?")
  1373. && str != ":" && str != "." && str != "," && str != ";" && str != ")" && str != "]"
  1374. && str != "}" && str != "|>" && str != "::" && !AfterNamespace && !AfterBracket
  1375. && !AfterInvokeExpr && !AfterDollarOrAt && !AfterDot && (!AfterQuestion || str != "?")
  1376. && (!InsideType || (str != "<" && str != ">" && str != "<>"))
  1377. && (!InsideType || !AfterLess)
  1378. && (!AfterKeyExpr || str != "[")
  1379. ) {
  1380. Out(' ');
  1381. }
  1382. }
  1383. SkipSpaceAfterUnaryOp = false;
  1384. if (AfterUnaryOp) {
  1385. if (str == "+" || str == "-" || str == "~") {
  1386. SkipSpaceAfterUnaryOp = true;
  1387. }
  1388. AfterUnaryOp = false;
  1389. }
  1390. AfterInvokeExpr = false;
  1391. AfterNamespace = (str == "::");
  1392. AfterBracket = (str == "(" || str == "[" || str == "{" || str == "<|");
  1393. AfterDot = (str == ".");
  1394. AfterDigits = !str.empty() && AllOf(str, [](char c) { return c >= '0' && c <= '9'; });
  1395. AfterQuestion = (str == "?");
  1396. AfterLess = (str == "<");
  1397. AfterKeyExpr = false;
  1398. AfterComment = false;
  1399. if (forceKeyword) {
  1400. str = to_upper(str);
  1401. } else if (currentScope == EScope::Default) {
  1402. if (auto p = StaticData.Keywords.find(to_upper(str)); p != StaticData.Keywords.end()) {
  1403. str = *p;
  1404. }
  1405. }
  1406. AfterDollarOrAt = (str == "$" || str == "@");
  1407. const auto& markedInfo = MarkedTokens[TokenIndex];
  1408. if (markedInfo.ClosingBracket) {
  1409. Y_ENSURE(!MarkTokenStack.empty());
  1410. auto beginTokenIndex = MarkTokenStack.back();
  1411. if (markedInfo.BracketForcedExpansion || ParsedTokens[beginTokenIndex].Line != ParsedTokens[TokenIndex].Line) {
  1412. // multiline
  1413. PopCurrentIndent();
  1414. NewLine();
  1415. }
  1416. MarkTokenStack.pop_back();
  1417. }
  1418. if (InCondExpr) {
  1419. if (str == "=") {
  1420. str = "==";
  1421. } else if (str == "<>") {
  1422. str = "!=";
  1423. }
  1424. }
  1425. if (!AnsiLexer && ParsedTokens[TokenIndex].Name == "STRING_VALUE") {
  1426. TStringBuf checkStr = str;
  1427. if (checkStr.SkipPrefix("\"") && checkStr.ChopSuffix("\"") && !checkStr.Contains("'")) {
  1428. str = TStringBuilder() << '\'' << checkStr << '\'';
  1429. }
  1430. }
  1431. Out(str);
  1432. if (TokenIndex + 1 >= ParsedTokens.size() || ParsedTokens[TokenIndex + 1].Line > LastLine) {
  1433. WriteComments(true);
  1434. }
  1435. if (str == ";") {
  1436. NewLine();
  1437. }
  1438. if (markedInfo.OpeningBracket) {
  1439. MarkTokenStack.push_back(TokenIndex);
  1440. if (markedInfo.BracketForcedExpansion || ParsedTokens[TokenIndex].Line != ParsedTokens[markedInfo.ClosingBracketIndex].Line) {
  1441. // multiline
  1442. PushCurrentIndent();
  1443. NewLine();
  1444. }
  1445. }
  1446. if (str == "," && !MarkTokenStack.empty()) {
  1447. const bool addNewline =
  1448. (TokenIndex + 1 < ParsedTokens.size() && ParsedTokens[TokenIndex].Line != ParsedTokens[TokenIndex + 1].Line)
  1449. || (TokenIndex > 0 && ParsedTokens[TokenIndex - 1].Line != ParsedTokens[TokenIndex].Line);
  1450. // add line for trailing comma
  1451. if (addNewline) {
  1452. NewLine();
  1453. }
  1454. }
  1455. TokenIndex++;
  1456. }
  1457. void VisitIntoValuesSource(const TRule_into_values_source& msg) {
  1458. switch (msg.Alt_case()) {
  1459. case TRule_into_values_source::kAltIntoValuesSource1: {
  1460. const auto& alt = msg.GetAlt_into_values_source1();
  1461. if (alt.HasBlock1()) {
  1462. const auto& columns = alt.GetBlock1().GetRule_pure_column_list1();
  1463. Visit(columns.GetToken1());
  1464. NewLine();
  1465. PushCurrentIndent();
  1466. Visit(columns.GetRule_an_id2());
  1467. for (const auto& block : columns.GetBlock3()) {
  1468. Visit(block.GetToken1());
  1469. NewLine();
  1470. Visit(block.GetRule_an_id2());
  1471. }
  1472. PopCurrentIndent();
  1473. NewLine();
  1474. Visit(columns.GetToken4());
  1475. NewLine();
  1476. }
  1477. Visit(alt.GetRule_values_source2());
  1478. break;
  1479. }
  1480. case TRule_into_values_source::kAltIntoValuesSource2: {
  1481. VisitAllFields(TRule_into_values_source::GetDescriptor(), msg);
  1482. break;
  1483. }
  1484. default:
  1485. ythrow yexception() << "Alt is not supported";
  1486. }
  1487. }
  1488. void VisitSelectKind(const TRule_select_kind& msg) {
  1489. if (msg.HasBlock1()) {
  1490. Visit(msg.GetBlock1());
  1491. }
  1492. Visit(msg.GetBlock2());
  1493. if (msg.HasBlock3()) {
  1494. NewLine();
  1495. Visit(msg.GetBlock3());
  1496. }
  1497. }
  1498. void VisitProcessCore(const TRule_process_core& msg) {
  1499. Visit(msg.GetToken1());
  1500. if (msg.HasBlock2()) {
  1501. Visit(msg.GetBlock2());
  1502. }
  1503. Visit(msg.GetRule_named_single_source3());
  1504. VisitRepeated(msg.GetBlock4());
  1505. if (msg.HasBlock5()) {
  1506. NewLine();
  1507. const auto& block5 = msg.GetBlock5();
  1508. Visit(block5.GetToken1());
  1509. Visit(block5.GetRule_using_call_expr2());
  1510. if (block5.HasBlock3()) {
  1511. Visit(block5.GetBlock3());
  1512. }
  1513. if (block5.HasBlock4()) {
  1514. NewLine();
  1515. Visit(block5.GetBlock4());
  1516. }
  1517. if (block5.HasBlock5()) {
  1518. NewLine();
  1519. const auto& whereBlock = block5.GetBlock5();
  1520. Visit(whereBlock.GetToken1());
  1521. NewLine();
  1522. PushCurrentIndent();
  1523. Visit(whereBlock.GetRule_expr2());
  1524. PopCurrentIndent();
  1525. NewLine();
  1526. }
  1527. if (block5.HasBlock6()) {
  1528. NewLine();
  1529. const auto& havingBlock = block5.GetBlock6();
  1530. Visit(havingBlock.GetToken1());
  1531. NewLine();
  1532. PushCurrentIndent();
  1533. Visit(havingBlock.GetRule_expr2());
  1534. PopCurrentIndent();
  1535. NewLine();
  1536. }
  1537. if (block5.HasBlock7()) {
  1538. NewLine();
  1539. Visit(block5.GetBlock7());
  1540. }
  1541. }
  1542. }
  1543. void VisitReduceCore(const TRule_reduce_core& msg) {
  1544. Visit(msg.GetToken1());
  1545. Visit(msg.GetRule_named_single_source2());
  1546. VisitRepeated(msg.GetBlock3());
  1547. if (msg.HasBlock4()) {
  1548. NewLine();
  1549. Visit(msg.GetBlock4());
  1550. }
  1551. NewLine();
  1552. Visit(msg.GetToken5());
  1553. const auto& columns = msg.GetRule_column_list6();
  1554. NewLine();
  1555. PushCurrentIndent();
  1556. Visit(columns.GetRule_column_name1());
  1557. for (const auto& block : columns.GetBlock2()) {
  1558. Visit(block.GetToken1());
  1559. NewLine();
  1560. Visit(block.GetRule_column_name2());
  1561. }
  1562. if (columns.HasBlock3()) {
  1563. Visit(columns.GetBlock3());
  1564. }
  1565. PopCurrentIndent();
  1566. NewLine();
  1567. Visit(msg.GetToken7());
  1568. if (msg.HasBlock8()) {
  1569. Visit(msg.GetBlock8());
  1570. }
  1571. Visit(msg.GetRule_using_call_expr9());
  1572. if (msg.HasBlock10()) {
  1573. Visit(msg.GetBlock10());
  1574. }
  1575. if (msg.HasBlock11()) {
  1576. NewLine();
  1577. const auto& whereBlock = msg.GetBlock11();
  1578. Visit(whereBlock.GetToken1());
  1579. NewLine();
  1580. PushCurrentIndent();
  1581. Visit(whereBlock.GetRule_expr2());
  1582. PopCurrentIndent();
  1583. NewLine();
  1584. }
  1585. if (msg.HasBlock12()) {
  1586. NewLine();
  1587. const auto& havingBlock = msg.GetBlock12();
  1588. Visit(havingBlock.GetToken1());
  1589. NewLine();
  1590. PushCurrentIndent();
  1591. Visit(havingBlock.GetRule_expr2());
  1592. PopCurrentIndent();
  1593. NewLine();
  1594. }
  1595. if (msg.HasBlock13()) {
  1596. NewLine();
  1597. Visit(msg.GetBlock13());
  1598. }
  1599. }
  1600. void VisitSortSpecificationList(const TRule_sort_specification_list& msg) {
  1601. NewLine();
  1602. PushCurrentIndent();
  1603. Visit(msg.GetRule_sort_specification1());
  1604. for (const auto& block : msg.GetBlock2()) {
  1605. Visit(block.GetToken1());
  1606. NewLine();
  1607. Visit(block.GetRule_sort_specification2());
  1608. }
  1609. PopCurrentIndent();
  1610. NewLine();
  1611. }
  1612. void VisitSelectCore(const TRule_select_core& msg) {
  1613. if (msg.HasBlock1()) {
  1614. Visit(msg.GetBlock1());
  1615. NewLine();
  1616. }
  1617. Visit(msg.GetToken2());
  1618. if (msg.HasBlock3()) {
  1619. Visit(msg.GetBlock3());
  1620. }
  1621. Visit(msg.GetRule_opt_set_quantifier4());
  1622. NewLine();
  1623. PushCurrentIndent();
  1624. Visit(msg.GetRule_result_column5());
  1625. for (const auto& block : msg.GetBlock6()) {
  1626. Visit(block.GetToken1());
  1627. NewLine();
  1628. Visit(block.GetRule_result_column2());
  1629. }
  1630. if (msg.HasBlock7()) {
  1631. Visit(msg.GetBlock7());
  1632. }
  1633. PopCurrentIndent();
  1634. NewLine();
  1635. if (msg.HasBlock8()) {
  1636. NewLine();
  1637. Visit(msg.GetBlock8());
  1638. }
  1639. if (msg.HasBlock9()) {
  1640. NewLine();
  1641. Visit(msg.GetBlock9());
  1642. }
  1643. if (msg.HasBlock10()) {
  1644. NewLine();
  1645. const auto& whereBlock = msg.GetBlock10();
  1646. Visit(whereBlock.GetToken1());
  1647. NewLine();
  1648. PushCurrentIndent();
  1649. Visit(whereBlock.GetRule_expr2());
  1650. PopCurrentIndent();
  1651. NewLine();
  1652. }
  1653. if (msg.HasBlock11()) {
  1654. NewLine();
  1655. Visit(msg.GetBlock11());
  1656. }
  1657. if (msg.HasBlock12()) {
  1658. NewLine();
  1659. const auto& havingBlock = msg.GetBlock12();
  1660. Visit(havingBlock.GetToken1());
  1661. NewLine();
  1662. PushCurrentIndent();
  1663. Visit(havingBlock.GetRule_expr2());
  1664. PopCurrentIndent();
  1665. NewLine();
  1666. }
  1667. if (msg.HasBlock13()) {
  1668. NewLine();
  1669. Visit(msg.GetBlock13());
  1670. }
  1671. if (msg.HasBlock14()) {
  1672. NewLine();
  1673. Visit(msg.GetBlock14());
  1674. }
  1675. }
  1676. void VisitRowPatternRecognitionClause(const TRule_row_pattern_recognition_clause& msg) {
  1677. VisitToken(msg.GetToken1());
  1678. VisitToken(msg.GetToken2());
  1679. NewLine();
  1680. PushCurrentIndent();
  1681. if (msg.HasBlock3()) {
  1682. Visit(msg.GetBlock3());
  1683. NewLine();
  1684. }
  1685. if (msg.HasBlock4()) {
  1686. Visit(msg.GetBlock4());
  1687. NewLine();
  1688. }
  1689. if (msg.HasBlock5()) {
  1690. const auto& block = msg.GetBlock5().GetRule_row_pattern_measures1();
  1691. VisitToken(block.GetToken1());
  1692. NewLine();
  1693. PushCurrentIndent();
  1694. const auto& measureList = block.GetRule_row_pattern_measure_list2();
  1695. Visit(measureList.GetRule_row_pattern_measure_definition1());
  1696. for (const auto& measureDefinitionBlock : measureList.GetBlock2()) {
  1697. VisitToken(measureDefinitionBlock.GetToken1());
  1698. NewLine();
  1699. Visit(measureDefinitionBlock.GetRule_row_pattern_measure_definition2());
  1700. }
  1701. PopCurrentIndent();
  1702. NewLine();
  1703. }
  1704. if (msg.HasBlock6()) {
  1705. Visit(msg.GetBlock6());
  1706. NewLine();
  1707. }
  1708. const auto& common = msg.GetRule_row_pattern_common_syntax7();
  1709. if (common.HasBlock1()) {
  1710. Visit(common.GetBlock1());
  1711. NewLine();
  1712. }
  1713. if (common.HasBlock2()) {
  1714. Visit(common.GetBlock2());
  1715. }
  1716. VisitToken(common.GetToken3());
  1717. VisitToken(common.GetToken4());
  1718. Visit(common.GetRule_row_pattern5());
  1719. VisitToken(common.GetToken6());
  1720. NewLine();
  1721. if (common.HasBlock7()) {
  1722. const auto& block = common.GetBlock7().GetRule_row_pattern_subset_clause1();
  1723. VisitToken(block.GetToken1());
  1724. NewLine();
  1725. PushCurrentIndent();
  1726. const auto& subsetList = block.GetRule_row_pattern_subset_list2();
  1727. Visit(subsetList.GetRule_row_pattern_subset_item1());
  1728. for (const auto& subsetItemBlock : subsetList.GetBlock2()) {
  1729. VisitToken(subsetItemBlock.GetToken1());
  1730. NewLine();
  1731. Visit(subsetItemBlock.GetRule_row_pattern_subset_item2());
  1732. }
  1733. PopCurrentIndent();
  1734. NewLine();
  1735. }
  1736. VisitToken(common.GetToken8());
  1737. NewLine();
  1738. PushCurrentIndent();
  1739. const auto& definitionList = common.GetRule_row_pattern_definition_list9();
  1740. Visit(definitionList.GetRule_row_pattern_definition1());
  1741. for (const auto& definitionBlock : definitionList.GetBlock2()) {
  1742. VisitToken(definitionBlock.GetToken1());
  1743. NewLine();
  1744. Visit(definitionBlock.GetRule_row_pattern_definition2());
  1745. }
  1746. PopCurrentIndent();
  1747. NewLine();
  1748. PopCurrentIndent();
  1749. NewLine();
  1750. VisitToken(msg.GetToken8());
  1751. }
  1752. void VisitJoinSource(const TRule_join_source& msg) {
  1753. if (msg.HasBlock1()) {
  1754. Visit(msg.GetBlock1());
  1755. }
  1756. Visit(msg.GetRule_flatten_source2());
  1757. for (const auto& block : msg.GetBlock3()) {
  1758. NewLine();
  1759. Visit(block.GetRule_join_op1());
  1760. if (block.HasBlock2()) {
  1761. Visit(block.GetBlock2());
  1762. }
  1763. Visit(block.GetRule_flatten_source3());
  1764. if (block.HasBlock4()) {
  1765. NewLine();
  1766. Visit(block.GetBlock4());
  1767. }
  1768. }
  1769. }
  1770. void VisitJoinConstraint(const TRule_join_constraint& msg) {
  1771. if (msg.Alt_case() == TRule_join_constraint::kAltJoinConstraint1) {
  1772. const auto& alt = msg.GetAlt_join_constraint1();
  1773. Visit(alt.GetToken1());
  1774. NewLine();
  1775. PushCurrentIndent();
  1776. Visit(alt.GetRule_expr2());
  1777. PopCurrentIndent();
  1778. NewLine();
  1779. } else {
  1780. VisitAllFields(TRule_join_constraint::GetDescriptor(), msg);
  1781. }
  1782. }
  1783. void VisitSingleSource(const TRule_single_source& msg) {
  1784. switch (msg.Alt_case()) {
  1785. case TRule_single_source::kAltSingleSource1: {
  1786. const auto& alt = msg.GetAlt_single_source1();
  1787. Visit(alt);
  1788. break;
  1789. }
  1790. case TRule_single_source::kAltSingleSource2: {
  1791. const auto& alt = msg.GetAlt_single_source2();
  1792. Visit(alt.GetToken1());
  1793. NewLine();
  1794. PushCurrentIndent();
  1795. Visit(alt.GetRule_select_stmt2());
  1796. PopCurrentIndent();
  1797. NewLine();
  1798. Visit(alt.GetToken3());
  1799. break;
  1800. }
  1801. case TRule_single_source::kAltSingleSource3: {
  1802. const auto& alt = msg.GetAlt_single_source3();
  1803. Visit(alt.GetToken1());
  1804. NewLine();
  1805. PushCurrentIndent();
  1806. Visit(alt.GetRule_values_stmt2());
  1807. PopCurrentIndent();
  1808. NewLine();
  1809. Visit(alt.GetToken3());
  1810. break;
  1811. }
  1812. default:
  1813. ythrow yexception() << "Alt is not supported";
  1814. }
  1815. }
  1816. void VisitFlattenSource(const TRule_flatten_source& msg) {
  1817. const auto& namedSingleSource = msg.GetRule_named_single_source1();
  1818. bool indentBeforeSource = namedSingleSource.GetRule_single_source1().Alt_case() == TRule_single_source::kAltSingleSource1;
  1819. if (indentBeforeSource) {
  1820. NewLine();
  1821. PushCurrentIndent();
  1822. }
  1823. Visit(namedSingleSource);
  1824. if (indentBeforeSource) {
  1825. PopCurrentIndent();
  1826. NewLine();
  1827. }
  1828. if (msg.HasBlock2()) {
  1829. NewLine();
  1830. PushCurrentIndent();
  1831. Visit(msg.GetBlock2());
  1832. PopCurrentIndent();
  1833. NewLine();
  1834. }
  1835. }
  1836. void VisitNamedSingleSource(const TRule_named_single_source& msg) {
  1837. Visit(msg.GetRule_single_source1());
  1838. if (msg.HasBlock2()) {
  1839. Visit(msg.GetBlock2());
  1840. }
  1841. if (msg.HasBlock3()) {
  1842. const auto& block3 = msg.GetBlock3();
  1843. Visit(block3.GetBlock1());
  1844. if (block3.HasBlock2()) {
  1845. const auto& columns = block3.GetBlock2().GetRule_pure_column_list1();
  1846. Visit(columns.GetToken1());
  1847. NewLine();
  1848. PushCurrentIndent();
  1849. Visit(columns.GetRule_an_id2());
  1850. for (const auto& block : columns.GetBlock3()) {
  1851. Visit(block.GetToken1());
  1852. NewLine();
  1853. Visit(block.GetRule_an_id2());
  1854. }
  1855. NewLine();
  1856. PopCurrentIndent();
  1857. Visit(columns.GetToken4());
  1858. }
  1859. }
  1860. if (msg.HasBlock4()) {
  1861. NewLine();
  1862. Visit(msg.GetBlock4());
  1863. }
  1864. }
  1865. void VisitTableHints(const TRule_table_hints& msg) {
  1866. Visit(msg.GetToken1());
  1867. const auto& block2 = msg.GetBlock2();
  1868. if (block2.Alt_case() == TRule_table_hints::TBlock2::kAlt2) {
  1869. const auto& alt = block2.GetAlt2();
  1870. Visit(alt.GetToken1());
  1871. NewLine();
  1872. PushCurrentIndent();
  1873. Visit(alt.GetRule_table_hint2());
  1874. for (const auto& block : alt.GetBlock3()) {
  1875. Visit(block.GetToken1());
  1876. NewLine();
  1877. Visit(block.GetRule_table_hint2());
  1878. }
  1879. NewLine();
  1880. PopCurrentIndent();
  1881. Visit(alt.GetToken4());
  1882. } else {
  1883. Visit(block2);
  1884. }
  1885. }
  1886. void VisitSimpleTableRef(const TRule_simple_table_ref& msg) {
  1887. Visit(msg.GetRule_simple_table_ref_core1());
  1888. if (msg.HasBlock2()) {
  1889. Visit(msg.GetBlock2());
  1890. }
  1891. }
  1892. void VisitIntoSimpleTableRef(const TRule_into_simple_table_ref& msg) {
  1893. Visit(msg.GetRule_simple_table_ref1());
  1894. if (msg.HasBlock2()) {
  1895. const auto& block2 = msg.GetBlock2();
  1896. NewLine();
  1897. PushCurrentIndent();
  1898. Visit(block2.GetToken1());
  1899. Visit(block2.GetToken2());
  1900. const auto& columns = block2.GetRule_pure_column_list3();
  1901. Visit(columns.GetToken1());
  1902. NewLine();
  1903. PushCurrentIndent();
  1904. Visit(columns.GetRule_an_id2());
  1905. for (const auto& block : columns.GetBlock3()) {
  1906. Visit(block.GetToken1());
  1907. NewLine();
  1908. Visit(block.GetRule_an_id2());
  1909. }
  1910. PopCurrentIndent();
  1911. NewLine();
  1912. Visit(columns.GetToken4());
  1913. PopCurrentIndent();
  1914. }
  1915. }
  1916. void VisitSelectKindPartial(const TRule_select_kind_partial& msg) {
  1917. Visit(msg.GetRule_select_kind1());
  1918. if (msg.HasBlock2()) {
  1919. NewLine();
  1920. Visit(msg.GetBlock2());
  1921. }
  1922. }
  1923. void VisitFlattenByArg(const TRule_flatten_by_arg& msg) {
  1924. switch (msg.Alt_case()) {
  1925. case TRule_flatten_by_arg::kAltFlattenByArg1: {
  1926. const auto& alt = msg.GetAlt_flatten_by_arg1();
  1927. Visit(alt);
  1928. break;
  1929. }
  1930. case TRule_flatten_by_arg::kAltFlattenByArg2: {
  1931. const auto& alt = msg.GetAlt_flatten_by_arg2();
  1932. Visit(alt.GetToken1());
  1933. NewLine();
  1934. PushCurrentIndent();
  1935. const auto& exprs = alt.GetRule_named_expr_list2();
  1936. Visit(exprs.GetRule_named_expr1());
  1937. for (const auto& block : exprs.GetBlock2()) {
  1938. Visit(block.GetToken1());
  1939. NewLine();
  1940. Visit(block.GetRule_named_expr2());
  1941. }
  1942. if (alt.HasBlock3()) {
  1943. Visit(alt.GetBlock3());
  1944. }
  1945. NewLine();
  1946. PopCurrentIndent();
  1947. Visit(alt.GetToken4());
  1948. break;
  1949. }
  1950. default:
  1951. ythrow yexception() << "Alt is not supported";
  1952. }
  1953. }
  1954. void VisitWithoutColumnList(const TRule_without_column_list& msg) {
  1955. NewLine();
  1956. PushCurrentIndent();
  1957. Visit(msg.GetRule_without_column_name1());
  1958. for (const auto& block : msg.GetBlock2()) {
  1959. Visit(block.GetToken1());
  1960. NewLine();
  1961. Visit(block.GetRule_without_column_name2());
  1962. }
  1963. if (msg.HasBlock3()) {
  1964. Visit(msg.GetBlock3());
  1965. }
  1966. PopCurrentIndent();
  1967. NewLine();
  1968. }
  1969. void VisitTableRef(const TRule_table_ref& msg) {
  1970. if (msg.HasBlock1()) {
  1971. Visit(msg.GetBlock1());
  1972. }
  1973. if (msg.HasBlock2()) {
  1974. Visit(msg.GetBlock2());
  1975. }
  1976. const auto& block3 = msg.GetBlock3();
  1977. switch (block3.Alt_case()) {
  1978. case TRule_table_ref::TBlock3::kAlt1: {
  1979. const auto& alt = block3.GetAlt1();
  1980. const auto& key = alt.GetRule_table_key1();
  1981. Visit(key.GetRule_id_table_or_type1());
  1982. Visit(key.GetBlock2());
  1983. break;
  1984. }
  1985. case TRule_table_ref::TBlock3::kAlt2: {
  1986. const auto& alt = block3.GetAlt2();
  1987. Visit(alt.GetRule_an_id_expr1());
  1988. AfterInvokeExpr = true;
  1989. Visit(alt.GetToken2());
  1990. if (alt.HasBlock3()) {
  1991. Visit(alt.GetBlock3());
  1992. }
  1993. Visit(alt.GetToken4());
  1994. break;
  1995. }
  1996. case TRule_table_ref::TBlock3::kAlt3: {
  1997. const auto& alt = block3.GetAlt3();
  1998. Visit(alt.GetRule_bind_parameter1());
  1999. if (alt.HasBlock2()) {
  2000. AfterInvokeExpr = true;
  2001. Visit(alt.GetBlock2());
  2002. }
  2003. if (alt.HasBlock3()) {
  2004. Visit(alt.GetBlock3());
  2005. }
  2006. break;
  2007. }
  2008. default:
  2009. ythrow yexception() << "Alt is not supported";
  2010. }
  2011. if (msg.HasBlock4()) {
  2012. Visit(msg.GetBlock4());
  2013. }
  2014. }
  2015. void VisitGroupingElementList(const TRule_grouping_element_list& msg) {
  2016. NewLine();
  2017. PushCurrentIndent();
  2018. Visit(msg.GetRule_grouping_element1());
  2019. for (const auto& block : msg.GetBlock2()) {
  2020. Visit(block.GetToken1());
  2021. NewLine();
  2022. Visit(block.GetRule_grouping_element2());
  2023. }
  2024. PopCurrentIndent();
  2025. NewLine();
  2026. }
  2027. void VisitGroupByClause(const TRule_group_by_clause& msg) {
  2028. Visit(msg.GetToken1());
  2029. if (msg.HasBlock2()) {
  2030. Visit(msg.GetBlock2());
  2031. }
  2032. Visit(msg.GetToken3());
  2033. Visit(msg.GetRule_opt_set_quantifier4());
  2034. Visit(msg.GetRule_grouping_element_list5());
  2035. if (msg.HasBlock6()) {
  2036. NewLine();
  2037. PushCurrentIndent();
  2038. Visit(msg.GetBlock6());
  2039. PopCurrentIndent();
  2040. NewLine();
  2041. }
  2042. }
  2043. void VisitWindowDefinitionList(const TRule_window_definition_list& msg) {
  2044. NewLine();
  2045. PushCurrentIndent();
  2046. Visit(msg.GetRule_window_definition1());
  2047. for (const auto& block : msg.GetBlock2()) {
  2048. Visit(block.GetToken1());
  2049. NewLine();
  2050. Visit(block.GetRule_window_definition2());
  2051. }
  2052. PopCurrentIndent();
  2053. NewLine();
  2054. }
  2055. void VisitWindowSpecification(const TRule_window_specification& msg) {
  2056. Visit(msg.GetToken1());
  2057. const auto& details = msg.GetRule_window_specification_details2();
  2058. const bool needsNewline = details.HasBlock1() || details.HasBlock2() ||
  2059. details.HasBlock3() || details.HasBlock4();
  2060. if (needsNewline) {
  2061. NewLine();
  2062. PushCurrentIndent();
  2063. }
  2064. if (details.HasBlock1()) {
  2065. NewLine();
  2066. Visit(details.GetBlock1());
  2067. }
  2068. if (details.HasBlock2()) {
  2069. NewLine();
  2070. Visit(details.GetBlock2());
  2071. }
  2072. if (details.HasBlock3()) {
  2073. NewLine();
  2074. Visit(details.GetBlock3());
  2075. }
  2076. if (details.HasBlock4()) {
  2077. NewLine();
  2078. Visit(details.GetBlock4());
  2079. }
  2080. if (needsNewline) {
  2081. NewLine();
  2082. PopCurrentIndent();
  2083. }
  2084. Visit(msg.GetToken3());
  2085. }
  2086. void VisitWindowParitionClause(const TRule_window_partition_clause& msg) {
  2087. Visit(msg.GetToken1());
  2088. if (msg.HasBlock2()) {
  2089. Visit(msg.GetBlock2());
  2090. }
  2091. Visit(msg.GetToken3());
  2092. const auto& exprs = msg.GetRule_named_expr_list4();
  2093. PushCurrentIndent();
  2094. NewLine();
  2095. Visit(exprs.GetRule_named_expr1());
  2096. for (const auto& block : exprs.GetBlock2()) {
  2097. Visit(block.GetToken1());
  2098. NewLine();
  2099. Visit(block.GetRule_named_expr2());
  2100. }
  2101. PopCurrentIndent();
  2102. NewLine();
  2103. }
  2104. void VisitLambdaBody(const TRule_lambda_body& msg) {
  2105. PushCurrentIndent();
  2106. NewLine();
  2107. SkipSemicolons(msg.GetBlock1());
  2108. for (const auto& block : msg.GetBlock2()) {
  2109. Visit(block.GetRule_lambda_stmt1());
  2110. SkipSemicolons(block.GetBlock2(), /* printOne = */ true);
  2111. NewLine();
  2112. }
  2113. Visit(msg.GetToken3());
  2114. ExprLineIndent = CurrentIndent;
  2115. Visit(msg.GetRule_expr4());
  2116. SkipSemicolons(msg.GetBlock5(), /* printOne = */ true);
  2117. ExprLineIndent = 0;
  2118. PopCurrentIndent();
  2119. NewLine();
  2120. }
  2121. void VisitInAtomExpr(const TRule_in_atom_expr& msg) {
  2122. if (msg.Alt_case() == TRule_in_atom_expr::kAltInAtomExpr7) {
  2123. const auto& alt = msg.GetAlt_in_atom_expr7();
  2124. Visit(alt.GetToken1());
  2125. NewLine();
  2126. PushCurrentIndent();
  2127. Visit(alt.GetRule_select_stmt2());
  2128. NewLine();
  2129. PopCurrentIndent();
  2130. Visit(alt.GetToken3());
  2131. } else {
  2132. VisitAllFields(TRule_in_atom_expr::GetDescriptor(), msg);
  2133. }
  2134. }
  2135. void VisitSelectKindParenthesis(const TRule_select_kind_parenthesis& msg) {
  2136. if (msg.Alt_case() == TRule_select_kind_parenthesis::kAltSelectKindParenthesis2) {
  2137. const auto& alt = msg.GetAlt_select_kind_parenthesis2();
  2138. Visit(alt.GetToken1());
  2139. NewLine();
  2140. PushCurrentIndent();
  2141. Visit(alt.GetRule_select_kind_partial2());
  2142. PopCurrentIndent();
  2143. NewLine();
  2144. Visit(alt.GetToken3());
  2145. } else {
  2146. VisitAllFields(TRule_select_kind_parenthesis::GetDescriptor(), msg);
  2147. }
  2148. }
  2149. void VisitCastExpr(const TRule_cast_expr& msg) {
  2150. Visit(msg.GetToken1());
  2151. AfterInvokeExpr = true;
  2152. Visit(msg.GetToken2());
  2153. Visit(msg.GetRule_expr3());
  2154. Visit(msg.GetToken4());
  2155. Visit(msg.GetRule_type_name_or_bind5());
  2156. Visit(msg.GetToken6());
  2157. }
  2158. void VisitBitCastExpr(const TRule_bitcast_expr& msg) {
  2159. Visit(msg.GetToken1());
  2160. AfterInvokeExpr = true;
  2161. Visit(msg.GetToken2());
  2162. Visit(msg.GetRule_expr3());
  2163. Visit(msg.GetToken4());
  2164. Visit(msg.GetRule_type_name_simple5());
  2165. Visit(msg.GetToken6());
  2166. }
  2167. void VisitExtOrderByClause(const TRule_ext_order_by_clause& msg) {
  2168. if (msg.HasBlock1()) {
  2169. Visit(msg.GetBlock1());
  2170. }
  2171. Visit(msg.GetRule_order_by_clause2());
  2172. }
  2173. void VisitKeyExpr(const TRule_key_expr& msg) {
  2174. AfterKeyExpr = true;
  2175. VisitAllFields(TRule_key_expr::GetDescriptor(), msg);
  2176. }
  2177. void VisitExistsExpr(const TRule_exists_expr& msg) {
  2178. VisitKeyword(msg.GetToken1());
  2179. VisitToken(msg.GetToken2());
  2180. NewLine();
  2181. PushCurrentIndent();
  2182. Visit(msg.GetBlock3());
  2183. PopCurrentIndent();
  2184. NewLine();
  2185. VisitToken(msg.GetToken4());
  2186. }
  2187. void VisitCaseExpr(const TRule_case_expr& msg) {
  2188. VisitKeyword(msg.GetToken1());
  2189. if (msg.HasBlock2()) {
  2190. Visit(msg.GetBlock2());
  2191. }
  2192. NewLine();
  2193. PushCurrentIndent();
  2194. for (const auto& block : msg.GetBlock3()) {
  2195. Visit(block);
  2196. NewLine();
  2197. }
  2198. if (msg.HasBlock4()) {
  2199. const auto& block = msg.GetBlock4();
  2200. VisitKeyword(block.GetToken1());
  2201. Visit(block.GetRule_expr2());
  2202. }
  2203. PopCurrentIndent();
  2204. NewLine();
  2205. Visit(msg.GetToken5());
  2206. }
  2207. void VisitWithTableSettingsExpr(const TRule_with_table_settings& msg) {
  2208. VisitKeyword(msg.GetToken1());
  2209. Visit(msg.GetToken2());
  2210. const bool needIndent = msg.Block4Size() > 0; // more then one setting
  2211. if (needIndent) {
  2212. NewLine();
  2213. PushCurrentIndent();
  2214. Visit(msg.GetRule_table_settings_entry3()); // first setting
  2215. for (const auto& entry : msg.GetBlock4()) {
  2216. Visit(entry.GetToken1()); // comma
  2217. NewLine();
  2218. Visit(entry.GetRule_table_settings_entry2()); // other settings
  2219. }
  2220. PopCurrentIndent();
  2221. NewLine();
  2222. } else {
  2223. Visit(msg.GetRule_table_settings_entry3());
  2224. }
  2225. Visit(msg.GetToken5());
  2226. }
  2227. void VisitTableSettingValue(const TRule_table_setting_value& msg) {
  2228. switch (msg.GetAltCase()) {
  2229. case TRule_table_setting_value::kAltTableSettingValue5: {
  2230. // | ttl_tier_list ON an_id (AS (SECONDS | MILLISECONDS | MICROSECONDS | NANOSECONDS))?
  2231. const auto& ttlSettings = msg.GetAlt_table_setting_value5();
  2232. const auto& tierList = ttlSettings.GetRule_ttl_tier_list1();
  2233. const bool needIndent = tierList.HasBlock2() && tierList.GetBlock2().Block2Size() > 0; // more then one tier
  2234. if (needIndent) {
  2235. NewLine();
  2236. PushCurrentIndent();
  2237. Visit(tierList.GetRule_expr1());
  2238. VisitTtlTierAction(tierList.GetBlock2().GetRule_ttl_tier_action1());
  2239. for (const auto& tierEntry : tierList.GetBlock2().GetBlock2()) {
  2240. Visit(tierEntry.GetToken1()); // comma
  2241. NewLine();
  2242. Visit(tierEntry.GetRule_expr2());
  2243. VisitTtlTierAction(tierEntry.GetRule_ttl_tier_action3());
  2244. }
  2245. PopCurrentIndent();
  2246. NewLine();
  2247. } else {
  2248. Visit(tierList.GetRule_expr1());
  2249. if (tierList.HasBlock2()) {
  2250. VisitTtlTierAction(tierList.GetBlock2().GetRule_ttl_tier_action1());
  2251. }
  2252. }
  2253. VisitKeyword(ttlSettings.GetToken2());
  2254. Visit(ttlSettings.GetRule_an_id3());
  2255. if (ttlSettings.HasBlock4()) {
  2256. VisitKeyword(ttlSettings.GetBlock4().GetToken1());
  2257. VisitKeyword(ttlSettings.GetBlock4().GetToken2());
  2258. }
  2259. } break;
  2260. default:
  2261. VisitAllFields(TRule_table_setting_value::GetDescriptor(), msg);
  2262. }
  2263. }
  2264. void VisitTtlTierAction(const TRule_ttl_tier_action& msg) {
  2265. switch (msg.GetAltCase()) {
  2266. case TRule_ttl_tier_action::kAltTtlTierAction1:
  2267. // | TO EXTERNAL DATA SOURCE an_id
  2268. VisitKeyword(msg.GetAlt_ttl_tier_action1().GetToken1());
  2269. VisitKeyword(msg.GetAlt_ttl_tier_action1().GetToken2());
  2270. VisitKeyword(msg.GetAlt_ttl_tier_action1().GetToken3());
  2271. VisitKeyword(msg.GetAlt_ttl_tier_action1().GetToken4());
  2272. Visit(msg.GetAlt_ttl_tier_action1().GetRule_an_id5());
  2273. break;
  2274. case TRule_ttl_tier_action::kAltTtlTierAction2:
  2275. // | DELETE
  2276. VisitKeyword(msg.GetAlt_ttl_tier_action2().GetToken1());
  2277. break;
  2278. case TRule_ttl_tier_action::ALT_NOT_SET:
  2279. break;
  2280. }
  2281. }
  2282. void VisitExpr(const TRule_expr& msg) {
  2283. if (msg.HasAlt_expr2()) {
  2284. Visit(msg.GetAlt_expr2());
  2285. return;
  2286. }
  2287. const auto& orExpr = msg.GetAlt_expr1();
  2288. auto getExpr = [](const TRule_expr::TAlt1::TBlock2& b) -> const TRule_or_subexpr& { return b.GetRule_or_subexpr2(); };
  2289. auto getOp = [](const TRule_expr::TAlt1::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2290. VisitBinaryOp(orExpr.GetRule_or_subexpr1(), getOp, getExpr, orExpr.GetBlock2().begin(), orExpr.GetBlock2().end());
  2291. }
  2292. void VisitCondExpr(const TRule_cond_expr& msg) {
  2293. if (msg.Alt_case() == TRule_cond_expr::kAltCondExpr5) {
  2294. for (const auto& block : msg.GetAlt_cond_expr5().GetBlock1()) {
  2295. InCondExpr = true;
  2296. Visit(block.GetBlock1());
  2297. InCondExpr = false;
  2298. Visit(block.GetRule_eq_subexpr2());
  2299. }
  2300. } else {
  2301. VisitAllFields(TRule_cond_expr::GetDescriptor(), msg);
  2302. }
  2303. }
  2304. void VisitOrSubexpr(const TRule_or_subexpr& msg) {
  2305. auto getExpr = [](const TRule_or_subexpr::TBlock2& b) -> const TRule_and_subexpr& { return b.GetRule_and_subexpr2(); };
  2306. auto getOp = [](const TRule_or_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2307. VisitBinaryOp(msg.GetRule_and_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2308. }
  2309. void VisitAndSubexpr(const TRule_and_subexpr& msg) {
  2310. auto getExpr = [](const TRule_and_subexpr::TBlock2& b) -> const TRule_xor_subexpr& { return b.GetRule_xor_subexpr2(); };
  2311. auto getOp = [](const TRule_and_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2312. VisitBinaryOp(msg.GetRule_xor_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2313. }
  2314. void VisitEqSubexpr(const TRule_eq_subexpr& msg) {
  2315. auto getExpr = [](const TRule_eq_subexpr::TBlock2& b) -> const TRule_neq_subexpr& { return b.GetRule_neq_subexpr2(); };
  2316. auto getOp = [](const TRule_eq_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2317. VisitBinaryOp(msg.GetRule_neq_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2318. }
  2319. void VisitNeqSubexpr(const TRule_neq_subexpr& msg) {
  2320. bool pushedIndent = false;
  2321. VisitNeqSubexprImpl(msg, pushedIndent, true);
  2322. }
  2323. void VisitNeqSubexprImpl(const TRule_neq_subexpr& msg, bool& pushedIndent, bool top) {
  2324. auto getExpr = [](const TRule_neq_subexpr::TBlock2& b) -> const TRule_bit_subexpr& { return b.GetRule_bit_subexpr2(); };
  2325. auto getOp = [](const TRule_neq_subexpr::TBlock2& b) -> const TRule_neq_subexpr::TBlock2::TBlock1& { return b.GetBlock1(); };
  2326. VisitBinaryOp(msg.GetRule_bit_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2327. if (msg.HasBlock3()) {
  2328. const auto& b = msg.GetBlock3();
  2329. switch (b.Alt_case()) {
  2330. case TRule_neq_subexpr_TBlock3::kAlt1: {
  2331. const auto& alt = b.GetAlt1();
  2332. const bool hasFirstNewline = LastLine != ParsedTokens[TokenIndex].Line;
  2333. // 2 is `??` size in tokens
  2334. const bool hasSecondNewline = ParsedTokens[TokenIndex].Line != ParsedTokens[TokenIndex + 2].Line;
  2335. const ui32 currentOutLine = OutLine;
  2336. if (currentOutLine != OutLine || (hasFirstNewline && hasSecondNewline)) {
  2337. NewLine();
  2338. if (!pushedIndent) {
  2339. PushCurrentIndent();
  2340. pushedIndent = true;
  2341. }
  2342. }
  2343. Visit(alt.GetRule_double_question1());
  2344. if (hasFirstNewline || hasSecondNewline) {
  2345. NewLine();
  2346. if (!pushedIndent) {
  2347. PushCurrentIndent();
  2348. pushedIndent = true;
  2349. }
  2350. }
  2351. VisitNeqSubexprImpl(alt.GetRule_neq_subexpr2(), pushedIndent, false);
  2352. if (pushedIndent && top) {
  2353. PopCurrentIndent();
  2354. pushedIndent = false;
  2355. }
  2356. break;
  2357. }
  2358. case TRule_neq_subexpr_TBlock3::kAlt2:
  2359. Visit(b.GetAlt2());
  2360. break;
  2361. default:
  2362. ythrow yexception() << "Alt is not supported";
  2363. }
  2364. }
  2365. }
  2366. void VisitBitSubexpr(const TRule_bit_subexpr& msg) {
  2367. auto getExpr = [](const TRule_bit_subexpr::TBlock2& b) -> const TRule_add_subexpr& { return b.GetRule_add_subexpr2(); };
  2368. auto getOp = [](const TRule_bit_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2369. VisitBinaryOp(msg.GetRule_add_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2370. }
  2371. void VisitAddSubexpr(const TRule_add_subexpr& msg) {
  2372. auto getExpr = [](const TRule_add_subexpr::TBlock2& b) -> const TRule_mul_subexpr& { return b.GetRule_mul_subexpr2(); };
  2373. auto getOp = [](const TRule_add_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2374. VisitBinaryOp(msg.GetRule_mul_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2375. }
  2376. void VisitMulSubexpr(const TRule_mul_subexpr& msg) {
  2377. auto getExpr = [](const TRule_mul_subexpr::TBlock2& b) -> const TRule_con_subexpr& { return b.GetRule_con_subexpr2(); };
  2378. auto getOp = [](const TRule_mul_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2379. VisitBinaryOp(msg.GetRule_con_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2380. }
  2381. ui32 BinaryOpTokenSize(const TToken&) {
  2382. return 1;
  2383. }
  2384. ui32 BinaryOpTokenSize(const TRule_neq_subexpr::TBlock2::TBlock1& block) {
  2385. switch (block.Alt_case()) {
  2386. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt1:
  2387. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt3:
  2388. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt5:
  2389. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt6:
  2390. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt7:
  2391. return 1;
  2392. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt2:
  2393. return 2;
  2394. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt4:
  2395. return 3;
  2396. default:
  2397. ythrow yexception() << "Alt is not supported";
  2398. }
  2399. }
  2400. void VisitShiftRight(const TRule_shift_right& msg) {
  2401. VisitToken(msg.GetToken1());
  2402. InMultiTokenOp = true;
  2403. VisitToken(msg.GetToken2());
  2404. InMultiTokenOp = false;
  2405. }
  2406. void VisitRotRight(const TRule_rot_right& msg) {
  2407. VisitToken(msg.GetToken1());
  2408. InMultiTokenOp = true;
  2409. VisitToken(msg.GetToken2());
  2410. VisitToken(msg.GetToken3());
  2411. InMultiTokenOp = false;
  2412. }
  2413. template <typename TExpr, typename TGetOp, typename TGetExpr, typename TIter>
  2414. void VisitBinaryOp(const TExpr& expr, TGetOp getOp, TGetExpr getExpr, TIter begin, TIter end) {
  2415. Visit(expr);
  2416. bool pushedIndent = false;
  2417. for (; begin != end; ++begin) {
  2418. const auto op = getOp(*begin);
  2419. const auto opSize = BinaryOpTokenSize(op);
  2420. const bool hasFirstNewline = LastLine != ParsedTokens[TokenIndex].Line;
  2421. const bool hasSecondNewline = ParsedTokens[TokenIndex].Line != ParsedTokens[TokenIndex + opSize].Line;
  2422. const ui32 currentOutLine = OutLine;
  2423. if (currentOutLine != OutLine || hasFirstNewline || hasSecondNewline) {
  2424. NewLine();
  2425. if (!pushedIndent && CurrentIndent == ExprLineIndent) {
  2426. PushCurrentIndent();
  2427. pushedIndent = true;
  2428. }
  2429. }
  2430. Visit(op);
  2431. if (hasFirstNewline && hasSecondNewline) {
  2432. NewLine();
  2433. }
  2434. Visit(getExpr(*begin));
  2435. }
  2436. if (pushedIndent) {
  2437. PopCurrentIndent();
  2438. }
  2439. }
  2440. void PushCurrentIndent() {
  2441. CurrentIndent += OneIndent;
  2442. BlockFirstLine = OutLine;
  2443. if (OutColumn > 0) {
  2444. ++BlockFirstLine;
  2445. }
  2446. }
  2447. void PopCurrentIndent() {
  2448. CurrentIndent -= OneIndent;
  2449. }
  2450. private:
  2451. const TStaticData& StaticData;
  2452. const TParsedTokenList& ParsedTokens;
  2453. const TParsedTokenList& Comments;
  2454. const bool AnsiLexer;
  2455. TStringBuilder SB;
  2456. ui32 OutColumn = 0;
  2457. ui32 OutLine = 1;
  2458. ui32 LastLine = 0;
  2459. ui32 LastColumn = 0;
  2460. ui32 LastComment = 0;
  2461. ui32 CommentLines = 0;
  2462. i32 CurrentIndent = 0;
  2463. TVector<EScope> Scopes;
  2464. TMaybe<bool> AddLine;
  2465. TMaybe<ui32> StmtCoreAltCase;
  2466. ui64 InsideType = 0;
  2467. bool AfterNamespace = false;
  2468. bool AfterBracket = false;
  2469. bool AfterInvokeExpr = false;
  2470. bool AfterUnaryOp = false;
  2471. bool SkipSpaceAfterUnaryOp = false;
  2472. bool AfterDollarOrAt = false;
  2473. bool AfterDot = false;
  2474. bool AfterDigits = false;
  2475. bool AfterQuestion = false;
  2476. bool AfterLess = false;
  2477. bool AfterKeyExpr = false;
  2478. bool AfterComment = false;
  2479. bool InMultiTokenOp = false;
  2480. bool InCondExpr = false;
  2481. ui32 ForceExpandedLine = 0;
  2482. ui32 ForceExpandedColumn = 0;
  2483. ui32 BlockFirstLine = 1;
  2484. i32 ExprLineIndent = 0;
  2485. ui32 TokenIndex = 0;
  2486. TMarkTokenStack MarkTokenStack;
  2487. TVector<TTokenInfo> MarkedTokens;
  2488. ui64 InsideExpr = 0;
  2489. };
  2490. template <typename T>
  2491. TPrettyFunctor MakePrettyFunctor(void (TPrettyVisitor::*memberPtr)(const T& msg)) {
  2492. return [memberPtr](TPrettyVisitor& visitor, const NProtoBuf::Message& rawMsg) {
  2493. (visitor.*memberPtr)(dynamic_cast<const T&>(rawMsg));
  2494. };
  2495. }
  2496. template <typename T>
  2497. TObfuscatingFunctor MakeObfuscatingFunctor(void (TObfuscatingVisitor::*memberPtr)(const T& msg)) {
  2498. return [memberPtr](TObfuscatingVisitor& visitor, const NProtoBuf::Message& rawMsg) {
  2499. (visitor.*memberPtr)(dynamic_cast<const T&>(rawMsg));
  2500. };
  2501. }
  2502. TStaticData::TStaticData()
  2503. : Keywords(GetKeywords())
  2504. , ScopeDispatch({
  2505. {TRule_type_name::GetDescriptor(), EScope::TypeName},
  2506. {TRule_type_name_composite::GetDescriptor(), EScope::TypeName},
  2507. {TRule_double_question::GetDescriptor(), EScope::DoubleQuestion},
  2508. {TRule_id::GetDescriptor(), EScope::Identifier},
  2509. {TRule_id_or_type::GetDescriptor(), EScope::Identifier},
  2510. {TRule_id_schema::GetDescriptor(), EScope::Identifier},
  2511. {TRule_id_expr::GetDescriptor(), EScope::Identifier},
  2512. {TRule_id_expr_in::GetDescriptor(), EScope::Identifier},
  2513. {TRule_id_window::GetDescriptor(), EScope::Identifier},
  2514. {TRule_id_table::GetDescriptor(), EScope::Identifier},
  2515. {TRule_id_without::GetDescriptor(), EScope::Identifier},
  2516. {TRule_id_hint::GetDescriptor(), EScope::Identifier},
  2517. {TRule_identifier::GetDescriptor(), EScope::Identifier},
  2518. {TRule_id_table_or_type::GetDescriptor(), EScope::Identifier},
  2519. {TRule_bind_parameter::GetDescriptor(), EScope::Identifier},
  2520. {TRule_an_id_as_compat::GetDescriptor(), EScope::Identifier},
  2521. })
  2522. , PrettyVisitDispatch({
  2523. {TToken::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitToken)},
  2524. {TRule_into_values_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitIntoValuesSource)},
  2525. {TRule_select_kind::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectKind)},
  2526. {TRule_process_core::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitProcessCore)},
  2527. {TRule_reduce_core::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitReduceCore)},
  2528. {TRule_sort_specification_list::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSortSpecificationList)},
  2529. {TRule_select_core::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectCore)},
  2530. {TRule_row_pattern_recognition_clause::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitRowPatternRecognitionClause)},
  2531. {TRule_join_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitJoinSource)},
  2532. {TRule_join_constraint::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitJoinConstraint)},
  2533. {TRule_single_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSingleSource)},
  2534. {TRule_flatten_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitFlattenSource)},
  2535. {TRule_named_single_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitNamedSingleSource)},
  2536. {TRule_table_hints::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitTableHints)},
  2537. {TRule_simple_table_ref::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSimpleTableRef)},
  2538. {TRule_into_simple_table_ref::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitIntoSimpleTableRef)},
  2539. {TRule_select_kind_partial::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectKindPartial)},
  2540. {TRule_flatten_by_arg::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitFlattenByArg)},
  2541. {TRule_without_column_list::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitWithoutColumnList)},
  2542. {TRule_table_ref::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitTableRef)},
  2543. {TRule_grouping_element_list::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitGroupingElementList)},
  2544. {TRule_group_by_clause::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitGroupByClause)},
  2545. {TRule_window_definition_list::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitWindowDefinitionList)},
  2546. {TRule_window_specification::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitWindowSpecification)},
  2547. {TRule_window_partition_clause::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitWindowParitionClause)},
  2548. {TRule_lambda_body::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitLambdaBody)},
  2549. {TRule_in_atom_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitInAtomExpr)},
  2550. {TRule_select_kind_parenthesis::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectKindParenthesis)},
  2551. {TRule_cast_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCastExpr)},
  2552. {TRule_bitcast_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitBitCastExpr)},
  2553. {TRule_ext_order_by_clause::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitExtOrderByClause)},
  2554. {TRule_key_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitKeyExpr)},
  2555. {TRule_define_action_or_subquery_body::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDefineActionOrSubqueryBody)},
  2556. {TRule_exists_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitExistsExpr)},
  2557. {TRule_case_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCaseExpr)},
  2558. {TRule_with_table_settings::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitWithTableSettingsExpr)},
  2559. {TRule_table_setting_value::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitTableSettingValue)},
  2560. {TRule_ttl_tier_action::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitTtlTierAction)},
  2561. {TRule_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitExpr)},
  2562. {TRule_cond_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCondExpr)},
  2563. {TRule_or_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitOrSubexpr)},
  2564. {TRule_and_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAndSubexpr)},
  2565. {TRule_eq_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitEqSubexpr)},
  2566. {TRule_neq_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitNeqSubexpr)},
  2567. {TRule_bit_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitBitSubexpr)},
  2568. {TRule_add_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAddSubexpr)},
  2569. {TRule_mul_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitMulSubexpr)},
  2570. {TRule_rot_right::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitRotRight)},
  2571. {TRule_shift_right::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitShiftRight)},
  2572. {TRule_pragma_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitPragma)},
  2573. {TRule_select_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelect)},
  2574. {TRule_select_unparenthesized_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectUnparenthesized)},
  2575. {TRule_named_nodes_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitNamedNodes)},
  2576. {TRule_create_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateTable)},
  2577. {TRule_drop_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropTable)},
  2578. {TRule_use_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitUse)},
  2579. {TRule_into_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitIntoTable)},
  2580. {TRule_commit_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCommit)},
  2581. {TRule_update_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitUpdate)},
  2582. {TRule_delete_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDelete)},
  2583. {TRule_rollback_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitRollback)},
  2584. {TRule_declare_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDeclare)},
  2585. {TRule_import_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitImport)},
  2586. {TRule_export_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitExport)},
  2587. {TRule_alter_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterTable)},
  2588. {TRule_alter_external_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterExternalTable)},
  2589. {TRule_do_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDo)},
  2590. {TRule_define_action_or_subquery_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAction)},
  2591. {TRule_if_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitIf)},
  2592. {TRule_for_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitFor)},
  2593. {TRule_values_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitValues)},
  2594. {TRule_create_user_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateUser)},
  2595. {TRule_alter_user_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterUser)},
  2596. {TRule_create_group_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateGroup)},
  2597. {TRule_alter_group_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterGroup)},
  2598. {TRule_drop_role_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropRole)},
  2599. {TRule_upsert_object_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitUpsertObject)},
  2600. {TRule_create_object_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateObject)},
  2601. {TRule_alter_object_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterObject)},
  2602. {TRule_drop_object_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropObject)},
  2603. {TRule_create_external_data_source_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateExternalDataSource)},
  2604. {TRule_alter_external_data_source_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterExternalDataSource)},
  2605. {TRule_drop_external_data_source_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropExternalDataSource)},
  2606. {TRule_create_replication_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateAsyncReplication)},
  2607. {TRule_alter_replication_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterAsyncReplication)},
  2608. {TRule_drop_replication_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropAsyncReplication)},
  2609. {TRule_create_transfer_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateTransfer)},
  2610. {TRule_alter_transfer_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterTransfer)},
  2611. {TRule_drop_transfer_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropTransfer)},
  2612. {TRule_create_topic_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateTopic)},
  2613. {TRule_alter_topic_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterTopic)},
  2614. {TRule_drop_topic_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropTopic)},
  2615. {TRule_grant_permissions_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitGrantPermissions)},
  2616. {TRule_revoke_permissions_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitRevokePermissions)},
  2617. {TRule_alter_table_store_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterTableStore)},
  2618. {TRule_create_view_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateView)},
  2619. {TRule_drop_view_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropView)},
  2620. {TRule_create_resource_pool_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateResourcePool)},
  2621. {TRule_alter_resource_pool_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterResourcePool)},
  2622. {TRule_drop_resource_pool_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropResourcePool)},
  2623. {TRule_create_backup_collection_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateBackupCollection)},
  2624. {TRule_alter_backup_collection_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterBackupCollection)},
  2625. {TRule_drop_backup_collection_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropBackupCollection)},
  2626. {TRule_analyze_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAnalyze)},
  2627. {TRule_create_resource_pool_classifier_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateResourcePoolClassifier)},
  2628. {TRule_alter_resource_pool_classifier_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterResourcePoolClassifier)},
  2629. {TRule_drop_resource_pool_classifier_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropResourcePoolClassifier)},
  2630. {TRule_backup_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitBackup)},
  2631. {TRule_restore_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitRestore)},
  2632. {TRule_alter_sequence_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterSequence)},
  2633. {TRule_alter_database_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterDatabase)},
  2634. })
  2635. , ObfuscatingVisitDispatch({
  2636. {TToken::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitToken)},
  2637. {TRule_literal_value::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitLiteralValue)},
  2638. {TRule_pragma_value::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitPragmaValue)},
  2639. {TRule_atom_expr::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitAtomExpr)},
  2640. {TRule_in_atom_expr::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitInAtomExpr)},
  2641. {TRule_unary_casual_subexpr::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitUnaryCasualSubexpr)},
  2642. {TRule_in_unary_casual_subexpr::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitInUnaryCasualSubexpr)},
  2643. })
  2644. {
  2645. // ensure that all statements have a visitor
  2646. auto coreDescr = TRule_sql_stmt_core::GetDescriptor();
  2647. for (int i = 0; i < coreDescr->field_count(); ++i) {
  2648. const NProtoBuf::FieldDescriptor* fd = coreDescr->field(i);
  2649. if (fd->cpp_type() != NProtoBuf::FieldDescriptor::CPPTYPE_MESSAGE) {
  2650. continue;
  2651. }
  2652. auto altDescr = fd->message_type();
  2653. for (int j = 0; j < altDescr->field_count(); ++j) {
  2654. auto fd2 = altDescr->field(j);
  2655. if (fd2->cpp_type() != NProtoBuf::FieldDescriptor::CPPTYPE_MESSAGE) {
  2656. continue;
  2657. }
  2658. auto stmtMessage = fd2->message_type();
  2659. Y_ENSURE(PrettyVisitDispatch.contains(stmtMessage), TStringBuilder() << "Missing visitor for " << stmtMessage->name());
  2660. }
  2661. }
  2662. }
  2663. class TSqlFormatter : public NSQLFormat::ISqlFormatter {
  2664. public:
  2665. TSqlFormatter(const NSQLTranslation::TTranslationSettings& settings)
  2666. : Settings(settings)
  2667. {}
  2668. bool Format(const TString& query, TString& formattedQuery, NYql::TIssues& issues, EFormatMode mode) override {
  2669. formattedQuery = (mode == EFormatMode::Obfuscate) ? "" : query;
  2670. auto parsedSettings = Settings;
  2671. if (!NSQLTranslation::ParseTranslationSettings(query, parsedSettings, issues)) {
  2672. return false;
  2673. }
  2674. if (parsedSettings.PgParser) {
  2675. return mode != EFormatMode::Obfuscate;
  2676. }
  2677. if (mode == EFormatMode::Obfuscate) {
  2678. auto message = NSQLTranslationV1::SqlAST(query, parsedSettings.File, issues, NSQLTranslation::SQL_MAX_PARSER_ERRORS, parsedSettings.AnsiLexer, parsedSettings.Antlr4Parser, parsedSettings.TestAntlr4, parsedSettings.Arena);
  2679. if (!message) {
  2680. return false;
  2681. }
  2682. TObfuscatingVisitor visitor;
  2683. return Format(visitor.Process(*message), formattedQuery, issues, EFormatMode::Pretty);
  2684. }
  2685. auto lexer = NSQLTranslationV1::MakeLexer(parsedSettings.AnsiLexer, parsedSettings.Antlr4Parser);
  2686. TVector<TString> statements;
  2687. if (!NSQLTranslationV1::SplitQueryToStatements(query, lexer, statements, issues, parsedSettings.File)) {
  2688. return false;
  2689. }
  2690. TStringBuilder finalFormattedQuery;
  2691. bool prevAddLine = false;
  2692. TMaybe<ui32> prevStmtCoreAltCase;
  2693. for (const TString& currentQuery : statements) {
  2694. TVector<NSQLTranslation::TParsedToken> comments;
  2695. TParsedTokenList parsedTokens, stmtTokens;
  2696. auto onNextRawToken = [&](NSQLTranslation::TParsedToken&& token) {
  2697. stmtTokens.push_back(token);
  2698. if (token.Name == "COMMENT") {
  2699. comments.emplace_back(std::move(token));
  2700. } else if (token.Name != "WS" && token.Name != "EOF") {
  2701. parsedTokens.emplace_back(std::move(token));
  2702. }
  2703. };
  2704. if (!lexer->Tokenize(currentQuery, parsedSettings.File, onNextRawToken, issues, NSQLTranslation::SQL_MAX_PARSER_ERRORS)) {
  2705. return false;
  2706. }
  2707. NYql::TIssues parserIssues;
  2708. auto message = NSQLTranslationV1::SqlAST(currentQuery, parsedSettings.File, parserIssues, NSQLTranslation::SQL_MAX_PARSER_ERRORS, parsedSettings.AnsiLexer, parsedSettings.Antlr4Parser, parsedSettings.TestAntlr4, parsedSettings.Arena);
  2709. if (!message) {
  2710. finalFormattedQuery << currentQuery;
  2711. if (!currentQuery.EndsWith("\n")) {
  2712. finalFormattedQuery << "\n";
  2713. }
  2714. continue;
  2715. }
  2716. TPrettyVisitor visitor(parsedTokens, comments, parsedSettings.AnsiLexer);
  2717. bool addLineBefore = false;
  2718. bool addLineAfter = false;
  2719. TMaybe<ui32> stmtCoreAltCase;
  2720. auto currentFormattedQuery = visitor.Process(*message, addLineBefore, addLineAfter, stmtCoreAltCase);
  2721. TParsedTokenList stmtFormattedTokens;
  2722. auto onNextFormattedToken = [&](NSQLTranslation::TParsedToken&& token) {
  2723. stmtFormattedTokens.push_back(token);
  2724. };
  2725. if (!lexer->Tokenize(currentFormattedQuery, parsedSettings.File, onNextFormattedToken, issues, NSQLTranslation::SQL_MAX_PARSER_ERRORS)) {
  2726. return false;
  2727. }
  2728. if (!Validate(stmtTokens, stmtFormattedTokens)) {
  2729. issues.AddIssue(NYql::TIssue({}, TStringBuilder() << "Validation failed: " << currentQuery.Quote() << " != " << currentFormattedQuery.Quote()));
  2730. return false;
  2731. }
  2732. const bool differentStmtAltCase = prevStmtCoreAltCase.Defined() && stmtCoreAltCase != prevStmtCoreAltCase;
  2733. if ((addLineBefore || prevAddLine || differentStmtAltCase) && !finalFormattedQuery.empty()) {
  2734. finalFormattedQuery << "\n";
  2735. }
  2736. prevAddLine = addLineAfter;
  2737. prevStmtCoreAltCase = stmtCoreAltCase;
  2738. finalFormattedQuery << currentFormattedQuery;
  2739. if (parsedTokens.back().Name != "SEMICOLON") {
  2740. finalFormattedQuery << ";\n";
  2741. }
  2742. }
  2743. formattedQuery = finalFormattedQuery;
  2744. return true;
  2745. }
  2746. private:
  2747. const NSQLTranslation::TTranslationSettings Settings;
  2748. };
  2749. }
  2750. ISqlFormatter::TPtr MakeSqlFormatter(const NSQLTranslation::TTranslationSettings& settings) {
  2751. return ISqlFormatter::TPtr(new TSqlFormatter(settings));
  2752. }
  2753. TString MutateQuery(const TString& query, const NSQLTranslation::TTranslationSettings& settings) {
  2754. auto parsedSettings = settings;
  2755. NYql::TIssues issues;
  2756. if (!NSQLTranslation::ParseTranslationSettings(query, parsedSettings, issues)) {
  2757. throw yexception() << issues.ToString();
  2758. }
  2759. auto lexer = NSQLTranslationV1::MakeLexer(parsedSettings.AnsiLexer, parsedSettings.Antlr4Parser);
  2760. TVector<NSQLTranslation::TParsedToken> allTokens;
  2761. auto onNextToken = [&](NSQLTranslation::TParsedToken&& token) {
  2762. if (token.Name != "EOF") {
  2763. allTokens.push_back(token);
  2764. }
  2765. };
  2766. if (!lexer->Tokenize(query, parsedSettings.File, onNextToken, issues, NSQLTranslation::SQL_MAX_PARSER_ERRORS)) {
  2767. throw yexception() << issues.ToString();
  2768. }
  2769. TStringBuilder newQueryBuilder;
  2770. ui32 index = 0;
  2771. for (const auto& x : allTokens) {
  2772. newQueryBuilder << " /*" << index++ << "*/ ";
  2773. newQueryBuilder << x.Content;
  2774. }
  2775. newQueryBuilder << " /*" << index++ << "*/ ";
  2776. return newQueryBuilder;
  2777. }
  2778. bool SqlFormatSimple(const TString& query, TString& formattedQuery, TString& error) {
  2779. try {
  2780. google::protobuf::Arena arena;
  2781. NSQLTranslation::TTranslationSettings settings;
  2782. settings.Arena = &arena;
  2783. auto formatter = MakeSqlFormatter(settings);
  2784. NYql::TIssues issues;
  2785. const bool result = formatter->Format(query, formattedQuery, issues);
  2786. if (!result) {
  2787. error = issues.ToString();
  2788. }
  2789. return result;
  2790. } catch (const std::exception& e) {
  2791. error = e.what();
  2792. return false;
  2793. }
  2794. }
  2795. THashSet<TString> GetKeywords() {
  2796. TString grammar;
  2797. // ANTLR4-MIGRATION: just change SQLv1 to SQLv1Antlr4
  2798. Y_ENSURE(NResource::FindExact("SQLv1.g.in", &grammar));
  2799. THashSet<TString> res;
  2800. TVector<TString> lines;
  2801. Split(grammar, "\n", lines);
  2802. for (auto s : lines) {
  2803. s = StripString(s);
  2804. if (s.StartsWith("//")) {
  2805. continue;
  2806. }
  2807. auto pos1 = s.find(':');
  2808. auto pos2 = s.find(';');
  2809. if (pos1 == TString::npos || pos2 == TString::npos || pos2 < pos1 + 2) {
  2810. continue;
  2811. }
  2812. auto before = s.substr(0, pos1);
  2813. auto after = s.substr(pos1 + 1, pos2 - pos1 - 1);
  2814. SubstGlobal(after, " ", "");
  2815. SubstGlobal(after, "'", "");
  2816. if (after == before) {
  2817. //Cerr << before << "\n";
  2818. res.insert(before);
  2819. }
  2820. }
  2821. return res;
  2822. }
  2823. } // namespace NSQLFormat