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)");
}
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)");
}
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;
}
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());
}
}
}
}
}
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);
}
Aggregations