Skip to content
/ jflex Public
forked from jflex-de/jflex

Commit

Permalink
Split Main in Main and LexGenerator.
Browse files Browse the repository at this point in the history
This is a minor refactoring to support refacotring  jflex-de#214
In the future, LexGenerator.generate() will not be static and have internal state in order to support multithreading.
  • Loading branch information
regisd committed Oct 10, 2018
1 parent 986b267 commit afe31a8
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 107 deletions.
131 changes: 131 additions & 0 deletions jflex/src/main/java/jflex/LexGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* JFlex 1.7.1-SNAPSHOT *
* Copyright (C) 1998-2018 Gerwin Klein <lsf@jflex.de> *
* All rights reserved. *
* *
* License: BSD *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package jflex;

import static jflex.Options.encoding;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* This is the main class of JFlex controlling the scanner generation process. It is responsible for
* parsing the commandline, getting input files, starting up the GUI if necessary, etc.
*
* @author Gerwin Klein
* @author Régis Décamps
* @version JFlex 1.7.1-SNAPSHOT
*/
public class LexGenerator {

/** JFlex version */
public static final String version = "1.7.1-SNAPSHOT"; // $NON-NLS-1$

/**
* Generates a scanner for the specified input file.
*
* @param inputFile a file containing a lexical specification to generate a scanner for.
*/
public static void generate(File inputFile) {

Out.resetCounters();

Timer totalTime = new Timer();
Timer time = new Timer();

LexScan scanner = null;
LexParse parser = null;
Reader inputReader = null;

totalTime.start();

try {
Out.println(ErrorMessages.READING, inputFile.toString());
inputReader =
new InputStreamReader(Files.newInputStream(Paths.get(inputFile.toString())), encoding);
scanner = new LexScan(inputReader);
scanner.setFile(inputFile);
parser = new LexParse(scanner);
} catch (IOException e) {
Out.error(ErrorMessages.CANNOT_OPEN, inputFile.toString());
throw new GeneratorException();
}

try {
NFA nfa = (NFA) parser.parse().value;

Out.checkErrors();

if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.NFA_IS) + Out.NL + nfa + Out.NL);

if (Options.dot) nfa.writeDot(Emitter.normalize("nfa.dot", null)); // $NON-NLS-1$

Out.println(ErrorMessages.NFA_STATES, nfa.numStates);

time.start();
DFA dfa = nfa.getDFA();
time.stop();
Out.time(ErrorMessages.DFA_TOOK, time);

dfa.checkActions(scanner, parser);

nfa = null;

if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.DFA_IS) + Out.NL + dfa + Out.NL);

if (Options.dot) dfa.writeDot(Emitter.normalize("dfa-big.dot", null)); // $NON-NLS-1$

Out.checkErrors();

time.start();
dfa.minimize();
time.stop();

Out.time(ErrorMessages.MIN_TOOK, time);

if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.MIN_DFA_IS) + Out.NL + dfa);

if (Options.dot) dfa.writeDot(Emitter.normalize("dfa-min.dot", null)); // $NON-NLS-1$

time.start();

Emitter e = new Emitter(inputFile, parser, dfa);
e.emit();

time.stop();

Out.time(ErrorMessages.WRITE_TOOK, time);

totalTime.stop();

Out.time(ErrorMessages.TOTAL_TIME, totalTime);
} catch (ScannerException e) {
Out.error(e.file, e.message, e.line, e.column);
throw new GeneratorException();
} catch (MacroException e) {
Out.error(e.getMessage());
throw new GeneratorException();
} catch (IOException e) {
Out.error(ErrorMessages.IO_ERROR, e.toString());
throw new GeneratorException();
} catch (OutOfMemoryError e) {
Out.error(ErrorMessages.OUT_OF_MEMORY);
throw new GeneratorException();
} catch (GeneratorException e) {
throw new GeneratorException();
} catch (Exception e) {
e.printStackTrace();
throw new GeneratorException();
}
}
}
112 changes: 5 additions & 107 deletions jflex/src/main/java/jflex/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@
package jflex;

import static jflex.ErrorMessages.NO_ENCODING;
import static jflex.Options.encoding;
import static jflex.Options.setEncoding;
import static jflex.Options.unused_warning;
import static jflex.Out.error;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -48,112 +42,14 @@ public class Main {
/** JFlex version */
public static final String version = "1.7.1-SNAPSHOT"; // $NON-NLS-1$

/**
* Generates a scanner for the specified input file.
*
* @param inputFile a file containing a lexical specification to generate a scanner for.
*/
public static void generate(File inputFile) {

Out.resetCounters();

Timer totalTime = new Timer();
Timer time = new Timer();

LexScan scanner = null;
LexParse parser = null;
Reader inputReader = null;

totalTime.start();

try {
Out.println(ErrorMessages.READING, inputFile.toString());
inputReader =
new InputStreamReader(Files.newInputStream(Paths.get(inputFile.toString())), encoding);
scanner = new LexScan(inputReader);
scanner.setFile(inputFile);
parser = new LexParse(scanner);
} catch (IOException e) {
Out.error(ErrorMessages.CANNOT_OPEN, inputFile.toString());
throw new GeneratorException();
}

try {
NFA nfa = (NFA) parser.parse().value;

Out.checkErrors();

if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.NFA_IS) + Out.NL + nfa + Out.NL);

if (Options.dot) nfa.writeDot(Emitter.normalize("nfa.dot", null)); // $NON-NLS-1$

Out.println(ErrorMessages.NFA_STATES, nfa.numStates);

time.start();
DFA dfa = nfa.getDFA();
time.stop();
Out.time(ErrorMessages.DFA_TOOK, time);

dfa.checkActions(scanner, parser);

nfa = null;

if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.DFA_IS) + Out.NL + dfa + Out.NL);

if (Options.dot) dfa.writeDot(Emitter.normalize("dfa-big.dot", null)); // $NON-NLS-1$

Out.checkErrors();

time.start();
dfa.minimize();
time.stop();

Out.time(ErrorMessages.MIN_TOOK, time);

if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.MIN_DFA_IS) + Out.NL + dfa);

if (Options.dot) dfa.writeDot(Emitter.normalize("dfa-min.dot", null)); // $NON-NLS-1$

time.start();

Emitter e = new Emitter(inputFile, parser, dfa);
e.emit();

time.stop();

Out.time(ErrorMessages.WRITE_TOOK, time);

totalTime.stop();

Out.time(ErrorMessages.TOTAL_TIME, totalTime);
} catch (ScannerException e) {
Out.error(e.file, e.message, e.line, e.column);
throw new GeneratorException();
} catch (MacroException e) {
Out.error(e.getMessage());
throw new GeneratorException();
} catch (IOException e) {
Out.error(ErrorMessages.IO_ERROR, e.toString());
throw new GeneratorException();
} catch (OutOfMemoryError e) {
Out.error(ErrorMessages.OUT_OF_MEMORY);
throw new GeneratorException();
} catch (GeneratorException e) {
throw new GeneratorException();
} catch (Exception e) {
e.printStackTrace();
throw new GeneratorException();
}
}

/**
* parseOptions.
*
* @param argv an array of {@link java.lang.String} objects.
* @return a {@link java.util.List} object.
* @throws jflex.SilentExit if any.
*/
public static List<File> parseOptions(String argv[]) throws SilentExit {
private static List<File> parseOptions(String argv[]) throws SilentExit {
List<File> files = new ArrayList<>();

for (int i = 0; i < argv.length; i++) {
Expand Down Expand Up @@ -377,7 +273,7 @@ private static void printUnicodePropertyValuesAndAliases(String unicodeVersion)
}

/** Prints the cli usage on stdout. */
public static void printUsage() {
private static void printUsage() {
Out.println(""); // $NON-NLS-1$
Out.println("Usage: jflex <options> <input-files>");
Out.println("");
Expand Down Expand Up @@ -418,7 +314,9 @@ public static void generate(String argv[]) throws SilentExit {
List<File> files = parseOptions(argv);

if (files.size() > 0) {
for (File file : files) generate(file);
for (File file : files) {
LexGenerator.generate(file);
}
} else {
new MainFrame();
}
Expand Down

0 comments on commit afe31a8

Please sign in to comment.