Skip to content

Commit 90ead54

Browse files
authored
[Parser][NFC] Clean up the lexer index/pos API (#6553)
The lexer previously had both `getPos` and `getIndex` APIs that did different things, but after a recent refactoring there is no difference between the index and the position. Deduplicate the API surface.
1 parent 9b65160 commit 90ead54

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

src/parser/lexer.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -905,12 +905,12 @@ std::optional<LexResult> keyword(std::string_view in) {
905905
void Lexer::skipSpace() {
906906
while (true) {
907907
if (auto ctx = annotation(next())) {
908-
index += ctx->span.size();
908+
pos += ctx->span.size();
909909
annotations.push_back(ctx->annotation);
910910
continue;
911911
}
912912
if (auto ctx = space(next())) {
913-
index += ctx->span.size();
913+
pos += ctx->span.size();
914914
continue;
915915
}
916916
break;
@@ -919,7 +919,7 @@ void Lexer::skipSpace() {
919919

920920
bool Lexer::takeLParen() {
921921
if (LexCtx(next()).startsWith("("sv)) {
922-
++index;
922+
++pos;
923923
advance();
924924
return true;
925925
}
@@ -928,7 +928,7 @@ bool Lexer::takeLParen() {
928928

929929
bool Lexer::takeRParen() {
930930
if (LexCtx(next()).startsWith(")"sv)) {
931-
++index;
931+
++pos;
932932
advance();
933933
return true;
934934
}
@@ -937,7 +937,7 @@ bool Lexer::takeRParen() {
937937

938938
std::optional<std::string> Lexer::takeString() {
939939
if (auto result = str(next())) {
940-
index += result->span.size();
940+
pos += result->span.size();
941941
advance();
942942
if (result->str) {
943943
return result->str;
@@ -950,7 +950,7 @@ std::optional<std::string> Lexer::takeString() {
950950

951951
std::optional<Name> Lexer::takeID() {
952952
if (auto result = ident(next())) {
953-
index += result->span.size();
953+
pos += result->span.size();
954954
advance();
955955
if (result->str) {
956956
return Name(*result->str);
@@ -967,7 +967,7 @@ std::optional<Name> Lexer::takeID() {
967967

968968
std::optional<std::string_view> Lexer::takeKeyword() {
969969
if (auto result = keyword(next())) {
970-
index += result->span.size();
970+
pos += result->span.size();
971971
advance();
972972
return result->span;
973973
}
@@ -976,7 +976,7 @@ std::optional<std::string_view> Lexer::takeKeyword() {
976976

977977
bool Lexer::takeKeyword(std::string_view expected) {
978978
if (auto result = keyword(next()); result && result->span == expected) {
979-
index += expected.size();
979+
pos += expected.size();
980980
advance();
981981
return true;
982982
}
@@ -990,7 +990,7 @@ std::optional<uint64_t> Lexer::takeOffset() {
990990
}
991991
Lexer subLexer(result->span.substr(7));
992992
if (auto o = subLexer.takeU64()) {
993-
index += result->span.size();
993+
pos += result->span.size();
994994
advance();
995995
return o;
996996
}
@@ -1005,7 +1005,7 @@ std::optional<uint32_t> Lexer::takeAlign() {
10051005
}
10061006
Lexer subLexer(result->span.substr(6));
10071007
if (auto o = subLexer.takeU32()) {
1008-
index += result->span.size();
1008+
pos += result->span.size();
10091009
advance();
10101010
return o;
10111011
}
@@ -1016,7 +1016,7 @@ std::optional<uint32_t> Lexer::takeAlign() {
10161016
template<typename T> std::optional<T> Lexer::takeU() {
10171017
static_assert(std::is_integral_v<T> && std::is_unsigned_v<T>);
10181018
if (auto result = integer(next()); result && result->isUnsigned<T>()) {
1019-
index += result->span.size();
1019+
pos += result->span.size();
10201020
advance();
10211021
return T(result->n);
10221022
}
@@ -1027,7 +1027,7 @@ template<typename T> std::optional<T> Lexer::takeU() {
10271027
template<typename T> std::optional<T> Lexer::takeS() {
10281028
static_assert(std::is_integral_v<T> && std::is_signed_v<T>);
10291029
if (auto result = integer(next()); result && result->isSigned<T>()) {
1030-
index += result->span.size();
1030+
pos += result->span.size();
10311031
advance();
10321032
return T(result->n);
10331033
}
@@ -1038,7 +1038,7 @@ template<typename T> std::optional<T> Lexer::takeI() {
10381038
static_assert(std::is_integral_v<T> && std::is_unsigned_v<T>);
10391039
if (auto result = integer(next())) {
10401040
if (result->isUnsigned<T>() || result->isSigned<std::make_signed_t<T>>()) {
1041-
index += result->span.size();
1041+
pos += result->span.size();
10421042
advance();
10431043
return T(result->n);
10441044
}
@@ -1078,12 +1078,12 @@ std::optional<double> Lexer::takeF64() {
10781078
bits = (bits & ~payloadMask) | payload;
10791079
memcpy(&d, &bits, sizeof(bits));
10801080
}
1081-
index += result->span.size();
1081+
pos += result->span.size();
10821082
advance();
10831083
return d;
10841084
}
10851085
if (auto result = integer(next())) {
1086-
index += result->span.size();
1086+
pos += result->span.size();
10871087
advance();
10881088
if (result->sign == Neg) {
10891089
if (result->n == 0) {
@@ -1115,12 +1115,12 @@ std::optional<float> Lexer::takeF32() {
11151115
bits = (bits & ~payloadMask) | payload;
11161116
memcpy(&f, &bits, sizeof(bits));
11171117
}
1118-
index += result->span.size();
1118+
pos += result->span.size();
11191119
advance();
11201120
return f;
11211121
}
11221122
if (auto result = integer(next())) {
1123-
index += result->span.size();
1123+
pos += result->span.size();
11241124
advance();
11251125
if (result->sign == Neg) {
11261126
if (result->n == 0) {

src/parser/lexer.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ extern Name srcAnnotationKind;
5858

5959
struct Lexer {
6060
private:
61-
size_t index = 0;
61+
size_t pos = 0;
6262
std::vector<Annotation> annotations;
6363

6464
public:
6565
std::string_view buffer;
6666

67-
Lexer(std::string_view buffer) : buffer(buffer) { setIndex(0); }
67+
Lexer(std::string_view buffer) : buffer(buffer) { setPos(0); }
6868

69-
size_t getIndex() const { return index; }
69+
size_t getPos() const { return pos; }
7070

71-
void setIndex(size_t i) {
72-
index = i;
71+
void setPos(size_t i) {
72+
pos = i;
7373
advance();
7474
}
7575

@@ -93,7 +93,7 @@ struct Lexer {
9393
if (takeString()) {
9494
continue;
9595
}
96-
++index;
96+
++pos;
9797
advance();
9898
}
9999
}
@@ -150,14 +150,14 @@ struct Lexer {
150150
return ret;
151151
}
152152

153-
std::string_view next() const { return buffer.substr(index); }
153+
std::string_view next() const { return buffer.substr(pos); }
154154

155155
void advance() {
156156
annotations.clear();
157157
skipSpace();
158158
}
159159

160-
bool empty() const { return index == buffer.size(); }
160+
bool empty() const { return pos == buffer.size(); }
161161

162162
TextPos position(const char* c) const;
163163
TextPos position(size_t i) const { return position(buffer.data() + i); }
@@ -166,8 +166,6 @@ struct Lexer {
166166
}
167167
TextPos position() const { return position(getPos()); }
168168

169-
size_t getPos() const { return index; }
170-
171169
[[nodiscard]] Err err(size_t pos, std::string reason) {
172170
std::stringstream msg;
173171
msg << position(pos) << ": error: " << reason;

src/parser/parsers.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,11 @@ template<typename Ctx> struct WithPosition {
372372
WithPosition(Ctx& ctx, Index pos)
373373
: ctx(ctx), original(ctx.in.getPos()),
374374
annotations(ctx.in.takeAnnotations()) {
375-
ctx.in.setIndex(pos);
375+
ctx.in.setPos(pos);
376376
}
377377

378378
~WithPosition() {
379-
ctx.in.setIndex(original);
379+
ctx.in.setPos(original);
380380
ctx.in.setAnnotations(std::move(annotations));
381381
}
382382
};
@@ -1325,7 +1325,7 @@ trycatch(Ctx& ctx, const std::vector<Annotation>& annotations, bool folded) {
13251325
if (id && id != label) {
13261326
// Instead of returning an error, retry without the ID.
13271327
parseID = false;
1328-
ctx.in.setIndex(afterCatchPos);
1328+
ctx.in.setPos(afterCatchPos);
13291329
continue;
13301330
}
13311331
}
@@ -1334,7 +1334,7 @@ trycatch(Ctx& ctx, const std::vector<Annotation>& annotations, bool folded) {
13341334
if (parseID && tag.getErr()) {
13351335
// Instead of returning an error, retry without the ID.
13361336
parseID = false;
1337-
ctx.in.setIndex(afterCatchPos);
1337+
ctx.in.setPos(afterCatchPos);
13381338
continue;
13391339
}
13401340
CHECK_ERR(tag);
@@ -3375,7 +3375,7 @@ template<typename Ctx> MaybeResult<> elem(Ctx& ctx) {
33753375
offset = *off;
33763376
} else {
33773377
// This must be the beginning of the elemlist instead.
3378-
ctx.in.setIndex(beforeLParen);
3378+
ctx.in.setPos(beforeLParen);
33793379
}
33803380
}
33813381
}

0 commit comments

Comments
 (0)