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