1 /* 2 * Copyright (c) 2021 - 2023 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 ES2PANDA_LEXER_KEYWORDS_BASE_H 17 #define ES2PANDA_LEXER_KEYWORDS_BASE_H 18 19 #include "lexer/keywordString.h" 20 #include "lexer/keywordsUtil.h" 21 #include "macros.h" 22 23 namespace panda::es2panda::lexer { 24 class Keywords { 25 public: Keywords(Lexer * lexer,lexer::NextTokenFlags flags)26 explicit Keywords(Lexer *lexer, lexer::NextTokenFlags flags) : util_(KeywordsUtil(lexer, flags)) {} 27 NO_COPY_SEMANTIC(Keywords); 28 NO_MOVE_SEMANTIC(Keywords); 29 ~Keywords() = default; 30 Util()31 KeywordsUtil &Util() 32 { 33 return util_; 34 } 35 Util()36 const KeywordsUtil &Util() const 37 { 38 return util_; 39 } 40 41 virtual void ScanKeyword(char32_t cp) = 0; 42 virtual void HandlePotentialEscapedKeyword(const KeywordString &kws) const = 0; 43 virtual Span<const KeywordString> KeywordMap(char32_t cp) const = 0; 44 SetKeyword(KeywordString kws)45 void SetKeyword(KeywordString kws) const 46 { 47 if ((util_.Flags() & NextTokenFlags::KEYWORD_TO_IDENT) != 0) { 48 util_.SetKeyword({kws.Str(), TokenType::LITERAL_IDENT, kws.GetKeywordType()}); 49 } else { 50 util_.CheckEscapedKeyword(); 51 util_.SetKeyword(kws); 52 } 53 } 54 55 protected: 56 template <KeywordString HANDLER(const KeywordsUtil &, std::string_view, TokenType)> SetKeyword(KeywordString kws)57 void SetKeyword(KeywordString kws) const 58 { 59 if ((util_.Flags() & NextTokenFlags::KEYWORD_TO_IDENT) != 0) { 60 util_.SetKeyword({kws.Str(), TokenType::LITERAL_IDENT, kws.GetKeywordType()}); 61 } else { 62 util_.SetKeyword(HANDLER(util_, kws.Str(), kws.GetTokenType())); 63 } 64 } 65 66 private: 67 KeywordsUtil util_; 68 }; 69 } // namespace panda::es2panda::lexer 70 #endif 71