From 593da3df5ef09d708174f9ac63025faca4ef8e66 Mon Sep 17 00:00:00 2001 From: Mike Lischke Date: Sun, 22 Oct 2017 12:27:49 +0200 Subject: [PATCH] Reapplied changes that got lost during merge. --- tool/src/org/antlr/v4/Tool.java | 26 +++---------------- .../org/antlr/v4/parse/TokenVocabParser.java | 18 ++++++++++++- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/tool/src/org/antlr/v4/Tool.java b/tool/src/org/antlr/v4/Tool.java index 42ace2ecb..6638ff5a1 100644 --- a/tool/src/org/antlr/v4/Tool.java +++ b/tool/src/org/antlr/v4/Tool.java @@ -761,18 +761,11 @@ public class Tool { 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)); @@ -781,21 +774,8 @@ public class Tool { // -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 diff --git a/tool/src/org/antlr/v4/parse/TokenVocabParser.java b/tool/src/org/antlr/v4/parse/TokenVocabParser.java index 4267a8d96..cf84a923a 100644 --- a/tool/src/org/antlr/v4/parse/TokenVocabParser.java +++ b/tool/src/org/antlr/v4/parse/TokenVocabParser.java @@ -146,6 +146,22 @@ public class TokenVocabParser { // 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); } }