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