use of edu.princeton.cs.algs4.ST in project antlr4 by antlr.
the class TestCodeGeneration method getEvalInfoForString.
public List<String> getEvalInfoForString(String grammarString, String pattern) throws RecognitionException {
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(grammarString);
List<String> evals = new ArrayList<String>();
if (g.ast != null && !g.ast.hasErrors) {
SemanticPipeline sem = new SemanticPipeline(g);
sem.process();
ATNFactory factory = new ParserATNFactory(g);
if (g.isLexer())
factory = new LexerATNFactory((LexerGrammar) g);
g.atn = factory.createATN();
CodeGenerator gen = new CodeGenerator(g);
ST outputFileST = gen.generateParser();
// STViz viz = outputFileST.inspect();
// try {
// viz.waitForClose();
// }
// catch (Exception e) {
// e.printStackTrace();
// }
boolean debug = false;
DebugInterpreter interp = new DebugInterpreter(outputFileST.groupThatCreatedThisInstance, outputFileST.impl.nativeGroup.errMgr, debug);
InstanceScope scope = new InstanceScope(null, outputFileST);
StringWriter sw = new StringWriter();
AutoIndentWriter out = new AutoIndentWriter(sw);
interp.exec(out, scope);
for (String e : interp.evals) {
if (e.contains(pattern)) {
evals.add(e);
}
}
}
if (equeue.size() > 0) {
System.err.println(equeue.toString());
}
return evals;
}
use of edu.princeton.cs.algs4.ST in project antlr4 by antlr.
the class TestBasicSemanticErrors method testArgumentRetvalLocalConflicts.
@Test
public void testArgumentRetvalLocalConflicts() throws Exception {
String grammarTemplate = "grammar T;\n" + "ss<if(args)>[<args>]<endif> <if(retvals)>returns [<retvals>]<endif>\n" + "<if(locals)>locals [<locals>]<endif>\n" + " : <body> EOF;\n" + "expr : '=';\n";
String expected = "error(" + ErrorType.ARG_CONFLICTS_WITH_RULE.code + "): T.g4:2:7: parameter expr conflicts with rule with same name\n" + "error(" + ErrorType.RETVAL_CONFLICTS_WITH_RULE.code + "): T.g4:2:26: return value expr conflicts with rule with same name\n" + "error(" + ErrorType.LOCAL_CONFLICTS_WITH_RULE.code + "): T.g4:3:12: local expr conflicts with rule with same name\n" + "error(" + ErrorType.RETVAL_CONFLICTS_WITH_ARG.code + "): T.g4:2:26: return value expr conflicts with parameter with same name\n" + "error(" + ErrorType.LOCAL_CONFLICTS_WITH_ARG.code + "): T.g4:3:12: local expr conflicts with parameter with same name\n" + "error(" + ErrorType.LOCAL_CONFLICTS_WITH_RETVAL.code + "): T.g4:3:12: local expr conflicts with return value with same name\n" + "error(" + ErrorType.LABEL_CONFLICTS_WITH_RULE.code + "): T.g4:4:4: label expr conflicts with rule with same name\n" + "error(" + ErrorType.LABEL_CONFLICTS_WITH_ARG.code + "): T.g4:4:4: label expr conflicts with parameter with same name\n" + "error(" + ErrorType.LABEL_CONFLICTS_WITH_RETVAL.code + "): T.g4:4:4: label expr conflicts with return value with same name\n" + "error(" + ErrorType.LABEL_CONFLICTS_WITH_LOCAL.code + "): T.g4:4:4: label expr conflicts with local with same name\n";
ST grammarST = new ST(grammarTemplate);
grammarST.add("args", "int expr");
grammarST.add("retvals", "int expr");
grammarST.add("locals", "int expr");
grammarST.add("body", "expr=expr");
testErrors(new String[] { grammarST.render(), expected }, false);
}
use of edu.princeton.cs.algs4.ST in project antlr4 by antlr.
the class Antlr4ErrorLog method error.
/**
* {@inheritDoc}
* <p>
* This implementation passes the message to the Maven log.
* </p>
* @param message The message to send to Maven.
*/
@Override
public void error(ANTLRMessage message) {
ST msgST = tool.errMgr.getMessageTemplate(message);
String outputMsg = msgST.render();
if (tool.errMgr.formatWantsSingleLineMessage()) {
outputMsg = outputMsg.replace('\n', ' ');
}
log.error(outputMsg);
if (message.fileName != null) {
String text = message.getMessageTemplate(false).render();
buildContext.addMessage(new File(message.fileName), message.line, message.charPosition, text, BuildContext.SEVERITY_ERROR, message.getCause());
}
}
use of edu.princeton.cs.algs4.ST in project antlr4 by antlr.
the class Antlr4ErrorLog method warning.
/**
* {@inheritDoc}
* <p>
* This implementation passes the message to the Maven log.
* </p>
* @param message
*/
@Override
public void warning(ANTLRMessage message) {
ST msgST = tool.errMgr.getMessageTemplate(message);
String outputMsg = msgST.render();
if (tool.errMgr.formatWantsSingleLineMessage()) {
outputMsg = outputMsg.replace('\n', ' ');
}
log.warn(outputMsg);
if (message.fileName != null) {
String text = message.getMessageTemplate(false).render();
buildContext.addMessage(new File(message.fileName), message.line, message.charPosition, text, BuildContext.SEVERITY_WARNING, message.getCause());
}
}
use of edu.princeton.cs.algs4.ST in project antlr4 by antlr.
the class SwiftTarget method getLexerOrParserATNJson.
private String getLexerOrParserATNJson(Grammar g, String fileName) {
ST extST = getTemplates().getInstanceOf("codeFileExtension");
String className = fileName.substring(0, fileName.lastIndexOf(extST.render()));
String JSON = "class " + className + "ATN {\n" + " let jsonString: String = \"" + serializeTojson(g.atn).replaceAll("\"", "\\\\\"") + //.replaceAll("\"", "\\\\\"");
"\"\n}";
return JSON;
}
Aggregations