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_TOKEN_H 17 #define ES2PANDA_PARSER_CORE_TOKEN_H 18 19 #include "lexer/token/sourceLocation.h" 20 #include "lexer/token/tokenType.h" 21 #include "lexer/token/number.h" 22 #include "macros.h" 23 #include "util/enumbitops.h" 24 #include "util/ustring.h" 25 26 namespace ark::es2panda::lexer { 27 28 using ENUMBITOPS_OPERATORS; 29 30 enum class TokenFlags : uint32_t { 31 NONE = 0U, 32 NEW_LINE = 1U << 0U, 33 HAS_ESCAPE = 1U << 2U, 34 NUMBER_BIGINT = 1U << 3U, 35 NUMBER_FLOAT = 1U << 4U, 36 NUMBER_HAS_UNDERSCORE = 1U << 5U, 37 }; 38 39 } // namespace ark::es2panda::lexer 40 41 template <> 42 struct enumbitops::IsAllowedType<ark::es2panda::lexer::TokenFlags> : std::true_type { 43 }; 44 45 namespace ark::es2panda::lexer { 46 47 class Token { 48 public: 49 Token() = default; 50 DEFAULT_COPY_SEMANTIC(Token); 51 DEFAULT_MOVE_SEMANTIC(Token); 52 ~Token() = default; 53 54 friend class Lexer; 55 friend class ETSLexer; 56 57 TokenType Type() const 58 { 59 return type_; 60 } 61 62 TokenFlags Flags() const 63 { 64 return flags_; 65 } 66 67 char16_t Utf16() const 68 { 69 return c16_; 70 } 71 72 void SetTokenType(TokenType type) 73 { 74 type_ = type; 75 } 76 77 TokenType KeywordType() const 78 { 79 return keywordType_; 80 } 81 82 const SourcePosition &Start() const 83 { 84 return loc_.start; 85 } 86 87 const SourcePosition &End() const 88 { 89 return loc_.end; 90 } 91 92 const SourceRange &Loc() const 93 { 94 return loc_; 95 } 96 97 const util::StringView &Ident() const 98 { 99 return src_; 100 } 101 102 const util::StringView &BigInt() const 103 { 104 ASSERT(type_ == TokenType::LITERAL_NUMBER && (flags_ & TokenFlags::NUMBER_BIGINT)); 105 return src_; 106 } 107 108 Number GetNumber() const 109 { 110 ASSERT(type_ == TokenType::LITERAL_NUMBER && !(flags_ & TokenFlags::NUMBER_BIGINT)); 111 return number_; 112 } 113 114 const util::StringView &String() const 115 { 116 ASSERT(type_ == TokenType::LITERAL_STRING || type_ == TokenType::LITERAL_NUMBER || 117 type_ == TokenType::LITERAL_CHAR); 118 return src_; 119 } 120 121 bool NewLine() const 122 { 123 return (flags_ & TokenFlags::NEW_LINE) != 0; 124 } 125 126 bool IsAccessability() const; 127 bool IsAsyncModifier() const; 128 bool IsForInOf() const; 129 bool IsStaticModifier() const; 130 bool IsDeclareModifier() const; 131 bool IsReadonlyModifier() const; 132 bool IsUpdate() const; 133 bool IsUnary() const; 134 bool IsPropNameLiteral() const; 135 bool IsKeyword() const; 136 bool IsReservedTypeName() const; 137 bool IsDefinableTypeName() const; 138 139 static bool IsBinaryToken(TokenType type); 140 static bool IsBinaryLvalueToken(TokenType type); 141 static bool IsUpdateToken(TokenType type); 142 static bool IsPunctuatorToken(TokenType type); 143 static bool IsTsParamToken(TokenType type); 144 145 private: 146 friend class KeywordsUtil; 147 TokenType type_ {TokenType::EOS}; 148 TokenType keywordType_ {TokenType::EOS}; 149 TokenFlags flags_ {TokenFlags::NONE}; 150 SourceRange loc_ {}; 151 Number number_ {}; 152 util::StringView src_ {}; 153 char16_t c16_ {}; 154 }; 155 } // namespace ark::es2panda::lexer 156 157 #endif /* TOKEN_H */ 158