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,util::ErrorLogger * errorLogger)25 explicit ETSLexer(const parser::ParserContext *parserContext, util::ErrorLogger *errorLogger) 26 : Lexer(parserContext, errorLogger, 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 LogSyntaxError("Number is too large"); 55 } 56 } 57 58 if ((GetToken().flags_ & TokenFlags::NUMBER_BIGINT) != 0) { 59 if (!allowBigint) { 60 LogSyntaxError("Invalid BigInt number"); 61 } 62 } 63 } 64 65 void CheckNumberLiteralEnd() override; 66 bool CheckUtf16Compatible(char32_t cp) const; 67 void ConvertNumber(NumberFlags flags) override; 68 69 protected: 70 void ScanEqualsPunctuator() override; 71 void ScanExclamationPunctuator() override; 72 bool ScanDollarPunctuator() override; 73 }; 74 } // namespace ark::es2panda::lexer 75 76 #endif 77