diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2016-07-26 21:17:20 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2016-07-26 21:17:20 +0200 |
commit | 4d8b6e0ceeb9c8df06decf33b83781adc00cbead (patch) | |
tree | 8adb7fffd5f609a7549f2cf21eca7803d8c1a326 /include-priv/trygvis | |
parent | ea58b3887a7deba05e3fc85eb536038a547f8871 (diff) | |
download | elfinfo-master.tar.gz elfinfo-master.tar.bz2 elfinfo-master.tar.xz elfinfo-master.zip |
Diffstat (limited to 'include-priv/trygvis')
-rw-r--r-- | include-priv/trygvis/antlr.h | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/include-priv/trygvis/antlr.h b/include-priv/trygvis/antlr.h new file mode 100644 index 0000000..f5656ea --- /dev/null +++ b/include-priv/trygvis/antlr.h @@ -0,0 +1,67 @@ +#pragma once + +#include "antlr4-runtime.h" + +namespace trygvis { +namespace antlr { + +// This namespace is shared copied code + +using ParseTree = antlr4::tree::ParseTree; + +class MissingParseTreeProperty : public std::out_of_range { +public: + explicit MissingParseTreeProperty(const std::string &what) : out_of_range(what) { } +}; + +template<typename V, bool debug = false> +class ParseTreeProperty { +public: + virtual V get(Ref<ParseTree> node) { + return get(node.get()); + } + + virtual V get(ParseTree *const node) { + if (!debug) { + return _annotations.at(node); + } + + try { +// cerr << "node = " << node->getText() << endl; + return _annotations.at(node); + } catch (std::out_of_range &e) { + std::cerr << "get(" << node << "), text=" << node->getText() << std::endl; + std::stringstream buf; + buf << "out of range: " << node << ", text=" << node->getText(); + auto msg = buf.str(); + std::cerr << msg << std::endl; + throw MissingParseTreeProperty(msg); + } + } + + virtual void put(ParseTree *const node, V value) { + if (debug) { + std::cerr << "put(" << node << ", " << value << "), text: " << node->getText() << std::endl; + } + _annotations[node] = value; + } + + virtual V removeFrom(ParseTree *const node) { + auto it = _annotations.find(node); + + if (it == _annotations.end()) { + throw MissingParseTreeProperty(node->getText()); + } + + return it->second; + } + +protected: + std::map<ParseTree *, V> _annotations; + +private: +}; + +} // namespace antlr + +} |