summaryrefslogtreecommitdiff
path: root/src/parser/input.h
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2023-09-19 11:08:58 -0700
committerGitHub <noreply@github.com>2023-09-19 18:08:58 +0000
commit0fe05b19eb01c65fedbaee8e102c4f7aa0e5fdda (patch)
tree32e3616b9f8e476a4bea123fa2fdee6eeaee7b8d /src/parser/input.h
parenta51bd6df919a5b79574f0996a760cc20cb05697e (diff)
downloadbinaryen-0fe05b19eb01c65fedbaee8e102c4f7aa0e5fdda.tar.gz
binaryen-0fe05b19eb01c65fedbaee8e102c4f7aa0e5fdda.tar.bz2
binaryen-0fe05b19eb01c65fedbaee8e102c4f7aa0e5fdda.zip
[NFC] Split the new wat parser into multiple files (#5960)
And put the new files in a new source directory, "parser". This is a rough split and is not yet expected to dramatically improve compile times. The exact organization of the new files is subject to change, but this splitting should be enough to make further parser development more pleasant.
Diffstat (limited to 'src/parser/input.h')
-rw-r--r--src/parser/input.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/parser/input.h b/src/parser/input.h
new file mode 100644
index 000000000..5c7c57d20
--- /dev/null
+++ b/src/parser/input.h
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2023 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef parser_input_h
+#define parser_input_h
+
+#include "lexer.h"
+#include "support/result.h"
+#include "wasm.h"
+
+namespace wasm::WATParser {
+
+using namespace std::string_view_literals;
+
+// Wraps a lexer and provides utilities for consuming tokens.
+struct ParseInput {
+ Lexer lexer;
+
+ explicit ParseInput(std::string_view in) : lexer(in) {}
+
+ ParseInput(std::string_view in, size_t index) : lexer(in) {
+ lexer.setIndex(index);
+ }
+
+ ParseInput(const ParseInput& other, size_t index) : lexer(other.lexer) {
+ lexer.setIndex(index);
+ }
+
+ bool empty() { return lexer.empty(); }
+
+ std::optional<Token> peek();
+ bool takeLParen();
+ bool takeRParen();
+ bool takeUntilParen();
+ std::optional<Name> takeID();
+ std::optional<std::string_view> takeKeyword();
+ bool takeKeyword(std::string_view expected);
+ std::optional<uint64_t> takeOffset();
+ std::optional<uint32_t> takeAlign();
+ std::optional<uint64_t> takeU64();
+ std::optional<int64_t> takeS64();
+ std::optional<int64_t> takeI64();
+ std::optional<uint32_t> takeU32();
+ std::optional<int32_t> takeS32();
+ std::optional<int32_t> takeI32();
+ std::optional<uint8_t> takeU8();
+ std::optional<double> takeF64();
+ std::optional<float> takeF32();
+ std::optional<std::string_view> takeString();
+ std::optional<Name> takeName();
+ bool takeSExprStart(std::string_view expected);
+
+ Index getPos();
+ [[nodiscard]] Err err(Index pos, std::string reason);
+ [[nodiscard]] Err err(std::string reason) { return err(getPos(), reason); }
+};
+
+#include "input-impl.h"
+
+} // namespace wasm::WATParser
+
+#endif // parser_input_h