summaryrefslogtreecommitdiff
path: root/src/parser/input-impl.h
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2024-02-05 10:24:16 -0800
committerGitHub <noreply@github.com>2024-02-05 10:24:16 -0800
commited15efeedd33bbdbadfafabc812d70a792a9a06c (patch)
tree187d8cdd828369022d406e517d92be1f85ab2443 /src/parser/input-impl.h
parent845e0700c99d603c920d82c2312ec9b39a555c68 (diff)
downloadbinaryen-ed15efeedd33bbdbadfafabc812d70a792a9a06c.tar.gz
binaryen-ed15efeedd33bbdbadfafabc812d70a792a9a06c.tar.bz2
binaryen-ed15efeedd33bbdbadfafabc812d70a792a9a06c.zip
[Parser] Templatize lexing of integers (#6272)
Have a single implementation for lexing each of unsigned, signed, and uninterpreted integers, each generic over the bit width of the integer. This reduces duplication in the existing code and it will make it much easier to support lexing more 8- and 16-bit integers.
Diffstat (limited to 'src/parser/input-impl.h')
-rw-r--r--src/parser/input-impl.h66
1 files changed, 16 insertions, 50 deletions
diff --git a/src/parser/input-impl.h b/src/parser/input-impl.h
index 0f8fc2e86..3ffce07f8 100644
--- a/src/parser/input-impl.h
+++ b/src/parser/input-impl.h
@@ -100,7 +100,7 @@ inline std::optional<uint64_t> ParseInput::takeOffset() {
if (subLexer == subLexer.end()) {
return {};
}
- if (auto o = subLexer->getU64()) {
+ if (auto o = subLexer->getU<uint64_t>()) {
++subLexer;
if (subLexer == subLexer.end()) {
++lexer;
@@ -122,7 +122,7 @@ inline std::optional<uint32_t> ParseInput::takeAlign() {
if (subLexer == subLexer.end()) {
return {};
}
- if (auto a = subLexer->getU32()) {
+ if (auto a = subLexer->getU<uint32_t>()) {
++subLexer;
if (subLexer == subLexer.end()) {
++lexer;
@@ -134,9 +134,9 @@ inline std::optional<uint32_t> ParseInput::takeAlign() {
return {};
}
-inline std::optional<uint64_t> ParseInput::takeU64() {
+template<typename T> inline std::optional<T> ParseInput::takeU() {
if (auto t = peek()) {
- if (auto n = t->getU64()) {
+ if (auto n = t->getU<T>()) {
++lexer;
return n;
}
@@ -144,67 +144,33 @@ inline std::optional<uint64_t> ParseInput::takeU64() {
return std::nullopt;
}
-inline std::optional<int64_t> ParseInput::takeS64() {
+template<typename T> inline std::optional<T> ParseInput::takeI() {
if (auto t = peek()) {
- if (auto n = t->getS64()) {
+ if (auto n = t->getI<T>()) {
++lexer;
return n;
}
}
- return {};
+ return std::nullopt;
}
-inline std::optional<int64_t> ParseInput::takeI64() {
- if (auto t = peek()) {
- if (auto n = t->getI64()) {
- ++lexer;
- return n;
- }
- }
- return {};
+inline std::optional<uint64_t> ParseInput::takeU64() {
+ return takeU<uint64_t>();
}
-inline std::optional<uint32_t> ParseInput::takeU32() {
- if (auto t = peek()) {
- if (auto n = t->getU32()) {
- ++lexer;
- return n;
- }
- }
- return std::nullopt;
+inline std::optional<uint64_t> ParseInput::takeI64() {
+ return takeI<uint64_t>();
}
-inline std::optional<int32_t> ParseInput::takeS32() {
- if (auto t = peek()) {
- if (auto n = t->getS32()) {
- ++lexer;
- return n;
- }
- }
- return {};
+inline std::optional<uint32_t> ParseInput::takeU32() {
+ return takeU<uint64_t>();
}
-inline std::optional<int32_t> ParseInput::takeI32() {
- if (auto t = peek()) {
- if (auto n = t->getI32()) {
- ++lexer;
- return n;
- }
- }
- return {};
+inline std::optional<uint32_t> ParseInput::takeI32() {
+ return takeI<uint32_t>();
}
-inline std::optional<uint8_t> ParseInput::takeU8() {
- if (auto t = peek()) {
- if (auto n = t->getU32()) {
- if (n <= std::numeric_limits<uint8_t>::max()) {
- ++lexer;
- return uint8_t(*n);
- }
- }
- }
- return {};
-}
+inline std::optional<uint8_t> ParseInput::takeU8() { return takeU<uint8_t>(); }
inline std::optional<double> ParseInput::takeF64() {
if (auto t = peek()) {