Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/wabt/wast-parser.h
1 /* 2 * Copyright 2016 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_WAST_PARSER_H_ 18 #define WABT_WAST_PARSER_H_ 19 20 #include <array> 21 #include <memory> 22 #include <optional> 23 #include <unordered_map> 24 25 #include "wabt/error.h" 26 #include "wabt/feature.h" 27 #include "wabt/intrusive-list.h" 28 #include "wabt/ir.h" 29 #include "wabt/wast-lexer.h" 30 31 namespace wabt { 32 33 struct WastParseOptions { 34 WastParseOptions(const Features& features) : features(features) {} 35 36 Features features; 37 bool debug_parsing = false; 38 }; 39 40 using TokenTypePair = std::array<TokenType, 2>; 41 42 class WastParser { 43 public: 44 WastParser(WastLexer*, Errors*, WastParseOptions*); 45 46 void WABT_PRINTF_FORMAT(3, 4) Error(Location, const char* format, ...); 47 Result ParseModule(std::unique_ptr<Module>* out_module); 48 Result ParseScript(std::unique_ptr<Script>* out_script); 49 50 std::unique_ptr<Script> ReleaseScript(); 51 52 private: 53 enum class ConstType { 54 Normal, 55 Expectation, 56 }; 57 58 void ErrorUnlessOpcodeEnabled(const Token&); 59 60 // Print an error message listing the expected tokens, as well as an example 61 // of expected input. 62 Result ErrorExpected(const std::vector<std::string>& expected, 63 const char* example = nullptr); 64 65 // Print an error message, and and return Result::Error if the next token is 66 // '('. This is commonly used after parsing a sequence of s-expressions -- if 67 // no more can be parsed, we know that a following '(' is invalid. This 68 // function consumes the '(' so a better error message can be provided 69 // (assuming the following token was unexpected). 70 Result ErrorIfLpar(const std::vector<std::string>& expected, 71 const char* example = nullptr); 72 73 // Returns the next token without consuming it. 74 Token GetToken(); 75 76 // Returns the location of the next token. 77 Location GetLocation(); 78 79 // Returns the type of the next token. 80 TokenType Peek(size_t n = 0); 81 82 // Returns the types of the next two tokens. 83 TokenTypePair PeekPair(); 84 85 // Returns true if the next token's type is equal to the parameter. 86 bool PeekMatch(TokenType, size_t n = 0); 87 88 // Returns true if the next token's type is '(' and the following token is 89 // equal to the parameter. 90 bool PeekMatchLpar(TokenType); 91 92 // Returns true if the next two tokens can start an Expr. This allows for 93 // folded expressions, plain instructions and block instructions. 94 bool PeekMatchExpr(); 95 96 // Returns true if the next two tokens are form reference type - (ref $t) 97 bool PeekMatchRefType(); 98 99 // Returns true if the next token's type is equal to the parameter. If so, 100 // then the token is consumed. 101 bool Match(TokenType); 102 103 // Returns true if the next token's type is equal to '(' and the following 104 // token is equal to the parameter. If so, then the token is consumed. 105 bool MatchLpar(TokenType); 106 107 // Like Match(), but prints an error message if the token doesn't match, and 108 // returns Result::Error. 109 Result Expect(TokenType); 110 111 // Consume one token and return it. 112 Token Consume(); 113 114 // Give the Match() function a clearer name when used to optionally consume a 115 // token (used for printing better error messages). 116 void ConsumeIfLpar() { Match(TokenType::Lpar); } 117 118 using SynchronizeFunc = bool(*)(TokenTypePair pair); 119 120 // Attempt to synchronize the token stream by dropping tokens until the 121 // SynchronizeFunc returns true, or until a token limit is reached. This 122 // function returns Result::Error if the stream was not able to be 123 // synchronized. 124 Result Synchronize(SynchronizeFunc); 125 126 bool ParseBindVarOpt(std::string* name); 127 Result ParseVar(Var* out_var); 128 bool ParseVarOpt(Var* out_var, Var default_var = Var()); 129 Result ParseOffsetExpr(ExprList* out_expr_list); 130 bool ParseOffsetExprOpt(ExprList* out_expr_list); 131 Result ParseTextList(std::vector<uint8_t>* out_data); 132 bool ParseTextListOpt(std::vector<uint8_t>* out_data); 133 Result ParseVarList(VarVector* out_var_list); 134 bool ParseElemExprOpt(ExprList* out_elem_expr); 135 bool ParseElemExprListOpt(ExprListVector* out_list); 136 bool ParseElemExprVarListOpt(ExprListVector* out_list); 137 Result ParseValueType(Var* out_type); 138 Result ParseValueTypeList( 139 TypeVector* out_type_list, 140 std::unordered_map<uint32_t, std::string>* type_names); 141 Result ParseRefKind(Type* out_type); 142 Result ParseRefType(Type* out_type); 143 bool ParseRefTypeOpt(Type* out_type); 144 Result ParseQuotedText(std::string* text, bool check_utf8 = true); 145 bool ParseOffsetOpt(Address* offset); 146 bool ParseAlignOpt(Address* align); 147 Result ParseMemidx(Location loc, Var* memidx); 148 Result ParseLimitsIndex(Limits*); 149 Result ParseLimits(Limits*); 150 Result ParseNat(uint64_t*, bool is_64); 151 152 Result ParseModuleFieldList(Module*); 153 Result ParseModuleField(Module*); 154 Result ParseDataModuleField(Module*); 155 Result ParseElemModuleField(Module*); 156 Result ParseTagModuleField(Module*); 157 Result ParseExportModuleField(Module*); 158 Result ParseFuncModuleField(Module*); 159 Result ParseTypeModuleField(Module*); 160 Result ParseGlobalModuleField(Module*); 161 Result ParseImportModuleField(Module*); 162 Result ParseMemoryModuleField(Module*); 163 Result ParseStartModuleField(Module*); 164 Result ParseTableModuleField(Module*); 165 166 Result ParseCustomSectionAnnotation(Module*); 167 bool PeekIsCustom(); 168 169 Result ParseExportDesc(Export*); 170 Result ParseInlineExports(ModuleFieldList*, ExternalKind); 171 Result ParseInlineImport(Import*); 172 Result ParseTypeUseOpt(FuncDeclaration*); 173 Result ParseFuncSignature(FuncSignature*, BindingHash* param_bindings); 174 Result ParseUnboundFuncSignature(FuncSignature*); 175 Result ParseBoundValueTypeList(TokenType, 176 TypeVector*, 177 BindingHash*, 178 std::unordered_map<uint32_t, std::string>*, 179 Index binding_index_offset = 0); 180 Result ParseUnboundValueTypeList(TokenType, 181 TypeVector*, 182 std::unordered_map<uint32_t, std::string>*); 183 Result ParseResultList(TypeVector*, 184 std::unordered_map<uint32_t, std::string>*); 185 Result ParseInstrList(ExprList*); 186 Result ParseTerminatingInstrList(ExprList*); 187 Result ParseInstr(ExprList*); 188 Result ParseCodeMetadataAnnotation(ExprList*); 189 Result ParsePlainInstr(std::unique_ptr<Expr>*); 190 Result ParseF32(Const*, ConstType type); 191 Result ParseF64(Const*, ConstType type); 192 Result ParseConst(Const*, ConstType type); 193 Result ParseExpectedValues(ExpectationPtr*); 194 Result ParseEither(ConstVector*); 195 Result ParseExternref(Const*); 196 Result ParseExpectedNan(ExpectedNan* expected); 197 Result ParseConstList(ConstVector*, ConstType type); 198 Result ParseBlockInstr(std::unique_ptr<Expr>*); 199 Result ParseLabelOpt(std::string*); 200 Result ParseEndLabelOpt(const std::string&); 201 Result ParseBlockDeclaration(BlockDeclaration*); 202 Result ParseBlock(Block*); 203 Result ParseExprList(ExprList*); 204 Result ParseExpr(ExprList*); 205 Result ParseCatchInstrList(CatchVector* catches); 206 Result ParseCatchExprList(CatchVector* catches); 207 Result ParseGlobalType(Global*); 208 Result ParseField(Field*); 209 Result ParseFieldList(std::vector<Field>*); 210 211 template <typename T> 212 Result ParsePlainInstrVar(Location, std::unique_ptr<Expr>*); 213 template <typename T> 214 Result ParseMemoryInstrVar(Location, std::unique_ptr<Expr>*); 215 template <typename T> 216 Result ParseLoadStoreInstr(Location, Token, std::unique_ptr<Expr>*); 217 template <typename T> 218 Result ParseSIMDLoadStoreInstr(Location loc, 219 Token token, 220 std::unique_ptr<Expr>* out_expr); 221 template <typename T> 222 Result ParseMemoryExpr(Location, std::unique_ptr<Expr>*); 223 template <typename T> 224 Result ParseMemoryBinaryExpr(Location, std::unique_ptr<Expr>*); 225 Result ParseSimdLane(Location, uint64_t*); 226 227 Result ParseCommandList(Script*, CommandPtrVector*); 228 Result ParseCommand(Script*, CommandPtr*); 229 Result ParseAssertExceptionCommand(CommandPtr*); 230 Result ParseAssertExhaustionCommand(CommandPtr*); 231 Result ParseAssertInvalidCommand(CommandPtr*); 232 Result ParseAssertMalformedCommand(CommandPtr*); 233 Result ParseAssertReturnCommand(CommandPtr*); 234 Result ParseAssertReturnFuncCommand(CommandPtr*); 235 Result ParseAssertTrapCommand(CommandPtr*); 236 Result ParseAssertUnlinkableCommand(CommandPtr*); 237 Result ParseActionCommand(CommandPtr*); 238 Result ParseModuleCommand(Script*, CommandPtr*); 239 Result ParseRegisterCommand(CommandPtr*); 240 Result ParseInputCommand(CommandPtr*); 241 Result ParseOutputCommand(CommandPtr*); 242 243 Result ParseAction(ActionPtr*); 244 Result ParseScriptModule(std::unique_ptr<ScriptModule>*); 245 246 template <typename T> 247 Result ParseActionCommand(TokenType, CommandPtr*); 248 template <typename T> 249 Result ParseAssertActionCommand(TokenType, CommandPtr*); 250 template <typename T> 251 Result ParseAssertActionTextCommand(TokenType, CommandPtr*); 252 template <typename T> 253 Result ParseAssertScriptModuleCommand(TokenType, CommandPtr*); 254 255 Result ParseSimdV128Const(Const*, TokenType, ConstType); 256 257 void CheckImportOrdering(Module*); 258 bool HasError() const; 259 260 WastLexer* lexer_; 261 Index last_module_index_ = kInvalidIndex; 262 Errors* errors_; 263 WastParseOptions* options_; 264 265 // two-element queue of upcoming tokens 266 class TokenQueue { 267 std::array<std::optional<Token>, 2> tokens{}; 268 bool i{}; 269 270 public: 271 void push_back(Token t); 272 void pop_front(); 273 const Token& at(size_t n) const; 274 const Token& front() const; 275 bool empty() const; 276 size_t size() const; 277 }; 278 279 TokenQueue tokens_{}; 280 }; 281 282 Result ParseWatModule(WastLexer* lexer, 283 std::unique_ptr<Module>* out_module, 284 Errors*, 285 WastParseOptions* options); 286 287 Result ParseWastScript(WastLexer* lexer, 288 std::unique_ptr<Script>* out_script, 289 Errors*, 290 WastParseOptions* options); 291 292 } // namespace wabt 293 294 #endif /* WABT_WAST_PARSER_H_ */