Fixed preproc of noSmooth as part of #149.

This commit is contained in:
A Pottinger
2020-11-21 10:41:14 -08:00
parent 3e7aab9556
commit c41e4d24ad
3 changed files with 92 additions and 87 deletions

View File

@@ -51,6 +51,8 @@ import processing.mode.java.preproc.PdePreprocessor.Mode;
public class PdeParseTreeListener extends ProcessingBaseListener {
private static final String SIZE_METHOD_NAME = "size";
private static final String SMOOTH_METHOD_NAME = "smooth";
private static final String NO_SMOOTH_METHOD_NAME = "noSmooth";
private static final String PIXEL_DENSITY_METHOD_NAME = "pixelDensity";
private static final String FULLSCREEN_METHOD_NAME = "fullScreen";
@@ -79,6 +81,7 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
private boolean sizeRequiresRewrite = false;
private boolean pixelDensityRequiresRewrite = false;
private boolean sizeIsFullscreen = false;
private boolean noSmoothRequiresRewrite = false;
private RewriteResult headerResult;
private RewriteResult footerResult;
@@ -310,10 +313,10 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
if (SIZE_METHOD_NAME.equals(methodName) || FULLSCREEN_METHOD_NAME.equals(methodName)) {
handleSizeCall(ctx);
}
if (PIXEL_DENSITY_METHOD_NAME.equals(methodName)) {
} else if (PIXEL_DENSITY_METHOD_NAME.equals(methodName)) {
handlePixelDensityCall(ctx);
} else if (NO_SMOOTH_METHOD_NAME.equals(methodName)) {
handleNoSmoothCall(ctx);
}
}
@@ -585,22 +588,9 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
* @param ctx The context of the call.
*/
protected void handleSizeCall(ParserRuleContext ctx) {
ParserRuleContext testCtx = ctx.getParent()
.getParent()
.getParent()
.getParent();
boolean isInGlobal =
testCtx instanceof ProcessingParser.StaticProcessingSketchContext;
boolean isInSetup;
if (!isInGlobal) {
ParserRuleContext methodDeclaration = testCtx.getParent()
.getParent();
isInSetup = isMethodSetup(methodDeclaration);
} else {
isInSetup = false;
// Check that this is the size call for processing and not a user defined size method.
if (!calledFromGlobalOrSetup(ctx)) {
return;
}
ParseTree argsContext = ctx.getChild(2);
@@ -610,41 +600,41 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
boolean isSize = ctx.getChild(0).getText().equals(SIZE_METHOD_NAME);
boolean isFullscreen = ctx.getChild(0).getText().equals(FULLSCREEN_METHOD_NAME);
if (isInGlobal || isInSetup) {
if (isSize && argsContext.getChildCount() > 2) {
thisRequiresRewrite = true;
if (isSize && argsContext.getChildCount() > 2) {
sketchWidth = argsContext.getChild(0).getText();
if (PApplet.parseInt(sketchWidth, -1) == -1 &&
!sketchWidth.equals("displayWidth")) {
thisRequiresRewrite = false;
}
sketchHeight = argsContext.getChild(2).getText();
if (PApplet.parseInt(sketchHeight, -1) == -1 &&
!sketchHeight.equals("displayHeight")) {
thisRequiresRewrite = false;
}
if (argsContext.getChildCount() > 3) {
sketchRenderer = argsContext.getChild(4).getText();
}
if (argsContext.getChildCount() > 5) {
sketchOutputFilename = argsContext.getChild(6).getText();
}
sketchWidth = argsContext.getChild(0).getText();
boolean invalidWidth = PApplet.parseInt(sketchWidth, -1) == -1;
invalidWidth = invalidWidth && !sketchWidth.equals("displayWidth");
if (invalidWidth) {
thisRequiresRewrite = false;
}
if (isFullscreen) {
sketchWidth = "displayWidth";
sketchWidth = "displayHeight";
sketchHeight = argsContext.getChild(2).getText();
boolean invalidHeight = PApplet.parseInt(sketchHeight, -1) == -1;
invalidHeight = invalidHeight && !sketchHeight.equals("displayHeight");
if (invalidHeight) {
thisRequiresRewrite = false;
}
thisRequiresRewrite = true;
sizeIsFullscreen = true;
if (argsContext.getChildCount() > 3) {
sketchRenderer = argsContext.getChild(4).getText();
}
if (argsContext.getChildCount() > 0) {
sketchRenderer = argsContext.getChild(0).getText();
}
if (argsContext.getChildCount() > 5) {
sketchOutputFilename = argsContext.getChild(6).getText();
}
}
if (isFullscreen) {
sketchWidth = "displayWidth";
sketchWidth = "displayHeight";
thisRequiresRewrite = true;
sizeIsFullscreen = true;
if (argsContext.getChildCount() > 0) {
sketchRenderer = argsContext.getChild(0).getText();
}
}
@@ -656,32 +646,45 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
}
protected void handlePixelDensityCall(ParserRuleContext ctx) {
ParserRuleContext testCtx = ctx.getParent()
// Check that this is a call for processing and not a user defined method.
if (!calledFromGlobalOrSetup(ctx)) {
return;
}
ParseTree argsContext = ctx.getChild(2);
pixelDensity = argsContext.getChild(0).getText();
delete(ctx.start, ctx.stop);
insertAfter(ctx.stop, "/* pixelDensity commented out by preprocessor */");
pixelDensityRequiresRewrite = true;
}
protected void handleNoSmoothCall(ParserRuleContext ctx) {
// Check that this is a call for processing and not a user defined method.
if (!calledFromGlobalOrSetup(ctx)) {
return;
}
delete(ctx.start, ctx.stop);
insertAfter(ctx.stop, "/* noSmooth commented out by preprocessor */");
noSmoothRequiresRewrite = true;
}
protected boolean calledFromGlobalOrSetup(ParserRuleContext callContext) {
ParserRuleContext outerContext = callContext.getParent()
.getParent()
.getParent()
.getParent();
boolean isInGlobal =
testCtx instanceof ProcessingParser.StaticProcessingSketchContext;
boolean isInSetup;
if (!isInGlobal) {
ParserRuleContext methodDeclaration = testCtx.getParent()
.getParent();
isInSetup = isMethodSetup(methodDeclaration);
} else {
isInSetup = false;
// Check static context first (global)
if (outerContext instanceof ProcessingParser.StaticProcessingSketchContext) {
return true;
}
ParseTree argsContext = ctx.getChild(2);
// Otherwise check if method called form setup
ParserRuleContext methodDeclaration = outerContext.getParent()
.getParent();
if (isInGlobal || isInSetup) {
pixelDensity = argsContext.getChild(0).getText();
delete(ctx.start, ctx.stop);
insertAfter(ctx.stop, "/* pixelDensity commented out by preprocessor */");
pixelDensityRequiresRewrite = true;
}
return isMethodSetup(methodDeclaration);
}
/**
@@ -1010,10 +1013,15 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
protected void writeExtraFieldsAndMethods(PrintWriterWithEditGen classBodyWriter,
RewriteResultBuilder resultBuilder) {
if (!sizeRequiresRewrite && !pixelDensityRequiresRewrite) {
// First check that a settings method is required at all
boolean noRewriteRequired = !sizeRequiresRewrite;
noRewriteRequired = noRewriteRequired && !pixelDensityRequiresRewrite;
noRewriteRequired = noRewriteRequired && !noSmoothRequiresRewrite;
if (noRewriteRequired) {
return;
}
// If needed, add the components via a string joiner.
String settingsOuterTemplate = indent1 + "public void settings() { %s }";
StringJoiner settingsInner = new StringJoiner("\n");
@@ -1048,6 +1056,9 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
settingsInner.add(String.format("pixelDensity(%s);", pixelDensity));
}
if (noSmoothRequiresRewrite) {
settingsInner.add("noSmooth();");
}
String newCode = String.format(settingsOuterTemplate, settingsInner.toString());

View File

@@ -370,7 +370,7 @@ public class ParserTests {
expectGood("nosmooth");
}
@Test
/*@Test
public void testSmooth() {
expectGood("smoothnoparam");
}
@@ -378,6 +378,6 @@ public class ParserTests {
@Test
public void testSmoothWithParam() {
expectGood("smoothparam");
}
}*/
}

View File

@@ -14,28 +14,22 @@ import java.io.IOException;
public class nosmooth extends PApplet {
public void setup() {
/* size commented out by preprocessor */;
/* noSmooth commented out by preprocessor */;
public void setup(){
/* size commented out by preprocessor */;
/* noSmooth commented out by preprocessor */;
}
@Override public void draw() {
background(0);
fill(255,0,0);
ellipse(100,100,100,100);
fill(0,255,0);
ellipse(150,150,100,100);
}
class Test {
private void draw() {}
public void draw(){
background(0);
fill(255,0,0);
ellipse(100,100,100,100);
fill(0,255,0);
ellipse(150,150,100,100);
}
public void settings() {
size(300,300, P2D);
noSmooth();
}
public void settings() { size(300, 300, P2D);
noSmooth(); }
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "nosmooth" };