Skip to content

Commit b9fa958

Browse files
authored
Indicate the lex source file in the header of the java source (#371)
* Indicate in the source lexFile in the header of the java source - Emit in the header of the generated java source the path of the lex file. - Do not show the working directory, only the relative path to the working directory. * Set the source directory in the jflex-maven-plugin e.g. ``` // DO NOT EDIT // Generated by JFlex 1.7.1-SNAPSHOT // source: src/main/jflex/lcalc.flex ```
1 parent d0b757f commit b9fa958

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

jflex-maven-plugin/src/main/java/de/jflex/plugin/maven/JFlexMojo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ private void parseLexFile(File lexFile) throws MojoFailureException, MojoExecuti
215215
// set options. Very strange that JFlex expects this in a static way.
216216
Options.setDefaults();
217217
Options.setDir(generatedFile.getParentFile());
218+
Options.setRootDirectory(project.getBasedir());
218219
Options.dump = dump;
219220
Options.verbose = verbose;
220221
Options.unused_warning = unusedWarning;

jflex/src/main/java/jflex/Emitter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020
import java.util.regex.Matcher;
2121
import java.util.regex.Pattern;
22+
import jflex.io.FileUtil;
2223

2324
/**
2425
* This class manages the actual code generation, putting the scanner together, filling in skeleton
@@ -406,7 +407,9 @@ private void emitNextInput() {
406407
}
407408

408409
private void emitHeader() {
409-
println("/* The following code was generated by JFlex " + Main.version + " */");
410+
println("// DO NOT EDIT");
411+
println("// Generated by JFlex " + Main.version);
412+
println("// source: " + FileUtil.getRelativePath(Options.getRootDirectory(), inputFile));
410413
println("");
411414
}
412415

jflex/src/main/java/jflex/Options.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public class Options {
2525

2626
/** output directory */
2727
private static File directory;
28+
/**
29+
* The root source directory.
30+
*
31+
* <p>In a maven project, this is the directory that contains {@code src} and {@code target}.
32+
*/
33+
private static File rootDirectory;
2834
/** strict JLex compatibility */
2935
public static boolean jlex;
3036
/** don't run minimization algorithm if this is true */
@@ -91,6 +97,18 @@ public static void setDir(File d) {
9197
directory = d;
9298
}
9399

100+
/**
101+
* Returns the root directory that contains source code. This is the java working (from system
102+
* property {@code user.dir}) by default.
103+
*/
104+
public static File getRootDirectory() {
105+
return rootDirectory;
106+
}
107+
108+
public static void setRootDirectory(File rootDir) {
109+
rootDirectory = rootDir;
110+
}
111+
94112
/** Sets encoding for input files, and check availability of encoding on this JVM. */
95113
public static void setEncoding(String encodingName) {
96114
if (Charset.isSupported(encodingName)) {
@@ -104,6 +122,8 @@ public static void setEncoding(String encodingName) {
104122
/** Sets all options back to default values. */
105123
public static void setDefaults() {
106124
directory = null;
125+
// System.getProperty("user.dir"), the directory where java was run from.
126+
rootDirectory = new File("");
107127
jlex = false;
108128
no_minimize = false;
109129
no_backup = false;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package jflex.io;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
public class FileUtil {
7+
8+
/** Returns the path of {@code file} relative to {@code rootDirectory}. */
9+
public static String getRelativePath(File rootDirectory, File file) {
10+
try {
11+
String rootDir = rootDirectory.getCanonicalPath() + File.separator;
12+
String f = file.getCanonicalPath();
13+
if (f.startsWith(rootDir)) {
14+
return f.substring(rootDir.length());
15+
}
16+
} catch (IOException e) {
17+
// fall back to file.getPath()
18+
}
19+
return file.getPath();
20+
}
21+
22+
private FileUtil() {} // utility class
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package jflex.io;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.File;
6+
import junit.framework.TestCase;
7+
import org.junit.Test;
8+
9+
public class FileUtilTest extends TestCase {
10+
11+
@Test
12+
public void test_getRelativePath_fileInDir() {
13+
File dir = new File("/a/b/c");
14+
File f = new File("/a/b/c/d/foo.bar");
15+
assertEquals("d/foo.bar", FileUtil.getRelativePath(dir, f));
16+
}
17+
18+
@Test
19+
public void test_getRelativePath_fileNotInDir() {
20+
File dir = new File("/a/b/c");
21+
File f = new File("/d/e/f/foo.bar");
22+
assertEquals("/d/e/f/foo.bar", FileUtil.getRelativePath(dir, f));
23+
}
24+
25+
@Test
26+
public void test_getRelativePath_sameStart() {
27+
File dir = new File("/a/b/c");
28+
File f = new File("/a/b/c.txt");
29+
assertEquals("/a/b/c.txt", FileUtil.getRelativePath(dir, f));
30+
}
31+
}

0 commit comments

Comments
 (0)