use of org.antlr.v4.Tool in project antlr4 by antlr.
the class BaseGoTest method locateTool.
private static String locateTool(String tool) {
// default cap is about right
ArrayList<String> paths = new ArrayList<String>();
// GOROOT should have priority if set
String goroot = System.getenv("GOROOT");
if (goroot != null) {
paths.add(goroot + File.separatorChar + "bin");
}
String pathEnv = System.getenv("PATH");
if (pathEnv != null) {
paths.addAll(Arrays.asList(pathEnv.split(File.pathSeparator)));
}
// OS specific default locations of binary dist as last resort
paths.add("/usr/local/go/bin");
paths.add("c:\\Go\\bin");
for (String path : paths) {
File candidate = new File(new File(path), tool);
if (candidate.exists()) {
return candidate.getPath();
}
candidate = new File(new File(path), tool + ".exe");
if (candidate.exists()) {
return candidate.getPath();
}
}
return null;
}
use of org.antlr.v4.Tool in project antlr4 by antlr.
the class BaseJavaTest method semanticProcess.
protected void semanticProcess(Grammar g) {
if (g.ast != null && !g.ast.hasErrors) {
// System.out.println(g.ast.toStringTree());
Tool antlr = new Tool();
SemanticPipeline sem = new SemanticPipeline(g);
sem.process();
if (g.getImportedGrammars() != null) {
// process imported grammars (if any)
for (Grammar imp : g.getImportedGrammars()) {
antlr.processNonCombinedGrammar(imp, false);
}
}
}
}
use of org.antlr.v4.Tool 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 in project antlr4 by antlr.
the class TestATNConstruction method testParserRuleRefInLexerRule.
@Test
public void testParserRuleRefInLexerRule() throws Exception {
boolean threwException = false;
ErrorQueue errorQueue = new ErrorQueue();
try {
String gstr = "grammar U;\n" + "a : A;\n" + "A : a;\n";
Tool tool = new Tool();
tool.removeListeners();
tool.addListener(errorQueue);
assertEquals(0, errorQueue.size());
GrammarRootAST grammarRootAST = tool.parseGrammarFromString(gstr);
assertEquals(0, errorQueue.size());
Grammar g = tool.createGrammar(grammarRootAST);
assertEquals(0, errorQueue.size());
g.fileName = "<string>";
tool.process(g, false);
} catch (Exception e) {
threwException = true;
e.printStackTrace();
}
System.out.println(errorQueue);
assertEquals(1, errorQueue.errors.size());
assertEquals(ErrorType.PARSER_RULE_REF_IN_LEXER_RULE, errorQueue.errors.get(0).getErrorType());
assertEquals("[a, A]", Arrays.toString(errorQueue.errors.get(0).getArgs()));
assertTrue(!threwException);
}
use of org.antlr.v4.Tool in project antlr4 by antlr.
the class BaseNodeTest method canExecute.
private boolean canExecute(String tool) {
try {
ProcessBuilder builder = new ProcessBuilder(tool, "--version");
builder.redirectErrorStream(true);
Process process = builder.start();
StreamVacuum vacuum = new StreamVacuum(process.getInputStream());
vacuum.start();
process.waitFor();
vacuum.join();
return process.exitValue() == 0;
} catch (Exception e) {
;
}
return false;
}
Aggregations