• 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 #include "token.h"
17 
18 namespace ark::es2panda::lexer {
19 
20 #include "generated/token.inl"
21 
IsAccessability() const22 bool Token::IsAccessability() const
23 {
24     return (type_ == TokenType::LITERAL_IDENT &&
25             (keywordType_ == TokenType::KEYW_PUBLIC || keywordType_ == TokenType::KEYW_PROTECTED ||
26              keywordType_ == TokenType::KEYW_PRIVATE || keywordType_ == TokenType::KEYW_INTERNAL) &&
27             ((flags_ & TokenFlags::HAS_ESCAPE) == 0));
28 }
29 
IsAsyncModifier() const30 bool Token::IsAsyncModifier() const
31 {
32     return (type_ == TokenType::LITERAL_IDENT && keywordType_ == TokenType::KEYW_ASYNC &&
33             (flags_ & TokenFlags::HAS_ESCAPE) == 0);
34 }
35 
IsForInOf() const36 bool Token::IsForInOf() const
37 {
38     return (keywordType_ == TokenType::KEYW_IN || keywordType_ == TokenType::KEYW_OF);
39 }
40 
IsStaticModifier() const41 bool Token::IsStaticModifier() const
42 {
43     return (type_ == TokenType::LITERAL_IDENT && keywordType_ == TokenType::KEYW_STATIC &&
44             (flags_ & TokenFlags::HAS_ESCAPE) == 0);
45 }
46 
IsDeclareModifier() const47 bool Token::IsDeclareModifier() const
48 {
49     return (type_ == TokenType::LITERAL_IDENT && keywordType_ == TokenType::KEYW_DECLARE &&
50             (flags_ & TokenFlags::HAS_ESCAPE) == 0);
51 }
52 
IsReadonlyModifier() const53 bool Token::IsReadonlyModifier() const
54 {
55     return (type_ == TokenType::LITERAL_IDENT && keywordType_ == TokenType::KEYW_READONLY &&
56             (flags_ & TokenFlags::HAS_ESCAPE) == 0);
57 }
58 
IsUpdate() const59 bool Token::IsUpdate() const
60 {
61     return (type_ == TokenType::PUNCTUATOR_MINUS_MINUS || type_ == TokenType::PUNCTUATOR_PLUS_PLUS);
62 }
63 
IsPropNameLiteral() const64 bool Token::IsPropNameLiteral() const noexcept
65 {
66     return (type_ == TokenType::LITERAL_STRING || type_ == TokenType::LITERAL_NUMBER ||
67             type_ == TokenType::LITERAL_TRUE || type_ == TokenType::LITERAL_FALSE);
68 }
69 
IsLiteral() const70 bool Token::IsLiteral() const noexcept
71 {
72     return (type_ == TokenType::LITERAL_IDENT || IsPropNameLiteral() || type_ == TokenType::LITERAL_CHAR ||
73             type_ == TokenType::LITERAL_REGEXP || type_ == TokenType::LITERAL_NULL);
74 }
75 
ToString() const76 std::string_view Token::ToString() const noexcept
77 {
78     if (!IsLiteral()) {
79         return TokenToString(type_);
80     }
81 
82     auto const str = Ident().Utf8();
83     if (!str.empty() && (str[0U] == '\n')) {
84         return "end of stream";
85     }
86 
87     return str;
88 }
89 
IsKeyword() const90 bool Token::IsKeyword() const
91 {
92     return (type_ >= TokenType::FIRST_KEYW);
93 }
94 
IsUpdateToken(TokenType type)95 bool Token::IsUpdateToken(TokenType type)
96 {
97     return (type == TokenType::PUNCTUATOR_MINUS_MINUS || type == TokenType::PUNCTUATOR_PLUS_PLUS);
98 }
99 
IsPunctuatorToken(TokenType type)100 bool Token::IsPunctuatorToken(TokenType type)
101 {
102     return type >= TokenType::FIRST_PUNCTUATOR && type < TokenType::FIRST_KEYW;
103 }
104 
IsTsParamToken(TokenType type)105 bool Token::IsTsParamToken(TokenType type)
106 {
107     return (type == TokenType::PUNCTUATOR_COLON || type == TokenType::PUNCTUATOR_QUESTION_MARK);
108 }
109 
110 }  // namespace ark::es2panda::lexer
111