Skip to content

Instantly share code, notes, and snippets.

@kripken
Created September 27, 2023 22:57
Show Gist options
  • Save kripken/a8493f8944e2fc5d8882fdff92f55e22 to your computer and use it in GitHub Desktop.
Save kripken/a8493f8944e2fc5d8882fdff92f55e22 to your computer and use it in GitHub Desktop.
diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp
index dec9249c3..b56348b31 100644
--- a/src/wasm/wasm-s-parser.cpp
+++ b/src/wasm/wasm-s-parser.cpp
@@ -42,70 +42,76 @@ int unhex(char c) {
}
if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
throw wasm::ParseException("invalid hexadecimal");
}
} // namespace
namespace wasm {
+// Similar to ParseException but built from an Element.
+struct SParseException : public ParseException {
+ SParseException(std::string text, const Element& s) :
+ ParseException(text + ": " + s.toString(), s.line, s.col) {}
+};
+
static Name STRUCT("struct"), FIELD("field"), ARRAY("array"), REC("rec"),
I8("i8"), I16("i16"), DECLARE("declare"), ITEM("item"), OFFSET("offset"),
SUB("sub"), FINAL("final");
static Address getAddress(const Element* s) {
return std::stoll(s->toString());
}
static void
checkAddress(Address a, const char* errorText, const Element* errorElem) {
if (a > std::numeric_limits<Address::address32_t>::max()) {
- throw ParseException(errorText, errorElem->line, errorElem->col);
+ throw SParseException(errorText, *errorElem);
}
}
static bool elementStartsWith(Element& s, IString str) {
return s.isList() && s.size() > 0 && s[0]->isStr() && s[0]->str() == str;
}
static bool elementStartsWith(Element* s, IString str) {
return elementStartsWith(*s, str);
}
Element::List& Element::list() {
if (!isList()) {
- throw ParseException("expected list", line, col);
+ throw SParseException("expected list", *this);
}
return list_;
}
Element* Element::operator[](unsigned i) {
if (!isList()) {
- throw ParseException("expected list", line, col);
+ throw SParseException("expected list", *this);
}
if (i >= list().size()) {
- throw ParseException("expected more elements in list", line, col);
+ throw SParseException("expected more elements in list", *this);
}
return list()[i];
}
IString Element::str() const {
if (!isStr()) {
- throw ParseException("expected string", line, col);
+ throw SParseException("expected string", *this);
}
return str_;
}
std::string Element::toString() const {
if (!isStr()) {
- throw ParseException("expected string", line, col);
+ throw SParseException("expected string", *this);
}
return str_.toString();
}
Element* Element::setString(IString str__, bool dollared__, bool quoted__) {
isList_ = false;
str_ = str__;
dollared_ = dollared__;
quoted_ = quoted__;
return this;
@@ -175,21 +181,21 @@ Element* SExpressionParser::parse() {
assert(stack.size() == stackLocs.size());
stack.pop_back();
loc = stackLocs.back();
stackLocs.pop_back();
curr->list().push_back(last);
} else {
curr->list().push_back(parseString());
}
}
if (stack.size() != 0) {
- throw ParseException("stack is not empty", curr->line, curr->col);
+ throw SParseException("stack is not empty", *curr);
}
return curr;
}
void SExpressionParser::parseDebugLocation() {
// Extracting debug location (if valid)
char const* debugLoc = input + 3; // skipping ";;@"
while (debugLoc[0] && debugLoc[0] == ' ') {
debugLoc++;
}
@@ -409,22 +415,22 @@ void SExpressionWasmBuilder::preParseImports(Element& curr) {
parseFunction(curr, true /* preParseImport */);
} else if (id == GLOBAL) {
parseGlobal(curr, true /* preParseImport */);
} else if (id == TABLE) {
parseTable(curr, true /* preParseImport */);
} else if (id == MEMORY) {
parseMemory(curr, true /* preParseImport */);
} else if (id == TAG) {
parseTag(curr, true /* preParseImport */);
} else {
- throw ParseException(
- "fancy import we don't support yet", curr.line, curr.col);
+ throw SParseException(
+ "fancy import we don't support yet", curr);
}
}
}
void SExpressionWasmBuilder::preParseMemory(Element& curr) {
IString id = curr[0]->str();
if (id == MEMORY && !isImport(curr)) {
parseMemory(curr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment