TestParseTrees passes now.

This commit is contained in:
Mike Lischke 2016-06-01 15:20:11 +02:00
parent 6bf1193839
commit 28ccc4962b
4 changed files with 62 additions and 61 deletions

View File

@ -233,7 +233,7 @@ ImportListener(X) ::= ""
GetExpectedTokenNames() ::= "getExpectedTokens().toString(_tokenNames)" GetExpectedTokenNames() ::= "getExpectedTokens().toString(_tokenNames)"
RuleInvocationStack() ::= "str_list(getRuleInvocationStack())" RuleInvocationStack() ::= "Arrays::listToString(getRuleInvocationStack(), \", \")"
LL_EXACT_AMBIG_DETECTION() ::= <<getInterpreter\<atn::ParserATNSimulator>()->setPredictionMode(atn::PredictionMode::LL_EXACT_AMBIG_DETECTION);>> LL_EXACT_AMBIG_DETECTION() ::= <<getInterpreter\<atn::ParserATNSimulator>()->setPredictionMode(atn::PredictionMode::LL_EXACT_AMBIG_DETECTION);>>
@ -342,31 +342,28 @@ tree::ParseTreeWalker::DEFAULT->walk(std::make_shared\<LeafListener>(), <s>);
TreeNodeWithAltNumField(X) ::= << TreeNodeWithAltNumField(X) ::= <<
@parser::members { @parser::members {
class MyRuleNode(ParserRuleContext): class MyRuleNode : public ParserRuleContext {
def __init__(self, parent = None, invokingStateNumber = None ): public:
super(<X>Parser.MyRuleNode, self).__init__(parent, invokingStateNumber) int altNum;
altNum = 0; MyRuleNode(std::weak_ptr\<ParserRuleContext> parent, int invokingStateNumber)
def getAltNumber(self): : ParserRuleContext(parent, invokingStateNumber) {
return altNum }
def setAltNumber(self, altNum): virtual int getAltNumber() const override { return altNum; }
this->altNum = altNum virtual void setAltNumber(int altNum) override { this->altNum = altNum; }
};
} }
>> >>
TokenGetterListener(X) ::= << TokenGetterListener(X) ::= <<
if __name__ is not None and "." in __name__: class LeafListener : TBaseListener {
from .<X>Listener import <X>Listener public:
else: void exitA(Ref\<TParser::AContext> ctx) {
from <X>Listener import <X>Listener if (ctx->getChildCount() == 2)
std::cout \<\< ctx->INT(0)->getSymbol()->getText() \<\< ctx->INT(1)->getSymbol()->getText() \<\< ctx->INT() \<\< std::endl;
class LeafListener(TListener): else
def exitA(self, ctx): std::cout \<\< ctx->ID()->getSymbol() \<\< std::endl;
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);
} }
};
>> >>
RuleGetterListener(X) ::= << RuleGetterListener(X) ::= <<

View File

@ -13,14 +13,14 @@ public class TestParseTrees extends BaseCppTest {
public void test2AltLoop() throws Exception { public void test2AltLoop() throws Exception {
mkdir(tmpdir); mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(146); StringBuilder grammarBuilder = new StringBuilder(147);
grammarBuilder.append("grammar T;\n"); grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n"); grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n"); grammarBuilder.append("@init {\n");
grammarBuilder.append("_buildParseTrees = true\n"); grammarBuilder.append("_buildParseTrees = true;\n");
grammarBuilder.append("}\n"); grammarBuilder.append("}\n");
grammarBuilder.append("@after {\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("}\n");
grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : ('x' | 'y')* 'z'\n"); grammarBuilder.append("a : ('x' | 'y')* 'z'\n");
@ -41,14 +41,14 @@ public class TestParseTrees extends BaseCppTest {
public void test2Alts() throws Exception { public void test2Alts() throws Exception {
mkdir(tmpdir); mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(139); StringBuilder grammarBuilder = new StringBuilder(140);
grammarBuilder.append("grammar T;\n"); grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n"); grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n"); grammarBuilder.append("@init {\n");
grammarBuilder.append("_buildParseTrees = true\n"); grammarBuilder.append("_buildParseTrees = true;\n");
grammarBuilder.append("}\n"); grammarBuilder.append("}\n");
grammarBuilder.append("@after {\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("}\n");
grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' | 'y'\n"); grammarBuilder.append("a : 'x' | 'y'\n");
@ -69,29 +69,30 @@ public class TestParseTrees extends BaseCppTest {
public void testAltNum() throws Exception { public void testAltNum() throws Exception {
mkdir(tmpdir); mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(563); StringBuilder grammarBuilder = new StringBuilder(587);
grammarBuilder.append("grammar T;\n"); grammarBuilder.append("grammar T;\n");
grammarBuilder.append("\n"); grammarBuilder.append("\n");
grammarBuilder.append("options { contextSuperClass=MyRuleNode; }\n"); grammarBuilder.append("options { contextSuperClass=MyRuleNode; }\n");
grammarBuilder.append("\n"); grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n"); grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("class MyRuleNode(ParserRuleContext):\n"); grammarBuilder.append("class MyRuleNode : public ParserRuleContext {\n");
grammarBuilder.append(" def __init__(self, parent = None, invokingStateNumber = None ):\n"); grammarBuilder.append("public:\n");
grammarBuilder.append(" super(TParser.MyRuleNode, self).__init__(parent, invokingStateNumber)\n"); grammarBuilder.append(" int altNum;\n");
grammarBuilder.append(" altNum = 0;\n"); grammarBuilder.append(" MyRuleNode(std::weak_ptr<ParserRuleContext> parent, int invokingStateNumber)\n");
grammarBuilder.append(" def getAltNumber(self):\n"); grammarBuilder.append(" : ParserRuleContext(parent, invokingStateNumber) {\n");
grammarBuilder.append(" return altNum\n"); grammarBuilder.append(" }\n");
grammarBuilder.append(" def setAltNumber(self, altNum):\n"); grammarBuilder.append(" virtual int getAltNumber() const override { return altNum; }\n");
grammarBuilder.append(" this->altNum = 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("\n");
grammarBuilder.append("\n"); grammarBuilder.append("\n");
grammarBuilder.append("s\n"); grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n"); grammarBuilder.append("@init {\n");
grammarBuilder.append("_buildParseTrees = true\n"); grammarBuilder.append("_buildParseTrees = true;\n");
grammarBuilder.append("}\n"); grammarBuilder.append("}\n");
grammarBuilder.append("@after {\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("}\n");
grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("\n"); grammarBuilder.append("\n");
@ -117,14 +118,14 @@ public class TestParseTrees extends BaseCppTest {
public void testExtraToken() throws Exception { public void testExtraToken() throws Exception {
mkdir(tmpdir); mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(152); StringBuilder grammarBuilder = new StringBuilder(153);
grammarBuilder.append("grammar T;\n"); grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n"); grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n"); grammarBuilder.append("@init {\n");
grammarBuilder.append("_buildParseTrees = true\n"); grammarBuilder.append("_buildParseTrees = true;\n");
grammarBuilder.append("}\n"); grammarBuilder.append("}\n");
grammarBuilder.append("@after {\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("}\n");
grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' 'y'\n"); grammarBuilder.append("a : 'x' 'y'\n");
@ -149,14 +150,14 @@ public class TestParseTrees extends BaseCppTest {
public void testNoViableAlt() throws Exception { public void testNoViableAlt() throws Exception {
mkdir(tmpdir); mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(154); StringBuilder grammarBuilder = new StringBuilder(155);
grammarBuilder.append("grammar T;\n"); grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n"); grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n"); grammarBuilder.append("@init {\n");
grammarBuilder.append("_buildParseTrees = true\n"); grammarBuilder.append("_buildParseTrees = true;\n");
grammarBuilder.append("}\n"); grammarBuilder.append("}\n");
grammarBuilder.append("@after {\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("}\n");
grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' | 'y'\n"); grammarBuilder.append("a : 'x' | 'y'\n");
@ -181,14 +182,14 @@ public class TestParseTrees extends BaseCppTest {
public void testRuleRef() throws Exception { public void testRuleRef() throws Exception {
mkdir(tmpdir); mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(148); StringBuilder grammarBuilder = new StringBuilder(149);
grammarBuilder.append("grammar T;\n"); grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n"); grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n"); grammarBuilder.append("@init {\n");
grammarBuilder.append("_buildParseTrees = true\n"); grammarBuilder.append("_buildParseTrees = true;\n");
grammarBuilder.append("}\n"); grammarBuilder.append("}\n");
grammarBuilder.append("@after {\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("}\n");
grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b 'x'\n"); grammarBuilder.append("a : b 'x'\n");
@ -211,14 +212,14 @@ public class TestParseTrees extends BaseCppTest {
public void testSync() throws Exception { public void testSync() throws Exception {
mkdir(tmpdir); mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(155); StringBuilder grammarBuilder = new StringBuilder(156);
grammarBuilder.append("grammar T;\n"); grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n"); grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n"); grammarBuilder.append("@init {\n");
grammarBuilder.append("_buildParseTrees = true\n"); grammarBuilder.append("_buildParseTrees = true;\n");
grammarBuilder.append("}\n"); grammarBuilder.append("}\n");
grammarBuilder.append("@after {\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("}\n");
grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' 'y'* '!'\n"); grammarBuilder.append("a : 'x' 'y'* '!'\n");
@ -242,14 +243,14 @@ public class TestParseTrees extends BaseCppTest {
public void testToken2() throws Exception { public void testToken2() throws Exception {
mkdir(tmpdir); mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(137); StringBuilder grammarBuilder = new StringBuilder(138);
grammarBuilder.append("grammar T;\n"); grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n"); grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n"); grammarBuilder.append("@init {\n");
grammarBuilder.append("_buildParseTrees = true\n"); grammarBuilder.append("_buildParseTrees = true;\n");
grammarBuilder.append("}\n"); grammarBuilder.append("}\n");
grammarBuilder.append("@after {\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("}\n");
grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' 'y'\n"); grammarBuilder.append("a : 'x' 'y'\n");
@ -270,18 +271,18 @@ public class TestParseTrees extends BaseCppTest {
public void testTokenAndRuleContextString() throws Exception { public void testTokenAndRuleContextString() throws Exception {
mkdir(tmpdir); mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(193); StringBuilder grammarBuilder = new StringBuilder(217);
grammarBuilder.append("grammar T;\n"); grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n"); grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n"); grammarBuilder.append("@init {\n");
grammarBuilder.append("_buildParseTrees = true\n"); grammarBuilder.append("_buildParseTrees = true;\n");
grammarBuilder.append("}\n"); grammarBuilder.append("}\n");
grammarBuilder.append("@after {\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("}\n");
grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' { \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("} ;"); grammarBuilder.append("} ;");
String grammar = grammarBuilder.toString(); String grammar = grammarBuilder.toString();

View File

@ -1037,7 +1037,7 @@ Ref<ATNConfig> ParserATNSimulator::precedenceTransition(Ref<ATNConfig> config, P
if (debug) { if (debug) {
std::cout << "PRED (collectPredicates=" << collectPredicates << ") " << pt->precedence << ">=_p" << ", ctx dependent=true" << std::endl; std::cout << "PRED (collectPredicates=" << collectPredicates << ") " << pt->precedence << ">=_p" << ", ctx dependent=true" << std::endl;
if (parser != nullptr) { 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;
} }
} }

View File

@ -37,14 +37,17 @@ std::string Arrays::listToString(const std::vector<std::string> &list, const std
{ {
std::stringstream ss; std::stringstream ss;
bool firstEntry = true; bool firstEntry = true;
ss << '[';
for (auto &entry : list) { for (auto &entry : list) {
ss << entry; ss << entry;
if (!firstEntry) if (firstEntry) {
ss << separator; ss << separator;
firstEntry = false; firstEntry = false;
}
} }
ss << ']';
return ss.str(); return ss.str();
} }