forked from jasder/antlr
Updates to get all tests green again.
- Had to take back some of the message beautifying, as this won't match expected runtime test output. - Updated C++ test stg file for recent runtime changes. Regenerated tests (only one file changed actually). - Reworked C++ test preparation. The C++ runtime is now built on first invocation of a test. This works only on Linux + OSX/macOS. Windows needs extra handling.
This commit is contained in:
parent
5c63bc9a07
commit
de4df872fe
|
@ -303,9 +303,10 @@ public:
|
|||
|
||||
BasicListener(X) ::= <<
|
||||
@parser::definitions {
|
||||
#include "TBaseListener.h"
|
||||
class LeafListener : public TBaseListener {
|
||||
public:
|
||||
virtual void visitTerminal(Ref\<tree::TerminalNode> const& node) override {
|
||||
virtual void visitTerminal(tree::TerminalNode *node) override {
|
||||
std::cout \<\< node->getSymbol()->getText() \<\< std::endl;
|
||||
}
|
||||
};
|
||||
|
@ -314,7 +315,7 @@ public:
|
|||
|
||||
WalkListener(s) ::= <<
|
||||
LeafListener listener;
|
||||
tree::ParseTreeWalker::DEFAULT.walk(&listener, <s>);
|
||||
tree::ParseTreeWalker::DEFAULT.walk(&listener, <s>.get());
|
||||
>>
|
||||
|
||||
TreeNodeWithAltNumField(X) ::= <<
|
||||
|
@ -333,10 +334,11 @@ public:
|
|||
|
||||
TokenGetterListener(X) ::= <<
|
||||
@parser::definitions {
|
||||
#include "TBaseListener.h"
|
||||
class LeafListener : public TBaseListener {
|
||||
public:
|
||||
virtual void exitA(TParser::AContext *ctx) override {
|
||||
if (ctx->getChildCount() == 2)
|
||||
if (ctx->children.size() == 2)
|
||||
std::cout \<\< ctx->INT(0)->getSymbol()->getText() \<\< " " \<\< ctx->INT(1)->getSymbol()->getText()
|
||||
\<\< " " \<\< Arrays::toString(ctx->INT()) \<\< std::endl;
|
||||
else
|
||||
|
@ -348,10 +350,11 @@ public:
|
|||
|
||||
RuleGetterListener(X) ::= <<
|
||||
@parser::definitions {
|
||||
#include "TBaseListener.h"
|
||||
class LeafListener : public TBaseListener {
|
||||
public:
|
||||
virtual void exitA(TParser::AContext *ctx) override {
|
||||
if (ctx->getChildCount() == 2) {
|
||||
if (ctx->children.size() == 2) {
|
||||
std::cout \<\< ctx->b(0)->start->getText() \<\< " " \<\< ctx->b(1)->start->getText() \<\< " " \<\< ctx->b()[0]->start->getText() \<\< std::endl;
|
||||
} else {
|
||||
std::cout \<\< ctx->b(0)->start->getText() \<\< std::endl;
|
||||
|
@ -364,10 +367,11 @@ public:
|
|||
|
||||
LRListener(X) ::= <<
|
||||
@parser::definitions {
|
||||
#include "TBaseListener.h"
|
||||
class LeafListener : public TBaseListener {
|
||||
public:
|
||||
virtual void exitE(TParser::EContext *ctx) override {
|
||||
if (ctx->getChildCount() == 3) {
|
||||
if (ctx->children.size() == 3) {
|
||||
std::cout \<\< ctx->e(0)->start->getText() \<\< " " \<\< ctx->e(1)->start->getText() \<\< " " \<\< ctx->e()[0]->start->getText() \<\< std::endl;
|
||||
} else {
|
||||
std::cout \<\< ctx->INT()->getSymbol()->getText() \<\< std::endl;
|
||||
|
@ -379,6 +383,7 @@ public:
|
|||
|
||||
LRWithLabelsListener(X) ::= <<
|
||||
@parser::definitions {
|
||||
#include "TBaseListener.h"
|
||||
class LeafListener : public TBaseListener {
|
||||
public:
|
||||
virtual void exitCall(TParser::CallContext *ctx) override {
|
||||
|
|
|
@ -543,45 +543,122 @@ public abstract class BaseCppTest {
|
|||
return files;
|
||||
}
|
||||
|
||||
private String runProcess(ProcessBuilder builder, String description) throws Exception {
|
||||
Process process = builder.start();
|
||||
StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
|
||||
StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
|
||||
stdoutVacuum.start();
|
||||
stderrVacuum.start();
|
||||
int errcode = process.waitFor();
|
||||
stdoutVacuum.join();
|
||||
stderrVacuum.join();
|
||||
String output = stdoutVacuum.toString();
|
||||
if (stderrVacuum.toString().length() > 0) {
|
||||
this.stderrDuringParse = stderrVacuum.toString();
|
||||
System.err.println("exec stderrVacuum: "+ stderrVacuum);
|
||||
}
|
||||
if (errcode != 0) {
|
||||
this.stderrDuringParse = "execution failed with error code: " + errcode;
|
||||
System.err.println(description + " exited with error code: " + errcode);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
// TODO: add a buildRuntimeOnWindows variant.
|
||||
private boolean buildRuntime() {
|
||||
String runtimePath = locateRuntime();
|
||||
System.out.println("Building ANTLR4 C++ runtime (if necessary) at "+ runtimePath);
|
||||
|
||||
try {
|
||||
ArrayList<String> args = new ArrayList<String>();
|
||||
args.add("cmake");
|
||||
args.add(".");
|
||||
args.add("-DCMAKE_BUILD_TYPE=release");
|
||||
ProcessBuilder builder = new ProcessBuilder(args.toArray(new String[0]));
|
||||
builder.directory(new File(runtimePath));
|
||||
if (runProcess(builder, "antlr runtime cmake") == null)
|
||||
return false;
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("can't configure antlr cpp runtime cmake file");
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
|
||||
try {
|
||||
ArrayList<String> args = new ArrayList<String>();
|
||||
args.add("make");
|
||||
args.add("-j");
|
||||
args.add("8"); // Assuming a reasonable amount of available CPU cores.
|
||||
ProcessBuilder builder = new ProcessBuilder(args.toArray(new String[0]));
|
||||
builder.directory(new File(runtimePath));
|
||||
if (runProcess(builder, "building antlr runtime") == null)
|
||||
return false;
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("can't compile antlr cpp runtime");
|
||||
//e.printStackTrace(System.err);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static boolean runtimeBuiltOnce = false;
|
||||
|
||||
public String execModule(String fileName) {
|
||||
String compilerPath = locateCompiler();
|
||||
String runtimePath = locateRuntime();
|
||||
String includePath = runtimePath + "/runtime/src";
|
||||
String binPath = new File(new File(tmpdir), "a.out").getAbsolutePath();
|
||||
String inputPath = new File(new File(tmpdir), "input").getAbsolutePath();
|
||||
// First compile the test code.
|
||||
|
||||
// Build runtime using cmake once.
|
||||
if (!runtimeBuiltOnce) {
|
||||
runtimeBuiltOnce = true;
|
||||
if (!buildRuntime()) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Create symlink to the runtime.
|
||||
// TODO: make this platform neutral.
|
||||
try {
|
||||
ArrayList<String> args = new ArrayList<String>();
|
||||
args.add("ln");
|
||||
args.add("-s");
|
||||
args.add(runtimePath + "/dist/libantlr4-runtime.dylib"); // TODO: make this platform neutral
|
||||
ProcessBuilder builder = new ProcessBuilder(args.toArray(new String[0]));
|
||||
builder.directory(new File(tmpdir));
|
||||
String output = runProcess(builder, "sym linking C++ runtime");
|
||||
if (output == null)
|
||||
return null;
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("can't exec module: " + fileName);
|
||||
//e.printStackTrace(System.err);
|
||||
}
|
||||
|
||||
// Compile the test code.
|
||||
try {
|
||||
ArrayList<String> args = new ArrayList<String>();
|
||||
args.add(compilerPath);
|
||||
args.add("-std=c++11");
|
||||
args.add("-I");
|
||||
args.add(runtimePath);
|
||||
args.add(includePath);
|
||||
args.add("-L");
|
||||
args.add(runtimePath);
|
||||
args.add(".");
|
||||
args.add("-lantlr4-runtime");
|
||||
args.addAll(allCppFiles(tmpdir));
|
||||
ProcessBuilder builder = new ProcessBuilder(args.toArray(new String[0]));
|
||||
builder.directory(new File(tmpdir));
|
||||
Process process = builder.start();
|
||||
StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
|
||||
StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
|
||||
stdoutVacuum.start();
|
||||
stderrVacuum.start();
|
||||
int errcode = process.waitFor();
|
||||
stdoutVacuum.join();
|
||||
stderrVacuum.join();
|
||||
if ( stderrVacuum.toString().length()>0 ) {
|
||||
this.stderrDuringParse = stderrVacuum.toString();
|
||||
System.err.println("compile stderrVacuum: "+ stderrVacuum);
|
||||
}
|
||||
if (errcode != 0) {
|
||||
this.stderrDuringParse = "execution failed with error code: " + errcode;
|
||||
System.err.println("compile exited with error code: " + errcode);
|
||||
String output = runProcess(builder, "building test binary");
|
||||
if (output == null) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("can't compile module: " + fileName);
|
||||
e.printStackTrace(System.err);
|
||||
//e.printStackTrace(System.err);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -589,29 +666,15 @@ public abstract class BaseCppTest {
|
|||
try {
|
||||
ProcessBuilder builder = new ProcessBuilder(binPath, inputPath);
|
||||
builder.directory(new File(tmpdir));
|
||||
Process process = builder.start();
|
||||
StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
|
||||
StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
|
||||
stdoutVacuum.start();
|
||||
stderrVacuum.start();
|
||||
int errcode = process.waitFor();
|
||||
stdoutVacuum.join();
|
||||
stderrVacuum.join();
|
||||
String output = stdoutVacuum.toString();
|
||||
if ( stderrVacuum.toString().length()>0 ) {
|
||||
this.stderrDuringParse = stderrVacuum.toString();
|
||||
System.err.println("exec stderrVacuum: "+ stderrVacuum);
|
||||
}
|
||||
if (errcode != 0) {
|
||||
this.stderrDuringParse = "execution failed with error code: " + errcode;
|
||||
System.err.println("exec exited with error code: " + errcode);
|
||||
}
|
||||
String output = runProcess(builder, "running test binary");
|
||||
|
||||
return output;
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("can't exec module: " + fileName);
|
||||
e.printStackTrace(System.err);
|
||||
//e.printStackTrace(System.err);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -637,8 +700,8 @@ public abstract class BaseCppTest {
|
|||
|
||||
protected String locateRuntime() {
|
||||
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
final URL runtimeSrc = loader.getResource("Cpp/runtime/src");
|
||||
if ( runtimeSrc==null ) {
|
||||
final URL runtimeSrc = loader.getResource("Cpp");
|
||||
if (runtimeSrc == null) {
|
||||
throw new RuntimeException("Cannot find runtime");
|
||||
}
|
||||
return runtimeSrc.getPath();
|
||||
|
@ -892,12 +955,12 @@ public abstract class BaseCppTest {
|
|||
+ "\n"
|
||||
+ "class TreeShapeListener : public tree::ParseTreeListener {\n"
|
||||
+ "public:\n"
|
||||
+ " void visitTerminal(Ref\\<tree::TerminalNode> const& node) override {}\n"
|
||||
+ " void visitErrorNode(Ref\\<tree::ErrorNode> const& node) override {}\n"
|
||||
+ " void visitTerminal(tree::TerminalNode *node) override {}\n"
|
||||
+ " void visitErrorNode(tree::ErrorNode *node) override {}\n"
|
||||
+ " void exitEveryRule(ParserRuleContext *ctx) override {}\n"
|
||||
+ " void enterEveryRule(ParserRuleContext *ctx) override {\n"
|
||||
+ " for (auto child : ctx->children) {\n"
|
||||
+ " auto parent = child->getParent().lock();\n"
|
||||
+ " auto parent = child->parent.lock();\n"
|
||||
+ " if (!antlrcpp::is\\<tree::RuleNode>(parent)) {\n"
|
||||
+ " throw \"Invalid parse tree shape detected.\";\n"
|
||||
+ " }\n"
|
||||
|
@ -919,7 +982,7 @@ public abstract class BaseCppTest {
|
|||
+ "\n"
|
||||
+ " Ref\\<tree::ParseTree> tree = parser.<parserStartRuleName>;\n"
|
||||
+ " TreeShapeListener listener;\n"
|
||||
+ " tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);\n"
|
||||
+ " tree::ParseTreeWalker::DEFAULT.walk(&listener, tree.get());\n"
|
||||
+ "\n"
|
||||
+ " return 0;\n"
|
||||
+ "}\n"
|
||||
|
@ -931,7 +994,7 @@ public abstract class BaseCppTest {
|
|||
stSource += " parser.addErrorListener(&errorListener);\n";
|
||||
}
|
||||
if(trace)
|
||||
stSource += " parser.setTrace(True);\n";
|
||||
stSource += " parser.setTrace(true);\n";
|
||||
ST createParserST = new ST(stSource);
|
||||
outputFileST.add("createParser", createParserST);
|
||||
outputFileST.add("parserName", parserName);
|
||||
|
|
|
@ -13,13 +13,14 @@ public class TestListeners extends BaseCppTest {
|
|||
public void testBasic() throws Exception {
|
||||
mkdir(tmpdir);
|
||||
|
||||
StringBuilder grammarBuilder = new StringBuilder(490);
|
||||
StringBuilder grammarBuilder = new StringBuilder(512);
|
||||
grammarBuilder.append("grammar T;\n");
|
||||
grammarBuilder.append("\n");
|
||||
grammarBuilder.append("@parser::definitions {\n");
|
||||
grammarBuilder.append("#include \"TBaseListener.h\"\n");
|
||||
grammarBuilder.append("class LeafListener : public TBaseListener {\n");
|
||||
grammarBuilder.append("public:\n");
|
||||
grammarBuilder.append(" virtual void visitTerminal(Ref<tree::TerminalNode> const& node) override {\n");
|
||||
grammarBuilder.append(" virtual void visitTerminal(tree::TerminalNode *node) override {\n");
|
||||
grammarBuilder.append(" std::cout << node->getSymbol()->getText() << std::endl;\n");
|
||||
grammarBuilder.append(" }\n");
|
||||
grammarBuilder.append("};\n");
|
||||
|
@ -29,7 +30,7 @@ public class TestListeners extends BaseCppTest {
|
|||
grammarBuilder.append("@after {\n");
|
||||
grammarBuilder.append("std::cout << $ctx->r->toStringTree(this) << std::endl;\n");
|
||||
grammarBuilder.append("LeafListener listener;\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r);\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r.get());\n");
|
||||
grammarBuilder.append("}\n");
|
||||
grammarBuilder.append(" : r=a ;\n");
|
||||
grammarBuilder.append("a : INT INT\n");
|
||||
|
@ -59,14 +60,15 @@ public class TestListeners extends BaseCppTest {
|
|||
public void testLR() throws Exception {
|
||||
mkdir(tmpdir);
|
||||
|
||||
StringBuilder grammarBuilder = new StringBuilder(689);
|
||||
StringBuilder grammarBuilder = new StringBuilder(722);
|
||||
grammarBuilder.append("grammar T;\n");
|
||||
grammarBuilder.append("\n");
|
||||
grammarBuilder.append("@parser::definitions {\n");
|
||||
grammarBuilder.append("#include \"TBaseListener.h\"\n");
|
||||
grammarBuilder.append("class LeafListener : public TBaseListener {\n");
|
||||
grammarBuilder.append("public:\n");
|
||||
grammarBuilder.append(" virtual void exitE(TParser::EContext *ctx) override {\n");
|
||||
grammarBuilder.append(" if (ctx->getChildCount() == 3) {\n");
|
||||
grammarBuilder.append(" if (ctx->children.size() == 3) {\n");
|
||||
grammarBuilder.append(" std::cout << ctx->e(0)->start->getText() << \" \" << ctx->e(1)->start->getText() << \" \" << ctx->e()[0]->start->getText() << std::endl;\n");
|
||||
grammarBuilder.append(" } else {\n");
|
||||
grammarBuilder.append(" std::cout << ctx->INT()->getSymbol()->getText() << std::endl;\n");
|
||||
|
@ -79,7 +81,7 @@ public class TestListeners extends BaseCppTest {
|
|||
grammarBuilder.append("@after {\n");
|
||||
grammarBuilder.append("std::cout << $ctx->r->toStringTree(this) << std::endl;\n");
|
||||
grammarBuilder.append("LeafListener listener;\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r);\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r.get());\n");
|
||||
grammarBuilder.append("}\n");
|
||||
grammarBuilder.append(" : r=e ;\n");
|
||||
grammarBuilder.append("e : e op='*' e\n");
|
||||
|
@ -113,10 +115,11 @@ public class TestListeners extends BaseCppTest {
|
|||
public void testLRWithLabels() throws Exception {
|
||||
mkdir(tmpdir);
|
||||
|
||||
StringBuilder grammarBuilder = new StringBuilder(691);
|
||||
StringBuilder grammarBuilder = new StringBuilder(724);
|
||||
grammarBuilder.append("grammar T;\n");
|
||||
grammarBuilder.append("\n");
|
||||
grammarBuilder.append("@parser::definitions {\n");
|
||||
grammarBuilder.append("#include \"TBaseListener.h\"\n");
|
||||
grammarBuilder.append("class LeafListener : public TBaseListener {\n");
|
||||
grammarBuilder.append("public:\n");
|
||||
grammarBuilder.append(" virtual void exitCall(TParser::CallContext *ctx) override {\n");
|
||||
|
@ -132,7 +135,7 @@ public class TestListeners extends BaseCppTest {
|
|||
grammarBuilder.append("@after {\n");
|
||||
grammarBuilder.append("std::cout << $ctx->r->toStringTree(this) << std::endl;\n");
|
||||
grammarBuilder.append("LeafListener listener;\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r);\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r.get());\n");
|
||||
grammarBuilder.append("}\n");
|
||||
grammarBuilder.append(" : r=e ;\n");
|
||||
grammarBuilder.append("e : e '(' eList ')' # Call\n");
|
||||
|
@ -165,14 +168,15 @@ public class TestListeners extends BaseCppTest {
|
|||
public void testRuleGetters_1() throws Exception {
|
||||
mkdir(tmpdir);
|
||||
|
||||
StringBuilder grammarBuilder = new StringBuilder(709);
|
||||
StringBuilder grammarBuilder = new StringBuilder(742);
|
||||
grammarBuilder.append("grammar T;\n");
|
||||
grammarBuilder.append("\n");
|
||||
grammarBuilder.append("@parser::definitions {\n");
|
||||
grammarBuilder.append("#include \"TBaseListener.h\"\n");
|
||||
grammarBuilder.append("class LeafListener : public TBaseListener {\n");
|
||||
grammarBuilder.append("public:\n");
|
||||
grammarBuilder.append(" virtual void exitA(TParser::AContext *ctx) override {\n");
|
||||
grammarBuilder.append(" if (ctx->getChildCount() == 2) {\n");
|
||||
grammarBuilder.append(" if (ctx->children.size() == 2) {\n");
|
||||
grammarBuilder.append(" std::cout << ctx->b(0)->start->getText() << \" \" << ctx->b(1)->start->getText() << \" \" << ctx->b()[0]->start->getText() << std::endl;\n");
|
||||
grammarBuilder.append(" } else {\n");
|
||||
grammarBuilder.append(" std::cout << ctx->b(0)->start->getText() << std::endl;\n");
|
||||
|
@ -185,7 +189,7 @@ public class TestListeners extends BaseCppTest {
|
|||
grammarBuilder.append("@after {\n");
|
||||
grammarBuilder.append("std::cout << $ctx->r->toStringTree(this) << std::endl;\n");
|
||||
grammarBuilder.append("LeafListener listener;\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r);\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r.get());\n");
|
||||
grammarBuilder.append("}\n");
|
||||
grammarBuilder.append(" : r=a ;\n");
|
||||
grammarBuilder.append("a : b b // forces list\n");
|
||||
|
@ -215,14 +219,15 @@ public class TestListeners extends BaseCppTest {
|
|||
public void testRuleGetters_2() throws Exception {
|
||||
mkdir(tmpdir);
|
||||
|
||||
StringBuilder grammarBuilder = new StringBuilder(709);
|
||||
StringBuilder grammarBuilder = new StringBuilder(742);
|
||||
grammarBuilder.append("grammar T;\n");
|
||||
grammarBuilder.append("\n");
|
||||
grammarBuilder.append("@parser::definitions {\n");
|
||||
grammarBuilder.append("#include \"TBaseListener.h\"\n");
|
||||
grammarBuilder.append("class LeafListener : public TBaseListener {\n");
|
||||
grammarBuilder.append("public:\n");
|
||||
grammarBuilder.append(" virtual void exitA(TParser::AContext *ctx) override {\n");
|
||||
grammarBuilder.append(" if (ctx->getChildCount() == 2) {\n");
|
||||
grammarBuilder.append(" if (ctx->children.size() == 2) {\n");
|
||||
grammarBuilder.append(" std::cout << ctx->b(0)->start->getText() << \" \" << ctx->b(1)->start->getText() << \" \" << ctx->b()[0]->start->getText() << std::endl;\n");
|
||||
grammarBuilder.append(" } else {\n");
|
||||
grammarBuilder.append(" std::cout << ctx->b(0)->start->getText() << std::endl;\n");
|
||||
|
@ -235,7 +240,7 @@ public class TestListeners extends BaseCppTest {
|
|||
grammarBuilder.append("@after {\n");
|
||||
grammarBuilder.append("std::cout << $ctx->r->toStringTree(this) << std::endl;\n");
|
||||
grammarBuilder.append("LeafListener listener;\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r);\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r.get());\n");
|
||||
grammarBuilder.append("}\n");
|
||||
grammarBuilder.append(" : r=a ;\n");
|
||||
grammarBuilder.append("a : b b // forces list\n");
|
||||
|
@ -265,14 +270,15 @@ public class TestListeners extends BaseCppTest {
|
|||
public void testTokenGetters_1() throws Exception {
|
||||
mkdir(tmpdir);
|
||||
|
||||
StringBuilder grammarBuilder = new StringBuilder(674);
|
||||
StringBuilder grammarBuilder = new StringBuilder(707);
|
||||
grammarBuilder.append("grammar T;\n");
|
||||
grammarBuilder.append("\n");
|
||||
grammarBuilder.append("@parser::definitions {\n");
|
||||
grammarBuilder.append("#include \"TBaseListener.h\"\n");
|
||||
grammarBuilder.append("class LeafListener : public TBaseListener {\n");
|
||||
grammarBuilder.append("public:\n");
|
||||
grammarBuilder.append(" virtual void exitA(TParser::AContext *ctx) override {\n");
|
||||
grammarBuilder.append(" if (ctx->getChildCount() == 2)\n");
|
||||
grammarBuilder.append(" if (ctx->children.size() == 2)\n");
|
||||
grammarBuilder.append(" std::cout << ctx->INT(0)->getSymbol()->getText() << \" \" << ctx->INT(1)->getSymbol()->getText()\n");
|
||||
grammarBuilder.append(" << \" \" << Arrays::toString(ctx->INT()) << std::endl;\n");
|
||||
grammarBuilder.append(" else\n");
|
||||
|
@ -285,7 +291,7 @@ public class TestListeners extends BaseCppTest {
|
|||
grammarBuilder.append("@after {\n");
|
||||
grammarBuilder.append("std::cout << $ctx->r->toStringTree(this) << std::endl;\n");
|
||||
grammarBuilder.append("LeafListener listener;\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r);\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r.get());\n");
|
||||
grammarBuilder.append("}\n");
|
||||
grammarBuilder.append(" : r=a ;\n");
|
||||
grammarBuilder.append("a : INT INT\n");
|
||||
|
@ -314,14 +320,15 @@ public class TestListeners extends BaseCppTest {
|
|||
public void testTokenGetters_2() throws Exception {
|
||||
mkdir(tmpdir);
|
||||
|
||||
StringBuilder grammarBuilder = new StringBuilder(674);
|
||||
StringBuilder grammarBuilder = new StringBuilder(707);
|
||||
grammarBuilder.append("grammar T;\n");
|
||||
grammarBuilder.append("\n");
|
||||
grammarBuilder.append("@parser::definitions {\n");
|
||||
grammarBuilder.append("#include \"TBaseListener.h\"\n");
|
||||
grammarBuilder.append("class LeafListener : public TBaseListener {\n");
|
||||
grammarBuilder.append("public:\n");
|
||||
grammarBuilder.append(" virtual void exitA(TParser::AContext *ctx) override {\n");
|
||||
grammarBuilder.append(" if (ctx->getChildCount() == 2)\n");
|
||||
grammarBuilder.append(" if (ctx->children.size() == 2)\n");
|
||||
grammarBuilder.append(" std::cout << ctx->INT(0)->getSymbol()->getText() << \" \" << ctx->INT(1)->getSymbol()->getText()\n");
|
||||
grammarBuilder.append(" << \" \" << Arrays::toString(ctx->INT()) << std::endl;\n");
|
||||
grammarBuilder.append(" else\n");
|
||||
|
@ -334,7 +341,7 @@ public class TestListeners extends BaseCppTest {
|
|||
grammarBuilder.append("@after {\n");
|
||||
grammarBuilder.append("std::cout << $ctx->r->toStringTree(this) << std::endl;\n");
|
||||
grammarBuilder.append("LeafListener listener;\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r);\n");
|
||||
grammarBuilder.append("tree::ParseTreeWalker::DEFAULT.walk(&listener, $ctx->r.get());\n");
|
||||
grammarBuilder.append("}\n");
|
||||
grammarBuilder.append(" : r=a ;\n");
|
||||
grammarBuilder.append("a : INT INT\n");
|
||||
|
|
|
@ -178,14 +178,14 @@ void DefaultErrorStrategy::reportNoViableAlternative(Parser *recognizer, const N
|
|||
|
||||
void DefaultErrorStrategy::reportInputMismatch(Parser *recognizer, const InputMismatchException &e) {
|
||||
std::string msg = "mismatched input " + getTokenErrorDisplay(e.getOffendingToken()) +
|
||||
" expecting " + escapeWSAndQuote(e.getExpectedTokens().toString(recognizer->getVocabulary()));
|
||||
" expecting " + e.getExpectedTokens().toString(recognizer->getVocabulary());
|
||||
recognizer->notifyErrorListeners(e.getOffendingToken(), msg, std::make_exception_ptr(e));
|
||||
}
|
||||
|
||||
void DefaultErrorStrategy::reportFailedPredicate(Parser *recognizer, const FailedPredicateException &e) {
|
||||
const std::string& ruleName = recognizer->getRuleNames()[(size_t)recognizer->getContext()->getRuleIndex()];
|
||||
std::string msg = "rule " + ruleName + " " + e.what();
|
||||
recognizer->notifyErrorListeners(e.getOffendingToken(), escapeWSAndQuote(msg), std::make_exception_ptr(e));
|
||||
recognizer->notifyErrorListeners(e.getOffendingToken(), msg, std::make_exception_ptr(e));
|
||||
}
|
||||
|
||||
void DefaultErrorStrategy::reportUnwantedToken(Parser *recognizer) {
|
||||
|
@ -199,8 +199,7 @@ void DefaultErrorStrategy::reportUnwantedToken(Parser *recognizer) {
|
|||
std::string tokenName = getTokenErrorDisplay(t);
|
||||
misc::IntervalSet expecting = getExpectedTokens(recognizer);
|
||||
|
||||
std::string msg = "extraneous input " + tokenName + " expecting " +
|
||||
escapeWSAndQuote(expecting.toString(recognizer->getVocabulary()));
|
||||
std::string msg = "extraneous input " + tokenName + " expecting " + expecting.toString(recognizer->getVocabulary());
|
||||
recognizer->notifyErrorListeners(t, msg, nullptr);
|
||||
}
|
||||
|
||||
|
@ -214,7 +213,7 @@ void DefaultErrorStrategy::reportMissingToken(Parser *recognizer) {
|
|||
Token *t = recognizer->getCurrentToken();
|
||||
misc::IntervalSet expecting = getExpectedTokens(recognizer);
|
||||
std::string expectedText = expecting.toString(recognizer->getVocabulary());
|
||||
std::string msg = "missing " + escapeWSAndQuote(expectedText) + " at " + getTokenErrorDisplay(t);
|
||||
std::string msg = "missing " + expectedText + " at " + getTokenErrorDisplay(t);
|
||||
|
||||
recognizer->notifyErrorListeners(t, msg, nullptr);
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ std::string DFASerializer::toString() const {
|
|||
}
|
||||
|
||||
std::string DFASerializer::getEdgeLabel(size_t i) const {
|
||||
return _vocabulary.getDisplayName((int)i - 1);
|
||||
return _vocabulary.getDisplayName(i); // ml: no longer needed -1 as we use a map for edges, without offset.
|
||||
}
|
||||
|
||||
std::string DFASerializer::getStateString(DFAState *s) const {
|
||||
|
|
|
@ -84,6 +84,7 @@ namespace dfa {
|
|||
/// {@code edges[symbol]} points to target of symbol. Shift up by 1 so (-1)
|
||||
/// <seealso cref="Token#EOF"/> maps to {@code edges[0]}.
|
||||
// ml: this is a sparse list, so we use a map instead of a vector.
|
||||
// Watch out: we no longer have the -1 offset, as it isn't needed anymore.
|
||||
std::unordered_map<ssize_t, DFAState *> edges;
|
||||
|
||||
bool isAcceptState;
|
||||
|
|
Loading…
Reference in New Issue