ast_nodes.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #pragma once
  2. #include <yql/essentials/public/issue/yql_issue.h>
  3. #include <library/cpp/json/json_value.h>
  4. #include <yql/essentials/minikql/jsonpath/rewrapper/re.h>
  5. namespace NYql::NJsonPath {
  6. class TRootNode;
  7. class TContextObjectNode;
  8. class TVariableNode;
  9. class TLastArrayIndexNode;
  10. class TNumberLiteralNode;
  11. class TAccessorExprNode;
  12. class TMemberAccessNode;
  13. class TWildcardMemberAccessNode;
  14. class TArrayAccessNode;
  15. class TWildcardArrayAccessNode;
  16. class TUnaryOperationNode;
  17. class TBinaryOperationNode;
  18. class TBooleanLiteralNode;
  19. class TNullLiteralNode;
  20. class TStringLiteralNode;
  21. class TFilterObjectNode;
  22. class TFilterPredicateNode;
  23. class TMethodCallNode;
  24. class TStartsWithPredicateNode;
  25. class TExistsPredicateNode;
  26. class TIsUnknownPredicateNode;
  27. class TLikeRegexPredicateNode;
  28. enum class EJsonPathMode {
  29. Lax = 0,
  30. Strict = 1,
  31. };
  32. class IAstNodeVisitor {
  33. public:
  34. virtual void VisitRoot(const TRootNode& node) = 0;
  35. virtual void VisitContextObject(const TContextObjectNode& node) = 0;
  36. virtual void VisitVariable(const TVariableNode& node) = 0;
  37. virtual void VisitLastArrayIndex(const TLastArrayIndexNode& node) = 0;
  38. virtual void VisitNumberLiteral(const TNumberLiteralNode& node) = 0;
  39. virtual void VisitMemberAccess(const TMemberAccessNode& node) = 0;
  40. virtual void VisitWildcardMemberAccess(const TWildcardMemberAccessNode& node) = 0;
  41. virtual void VisitArrayAccess(const TArrayAccessNode& node) = 0;
  42. virtual void VisitWildcardArrayAccess(const TWildcardArrayAccessNode& node) = 0;
  43. virtual void VisitUnaryOperation(const TUnaryOperationNode& node) = 0;
  44. virtual void VisitBinaryOperation(const TBinaryOperationNode& node) = 0;
  45. virtual void VisitBooleanLiteral(const TBooleanLiteralNode& node) = 0;
  46. virtual void VisitNullLiteral(const TNullLiteralNode& node) = 0;
  47. virtual void VisitStringLiteral(const TStringLiteralNode& node) = 0;
  48. virtual void VisitFilterObject(const TFilterObjectNode& node) = 0;
  49. virtual void VisitFilterPredicate(const TFilterPredicateNode& node) = 0;
  50. virtual void VisitMethodCall(const TMethodCallNode& node) = 0;
  51. virtual void VisitStartsWithPredicate(const TStartsWithPredicateNode& node) = 0;
  52. virtual void VisitExistsPredicate(const TExistsPredicateNode& node) = 0;
  53. virtual void VisitIsUnknownPredicate(const TIsUnknownPredicateNode& node) = 0;
  54. virtual void VisitLikeRegexPredicate(const TLikeRegexPredicateNode& node) = 0;
  55. virtual ~IAstNodeVisitor() = default;
  56. };
  57. enum class EReturnType {
  58. Any = 0,
  59. Bool = 1,
  60. };
  61. class TAstNode : public TSimpleRefCount<TAstNode> {
  62. public:
  63. explicit TAstNode(TPosition pos);
  64. TPosition GetPos() const;
  65. virtual void Accept(IAstNodeVisitor& visitor) const = 0;
  66. virtual EReturnType GetReturnType() const;
  67. virtual ~TAstNode() = default;
  68. private:
  69. TPosition Pos;
  70. };
  71. using TAstNodePtr = TIntrusivePtr<TAstNode>;
  72. class TRootNode : public TAstNode {
  73. public:
  74. TRootNode(TPosition pos, TAstNodePtr expr, EJsonPathMode mode);
  75. const TAstNodePtr GetExpr() const;
  76. EJsonPathMode GetMode() const;
  77. void Accept(IAstNodeVisitor& visitor) const override;
  78. EReturnType GetReturnType() const override;
  79. private:
  80. TAstNodePtr Expr;
  81. EJsonPathMode Mode;
  82. };
  83. class TContextObjectNode : public TAstNode {
  84. public:
  85. explicit TContextObjectNode(TPosition pos);
  86. void Accept(IAstNodeVisitor& visitor) const override;
  87. };
  88. class TVariableNode : public TAstNode {
  89. public:
  90. TVariableNode(TPosition pos, const TString& name);
  91. const TString& GetName() const;
  92. void Accept(IAstNodeVisitor& visitor) const override;
  93. private:
  94. TString Name;
  95. };
  96. class TLastArrayIndexNode : public TAstNode {
  97. public:
  98. explicit TLastArrayIndexNode(TPosition pos);
  99. void Accept(IAstNodeVisitor& visitor) const override;
  100. };
  101. class TNumberLiteralNode : public TAstNode {
  102. public:
  103. TNumberLiteralNode(TPosition pos, double value);
  104. double GetValue() const;
  105. void Accept(IAstNodeVisitor& visitor) const override;
  106. private:
  107. double Value;
  108. };
  109. class TMemberAccessNode : public TAstNode {
  110. public:
  111. TMemberAccessNode(TPosition pos, const TString& member, TAstNodePtr input);
  112. const TStringBuf GetMember() const;
  113. const TAstNodePtr GetInput() const;
  114. void Accept(IAstNodeVisitor& visitor) const override;
  115. private:
  116. TString Member;
  117. TAstNodePtr Input;
  118. };
  119. class TWildcardMemberAccessNode : public TAstNode {
  120. public:
  121. TWildcardMemberAccessNode(TPosition pos, TAstNodePtr input);
  122. const TAstNodePtr GetInput() const;
  123. void Accept(IAstNodeVisitor& visitor) const override;
  124. private:
  125. TAstNodePtr Input;
  126. };
  127. class TArrayAccessNode : public TAstNode {
  128. public:
  129. struct TSubscript {
  130. TAstNodePtr From;
  131. TAstNodePtr To;
  132. };
  133. TArrayAccessNode(TPosition pos, TVector<TSubscript> subscripts, TAstNodePtr input);
  134. const TVector<TSubscript>& GetSubscripts() const;
  135. const TAstNodePtr GetInput() const;
  136. void Accept(IAstNodeVisitor& visitor) const override;
  137. private:
  138. TVector<TSubscript> Subscripts;
  139. TAstNodePtr Input;
  140. };
  141. class TWildcardArrayAccessNode : public TAstNode {
  142. public:
  143. TWildcardArrayAccessNode(TPosition pos, TAstNodePtr input);
  144. const TAstNodePtr GetInput() const;
  145. void Accept(IAstNodeVisitor& visitor) const override;
  146. private:
  147. TAstNodePtr Input;
  148. };
  149. enum class EUnaryOperation {
  150. Plus = 0,
  151. Minus = 1,
  152. Not = 2,
  153. };
  154. class TUnaryOperationNode : public TAstNode {
  155. public:
  156. TUnaryOperationNode(TPosition pos, EUnaryOperation op, TAstNodePtr expr);
  157. EUnaryOperation GetOp() const;
  158. const TAstNodePtr GetExpr() const;
  159. void Accept(IAstNodeVisitor& visitor) const override;
  160. EReturnType GetReturnType() const override;
  161. private:
  162. EUnaryOperation Operation;
  163. TAstNodePtr Expr;
  164. };
  165. enum class EBinaryOperation {
  166. Add = 0,
  167. Substract = 1,
  168. Multiply = 2,
  169. Divide = 3,
  170. Modulo = 4,
  171. Less = 5,
  172. LessEqual = 6,
  173. Greater = 7,
  174. GreaterEqual = 8,
  175. Equal = 9,
  176. NotEqual = 10,
  177. And = 11,
  178. Or = 12,
  179. };
  180. class TBinaryOperationNode : public TAstNode {
  181. public:
  182. TBinaryOperationNode(TPosition pos, EBinaryOperation op, TAstNodePtr leftExpr, TAstNodePtr rightExpr);
  183. EBinaryOperation GetOp() const;
  184. const TAstNodePtr GetLeftExpr() const;
  185. const TAstNodePtr GetRightExpr() const;
  186. void Accept(IAstNodeVisitor& visitor) const override;
  187. EReturnType GetReturnType() const override;
  188. private:
  189. EBinaryOperation Operation;
  190. TAstNodePtr LeftExpr;
  191. TAstNodePtr RightExpr;
  192. };
  193. class TBooleanLiteralNode : public TAstNode {
  194. public:
  195. TBooleanLiteralNode(TPosition pos, bool value);
  196. bool GetValue() const;
  197. void Accept(IAstNodeVisitor& visitor) const override;
  198. private:
  199. bool Value;
  200. };
  201. class TNullLiteralNode : public TAstNode {
  202. public:
  203. explicit TNullLiteralNode(TPosition pos);
  204. void Accept(IAstNodeVisitor& visitor) const override;
  205. };
  206. class TStringLiteralNode : public TAstNode {
  207. public:
  208. TStringLiteralNode(TPosition pos, const TString& value);
  209. const TString& GetValue() const;
  210. void Accept(IAstNodeVisitor& visitor) const override;
  211. private:
  212. TString Value;
  213. };
  214. class TFilterObjectNode : public TAstNode {
  215. public:
  216. explicit TFilterObjectNode(TPosition pos);
  217. void Accept(IAstNodeVisitor& visitor) const override;
  218. };
  219. class TFilterPredicateNode : public TAstNode {
  220. public:
  221. TFilterPredicateNode(TPosition pos, TAstNodePtr predicate, TAstNodePtr input);
  222. const TAstNodePtr GetPredicate() const;
  223. const TAstNodePtr GetInput() const;
  224. void Accept(IAstNodeVisitor& visitor) const override;
  225. private:
  226. TAstNodePtr Predicate;
  227. TAstNodePtr Input;
  228. };
  229. enum class EMethodType {
  230. Abs = 0,
  231. Floor = 1,
  232. Ceiling = 2,
  233. Double = 3,
  234. Type = 4,
  235. Size = 5,
  236. KeyValue = 6,
  237. };
  238. class TMethodCallNode : public TAstNode {
  239. public:
  240. TMethodCallNode(TPosition pos, EMethodType type, TAstNodePtr input);
  241. EMethodType GetType() const;
  242. const TAstNodePtr GetInput() const;
  243. void Accept(IAstNodeVisitor& visitor) const override;
  244. private:
  245. EMethodType Type;
  246. TAstNodePtr Input;
  247. };
  248. class TStartsWithPredicateNode : public TAstNode {
  249. public:
  250. TStartsWithPredicateNode(TPosition pos, TAstNodePtr input, TAstNodePtr prefix);
  251. const TAstNodePtr GetInput() const;
  252. const TAstNodePtr GetPrefix() const;
  253. EReturnType GetReturnType() const override;
  254. void Accept(IAstNodeVisitor& visitor) const override;
  255. private:
  256. TAstNodePtr Input;
  257. TAstNodePtr Prefix;
  258. };
  259. class TExistsPredicateNode : public TAstNode {
  260. public:
  261. TExistsPredicateNode(TPosition pos, TAstNodePtr input);
  262. const TAstNodePtr GetInput() const;
  263. EReturnType GetReturnType() const override;
  264. void Accept(IAstNodeVisitor& visitor) const override;
  265. private:
  266. TAstNodePtr Input;
  267. };
  268. class TIsUnknownPredicateNode : public TAstNode {
  269. public:
  270. TIsUnknownPredicateNode(TPosition pos, TAstNodePtr input);
  271. const TAstNodePtr GetInput() const;
  272. EReturnType GetReturnType() const override;
  273. void Accept(IAstNodeVisitor& visitor) const override;
  274. private:
  275. TAstNodePtr Input;
  276. };
  277. class TLikeRegexPredicateNode : public TAstNode {
  278. public:
  279. TLikeRegexPredicateNode(TPosition pos, TAstNodePtr input, NReWrapper::IRePtr&& regex);
  280. const TAstNodePtr GetInput() const;
  281. const NReWrapper::IRePtr& GetRegex() const;
  282. EReturnType GetReturnType() const override;
  283. void Accept(IAstNodeVisitor& visitor) const override;
  284. private:
  285. TAstNodePtr Input;
  286. NReWrapper::IRePtr Regex;
  287. };
  288. }