Skip to content

Commit

Permalink
fold in Mike Lischke's PR antlr#2065 to make the output directory exa…
Browse files Browse the repository at this point in the history
…ct if you use a new option `-Xexact-output-dir`. Fixes antlr#1087, Fixes antlr#753, Fixes antlr#638.
  • Loading branch information
parrt committed Nov 4, 2017
1 parent 5665502 commit 121e767
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions tool/src/org/antlr/v4/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public Option(String fieldName, String name, OptionArgType argType, String descr
public Map<String, String> grammarOptions = null;
public boolean warnings_are_errors = false;
public boolean longMessages = false;
public boolean exact_output_dir = false;

public static Option[] optionDefs = {
new Option("outputDirectory", "-o", OptionArgType.STRING, "specify output directory where all output is generated"),
Expand All @@ -134,7 +135,8 @@ public Option(String fieldName, String name, OptionArgType argType, String descr
new Option("launch_ST_inspector", "-XdbgST", "launch StringTemplate visualizer on generated code"),
new Option("ST_inspector_wait_for_close", "-XdbgSTWait", "wait for STViz to close before continuing"),
new Option("force_atn", "-Xforce-atn", "use the ATN simulator for all predictions"),
new Option("log", "-Xlog", "dump lots of logging info to antlr-timestamp.log"),
new Option("log", "-Xlog", "dump lots of logging info to antlr-timestamp.log"),
new Option("exact_output_dir", "-Xexact-output-dir", "all output goes into -o dir regardless of paths/package"),
};

// helper vars for option management
Expand Down Expand Up @@ -758,6 +760,61 @@ public File getImportedGrammarFile(Grammar g, String fileName) {
* @param fileNameWithPath path to input source
*/
public File getOutputDirectory(String fileNameWithPath) {
if ( exact_output_dir ) {
return new_getOutputDirectory(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 ""
// but when it is not, we will write the file in the correct place.
fileDirectory = ".";

}
else {
fileDirectory = fileNameWithPath.substring(0, fileNameWithPath.lastIndexOf(File.separatorChar));
}
if ( haveOutputDir ) {
// -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.java
if (fileDirectory != null) {
outputDir = new File(outputDirectory, fileDirectory);
}
else {
outputDir = new File(outputDirectory);
}
}
}
else {
// they didn't specify a -o dir so just write to location
// where grammar is, absolute or relative, this will only happen
// with command line invocation as build tools will always
// supply an output directory.
outputDir = new File(fileDirectory);
}
return outputDir;
}

/** @since 4.7.1 in response to -Xexact-output-dir */
public File new_getOutputDirectory(String fileNameWithPath) {
File outputDir;
String fileDirectory;

Expand All @@ -774,7 +831,7 @@ 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
// -o /tmp subdir/t.g4 => /tmp/t.g4
// -o /tmp subdir/t.g4 => /tmp/T.java
outputDir = new File(outputDirectory);
}
else {
Expand Down

5 comments on commit 121e767

@mike-lischke
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm missing the changes in the TokenVocabParser.java file. These are important as well to improve *.tokens import. It makes it possible to load .tokens files from the grammar's folder (think of predefined tokens, not generated ones).

@parrt
Copy link
Owner Author

@parrt parrt commented on 121e767 Nov 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird, those didn't make it? crap. ok, let me look.

@parrt
Copy link
Owner Author

@parrt parrt commented on 121e767 Nov 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mike-lischke Seems to be there in the master branch: https://github.com/antlr/antlr4/blob/master/tool/src/org/antlr/v4/parse/TokenVocabParser.java Are those the changes you're talking about

@mike-lischke
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, I checked the code (did a fresh pull) to see if everything is there and it wasn't. Hence my comment. But now it looks all ok.

@parrt
Copy link
Owner Author

@parrt parrt commented on 121e767 Nov 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool. yeah, all the branching can make it confusing.

Please sign in to comment.