diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/cpp/Cpp.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/cpp/Cpp.test.stg index 88cb9d4ac..224b504eb 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/cpp/Cpp.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/cpp/Cpp.test.stg @@ -233,7 +233,7 @@ ImportListener(X) ::= "" GetExpectedTokenNames() ::= "getExpectedTokens().toString(_tokenNames)" -RuleInvocationStack() ::= "str_list(getRuleInvocationStack())" +RuleInvocationStack() ::= "Arrays::listToString(getRuleInvocationStack(), \", \")" LL_EXACT_AMBIG_DETECTION() ::= <()->setPredictionMode(atn::PredictionMode::LL_EXACT_AMBIG_DETECTION);>> @@ -342,31 +342,28 @@ tree::ParseTreeWalker::DEFAULT->walk(std::make_shared\(), ); TreeNodeWithAltNumField(X) ::= << @parser::members { -class MyRuleNode(ParserRuleContext): - def __init__(self, parent = None, invokingStateNumber = None ): - super(Parser.MyRuleNode, self).__init__(parent, invokingStateNumber) - altNum = 0; - def getAltNumber(self): - return altNum - def setAltNumber(self, altNum): - this->altNum = altNum +class MyRuleNode : public ParserRuleContext { +public: + int altNum; + MyRuleNode(std::weak_ptr\ parent, int invokingStateNumber) + : ParserRuleContext(parent, invokingStateNumber) { + } + virtual int getAltNumber() const override { return altNum; } + virtual void setAltNumber(int altNum) override { this->altNum = altNum; } +}; } >> TokenGetterListener(X) ::= << -if __name__ is not None and "." in __name__: - from .Listener import Listener -else: - from Listener import Listener - -class LeafListener(TListener): - def exitA(self, ctx): - if (ctx.getChildCount()==2) { - std::cout \<\< ctx.INT(0).symbol.text \<\< " " \<\< ctx.INT(1).symbol.text \<\< " " \<\< str_list(ctx.INT()); - } else { - std::cout \<\< str(ctx.ID().symbol); +class LeafListener : TBaseListener { +public: + void exitA(Ref\ ctx) { + if (ctx->getChildCount() == 2) + std::cout \<\< ctx->INT(0)->getSymbol()->getText() \<\< ctx->INT(1)->getSymbol()->getText() \<\< ctx->INT() \<\< std::endl; + else + std::cout \<\< ctx->ID()->getSymbol() \<\< std::endl; } - +}; >> RuleGetterListener(X) ::= << diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/TestParseTrees.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/TestParseTrees.java index 0dcc8f422..1cc98cfe1 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/TestParseTrees.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/TestParseTrees.java @@ -13,14 +13,14 @@ public class TestParseTrees extends BaseCppTest { public void test2AltLoop() throws Exception { mkdir(tmpdir); - StringBuilder grammarBuilder = new StringBuilder(146); + StringBuilder grammarBuilder = new StringBuilder(147); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s\n"); grammarBuilder.append("@init {\n"); - grammarBuilder.append("_buildParseTrees = true\n"); + grammarBuilder.append("_buildParseTrees = true;\n"); grammarBuilder.append("}\n"); grammarBuilder.append("@after {\n"); - grammarBuilder.append("std::cout << $r.ctx.toStringTree(recog=self) << \"\\n\";\n"); + grammarBuilder.append("std::cout << $r.ctx->toStringTree(this) << std::endl;\n"); grammarBuilder.append("}\n"); grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append("a : ('x' | 'y')* 'z'\n"); @@ -41,14 +41,14 @@ public class TestParseTrees extends BaseCppTest { public void test2Alts() throws Exception { mkdir(tmpdir); - StringBuilder grammarBuilder = new StringBuilder(139); + StringBuilder grammarBuilder = new StringBuilder(140); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s\n"); grammarBuilder.append("@init {\n"); - grammarBuilder.append("_buildParseTrees = true\n"); + grammarBuilder.append("_buildParseTrees = true;\n"); grammarBuilder.append("}\n"); grammarBuilder.append("@after {\n"); - grammarBuilder.append("std::cout << $r.ctx.toStringTree(recog=self) << \"\\n\";\n"); + grammarBuilder.append("std::cout << $r.ctx->toStringTree(this) << std::endl;\n"); grammarBuilder.append("}\n"); grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append("a : 'x' | 'y'\n"); @@ -69,29 +69,30 @@ public class TestParseTrees extends BaseCppTest { public void testAltNum() throws Exception { mkdir(tmpdir); - StringBuilder grammarBuilder = new StringBuilder(563); + StringBuilder grammarBuilder = new StringBuilder(587); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("\n"); grammarBuilder.append("options { contextSuperClass=MyRuleNode; }\n"); grammarBuilder.append("\n"); grammarBuilder.append("@parser::members {\n"); - grammarBuilder.append("class MyRuleNode(ParserRuleContext):\n"); - grammarBuilder.append(" def __init__(self, parent = None, invokingStateNumber = None ):\n"); - grammarBuilder.append(" super(TParser.MyRuleNode, self).__init__(parent, invokingStateNumber)\n"); - grammarBuilder.append(" altNum = 0;\n"); - grammarBuilder.append(" def getAltNumber(self):\n"); - grammarBuilder.append(" return altNum\n"); - grammarBuilder.append(" def setAltNumber(self, altNum):\n"); - grammarBuilder.append(" this->altNum = altNum\n"); + grammarBuilder.append("class MyRuleNode : public ParserRuleContext {\n"); + grammarBuilder.append("public:\n"); + grammarBuilder.append(" int altNum;\n"); + grammarBuilder.append(" MyRuleNode(std::weak_ptr parent, int invokingStateNumber)\n"); + grammarBuilder.append(" : ParserRuleContext(parent, invokingStateNumber) {\n"); + grammarBuilder.append(" }\n"); + grammarBuilder.append(" virtual int getAltNumber() const override { return altNum; }\n"); + grammarBuilder.append(" virtual void setAltNumber(int altNum) override { this->altNum = altNum; }\n"); + grammarBuilder.append("};\n"); grammarBuilder.append("}\n"); grammarBuilder.append("\n"); grammarBuilder.append("\n"); grammarBuilder.append("s\n"); grammarBuilder.append("@init {\n"); - grammarBuilder.append("_buildParseTrees = true\n"); + grammarBuilder.append("_buildParseTrees = true;\n"); grammarBuilder.append("}\n"); grammarBuilder.append("@after {\n"); - grammarBuilder.append("std::cout << $r.ctx.toStringTree(recog=self) << \"\\n\";\n"); + grammarBuilder.append("std::cout << $r.ctx->toStringTree(this) << std::endl;\n"); grammarBuilder.append("}\n"); grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append("\n"); @@ -117,14 +118,14 @@ public class TestParseTrees extends BaseCppTest { public void testExtraToken() throws Exception { mkdir(tmpdir); - StringBuilder grammarBuilder = new StringBuilder(152); + StringBuilder grammarBuilder = new StringBuilder(153); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s\n"); grammarBuilder.append("@init {\n"); - grammarBuilder.append("_buildParseTrees = true\n"); + grammarBuilder.append("_buildParseTrees = true;\n"); grammarBuilder.append("}\n"); grammarBuilder.append("@after {\n"); - grammarBuilder.append("std::cout << $r.ctx.toStringTree(recog=self) << \"\\n\";\n"); + grammarBuilder.append("std::cout << $r.ctx->toStringTree(this) << std::endl;\n"); grammarBuilder.append("}\n"); grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append("a : 'x' 'y'\n"); @@ -149,14 +150,14 @@ public class TestParseTrees extends BaseCppTest { public void testNoViableAlt() throws Exception { mkdir(tmpdir); - StringBuilder grammarBuilder = new StringBuilder(154); + StringBuilder grammarBuilder = new StringBuilder(155); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s\n"); grammarBuilder.append("@init {\n"); - grammarBuilder.append("_buildParseTrees = true\n"); + grammarBuilder.append("_buildParseTrees = true;\n"); grammarBuilder.append("}\n"); grammarBuilder.append("@after {\n"); - grammarBuilder.append("std::cout << $r.ctx.toStringTree(recog=self) << \"\\n\";\n"); + grammarBuilder.append("std::cout << $r.ctx->toStringTree(this) << std::endl;\n"); grammarBuilder.append("}\n"); grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append("a : 'x' | 'y'\n"); @@ -181,14 +182,14 @@ public class TestParseTrees extends BaseCppTest { public void testRuleRef() throws Exception { mkdir(tmpdir); - StringBuilder grammarBuilder = new StringBuilder(148); + StringBuilder grammarBuilder = new StringBuilder(149); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s\n"); grammarBuilder.append("@init {\n"); - grammarBuilder.append("_buildParseTrees = true\n"); + grammarBuilder.append("_buildParseTrees = true;\n"); grammarBuilder.append("}\n"); grammarBuilder.append("@after {\n"); - grammarBuilder.append("std::cout << $r.ctx.toStringTree(recog=self) << \"\\n\";\n"); + grammarBuilder.append("std::cout << $r.ctx->toStringTree(this) << std::endl;\n"); grammarBuilder.append("}\n"); grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append("a : b 'x'\n"); @@ -211,14 +212,14 @@ public class TestParseTrees extends BaseCppTest { public void testSync() throws Exception { mkdir(tmpdir); - StringBuilder grammarBuilder = new StringBuilder(155); + StringBuilder grammarBuilder = new StringBuilder(156); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s\n"); grammarBuilder.append("@init {\n"); - grammarBuilder.append("_buildParseTrees = true\n"); + grammarBuilder.append("_buildParseTrees = true;\n"); grammarBuilder.append("}\n"); grammarBuilder.append("@after {\n"); - grammarBuilder.append("std::cout << $r.ctx.toStringTree(recog=self) << \"\\n\";\n"); + grammarBuilder.append("std::cout << $r.ctx->toStringTree(this) << std::endl;\n"); grammarBuilder.append("}\n"); grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append("a : 'x' 'y'* '!'\n"); @@ -242,14 +243,14 @@ public class TestParseTrees extends BaseCppTest { public void testToken2() throws Exception { mkdir(tmpdir); - StringBuilder grammarBuilder = new StringBuilder(137); + StringBuilder grammarBuilder = new StringBuilder(138); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s\n"); grammarBuilder.append("@init {\n"); - grammarBuilder.append("_buildParseTrees = true\n"); + grammarBuilder.append("_buildParseTrees = true;\n"); grammarBuilder.append("}\n"); grammarBuilder.append("@after {\n"); - grammarBuilder.append("std::cout << $r.ctx.toStringTree(recog=self) << \"\\n\";\n"); + grammarBuilder.append("std::cout << $r.ctx->toStringTree(this) << std::endl;\n"); grammarBuilder.append("}\n"); grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append("a : 'x' 'y'\n"); @@ -270,18 +271,18 @@ public class TestParseTrees extends BaseCppTest { public void testTokenAndRuleContextString() throws Exception { mkdir(tmpdir); - StringBuilder grammarBuilder = new StringBuilder(193); + StringBuilder grammarBuilder = new StringBuilder(217); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s\n"); grammarBuilder.append("@init {\n"); - grammarBuilder.append("_buildParseTrees = true\n"); + grammarBuilder.append("_buildParseTrees = true;\n"); grammarBuilder.append("}\n"); grammarBuilder.append("@after {\n"); - grammarBuilder.append("std::cout << $r.ctx.toStringTree(recog=self) << \"\\n\";\n"); + grammarBuilder.append("std::cout << $r.ctx->toStringTree(this) << std::endl;\n"); grammarBuilder.append("}\n"); grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append("a : 'x' { \n"); - grammarBuilder.append("std::cout << str_list(getRuleInvocationStack()) << \"\\n\";\n"); + grammarBuilder.append("std::cout << Arrays::listToString(getRuleInvocationStack(), \", \") << std::endl;\n"); grammarBuilder.append("} ;"); String grammar = grammarBuilder.toString(); diff --git a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp index 48f23f1e6..6d27fc763 100755 --- a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp +++ b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp @@ -1037,7 +1037,7 @@ Ref ParserATNSimulator::precedenceTransition(Ref config, P if (debug) { std::cout << "PRED (collectPredicates=" << collectPredicates << ") " << pt->precedence << ">=_p" << ", ctx dependent=true" << std::endl; if (parser != nullptr) { - std::cout << "context surrounding pred is " << Arrays::listToString( parser->getRuleInvocationStack(), ", ") << std::endl; + std::cout << "context surrounding pred is " << Arrays::listToString(parser->getRuleInvocationStack(), ", ") << std::endl; } } diff --git a/runtime/Cpp/runtime/src/support/Arrays.cpp b/runtime/Cpp/runtime/src/support/Arrays.cpp index 49e51f3f5..2cd49e737 100644 --- a/runtime/Cpp/runtime/src/support/Arrays.cpp +++ b/runtime/Cpp/runtime/src/support/Arrays.cpp @@ -37,14 +37,17 @@ std::string Arrays::listToString(const std::vector &list, const std { std::stringstream ss; bool firstEntry = true; - + + ss << '['; for (auto &entry : list) { ss << entry; - if (!firstEntry) + if (firstEntry) { ss << separator; - firstEntry = false; + firstEntry = false; + } } + ss << ']'; return ss.str(); }