Search in sources :

Example 56 with Grammar

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

the class TestParserInterpreter method testAorB.

@Test
public void testAorB() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : A{;} | B ;", lg);
    testInterp(lg, g, "s", "a", "(s a)");
    testInterp(lg, g, "s", "b", "(s b)");
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 57 with Grammar

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

the class TestParserInterpreter method testOptionalA.

@Test
public void testOptionalA() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : A? B ;\n", lg);
    testInterp(lg, g, "s", "b", "(s b)");
    testInterp(lg, g, "s", "ab", "(s a b)");
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 58 with Grammar

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

the class Antlr4Mojo method processGrammarFiles.

/**
     *
     * @param sourceDirectory
     * @exception InclusionScanException
     */
private List<List<String>> processGrammarFiles(List<String> args, Set<File> grammarFiles, GrammarDependencies dependencies, File sourceDirectory) throws InclusionScanException, IOException {
    // We don't want the plugin to run for every grammar, regardless of whether
    // it's changed since the last compilation. Check the mtime of the tokens vs
    // the grammar file mtime to determine whether we even need to execute.
    Set<File> grammarFilesToProcess = new HashSet<File>();
    for (File grammarFile : grammarFiles) {
        String tokensFileName = grammarFile.getName().split("\\.")[0] + ".tokens";
        File outputFile = new File(outputDirectory, tokensFileName);
        if ((!outputFile.exists()) || outputFile.lastModified() < grammarFile.lastModified() || dependencies.isDependencyChanged(grammarFile)) {
            grammarFilesToProcess.add(grammarFile);
        }
    }
    grammarFiles = grammarFilesToProcess;
    if (grammarFiles.isEmpty()) {
        getLog().info("No grammars to process");
        return Collections.emptyList();
    }
    MultiMap<String, File> grammarFileByFolder = new MultiMap<String, File>();
    // grammars to process.
    for (File grammarFile : grammarFiles) {
        if (!buildContext.hasDelta(grammarFile)) {
            continue;
        }
        buildContext.removeMessages(grammarFile);
        getLog().debug("Grammar file '" + grammarFile.getPath() + "' detected.");
        String relPathBase = MojoUtils.findSourceSubdir(sourceDirectory, grammarFile);
        String relPath = relPathBase + grammarFile.getName();
        getLog().debug("  ... relative path is: " + relPath);
        grammarFileByFolder.map(relPathBase, grammarFile);
    }
    List<List<String>> result = new ArrayList<List<String>>();
    for (Map.Entry<String, List<File>> entry : grammarFileByFolder.entrySet()) {
        List<String> folderArgs = new ArrayList<String>(args);
        if (!folderArgs.contains("-package") && !entry.getKey().isEmpty()) {
            folderArgs.add("-package");
            folderArgs.add(getPackageName(entry.getKey()));
        }
        for (File file : entry.getValue()) {
            folderArgs.add(entry.getKey() + file.getName());
        }
        result.add(folderArgs);
    }
    return result;
}
Also used : MultiMap(org.antlr.v4.runtime.misc.MultiMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Map(java.util.Map) MultiMap(org.antlr.v4.runtime.misc.MultiMap) HashSet(java.util.HashSet)

Example 59 with Grammar

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

the class GrammarDependencies method analyse.

private void analyse(File grammarFile, Collection<File> grammarFiles, Tool tool) {
    GrammarRootAST grammar = tool.parseGrammar(grammarFile.getAbsolutePath());
    if (grammar == null)
        return;
    for (GrammarAST importDecl : grammar.getAllChildrenWithType(ANTLRParser.IMPORT)) {
        Tree id = importDecl.getFirstChildWithType(ANTLRParser.ID);
        // being reported by the ANTLR tool
        if (id != null) {
            String grammarPath = getRelativePath(grammarFile);
            graph.addEdge(id.getText() + ".g4", grammarPath);
        }
    }
    for (GrammarAST options : grammar.getAllChildrenWithType(ANTLRParser.OPTIONS)) {
        for (int i = 0, count = options.getChildCount(); i < count; i++) {
            Tree option = options.getChild(i);
            if (option.getType() == ANTLRParser.ASSIGN) {
                String key = option.getChild(0).getText();
                String value = option.getChild(1).getText();
                if ("tokenVocab".equals(key)) {
                    String name = stripQuotes(value);
                    // the grammar name may be qualified, but we resolve the path anyway
                    String grammarName = stripPath(name);
                    String grammarPath = MojoUtils.findSourceSubdir(sourceDirectory, grammarFile);
                    File depGrammarFile = resolve(grammarName, grammarPath);
                    // (files probably reside in the root directory anyway with such a configuration )
                    if (packageName != null)
                        grammarPath = packageName;
                    graph.addEdge(getRelativePath(depGrammarFile), grammarPath + grammarFile.getName());
                }
            }
        }
    }
}
Also used : GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Tree(org.antlr.runtime.tree.Tree) File(java.io.File)

Example 60 with Grammar

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

the class TestATNLexerInterpreter method testEOFSuffixInFirstRule.

@Test
public void testEOFSuffixInFirstRule() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' EOF ;\n" + "B : 'a';\n");
    String expecting = "A, EOF";
    checkLexerMatches(lg, "a", expecting);
}
Also used : LexerGrammar(org.antlr.v4.tool.LexerGrammar) 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