summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/parser/context-decls.cpp2
-rw-r--r--src/parser/contexts.h16
-rw-r--r--src/parser/input.h88
-rw-r--r--src/parser/lexer.h9
-rw-r--r--src/parser/parsers.h24
-rw-r--r--src/parser/wat-parser.cpp3
6 files changed, 31 insertions, 111 deletions
diff --git a/src/parser/context-decls.cpp b/src/parser/context-decls.cpp
index 7d2d8a0a9..d9279a0b6 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<Name>& exports,
diff --git a/src/parser/contexts.h b/src/parser/contexts.h
index ab82f3963..50a2abd96 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<HeapTypeT>, 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<ParseTypeDefsCtx> {
- ParseInput in;
+ Lexer in;
// We update slots in this builder as we parse type definitions.
TypeBuilder& builder;
@@ -844,7 +844,7 @@ struct ParseTypeDefsCtx : TypeParserCtx<ParseTypeDefsCtx> {
struct ParseImplicitTypeDefsCtx : TypeParserCtx<ParseImplicitTypeDefsCtx> {
using TypeUseT = Ok;
- ParseInput in;
+ Lexer in;
// Types parsed so far.
std::vector<HeapType>& types;
@@ -914,7 +914,7 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
using ElemListT = Type;
- ParseInput in;
+ Lexer in;
Module& wasm;
@@ -1099,7 +1099,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
using TagLabelListT = std::vector<std::pair<TagIdxT, LabelIdxT>>;
- 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 f83f5a40a..000000000
--- 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<Token> peek() { return lexer.peek(); }
- bool takeLParen() { return lexer.takeLParen(); }
- bool takeRParen() { return lexer.takeRParen(); }
- bool takeUntilParen() { return lexer.takeUntilParen(); }
- std::optional<Name> takeID() { return lexer.takeID(); }
- std::optional<std::string_view> takeKeyword() { return lexer.takeKeyword(); }
- bool takeKeyword(std::string_view expected) {
- return lexer.takeKeyword(expected);
- }
- std::optional<uint64_t> takeOffset() { return lexer.takeOffset(); }
- std::optional<uint32_t> takeAlign() { return lexer.takeAlign(); }
- std::optional<uint64_t> takeU64() { return lexer.takeU64(); }
- std::optional<uint64_t> takeI64() { return lexer.takeI64(); }
- std::optional<uint32_t> takeU32() { return lexer.takeU32(); }
- std::optional<uint32_t> takeI32() { return lexer.takeI32(); }
- std::optional<uint16_t> takeI16() { return lexer.takeI16(); }
- std::optional<uint8_t> takeU8() { return lexer.takeU8(); }
- std::optional<uint8_t> takeI8() { return lexer.takeI8(); }
- std::optional<double> takeF64() { return lexer.takeF64(); }
- std::optional<float> takeF32() { return lexer.takeF32(); }
- std::optional<std::string> takeString() { return lexer.takeString(); }
- std::optional<Name> 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 8f9bd103a..6c0a588c0 100644
--- a/src/parser/lexer.h
+++ b/src/parser/lexer.h
@@ -24,6 +24,7 @@
#include <variant>
#include "support/name.h"
+#include "support/result.h"
#ifndef parser_lexer_h
#define parser_lexer_h
@@ -397,6 +398,14 @@ public:
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 960ccb26d..bd3aef9dc 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<typename Ctx>
Result<typename Ctx::LabelIdxT> labelidx(Ctx&, bool inDelegate = false);
template<typename Ctx> Result<typename Ctx::TagIdxT> tagidx(Ctx&);
template<typename Ctx> Result<typename Ctx::TypeUseT> typeuse(Ctx&);
-MaybeResult<ImportNames> inlineImport(ParseInput&);
-Result<std::vector<Name>> inlineExports(ParseInput&);
+MaybeResult<ImportNames> inlineImport(Lexer&);
+Result<std::vector<Name>> inlineExports(Lexer&);
template<typename Ctx> Result<> strtype(Ctx&);
template<typename Ctx> MaybeResult<typename Ctx::ModuleNameT> subtype(Ctx&);
template<typename Ctx> MaybeResult<> deftype(Ctx&);
@@ -223,10 +223,10 @@ template<typename Ctx> 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<typename Ctx> 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<std::pair<Index, std::optional<Index>>> foldedInstrs;
+ std::vector<std::pair<size_t, std::optional<size_t>>> foldedInstrs;
do {
if (ctx.in.takeRParen()) {
@@ -893,7 +893,7 @@ template<typename Ctx> Result<typename Ctx::BlockTypeT> 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<typename Ctx> 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<typename Ctx> 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<typename Ctx> Result<typename Ctx::TypeUseT> typeuse(Ctx& ctx) {
}
// ('(' 'import' mod:name nm:name ')')?
-MaybeResult<ImportNames> inlineImport(ParseInput& in) {
+MaybeResult<ImportNames> inlineImport(Lexer& in) {
if (!in.takeSExprStart("import"sv)) {
return {};
}
@@ -2267,7 +2267,7 @@ MaybeResult<ImportNames> inlineImport(ParseInput& in) {
}
// ('(' 'export' name ')')*
-Result<std::vector<Name>> inlineExports(ParseInput& in) {
+Result<std::vector<Name>> inlineExports(Lexer& in) {
std::vector<Name> exports;
while (in.takeSExprStart("export"sv)) {
auto name = in.takeName();
@@ -2834,7 +2834,7 @@ template<typename Ctx> 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 33066ea71..ff0dea768 100644
--- a/src/parser/wat-parser.cpp
+++ b/src/parser/wat-parser.cpp
@@ -60,8 +60,7 @@ namespace wasm::WATParser {
namespace {
-Result<IndexMap> createIndexMap(ParseInput& in,
- const std::vector<DefPos>& defs) {
+Result<IndexMap> createIndexMap(Lexer& in, const std::vector<DefPos>& defs) {
IndexMap indices;
for (auto& def : defs) {
if (def.name.is()) {