1 /** 2 * Copyright (c) 2021-2022 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 #include "token.h" 17 18 #include "letters.h" 19 20 namespace panda::es2panda::lexer { 21 IsAccessability() const22bool Token::IsAccessability() const 23 { 24 return (type_ == TokenType::LITERAL_IDENT && 25 (keywordType_ == TokenType::KEYW_PUBLIC || keywordType_ == TokenType::KEYW_PROTECTED || 26 keywordType_ == TokenType::KEYW_PRIVATE) && 27 !(flags_ & TokenFlags::HAS_ESCAPE)); 28 } 29 IsAsyncModifier() const30bool Token::IsAsyncModifier() const 31 { 32 return (type_ == TokenType::LITERAL_IDENT && keywordType_ == TokenType::KEYW_ASYNC && 33 !(flags_ & TokenFlags::HAS_ESCAPE)); 34 } 35 IsStaticModifier() const36bool Token::IsStaticModifier() const 37 { 38 return (type_ == TokenType::LITERAL_IDENT && keywordType_ == TokenType::KEYW_STATIC && 39 !(flags_ & TokenFlags::HAS_ESCAPE)); 40 } 41 IsDeclareModifier() const42bool Token::IsDeclareModifier() const 43 { 44 return (type_ == TokenType::LITERAL_IDENT && keywordType_ == TokenType::KEYW_DECLARE && 45 !(flags_ & TokenFlags::HAS_ESCAPE)); 46 } 47 IsReadonlyModifier() const48bool Token::IsReadonlyModifier() const 49 { 50 return (type_ == TokenType::LITERAL_IDENT && keywordType_ == TokenType::KEYW_READONLY && 51 !(flags_ & TokenFlags::HAS_ESCAPE)); 52 } 53 IsUpdate() const54bool Token::IsUpdate() const 55 { 56 return (type_ == TokenType::PUNCTUATOR_MINUS_MINUS || type_ == TokenType::PUNCTUATOR_PLUS_PLUS); 57 } 58 IsUnary() const59bool Token::IsUnary() const 60 { 61 return (type_ == TokenType::PUNCTUATOR_MINUS || type_ == TokenType::PUNCTUATOR_PLUS || 62 type_ == TokenType::PUNCTUATOR_TILDE || type_ == TokenType::PUNCTUATOR_EXCLAMATION_MARK || 63 type_ == TokenType::PUNCTUATOR_PLUS_PLUS || type_ == TokenType::PUNCTUATOR_MINUS_MINUS || 64 type_ == TokenType::KEYW_TYPEOF || type_ == TokenType::KEYW_VOID || type_ == TokenType::KEYW_DELETE || 65 type_ == TokenType::KEYW_AWAIT); 66 } 67 IsPropNameLiteral() const68bool Token::IsPropNameLiteral() const 69 { 70 return (type_ == TokenType::LITERAL_STRING || type_ == TokenType::LITERAL_NUMBER || 71 type_ == TokenType::LITERAL_TRUE || type_ == TokenType::LITERAL_FALSE); 72 } 73 IsKeyword() const74bool Token::IsKeyword() const 75 { 76 return (type_ >= TokenType::FIRST_KEYW || type_ == TokenType::KEYW_IN || type_ == TokenType::KEYW_INSTANCEOF); 77 } 78 IsReservedTypeName() const79bool Token::IsReservedTypeName() const 80 { 81 switch (keywordType_) { 82 case TokenType::KEYW_ANY: 83 case TokenType::KEYW_UNKNOWN: 84 case TokenType::KEYW_NEVER: 85 case TokenType::KEYW_NUMBER: 86 case TokenType::KEYW_BIGINT: 87 case TokenType::KEYW_BOOLEAN: 88 case TokenType::KEYW_STRING: 89 case TokenType::KEYW_VOID: 90 case TokenType::KEYW_OBJECT: 91 return true; 92 default: 93 return false; 94 } 95 } 96 IsJsStrictReservedWord() const97bool Token::IsJsStrictReservedWord() const 98 { 99 switch (keywordType_) { 100 case TokenType::KEYW_ARGUMENTS: 101 case TokenType::KEYW_EVAL: 102 case TokenType::KEYW_STATIC: 103 case TokenType::KEYW_PRIVATE: 104 case TokenType::KEYW_PROTECTED: 105 case TokenType::KEYW_PUBLIC: 106 case TokenType::KEYW_IMPLEMENTS: 107 case TokenType::KEYW_INTERFACE: 108 case TokenType::KEYW_PACKAGE: 109 case TokenType::KEYW_LET: 110 case TokenType::KEYW_YIELD: 111 return true; 112 default: 113 return false; 114 } 115 } 116 IsBinaryToken(TokenType type)117bool Token::IsBinaryToken(TokenType type) 118 { 119 return (type >= TokenType::PUNCTUATOR_NULLISH_COALESCING && type <= TokenType::PUNCTUATOR_EXPONENTIATION); 120 } 121 IsBinaryLvalueToken(TokenType type)122bool Token::IsBinaryLvalueToken(TokenType type) 123 { 124 return (type >= TokenType::PUNCTUATOR_SUBSTITUTION && type <= TokenType::PUNCTUATOR_EXPONENTIATION_EQUAL); 125 } 126 IsUpdateToken(TokenType type)127bool Token::IsUpdateToken(TokenType type) 128 { 129 return (type == TokenType::PUNCTUATOR_MINUS_MINUS || type == TokenType::PUNCTUATOR_PLUS_PLUS); 130 } 131 IsPunctuatorToken(TokenType type)132bool Token::IsPunctuatorToken(TokenType type) 133 { 134 return (type >= TokenType::PUNCTUATOR_NULLISH_COALESCING && type <= TokenType::PUNCTUATOR_ARROW); 135 } 136 IsTsParamToken(TokenType type,char32_t nextChar)137bool Token::IsTsParamToken(TokenType type, char32_t nextChar) 138 { 139 return (type == TokenType::PUNCTUATOR_COLON || (type == TokenType::PUNCTUATOR_QUESTION_MARK && 140 (nextChar == LEX_CHAR_COMMA || nextChar == LEX_CHAR_COLON || nextChar == LEX_CHAR_RIGHT_PAREN))); 141 } 142 TokenToString(TokenType type)143const char *TokenToString(TokenType type) 144 { 145 switch (type) { 146 case TokenType::PUNCTUATOR_BITWISE_AND: 147 return "&"; 148 case TokenType::PUNCTUATOR_BITWISE_OR: 149 return "|"; 150 case TokenType::PUNCTUATOR_MULTIPLY: 151 return "*"; 152 case TokenType::PUNCTUATOR_DIVIDE: 153 return "/"; 154 case TokenType::PUNCTUATOR_MINUS: 155 return "-"; 156 case TokenType::PUNCTUATOR_EXCLAMATION_MARK: 157 return "!"; 158 case TokenType::PUNCTUATOR_TILDE: 159 return "~"; 160 case TokenType::PUNCTUATOR_MINUS_MINUS: 161 return "--"; 162 case TokenType::PUNCTUATOR_LEFT_SHIFT: 163 return "<<"; 164 case TokenType::PUNCTUATOR_RIGHT_SHIFT: 165 return ">>"; 166 case TokenType::PUNCTUATOR_LESS_THAN_EQUAL: 167 return "<="; 168 case TokenType::PUNCTUATOR_GREATER_THAN_EQUAL: 169 return ">="; 170 case TokenType::PUNCTUATOR_MOD: 171 return "%"; 172 case TokenType::PUNCTUATOR_BITWISE_XOR: 173 return "^"; 174 case TokenType::PUNCTUATOR_EXPONENTIATION: 175 return "**"; 176 case TokenType::PUNCTUATOR_MULTIPLY_EQUAL: 177 return "*="; 178 case TokenType::PUNCTUATOR_EXPONENTIATION_EQUAL: 179 return "**="; 180 case TokenType::PUNCTUATOR_DIVIDE_EQUAL: 181 return "/="; 182 case TokenType::PUNCTUATOR_MOD_EQUAL: 183 return "%="; 184 case TokenType::PUNCTUATOR_MINUS_EQUAL: 185 return "-="; 186 case TokenType::PUNCTUATOR_LEFT_SHIFT_EQUAL: 187 return "<<="; 188 case TokenType::PUNCTUATOR_RIGHT_SHIFT_EQUAL: 189 return ">>="; 190 case TokenType::PUNCTUATOR_UNSIGNED_RIGHT_SHIFT: 191 return ">>>"; 192 case TokenType::PUNCTUATOR_UNSIGNED_RIGHT_SHIFT_EQUAL: 193 return ">>>="; 194 case TokenType::PUNCTUATOR_BITWISE_AND_EQUAL: 195 return "&="; 196 case TokenType::PUNCTUATOR_BITWISE_OR_EQUAL: 197 return "|="; 198 case TokenType::PUNCTUATOR_LOGICAL_AND_EQUAL: 199 return "&&="; 200 case TokenType::PUNCTUATOR_NULLISH_COALESCING: 201 return "??"; 202 case TokenType::PUNCTUATOR_LOGICAL_OR_EQUAL: 203 return "||="; 204 case TokenType::PUNCTUATOR_LOGICAL_NULLISH_EQUAL: 205 return "\?\?="; 206 case TokenType::PUNCTUATOR_BITWISE_XOR_EQUAL: 207 return "^="; 208 case TokenType::PUNCTUATOR_PLUS: 209 return "+"; 210 case TokenType::PUNCTUATOR_PLUS_PLUS: 211 return "++"; 212 case TokenType::PUNCTUATOR_PLUS_EQUAL: 213 return "+="; 214 case TokenType::PUNCTUATOR_LESS_THAN: 215 return "<"; 216 case TokenType::PUNCTUATOR_GREATER_THAN: 217 return ">"; 218 case TokenType::PUNCTUATOR_EQUAL: 219 return "=="; 220 case TokenType::PUNCTUATOR_NOT_EQUAL: 221 return "!="; 222 case TokenType::PUNCTUATOR_STRICT_EQUAL: 223 return "==="; 224 case TokenType::PUNCTUATOR_NOT_STRICT_EQUAL: 225 return "!=="; 226 case TokenType::KEYW_INSTANCEOF: 227 return "instanceof"; 228 case TokenType::KEYW_IN: 229 return "in"; 230 case TokenType::PUNCTUATOR_LOGICAL_AND: 231 return "&&"; 232 case TokenType::PUNCTUATOR_LOGICAL_OR: 233 return "||"; 234 case TokenType::PUNCTUATOR_SUBSTITUTION: 235 return "="; 236 case TokenType::PUNCTUATOR_QUESTION_MARK: 237 return "?"; 238 case TokenType::PUNCTUATOR_AT: 239 return "@"; 240 case TokenType::KEYW_ANY: 241 return "any"; 242 case TokenType::KEYW_UNKNOWN: 243 return "unknown"; 244 case TokenType::KEYW_NEVER: 245 return "never"; 246 case TokenType::KEYW_NUMBER: 247 return "number"; 248 case TokenType::KEYW_BIGINT: 249 return "bigint"; 250 case TokenType::KEYW_BOOLEAN: 251 return "boolean"; 252 case TokenType::KEYW_STRING: 253 return "string"; 254 case TokenType::KEYW_VOID: 255 return "void"; 256 case TokenType::KEYW_OBJECT: 257 return "object"; 258 case TokenType::KEYW_TYPEOF: 259 return "typeof"; 260 case TokenType::KEYW_DELETE: 261 return "delete"; 262 default: 263 UNREACHABLE(); 264 } 265 } 266 267 } // namespace panda::es2panda::lexer 268