diff --git a/src/parser/context-decls.cpp b/src/parser/context-decls.cpp index 7d2d8a0a99c..d9279a0b698 100644 --- a/src/parser/context-decls.cpp +++ b/src/parser/context-decls.cpp @@ -27,7 +27,7 @@ void applyImportNames(Importable& item, ImportNames* names) { } } -Result<> addExports(ParseInput& in, +Result<> addExports(Lexer& in, Module& wasm, const Named* item, const std::vector& exports, diff --git a/src/parser/contexts.h b/src/parser/contexts.h index ab82f396318..50a2abd9638 100644 --- a/src/parser/contexts.h +++ b/src/parser/contexts.h @@ -18,8 +18,8 @@ #define parser_context_h #include "common.h" -#include "input.h" #include "ir/names.h" +#include "lexer.h" #include "support/name.h" #include "support/result.h" #include "wasm-builder.h" @@ -577,8 +577,8 @@ struct NullInstrParserCtx { }; struct NullCtx : NullTypeParserCtx, NullInstrParserCtx { - ParseInput in; - NullCtx(const ParseInput& in) : in(in) {} + Lexer in; + NullCtx(const Lexer& in) : in(in) {} Result<> makeTypeUse(Index, std::optional, ParamsT*, ResultsT*) { return Ok{}; } @@ -594,7 +594,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx { using TableTypeT = Limits; using MemTypeT = MemType; - ParseInput in; + Lexer in; // At this stage we only look at types to find implicit type definitions, // which are inserted directly into the context. We cannot materialize or @@ -772,7 +772,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx { // Phase 2: Parse type definitions into a TypeBuilder. struct ParseTypeDefsCtx : TypeParserCtx { - ParseInput in; + Lexer in; // We update slots in this builder as we parse type definitions. TypeBuilder& builder; @@ -844,7 +844,7 @@ struct ParseTypeDefsCtx : TypeParserCtx { struct ParseImplicitTypeDefsCtx : TypeParserCtx { using TypeUseT = Ok; - ParseInput in; + Lexer in; // Types parsed so far. std::vector& types; @@ -914,7 +914,7 @@ struct ParseModuleTypesCtx : TypeParserCtx, using ElemListT = Type; - ParseInput in; + Lexer in; Module& wasm; @@ -1099,7 +1099,7 @@ struct ParseDefsCtx : TypeParserCtx { using TagLabelListT = std::vector>; - ParseInput in; + Lexer in; Module& wasm; Builder builder; diff --git a/src/parser/input.h b/src/parser/input.h deleted file mode 100644 index f83f5a40a08..00000000000 --- a/src/parser/input.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2023 WebAssembly Community Group participants - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef parser_input_h -#define parser_input_h - -#include "lexer.h" -#include "support/result.h" -#include "wasm.h" - -namespace wasm::WATParser { - -using namespace std::string_view_literals; - -// Wraps a lexer and provides utilities for consuming tokens. -struct ParseInput { - Lexer lexer; - - explicit ParseInput(std::string_view in) : lexer(in) {} - - ParseInput(std::string_view in, size_t index) : lexer(in) { - lexer.setIndex(index); - } - - ParseInput(const ParseInput& other, size_t index) : lexer(other.lexer) { - lexer.setIndex(index); - } - - bool empty() { return lexer.empty(); } - - // TODO: Remove this useless layer of abstraction between the Lexer and - // Parser. - std::optional peek() { return lexer.peek(); } - bool takeLParen() { return lexer.takeLParen(); } - bool takeRParen() { return lexer.takeRParen(); } - bool takeUntilParen() { return lexer.takeUntilParen(); } - std::optional takeID() { return lexer.takeID(); } - std::optional takeKeyword() { return lexer.takeKeyword(); } - bool takeKeyword(std::string_view expected) { - return lexer.takeKeyword(expected); - } - std::optional takeOffset() { return lexer.takeOffset(); } - std::optional takeAlign() { return lexer.takeAlign(); } - std::optional takeU64() { return lexer.takeU64(); } - std::optional takeI64() { return lexer.takeI64(); } - std::optional takeU32() { return lexer.takeU32(); } - std::optional takeI32() { return lexer.takeI32(); } - std::optional takeI16() { return lexer.takeI16(); } - std::optional takeU8() { return lexer.takeU8(); } - std::optional takeI8() { return lexer.takeI8(); } - std::optional takeF64() { return lexer.takeF64(); } - std::optional takeF32() { return lexer.takeF32(); } - std::optional takeString() { return lexer.takeString(); } - std::optional takeName() { return lexer.takeName(); } - bool takeSExprStart(std::string_view expected) { - return lexer.takeSExprStart(expected); - } - bool peekSExprStart(std::string_view expected) { - return lexer.peekSExprStart(expected); - } - - Index getPos() { return lexer.getPos(); } - - [[nodiscard]] Err err(Index pos, std::string reason) { - std::stringstream msg; - msg << lexer.position(pos) << ": error: " << reason; - return Err{msg.str()}; - } - - [[nodiscard]] Err err(std::string reason) { return err(getPos(), reason); } -}; - -} // namespace wasm::WATParser - -#endif // parser_input_h diff --git a/src/parser/lexer.h b/src/parser/lexer.h index 8f9bd103a8f..6c0a588c052 100644 --- a/src/parser/lexer.h +++ b/src/parser/lexer.h @@ -24,6 +24,7 @@ #include #include "support/name.h" +#include "support/result.h" #ifndef parser_lexer_h #define parser_lexer_h @@ -397,6 +398,14 @@ struct Lexer { return getIndex(); } + [[nodiscard]] Err err(size_t pos, std::string reason) { + std::stringstream msg; + msg << position(pos) << ": error: " << reason; + return Err{msg.str()}; + } + + [[nodiscard]] Err err(std::string reason) { return err(getPos(), reason); } + private: void skipSpace(); void lexToken(); diff --git a/src/parser/parsers.h b/src/parser/parsers.h index 960ccb26da8..bd3aef9dc75 100644 --- a/src/parser/parsers.h +++ b/src/parser/parsers.h @@ -19,7 +19,7 @@ #include "common.h" #include "contexts.h" -#include "input.h" +#include "lexer.h" namespace wasm::WATParser { @@ -190,8 +190,8 @@ template Result labelidx(Ctx&, bool inDelegate = false); template Result tagidx(Ctx&); template Result typeuse(Ctx&); -MaybeResult inlineImport(ParseInput&); -Result> inlineExports(ParseInput&); +MaybeResult inlineImport(Lexer&); +Result> inlineExports(Lexer&); template Result<> strtype(Ctx&); template MaybeResult subtype(Ctx&); template MaybeResult<> deftype(Ctx&); @@ -223,10 +223,10 @@ template struct WithPosition { Index original; WithPosition(Ctx& ctx, Index pos) : ctx(ctx), original(ctx.in.getPos()) { - ctx.in.lexer.setIndex(pos); + ctx.in.setIndex(pos); } - ~WithPosition() { ctx.in.lexer.setIndex(original); } + ~WithPosition() { ctx.in.setIndex(original); } }; // Deduction guide to satisfy -Wctad-maybe-unsupported. @@ -786,7 +786,7 @@ template MaybeResult<> foldedinstr(Ctx& ctx) { // A stack of (start, end) position pairs defining the positions of // instructions that need to be parsed after their folded children. - std::vector>> foldedInstrs; + std::vector>> foldedInstrs; do { if (ctx.in.takeRParen()) { @@ -893,7 +893,7 @@ template Result blocktype(Ctx& ctx) { // We either had no results or multiple results. Reset and parse again as a // type use. - ctx.in.lexer.setIndex(pos); + ctx.in.setIndex(pos); auto use = typeuse(ctx); CHECK_ERR(use); @@ -1129,7 +1129,7 @@ template MaybeResult<> trycatch(Ctx& ctx, bool folded) { if (id && id != label) { // Instead of returning an error, retry without the ID. parseID = false; - ctx.in.lexer.setIndex(afterCatchPos); + ctx.in.setIndex(afterCatchPos); continue; } } @@ -1138,7 +1138,7 @@ template MaybeResult<> trycatch(Ctx& ctx, bool folded) { if (parseID && tag.getErr()) { // Instead of returning an error, retry without the ID. parseID = false; - ctx.in.lexer.setIndex(afterCatchPos); + ctx.in.setIndex(afterCatchPos); continue; } CHECK_ERR(tag); @@ -2247,7 +2247,7 @@ template Result typeuse(Ctx& ctx) { } // ('(' 'import' mod:name nm:name ')')? -MaybeResult inlineImport(ParseInput& in) { +MaybeResult inlineImport(Lexer& in) { if (!in.takeSExprStart("import"sv)) { return {}; } @@ -2267,7 +2267,7 @@ MaybeResult inlineImport(ParseInput& in) { } // ('(' 'export' name ')')* -Result> inlineExports(ParseInput& in) { +Result> inlineExports(Lexer& in) { std::vector exports; while (in.takeSExprStart("export"sv)) { auto name = in.takeName(); @@ -2834,7 +2834,7 @@ template MaybeResult<> elem(Ctx& ctx) { offset = *off; } else { // This must be the beginning of the elemlist instead. - ctx.in.lexer.setIndex(beforeLParen); + ctx.in.setIndex(beforeLParen); } } } diff --git a/src/parser/wat-parser.cpp b/src/parser/wat-parser.cpp index 33066ea7163..ff0dea76816 100644 --- a/src/parser/wat-parser.cpp +++ b/src/parser/wat-parser.cpp @@ -60,8 +60,7 @@ namespace wasm::WATParser { namespace { -Result createIndexMap(ParseInput& in, - const std::vector& defs) { +Result createIndexMap(Lexer& in, const std::vector& defs) { IndexMap indices; for (auto& def : defs) { if (def.name.is()) {