1 /** 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 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 16 #ifndef RULE_H 17 #define RULE_H 18 19 #include <functional> 20 #include <vector> 21 #include "ir/astNode.h" 22 #include "formatting_context.h" 23 24 namespace ark::es2panda::lsp { 25 26 using ContextPredicate = std::function<bool(FormattingContext *)>; 27 28 enum class RuleAction : uint16_t { 29 NONE = 0U, 30 STOP_PROCESSING_SPACE_ACTIONS = 1U << 0U, 31 STOP_PROCESSING_TOKEN_ACTIONS = 1U << 1U, 32 INSERT_SPACE = 1U << 2U, 33 INSERT_NEWLINE = 1U << 3U, 34 DELETE_SPACE = 1U << 4U, 35 DELETE_TOKEN = 1U << 5U, 36 INSERT_TRAILING_SEMICOLON = 1U << 6U 37 }; 38 39 enum class RuleFlags { NONE, CAN_DELETE_NEWLINES }; 40 41 struct Rule { 42 public: RuleRule43 explicit Rule(std::vector<ContextPredicate> &cb, RuleAction action, RuleFlags flag) 44 : context_(std::move(cb)), action_(action), flags_(flag) 45 { 46 } 47 GetContextRule48 std::vector<ContextPredicate> &GetContext() 49 { 50 return context_; 51 } 52 GetRuleActionRule53 RuleAction GetRuleAction() 54 { 55 return action_; 56 } 57 GetRuleFlagsRule58 RuleFlags GetRuleFlags() 59 { 60 return flags_; 61 } 62 63 private: 64 std::vector<ContextPredicate> context_; 65 RuleAction action_; 66 RuleFlags flags_; 67 }; 68 69 struct TokenRange { 70 public: TokenRangeTokenRange71 explicit TokenRange(std::vector<ir::AstNodeType> &tokens, bool isSpecific) 72 : tokens_(std::move(tokens)), isSpecific_(isSpecific) 73 { 74 } 75 GetTokensTokenRange76 std::vector<ir::AstNodeType> &GetTokens() 77 { 78 return tokens_; 79 } 80 GetIsSpecifierTokenRange81 bool GetIsSpecifier() 82 { 83 return isSpecific_; 84 } 85 86 private: 87 std::vector<ir::AstNodeType> tokens_; 88 bool isSpecific_; 89 }; 90 91 } // namespace ark::es2panda::lsp 92 93 #endif