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 <macros.h>
22 #include <util/enumbitops.h>
23 #include <util/ustring.h>
24
25 namespace panda::es2panda::lexer {
26
27 enum class TokenFlags : uint8_t {
28 NONE,
29 NEW_LINE = (1 << 0),
30 HAS_ESCAPE = (1 << 2),
31 NUMBER_BIGINT = (1 << 3),
32 NUMBER_HAS_UNDERSCORE = (1 << 4),
33 ESCAPE_ERROR = (1 << 5),
34 TAGGED_TEMPLATE = (1 << 6),
35 };
36
DEFINE_BITOPS(TokenFlags)37 DEFINE_BITOPS(TokenFlags)
38
39 class Token {
40 public:
41 Token() = default;
42 DEFAULT_COPY_SEMANTIC(Token);
43 DEFAULT_MOVE_SEMANTIC(Token);
44 ~Token() = default;
45
46 friend class Lexer;
47
48 TokenType Type() const
49 {
50 return type_;
51 }
52
53 TokenFlags Flags() const
54 {
55 return flags_;
56 }
57
58 void SetTokenType(TokenType type)
59 {
60 type_ = type;
61 }
62
63 TokenType KeywordType() const
64 {
65 return keywordType_;
66 }
67
68 const SourcePosition &Start() const
69 {
70 return loc_.start;
71 }
72
73 const SourcePosition &End() const
74 {
75 return loc_.end;
76 }
77
78 const SourceRange &Loc() const
79 {
80 return loc_;
81 }
82
83 const util::StringView &Ident() const
84 {
85 return src_;
86 }
87
88 void SetIdent(const util::StringView &ident)
89 {
90 src_ = ident;
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 double Number() 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 return src_;
109 }
110
111 bool NewLine() const
112 {
113 return flags_ & TokenFlags::NEW_LINE;
114 }
115
116 bool EscapeError() const
117 {
118 return flags_ & TokenFlags::ESCAPE_ERROR;
119 }
120
121 bool IsTaggedTemplate() const
122 {
123 return flags_ & TokenFlags::TAGGED_TEMPLATE;
124 }
125
126 bool IsAccessability() const;
127 bool IsAsyncModifier() const;
128 bool IsStaticModifier() const;
129 bool IsDeclareModifier() const;
130 bool IsReadonlyModifier() const;
131 bool IsAccessorModifier() const;
132 bool IsUpdate() const;
133 bool IsUnary() const;
134 bool IsPropNameLiteral() const;
135 bool IsBooleanOrNullLiteral() const;
136 bool IsKeyword() const;
137 bool IsReservedTypeName() const;
138 bool IsJsStrictReservedWord() const;
139
140 static bool IsBinaryToken(TokenType type);
141 static bool IsBinaryLvalueToken(TokenType type);
142 static bool IsUpdateToken(TokenType type);
143 static bool IsPunctuatorToken(TokenType type);
144 static bool IsTsParamToken(TokenType type, char32_t nextChar);
145
146 private:
147 friend class KeywordsUtil;
148 TokenType type_ {TokenType::EOS};
149 TokenType keywordType_ {TokenType::EOS};
150 TokenFlags flags_ {TokenFlags::NONE};
151 SourceRange loc_ {};
152 double number_ {};
153 util::StringView src_ {};
154 };
155
156 } // namespace panda::es2panda::lexer
157
158 #endif /* TOKEN_H */
159