diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-05-12 14:58:20 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-05-12 15:38:29 -0700 |
commit | 031f22eb8e84e098afceffa787126d8b6ab79e92 (patch) | |
tree | 2330641d3fceb14a29c6d6d8a74e86448cfbbebe /src | |
parent | a18b153e1940b6e1504084c60f3feca46dfa2870 (diff) | |
download | binaryen-031f22eb8e84e098afceffa787126d8b6ab79e92.tar.gz binaryen-031f22eb8e84e098afceffa787126d8b6ab79e92.tar.bz2 binaryen-031f22eb8e84e098afceffa787126d8b6ab79e92.zip |
parse error details
Diffstat (limited to 'src')
-rw-r--r-- | src/parsing.h | 27 | ||||
-rw-r--r-- | src/shell-interface.h | 1 | ||||
-rw-r--r-- | src/wasm-binary.h | 16 | ||||
-rw-r--r-- | src/wasm-s-parser.h | 28 |
4 files changed, 49 insertions, 23 deletions
diff --git a/src/parsing.h b/src/parsing.h index 2104337a6..6745ad7cd 100644 --- a/src/parsing.h +++ b/src/parsing.h @@ -17,12 +17,15 @@ #ifndef wasm_parsing_h #define wasm_parsing_h +#include <ostream> #include <sstream> +#include <string> #include "asmjs/shared-constants.h" #include "mixed_arena.h" #include "support/utilities.h" #include "wasm.h" +#include "wasm-printing.h" namespace wasm { @@ -162,6 +165,30 @@ inline Expression* parseConst(cashew::IString s, WasmType type, MixedArena& allo return ret; } +struct ParseException { + std::string text; + size_t line, col; + + ParseException() : text("unknown parse error"), line(-1), col(-1) {} + ParseException(std::string text) : text(text), line(-1), col(-1) {} + ParseException(std::string text, size_t line, size_t col) : text(text), line(line), col(col) {} + + void dump(std::ostream& o) { + Colors::magenta(o); + o << "["; + Colors::red(o); + o << "parse exception: "; + Colors::green(o); + o << text; + if (line != size_t(-1)) { + Colors::normal(o); + o << " (at " << line << ":" << col << ")"; + } + Colors::magenta(o); + o << "]"; + Colors::normal(o); + } +}; } // namespace wasm diff --git a/src/shell-interface.h b/src/shell-interface.h index 5b6a64e79..b87460a48 100644 --- a/src/shell-interface.h +++ b/src/shell-interface.h @@ -29,7 +29,6 @@ namespace wasm { struct ExitException {}; struct TrapException {}; -struct ParseException {}; struct ShellExternalInterface : ModuleInstance::ExternalInterface { // The underlying memory can be accessed through unaligned pointers which diff --git a/src/wasm-binary.h b/src/wasm-binary.h index e49abe71e..bd78d9f9d 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -32,7 +32,7 @@ #include "asm_v_wasm.h" #include "wasm-builder.h" #include "ast_utils.h" -#include "shell-interface.h" +#include "parsing.h" #include "wasm-validator.h" namespace wasm { @@ -1226,7 +1226,7 @@ public: } uint8_t getInt8() { - if (!more()) throw ParseException(); + if (!more()) throw ParseException("unexpected end of input"); if (debug) std::cerr << "getInt8: " << (int)(uint8_t)input[pos] << " (at " << pos << ")" << std::endl; return input[pos++]; } @@ -1330,27 +1330,27 @@ public: void verifyInt8(int8_t x) { int8_t y = getInt8(); - if (x != y) throw ParseException(); + if (x != y) throw ParseException("surprising value", 0, pos); } void verifyInt16(int16_t x) { int16_t y = getInt16(); - if (x != y) throw ParseException(); + if (x != y) throw ParseException("surprising value", 0, pos); } void verifyInt32(int32_t x) { int32_t y = getInt32(); - if (x != y) throw ParseException(); + if (x != y) throw ParseException("surprising value", 0, pos); } void verifyInt64(int64_t x) { int64_t y = getInt64(); - if (x != y) throw ParseException(); + if (x != y) throw ParseException("surprising value", 0, pos); } void verifyFloat32(float x) { float y = getFloat32(); - if (x != y) throw ParseException(); + if (x != y) throw ParseException("surprising value", 0, pos); } void verifyFloat64(double x) { double y = getFloat64(); - if (x != y) throw ParseException(); + if (x != y) throw ParseException("surprising value", 0, pos); } void ungetInt8() { diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index e38c33ce6..e0f85227d 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -299,7 +299,7 @@ private: return; } else if (id == TYPE) { Name typeName = curr[1]->str(); - if (!wasm.checkFunctionType(typeName)) throw ParseException(); + if (!wasm.checkFunctionType(typeName)) throw ParseException("unknown function"); FunctionType* type = wasm.getFunctionType(typeName); functionTypes[name] = type->result; return; @@ -323,7 +323,7 @@ private: if (id == TABLE) return parseTable(curr); if (id == TYPE) return; // already done std::cerr << "bad module element " << id.str << '\n'; - throw ParseException(); + throw ParseException("unknown module element"); } // function parsing state @@ -343,7 +343,7 @@ private: } else { // index size_t offset = atoi(s.str().c_str()); - if (offset >= functionNames.size()) throw ParseException(); + if (offset >= functionNames.size()) throw ParseException("unknown function"); return functionNames[offset]; } } @@ -413,7 +413,7 @@ private: } else if (id == TYPE) { Name name = curr[1]->str(); type = name; - if (!wasm.checkFunctionType(name)) throw ParseException(); + if (!wasm.checkFunctionType(name)) throw ParseException("unknown function"); FunctionType* type = wasm.getFunctionType(name); result = type->result; for (size_t j = 0; j < type->params.size(); j++) { @@ -468,7 +468,7 @@ private: if (str[1] == '6' && str[2] == '4' && (prefix || str[3] == 0)) return f64; } if (allowError) return none; - throw ParseException(); + throw ParseException("unknown type"); abort(); } @@ -477,7 +477,7 @@ public: return parseExpression(*s); } - #define abort_on(str) { std::cerr << "aborting on " << str << '\n'; throw ParseException(); } + #define abort_on(str) { throw ParseException(std::string("abort_on ") + str); } Expression* parseExpression(Element& s) { IString id = s[0]->str(); @@ -816,7 +816,7 @@ private: Expression* makeConst(Element& s, WasmType type) { auto ret = parseConst(s[1]->str(), type, allocator); - if (!ret) throw ParseException(); + if (!ret) throw ParseException("bad const"); return ret; } @@ -850,9 +850,9 @@ private: ret->align = atoi(eq); } else if (str[0] == 'o') { uint64_t offset = atoll(eq); - if (offset > std::numeric_limits<uint32_t>::max()) throw ParseException(); + if (offset > std::numeric_limits<uint32_t>::max()) throw ParseException("bad offset"); ret->offset = (uint32_t)offset; - } else throw ParseException(); + } else throw ParseException("bad load attribute"); i++; } ret->ptr = parseExpression(s[i]); @@ -888,7 +888,7 @@ private: ret->align = atoi(eq); } else if (str[0] == 'o') { ret->offset = atoi(eq); - } else throw ParseException(); + } else throw ParseException("bad store attribute"); i++; } ret->ptr = parseExpression(s[i]); @@ -1136,7 +1136,7 @@ private: void parseExport(Element& s) { if (!s[2]->dollared() && !std::isdigit(s[2]->str()[0])) { assert(s[2]->str() == MEMORY); - if (!hasMemory) throw ParseException(); + if (!hasMemory) throw ParseException("memory exported but no memory"); wasm.memory.exportName = s[1]->str(); return; } @@ -1156,7 +1156,7 @@ private: } importCounter++; im->module = s[i++]->str(); - if (!s[i]->isStr()) throw ParseException(); + if (!s[i]->isStr()) throw ParseException("no name for import"); im->base = s[i++]->str(); std::unique_ptr<FunctionType> type = make_unique<FunctionType>(); if (s.size() > i) { @@ -1170,10 +1170,10 @@ private: type->result = stringToWasmType(params[1]->str()); } else if (id == TYPE) { IString name = params[1]->str(); - if (!wasm.checkFunctionType(name)) throw ParseException(); + if (!wasm.checkFunctionType(name)) throw ParseException("bad function type for import"); *type = *wasm.getFunctionType(name); } else { - throw ParseException(); + throw ParseException("bad import element"); } if (s.size() > i+1) { Element& result = *s[i+1]; |