Search in sources :

Example 81 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue 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 82 with ErrorQueue

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

the class TestCompositeGrammars method testSyntaxErrorsInImportsNotThrownOut.

@Test
public void testSyntaxErrorsInImportsNotThrownOut() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = "parser grammar S;\n" + "options {toke\n";
    RuntimeTestUtils.mkdir(getTempDirPath());
    writeFile(getTempDirPath(), "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(getTempDirPath(), "M.g4", master);
    /*Grammar g =*/
    new Grammar(getTempDirPath() + "/M.g4", master, equeue);
    assertEquals(ErrorType.SYNTAX_ERROR, equeue.errors.get(0).getErrorType());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 83 with ErrorQueue

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

the class TestCompositeGrammars method testImportClashingChannelsIntoLexerGrammar.

@Test
public void testImportClashingChannelsIntoLexerGrammar() throws Exception {
    RuntimeTestUtils.mkdir(getTempDirPath());
    String master = "lexer grammar M;\n" + "import S;\n" + "channels {CH_A, CH_B, CH_C}\n" + "A : 'a' -> channel(CH_A);\n" + "B : 'b' -> channel(CH_B);\n" + "C : 'C' -> channel(CH_C);\n";
    writeFile(getTempDirPath(), "M.g4", master);
    String slave = "lexer grammar S;\n" + "channels {CH_C}\n" + "C : 'c' -> channel(CH_C);\n";
    writeFile(getTempDirPath(), "S.g4", slave);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath());
    assertEquals(0, equeue.errors.size());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 84 with ErrorQueue

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

the class BaseCSharpTest method rawGenerateRecognizer.

/**
 * Return true if all is well
 */
protected boolean rawGenerateRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) {
    ErrorQueue equeue = antlrOnString(getTempDirPath(), "CSharp", grammarFileName, grammarStr, defaultListener, extraOptions);
    if (!equeue.errors.isEmpty()) {
        return false;
    }
    List<String> files = new ArrayList<String>();
    if (lexerName != null) {
        files.add(lexerName + ".cs");
    }
    if (parserName != null) {
        files.add(parserName + ".cs");
        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.cs");
            files.add(grammarName + "BaseListener.cs");
        }
        if (optionsSet.contains("-visitor")) {
            files.add(grammarName + "Visitor.cs");
            files.add(grammarName + "BaseVisitor.cs");
        }
    }
    addSourceFiles(files.toArray(new String[files.size()]));
    return true;
}
Also used : ArrayList(java.util.ArrayList) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) HashSet(java.util.HashSet)

Example 85 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue 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

ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)45 Grammar (org.antlr.v4.tool.Grammar)34 Test (org.junit.Test)34 ANTLRMessage (org.antlr.v4.tool.ANTLRMessage)27 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)23 LexerGrammar (org.antlr.v4.tool.LexerGrammar)23 GrammarSemanticsMessage (org.antlr.v4.tool.GrammarSemanticsMessage)17 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)12 STGroupString (org.stringtemplate.v4.STGroupString)12 ArrayList (java.util.ArrayList)10 ST (org.stringtemplate.v4.ST)10 ATNFactory (org.antlr.v4.automata.ATNFactory)9 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)9 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)9 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)9 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)9 ATN (org.antlr.v4.runtime.atn.ATN)8 DecisionState (org.antlr.v4.runtime.atn.DecisionState)8 STGroup (org.stringtemplate.v4.STGroup)7 HashSet (java.util.HashSet)6