yql_visit.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #include <yql/essentials/core/yql_graph_transformer.h>
  3. #include <yql/essentials/core/expr_nodes/yql_expr_nodes.h>
  4. #include <yql/essentials/ast/yql_expr.h>
  5. #include <util/generic/hash.h>
  6. #include <util/generic/strbuf.h>
  7. #include <functional>
  8. #include <initializer_list>
  9. namespace NYql {
  10. class TVisitorTransformerBase: public TSyncTransformerBase {
  11. public:
  12. using THandler = std::function<TStatus(const TExprNode::TPtr&, TExprNode::TPtr&, TExprContext&)>;
  13. TVisitorTransformerBase(bool failOnUnknown)
  14. : FailOnUnknown(failOnUnknown)
  15. {
  16. }
  17. TStatus DoTransform(TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) final;
  18. void Rewind() final {
  19. }
  20. bool CanParse(const TExprNode& node) const {
  21. return Handlers.contains(node.Content());
  22. }
  23. protected:
  24. void AddHandler(std::initializer_list<TStringBuf> names, THandler handler);
  25. template <class TDerived>
  26. THandler Hndl(TStatus(TDerived::* handler)(const TExprNode::TPtr&, TExprNode::TPtr&, TExprContext&)) {
  27. return [this, handler] (TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) {
  28. return (static_cast<TDerived*>(this)->*handler)(input, output, ctx);
  29. };
  30. }
  31. template <class TDerived>
  32. THandler Hndl(TStatus(TDerived::* handler)(const TExprNode::TPtr&, TExprContext&)) {
  33. return [this, handler] (TExprNode::TPtr input, TExprNode::TPtr& /*output*/, TExprContext& ctx) {
  34. return (static_cast<TDerived*>(this)->*handler)(input, ctx);
  35. };
  36. }
  37. template <class TDerived>
  38. THandler Hndl(TStatus(TDerived::* handler)(NNodes::TExprBase, TExprContext&)) {
  39. return [this, handler] (TExprNode::TPtr input, TExprNode::TPtr& /*output*/, TExprContext& ctx) {
  40. return (static_cast<TDerived*>(this)->*handler)(NNodes::TExprBase(input), ctx);
  41. };
  42. }
  43. THandler Hndl(TStatus(*handler)(const TExprNode::TPtr&, TExprContext&)) {
  44. return [handler] (TExprNode::TPtr input, TExprNode::TPtr& /*output*/, TExprContext& ctx) {
  45. return handler(input, ctx);
  46. };
  47. }
  48. THandler Hndl(TStatus(*handler)(NNodes::TExprBase, TExprContext&)) {
  49. return [handler] (TExprNode::TPtr input, TExprNode::TPtr& /*output*/, TExprContext& ctx) {
  50. return handler(NNodes::TExprBase(input), ctx);
  51. };
  52. }
  53. protected:
  54. const bool FailOnUnknown;
  55. THashMap<TStringBuf, THandler> Handlers;
  56. };
  57. } // NYql