Search in sources :

Example 26 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestCompositeGrammars method testBigTreeOfImports.

@Test
public void testBigTreeOfImports() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = "parser grammar T;\n" + "tokens{T}\n" + "x : T ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "T.g4", slave);
    slave = "parser grammar S;\n" + "import T;\n" + "tokens{S}\n" + "y : S ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave);
    slave = "parser grammar C;\n" + "tokens{C}\n" + "i : C ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "C.g4", slave);
    slave = "parser grammar B;\n" + "tokens{B}\n" + "j : B ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "B.g4", slave);
    slave = "parser grammar A;\n" + "import B,C;\n" + "tokens{A}\n" + "k : A ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "A.g4", slave);
    String master = "grammar M;\n" + "import S,A;\n" + "tokens{M}\n" + "a : M ;\n";
    writeFile(tmpdir, "M.g4", master);
    Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
    assertEquals("[]", equeue.errors.toString());
    assertEquals("[]", equeue.warnings.toString());
    String expectedTokenIDToTypeMap = "{EOF=-1, M=1, S=2, T=3, A=4, B=5, C=6}";
    String expectedStringLiteralToTypeMap = "{}";
    String expectedTypeToTokenList = "[M, S, T, A, B, C]";
    assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
    assertEquals(expectedStringLiteralToTypeMap, g.stringLiteralToTypeMap.toString());
    assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
    boolean ok = rawGenerateAndBuildRecognizer("M.g4", master, "MParser", null);
    // should be ok
    boolean expecting = true;
    assertEquals(expecting, ok);
}
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 27 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestCompositeGrammars method testHeadersPropogatedCorrectlyToImportedGrammars.

@Test
public void testHeadersPropogatedCorrectlyToImportedGrammars() throws Exception {
    String slave = "parser grammar S;\n" + "a : B {System.out.print(\"S.a\");} ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n" + "@header{package mypackage;}\n" + "s : a ;\n" + // defines B from inherited token space
    "B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", master, false);
    // should be ok
    int expecting = 0;
    assertEquals(expecting, equeue.errors.size());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 28 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestCompositeGrammars method testImportedTokenVocabIgnoredWithWarning.

@Test
public void testImportedTokenVocabIgnoredWithWarning() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = "parser grammar S;\n" + "options {tokenVocab=whatever;}\n" + "tokens { A }\n" + "x : A {System.out.println(\"S.x\");} ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(tmpdir, "M.g4", master);
    Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
    Object expectedArg = "S";
    ErrorType expectedMsgID = ErrorType.OPTIONS_IN_DELEGATE;
    GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g.fileName, null, expectedArg);
    checkGrammarSemanticsWarning(equeue, expectedMessage);
    assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
    assertEquals("unexpected warnings: " + equeue, 1, equeue.warnings.size());
}
Also used : ErrorType(org.antlr.v4.tool.ErrorType) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) GrammarSemanticsMessage(org.antlr.v4.tool.GrammarSemanticsMessage) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 29 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestCompositeGrammars method testRulesVisibleThroughMultilevelImport.

@Test
public void testRulesVisibleThroughMultilevelImport() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = "parser grammar T;\n" + "x : T ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "T.g4", slave);
    String slave2 = // A, B, C token type order
    "parser grammar S;\n" + "import T;\n" + "a : S ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave2);
    String master = "grammar M;\n" + "import S;\n" + // x MUST BE VISIBLE TO M
    "a : M x ;\n";
    writeFile(tmpdir, "M.g4", master);
    Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
    String expectedTokenIDToTypeMap = "{EOF=-1, M=1, T=2}";
    String expectedStringLiteralToTypeMap = "{}";
    String expectedTypeToTokenList = "[M, T]";
    assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
    assertEquals(expectedStringLiteralToTypeMap, g.stringLiteralToTypeMap.toString());
    assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
    assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
}
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 30 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestCompositeGrammars method testTokensFileInOutputDirAndImportFileInSubdir.

@Test
public void testTokensFileInOutputDirAndImportFileInSubdir() throws Exception {
    String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    String subdir = tmpdir + "/sub";
    BaseRuntimeTest.mkdir(subdir);
    writeFile(subdir, "S.g4", slave);
    String parser = "parser grammar MParser;\n" + "import S;\n" + "options {tokenVocab=MLexer;}\n" + "s : a ;\n";
    writeFile(tmpdir, "MParser.g4", parser);
    String lexer = "lexer grammar MLexer;\n" + // defines B from inherited token space
    "B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(tmpdir, "MLexer.g4", lexer);
    String outdir = tmpdir + "/out";
    BaseRuntimeTest.mkdir(outdir);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "MLexer.g4", false, "-o", outdir);
    assertEquals(0, equeue.size());
    equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "MParser.g4", false, "-o", outdir, "-lib", subdir);
    assertEquals(0, equeue.size());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)305 Test (org.junit.Test)304 Grammar (org.antlr.v4.tool.Grammar)183 ATN (org.antlr.v4.runtime.atn.ATN)65 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)62 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)59 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)53 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)47 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)31 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)29 Rule (org.antlr.v4.tool.Rule)25 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)21 STGroupString (org.stringtemplate.v4.STGroupString)21 ArrayList (java.util.ArrayList)18 ATNState (org.antlr.v4.runtime.atn.ATNState)18 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)17 ST (org.stringtemplate.v4.ST)17 ParseTree (org.antlr.v4.runtime.tree.ParseTree)15 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)15