From f103e05bc3401436b0a85713c10a9e058cbf70fc Mon Sep 17 00:00:00 2001 From: Mike Lischke Date: Tue, 24 May 2016 17:39:21 +0200 Subject: [PATCH] Some additions for better operation. - Allow loading new input instead of setting up a new input stream. - Made is<> template function inline, for hopefully faster execution. - A few other fixes in stream + lexer classes. - Reverted another rename from context to _localctx in the Cpp.stg file as this is used for certain actions. --- runtime/Cpp/runtime/src/ANTLRFileStream.cpp | 4 +-- runtime/Cpp/runtime/src/ANTLRFileStream.h | 2 +- runtime/Cpp/runtime/src/ANTLRInputStream.cpp | 8 +++-- runtime/Cpp/runtime/src/ANTLRInputStream.h | 1 + .../Cpp/runtime/src/BufferedTokenStream.cpp | 1 + runtime/Cpp/runtime/src/Lexer.cpp | 7 ++++- runtime/Cpp/runtime/src/Lexer.h | 1 + runtime/Cpp/runtime/src/support/CPPUtils.h | 6 ++-- runtime/Cpp/runtime/src/tree/Trees.cpp | 31 +++++++++++++++++++ .../v4/tool/templates/codegen/Cpp/Cpp.stg | 8 ++--- 10 files changed, 56 insertions(+), 13 deletions(-) diff --git a/runtime/Cpp/runtime/src/ANTLRFileStream.cpp b/runtime/Cpp/runtime/src/ANTLRFileStream.cpp index 45860aed4..d41cea557 100755 --- a/runtime/Cpp/runtime/src/ANTLRFileStream.cpp +++ b/runtime/Cpp/runtime/src/ANTLRFileStream.cpp @@ -35,10 +35,10 @@ using namespace org::antlr::v4::runtime; ANTLRFileStream::ANTLRFileStream(const std::string &fileName) { _fileName = fileName; - load(fileName); + loadFromFile(fileName); } -void ANTLRFileStream::load(const std::string &fileName) { +void ANTLRFileStream::loadFromFile(const std::string &fileName) { _fileName = fileName; if (_fileName.empty()) { return; diff --git a/runtime/Cpp/runtime/src/ANTLRFileStream.h b/runtime/Cpp/runtime/src/ANTLRFileStream.h index 592302633..fe1db6812 100755 --- a/runtime/Cpp/runtime/src/ANTLRFileStream.h +++ b/runtime/Cpp/runtime/src/ANTLRFileStream.h @@ -49,7 +49,7 @@ namespace runtime { // Assumes a file name encoded in UTF-8 and file content in the same encoding (with or w/o BOM). ANTLRFileStream(const std::string &fileName); - virtual void load(const std::string &fileName); + virtual void loadFromFile(const std::string &fileName); virtual std::string getSourceName() const override; }; diff --git a/runtime/Cpp/runtime/src/ANTLRInputStream.cpp b/runtime/Cpp/runtime/src/ANTLRInputStream.cpp index f602321fa..2098bd051 100755 --- a/runtime/Cpp/runtime/src/ANTLRInputStream.cpp +++ b/runtime/Cpp/runtime/src/ANTLRInputStream.cpp @@ -45,8 +45,7 @@ using misc::Interval; ANTLRInputStream::ANTLRInputStream(const std::string &input) { InitializeInstanceFields(); - - data = utfConverter.from_bytes(input); + load(input); } ANTLRInputStream::ANTLRInputStream(const char data[], size_t numberOfActualCharsInArray) @@ -57,6 +56,11 @@ ANTLRInputStream::ANTLRInputStream(std::wistream &stream) { load(stream); } +void ANTLRInputStream::load(const std::string &input) { + data = utfConverter.from_bytes(input); + p = 0; +} + void ANTLRInputStream::load(std::wistream &stream) { if (!stream.good() || stream.eof()) // No fail, bad or EOF. return; diff --git a/runtime/Cpp/runtime/src/ANTLRInputStream.h b/runtime/Cpp/runtime/src/ANTLRInputStream.h index 1c9eef926..dcd0a9533 100755 --- a/runtime/Cpp/runtime/src/ANTLRInputStream.h +++ b/runtime/Cpp/runtime/src/ANTLRInputStream.h @@ -57,6 +57,7 @@ namespace runtime { ANTLRInputStream(const char data[], size_t numberOfActualCharsInArray); ANTLRInputStream(std::wistream &stream); + virtual void load(const std::string &input); virtual void load(std::wistream &stream); /// Reset the stream so that it's in the same state it was diff --git a/runtime/Cpp/runtime/src/BufferedTokenStream.cpp b/runtime/Cpp/runtime/src/BufferedTokenStream.cpp index 858a0bdfc..6b776f19a 100755 --- a/runtime/Cpp/runtime/src/BufferedTokenStream.cpp +++ b/runtime/Cpp/runtime/src/BufferedTokenStream.cpp @@ -213,6 +213,7 @@ void BufferedTokenStream::setup() { void BufferedTokenStream::setTokenSource(TokenSource *tokenSource) { _tokenSource = tokenSource; _tokens.clear(); + _fetchedEOF = false; _needSetup = true; } diff --git a/runtime/Cpp/runtime/src/Lexer.cpp b/runtime/Cpp/runtime/src/Lexer.cpp index 779cee2aa..461bc8c43 100755 --- a/runtime/Cpp/runtime/src/Lexer.cpp +++ b/runtime/Cpp/runtime/src/Lexer.cpp @@ -44,7 +44,12 @@ using namespace antlrcpp; using namespace org::antlr::v4::runtime; -Lexer::Lexer(CharStream *input) : _input(input) { +Lexer::Lexer() : Recognizer() { + InitializeInstanceFields(); + _input = nullptr; +} + +Lexer::Lexer(CharStream *input) : Recognizer(), _input(input) { InitializeInstanceFields(); } diff --git a/runtime/Cpp/runtime/src/Lexer.h b/runtime/Cpp/runtime/src/Lexer.h index 20f78ae02..a7efac75f 100755 --- a/runtime/Cpp/runtime/src/Lexer.h +++ b/runtime/Cpp/runtime/src/Lexer.h @@ -109,6 +109,7 @@ namespace runtime { /// the input char buffer. Use setText() or can set this instance var. std::string text; + Lexer(); Lexer(CharStream *input); virtual void reset(); diff --git a/runtime/Cpp/runtime/src/support/CPPUtils.h b/runtime/Cpp/runtime/src/support/CPPUtils.h index 2ad0fd2c6..8ac2bbc00 100644 --- a/runtime/Cpp/runtime/src/support/CPPUtils.h +++ b/runtime/Cpp/runtime/src/support/CPPUtils.h @@ -63,17 +63,17 @@ namespace antlrcpp { // Convenience functions to avoid lengthy dynamic_cast() != nullptr checks in many places. template - bool is(T2 &obj) { // For value types. + inline bool is(T2 &obj) { // For value types. return dynamic_cast::type *>(&obj) != nullptr; } template - bool is(T2 *obj) { // For pointer types. + inline bool is(T2 *obj) { // For pointer types. return dynamic_cast(obj) != nullptr; } template - bool is(Ref obj) { // For shared pointers. + inline bool is(Ref obj) { // For shared pointers. return dynamic_cast(obj.get()) != nullptr; } diff --git a/runtime/Cpp/runtime/src/tree/Trees.cpp b/runtime/Cpp/runtime/src/tree/Trees.cpp index 6042da291..b0269b997 100755 --- a/runtime/Cpp/runtime/src/tree/Trees.cpp +++ b/runtime/Cpp/runtime/src/tree/Trees.cpp @@ -69,12 +69,43 @@ std::string Trees::toStringTree(Ref t, const std::vector &rul std::stringstream ss; ss << "(" << temp << ' '; + /* for (size_t i = 0; i < t->getChildCount(); i++) { if (i > 0) { ss << ' '; } ss << toStringTree(t->getChild(i), ruleNames); } + */ + + // Implement the recursive walk as iteration to avoid trouble we deep nesting. + std::stack stack; + size_t childIndex = 0; + Ref run = t; + while (childIndex < run->getChildCount()) { + if (childIndex > 0) { + ss << ' '; + } + Ref child = run->getChild(childIndex); + std::string temp = antlrcpp::escapeWhitespace(Trees::getNodeText(child, ruleNames), false); + if (child->getChildCount() > 0) { + // Go deeper one level. + stack.push(childIndex + 1); + run = child; + childIndex = 0; + ss << "(" << temp << " "; + } else { + ss << temp; + if (++childIndex == run->getChildCount() && stack.size() > 0) { + // Reached the end of the current level. See if we can step up from here. + childIndex = stack.top(); + stack.pop(); + run = run->getParent().lock(); + ss << ")"; + } + } + } + ss << ")"; return ss.str(); } diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg index 709ae0b98..5eed14ad2 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg @@ -75,7 +75,7 @@ public: virtual void action(Ref\ context, int ruleIndex, int actionIndex) override; - virtual bool sempred(Ref\ context, int ruleIndex, int predicateIndex) override; + virtual bool sempred(Ref\ _localctx, int ruleIndex, int predicateIndex) override; private: @@ -237,11 +237,11 @@ void ::Action(Ref\<\> context, int ac >> RuleSempredFunctionHeader(r, actions) ::= << -bool Sempred(Ref\<\> context, int predicateIndex); +bool Sempred(Ref\<\> _localctx, int predicateIndex); >> RuleSempredFunction(r, actions) ::= << -bool ::Sempred(Ref\<\> context, int predicateIndex) { +bool ::Sempred(Ref\<\> _localctx, int predicateIndex) { switch (predicateIndex) { : return ;}; separator="\n"> @@ -288,7 +288,7 @@ public: - virtual bool sempred(Ref\ context, int ruleIndex, int predicateIndex) override; + virtual bool sempred(Ref\ _localctx, int ruleIndex, int predicateIndex) override;