1 /*
2  * Copyright (c) 2021-2024 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)25     explicit ETSLexer(const parser::ParserContext *parserContext) : Lexer(parserContext, false)
26     {
27         SkipWhiteSpaces();
28     }
29 
30     NO_COPY_SEMANTIC(ETSLexer);
31     NO_MOVE_SEMANTIC(ETSLexer);
32     ~ETSLexer() override = default;
33 
34     // NOLINTNEXTLINE(google-default-arguments)
35     void NextToken(NextTokenFlags flags = NextTokenFlags::NONE) override;
36     void ScanHashMark() override;
37     bool ScanCharLiteral() override;
38     void ScanAsteriskPunctuator() override;
39 
40     void ScanNumberLeadingZero() override
41     {
42         const auto savedLexerPosition = Save();
43 
44         bool allowBigint = false;
45         if (Iterator().Peek() == LEX_CHAR_LOWERCASE_N) {
46             // 0n is the only allowed bigint literal with leading 0
47             allowBigint = true;
48         }
49 
50         if (!ScanNumberLeadingZeroImpl<uint32_t, uint32_t>()) {
51             Rewind(savedLexerPosition);
52             if (!ScanNumberLeadingZeroImpl<uint64_t, uint64_t>()) {
53                 ThrowError("Number is too large");
54             }
55         }
56 
57         if ((GetToken().flags_ & TokenFlags::NUMBER_BIGINT) != 0) {
58             if (!allowBigint) {
59                 ThrowError("Invalid BigInt number");
60             }
61         }
62     }
63 
64     void CheckNumberLiteralEnd() override;
65     void CheckUtf16Compatible(char32_t cp) const;
66     void ConvertNumber(const std::string &utf8, NumberFlags flags) override;
67 
68 protected:
69     void ScanEqualsPunctuator() override;
70     void ScanExclamationPunctuator() override;
71     bool ScanDollarPunctuator() override;
72 };
73 }  // namespace ark::es2panda::lexer
74 
75 #endif
76