Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify output path generation; new -Xexact-output-dir option #2065

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions tool/src/org/antlr/v4/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -761,18 +761,11 @@ public File getOutputDirectory(String fileNameWithPath) {
File outputDir;
String fileDirectory;

// Some files are given to us without a PATH but should should
// still be written to the output directory in the relative path of
// the output directory. The file directory is either the set of sub directories
// or just or the relative path recorded for the parent grammar. This means
// that when we write the tokens files, or the .java files for imported grammars
// taht we will write them in the correct place.
if (fileNameWithPath.lastIndexOf(File.separatorChar) == -1) {
// No path is included in the file name, so make the file
// directory the same as the parent grammar (which might sitll be just ""
// directory the same as the parent grammar (which might still be just ""
// but when it is not, we will write the file in the correct place.
fileDirectory = ".";

}
else {
fileDirectory = fileNameWithPath.substring(0, fileNameWithPath.lastIndexOf(File.separatorChar));
Expand All @@ -781,21 +774,8 @@ public File getOutputDirectory(String fileNameWithPath) {
// -o /tmp /var/lib/t.g4 => /tmp/T.java
// -o subdir/output /usr/lib/t.g4 => subdir/output/T.java
// -o . /usr/lib/t.g4 => ./T.java
if (fileDirectory != null &&
(new File(fileDirectory).isAbsolute() ||
fileDirectory.startsWith("~"))) { // isAbsolute doesn't count this :(
// somebody set the dir, it takes precendence; write new file there
outputDir = new File(outputDirectory);
}
else {
// -o /tmp subdir/t.g4 => /tmp/subdir/t.g4
if (fileDirectory != null) {
outputDir = new File(outputDirectory, fileDirectory);
}
else {
outputDir = new File(outputDirectory);
}
}
// -o /tmp subdir/t.g4 => /tmp/t.g4
outputDir = new File(outputDirectory);
}
else {
// they didn't specify a -o dir so just write to location
Expand Down
18 changes: 17 additions & 1 deletion tool/src/org/antlr/v4/parse/TokenVocabParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ public File getImportedVocabFile() {
// files are generated (in the base, not relative to the input
// location.)
f = new File(g.tool.outputDirectory, vocabName + CodeGenerator.VOCAB_FILE_EXTENSION);
return f;
if ( f.exists() ) {
return f;
}

// Still not found? Use the grammar's subfolder then.
String fileDirectory;

if (g.fileName.lastIndexOf(File.separatorChar) == -1) {
// No path is included in the file name, so make the file
// directory the same as the parent grammar (which might still be just ""
// but when it is not, we will write the file in the correct place.
fileDirectory = ".";
}
else {
fileDirectory = g.fileName.substring(0, g.fileName.lastIndexOf(File.separatorChar));
}
return new File(fileDirectory, vocabName + CodeGenerator.VOCAB_FILE_EXTENSION);
}
}