1 // Copyright (C) 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ICING_QUERY_ADVANCED_QUERY_PARSER_ABSTRACT_SYNTAX_TREE_H_ 16 #define ICING_QUERY_ADVANCED_QUERY_PARSER_ABSTRACT_SYNTAX_TREE_H_ 17 18 #include <memory> 19 #include <string> 20 #include <string_view> 21 #include <utility> 22 #include <vector> 23 24 namespace icing { 25 namespace lib { 26 27 class StringNode; 28 class TextNode; 29 class MemberNode; 30 class FunctionNode; 31 class UnaryOperatorNode; 32 class NaryOperatorNode; 33 34 class AbstractSyntaxTreeVisitor { 35 public: 36 virtual ~AbstractSyntaxTreeVisitor() = default; 37 38 virtual void VisitString(const StringNode* node) = 0; 39 virtual void VisitText(const TextNode* node) = 0; 40 virtual void VisitMember(const MemberNode* node) = 0; 41 virtual void VisitFunction(const FunctionNode* node) = 0; 42 virtual void VisitUnaryOperator(const UnaryOperatorNode* node) = 0; 43 virtual void VisitNaryOperator(const NaryOperatorNode* node) = 0; 44 }; 45 46 class Node { 47 public: 48 virtual ~Node() = default; 49 virtual void Accept(AbstractSyntaxTreeVisitor* visitor) const = 0; 50 }; 51 52 class StringNode : public Node { 53 public: 54 explicit StringNode(std::string value, std::string_view raw_value, 55 bool is_prefix = false) value_(std::move (value))56 : value_(std::move(value)), 57 raw_value_(raw_value), 58 is_prefix_(is_prefix) {} 59 value()60 const std::string& value() const& { return value_; } value()61 std::string value() && { return std::move(value_); } 62 is_prefix()63 bool is_prefix() const { return is_prefix_; } 64 raw_value()65 std::string_view raw_value() const { return raw_value_; } 66 Accept(AbstractSyntaxTreeVisitor * visitor)67 void Accept(AbstractSyntaxTreeVisitor* visitor) const override { 68 visitor->VisitString(this); 69 } 70 71 private: 72 std::string value_; 73 std::string_view raw_value_; 74 bool is_prefix_; 75 }; 76 77 class TextNode : public Node { 78 public: 79 explicit TextNode(std::string value, std::string_view raw_value, 80 bool is_prefix = false) value_(std::move (value))81 : value_(std::move(value)), 82 raw_value_(raw_value), 83 is_prefix_(is_prefix) {} 84 value()85 const std::string& value() const& { return value_; } value()86 std::string value() && { return std::move(value_); } 87 is_prefix()88 bool is_prefix() const { return is_prefix_; } 89 raw_value()90 std::string_view raw_value() const { return raw_value_; } 91 Accept(AbstractSyntaxTreeVisitor * visitor)92 void Accept(AbstractSyntaxTreeVisitor* visitor) const override { 93 visitor->VisitText(this); 94 } 95 96 private: 97 std::string value_; 98 std::string_view raw_value_; 99 bool is_prefix_; 100 }; 101 102 class MemberNode : public Node { 103 public: MemberNode(std::vector<std::unique_ptr<TextNode>> children,std::unique_ptr<FunctionNode> function)104 explicit MemberNode(std::vector<std::unique_ptr<TextNode>> children, 105 std::unique_ptr<FunctionNode> function) 106 : children_(std::move(children)), function_(std::move(function)) {} 107 Accept(AbstractSyntaxTreeVisitor * visitor)108 void Accept(AbstractSyntaxTreeVisitor* visitor) const override { 109 visitor->VisitMember(this); 110 } children()111 const std::vector<std::unique_ptr<TextNode>>& children() const { 112 return children_; 113 } function()114 const FunctionNode* function() const { return function_.get(); } 115 116 private: 117 std::vector<std::unique_ptr<TextNode>> children_; 118 // This is nullable. When it is not nullptr, this class will represent a 119 // function call. 120 std::unique_ptr<FunctionNode> function_; 121 }; 122 123 class FunctionNode : public Node { 124 public: FunctionNode(std::string function_name)125 explicit FunctionNode(std::string function_name) 126 : FunctionNode(std::move(function_name), {}) {} FunctionNode(std::string function_name,std::vector<std::unique_ptr<Node>> args)127 explicit FunctionNode(std::string function_name, 128 std::vector<std::unique_ptr<Node>> args) 129 : function_name_(std::move(function_name)), 130 args_(std::move(args)) {} 131 Accept(AbstractSyntaxTreeVisitor * visitor)132 void Accept(AbstractSyntaxTreeVisitor* visitor) const override { 133 visitor->VisitFunction(this); 134 } function_name()135 const std::string& function_name() const { return function_name_; } args()136 const std::vector<std::unique_ptr<Node>>& args() const { return args_; } 137 138 private: 139 std::string function_name_; 140 std::vector<std::unique_ptr<Node>> args_; 141 }; 142 143 class UnaryOperatorNode : public Node { 144 public: UnaryOperatorNode(std::string operator_text,std::unique_ptr<Node> child)145 explicit UnaryOperatorNode(std::string operator_text, 146 std::unique_ptr<Node> child) 147 : operator_text_(std::move(operator_text)), child_(std::move(child)) {} 148 Accept(AbstractSyntaxTreeVisitor * visitor)149 void Accept(AbstractSyntaxTreeVisitor* visitor) const override { 150 visitor->VisitUnaryOperator(this); 151 } operator_text()152 const std::string& operator_text() const { return operator_text_; } child()153 const Node* child() const { return child_.get(); } 154 155 private: 156 std::string operator_text_; 157 std::unique_ptr<Node> child_; 158 }; 159 160 class NaryOperatorNode : public Node { 161 public: NaryOperatorNode(std::string operator_text,std::vector<std::unique_ptr<Node>> children)162 explicit NaryOperatorNode(std::string operator_text, 163 std::vector<std::unique_ptr<Node>> children) 164 : operator_text_(std::move(operator_text)), 165 children_(std::move(children)) {} 166 Accept(AbstractSyntaxTreeVisitor * visitor)167 void Accept(AbstractSyntaxTreeVisitor* visitor) const override { 168 visitor->VisitNaryOperator(this); 169 } operator_text()170 const std::string& operator_text() const { return operator_text_; } children()171 const std::vector<std::unique_ptr<Node>>& children() const { 172 return children_; 173 } 174 175 private: 176 std::string operator_text_; 177 std::vector<std::unique_ptr<Node>> children_; 178 }; 179 180 } // namespace lib 181 } // namespace icing 182 183 #endif // ICING_QUERY_ADVANCED_QUERY_PARSER_ABSTRACT_SYNTAX_TREE_H_ 184