• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 ES2PANDA_PARSER_CORE_KEYWORDS_UTIL_H
17 #define ES2PANDA_PARSER_CORE_KEYWORDS_UTIL_H
18 
19 #include "lexer/keywordString.h"
20 #include "lexer/lexer.h"
21 #include "lexer/token/letters.h"
22 #include "generated/tokenType.h"
23 #include "util/es2pandaMacros.h"
24 #include "util/ustring.h"
25 #include "utils/span.h"
26 
27 namespace ark::es2panda::lexer {
28 class Lexer;
29 class Keywords;
30 
31 class KeywordsUtil {
32 public:
KeywordsUtil(Lexer * lexer,lexer::NextTokenFlags flags)33     explicit KeywordsUtil(Lexer *lexer, lexer::NextTokenFlags flags) : lexer_(lexer), flags_(flags) {}
KeywordsUtil(Lexer * lexer,lexer::NextTokenFlags flags,char32_t cp)34     explicit KeywordsUtil(Lexer *lexer, lexer::NextTokenFlags flags, char32_t cp)
35         : lexer_(lexer), flags_(flags), cp_(cp)
36     {
37     }
38     NO_COPY_SEMANTIC(KeywordsUtil);
39     DEFAULT_MOVE_SEMANTIC(KeywordsUtil);
40     ~KeywordsUtil() = default;
41 
HasEscape()42     inline bool HasEscape() const
43     {
44         return (lexer_->GetToken().flags_ & lexer::TokenFlags::HAS_ESCAPE) != 0;
45     }
46 
GetParserContext()47     const parser::ParserContext *GetParserContext() const
48     {
49         return lexer_->parserContext_;
50     }
51 
52     void LogError(const diagnostic::DiagnosticKind &diagnostic,
53                   const util::DiagnosticMessageParams &diagnosticParams = {}) const
54     {
55         lexer_->LogError(diagnostic, diagnosticParams);
56     }
57 
LogUnexpectedStrictModeReservedKeyword()58     void LogUnexpectedStrictModeReservedKeyword() const
59     {
60         lexer_->LogUnexpectedStrictModeReservedKeyword();
61     }
62 
Flags()63     inline NextTokenFlags Flags() const
64     {
65         return flags_;
66     }
67 
KeywordToIdent()68     inline bool KeywordToIdent() const
69     {
70         return (flags_ & NextTokenFlags::KEYWORD_TO_IDENT) != 0;
71     }
72 
CheckEscapedKeyword()73     inline void CheckEscapedKeyword() const
74     {
75         if (HasEscape()) {
76             LogEscapedKeyword();
77         }
78     }
79 
LogEscapedKeyword()80     inline void LogEscapedKeyword() const
81     {
82         LogError(diagnostic::ESCAPE_SEQUENCES_IN_KEYWORD);
83     }
84 
SetKeyword(KeywordString kws)85     inline void SetKeyword(KeywordString kws) const
86     {
87         lexer_->GetToken().src_ = util::StringView(kws.Str());
88         lexer_->GetToken().type_ = kws.GetTokenType();
89         lexer_->GetToken().keywordType_ = kws.GetKeywordType();
90     }
91 
Iterator()92     inline util::StringView::Iterator &Iterator()
93     {
94         return lexer_->Iterator();
95     }
96 
97     void ScanIdentifierStart(const Keywords *kws, char32_t cp);
98     void ScanIdContinue();
99 
100     void ScanIdContinueMaybeKeyword(const Keywords *kws, Span<const KeywordString> map);
101     char32_t ScanUnicodeEscapeSequence();
102 
103     static bool IsIdentifierStart(char32_t cp);
104     static bool IsIdentifierPart(char32_t cp);
105 
106 private:
107     Lexer *lexer_;
108     NextTokenFlags flags_ {};
109     char32_t cp_ {util::StringView::Iterator::INVALID_CP};
110 };
111 
112 }  // namespace ark::es2panda::lexer
113 
114 #endif
115