Cleaned up RewriterCodeGen and added setDestinationPackage.

Allow client code to provide a destination package for generated code and removed some (now dead) code for RewriterCodeGenerator.
This commit is contained in:
A Pottinger
2019-10-13 11:47:35 -07:00
parent 96e4f0a203
commit fdcaf7d932
4 changed files with 44 additions and 57 deletions

View File

@@ -56,6 +56,7 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
private String sketchName;
private boolean isTesting;
private TokenStreamRewriter rewriter;
private Optional<String> destinationPackageName;
private Mode mode = Mode.JAVA;
private boolean foundMain;
@@ -76,7 +77,6 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
private boolean sizeIsFullscreen = false;
private RewriteResult headerResult;
private RewriteResult footerResult;
private RewriterCodeGenerator codeGen;
private String indent1;
private String indent2;
@@ -90,13 +90,17 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
* @param tokens The tokens over which to rewrite.
* @param newSketchName The name of the sketch being traversed.
* @param newTabSize Size of tab / indent.
* @param newDestinationPackageName The package to which generated code should be assigned (the
* package to which the sketch code java file should be assigned).
*/
public PdeParseTreeListener(TokenStream tokens, String newSketchName, int newTabSize) {
public PdeParseTreeListener(TokenStream tokens, String newSketchName, int newTabSize,
Optional<String> newDestinationPackageName) {
rewriter = new TokenStreamRewriter(tokens);
sketchName = newSketchName;
tabSize = newTabSize;
destinationPackageName = newDestinationPackageName;
codeGen = new RewriterCodeGenerator(tabSize);
pdeParseTreeErrorListenerMaybe = Optional.empty();
final char[] indentChars = new char[newTabSize];
@@ -801,7 +805,15 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
protected void writeHeaderContents(PrintWriterWithEditGen decoratedWriter, RewriteParams params,
RewriteResultBuilder resultBuilder) {
if (!params.getisTesting()) writePreprocessorComment(decoratedWriter, params, resultBuilder);
if (!params.getIsTesting()) {
writePreprocessorComment(decoratedWriter, params, resultBuilder);
}
if (destinationPackageName.isPresent()) {
decoratedWriter.addCodeLine("package " + destinationPackageName.get() + ";");
decoratedWriter.addEmptyLine();
}
writeImports(decoratedWriter, params, resultBuilder);
PdePreprocessor.Mode mode = params.getMode();

View File

@@ -77,6 +77,7 @@ public class PdePreprocessor {
private final ParseTreeListenerFactory listenerFactory;
private final List<String> defaultImports;
private final List<String> coreImports;
private final Optional<String> destinationPackage;
private boolean foundMain;
@@ -110,7 +111,7 @@ public class PdePreprocessor {
*/
public PdePreprocessor(final String newSketchName, final int newTabSize, boolean newIsTesting,
final ParseTreeListenerFactory newFactory, List<String> newDefaultImports,
List<String> newCoreImports) {
List<String> newCoreImports, Optional<String> newDestinationPackage) {
sketchName = newSketchName;
tabSize = newTabSize;
@@ -118,6 +119,7 @@ public class PdePreprocessor {
listenerFactory = newFactory;
defaultImports = newDefaultImports;
coreImports = newCoreImports;
destinationPackage = newDestinationPackage;
}
/**
@@ -185,7 +187,12 @@ public class PdePreprocessor {
// Parser
final List<PdePreprocessIssue> preprocessIssues = new ArrayList<>();
final List<PdePreprocessIssue> treeIssues = new ArrayList<>();
PdeParseTreeListener listener = listenerFactory.build(tokens, sketchName, tabSize);
PdeParseTreeListener listener = listenerFactory.build(
tokens,
sketchName,
tabSize,
Optional.empty()
);
listener.setTesting(isTesting);
listener.setCoreImports(coreImports);
listener.setDefaultImports(defaultImports);
@@ -279,6 +286,7 @@ public class PdePreprocessor {
private Optional<ParseTreeListenerFactory> parseTreeFactory;
private Optional<List<String>> defaultImports;
private Optional<List<String>> coreImports;
private Optional<String> destinationPackage;
/**
* The imports required for the Java processing mode.
@@ -333,6 +341,7 @@ public class PdePreprocessor {
parseTreeFactory = Optional.empty();
defaultImports = Optional.empty();
coreImports = Optional.empty();
destinationPackage = Optional.empty();
}
/**
@@ -396,6 +405,17 @@ public class PdePreprocessor {
return this;
}
/**
* Specify to which package generated code should be assigned.
*
* @param newDestinationPackage The package to which output code should be assigned.
* @return This builder for method chaining.
*/
public PdePreprocessorBuilder setDestinationPackage(String newDestinationPackage) {
destinationPackage = Optional.of(newDestinationPackage);
return this;
}
/**
* Build the preprocessor.
*
@@ -426,7 +446,8 @@ public class PdePreprocessor {
effectiveIsTesting,
effectiveFactory,
effectiveDefaultImports,
effectiveCoreImports
effectiveCoreImports,
destinationPackage
);
}
@@ -448,9 +469,11 @@ public class PdePreprocessor {
* @param tokens The token stream with sketch code contents.
* @param sketchName The name of the sketch that will be preprocessed.
* @param tabSize The size (number of spaces) of the tabs.
* @param packageName The optional package name for generated code.
* @return The newly created listener.
*/
PdeParseTreeListener build(CommonTokenStream tokens, String sketchName, int tabSize);
PdeParseTreeListener build(CommonTokenStream tokens, String sketchName, int tabSize,
Optional<String> packageName);
}
@@ -460,17 +483,6 @@ public class PdePreprocessor {
* ==================================
*/
/**
* Factory function to create a {PdeParseTreeListener} for use in preprocessing
*
* @param tokens The token stream for which the listener needs to be created.
* @param sketchName The name of the sketch being preprocessed.
* @return Newly created listener suitable for use in this {PdePreprocessor}.
*/
private PdeParseTreeListener createListener(CommonTokenStream tokens, String sketchName) {
return new PdeParseTreeListener(tokens, sketchName, tabSize);
}
/**
* Utility function to substitute non ascii characters for escaped unicode character sequences.
*

View File

@@ -101,7 +101,7 @@ public class RewriteParams {
*
* @return Flag indicating if this is being run as part of automated testing.
*/
public boolean getisTesting() {
public boolean getIsTesting() {
return isTesting;
}

View File

@@ -1,37 +0,0 @@
package processing.mode.java.preproc.code;
import org.antlr.v4.runtime.TokenStreamRewriter;
import processing.app.Preferences;
import processing.core.PApplet;
import processing.mode.java.preproc.PdePreprocessor;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;
/**
* Utility to rewrite code as part of preprocessing.
*/
public class RewriterCodeGenerator {
private final String indent1;
private final String indent2;
private final String indent3;
/**
* Create a new rewriter.
*
* @param indentSize Number of spaces in the indent.
*/
public RewriterCodeGenerator(int indentSize) {
final char[] indentChars = new char[indentSize];
Arrays.fill(indentChars, ' ');
indent1 = new String(indentChars);
indent2 = indent1 + indent1;
indent3 = indent2 + indent1;
}
}