• 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_ETS_LEXER_H
17 #define ES2PANDA_PARSER_CORE_ETS_LEXER_H
18 
19 #include "lexer/lexer.h"
20 #include "token/letters.h"
21 
22 namespace ark::es2panda::lexer {
23 class ETSLexer final : public Lexer {
24 public:
ETSLexer(const parser::ParserContext * parserContext,util::DiagnosticEngine & diagnosticEngine)25     explicit ETSLexer(const parser::ParserContext *parserContext, util::DiagnosticEngine &diagnosticEngine)
26         : Lexer(parserContext, diagnosticEngine, false)
27     {
28         SkipWhiteSpaces();
29     }
30 
31     NO_COPY_SEMANTIC(ETSLexer);
32     NO_MOVE_SEMANTIC(ETSLexer);
33     ~ETSLexer() override = default;
34 
35     // NOLINTNEXTLINE(google-default-arguments)
36     void NextToken(NextTokenFlags flags = NextTokenFlags::NONE) override;
37     void ScanHashMark() override;
38     bool ScanCharLiteral() override;
39     void ScanAsteriskPunctuator() override;
40 
ScanNumberLeadingZero(bool const leadingMinus)41     void ScanNumberLeadingZero(bool const leadingMinus) override
42     {
43         const auto savedLexerPosition = Save();
44 
45         bool allowBigint = false;
46         if (Iterator().Peek() == LEX_CHAR_LOWERCASE_N) {
47             // 0n is the only allowed bigint literal with leading 0
48             allowBigint = true;
49         }
50 
51         if (!ScanNumberLeadingZeroImpl<uint32_t>(leadingMinus)) {
52             Rewind(savedLexerPosition);
53             if (!ScanNumberLeadingZeroImpl<uint64_t>(leadingMinus)) {
54                 LogError(diagnostic::TOO_LARGE_NUM);
55             }
56         }
57 
58         if ((GetToken().flags_ & TokenFlags::NUMBER_BIGINT) != 0) {
59             if (!allowBigint) {
60                 LogError(diagnostic::INVALID_BIGINT);
61             }
62         }
63     }
64 
65     void CheckNumberLiteralEnd() override;
CheckNumberLiteralEndForIdentifier()66     void CheckNumberLiteralEndForIdentifier() override
67     {
68         // don't need check in ETS
69     }
70     bool CheckUtf16Compatible(char32_t cp) const;
71     void ConvertNumber(NumberFlags flags) override;
72 
73 protected:
74     void ScanEqualsPunctuator() override;
75     void ScanExclamationPunctuator() override;
76 };
77 }  // namespace ark::es2panda::lexer
78 
79 #endif
80