use of org.antlr.v4.tool.ANTLRMessage in project antlr4 by tunnelvisionlabs.
the class BaseTest method checkError.
protected void checkError(ErrorQueue equeue, ANTLRMessage expectedMessage) throws Exception {
// System.out.println("errors="+equeue);
ANTLRMessage foundMsg = null;
for (int i = 0; i < equeue.errors.size(); i++) {
ANTLRMessage m = equeue.errors.get(i);
if (m.getErrorType() == expectedMessage.getErrorType()) {
foundMsg = m;
}
}
assertTrue("no error; " + expectedMessage.getErrorType() + " expected", !equeue.errors.isEmpty());
assertTrue("too many errors; " + equeue.errors, equeue.errors.size() <= 1);
assertNotNull("couldn't find expected error: " + expectedMessage.getErrorType(), foundMsg);
/*
assertTrue("error is not a GrammarSemanticsMessage",
foundMsg instanceof GrammarSemanticsMessage);
*/
assertArrayEquals(expectedMessage.getArgs(), foundMsg.getArgs());
}
use of org.antlr.v4.tool.ANTLRMessage in project antlr4 by tunnelvisionlabs.
the class ErrorQueue method toString.
public String toString(boolean rendered) {
if (!rendered) {
return Utils.join(all.iterator(), "\n");
}
if (tool == null) {
throw new IllegalStateException(String.format("No %s instance is available.", Tool.class.getName()));
}
StringBuilder buf = new StringBuilder();
for (ANTLRMessage m : all) {
ST st = tool.errMgr.getMessageTemplate(m);
buf.append(st.render());
buf.append("\n");
}
return buf.toString();
}
use of org.antlr.v4.tool.ANTLRMessage in project antlr4 by antlr.
the class TestCompositeGrammars method testCombinedGrammarImportsModalLexerGrammar.
@Test
public void testCombinedGrammarImportsModalLexerGrammar() throws Exception {
RuntimeTestUtils.mkdir(getTempDirPath());
String master = "grammar M;\n" + "import S;\n" + "A : 'a';\n" + "B : 'b';\n" + "r : A B;\n";
writeFile(getTempDirPath(), "M.g4", master);
String slave = "lexer grammar S;\n" + "D : 'd';\n" + "mode X;\n" + "C : 'c' -> popMode;\n";
writeFile(getTempDirPath(), "S.g4", slave);
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath());
assertEquals(1, equeue.errors.size());
ANTLRMessage msg = equeue.errors.get(0);
assertEquals(ErrorType.MODE_NOT_IN_LEXER, msg.getErrorType());
assertEquals("X", msg.getArgs()[0]);
assertEquals(3, msg.line);
assertEquals(5, msg.charPosition);
assertEquals("M.g4", new File(msg.fileName).getName());
}
use of org.antlr.v4.tool.ANTLRMessage in project antlr4 by antlr.
the class ErrorQueue method toString.
public String toString(boolean rendered) {
if (!rendered) {
return Utils.join(all.iterator(), "\n");
}
if (tool == null) {
throw new IllegalStateException(String.format("No %s instance is available.", Tool.class.getName()));
}
StringBuilder buf = new StringBuilder();
for (ANTLRMessage m : all) {
ST st = tool.errMgr.getMessageTemplate(m);
buf.append(st.render());
buf.append("\n");
}
return buf.toString();
}
use of org.antlr.v4.tool.ANTLRMessage in project antlr4 by antlr.
the class BaseRuntimeTest method antlrOnString.
/**
* Run ANTLR on stuff in workdir and error queue back
*/
public static ErrorQueue antlrOnString(String workdir, String targetName, String grammarFileName, boolean defaultListener, String... extraOptions) {
final List<String> options = new ArrayList<>();
Collections.addAll(options, extraOptions);
if (targetName != null) {
options.add("-Dlanguage=" + targetName);
}
if (!options.contains("-o")) {
options.add("-o");
options.add(workdir);
}
if (!options.contains("-lib")) {
options.add("-lib");
options.add(workdir);
}
if (!options.contains("-encoding")) {
options.add("-encoding");
options.add("UTF-8");
}
options.add(new File(workdir, grammarFileName).toString());
final String[] optionsA = new String[options.size()];
options.toArray(optionsA);
Tool antlr = new Tool(optionsA);
ErrorQueue equeue = new ErrorQueue(antlr);
antlr.addListener(equeue);
if (defaultListener) {
antlr.addListener(new DefaultToolListener(antlr));
}
synchronized (antlrLock) {
antlr.processGrammarsOnCommandLine();
}
List<String> errors = new ArrayList<>();
if (!defaultListener && !equeue.errors.isEmpty()) {
for (int i = 0; i < equeue.errors.size(); i++) {
ANTLRMessage msg = equeue.errors.get(i);
ST msgST = antlr.errMgr.getMessageTemplate(msg);
errors.add(msgST.render());
}
}
if (!defaultListener && !equeue.warnings.isEmpty()) {
for (int i = 0; i < equeue.warnings.size(); i++) {
ANTLRMessage msg = equeue.warnings.get(i);
// antlrToolErrors.append(msg); warnings are hushed
}
}
return equeue;
}
Aggregations