Line | Count | Source |
1 | | /* |
2 | | * Copyright 2017 WebAssembly Community Group participants |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include "wabt/token.h" |
18 | | |
19 | | namespace wabt { |
20 | | |
21 | 3.57M | const char* GetTokenTypeName(TokenType token_type) { |
22 | 3.57M | static const char* s_names[] = { |
23 | 525M | #define WABT_TOKEN(name, string) string, |
24 | 3.57M | #define WABT_TOKEN_FIRST(name, string) |
25 | 3.57M | #define WABT_TOKEN_LAST(name, string) |
26 | 3.57M | #include "wabt/token.def" |
27 | 3.57M | #undef WABT_TOKEN |
28 | 3.57M | #undef WABT_TOKEN_FIRST |
29 | 3.57M | #undef WABT_TOKEN_LAST |
30 | 3.57M | }; |
31 | | |
32 | 3.57M | static_assert( |
33 | 3.57M | WABT_ARRAY_SIZE(s_names) == WABT_ENUM_COUNT(TokenType), |
34 | 3.57M | "Expected TokenType names list length to match number of TokenTypes."); |
35 | | |
36 | 3.57M | int x = static_cast<int>(token_type); |
37 | 3.57M | if (x < WABT_ENUM_COUNT(TokenType)) { |
38 | 3.57M | return s_names[x]; |
39 | 3.57M | } |
40 | | |
41 | 0 | return "Invalid"; |
42 | 3.57M | } |
43 | | |
44 | | Token::Token(Location loc, TokenType token_type) |
45 | 16.5M | : loc(loc), token_type_(token_type) { |
46 | 16.5M | assert(IsTokenTypeBare(token_type_)); |
47 | 16.5M | } |
48 | | |
49 | | Token::Token(Location loc, TokenType token_type, Type type) |
50 | 3.50M | : loc(loc), token_type_(token_type) { |
51 | 3.50M | assert(HasType()); |
52 | 3.50M | Construct(type_, type); |
53 | 3.50M | } |
54 | | |
55 | | Token::Token(Location loc, TokenType token_type, std::string_view text) |
56 | 8.11M | : loc(loc), token_type_(token_type) { |
57 | 8.11M | assert(HasText()); |
58 | 8.11M | Construct(text_, text); |
59 | 8.11M | } |
60 | | |
61 | | Token::Token(Location loc, TokenType token_type, Opcode opcode) |
62 | 1.29M | : loc(loc), token_type_(token_type) { |
63 | 1.29M | assert(HasOpcode()); |
64 | 1.29M | Construct(opcode_, opcode); |
65 | 1.29M | } |
66 | | |
67 | | Token::Token(Location loc, TokenType token_type, const Literal& literal) |
68 | 1.57M | : loc(loc), token_type_(token_type) { |
69 | 1.57M | assert(HasLiteral()); |
70 | 1.57M | Construct(literal_, literal); |
71 | 1.57M | } |
72 | | |
73 | 5.41M | std::string Token::to_string() const { |
74 | 5.41M | if (IsTokenTypeBare(token_type_)) { |
75 | 1.26M | return GetTokenTypeName(token_type_); |
76 | 4.14M | } else if (HasLiteral()) { |
77 | 148k | return std::string(literal_.text); |
78 | 3.99M | } else if (HasOpcode()) { |
79 | 13.8k | return opcode_.GetName(); |
80 | 3.98M | } else if (HasText()) { |
81 | 3.91M | return std::string(text_); |
82 | 3.91M | } else if (IsTokenTypeRefKind(token_type_)) { |
83 | 60.2k | return type_.GetRefKindName(); |
84 | 60.2k | } else { |
85 | 9.02k | assert(HasType()); |
86 | 9.02k | return type_.GetName(); |
87 | 9.02k | } |
88 | 5.41M | } |
89 | | |
90 | 4.41M | std::string Token::to_string_clamp(size_t max_length) const { |
91 | 4.41M | std::string s = to_string(); |
92 | 4.41M | if (s.length() > max_length) { |
93 | 48.6k | return s.substr(0, max_length - 3) + "..."; |
94 | 4.37M | } else { |
95 | 4.37M | return s; |
96 | 4.37M | } |
97 | 4.41M | } |
98 | | |
99 | | } // namespace wabt |