Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions src/parser/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,6 @@ struct LexAnnotationCtx : LexCtx {
}
};

std::optional<LexResult> lparen(std::string_view in) {
LexCtx ctx(in);
ctx.takePrefix("("sv);
return ctx.lexed();
}

std::optional<LexResult> rparen(std::string_view in) {
LexCtx ctx(in);
ctx.takePrefix(")"sv);
return ctx.lexed();
}

std::optional<LexResult> idchar(std::string_view);
std::optional<LexResult> space(std::string_view);
std::optional<LexResult> keyword(std::string_view);
Expand Down Expand Up @@ -554,8 +542,8 @@ bool LexCtx::canFinish() const {
// Logically we want to check for eof, parens, and space. But we don't
// actually want to parse more than a couple characters of space, so check for
// individual space chars or comment starts instead.
return empty() || lparen(next()) || rparen(next()) || spacechar(next()) ||
startsWith(";;"sv);
return empty() || startsWith("("sv) || startsWith(")"sv) ||
spacechar(next()) || startsWith(";;"sv);
}

// num ::= d:digit => d
Expand Down Expand Up @@ -1057,14 +1045,34 @@ void Lexer::skipSpace() {
}
}

bool Lexer::takeLParen() {
if (curr) {
return false;
}
if (LexCtx(next()).startsWith("("sv)) {
++index;
advance();
return true;
}
return false;
}

bool Lexer::takeRParen() {
if (curr) {
return false;
}
if (LexCtx(next()).startsWith(")"sv)) {
++index;
advance();
return true;
}
return false;
}

void Lexer::lexToken() {
// TODO: Ensure we're getting the longest possible match.
Token tok;
if (auto t = lparen(next())) {
tok = Token{t->span, LParenTok{}};
} else if (auto t = rparen(next())) {
tok = Token{t->span, RParenTok{}};
} else if (auto t = ident(next())) {
if (auto t = ident(next())) {
tok = Token{t->span, IdTok{t->isStr, t->str}};
} else if (auto t = integer(next())) {
tok = Token{t->span, IntTok{t->n, t->sign}};
Expand Down Expand Up @@ -1129,14 +1137,6 @@ std::ostream& operator<<(std::ostream& os, const TextPos& pos) {
return os << pos.line << ":" << pos.col;
}

std::ostream& operator<<(std::ostream& os, const LParenTok&) {
return os << "'('";
}

std::ostream& operator<<(std::ostream& os, const RParenTok&) {
return os << "')'";
}

std::ostream& operator<<(std::ostream& os, const IdTok&) { return os << "id"; }

std::ostream& operator<<(std::ostream& os, const IntTok& tok) {
Expand Down
47 changes: 9 additions & 38 deletions src/parser/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ struct TextPos {
// Tokens
// ======

struct LParenTok {
bool operator==(const LParenTok&) const { return true; }
friend std::ostream& operator<<(std::ostream&, const LParenTok&);
};

struct RParenTok {
bool operator==(const RParenTok&) const { return true; }
friend std::ostream& operator<<(std::ostream&, const RParenTok&);
};

struct IdTok {
// Whether this ID has `$"..."` format
bool isStr;
Expand Down Expand Up @@ -103,24 +93,14 @@ struct KeywordTok {
};

struct Token {
using Data = std::variant<LParenTok,
RParenTok,
IdTok,
IntTok,
FloatTok,
StringTok,
KeywordTok>;
using Data = std::variant<IdTok, IntTok, FloatTok, StringTok, KeywordTok>;
std::string_view span;
Data data;

// ====================
// Token classification
// ====================

bool isLParen() const { return std::get_if<LParenTok>(&data); }

bool isRParen() const { return std::get_if<RParenTok>(&data); }

std::optional<std::string_view> getKeyword() const {
if (std::get_if<KeywordTok>(&data)) {
return span;
Expand Down Expand Up @@ -173,34 +153,25 @@ struct Lexer {
advance();
}

bool takeLParen() {
if (!curr || !curr->isLParen()) {
return false;
}
advance();
return true;
}
bool takeLParen();

bool peekLParen() { return Lexer(*this).takeLParen(); }

bool takeRParen() {
if (!curr || !curr->isRParen()) {
return false;
}
advance();
return true;
}
bool takeRParen();

bool peekRParen() { return Lexer(*this).takeRParen(); }

bool takeUntilParen() {
while (true) {
if (!curr) {
if (empty()) {
return false;
}
if (curr->isLParen() || curr->isRParen()) {
if (peekLParen() || peekRParen()) {
return true;
}
if (!curr) {
++index;
}
advance();
}
}
Expand Down Expand Up @@ -392,7 +363,7 @@ struct Lexer {
lexToken();
}

bool empty() const { return !curr; }
bool empty() const { return !curr && index == buffer.size(); }

TextPos position(const char* c) const;
TextPos position(size_t i) const { return position(buffer.data() + i); }
Expand Down
1 change: 0 additions & 1 deletion src/wasm/wasm-io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ void ModuleReader::readStdin(Module& wasm, std::string sourceMapFilename) {
} else {
std::ostringstream s;
s.write(input.data(), input.size());
s << '\0';
std::string input_str = s.str();
readTextData(input_str, wasm, profile);
}
Expand Down
3 changes: 0 additions & 3 deletions test/gtest/wat-lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ TEST(LexerTest, LexBlockComment) {
}

TEST(LexerTest, LexParens) {
Token left{"("sv, LParenTok{}};
Token right{")"sv, RParenTok{}};

Lexer lexer("(())"sv);

ASSERT_FALSE(lexer.empty());
Expand Down