Search in sources :

Example 36 with ANTLRMessage

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());
}
Also used : ANTLRMessage(org.antlr.v4.tool.ANTLRMessage)

Example 37 with ANTLRMessage

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();
}
Also used : ST(org.stringtemplate.v4.ST) ANTLRMessage(org.antlr.v4.tool.ANTLRMessage)

Example 38 with ANTLRMessage

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());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) ANTLRMessage(org.antlr.v4.tool.ANTLRMessage) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 39 with ANTLRMessage

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();
}
Also used : ST(org.stringtemplate.v4.ST) ANTLRMessage(org.antlr.v4.tool.ANTLRMessage)

Example 40 with ANTLRMessage

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;
}
Also used : DefaultToolListener(org.antlr.v4.tool.DefaultToolListener) ST(org.stringtemplate.v4.ST) ArrayList(java.util.ArrayList) File(java.io.File) STGroupFile(org.stringtemplate.v4.STGroupFile) ANTLRMessage(org.antlr.v4.tool.ANTLRMessage) Tool(org.antlr.v4.Tool)

Aggregations

ANTLRMessage (org.antlr.v4.tool.ANTLRMessage)29 GrammarSemanticsMessage (org.antlr.v4.tool.GrammarSemanticsMessage)15 LexerGrammar (org.antlr.v4.tool.LexerGrammar)12 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)11 ATN (org.antlr.v4.runtime.atn.ATN)8 DecisionState (org.antlr.v4.runtime.atn.DecisionState)8 Grammar (org.antlr.v4.tool.Grammar)8 File (java.io.File)5 ATNState (org.antlr.v4.runtime.atn.ATNState)4 Test (org.junit.Test)3 ST (org.stringtemplate.v4.ST)3 ArrayList (java.util.ArrayList)2 Tool (org.antlr.v4.Tool)2 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)2 BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)2 DefaultToolListener (org.antlr.v4.tool.DefaultToolListener)2 IOException (java.io.IOException)1 STGroupFile (org.stringtemplate.v4.STGroupFile)1 STGroupString (org.stringtemplate.v4.STGroupString)1