Skip to content

Commit c75f748

Browse files
tlivelyradekdoulik
authored andcommitted
[Parser][NFC] Remove parser/input.h (WebAssembly#6332)
Remove the layer of abstraction sitting between the parser and the lexer now that the lexer has an interface the parser can use directly.
1 parent 9320e18 commit c75f748

File tree

6 files changed

+31
-111
lines changed

6 files changed

+31
-111
lines changed

src/parser/context-decls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void applyImportNames(Importable& item, ImportNames* names) {
2727
}
2828
}
2929

30-
Result<> addExports(ParseInput& in,
30+
Result<> addExports(Lexer& in,
3131
Module& wasm,
3232
const Named* item,
3333
const std::vector<Name>& exports,

src/parser/contexts.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#define parser_context_h
1919

2020
#include "common.h"
21-
#include "input.h"
2221
#include "ir/names.h"
22+
#include "lexer.h"
2323
#include "support/name.h"
2424
#include "support/result.h"
2525
#include "wasm-builder.h"
@@ -577,8 +577,8 @@ struct NullInstrParserCtx {
577577
};
578578

579579
struct NullCtx : NullTypeParserCtx, NullInstrParserCtx {
580-
ParseInput in;
581-
NullCtx(const ParseInput& in) : in(in) {}
580+
Lexer in;
581+
NullCtx(const Lexer& in) : in(in) {}
582582
Result<> makeTypeUse(Index, std::optional<HeapTypeT>, ParamsT*, ResultsT*) {
583583
return Ok{};
584584
}
@@ -594,7 +594,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
594594
using TableTypeT = Limits;
595595
using MemTypeT = MemType;
596596

597-
ParseInput in;
597+
Lexer in;
598598

599599
// At this stage we only look at types to find implicit type definitions,
600600
// which are inserted directly into the context. We cannot materialize or
@@ -772,7 +772,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
772772

773773
// Phase 2: Parse type definitions into a TypeBuilder.
774774
struct ParseTypeDefsCtx : TypeParserCtx<ParseTypeDefsCtx> {
775-
ParseInput in;
775+
Lexer in;
776776

777777
// We update slots in this builder as we parse type definitions.
778778
TypeBuilder& builder;
@@ -844,7 +844,7 @@ struct ParseTypeDefsCtx : TypeParserCtx<ParseTypeDefsCtx> {
844844
struct ParseImplicitTypeDefsCtx : TypeParserCtx<ParseImplicitTypeDefsCtx> {
845845
using TypeUseT = Ok;
846846

847-
ParseInput in;
847+
Lexer in;
848848

849849
// Types parsed so far.
850850
std::vector<HeapType>& types;
@@ -914,7 +914,7 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
914914

915915
using ElemListT = Type;
916916

917-
ParseInput in;
917+
Lexer in;
918918

919919
Module& wasm;
920920

@@ -1099,7 +1099,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
10991099

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

1102-
ParseInput in;
1102+
Lexer in;
11031103

11041104
Module& wasm;
11051105
Builder builder;

src/parser/input.h

Lines changed: 0 additions & 88 deletions
This file was deleted.

src/parser/lexer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <variant>
2525

2626
#include "support/name.h"
27+
#include "support/result.h"
2728

2829
#ifndef parser_lexer_h
2930
#define parser_lexer_h
@@ -397,6 +398,14 @@ struct Lexer {
397398
return getIndex();
398399
}
399400

401+
[[nodiscard]] Err err(size_t pos, std::string reason) {
402+
std::stringstream msg;
403+
msg << position(pos) << ": error: " << reason;
404+
return Err{msg.str()};
405+
}
406+
407+
[[nodiscard]] Err err(std::string reason) { return err(getPos(), reason); }
408+
400409
private:
401410
void skipSpace();
402411
void lexToken();

src/parser/parsers.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include "common.h"
2121
#include "contexts.h"
22-
#include "input.h"
22+
#include "lexer.h"
2323

2424
namespace wasm::WATParser {
2525

@@ -190,8 +190,8 @@ template<typename Ctx>
190190
Result<typename Ctx::LabelIdxT> labelidx(Ctx&, bool inDelegate = false);
191191
template<typename Ctx> Result<typename Ctx::TagIdxT> tagidx(Ctx&);
192192
template<typename Ctx> Result<typename Ctx::TypeUseT> typeuse(Ctx&);
193-
MaybeResult<ImportNames> inlineImport(ParseInput&);
194-
Result<std::vector<Name>> inlineExports(ParseInput&);
193+
MaybeResult<ImportNames> inlineImport(Lexer&);
194+
Result<std::vector<Name>> inlineExports(Lexer&);
195195
template<typename Ctx> Result<> strtype(Ctx&);
196196
template<typename Ctx> MaybeResult<typename Ctx::ModuleNameT> subtype(Ctx&);
197197
template<typename Ctx> MaybeResult<> deftype(Ctx&);
@@ -223,10 +223,10 @@ template<typename Ctx> struct WithPosition {
223223
Index original;
224224

225225
WithPosition(Ctx& ctx, Index pos) : ctx(ctx), original(ctx.in.getPos()) {
226-
ctx.in.lexer.setIndex(pos);
226+
ctx.in.setIndex(pos);
227227
}
228228

229-
~WithPosition() { ctx.in.lexer.setIndex(original); }
229+
~WithPosition() { ctx.in.setIndex(original); }
230230
};
231231

232232
// Deduction guide to satisfy -Wctad-maybe-unsupported.
@@ -786,7 +786,7 @@ template<typename Ctx> MaybeResult<> foldedinstr(Ctx& ctx) {
786786

787787
// A stack of (start, end) position pairs defining the positions of
788788
// instructions that need to be parsed after their folded children.
789-
std::vector<std::pair<Index, std::optional<Index>>> foldedInstrs;
789+
std::vector<std::pair<size_t, std::optional<size_t>>> foldedInstrs;
790790

791791
do {
792792
if (ctx.in.takeRParen()) {
@@ -893,7 +893,7 @@ template<typename Ctx> Result<typename Ctx::BlockTypeT> blocktype(Ctx& ctx) {
893893

894894
// We either had no results or multiple results. Reset and parse again as a
895895
// type use.
896-
ctx.in.lexer.setIndex(pos);
896+
ctx.in.setIndex(pos);
897897
auto use = typeuse(ctx);
898898
CHECK_ERR(use);
899899

@@ -1129,7 +1129,7 @@ template<typename Ctx> MaybeResult<> trycatch(Ctx& ctx, bool folded) {
11291129
if (id && id != label) {
11301130
// Instead of returning an error, retry without the ID.
11311131
parseID = false;
1132-
ctx.in.lexer.setIndex(afterCatchPos);
1132+
ctx.in.setIndex(afterCatchPos);
11331133
continue;
11341134
}
11351135
}
@@ -1138,7 +1138,7 @@ template<typename Ctx> MaybeResult<> trycatch(Ctx& ctx, bool folded) {
11381138
if (parseID && tag.getErr()) {
11391139
// Instead of returning an error, retry without the ID.
11401140
parseID = false;
1141-
ctx.in.lexer.setIndex(afterCatchPos);
1141+
ctx.in.setIndex(afterCatchPos);
11421142
continue;
11431143
}
11441144
CHECK_ERR(tag);
@@ -2247,7 +2247,7 @@ template<typename Ctx> Result<typename Ctx::TypeUseT> typeuse(Ctx& ctx) {
22472247
}
22482248

22492249
// ('(' 'import' mod:name nm:name ')')?
2250-
MaybeResult<ImportNames> inlineImport(ParseInput& in) {
2250+
MaybeResult<ImportNames> inlineImport(Lexer& in) {
22512251
if (!in.takeSExprStart("import"sv)) {
22522252
return {};
22532253
}
@@ -2267,7 +2267,7 @@ MaybeResult<ImportNames> inlineImport(ParseInput& in) {
22672267
}
22682268

22692269
// ('(' 'export' name ')')*
2270-
Result<std::vector<Name>> inlineExports(ParseInput& in) {
2270+
Result<std::vector<Name>> inlineExports(Lexer& in) {
22712271
std::vector<Name> exports;
22722272
while (in.takeSExprStart("export"sv)) {
22732273
auto name = in.takeName();
@@ -2834,7 +2834,7 @@ template<typename Ctx> MaybeResult<> elem(Ctx& ctx) {
28342834
offset = *off;
28352835
} else {
28362836
// This must be the beginning of the elemlist instead.
2837-
ctx.in.lexer.setIndex(beforeLParen);
2837+
ctx.in.setIndex(beforeLParen);
28382838
}
28392839
}
28402840
}

src/parser/wat-parser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ namespace wasm::WATParser {
6060

6161
namespace {
6262

63-
Result<IndexMap> createIndexMap(ParseInput& in,
64-
const std::vector<DefPos>& defs) {
63+
Result<IndexMap> createIndexMap(Lexer& in, const std::vector<DefPos>& defs) {
6564
IndexMap indices;
6665
for (auto& def : defs) {
6766
if (def.name.is()) {

0 commit comments

Comments
 (0)