Search in sources :

Example 56 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.

the class BaseNodeTest method checkGrammarSemanticsError.

protected void checkGrammarSemanticsError(ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) throws Exception {
    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;
        }
    }
    assertNotNull("no error; " + expectedMessage.getErrorType() + " expected", foundMsg);
    assertTrue("error is not a GrammarSemanticsMessage", foundMsg instanceof GrammarSemanticsMessage);
    assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs()));
    if (equeue.size() != 1) {
        System.err.println(equeue);
    }
}
Also used : GrammarSemanticsMessage(org.antlr.v4.tool.GrammarSemanticsMessage) ANTLRMessage(org.antlr.v4.tool.ANTLRMessage)

Example 57 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.

the class BaseNodeTest method rawGenerateAndBuildRecognizer.

/** Return true if all is well */
protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) {
    ErrorQueue equeue = antlrOnString(getTmpDir(), "JavaScript", grammarFileName, grammarStr, defaultListener, extraOptions);
    if (!equeue.errors.isEmpty()) {
        return false;
    }
    List<String> files = new ArrayList<String>();
    if (lexerName != null) {
        files.add(lexerName + ".js");
    }
    if (parserName != null) {
        files.add(parserName + ".js");
        Set<String> optionsSet = new HashSet<String>(Arrays.asList(extraOptions));
        if (!optionsSet.contains("-no-listener")) {
            files.add(grammarFileName.substring(0, grammarFileName.lastIndexOf('.')) + "Listener.js");
        }
        if (optionsSet.contains("-visitor")) {
            files.add(grammarFileName.substring(0, grammarFileName.lastIndexOf('.')) + "Visitor.js");
        }
    }
    // allIsWell: no compile
    return true;
}
Also used : ArrayList(java.util.ArrayList) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) HashSet(java.util.HashSet)

Example 58 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.

the class BaseSwiftTest method rawGenerateRecognizer.

/**
	 * Return true if all is well
	 */
private boolean rawGenerateRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) {
    ErrorQueue equeue = antlrOnString(getTmpDir(), "Swift", grammarFileName, grammarStr, defaultListener, extraOptions);
    if (!equeue.errors.isEmpty()) {
        return false;
    }
    List<String> files = new ArrayList<String>();
    if (lexerName != null) {
        files.add(lexerName + ".swift");
        files.add(lexerName + "ATN.swift");
    }
    if (parserName != null) {
        files.add(parserName + ".swift");
        files.add(parserName + "ATN.swift");
        Set<String> optionsSet = new HashSet<String>(Arrays.asList(extraOptions));
        String grammarName = grammarFileName.substring(0, grammarFileName.lastIndexOf('.'));
        if (!optionsSet.contains("-no-listener")) {
            files.add(grammarName + "Listener.swift");
            files.add(grammarName + "BaseListener.swift");
        }
        if (optionsSet.contains("-visitor")) {
            files.add(grammarName + "Visitor.swift");
            files.add(grammarName + "BaseVisitor.swift");
        }
    }
    addSourceFiles(files.toArray(new String[files.size()]));
    return true;
}
Also used : ArrayList(java.util.ArrayList) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) HashSet(java.util.HashSet)

Example 59 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.

the class BaseJavaToolTest method testErrors.

public void testErrors(String[] pairs, boolean printTree) {
    for (int i = 0; i < pairs.length; i += 2) {
        String grammarStr = pairs[i];
        String expect = pairs[i + 1];
        String[] lines = grammarStr.split("\n");
        String fileName = getFilenameFromFirstLineOfGrammar(lines[0]);
        // use default language target in case test overrides
        ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, null, fileName, grammarStr, false);
        String actual = equeue.toString(true);
        actual = actual.replace(tmpdir + File.separator, "");
        //			System.err.println(actual);
        String msg = grammarStr;
        msg = msg.replace("\n", "\\n");
        msg = msg.replace("\r", "\\r");
        msg = msg.replace("\t", "\\t");
        assertEquals("error in: " + msg, expect, actual);
    }
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue)

Example 60 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.

the class TestPerformance method testExponentialInclude.

@Test(timeout = 20000)
public void testExponentialInclude() {
    String grammarFormat = "parser grammar Level_%d_%d;\n" + "\n" + "%s import Level_%d_1, Level_%d_2;\n" + "\n" + "rule_%d_%d : EOF;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    long startTime = System.nanoTime();
    int levels = 20;
    for (int level = 0; level < levels; level++) {
        String leafPrefix = level == levels - 1 ? "//" : "";
        String grammar1 = String.format(grammarFormat, level, 1, leafPrefix, level + 1, level + 1, level, 1);
        writeFile(tmpdir, "Level_" + level + "_1.g4", grammar1);
        if (level > 0) {
            String grammar2 = String.format(grammarFormat, level, 2, leafPrefix, level + 1, level + 1, level, 1);
            writeFile(tmpdir, "Level_" + level + "_2.g4", grammar2);
        }
    }
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "Level_0_1.g4", false);
    Assert.assertTrue(equeue.errors.isEmpty());
    long endTime = System.nanoTime();
    System.out.format("%s milliseconds.%n", (endTime - startTime) / 1000000.0);
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Aggregations

ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)40 ANTLRMessage (org.antlr.v4.tool.ANTLRMessage)21 Grammar (org.antlr.v4.tool.Grammar)21 LexerGrammar (org.antlr.v4.tool.LexerGrammar)17 Test (org.junit.Test)16 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)15 GrammarSemanticsMessage (org.antlr.v4.tool.GrammarSemanticsMessage)14 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)11 STGroupString (org.stringtemplate.v4.STGroupString)11 ArrayList (java.util.ArrayList)9 ST (org.stringtemplate.v4.ST)8 HashSet (java.util.HashSet)7 ATNFactory (org.antlr.v4.automata.ATNFactory)7 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)7 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)7 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)7 ATN (org.antlr.v4.runtime.atn.ATN)6 DecisionState (org.antlr.v4.runtime.atn.DecisionState)6 STGroup (org.stringtemplate.v4.STGroup)6