Skip to content

Commit

Permalink
Address further error-prone warnings.
Browse files Browse the repository at this point in the history
Mostly:
 * make final where possible
 * avoid implicit use of platform default charset (also fixes #470).

The charset for skeletons and graphviz dot files is UTF-8 (docs updated), for
spec input/output files, Options.enconding.
  • Loading branch information
lsf37 committed Jan 2, 2020
1 parent c6bfaa6 commit 1d46401
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 97 deletions.
12 changes: 6 additions & 6 deletions docs/md/ant-task.md
Expand Up @@ -41,29 +41,29 @@ The following attributes are available for invoking the JFlex task.
be written to `{destdir}/`**`{packagename}`**. This behaviour is similar
to `javac -d dir.

- `outdir="dir"`\
- `outdir="dir"`\
The directory to write the generated files to. If not set, the files are
written to the directory containing the grammar file. This options works
exactly like JFlex's `-d` command line option, it causes the output file
to be written to `dir` regardless of the package name.

- `verbose` (default `"off"`)\
Display generation process messages.
Display generation process messages.

- `encoding` (if unset uses the JVM default encoding)\
The character encoding to use when reading lexer specifications and writing java files.

- `dump` (default `"off"`)\
Dump character classes, NFA and DFA tables.
- `dump` (default `"off"`)\
Dump character classes, NFA and DFA tables.

- `time` or `timeStatistics` (default `"off"`)\
Display generation time statistics.

- `nomin` or `skipMinimization` (default `"off"`)\
Skip DFA minimisation step.
Skip DFA minimisation step.

- `skel="file"` or `skeleton="file"`\
Use external skeleton file.
Use external skeleton file in UTF-8 encoding.

- `dot` or `generateDot` (default `"off"`)\
Write graphviz `.dot` files for the generated automata.
Expand Down
60 changes: 30 additions & 30 deletions docs/md/installing.md
Expand Up @@ -8,35 +8,35 @@ Installing JFlex

To install JFlex on Windows, follow these three steps:

1. Unzip the file you downloaded into the directory you want JFlex in.
1. Unzip the file you downloaded into the directory you want JFlex in.
If you unzipped it to say `C:\`, the following directory structure
should be generated:

```
C:\jflex-$VERSION\
+--bin\ (start scripts)
+--doc\ (FAQ and manual)
+--examples\
+--byaccj\ (calculator example for BYacc/J)
+--cup-maven\ (calculator example for cup and maven)
+--interpreter\ (interpreter example for cup)
+--java\ (Java lexer specification)
C:\jflex-$VERSION\
+--bin\ (start scripts)
+--doc\ (FAQ and manual)
+--examples\
+--byaccj\ (calculator example for BYacc/J)
+--cup-maven\ (calculator example for cup and maven)
+--interpreter\ (interpreter example for cup)
+--java\ (Java lexer specification)
+--simple\ (example scanner with no parser)
+--standalone-maven\ (a simple standalone scanner,
built with maven)
+--zero-reader\ (Readers that return 0 characters)
+--lib\ (precompiled classes)
+--src\
+--main\
+--config\ (PMD source analyzer configuration)
+--cup\ (JFlex parser spec)
+--java\
+--jflex\ (source code of JFlex)
+--anttask\ (source code of JFlex Ant Task)
+--gui\ (source code of JFlex UI classes)
+--unicode\ (source code for Unicode properties)
+--jflex\ (JFlex scanner spec)
+--resources\ (messages and default skeleton file)
+--standalone-maven\ (a simple standalone scanner,
built with maven)
+--zero-reader\ (Readers that return 0 characters)
+--lib\ (precompiled classes)
+--src\
+--main\
+--config\ (PMD source analyzer configuration)
+--cup\ (JFlex parser spec)
+--java\
+--jflex\ (source code of JFlex)
+--anttask\ (source code of JFlex Ant Task)
+--gui\ (source code of JFlex UI classes)
+--unicode\ (source code for Unicode properties)
+--jflex\ (JFlex scanner spec)
+--resources\ (messages and default skeleton file)
+--test\ (unit tests)
```

Expand Down Expand Up @@ -118,11 +118,11 @@ uses the character encoding `<name>` (e.g. `utf-8`) to read lexer
specifications and write java files.

`--skel <file>`\
uses external skeleton `<file>`. This is mainly for JFlex maintenance
and special low level customisations. Use only when you know what you
are doing! JFlex comes with a skeleton file in the `src` directory that
reflects exactly the internal, pre-compiled skeleton and can be used
with the `-skel` option.
uses external skeleton `<file>` in UTF-8 encoding. This is mainly for JFlex
maintenance and special low level customisations. Use only when you know what
you are doing! JFlex comes with a skeleton file in the `src` directory that
reflects exactly the internal, pre-compiled skeleton and can be used with the
`-skel` option.

`--nomin`\
skip the DFA minimisation step during scanner generation.
Expand Down
8 changes: 5 additions & 3 deletions jflex/src/main/java/jflex/anttask/JFlexTask.java
Expand Up @@ -11,9 +11,11 @@
package jflex.anttask;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jflex.core.OptionUtils;
Expand Down Expand Up @@ -93,8 +95,8 @@ public void findPackageAndClass() throws IOException {
// find name of the package and class in jflex source file
packageName = null;
className = null;

try (LineNumberReader reader = new LineNumberReader(new FileReader(inputFile))) {
Reader r = Files.newBufferedReader(inputFile.toPath(), StandardCharsets.UTF_8);
try (LineNumberReader reader = new LineNumberReader(r)) {
while (className == null || packageName == null) {
String line = reader.readLine();
if (line == null) {
Expand Down
6 changes: 3 additions & 3 deletions jflex/src/main/java/jflex/core/AbstractLexScan.java
@@ -1,7 +1,7 @@
package jflex.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
Expand Down Expand Up @@ -194,7 +194,7 @@ void includeFile(String filePath) {
files.push(file);
file = f;
Out.println("Including \"" + file + "\"");
} catch (FileNotFoundException e) {
} catch (IOException e) {
throw new ScannerException(file, ErrorMessages.NOT_READABLE, lexLine());
}
}
Expand Down Expand Up @@ -387,5 +387,5 @@ public boolean isColumnCount() {
protected abstract String lexText();

@SuppressWarnings("WeakerAccess") // Implemented by generated LexScan
protected abstract void lexPushStream(File f) throws FileNotFoundException;
protected abstract void lexPushStream(File f) throws IOException;
}
2 changes: 1 addition & 1 deletion jflex/src/main/java/jflex/core/unicode/CharClasses.java
Expand Up @@ -28,7 +28,7 @@ public class CharClasses {
/** debug flag (for char classes only) */
private static final boolean DEBUG = false;

private Comparator<IntCharSet> INT_CHAR_SET_COMPARATOR = new IntCharSetComparator();
private static final Comparator<IntCharSet> INT_CHAR_SET_COMPARATOR = new IntCharSetComparator();

/** the largest character that can be used in char classes */
public static final int maxChar = 0x10FFFF;
Expand Down
72 changes: 35 additions & 37 deletions jflex/src/main/java/jflex/core/unicode/IntCharSet.java
Expand Up @@ -34,7 +34,7 @@ public final class IntCharSet implements Iterable<Integer> {
private static final boolean DEBUG = false;

/* invariant: all intervals are disjoint, ordered */
private List<Interval> intervals = new ArrayList<>();
private final List<Interval> intervals = new ArrayList<>();

/** Creates a charset that contains only one interval. */
public static IntCharSet of(Interval interval) {
Expand Down Expand Up @@ -167,50 +167,48 @@ public void add(Interval interval) {
for (int i = 0; i < size; i++) {
Interval elem = intervals.get(i);

if (elem.end + 1 < interval.start) {
continue;
}

if (elem.contains(interval)) {
if (DEBUG) assert invariants();
return;
}

if (elem.start > interval.end + 1) {
intervals.add(i, Interval.copyOf(interval));
if (DEBUG) assert invariants();
return;
}

if (interval.start < elem.start) {
elem.start = interval.start;
}
if (elem.end + 1 >= interval.start) {
if (elem.contains(interval)) {
if (DEBUG) assert invariants();
return;
}

if (interval.end <= elem.end) {
if (DEBUG) assert invariants();
return;
}
if (elem.start > interval.end + 1) {
intervals.add(i, Interval.copyOf(interval));
if (DEBUG) assert invariants();
return;
}

elem.end = interval.end;
if (interval.start < elem.start) {
elem.start = interval.start;
}

i++;
// delete all x with x.contains( interval.end )
while (i < size) {
Interval x = intervals.get(i);
if (x.start > elem.end + 1) {
if (interval.end <= elem.end) {
if (DEBUG) assert invariants();
return;
}

if (x.end > elem.end) {
elem.end = x.end;
elem.end = interval.end;

i++;
// delete all x with x.contains( interval.end )
while (i < size) {
Interval x = intervals.get(i);
if (x.start > elem.end + 1) {
if (DEBUG) assert invariants();
return;
}

if (x.end > elem.end) {
elem.end = x.end;
}
intervals.remove(i);
size--;
}
intervals.remove(i);
size--;
}

if (DEBUG) assert invariants();
return;
if (DEBUG) assert invariants();
return;
}
}

intervals.add(Interval.copyOf(interval));
Expand Down Expand Up @@ -611,7 +609,7 @@ Interval getFirstInterval() {
/** Iterator for enumerating the elements of this IntCharSet */
public class IntCharSetIterator implements PrimitiveIterator.OfInt {
/** Iterator over the Interval list */
private Iterator<Interval> intervalsIterator;
private final Iterator<Interval> intervalsIterator;
/** Iterator within the current Interval */
private IntervalIterator current;

Expand Down
Expand Up @@ -29,7 +29,7 @@ public class UnicodeProperties {
private static final Pattern WORD_SEP_PATTERN = Pattern.compile("[-_\\s()]");

private int maximumCodePoint;
private Map<String, IntCharSet> propertyValueIntervals = new HashMap<>();
private final Map<String, IntCharSet> propertyValueIntervals = new HashMap<>();
private String caselessMatchPartitions;
private int caselessMatchPartitionSize;
private IntCharSet caselessMatches[];
Expand Down
8 changes: 6 additions & 2 deletions jflex/src/main/java/jflex/dfa/DFA.java
Expand Up @@ -10,9 +10,11 @@
package jflex.dfa;

import java.io.File;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -252,7 +254,9 @@ private static boolean tableEquals(int[][] a, int[][] b) {
*/
public void writeDot(File file) {
try {
PrintWriter writer = new PrintWriter(new FileWriter(file));
PrintWriter writer =
new PrintWriter(
new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
writer.println(dotFormat());
writer.close();
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion jflex/src/main/java/jflex/exceptions/SilentExit.java
Expand Up @@ -18,7 +18,7 @@
public class SilentExit extends Exception {

/** Program exit code if this exception is taken */
private int exitCode;
private final int exitCode;

/**
* SilentExit with specified program exit code.
Expand Down
4 changes: 2 additions & 2 deletions jflex/src/main/java/jflex/logging/Out.java
Expand Up @@ -12,9 +12,9 @@
import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import jflex.base.Build;
import jflex.exceptions.GeneratorException;
import jflex.l10n.ErrorMessages;
Expand Down Expand Up @@ -385,7 +385,7 @@ public static void showPosition(File file, int line) {
* @throws IOException if any error occurs
*/
private static String getLine(File file, int line) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedReader reader = Files.newBufferedReader(file.toPath(), Options.encoding);

String msg = "";

Expand Down
12 changes: 6 additions & 6 deletions jflex/src/main/java/jflex/skeleton/Skeleton.java
Expand Up @@ -41,7 +41,7 @@
public class Skeleton {

/** location of default skeleton */
private static final String DEFAULT_LOC = "jflex/skeleton.default"; // $NON-NLS-1$
private static final String DEFAULT_LOC = "jflex/skeleton.default";

/** expected number of sections in the skeleton file */
private static final int size = 21;
Expand All @@ -59,7 +59,7 @@ public class Skeleton {
private int pos;

/** The writer to write the skeleton-parts to */
private PrintWriter out;
private final PrintWriter out;

/**
* Creates a new skeleton (iterator) instance.
Expand All @@ -82,7 +82,7 @@ public void emitNext() {
*/
public static void makePrivate() {
for (int i = 0; i < line.length; i++) {
line[i] = replace(" public ", " private ", line[i]); // $NON-NLS-1$ //$NON-NLS-2$
line[i] = replace(" public ", " private ", line[i]);
}
}

Expand All @@ -92,8 +92,7 @@ public static void makePrivate() {
* @param skeletonFile the file to read (must be != null and readable)
*/
public static void readSkelFile(File skeletonFile) {
if (skeletonFile == null)
throw new IllegalArgumentException("Skeleton file must not be null"); // $NON-NLS-1$
if (skeletonFile == null) throw new IllegalArgumentException("Skeleton file must not be null");

if (!skeletonFile.isFile() || !skeletonFile.canRead()) {
Out.error(ErrorMessages.CANNOT_READ_SKEL, skeletonFile.toString());
Expand Down Expand Up @@ -189,7 +188,8 @@ public static void readDefault() {
throw new GeneratorException();
}

try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) {
readSkel(reader);
} catch (IOException e) {
Out.error(ErrorMessages.SKEL_IO_ERROR_DEFAULT);
Expand Down

0 comments on commit 1d46401

Please sign in to comment.