• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     void SetTokenStr(util::StringView error)
78     {
79         src_ = error;
80     }
81 
82     TokenType KeywordType() const
83     {
84         return keywordType_;
85     }
86 
87     const SourcePosition &Start() const
88     {
89         return loc_.start;
90     }
91 
92     const SourcePosition &End() const
93     {
94         return loc_.end;
95     }
96 
97     const SourceRange &Loc() const
98     {
99         return loc_;
100     }
101 
102     const util::StringView &Ident() const
103     {
104         return src_;
105     }
106 
107     const util::StringView &BigInt() const
108     {
109         ASSERT(type_ == TokenType::LITERAL_NUMBER && (flags_ & TokenFlags::NUMBER_BIGINT));
110         return src_;
111     }
112 
113     Number GetNumber() const
114     {
115         ASSERT(type_ == TokenType::LITERAL_NUMBER && !(flags_ & TokenFlags::NUMBER_BIGINT));
116         return number_;
117     }
118 
119     const util::StringView &String() const
120     {
121         ASSERT(type_ == TokenType::LITERAL_STRING || type_ == TokenType::LITERAL_NUMBER ||
122                type_ == TokenType::LITERAL_CHAR);
123         return src_;
124     }
125 
126     bool NewLine() const
127     {
128         return (flags_ & TokenFlags::NEW_LINE) != 0;
129     }
130 
131     bool IsAccessability() const;
132     bool IsAsyncModifier() const;
133     bool IsForInOf() const;
134     bool IsStaticModifier() const;
135     bool IsDeclareModifier() const;
136     bool IsReadonlyModifier() const;
137     bool IsUpdate() const;
138     bool IsUnary() const;
139     bool IsPropNameLiteral() const;
140     bool IsKeyword() const;
141     bool IsReservedTypeName() const;
142     bool IsDefinableTypeName() const;
143 
144     static bool IsBinaryToken(TokenType type);
145     static bool IsBinaryLvalueToken(TokenType type);
146     static bool IsUpdateToken(TokenType type);
147     static bool IsPunctuatorToken(TokenType type);
148     static bool IsTsParamToken(TokenType type);
149 
150 private:
151     friend class KeywordsUtil;
152     TokenType type_ {TokenType::EOS};
153     TokenType keywordType_ {TokenType::EOS};
154     TokenFlags flags_ {TokenFlags::NONE};
155     SourceRange loc_ {};
156     Number number_ {};
157     util::StringView src_ {};
158     char16_t c16_ {};
159 };
160 }  // namespace ark::es2panda::lexer
161 
162 #endif /* TOKEN_H */
163