Skip to content

Commit

Permalink
[Parser][NFC] Remove parser/input.h (WebAssembly#6332)
Browse files Browse the repository at this point in the history
Remove the layer of abstraction sitting between the parser and the lexer now
that the lexer has an interface the parser can use directly.
  • Loading branch information
tlively authored and radekdoulik committed Jul 12, 2024
1 parent 9320e18 commit c75f748
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 111 deletions.
2 changes: 1 addition & 1 deletion src/parser/context-decls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{};
}
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -914,7 +914,7 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,

using ElemListT = Type;

ParseInput in;
Lexer in;

Module& wasm;

Expand Down Expand Up @@ -1099,7 +1099,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {

using TagLabelListT = std::vector<std::pair<TagIdxT, LabelIdxT>>;

ParseInput in;
Lexer in;

Module& wasm;
Builder builder;
Expand Down
88 changes: 0 additions & 88 deletions src/parser/input.h

This file was deleted.

9 changes: 9 additions & 0 deletions src/parser/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <variant>

#include "support/name.h"
#include "support/result.h"

#ifndef parser_lexer_h
#define parser_lexer_h
Expand Down Expand Up @@ -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();
Expand Down
24 changes: 12 additions & 12 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "common.h"
#include "contexts.h"
#include "input.h"
#include "lexer.h"

namespace wasm::WATParser {

Expand Down Expand Up @@ -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&);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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 {};
}
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/parser/wat-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit c75f748

Please sign in to comment.