Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/wabt/token.h
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 #ifndef WABT_TOKEN_H_ 18 #define WABT_TOKEN_H_ 19 20 #include <string_view> 21 22 #include "wabt/literal.h" 23 #include "wabt/opcode.h" 24 25 namespace wabt { 26 27 struct Literal { 28 Literal() = default; 29 Literal(LiteralType type, std::string_view text) : type(type), text(text) {} 30 31 LiteralType type; 32 std::string_view text; 33 }; 34 35 enum class TokenType { 36 #define WABT_TOKEN(name, string) name, 37 #define WABT_TOKEN_FIRST(group, first) First_##group = first, 38 #define WABT_TOKEN_LAST(group, last) Last_##group = last, 39 #include "wabt/token.def" 40 #undef WABT_TOKEN 41 #undef WABT_TOKEN_FIRST 42 #undef WABT_TOKEN_LAST 43 44 First = First_Bare, 45 Last = Last_RefKind, 46 }; 47 48 const char* GetTokenTypeName(TokenType); 49 50 inline bool IsTokenTypeBare(TokenType token_type) { 51 return token_type >= TokenType::First_Bare && 52 token_type <= TokenType::Last_Bare; 53 } 54 55 inline bool IsTokenTypeString(TokenType token_type) { 56 return token_type >= TokenType::First_String && 57 token_type <= TokenType::Last_String; 58 } 59 60 inline bool IsTokenTypeType(TokenType token_type) { 61 return token_type == TokenType::ValueType; 62 } 63 64 inline bool IsTokenTypeOpcode(TokenType token_type) { 65 return token_type >= TokenType::First_Opcode && 66 token_type <= TokenType::Last_Opcode; 67 } 68 69 inline bool IsTokenTypeLiteral(TokenType token_type) { 70 return token_type >= TokenType::First_Literal && 71 token_type <= TokenType::Last_Literal; 72 } 73 74 inline bool IsTokenTypeRefKind(TokenType token_type) { 75 return token_type >= TokenType::First_RefKind && 76 token_type <= TokenType::Last_RefKind; 77 } 78 79 struct Token { 80 Token() : token_type_(TokenType::Invalid) {} 81 Token(Location, TokenType); 82 Token(Location, TokenType, Type); 83 Token(Location, TokenType, std::string_view); 84 Token(Location, TokenType, Opcode); 85 Token(Location, TokenType, const Literal&); 86 87 Location loc; 88 89 TokenType token_type() const { return token_type_; } 90 91 bool HasText() const { return IsTokenTypeString(token_type_); } 92 bool HasType() const { 93 return IsTokenTypeType(token_type_) || IsTokenTypeRefKind(token_type_); 94 } 95 bool HasOpcode() const { return IsTokenTypeOpcode(token_type_); } 96 bool HasLiteral() const { return IsTokenTypeLiteral(token_type_); } 97 98 std::string_view text() const { 99 assert(HasText()); 100 return text_; 101 } 102 103 Type type() const { 104 assert(HasType()); 105 return type_; 106 } 107 108 Opcode opcode() const { 109 assert(HasOpcode()); 110 return opcode_; 111 } 112 113 const Literal& literal() const { 114 assert(HasLiteral()); 115 return literal_; 116 } 117 118 std::string to_string() const; 119 std::string to_string_clamp(size_t max_length) const; 120 121 private: 122 TokenType token_type_; 123 124 union { 125 std::string_view text_; 126 Type type_; 127 Opcode opcode_; 128 Literal literal_; 129 }; 130 }; 131 132 } // namespace wabt 133 134 #endif // WABT_TOKEN_H_