yql_expr_builder.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #pragma once
  2. #include "yql_ast.h"
  3. #include "yql_errors.h"
  4. #include "yql_pos_handle.h"
  5. #include <functional>
  6. namespace NYql {
  7. struct TExprContext;
  8. class TExprNode;
  9. typedef TIntrusivePtr<TExprNode> TExprNodePtr;
  10. typedef std::vector<TExprNodePtr> TExprNodeList;
  11. class TExprNodeReplaceBuilder;
  12. class TExprNodeBuilder {
  13. friend class TExprNodeReplaceBuilder;
  14. public:
  15. typedef std::function<TExprNodePtr(const TStringBuf&)> ExtArgsFuncType;
  16. public:
  17. TExprNodeBuilder(TPositionHandle pos, TExprContext& ctx);
  18. TExprNodeBuilder(TPositionHandle pos, TExprContext& ctx, ExtArgsFuncType extArgsFunc);
  19. TExprNodePtr Build();
  20. TExprNodeBuilder& Seal();
  21. TExprNodeReplaceBuilder& Done();
  22. // Indexed version of methods must be used inside of Callable or List, otherwise
  23. // non-indexed version must be used (at root or as lambda body)
  24. TExprNodeBuilder& Atom(ui32 index, TPositionHandle pos, const TStringBuf& content, ui32 flags = TNodeFlags::ArbitraryContent);
  25. TExprNodeBuilder& Atom(TPositionHandle pos, const TStringBuf& content, ui32 flags = TNodeFlags::ArbitraryContent);
  26. TExprNodeBuilder& Atom(ui32 index, const TStringBuf& content, ui32 flags = TNodeFlags::ArbitraryContent);
  27. TExprNodeBuilder& Atom(const TStringBuf& content, ui32 flags = TNodeFlags::ArbitraryContent);
  28. TExprNodeBuilder& Atom(ui32 index, ui32 literalIndexValue);
  29. TExprNodeBuilder& Atom(ui32 literalIndexValue);
  30. TExprNodeBuilder List(ui32 index, TPositionHandle pos);
  31. TExprNodeBuilder List(TPositionHandle pos);
  32. TExprNodeBuilder List(ui32 index);
  33. TExprNodeBuilder List();
  34. TExprNodeBuilder& Add(ui32 index, TExprNodePtr&& child);
  35. TExprNodeBuilder& Add(ui32 index, const TExprNodePtr& child);
  36. TExprNodeBuilder& Add(TExprNodeList&& children);
  37. // only for lambda bodies
  38. TExprNodeBuilder& Set(TExprNodePtr&& body);
  39. TExprNodeBuilder& Set(const TExprNodePtr& body);
  40. TExprNodeBuilder Callable(ui32 index, TPositionHandle pos, const TStringBuf& content);
  41. TExprNodeBuilder Callable(TPositionHandle pos, const TStringBuf& content);
  42. TExprNodeBuilder Callable(ui32 index, const TStringBuf& content);
  43. TExprNodeBuilder Callable(const TStringBuf& content);
  44. TExprNodeBuilder& World(ui32 index, TPositionHandle pos);
  45. TExprNodeBuilder& World(TPositionHandle pos);
  46. TExprNodeBuilder& World(ui32 index);
  47. TExprNodeBuilder& World();
  48. TExprNodeBuilder Lambda(ui32 index, TPositionHandle pos);
  49. TExprNodeBuilder Lambda(TPositionHandle pos);
  50. TExprNodeBuilder Lambda(ui32 index);
  51. TExprNodeBuilder Lambda();
  52. TExprNodeBuilder& Param(TPositionHandle pos, const TStringBuf& name);
  53. TExprNodeBuilder& Param(const TStringBuf& name);
  54. TExprNodeBuilder& Params(const TStringBuf& name, ui32 width);
  55. TExprNodeBuilder& Arg(ui32 index, const TStringBuf& name);
  56. TExprNodeBuilder& Arg(const TStringBuf& name);
  57. TExprNodeBuilder& Arg(ui32 index, const TStringBuf& name, ui32 toIndex);
  58. TExprNodeBuilder& Arg(const TStringBuf& name, ui32 toIndex);
  59. TExprNodeBuilder& Arg(const TExprNodePtr& arg);
  60. TExprNodeBuilder& Args(ui32 index, const TStringBuf& name, ui32 toIndex);
  61. TExprNodeBuilder& Args(const TStringBuf& name, ui32 toIndex);
  62. TExprNodeReplaceBuilder Apply(ui32 index, const TExprNode& lambda);
  63. TExprNodeReplaceBuilder Apply(ui32 index, const TExprNodePtr& lambda);
  64. TExprNodeReplaceBuilder Apply(const TExprNode& lambda);
  65. TExprNodeReplaceBuilder Apply(const TExprNodePtr& lambda);
  66. TExprNodeReplaceBuilder ApplyPartial(ui32 index, TExprNodePtr args, TExprNodePtr body);
  67. TExprNodeReplaceBuilder ApplyPartial(ui32 index, TExprNodePtr args, TExprNodeList body);
  68. TExprNodeReplaceBuilder ApplyPartial(TExprNodePtr args, TExprNodePtr body);
  69. TExprNodeReplaceBuilder ApplyPartial(TExprNodePtr args, TExprNodeList body);
  70. template <typename TFunc>
  71. TExprNodeBuilder& Do(const TFunc& func) {
  72. return func(*this);
  73. }
  74. private:
  75. TExprNodeBuilder(TPositionHandle pos, TExprNodeBuilder* parent, const TExprNodePtr& container);
  76. TExprNodeBuilder(TPositionHandle pos, TExprNodeReplaceBuilder* parentReplacer);
  77. TExprNodePtr FindArgument(const TStringBuf& name);
  78. private:
  79. TExprContext& Ctx;
  80. TExprNodeBuilder* Parent;
  81. TExprNodeReplaceBuilder* ParentReplacer;
  82. TExprNodePtr Container;
  83. TPositionHandle Pos;
  84. TExprNodePtr CurrentNode;
  85. ExtArgsFuncType ExtArgsFunc;
  86. };
  87. namespace NNodes {
  88. template<typename TParent, typename TNode>
  89. class TNodeBuilder;
  90. }
  91. class TExprNodeReplaceBuilder {
  92. friend class TExprNodeBuilder;
  93. private:
  94. struct TBuildAdapter {
  95. typedef TExprNodeReplaceBuilder& ResultType;
  96. TBuildAdapter(TExprNodeReplaceBuilder& builder)
  97. : Builder(builder) {}
  98. ResultType Value() {
  99. return Builder;
  100. }
  101. TExprNodeReplaceBuilder& Builder;
  102. };
  103. public:
  104. TExprNodeReplaceBuilder(TExprNodeBuilder* owner, TExprNodePtr container, const TExprNode& lambda);
  105. TExprNodeReplaceBuilder(TExprNodeBuilder* owner, TExprNodePtr container, TExprNodePtr&& args, TExprNodePtr&& body);
  106. TExprNodeReplaceBuilder(TExprNodeBuilder* owner, TExprNodePtr container, TExprNodePtr&& args, TExprNodeList&& body);
  107. TExprNodeReplaceBuilder& With(ui32 argIndex, const TStringBuf& toName);
  108. TExprNodeReplaceBuilder& With(ui32 argIndex, const TStringBuf& toName, ui32 toIndex);
  109. TExprNodeReplaceBuilder& With(ui32 argIndex, TExprNodePtr toNode);
  110. TExprNodeReplaceBuilder& With(const TStringBuf& toName);
  111. TExprNodeReplaceBuilder& With(const TStringBuf& toName, ui32 toIndex);
  112. TExprNodeReplaceBuilder& WithNode(const TExprNode& fromNode, TExprNodePtr&& toNode);
  113. TExprNodeReplaceBuilder& WithNode(const TExprNode& fromNode, const TStringBuf& toName);
  114. TExprNodeBuilder With(ui32 argIndex);
  115. TExprNodeBuilder WithNode(TExprNodePtr&& fromNode);
  116. template<typename TNode>
  117. NNodes::TNodeBuilder<TBuildAdapter, TNode> With(ui32 argIndex) {
  118. TBuildAdapter adapter(*this);
  119. NNodes::TNodeBuilder<TBuildAdapter, TNode> builder(Owner->Ctx, Owner->Pos,
  120. [adapter, argIndex](const TNode& node) mutable -> TBuildAdapter& {
  121. adapter.Builder = adapter.Builder.With(argIndex, node.Get());
  122. return adapter;
  123. },
  124. [adapter] (const TStringBuf& argName) {
  125. return adapter.Builder.Owner->FindArgument(argName);
  126. });
  127. return builder;
  128. }
  129. TExprNodeBuilder& Seal();
  130. template <typename TFunc>
  131. TExprNodeReplaceBuilder& Do(const TFunc& func) {
  132. return func(*this);
  133. }
  134. private:
  135. TExprNodeBuilder* Owner;
  136. TExprNodePtr Container;
  137. TExprNodePtr Args;
  138. TExprNodeList Body;
  139. ui32 CurrentIndex;
  140. TExprNodePtr CurrentNode;
  141. };
  142. } // namespace NYql